// File: /workspace/github_c_makefile_star2_O2/niuyuling[P]CProxy/http_request.c
// Function: parse_request_head
void parse_request_head(char *http_request_line, struct http_request *http_request)
{
    char *p, *head, *m, *u;
    size_t head_len;
    int host_len, port_len, uri_len;
    host_len = port_len = uri_len = 0;

    p = strstr(http_request_line, "\r\n"); // 查找"\r\n"
    if (p == NULL) {
        return;
    }

    head_len = p - http_request_line;
    head = (char *)malloc(sizeof(char) * head_len + 1);
    if (head == NULL)
        free(head);
    memset(head, 0, sizeof(char) * head_len + 1);
    memcpy(head, http_request_line, head_len);

    http_request->method = (char *)malloc(sizeof(char) * 8);
    http_request->U = (char *)malloc(sizeof(char) * head_len);
    http_request->version = (char *)malloc(10);
    if (http_request->method == NULL)
        perror("malloc");
    if (http_request->U == NULL)
        perror("malloc");
    if (http_request->version == NULL)
        perror("malloc");
    memset(http_request->method, 0, 8);
    memset(http_request->U, 0, sizeof(char) * head_len);
    memset(http_request->version, 0, 10);

    m = strchr(head, ' ');
    http_request->method_len = m - head;
    memmove(http_request->method, head, http_request->method_len);

    u = strchr(m + 1, ' ');
    memmove(http_request->U, m + 1, u - (m + 1));
    http_request->U_len = u - (m + 1);

    memmove(http_request->version, u + 1, 8);
    http_request->version_len = 8;

    // 获取Host、Port长度
    get_http_host_port_len(http_request_line, &host_len, &port_len);

    // URI LENGTH
    char *_p0 = strstr(http_request->U, "http://");
    if (_p0) {                  // 标准头
        char *_p1 = strchr(http_request->U + 7, '/');
        char *_p2 = strchr(http_request->U + 7, '\0');
        if (_p1) {
            uri_len = _p2 - _p1;
        }
    } else {                    // 非标准头
        char *_p1 = strchr(http_request->U, '/');
        char *_p2 = strchr(http_request->U, '\0');
        if (_p1) {
            uri_len = _p2 - _p1;
        } else {
            uri_len = 1;        // 没有uri时
        }
    }

    http_request->host = (char *)malloc(sizeof(char) * host_len + 1);
    if (http_request->host == NULL)
        perror("malloc");
    http_request->port = (char *)malloc(sizeof(char) * port_len + 1);
    if (http_request->port == NULL)
        perror("malloc");
    http_request->url = (char *)malloc(sizeof(char) * http_request->U_len + 1);
    if (http_request->url == NULL)
        perror("malloc");
    http_request->uri = (char *)malloc(sizeof(char) * uri_len + 1);
    if (http_request->uri == NULL)
        perror("malloc");
    http_request->H = (char *)malloc(sizeof(char) * host_len + port_len + 1);
    if (http_request->H == NULL)
        perror("malloc");

    memset(http_request->host, 0, host_len + 1);
    memset(http_request->port, 0, port_len + 1);
    memset(http_request->url, 0, http_request->U_len + 1);
    memset(http_request->uri, 0, uri_len + 1);
    memset(http_request->H, 0, host_len + port_len + 1);
    if (extract_host(http_request_line, http_request->host, http_request->port) == -1)
        return;
    http_request->host_len = (int)strlen(http_request->host);
    http_request->port_len = (int)strlen(http_request->port);
    memcpy(http_request->H, http_request->host, http_request->host_len);
    strncat(http_request->H, ":", 2);
    strncat(http_request->H, http_request->port, http_request->port_len);
    memcpy(http_request->url, http_request->U, http_request->U_len);
    http_request->url_len = http_request->U_len;
    if (get_http_path(http_request->url, http_request->url_len, http_request->uri) == NULL)
        return;

    http_request->uri_len = uri_len;
    http_request->H_len = http_request->host_len + http_request->port_len + 1;

/* 
   // 调试
   printf("%s %d\n", http_request->method, http_request->method_len);
   printf("%s %d\n", http_request->U, http_request->U_len);
   printf("%s %d\n", http_request->version, http_request->version_len);
   printf("%s %d\n", http_request->host, http_request->host_len);
   printf("%s %d\n", http_request->port, http_request->port_len);
   printf("%s %d\n", http_request->H, http_request->H_len);
   printf("%s %d\n", http_request->url, http_request->url_len);
   printf("%s %d\n", http_request->uri, http_request->uri_len);
*/

    free(head);
    return;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x18, %rsp
movq %rsi, %rbx
andl $0x0, (%rsp)
movq %rdi, %r15
andl $0x0, 0x4(%rsp)
leaq 0x4c43(%rip), %rsi      # 0x9385
callq 0x2540
testq %rax, %rax
je 0x4a48
movq %rax, %r13
subq %r15, %r13
leaq 0x1(%r13), %rsi
pushq $0x1
popq %rdi
callq 0x2270
movq %rax, %r14
movq %rax, %rdi
movq %r15, %rsi
movq %r13, %rdx
callq 0x22e0
pushq $0x8
popq %rdi
callq 0x2330
movq %rax, %rbp
movq %rax, (%rbx)
movq %r13, %rdi
callq 0x2330
movq %rax, %r12
movq %rax, 0x8(%rbx)
pushq $0xa
popq %rdi
callq 0x2330
movq %rax, 0x10(%rbx)
testq %rbp, %rbp
je 0x4a60
testq %r12, %r12
je 0x4a75
cmpq $0x0, 0x10(%rbx)
je 0x4a86
movq (%rbx), %rax
andq $0x0, (%rax)
movq 0x8(%rbx), %rdi
xorl %r12d, %r12d
xorl %esi, %esi
movq %r13, %rdx
callq 0x21b0
movq 0x10(%rbx), %rax
andq $0x0, (%rax)
andw $0x0, 0x8(%rax)
pushq $0x20
popq %rbp
movq %r14, %rdi
movl %ebp, %esi
callq 0x2160
movq %rax, %r13
subq %r14, %rax
movl %eax, 0x40(%rbx)
movq (%rbx), %rdi
movslq %eax, %rdx
movq %r14, 0x8(%rsp)
movq %r14, %rsi
callq 0x2430
incq %r13
movq %r13, %rdi
movl %ebp, %esi
callq 0x2160
movq %rax, %r14
movq 0x8(%rbx), %rdi
movq %rax, %rbp
subq %r13, %rbp
movq %r13, %rsi
movq %rbp, %rdx
callq 0x2430
movl %ebp, 0x44(%rbx)
movq 0x10(%rbx), %rax
movq 0x1(%r14), %rcx
movq %rcx, (%rax)
movl $0x8, 0x48(%rbx)
leaq 0x4(%rsp), %rsi
movq %rsp, %rdx
movq %r15, %rdi
callq 0x45f7
movq 0x8(%rbx), %r13
leaq 0x4b33(%rip), %rsi      # 0x938f
movq %r13, %rdi
callq 0x2540
testq %rax, %rax
je 0x487f
addq $0x7, %r13
pushq $0x2f
popq %rsi
movq %r13, %rdi
callq 0x2160
testq %rax, %rax
jne 0x4893
jmp 0x48a7
pushq $0x2f
popq %rsi
movq %r13, %rdi
callq 0x2160
testq %rax, %rax
je 0x4a57
movq %rax, %rbp
movq %r13, %rdi
callq 0x2140
movq %rax, %r12
addl %r13d, %r12d
subl %ebp, %r12d
movq %r15, 0x10(%rsp)
movslq 0x4(%rsp), %r14
leaq 0x1(%r14), %rbp
movq %rbp, %rdi
callq 0x2330
movq %rax, 0x18(%rbx)
testq %rax, %rax
je 0x4a97
movslq (%rsp), %r15
leaq 0x1(%r15), %r13
movq %r13, %rdi
callq 0x2330
movq %rax, 0x20(%rbx)
testq %rax, %rax
je 0x4aa8
movslq 0x44(%rbx), %rdi
incq %rdi
callq 0x2330
movq %rax, 0x30(%rbx)
testq %rax, %rax
je 0x4ab9
movslq %r12d, %rdi
incq %rdi
callq 0x2330
movq %rax, 0x38(%rbx)
testq %rax, %rax
je 0x4aca
addq %r15, %rbp
movq %rbp, %rdi
callq 0x2330
movq %rax, 0x28(%rbx)
testq %rax, %rax
je 0x4adb
movq 0x18(%rbx), %rdi
leal 0x1(%r14), %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x21b0
movq 0x20(%rbx), %rdi
xorl %esi, %esi
movq %r13, %rdx
callq 0x21b0
movq 0x30(%rbx), %rdi
movslq 0x44(%rbx), %rdx
incq %rdx
xorl %esi, %esi
callq 0x21b0
movq 0x38(%rbx), %rdi
leal 0x1(%r12), %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x21b0
movq 0x28(%rbx), %rdi
leal (%r14,%r15), %eax
incl %eax
movslq %eax, %rdx
xorl %esi, %esi
callq 0x21b0
movq 0x18(%rbx), %rsi
movq 0x20(%rbx), %rdx
movq 0x10(%rsp), %rdi
callq 0x414e
cmpl $-0x1, %eax
je 0x4a48
movq 0x18(%rbx), %r14
movq %r14, %rdi
callq 0x2140
movq %rax, %r15
movl %r15d, 0x4c(%rbx)
movq 0x20(%rbx), %rdi
callq 0x2140
movl %eax, 0x50(%rbx)
movq 0x28(%rbx), %rdi
movslq %r15d, %rdx
movq %r14, %rsi
callq 0x22e0
movq 0x28(%rbx), %r14
movq %r14, %rdi
callq 0x2140
movw $0x3a, (%r14,%rax)
movq 0x20(%rbx), %rsi
movq 0x28(%rbx), %rdi
movslq 0x50(%rbx), %rdx
callq 0x21d0
movq 0x8(%rbx), %rsi
movq 0x30(%rbx), %rdi
movslq 0x44(%rbx), %rdx
callq 0x22e0
movl 0x44(%rbx), %esi
movl %esi, 0x58(%rbx)
movq 0x30(%rbx), %rdi
movq 0x38(%rbx), %rdx
callq 0x4592
testq %rax, %rax
je 0x4a48
movl %r12d, 0x5c(%rbx)
movl 0x4c(%rbx), %eax
movl 0x50(%rbx), %ecx
addl %ecx, %eax
incl %eax
movl %eax, 0x54(%rbx)
movq 0x8(%rsp), %rdi
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0x2030
addq $0x18, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x1
popq %r12
jmp 0x48a7
leaq 0x4921(%rip), %rdi      # 0x9388
callq 0x2460
movq 0x8(%rbx), %r12
jmp 0x47a5
leaq 0x490c(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x47ae
leaq 0x48fb(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x47b9
leaq 0x48ea(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x48ca
leaq 0x48d9(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x48e7
leaq 0x48c8(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x4900
leaq 0x48b7(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x4918
leaq 0x48a6(%rip), %rdi      # 0x9388
callq 0x2460
jmp 0x4930