// File: /workspace/github_c_makefile_star2_O2/icebert[P]pblat/lib/net.c
// Function: netSkipHttpHeaderLinesWithRedirect
boolean netSkipHttpHeaderLinesWithRedirect(int sd, char *url, char **redirectedUrl)
/* Skip http header lines. Return FALSE if there's a problem.
 * The input is a standard sd or fd descriptor.
 * This is meant to be able work even with a re-passable stream handle,
 * e.g. can pass it to the pipes routines, which means we can't
 * attach a linefile since filling its buffer reads in more than just the http header.
 * Handles 300, 301, 302, 303, 307 http redirects by setting *redirectedUrl to
 * the new location. */
{
char buf[2000];
char *line = buf;
int maxbuf = sizeof(buf);
int i=0;
char c = ' ';
int nread = 0;
char *sep = NULL;
char *headerName = NULL;
char *headerVal = NULL;
boolean redirect = FALSE;
boolean byteRangeUsed = (strstr(url,";byterange=") != NULL);
ssize_t byteRangeStart = -1;
ssize_t byteRangeEnd = -1;
boolean foundContentRange = FALSE;
ssize_t contentRangeStart = -1;
ssize_t contentRangeEnd = -1;

boolean mustUseProxy = FALSE;  /* User must use proxy 305 error*/
char *proxyLocation = NULL;
boolean mustUseProxyAuth = FALSE;  /* User must use proxy authentication 407 error*/

if (byteRangeUsed)
    {
    parseByteRange(url, &byteRangeStart, &byteRangeEnd, FALSE);
    }

while(TRUE)
    {
    i = 0;
    while (TRUE)
	{
	nread = read(sd, &c, 1);  /* one char at a time, but http headers are small */
	if (nread != 1)
	    {
	    if (nread == -1)
		{
		if (errno == EINTR)
		    continue;
    		warn("Error (%s) reading http header on %s", strerror(errno), url);
		}
	    else if (nread == 0)
    		warn("Error unexpected end of input reading http header on %s", url);
	    else
    		warn("Error reading http header on %s", url);
	    return FALSE;  /* err reading descriptor */
	    }
	if (c == 10)
	    break;
	if (c != 13)
    	    buf[i++] = c;
	if (i >= maxbuf)
	    {
	    warn("http header line too long > %d chars.",maxbuf);
	    return FALSE;
	    }
	}
    buf[i] = 0;  /* add string terminator */

    if (sameString(line,""))
	{
	break; /* End of Header found */
	}
    if (startsWith("HTTP/", line))
        {
	char *code;
	nextWord(&line);  // version
	code = nextWord(&line);
	if (code == NULL)
	    {
	    warn("Strange http header on %s", url);
	    return FALSE;
	    }
	if (startsWith("30", code) && isdigit(code[2])
	    && ((code[2] >= '0' && code[2] <= '3') || code[2] == '7') && code[3] == 0)
	    {
	    redirect = TRUE;
	    }
	else if (sameString(code, "305"))
	    {
	    mustUseProxy = TRUE;
	    }
	else if (sameString(code, "407"))
	    {
	    mustUseProxyAuth = TRUE;
	    }
	else if (byteRangeUsed 
	    /* hack for Apache bug 2.2.20 and 2.2.21 2011-10-21 should be OK to remove after one year. */
		&& !(sameString(code, "200") && byteRangeStart == 0 && byteRangeEnd == -1))  
	    {
	    if (!sameString(code, "206"))
		{
		if (sameString(code, "200"))
		    warn("Byte-range request was ignored by server. ");
		warn("Expected Partial Content 206. %s: %s %s. rangeStart=%lld rangeEnd=%lld", 
		    url, code, line, (long long)byteRangeStart, (long long)byteRangeEnd);
		return FALSE;
		}
	    }
	else if (sameString(code, "404"))
	    {
	    warn("404 file not found on %s", url);
	    return FALSE;
	    }
	else if (!sameString(code, "200"))
	    {
	    warn("Expected 200 %s: %s %s", url, code, line);
	    return FALSE;
	    }
	line = buf;  /* restore it */
	}
    headerName = line;
    sep = strchr(line,':');
    if (sep)
	{
	*sep = 0;
	headerVal = skipLeadingSpaces(++sep);
	}
    else
	{
	headerVal = NULL;
	}
    if (sameWord(headerName,"Location"))
	{
	if (redirect)
	    *redirectedUrl = cloneString(headerVal);
	if (mustUseProxy)
	    proxyLocation = cloneString(headerVal);
	}
    if (sameWord(headerName,"Content-Range"))
	{
	if (byteRangeUsed)
	    {
	    foundContentRange = TRUE;
	    parseContentRange(headerVal, &contentRangeStart, &contentRangeEnd);	
    	    if ((contentRangeStart != byteRangeStart) ||
		(byteRangeEnd != -1 && (contentRangeEnd != byteRangeEnd)))
		{
		char bre[256];
		safef(bre, sizeof bre, "%lld", (long long)byteRangeEnd);
		if (byteRangeEnd == -1)
		    bre[0] = 0;
		warn("Found Content-Range: %s. Expected bytes %lld-%s. Improper caching of 206 reponse byte-ranges?",
		    headerVal, (long long) byteRangeStart, bre);
    		return FALSE;
		}
	    }
	}
    }
if (mustUseProxy ||  mustUseProxyAuth)
    {
    warn("%s: %s error. Use Proxy%s. Location = %s", url, 
	mustUseProxy ? "" : " Authentication", 
	mustUseProxy ? "305" : "407", 
	proxyLocation ? proxyLocation : "not given");
    return FALSE;
    }
if (byteRangeUsed && !foundContentRange && !redirect
	    /* hack for Apache bug 2.2.20 and 2.2.21 2011-10-21 should be OK to remove after one year. */
		&& !(byteRangeStart == 0 && byteRangeEnd == -1))  
    {
    char bre[256];
    safef(bre, sizeof bre, "%lld", (long long)byteRangeEnd);
    if (byteRangeEnd == -1)
	bre[0] = 0;
    warn("Expected response header Content-Range: %lld-%s", (long long) byteRangeStart, bre);
    return FALSE;
    }

return TRUE;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x948, %rsp            # imm = 0x948
movq %rdx, 0x58(%rsp)
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x170(%rsp), %rax
movq %rax, 0x28(%rsp)
movb $0x20, 0x7(%rsp)
leaq 0x560b8(%rip), %rsi     # 0x80fa8
movq %rbx, %rdi
callq 0x6180
orq $-0x1, 0x68(%rsp)
movq %rax, %r13
orq $-0x1, 0x60(%rsp)
pushq $-0x1
popq %rax
movq %rax, %rcx
testq %r13, %r13
je 0x2af32
leaq 0x68(%rsp), %r14
leaq 0x60(%rsp), %r15
movq %rbx, %rdi
movq %r14, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x29ba5
movq (%r14), %rax
movq (%r15), %rcx
movq %rcx, 0x18(%rsp)
movq %rax, 0x20(%rsp)
movq $0x0, 0x48(%rsp)
leaq 0x7(%rsp), %r12
pushq $0x1
popq %r14
movq $0x0, 0x38(%rsp)
movl $0x0, 0x14(%rsp)
movq $0x0, 0x50(%rsp)
movl $0x0, 0x44(%rsp)
xorl %r15d, %r15d
movl %ebp, %edi
movq %r12, %rsi
movq %r14, %rdx
callq 0x6aa0
cmpl $-0x1, %eax
jne 0x2af96
callq 0x6060
movl (%rax), %edi
cmpl $0x4, %edi
je 0x2af73
jmp 0x2b2e8
testl %eax, %eax
je 0x2b2ca
cmpl $0x1, %eax
jne 0x2b2d3
movzbl 0x7(%rsp), %eax
cmpl $0xd, %eax
je 0x2afc4
movslq %r15d, %rcx
cmpl $0xa, %eax
je 0x2afd2
leal 0x1(%rcx), %r15d
movb %al, 0x170(%rsp,%rcx)
cmpl $0x7d0, %r15d           # imm = 0x7D0
jl 0x2af73
jmp 0x2b317
movb $0x0, 0x170(%rsp,%rcx)
movq 0x28(%rsp), %rsi
cmpb $0x0, (%rsi)
je 0x2b32e
leaq 0x55403(%rip), %rdi     # 0x803f2
callq 0x1db41
testl %eax, %eax
je 0x2b103
leaq 0x28(%rsp), %r15
movq %r15, %rdi
callq 0x1e9c1
movq %r15, %rdi
callq 0x1e9c1
testq %rax, %rax
je 0x2b389
movq %rax, %r15
leaq 0x5616b(%rip), %rdi     # 0x8118f
movq %rax, %rsi
callq 0x1db41
testl %eax, %eax
je 0x2b063
callq 0x6790
movq (%rax), %rcx
movsbq 0x2(%r15), %rax
testb $0x8, 0x1(%rcx,%rax,2)
je 0x2b063
movl %eax, %ecx
andb $-0x4, %cl
cmpb $0x30, %cl
setne %cl
cmpb $0x37, %al
setne %al
testb %cl, %al
jne 0x2b063
cmpb $0x0, 0x3(%r15)
je 0x2b138
movq %r15, %rdi
leaq 0x56125(%rip), %rsi     # 0x81192
callq 0x6a50
testl %eax, %eax
je 0x2b10a
movq %r15, %rdi
leaq 0x56112(%rip), %rsi     # 0x81196
callq 0x6a50
testl %eax, %eax
je 0x2b113
testq %r13, %r13
je 0x2b0d4
cmpq $-0x1, 0x18(%rsp)
sete 0x30(%rsp)
cmpq $0x0, 0x20(%rsp)
movq %r15, 0x8(%rsp)
sete %r15b
movq 0x8(%rsp), %rdi
leaq 0x5537b(%rip), %rsi     # 0x80437
callq 0x6a50
testl %eax, %eax
sete %cl
andb %r15b, %cl
movq 0x8(%rsp), %r15
testb %cl, 0x30(%rsp)
je 0x2b11c
movq %r15, %rdi
leaq 0x56132(%rip), %rsi     # 0x81210
callq 0x6a50
testl %eax, %eax
je 0x2b414
movq %r15, %rdi
leaq 0x55342(%rip), %rsi     # 0x80437
callq 0x6a50
testl %eax, %eax
je 0x2b140
jmp 0x2b420
movq 0x28(%rsp), %rdi
jmp 0x2b150
pushq $0x1
popq %rax
movl %eax, 0x14(%rsp)
jmp 0x2b140
pushq $0x1
popq %rax
movl %eax, 0x44(%rsp)
jmp 0x2b140
movl %eax, 0x30(%rsp)
movq %r15, %rdi
leaq 0x56070(%rip), %rsi     # 0x8119a
callq 0x6a50
testl %eax, %eax
je 0x2b140
jmp 0x2b440
pushq $0x1
popq %rax
movq %rax, 0x48(%rsp)
leaq 0x170(%rsp), %rax
movq %rax, 0x28(%rsp)
movq %rax, %rdi
movq %rdi, %r15
pushq $0x3a
popq %rsi
callq 0x6270
testq %rax, %rax
je 0x2b175
movb $0x0, (%rax)
incq %rax
movq %rax, %rdi
callq 0x1d6a7
movq %rax, 0x8(%rsp)
jmp 0x2b17e
movq $0x0, 0x8(%rsp)
movq %r15, %rdi
leaq 0x560bc(%rip), %rsi     # 0x81244
callq 0x1cd23
testl %eax, %eax
jne 0x2b1c0
cmpl $0x0, 0x48(%rsp)
je 0x2b1aa
movq 0x8(%rsp), %rdi
callq 0x1c306
movq 0x58(%rsp), %rcx
movq %rax, (%rcx)
cmpl $0x0, 0x14(%rsp)
je 0x2b1c0
movq 0x8(%rsp), %rdi
callq 0x1c306
movq %rax, 0x50(%rsp)
movq %r15, %rdi
leaq 0x56083(%rip), %rsi     # 0x8124d
callq 0x1cd23
testq %r13, %r13
je 0x2af70
testl %eax, %eax
jne 0x2af70
leaq 0x56907(%rip), %rdi     # 0x81aee
movq 0x8(%rsp), %rsi
callq 0x1db41
movl %eax, %ecx
pushq $-0x1
popq %rax
movq %rax, %r15
testl %ecx, %ecx
je 0x2b25a
movq 0x8(%rsp), %rdi
pushq $0x20
popq %rsi
callq 0x6270
incq %rax
movq %rax, 0x30(%rsp)
movq %rax, %rdi
pushq $0x2d
popq %rsi
callq 0x6270
movq %rax, %rcx
pushq $-0x1
popq %rax
movq %rax, %r15
movq %rcx, 0x38(%rsp)
testq %rcx, %rcx
je 0x2b25a
movq 0x30(%rsp), %rdi
callq 0x6520
movq %rax, %r15
movq 0x38(%rsp), %rax
cmpb $0x0, 0x1(%rax)
je 0x2b257
movq 0x38(%rsp), %rdi
incq %rdi
callq 0x6520
jmp 0x2b25a
pushq $-0x1
popq %rax
movq 0x18(%rsp), %rdx
cmpq $-0x1, %rdx
sete %cl
cmpq %rdx, %rax
sete %al
cmpq 0x20(%rsp), %r15
jne 0x2b282
orb %al, %cl
movb $0x1, %al
movq %rax, 0x38(%rsp)
jne 0x2af70
leaq 0x541eb(%rip), %rdx     # 0x7f474
xorl %ebp, %ebp
leaq 0x70(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq 0x18(%rsp), %rbx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1fab2
cmpq $-0x1, %rbx
jne 0x2b2af
movb $0x0, 0x70(%rsp)
leaq 0x55fa5(%rip), %rdi     # 0x8125b
leaq 0x70(%rsp), %rcx
movq 0x8(%rsp), %rsi
movq 0x20(%rsp), %rdx
jmp 0x2b434
leaq 0x55e26(%rip), %rdi     # 0x810f7
jmp 0x2b2da
leaq 0x55e55(%rip), %rdi     # 0x8112f
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x22186
jmp 0x2b303
callq 0x6230
leaq 0x55dde(%rip), %rdi     # 0x810d2
xorl %ebp, %ebp
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x22186
movl %ebp, %eax
addq $0x948, %rsp            # imm = 0x948
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x55e31(%rip), %rdi     # 0x8114f
xorl %ebp, %ebp
movl $0x7d0, %esi            # imm = 0x7D0
xorl %eax, %eax
callq 0x22186
jmp 0x2b303
movl 0x14(%rsp), %eax
orl %eax, 0x44(%rsp)
je 0x2b395
testl %eax, %eax
leaq 0x5c03c(%rip), %rax     # 0x8737d
leaq 0x55f9a(%rip), %rdx     # 0x812e2
cmovneq %rax, %rdx
leaq 0x55e43(%rip), %rcx     # 0x81196
leaq 0x55e38(%rip), %rax     # 0x81192
cmovneq %rax, %rcx
movq 0x50(%rsp), %rax
testq %rax, %rax
leaq 0x55f85(%rip), %r8      # 0x812f2
cmovneq %rax, %r8
leaq 0x55f41(%rip), %rdi     # 0x812b9
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x22186
jmp 0x2b303
leaq 0x55de5(%rip), %rdi     # 0x81175
jmp 0x2b2da
movq 0x18(%rsp), %rax
notq %rax
testq %r13, %r13
sete %cl
movq 0x38(%rsp), %rdx
orb %cl, %dl
orq 0x20(%rsp), %rax
sete %al
pushq $0x1
popq %rbp
testb $0x1, %dl
jne 0x2b303
cmpl $0x0, 0x48(%rsp)
jne 0x2b303
testb %al, %al
jne 0x2b303
leaq 0x5409c(%rip), %rdx     # 0x7f474
xorl %ebp, %ebp
leaq 0x70(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq 0x18(%rsp), %rbx
movq %rbx, %rcx
xorl %eax, %eax
callq 0x1fab2
cmpq $-0x1, %rbx
jne 0x2b3fe
movb $0x0, 0x70(%rsp)
leaq 0x55ef7(%rip), %rdi     # 0x812fc
leaq 0x70(%rsp), %rdx
movq 0x20(%rsp), %rsi
jmp 0x2b2fc
leaq 0x55df9(%rip), %rdi     # 0x81214
jmp 0x2b2da
movq 0x28(%rsp), %rcx
leaq 0x55e01(%rip), %rdi     # 0x8122d
xorl %ebp, %ebp
movq %rbx, %rsi
movq %r15, %rdx
xorl %eax, %eax
callq 0x22186
jmp 0x2b303
cmpl $0x0, 0x30(%rsp)
jne 0x2b455
leaq 0x55d50(%rip), %rdi     # 0x8119e
xorl %eax, %eax
callq 0x22186
movq 0x28(%rsp), %rcx
leaq 0x55d68(%rip), %rdi     # 0x811c9
xorl %ebp, %ebp
movq %rbx, %rsi
movq 0x8(%rsp), %rdx
movq 0x20(%rsp), %r8
movq 0x18(%rsp), %r9
xorl %eax, %eax
callq 0x22186
jmp 0x2b303