// File: /workspace/github_c_makefile_star2_O1/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 $0x938, %rsp            # imm = 0x938
movq %rdx, 0x58(%rsp)
movq %rsi, %rbx
movl %edi, %ebp
leaq 0x160(%rsp), %rax
movq %rax, 0x20(%rsp)
movb $0x20, 0xe(%rsp)
leaq 0x61e2c(%rip), %rsi     # 0x91fb8
movq %rbx, %rdi
callq 0x6180
movq %rax, %r12
movq $-0x1, %rax
movq %rax, 0x48(%rsp)
movq %rax, 0x38(%rsp)
testq %r12, %r12
je 0x301c1
leaq 0x48(%rsp), %rsi
leaq 0x38(%rsp), %rdx
movq %rbx, %rdi
xorl %ecx, %ecx
callq 0x2edba
movq 0x48(%rsp), %rax
movq %rax, 0x30(%rsp)
movq 0x38(%rsp), %rax
movq %rax, 0x40(%rsp)
leaq 0xe(%rsp), %r15
movl $0x0, 0x18(%rsp)
xorl %r14d, %r14d
movl $0x0, 0x1c(%rsp)
movq $0x0, 0x50(%rsp)
movl $0x0, 0x2c(%rsp)
xorl %r13d, %r13d
movl $0x1, %edx
movl %ebp, %edi
movq %r15, %rsi
callq 0x6aa0
cmpl $-0x1, %eax
jne 0x30226
callq 0x6060
movl (%rax), %edi
cmpl $0x4, %edi
je 0x30201
jmp 0x305ec
testl %eax, %eax
je 0x305ce
cmpl $0x1, %eax
jne 0x305d7
movzbl 0xe(%rsp), %eax
cmpl $0xd, %eax
je 0x30254
movslq %r13d, %rcx
cmpl $0xa, %eax
je 0x30262
leal 0x1(%rcx), %r13d
movb %al, 0x160(%rsp,%rcx)
cmpl $0x7d0, %r13d           # imm = 0x7D0
jl 0x30201
jmp 0x3061b
movb $0x0, 0x160(%rsp,%rcx)
movq 0x20(%rsp), %rsi
cmpb $0x0, (%rsi)
je 0x30632
leaq 0x61183(%rip), %rdi     # 0x91402
callq 0x217c8
testl %eax, %eax
je 0x3045a
leaq 0x20(%rsp), %r13
movq %r13, %rdi
callq 0x229cd
movq %r13, %rdi
callq 0x229cd
testq %rax, %rax
je 0x303a9
movq %rax, %r13
leaq 0x61eeb(%rip), %rdi     # 0x9219f
movq %rax, %rsi
callq 0x217c8
testl %eax, %eax
je 0x302f3
callq 0x6790
movq (%rax), %rcx
movsbq 0x2(%r13), %rax
testb $0x8, 0x1(%rcx,%rax,2)
je 0x302f3
movl %eax, %ecx
andb $-0x4, %cl
cmpb $0x30, %cl
setne %cl
cmpb $0x37, %al
setne %al
testb %cl, %al
jne 0x302f3
cmpb $0x0, 0x3(%r13)
je 0x30439
movq %r13, %rdi
leaq 0x61ea5(%rip), %rsi     # 0x921a2
callq 0x6a50
testl %eax, %eax
je 0x303c2
movq %r13, %rdi
leaq 0x61e92(%rip), %rsi     # 0x921a6
callq 0x6a50
testl %eax, %eax
je 0x303cc
testq %r12, %r12
je 0x3035a
cmpq $-0x1, 0x40(%rsp)
sete 0x10(%rsp)
cmpq $0x0, 0x30(%rsp)
sete 0xf(%rsp)
movq %r13, %rdi
leaq 0x61101(%rip), %rsi     # 0x91447
callq 0x6a50
testl %eax, %eax
sete %cl
andb 0xf(%rsp), %cl
testb %cl, 0x10(%rsp)
je 0x303d6
movq %r13, %rdi
leaq 0x61ebc(%rip), %rsi     # 0x92220
callq 0x6a50
testl %eax, %eax
je 0x3042a
movq %r13, %rdi
leaq 0x610cc(%rip), %rsi     # 0x91447
callq 0x6a50
testl %eax, %eax
je 0x30441
movq 0x20(%rsp), %rcx
leaq 0x61ea9(%rip), %rdi     # 0x9223d
movq %rbx, %rsi
movq %r13, %rdx
xorl %r13d, %r13d
xorl %eax, %eax
callq 0x268ca
jmp 0x30451
xorl %r13d, %r13d
leaq 0x61dd2(%rip), %rdi     # 0x92185
movq %rbx, %rsi
xorl %eax, %eax
callq 0x268ca
jmp 0x30451
movl $0x1, 0x1c(%rsp)
jmp 0x30441
movl $0x1, 0x2c(%rsp)
jmp 0x30441
movl %eax, 0x10(%rsp)
movq %r13, %rdi
leaq 0x61dc6(%rip), %rsi     # 0x921aa
callq 0x6a50
testl %eax, %eax
je 0x30441
cmpl $0x0, 0x10(%rsp)
jne 0x30402
leaq 0x61db3(%rip), %rdi     # 0x921ae
xorl %eax, %eax
callq 0x268ca
movq 0x20(%rsp), %rcx
leaq 0x61dcb(%rip), %rdi     # 0x921d9
movq %rbx, %rsi
movq %r13, %rdx
xorl %r13d, %r13d
movq 0x30(%rsp), %r8
movq 0x40(%rsp), %r9
xorl %eax, %eax
callq 0x268ca
jmp 0x30451
xorl %r13d, %r13d
leaq 0x61df0(%rip), %rdi     # 0x92224
jmp 0x303b3
movl $0x1, 0x18(%rsp)
leaq 0x160(%rsp), %rax
movq %rax, 0x20(%rsp)
movb $0x1, %r13b
testb %r13b, %r13b
je 0x3068d
movq 0x20(%rsp), %rdi
movq %rdi, %r13
movl $0x3a, %esi
callq 0x6270
testq %rax, %rax
je 0x30486
movb $0x0, (%rax)
incq %rax
movq %rax, %rdi
callq 0x21238
movq %rax, 0x10(%rsp)
jmp 0x3048f
movq $0x0, 0x10(%rsp)
movq %r13, %rdi
leaq 0x61dbb(%rip), %rsi     # 0x92254
callq 0x2054a
testl %eax, %eax
jne 0x304d1
cmpl $0x0, 0x18(%rsp)
je 0x304bb
movq 0x10(%rsp), %rdi
callq 0x1f9f6
movq 0x58(%rsp), %rcx
movq %rax, (%rcx)
cmpl $0x0, 0x1c(%rsp)
je 0x304d1
movq 0x10(%rsp), %rdi
callq 0x1f9f6
movq %rax, 0x50(%rsp)
movq %r13, %rdi
leaq 0x61d82(%rip), %rsi     # 0x9225d
callq 0x2054a
testq %r12, %r12
je 0x301fe
testl %eax, %eax
jne 0x301fe
leaq 0x62606(%rip), %rdi     # 0x92afe
movq 0x10(%rsp), %r14
movq %r14, %rsi
callq 0x217c8
testl %eax, %eax
je 0x30550
movq %r14, %rdi
movl $0x20, %esi
callq 0x6270
movq %rax, %r14
incq %r14
movq %r14, %rdi
movl $0x2d, %esi
callq 0x6270
testq %rax, %rax
je 0x30550
movq %rax, %r13
movq %r14, %rdi
callq 0x6520
movq %rax, %r14
cmpb $0x0, 0x1(%r13)
je 0x30557
incq %r13
movq %r13, %rdi
callq 0x6520
jmp 0x3055e
movq $-0x1, %r14
movq $-0x1, %rax
movq 0x40(%rsp), %rcx
cmpq $-0x1, %rcx
sete %dl
cmpq %rcx, %rax
sete %cl
cmpq 0x30(%rsp), %r14
jne 0x30582
orb %cl, %dl
movb $0x1, %r14b
jne 0x301fe
movq 0x38(%rsp), %rbx
leaq 0x5fef6(%rip), %rdx     # 0x90484
xorl %ebp, %ebp
leaq 0x60(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %rbx, %rcx
xorl %eax, %eax
callq 0x23c18
cmpq $-0x1, %rbx
jne 0x305af
movb $0x0, 0x60(%rsp)
leaq 0x61cb5(%rip), %rdi     # 0x9226b
leaq 0x60(%rsp), %rcx
movq 0x10(%rsp), %rsi
movq 0x30(%rsp), %rdx
xorl %eax, %eax
callq 0x268ca
jmp 0x30607
leaq 0x61b32(%rip), %rdi     # 0x92107
jmp 0x305de
leaq 0x61b61(%rip), %rdi     # 0x9213f
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x268ca
jmp 0x30607
callq 0x6230
leaq 0x61aea(%rip), %rdi     # 0x920e2
xorl %ebp, %ebp
movq %rax, %rsi
movq %rbx, %rdx
xorl %eax, %eax
callq 0x268ca
movl %ebp, %eax
addq $0x938, %rsp            # imm = 0x938
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x61b3d(%rip), %rdi     # 0x9215f
xorl %ebp, %ebp
movl $0x7d0, %esi            # imm = 0x7D0
xorl %eax, %eax
callq 0x268ca
jmp 0x30607
movl 0x1c(%rsp), %eax
orl %eax, 0x2c(%rsp)
je 0x30694
testl %eax, %eax
leaq 0x67d1b(%rip), %rax     # 0x98360
leaq 0x61ca6(%rip), %rdx     # 0x922f2
cmovneq %rax, %rdx
leaq 0x61b4f(%rip), %rcx     # 0x921a6
leaq 0x61b44(%rip), %rax     # 0x921a2
cmovneq %rax, %rcx
movq 0x50(%rsp), %rax
testq %rax, %rax
leaq 0x61c91(%rip), %r8      # 0x92302
cmovneq %rax, %r8
leaq 0x61c4d(%rip), %rdi     # 0x922c9
xorl %ebp, %ebp
movq %rbx, %rsi
xorl %eax, %eax
callq 0x268ca
jmp 0x30607
xorl %ebp, %ebp
jmp 0x30607
testq %r12, %r12
sete %al
orb %al, %r14b
cmpl $0x0, 0x18(%rsp)
setne %al
orb %r14b, %al
movl $0x1, %ebp
testb $0x1, %al
jne 0x30607
movq 0x48(%rsp), %rbx
movq 0x38(%rsp), %r14
movq %r14, %rax
notq %rax
orq %rbx, %rax
je 0x30607
leaq 0x5fdaf(%rip), %rdx     # 0x90484
xorl %ebp, %ebp
leaq 0x60(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %r14, %rcx
xorl %eax, %eax
callq 0x23c18
cmpq $-0x1, %r14
jne 0x306f6
movb $0x0, 0x60(%rsp)
leaq 0x61c0f(%rip), %rdi     # 0x9230c
leaq 0x60(%rsp), %rdx
movq %rbx, %rsi
jmp 0x30600