// File: /workspace/github_c_makefile_star2_O3/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, %r14
movl %edi, %ebp
leaq 0x170(%rsp), %rax
movq %rax, 0x30(%rsp)
movb $0x20, 0xf(%rsp)
leaq 0x61c2c(%rip), %rsi     # 0x920a8
movq %r14, %rdi
callq 0x6180
movq %rax, %r13
movq $-0x1, %r15
movq %r15, 0x68(%rsp)
movq %r15, 0x60(%rsp)
movq $-0x1, %rax
testq %r13, %r13
je 0x304c4
leaq 0x68(%rsp), %rbx
leaq 0x60(%rsp), %r15
movq %r14, %rdi
movq %rbx, %rsi
movq %r15, %rdx
xorl %ecx, %ecx
callq 0x2f0f3
movq (%rbx), %rax
movq (%r15), %r15
movq %rax, 0x28(%rsp)
movq $0x0, 0x48(%rsp)
leaq 0xf(%rsp), %r12
movq $0x0, 0x38(%rsp)
movl $0x0, 0x1c(%rsp)
movq $0x0, 0x50(%rsp)
movl $0x0, 0x44(%rsp)
xorl %ebx, %ebx
movl $0x1, %edx
movl %ebp, %edi
movq %r12, %rsi
callq 0x6aa0
cmpl $-0x1, %eax
jne 0x30520
callq 0x6060
movl (%rax), %edi
cmpl $0x4, %edi
je 0x304fb
jmp 0x30846
testl %eax, %eax
je 0x30828
cmpl $0x1, %eax
jne 0x30831
movzbl 0xf(%rsp), %eax
cmpl $0xd, %eax
je 0x3054d
movslq %ebx, %rcx
cmpl $0xa, %eax
je 0x3055a
leal 0x1(%rcx), %ebx
movb %al, 0x170(%rsp,%rcx)
cmpl $0x7d0, %ebx            # imm = 0x7D0
jl 0x304fb
jmp 0x30875
movb $0x0, 0x170(%rsp,%rcx)
movq 0x30(%rsp), %rsi
cmpb $0x0, (%rsi)
je 0x3088c
leaq 0x60f7b(%rip), %rdi     # 0x914f2
callq 0x21b23
testl %eax, %eax
je 0x30682
leaq 0x30(%rsp), %rbx
movq %rbx, %rdi
callq 0x22cf1
movq %rbx, %rdi
callq 0x22cf1
testq %rax, %rax
je 0x308e7
movq %rax, %rbx
leaq 0x61ce3(%rip), %rdi     # 0x9228f
movq %rax, %rsi
callq 0x21b23
testl %eax, %eax
je 0x305ea
callq 0x6790
movq (%rax), %rcx
movsbq 0x2(%rbx), %rax
testb $0x8, 0x1(%rcx,%rax,2)
je 0x305ea
movl %eax, %ecx
andb $-0x4, %cl
cmpb $0x30, %cl
setne %cl
cmpb $0x37, %al
setne %al
testb %cl, %al
jne 0x305ea
cmpb $0x0, 0x3(%rbx)
je 0x306b9
movq %rbx, %rdi
leaq 0x61c9e(%rip), %rsi     # 0x92292
callq 0x6a50
testl %eax, %eax
je 0x30689
movq %rbx, %rdi
leaq 0x61c8b(%rip), %rsi     # 0x92296
callq 0x6a50
testl %eax, %eax
je 0x30693
testq %r13, %r13
je 0x30653
cmpq $-0x1, %r15
sete 0x20(%rsp)
cmpq $0x0, 0x28(%rsp)
movq %rbx, 0x10(%rsp)
sete %bl
movq 0x10(%rsp), %rdi
leaq 0x60efb(%rip), %rsi     # 0x91537
callq 0x6a50
testl %eax, %eax
sete %cl
andb %bl, %cl
movq 0x10(%rsp), %rbx
testb %cl, 0x20(%rsp)
je 0x3069d
movq %rbx, %rdi
leaq 0x61cb3(%rip), %rsi     # 0x92310
callq 0x6a50
testl %eax, %eax
je 0x3096d
movq %rbx, %rdi
leaq 0x60ec3(%rip), %rsi     # 0x91537
callq 0x6a50
testl %eax, %eax
je 0x306c3
jmp 0x30979
movq 0x30(%rsp), %rdi
jmp 0x306d3
movl $0x1, 0x1c(%rsp)
jmp 0x306c3
movl $0x1, 0x44(%rsp)
jmp 0x306c3
movl %eax, 0x20(%rsp)
movq %rbx, %rdi
leaq 0x61bef(%rip), %rsi     # 0x9229a
callq 0x6a50
testl %eax, %eax
je 0x306c3
jmp 0x3098f
movl $0x1, %eax
movq %rax, 0x48(%rsp)
leaq 0x170(%rsp), %rax
movq %rax, 0x30(%rsp)
movq %rax, %rdi
movq %rdi, %rbx
movl $0x3a, %esi
callq 0x6270
testq %rax, %rax
je 0x306fa
movb $0x0, (%rax)
incq %rax
movq %rax, %rdi
callq 0x215d4
movq %rax, 0x10(%rsp)
jmp 0x30703
movq $0x0, 0x10(%rsp)
movq %rbx, %rdi
leaq 0x61c37(%rip), %rsi     # 0x92344
callq 0x209d6
testl %eax, %eax
jne 0x30745
cmpl $0x0, 0x48(%rsp)
je 0x3072f
movq 0x10(%rsp), %rdi
callq 0x1feba
movq 0x58(%rsp), %rcx
movq %rax, (%rcx)
cmpl $0x0, 0x1c(%rsp)
je 0x30745
movq 0x10(%rsp), %rdi
callq 0x1feba
movq %rax, 0x50(%rsp)
movq %rbx, %rdi
leaq 0x61bfe(%rip), %rsi     # 0x9234d
callq 0x209d6
testq %r13, %r13
je 0x304f9
testl %eax, %eax
jne 0x304f9
leaq 0x62482(%rip), %rdi     # 0x92bee
movq 0x10(%rsp), %rbx
movq %rbx, %rsi
callq 0x21b23
movq $-0x1, 0x20(%rsp)
testl %eax, %eax
je 0x307da
movq %rbx, %rdi
movl $0x20, %esi
callq 0x6270
movq %rax, %rbx
incq %rbx
movq %rbx, %rdi
movl $0x2d, %esi
callq 0x6270
movq %rax, 0x38(%rsp)
testq %rax, %rax
je 0x307da
movq %rbx, %rdi
callq 0x6520
movq %rax, %rbx
movq 0x38(%rsp), %rax
cmpb $0x0, 0x1(%rax)
je 0x307e1
movq 0x38(%rsp), %rdi
incq %rdi
callq 0x6520
movq %rax, 0x20(%rsp)
jmp 0x307e1
movq $-0x1, %rbx
cmpq 0x28(%rsp), %rbx
jne 0x309ce
movb $0x1, %al
movq %rax, 0x38(%rsp)
cmpq $-0x1, %r15
je 0x304f9
cmpq %r15, 0x20(%rsp)
je 0x304f9
leaq 0x5fd35(%rip), %rdx     # 0x90544
leaq 0x70(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %r15, %rcx
xorl %eax, %eax
callq 0x23f33
jmp 0x309f4
leaq 0x619c8(%rip), %rdi     # 0x921f7
jmp 0x30838
leaq 0x619f7(%rip), %rdi     # 0x9222f
xorl %ebp, %ebp
movq %r14, %rsi
xorl %eax, %eax
callq 0x26bea
jmp 0x30861
callq 0x6230
leaq 0x61980(%rip), %rdi     # 0x921d2
xorl %ebp, %ebp
movq %rax, %rsi
movq %r14, %rdx
xorl %eax, %eax
callq 0x26bea
movl %ebp, %eax
addq $0x948, %rsp            # imm = 0x948
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x619d3(%rip), %rdi     # 0x9224f
xorl %ebp, %ebp
movl $0x7d0, %esi            # imm = 0x7D0
xorl %eax, %eax
callq 0x26bea
jmp 0x30861
movl 0x1c(%rsp), %eax
orl %eax, 0x44(%rsp)
je 0x308f3
testl %eax, %eax
leaq 0x67bb5(%rip), %rax     # 0x98454
leaq 0x61b3c(%rip), %rdx     # 0x923e2
cmovneq %rax, %rdx
leaq 0x619e5(%rip), %rcx     # 0x92296
leaq 0x619da(%rip), %rax     # 0x92292
cmovneq %rax, %rcx
movq 0x50(%rsp), %rax
testq %rax, %rax
leaq 0x61b27(%rip), %r8      # 0x923f2
cmovneq %rax, %r8
leaq 0x61ae3(%rip), %rdi     # 0x923b9
xorl %ebp, %ebp
movq %r14, %rsi
xorl %eax, %eax
callq 0x26bea
jmp 0x30861
leaq 0x61987(%rip), %rdi     # 0x92275
jmp 0x30838
movq %r15, %rax
notq %rax
testq %r13, %r13
sete %cl
movq 0x38(%rsp), %rdx
orb %cl, %dl
orq 0x28(%rsp), %rax
sete %al
movl $0x1, %ebp
testb $0x1, %dl
jne 0x30861
cmpl $0x0, 0x48(%rsp)
jne 0x30861
testb %al, %al
jne 0x30861
leaq 0x5fc0e(%rip), %rdx     # 0x90544
xorl %ebp, %ebp
leaq 0x70(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %r15, %rcx
xorl %eax, %eax
callq 0x23f33
cmpq $-0x1, %r15
jne 0x30957
movb $0x0, 0x70(%rsp)
leaq 0x61a9e(%rip), %rdi     # 0x923fc
leaq 0x70(%rsp), %rdx
movq 0x28(%rsp), %rsi
jmp 0x3085a
leaq 0x619a0(%rip), %rdi     # 0x92314
jmp 0x30838
movq 0x30(%rsp), %rcx
leaq 0x619a8(%rip), %rdi     # 0x9232d
xorl %ebp, %ebp
movq %r14, %rsi
movq %rbx, %rdx
jmp 0x30a0c
cmpl $0x0, 0x20(%rsp)
jne 0x309a4
leaq 0x61901(%rip), %rdi     # 0x9229e
xorl %eax, %eax
callq 0x26bea
movq 0x30(%rsp), %rcx
leaq 0x61919(%rip), %rdi     # 0x922c9
xorl %ebp, %ebp
movq %r14, %rsi
movq 0x10(%rsp), %rdx
movq 0x28(%rsp), %r8
movq %r15, %r9
xorl %eax, %eax
callq 0x26bea
jmp 0x30861
leaq 0x5fb6f(%rip), %rdx     # 0x90544
leaq 0x70(%rsp), %rdi
movl $0x100, %esi            # imm = 0x100
movq %r15, %rcx
xorl %eax, %eax
callq 0x23f33
cmpq $-0x1, %r15
jne 0x309f4
movb $0x0, 0x70(%rsp)
leaq 0x61960(%rip), %rdi     # 0x9235b
xorl %ebp, %ebp
leaq 0x70(%rsp), %rcx
movq 0x10(%rsp), %rsi
movq 0x28(%rsp), %rdx
xorl %eax, %eax
callq 0x26bea
jmp 0x30861