// File: /workspace/github_c_makefile_star2_O0/niuyuling[P]CProxy/http_request.c
// Function: extract_host
int extract_host(char *header, char *host, char *port)
{

//printf("%s\n", header);
    char *_p = strstr(header, "CONNECT"); // 在 CONNECT 方法中解析 隧道主机名称及端口号
    if (_p) {

        if (strchr(header, '[') || strchr(header, ']')) { // IPv6
            char *_p1 = strchr(header, '[');
            char *_p2 = strchr(_p1 + 1, ']');
            memcpy(host, _p1 + 1, (int)(_p2 - _p1) - 1);

            char *_p3 = strchr(_p2 + 1, ' ');
            memcpy(port, _p2 + 2, (int)(_p3 - _p2) - 1);

            return 0;
        }

        char *_p1 = strchr(_p, ' ');
        char *_p2 = strchr(_p1 + 1, ':');
        char *_p3 = strchr(_p1 + 1, ' ');

        if (_p2) {
            memcpy(host, _p1 + 1, (int)(_p2 - _p1) - 1);
            memcpy(port, _p2 + 1, (int)(_p3 - _p2) - 1);
        } else {                // 如果_p2等于空就返回-1
            return -1;
        }

        return 0;
    } else {

        char *_p = strstr(header, "Host:");
        if (_p == NULL) {
            _p = strstr(header, "host:");
        }
        if (_p == NULL) {       // 都为空时
            return -1;
        }

        char *_p1 = strchr(_p, '\n'); // 指向末尾'\n'
        if (!_p1) {
            return -1;
        }

        char *_p2 = strchr(_p + 5, ':'); // 5是指'Host:'的长度
        int h_len = (int)(_p1 - _p - 6);
        char s_host[h_len];
        memcpy(s_host, _p + 6, _p1 - _p - 6);
        s_host[h_len] = '\0';

        char *_p3 = strchr(s_host, ':');
        char *_p4 = NULL;
        if (_p3)
            _p4 = strchr(_p3 + 1, ':'); // 二次确认':'
        {                       // IPV6
            if (_p4 != NULL) {
                char *_p5 = NULL;
                char *_p6 = NULL;
                _p5 = strchr(header, ' ');
                if (_p5)
                    _p6 = strchr(_p5 + 1, ' ');

                char url[_p6 - _p5 - 1];
                memset(url, 0, _p6 - _p5 - 1);
                memcpy(url, _p5 + 1, _p6 - _p5 - 1);
                url[_p6 - _p5 - 1] = '\0';

                if (strstr(url, "http") != NULL) { // 去除 'http://'
                    memcpy(url, url + 7, (_p6 - _p5 - 1) - 7);
                    url[(_p6 - _p5 - 1) - 7] = '\0';
                    char *_p7 = strchr(url, '/');
                    if (_p7)    // 去除 uri
                        url[_p7 - url] = '\0';

                    char *_p8 = strchr(url, ']');
                    char *_p9 = strchr(url, '\0');
                    if (_p8) {
                        if ((_p9 - _p8) == 1) { // 如果不带端口就默认80, 并结束
                            memcpy(port, "80", 2);
                            memcpy(host, url + 1, _p8 - (url + 1));
                            return 0;
                        }

                        memcpy(port, _p8 + 2, _p9 - (_p8 + 2));
                        memcpy(host, url + 1, _p8 - (url + 1));
                    }
                    return 0;
                } else {        // HTTP头为不规范的url时处理Host, 主要Proxifier转发url为'/'时
                    //printf("s_host: %s\n", s_host);
                    char *_p1 = strchr(s_host, '[');
                    if (_p1 == NULL) // 涉及到自定义的Host, 不带'['、']'时, 默认截取最后为端口
                    {
                        char *_p2 = strrchr(s_host, ':');
                        remote_port = atoi(_p2 + 1);
                        memcpy(remote_host, s_host, _p2 - s_host);
                        return 0;
                    }

                    char *_p2 = strchr(_p1 + 1, ']');
                    char *_p3 = strchr(s_host, '\0');
                    if (_p1 && _p2) {
                        memcpy(host, _p1 + 1, _p2 - _p1 - 1);
                        if ((_p3-(_p2+1)) < 2) {
                            memcpy(port, "80", 2);
                        } else {
                            memcpy(port, _p2 + 2, _p3 - (_p2 + 2));
                        }

                    }
                    return 0;
                }

                return -1;
            }
        }
        // HTTP 非 CONNECT 方法
        {
            if (_p2 && _p2 < _p1) { // 带端口, p2指向':', p1指向末尾'\n', _p指向Host
                memcpy(port, _p2 + 1, (int)(_p1 - _p2 - 1));
                memcpy(host, _p + 5 + 1, (int)(_p2 - _p - 5 - 1));
            } else {            // 不带端口
                if (0 < (int)(_p1 - _p - 5 - 1 - 1)) {
                    memcpy(host, _p + 5 + 1, (_p1 - _p - 5 - 1 - 1));
                    memcpy(port, "80", 2);
                } else {
                    memcpy(host, _p + 5 + 1, (_p1 - _p) - 6);
                    memcpy(port, "80", 2);
                }
            }
        }

        return 0;
    }

    return 0;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x110, %rsp            # imm = 0x110
movq %rdi, -0x10(%rbp)
movq %rsi, -0x18(%rbp)
movq %rdx, -0x20(%rbp)
movq -0x10(%rbp), %rdi
leaq 0x73d8(%rip), %rsi      # 0xc3ca
callq 0x2570
movq %rax, -0x28(%rbp)
cmpq $0x0, -0x28(%rbp)
je 0x5163
movq -0x10(%rbp), %rdi
movl $0x5b, %esi
callq 0x2170
cmpq $0x0, %rax
jne 0x5032
movq -0x10(%rbp), %rdi
movl $0x5d, %esi
callq 0x2170
cmpq $0x0, %rax
je 0x50c0
movq -0x10(%rbp), %rdi
movl $0x5b, %esi
callq 0x2170
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
addq $0x1, %rdi
movl $0x5d, %esi
callq 0x2170
movq %rax, -0x38(%rbp)
movq -0x18(%rbp), %rdi
movq -0x30(%rbp), %rsi
addq $0x1, %rsi
movq -0x38(%rbp), %rax
movq -0x30(%rbp), %rcx
subq %rcx, %rax
subl $0x1, %eax
movslq %eax, %rdx
callq 0x2300
movq -0x38(%rbp), %rdi
addq $0x1, %rdi
movl $0x20, %esi
callq 0x2170
movq %rax, -0x40(%rbp)
movq -0x20(%rbp), %rdi
movq -0x38(%rbp), %rsi
addq $0x2, %rsi
movq -0x40(%rbp), %rax
movq -0x38(%rbp), %rcx
subq %rcx, %rax
subl $0x1, %eax
movslq %eax, %rdx
callq 0x2300
movl $0x0, -0x4(%rbp)
jmp 0x579e
movq -0x28(%rbp), %rdi
movl $0x20, %esi
callq 0x2170
movq %rax, -0x48(%rbp)
movq -0x48(%rbp), %rdi
addq $0x1, %rdi
movl $0x3a, %esi
callq 0x2170
movq %rax, -0x50(%rbp)
movq -0x48(%rbp), %rdi
addq $0x1, %rdi
movl $0x20, %esi
callq 0x2170
movq %rax, -0x58(%rbp)
cmpq $0x0, -0x50(%rbp)
je 0x514b
movq -0x18(%rbp), %rdi
movq -0x48(%rbp), %rsi
addq $0x1, %rsi
movq -0x50(%rbp), %rax
movq -0x48(%rbp), %rcx
subq %rcx, %rax
subl $0x1, %eax
movslq %eax, %rdx
callq 0x2300
movq -0x20(%rbp), %rdi
movq -0x50(%rbp), %rsi
addq $0x1, %rsi
movq -0x58(%rbp), %rax
movq -0x50(%rbp), %rcx
subq %rcx, %rax
subl $0x1, %eax
movslq %eax, %rdx
callq 0x2300
jmp 0x5157
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x579e
movl $0x0, -0x4(%rbp)
jmp 0x579e
movq -0x10(%rbp), %rdi
leaq 0x72a6(%rip), %rsi      # 0xc414
callq 0x2570
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x5192
movq -0x10(%rbp), %rdi
leaq 0x7291(%rip), %rsi      # 0xc41a
callq 0x2570
movq %rax, -0x60(%rbp)
cmpq $0x0, -0x60(%rbp)
jne 0x51a5
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x579e
movq -0x60(%rbp), %rdi
movl $0xa, %esi
callq 0x2170
movq %rax, -0x68(%rbp)
cmpq $0x0, -0x68(%rbp)
jne 0x51ca
movl $0xffffffff, -0x4(%rbp) # imm = 0xFFFFFFFF
jmp 0x579e
movq -0x60(%rbp), %rdi
addq $0x5, %rdi
movl $0x3a, %esi
callq 0x2170
movq %rax, -0x70(%rbp)
movl -0x68(%rbp), %eax
movl -0x60(%rbp), %ecx
subl %ecx, %eax
addl $-0x6, %eax
movl %eax, -0x74(%rbp)
movl -0x74(%rbp), %eax
movq %rsp, %rcx
movq %rcx, -0x80(%rbp)
movq %rax, %rcx
addq $0xf, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x100(%rbp)
movq %rdi, %rsp
movq %rax, -0x88(%rbp)
movq -0x60(%rbp), %rsi
addq $0x6, %rsi
movq -0x68(%rbp), %rdx
movq -0x60(%rbp), %rax
subq %rax, %rdx
subq $0x6, %rdx
callq 0x2300
movq -0x100(%rbp), %rdi
movslq -0x74(%rbp), %rax
movb $0x0, (%rdi,%rax)
movl $0x3a, %esi
callq 0x2170
movq %rax, -0x90(%rbp)
movq $0x0, -0x98(%rbp)
cmpq $0x0, -0x90(%rbp)
je 0x5287
movq -0x90(%rbp), %rdi
addq $0x1, %rdi
movl $0x3a, %esi
callq 0x2170
movq %rax, -0x98(%rbp)
cmpq $0x0, -0x98(%rbp)
je 0x5693
movq $0x0, -0xa0(%rbp)
movq $0x0, -0xa8(%rbp)
movq -0x10(%rbp), %rdi
movl $0x20, %esi
callq 0x2170
movq %rax, -0xa0(%rbp)
cmpq $0x0, -0xa0(%rbp)
je 0x52e6
movq -0xa0(%rbp), %rdi
addq $0x1, %rdi
movl $0x20, %esi
callq 0x2170
movq %rax, -0xa8(%rbp)
movq -0xa8(%rbp), %rcx
movq -0xa0(%rbp), %rax
subq %rax, %rcx
movq %rcx, %rax
decq %rax
movq %rsp, %rdx
movq %rdx, -0xb0(%rbp)
addq $0xe, %rcx
andq $-0x10, %rcx
movq %rsp, %rdi
subq %rcx, %rdi
movq %rdi, -0x108(%rbp)
movq %rdi, %rsp
movq %rax, -0xb8(%rbp)
movq -0xa8(%rbp), %rdx
movq -0xa0(%rbp), %rax
subq %rax, %rdx
subq $0x1, %rdx
xorl %esi, %esi
callq 0x21c0
movq -0x108(%rbp), %rdi
movq -0xa0(%rbp), %rsi
addq $0x1, %rsi
movq -0xa8(%rbp), %rdx
movq -0xa0(%rbp), %rax
subq %rax, %rdx
subq $0x1, %rdx
callq 0x2300
movq -0x108(%rbp), %rdi
movq -0xa8(%rbp), %rax
movq -0xa0(%rbp), %rcx
subq %rcx, %rax
subq $0x1, %rax
movb $0x0, (%rdi,%rax)
leaq 0x708b(%rip), %rsi      # 0xc420
callq 0x2570
cmpq $0x0, %rax
je 0x5523
movq -0x108(%rbp), %rdi
movq %rdi, %rsi
addq $0x7, %rsi
movq -0xa8(%rbp), %rdx
movq -0xa0(%rbp), %rax
subq %rax, %rdx
subq $0x1, %rdx
subq $0x7, %rdx
callq 0x2300
movq -0x108(%rbp), %rdi
movq -0xa8(%rbp), %rax
movq -0xa0(%rbp), %rcx
subq %rcx, %rax
subq $0x1, %rax
subq $0x7, %rax
movb $0x0, (%rdi,%rax)
movl $0x2f, %esi
callq 0x2170
movq %rax, -0xc0(%rbp)
cmpq $0x0, -0xc0(%rbp)
je 0x5424
movq -0x108(%rbp), %rax
movq -0xc0(%rbp), %rcx
subq %rax, %rcx
movb $0x0, (%rax,%rcx)
movq -0x108(%rbp), %rdi
movl $0x5d, %esi
callq 0x2170
movq -0x108(%rbp), %rdi
movq %rax, -0xc8(%rbp)
xorl %esi, %esi
callq 0x2170
movq %rax, -0xd0(%rbp)
cmpq $0x0, -0xc8(%rbp)
je 0x550d
movq -0xd0(%rbp), %rax
movq -0xc8(%rbp), %rcx
subq %rcx, %rax
cmpq $0x1, %rax
jne 0x54bf
movq -0x108(%rbp), %rax
movq -0x20(%rbp), %rcx
movw 0x6f9d(%rip), %dx       # 0xc425
movw %dx, (%rcx)
movq -0x18(%rbp), %rdi
movq %rax, %rsi
addq $0x1, %rsi
movq -0xc8(%rbp), %rdx
addq $0x1, %rax
subq %rax, %rdx
callq 0x2300
movl $0x0, -0x4(%rbp)
movl $0x1, -0xd4(%rbp)
jmp 0x5684
movq -0x20(%rbp), %rdi
movq -0xc8(%rbp), %rsi
addq $0x2, %rsi
movq -0xd0(%rbp), %rdx
movq -0xc8(%rbp), %rax
addq $0x2, %rax
subq %rax, %rdx
callq 0x2300
movq -0x108(%rbp), %rax
movq -0x18(%rbp), %rdi
movq %rax, %rsi
addq $0x1, %rsi
movq -0xc8(%rbp), %rdx
addq $0x1, %rax
subq %rax, %rdx
callq 0x2300
movl $0x0, -0x4(%rbp)
movl $0x1, -0xd4(%rbp)
jmp 0x5684
movq -0x100(%rbp), %rdi
movl $0x5b, %esi
callq 0x2170
movq %rax, -0xe0(%rbp)
cmpq $0x0, -0xe0(%rbp)
jne 0x55ab
movq -0x100(%rbp), %rdi
movl $0x3a, %esi
callq 0x21a0
movq %rax, -0xe8(%rbp)
movq -0xe8(%rbp), %rdi
addq $0x1, %rdi
callq 0x24a0
movq -0x100(%rbp), %rsi
movl %eax, %ecx
leaq 0x17133(%rip), %rax     # 0x1c6b0
movl %ecx, (%rax)
movq -0xe8(%rbp), %rdx
subq %rsi, %rdx
leaq 0x17010(%rip), %rdi     # 0x1c5a0
callq 0x2300
movl $0x0, -0x4(%rbp)
movl $0x1, -0xd4(%rbp)
jmp 0x5684
movq -0xe0(%rbp), %rdi
addq $0x1, %rdi
movl $0x5d, %esi
callq 0x2170
movq -0x100(%rbp), %rdi
movq %rax, -0xf0(%rbp)
xorl %esi, %esi
callq 0x2170
movq %rax, -0xf8(%rbp)
cmpq $0x0, -0xe0(%rbp)
je 0x5673
cmpq $0x0, -0xf0(%rbp)
je 0x5673
movq -0x18(%rbp), %rdi
movq -0xe0(%rbp), %rsi
addq $0x1, %rsi
movq -0xf0(%rbp), %rdx
movq -0xe0(%rbp), %rax
subq %rax, %rdx
subq $0x1, %rdx
callq 0x2300
movq -0xf8(%rbp), %rax
movq -0xf0(%rbp), %rcx
addq $0x1, %rcx
subq %rcx, %rax
cmpq $0x2, %rax
jge 0x5648
movq -0x20(%rbp), %rax
movw 0x6de2(%rip), %cx       # 0xc425
movw %cx, (%rax)
jmp 0x5671
movq -0x20(%rbp), %rdi
movq -0xf0(%rbp), %rsi
addq $0x2, %rsi
movq -0xf8(%rbp), %rdx
movq -0xf0(%rbp), %rax
addq $0x2, %rax
subq %rax, %rdx
callq 0x2300
jmp 0x5673
movl $0x0, -0x4(%rbp)
movl $0x1, -0xd4(%rbp)
movq -0xb0(%rbp), %rax
movq %rax, %rsp
jmp 0x5797
cmpq $0x0, -0x70(%rbp)
je 0x56f7
movq -0x70(%rbp), %rax
cmpq -0x68(%rbp), %rax
jae 0x56f7
movq -0x20(%rbp), %rdi
movq -0x70(%rbp), %rsi
addq $0x1, %rsi
movq -0x68(%rbp), %rax
movq -0x70(%rbp), %rcx
subq %rcx, %rax
subq $0x1, %rax
movslq %eax, %rdx
callq 0x2300
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x5, %rsi
addq $0x1, %rsi
movq -0x70(%rbp), %rax
movq -0x60(%rbp), %rcx
subq %rcx, %rax
subq $0x5, %rax
subq $0x1, %rax
movslq %eax, %rdx
callq 0x2300
jmp 0x5786
movq -0x68(%rbp), %rax
movq -0x60(%rbp), %rcx
subq %rcx, %rax
subq $0x5, %rax
subq $0x1, %rax
subq $0x1, %rax
movl %eax, %ecx
xorl %eax, %eax
cmpl %ecx, %eax
jge 0x5752
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x5, %rsi
addq $0x1, %rsi
movq -0x68(%rbp), %rdx
movq -0x60(%rbp), %rax
subq %rax, %rdx
subq $0x5, %rdx
subq $0x1, %rdx
subq $0x1, %rdx
callq 0x2300
movq -0x20(%rbp), %rax
movw 0x6cd8(%rip), %cx       # 0xc425
movw %cx, (%rax)
jmp 0x5784
movq -0x18(%rbp), %rdi
movq -0x60(%rbp), %rsi
addq $0x5, %rsi
addq $0x1, %rsi
movq -0x68(%rbp), %rdx
movq -0x60(%rbp), %rax
subq %rax, %rdx
subq $0x6, %rdx
callq 0x2300
movq -0x20(%rbp), %rax
movw 0x6ca4(%rip), %cx       # 0xc425
movw %cx, (%rax)
jmp 0x5786
movl $0x0, -0x4(%rbp)
movl $0x1, -0xd4(%rbp)
movq -0x80(%rbp), %rax
movq %rax, %rsp
movl -0x4(%rbp), %eax
movq %rbp, %rsp
popq %rbp
retq
nopw %cs:(%rax,%rax)