// File: /workspace/github_c_makefile_star2_O0/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
movq %rsp, %rbp
subq $0xa90, %rsp            # imm = 0xA90
movl %edi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
leaq -0x7f0(%rbp), %rax
movq %rax, -0x7f8(%rbp)
movl $0x7d0, -0x7fc(%rbp)    # imm = 0x7D0
movl $0x0, -0x800(%rbp)
movb $0x20, -0x801(%rbp)
movl $0x0, -0x808(%rbp)
movq $0x0, -0x810(%rbp)
movq $0x0, -0x818(%rbp)
movq $0x0, -0x820(%rbp)
movl $0x0, -0x824(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x97a11(%rip), %rsi     # 0xdbe60
callq 0x6170
cmpq $0x0, %rax
setne %al
andb $0x1, %al
movzbl %al, %eax
movl %eax, -0x828(%rbp)
movq $-0x1, -0x830(%rbp)
movq $-0x1, -0x838(%rbp)
movl $0x0, -0x83c(%rbp)
movq $-0x1, -0x848(%rbp)
movq $-0x1, -0x850(%rbp)
movl $0x0, -0x854(%rbp)
movq $0x0, -0x860(%rbp)
movl $0x0, -0x864(%rbp)
cmpl $0x0, -0x828(%rbp)
je 0x444dd
movq -0x10(%rbp), %rdi
leaq -0x830(%rbp), %rsi
leaq -0x838(%rbp), %rdx
xorl %ecx, %ecx
callq 0x42670
jmp 0x444df
movl $0x0, -0x800(%rbp)
movl -0x8(%rbp), %edi
leaq -0x801(%rbp), %rsi
movl $0x1, %edx
callq 0x6a90
movl %eax, -0x808(%rbp)
cmpl $0x1, -0x808(%rbp)
je 0x44581
cmpl $-0x1, -0x808(%rbp)
jne 0x44544
callq 0x6060
cmpl $0x4, (%rax)
jne 0x44521
jmp 0x444e9
callq 0x6060
movl (%rax), %edi
callq 0x6220
movq %rax, %rsi
movq -0x10(%rbp), %rdx
leaq 0x97a4f(%rip), %rdi     # 0xdbf8a
movb $0x0, %al
callq 0x358e0
jmp 0x44575
cmpl $0x0, -0x808(%rbp)
jne 0x44561
movq -0x10(%rbp), %rsi
leaq 0x97a57(%rip), %rdi     # 0xdbfaf
movb $0x0, %al
callq 0x358e0
jmp 0x44573
movq -0x10(%rbp), %rsi
leaq 0x97a7b(%rip), %rdi     # 0xdbfe7
movb $0x0, %al
callq 0x358e0
jmp 0x44575
movl $0x0, -0x4(%rbp)
jmp 0x44b88
movsbl -0x801(%rbp), %eax
cmpl $0xa, %eax
jne 0x4458f
jmp 0x445ee
movsbl -0x801(%rbp), %eax
cmpl $0xd, %eax
je 0x445bb
movb -0x801(%rbp), %cl
movl -0x800(%rbp), %eax
movl %eax, %edx
addl $0x1, %edx
movl %edx, -0x800(%rbp)
cltq
movb %cl, -0x7f0(%rbp,%rax)
movl -0x800(%rbp), %eax
cmpl -0x7fc(%rbp), %eax
jl 0x445e9
movl -0x7fc(%rbp), %esi
leaq 0x97a31(%rip), %rdi     # 0xdc007
movb $0x0, %al
callq 0x358e0
movl $0x0, -0x4(%rbp)
jmp 0x44b88
jmp 0x444e9
movslq -0x800(%rbp), %rax
movb $0x0, -0x7f0(%rbp,%rax)
movq -0x7f8(%rbp), %rdi
leaq 0xa0dfb(%rip), %rsi     # 0xe5406
callq 0x6a40
cmpl $0x0, %eax
jne 0x4461a
jmp 0x44a36
movq -0x7f8(%rbp), %rsi
leaq 0x96c82(%rip), %rdi     # 0xdb2aa
callq 0x2d3d0
cmpl $0x0, %eax
je 0x4489b
leaq -0x7f8(%rbp), %rdi
callq 0x2f230
leaq -0x7f8(%rbp), %rdi
callq 0x2f230
movq %rax, -0x870(%rbp)
cmpq $0x0, -0x870(%rbp)
jne 0x4467d
movq -0x10(%rbp), %rsi
leaq 0x979c3(%rip), %rdi     # 0xdc02d
movb $0x0, %al
callq 0x358e0
movl $0x0, -0x4(%rbp)
jmp 0x44b88
movq -0x870(%rbp), %rsi
leaq 0x979bc(%rip), %rdi     # 0xdc047
callq 0x2d3d0
cmpl $0x0, %eax
je 0x44708
callq 0x6780
movq (%rax), %rax
movq -0x870(%rbp), %rcx
movsbl 0x2(%rcx), %ecx
movslq %ecx, %rcx
movzwl (%rax,%rcx,2), %eax
andl $0x800, %eax            # imm = 0x800
cmpl $0x0, %eax
je 0x44708
movq -0x870(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x30, %eax
jl 0x446d9
movq -0x870(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x33, %eax
jle 0x446e9
movq -0x870(%rbp), %rax
movsbl 0x2(%rax), %eax
cmpl $0x37, %eax
jne 0x44708
movq -0x870(%rbp), %rax
movsbl 0x3(%rax), %eax
cmpl $0x0, %eax
jne 0x44708
movl $0x1, -0x824(%rbp)
jmp 0x4488d
movq -0x870(%rbp), %rdi
leaq 0x97934(%rip), %rsi     # 0xdc04a
callq 0x6a40
cmpl $0x0, %eax
jne 0x4472f
movl $0x1, -0x854(%rbp)
jmp 0x4488b
movq -0x870(%rbp), %rdi
leaq 0x97911(%rip), %rsi     # 0xdc04e
callq 0x6a40
cmpl $0x0, %eax
jne 0x44756
movl $0x1, -0x864(%rbp)
jmp 0x44889
cmpl $0x0, -0x828(%rbp)
je 0x44809
movq -0x870(%rbp), %rdi
leaq 0x96b7e(%rip), %rsi     # 0xdb2ef
callq 0x6a40
cmpl $0x0, %eax
jne 0x4478f
cmpq $0x0, -0x830(%rbp)
jne 0x4478f
cmpq $-0x1, -0x838(%rbp)
je 0x44809
movq -0x870(%rbp), %rdi
leaq 0x978b5(%rip), %rsi     # 0xdc052
callq 0x6a40
cmpl $0x0, %eax
je 0x44807
movq -0x870(%rbp), %rdi
leaq 0x96b3a(%rip), %rsi     # 0xdb2ef
callq 0x6a40
cmpl $0x0, %eax
jne 0x447cd
leaq 0x97890(%rip), %rdi     # 0xdc056
movb $0x0, %al
callq 0x358e0
movq -0x10(%rbp), %rsi
movq -0x870(%rbp), %rdx
movq -0x7f8(%rbp), %rcx
movq -0x830(%rbp), %r8
movq -0x838(%rbp), %r9
leaq 0x9788d(%rip), %rdi     # 0xdc081
movb $0x0, %al
callq 0x358e0
movl $0x0, -0x4(%rbp)
jmp 0x44b88
jmp 0x44887
movq -0x870(%rbp), %rdi
leaq 0x978b1(%rip), %rsi     # 0xdc0c8
callq 0x6a40
cmpl $0x0, %eax
jne 0x4483f
movq -0x10(%rbp), %rsi
leaq 0x978a0(%rip), %rdi     # 0xdc0cc
movb $0x0, %al
callq 0x358e0
movl $0x0, -0x4(%rbp)
jmp 0x44b88
movq -0x870(%rbp), %rdi
leaq 0x96aa2(%rip), %rsi     # 0xdb2ef
callq 0x6a40
cmpl $0x0, %eax
je 0x44883
movq -0x10(%rbp), %rsi
movq -0x870(%rbp), %rdx
movq -0x7f8(%rbp), %rcx
leaq 0x97875(%rip), %rdi     # 0xdc0e5
movb $0x0, %al
callq 0x358e0
movl $0x0, -0x4(%rbp)
jmp 0x44b88
jmp 0x44885
jmp 0x44887
jmp 0x44889
jmp 0x4488b
jmp 0x4488d
leaq -0x7f0(%rbp), %rax
movq %rax, -0x7f8(%rbp)
movq -0x7f8(%rbp), %rax
movq %rax, -0x818(%rbp)
movq -0x7f8(%rbp), %rdi
movl $0x3a, %esi
callq 0x6260
movq %rax, -0x810(%rbp)
cmpq $0x0, -0x810(%rbp)
je 0x448f5
movq -0x810(%rbp), %rax
movb $0x0, (%rax)
movq -0x810(%rbp), %rdi
addq $0x1, %rdi
movq %rdi, -0x810(%rbp)
callq 0x2ca80
movq %rax, -0x820(%rbp)
jmp 0x44900
movq $0x0, -0x820(%rbp)
movq -0x818(%rbp), %rdi
leaq 0x977ee(%rip), %rsi     # 0xdc0fc
callq 0x2b9f0
cmpl $0x0, %eax
jne 0x44955
cmpl $0x0, -0x824(%rbp)
je 0x44937
movq -0x820(%rbp), %rdi
callq 0x2a450
movq %rax, %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
cmpl $0x0, -0x854(%rbp)
je 0x44953
movq -0x820(%rbp), %rdi
callq 0x2a450
movq %rax, -0x860(%rbp)
jmp 0x44955
movq -0x818(%rbp), %rdi
leaq 0x977a2(%rip), %rsi     # 0xdc105
callq 0x2b9f0
cmpl $0x0, %eax
jne 0x44a31
cmpl $0x0, -0x828(%rbp)
je 0x44a2f
movl $0x1, -0x83c(%rbp)
movq -0x820(%rbp), %rdi
leaq -0x848(%rbp), %rsi
leaq -0x850(%rbp), %rdx
callq 0x44ba0
movq -0x848(%rbp), %rax
cmpq -0x830(%rbp), %rax
jne 0x449cc
cmpq $-0x1, -0x838(%rbp)
je 0x44a2d
movq -0x850(%rbp), %rax
cmpq -0x838(%rbp), %rax
je 0x44a2d
leaq -0x970(%rbp), %rdi
movq -0x838(%rbp), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0x95916(%rip), %rdx     # 0xda2fc
movb $0x0, %al
callq 0x31090
cmpq $-0x1, -0x838(%rbp)
jne 0x449fe
movb $0x0, -0x970(%rbp)
movq -0x820(%rbp), %rsi
movq -0x830(%rbp), %rdx
leaq -0x970(%rbp), %rcx
leaq 0x976f9(%rip), %rdi     # 0xdc113
movb $0x0, %al
callq 0x358e0
movl $0x0, -0x4(%rbp)
jmp 0x44b88
jmp 0x44a2f
jmp 0x44a31
jmp 0x444df
cmpl $0x0, -0x854(%rbp)
jne 0x44a4c
cmpl $0x0, -0x864(%rbp)
je 0x44afb
movq -0x10(%rbp), %rax
movq %rax, -0xa88(%rbp)
movl -0x854(%rbp), %edx
leaq 0x97736(%rip), %rax     # 0xdc19a
leaq 0xa099b(%rip), %rcx     # 0xe5406
cmpl $0x0, %edx
cmovneq %rcx, %rax
movq %rax, -0xa80(%rbp)
movl -0x854(%rbp), %edx
leaq 0x975c8(%rip), %rax     # 0xdc04e
leaq 0x975bd(%rip), %rcx     # 0xdc04a
cmpl $0x0, %edx
cmovneq %rcx, %rax
movq %rax, -0xa78(%rbp)
cmpq $0x0, -0x860(%rbp)
je 0x44ab5
movq -0x860(%rbp), %rax
movq %rax, -0xa90(%rbp)
jmp 0x44ac5
leaq 0x976ee(%rip), %rax     # 0xdc1aa
movq %rax, -0xa90(%rbp)
jmp 0x44ac5
movq -0xa78(%rbp), %rcx
movq -0xa80(%rbp), %rdx
movq -0xa88(%rbp), %rsi
movq -0xa90(%rbp), %r8
leaq 0x97689(%rip), %rdi     # 0xdc171
movb $0x0, %al
callq 0x358e0
movl $0x0, -0x4(%rbp)
jmp 0x44b88
cmpl $0x0, -0x828(%rbp)
je 0x44b81
cmpl $0x0, -0x83c(%rbp)
jne 0x44b81
cmpl $0x0, -0x824(%rbp)
jne 0x44b81
cmpq $0x0, -0x830(%rbp)
jne 0x44b2a
cmpq $-0x1, -0x838(%rbp)
je 0x44b81
leaq -0xa70(%rbp), %rdi
movq -0x838(%rbp), %rcx
movl $0x100, %esi            # imm = 0x100
leaq 0x957b8(%rip), %rdx     # 0xda2fc
movb $0x0, %al
callq 0x31090
cmpq $-0x1, -0x838(%rbp)
jne 0x44b5c
movb $0x0, -0xa70(%rbp)
movq -0x830(%rbp), %rsi
leaq -0xa70(%rbp), %rdx
leaq 0x97643(%rip), %rdi     # 0xdc1b4
movb $0x0, %al
callq 0x358e0
movl $0x0, -0x4(%rbp)
jmp 0x44b88
movl $0x1, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0xa90, %rsp            # imm = 0xA90
popq %rbp
retq
nopw %cs:(%rax,%rax)