// File: /workspace/github_cpp_makefile_star2_O3/JianjiaoSQ[P]MyPoorWebServer/http/http_conn.cpp
// Function: http_conn::do_request()
http_conn::HTTP_CODE http_conn::do_request()
{
    strcpy(m_real_file, doc_root);
    int len = strlen(doc_root);
    //printf("m_url:%s\n", m_url);
    const char *p = strrchr(m_url, '/');

    //处理cgi
    if (cgi == 1 && (*(p + 1) == '2' || *(p + 1) == '3'))
    {

        //根据标志判断是登录检测还是注册检测
        char flag = m_url[1];

        char *m_url_real = (char *)malloc(sizeof(char) * 200);
        strcpy(m_url_real, "/");
        strcat(m_url_real, m_url + 2);
        strncpy(m_real_file + len, m_url_real, FILENAME_LEN - len - 1);
        free(m_url_real);

        //将用户名和密码提取出来
        //user=123&passwd=123
        char name[100], password[100];
        int i;
        for (i = 5; m_string[i] != '&'; ++i)
            name[i - 5] = m_string[i];
        name[i - 5] = '\0';

        int j = 0;
        for (i = i + 10; m_string[i] != '\0'; ++i, ++j)
            password[j] = m_string[i];
        password[j] = '\0';

        if (*(p + 1) == '3')
        {
            //如果是注册，先检测数据库中是否有重名的
            //没有重名的，进行增加数据
            char *sql_insert = (char *)malloc(sizeof(char) * 200);
            strcpy(sql_insert, "INSERT INTO user(username, passwd) VALUES(");
            strcat(sql_insert, "'");
            strcat(sql_insert, name);
            strcat(sql_insert, "', '");
            strcat(sql_insert, password);
            strcat(sql_insert, "')");

            if (users.find(name) == users.end())  // 没有重名
            {
                m_lock.lock();
                int res = mysql_query(mysql, sql_insert); // 如果查询成功，返回0。如果出现错误，返回非0值。
                users.insert(pair<string, string>(name, password));
                m_lock.unlock();

                if (!res) // 数据查找成功进行注册
                    strcpy(m_url, "/log.html");
                else
                    strcpy(m_url, "/registerError.html");
            }
            else
                strcpy(m_url, "/registerError.html");
        }
        //如果是登录，直接判断
        //若浏览器端输入的用户名和密码在表中可以查找到，返回1，否则返回0
        else if (*(p + 1) == '2')
        {
            if (users.find(name) != users.end() && users[name] == password)
                strcpy(m_url, "/welcome.html");
            else
                strcpy(m_url, "/logError.html");
        }
    }

    if (*(p + 1) == '0')
    {
        char *m_url_real = (char *)malloc(sizeof(char) * 200);
        strcpy(m_url_real, "/register.html");
        strncpy(m_real_file + len, m_url_real, strlen(m_url_real));

        free(m_url_real);
    }
    else if (*(p + 1) == '1')
    {
        char *m_url_real = (char *)malloc(sizeof(char) * 200);
        strcpy(m_url_real, "/log.html");
        strncpy(m_real_file + len, m_url_real, strlen(m_url_real));

        free(m_url_real);
    }
    else if (*(p + 1) == '5')
    {
        char *m_url_real = (char *)malloc(sizeof(char) * 200);
        strcpy(m_url_real, "/picture.html");
        strncpy(m_real_file + len, m_url_real, strlen(m_url_real));

        free(m_url_real);
    }
    else if (*(p + 1) == '6')
    {
        char *m_url_real = (char *)malloc(sizeof(char) * 200);
        strcpy(m_url_real, "/video.html");
        strncpy(m_real_file + len, m_url_real, strlen(m_url_real));

        free(m_url_real);
    }
    else if (*(p + 1) == '7') // 关于页面的序号是怎么定的
    {
        char *m_url_real = (char *)malloc(sizeof(char) * 200);
        strcpy(m_url_real, "/audio.html");
        strncpy(m_real_file + len, m_url_real, strlen(m_url_real));

        free(m_url_real);
    }
    else
        strncpy(m_real_file + len, m_url, FILENAME_LEN - len - 1);

    if (stat(m_real_file, &m_file_stat) < 0)
        return NO_RESOURCE;

    if (!(m_file_stat.st_mode & S_IROTH))
        return FORBIDDEN_REQUEST;

    if (S_ISDIR(m_file_stat.st_mode))
        return BAD_REQUEST;

    int fd = open(m_real_file, O_RDONLY);
    m_file_address = (char *)mmap(0, m_file_stat.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
    close(fd);
    return FILE_REQUEST;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x138, %rsp            # imm = 0x138
movq %rdi, %rbx
leaq 0xc40(%rdi), %r14
movq 0xdf8(%rdi), %rsi
movq %r14, %rdi
callq 0x3370
movq 0xdf8(%rbx), %rdi
callq 0x3150
movq %rax, %r15
movq 0xd08(%rbx), %r13
movq %r13, %rdi
movl $0x2f, %esi
callq 0x33f0
movq %rax, %r12
cmpl $0x1, 0xde4(%rbx)
jne 0x4fd6
movb 0x1(%r12), %al
andb $-0x2, %al
cmpb $0x32, %al
jne 0x4fd6
movl $0xc8, %edi
callq 0x34c0
movq %rax, %rbp
movw $0x2f, (%rax)
addq $0x2, %r13
movq %rax, %rdi
movq %r13, %rsi
callq 0x35f0
movslq %r15d, %rax
leaq (%r14,%rax), %rdi
movl $0xc7, %ecx
subl %eax, %ecx
movslq %ecx, %rdx
movq %rbp, %rsi
callq 0x31e0
movq %rbp, %rdi
callq 0x3450
movq 0xde8(%rbx), %rax
movb 0x5(%rax), %dl
movl $0x5, %ecx
cmpb $0x26, %dl
je 0x4ccd
xorl %ecx, %ecx
movb %dl, 0x60(%rsp,%rcx)
movb 0x6(%rax,%rcx), %dl
incq %rcx
cmpb $0x26, %dl
jne 0x4cba
addl $0x5, %ecx
movslq %ecx, %rdx
leaq 0x60(%rsp), %rsi
movb $0x0, -0x5(%rdx,%rsi)
movb 0xa(%rax,%rdx), %dl
testb %dl, %dl
je 0x4d02
movl %ecx, %ecx
addq %rax, %rcx
addq $0xb, %rcx
xorl %eax, %eax
movb %dl, 0xd0(%rsp,%rax)
movb (%rcx,%rax), %dl
incq %rax
testb %dl, %dl
jne 0x4ced
movl %eax, %eax
jmp 0x4d04
xorl %eax, %eax
movb $0x0, 0xd0(%rsp,%rax)
movzbl 0x1(%r12), %eax
cmpl $0x32, %eax
je 0x4e1c
cmpl $0x33, %eax
jne 0x4fd6
movl $0xc8, %edi
callq 0x34c0
movq %rax, %r13
movups 0x456b(%rip), %xmm0     # 0x92a3
movups %xmm0, (%rax)
movups 0x4571(%rip), %xmm0     # 0x92b3
movups %xmm0, 0x10(%rax)
movups 0x4571(%rip), %xmm0     # 0x92be
movups %xmm0, 0x1b(%rax)
movq %rax, %rdi
callq 0x3150
movw $0x27, (%r13,%rax)
leaq 0x60(%rsp), %rsi
movq %r13, %rdi
callq 0x35f0
movq %r13, %rdi
callq 0x3150
xorl %ebp, %ebp
movb %bpl, 0x4(%r13,%rax)
movl $0x27202c27, (%r13,%rax) # imm = 0x27202C27
leaq 0xd0(%rsp), %rsi
movq %r13, %rdi
callq 0x35f0
movq %r13, %rdi
callq 0x3150
movb %bpl, 0x2(%r13,%rax)
movw $0x2927, (%r13,%rax)    # imm = 0x2927
leaq 0x10(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x60(%rsp), %rdi
callq 0x3150
leaq (%rsp,%rax), %rdx
addq $0x60, %rdx
movq %rsp, %rdi
leaq 0x60(%rsp), %rsi
callq 0x3d72
leaq 0x863e(%rip), %rdi      # 0xd418
movq %rsp, %rsi
callq 0x5f64
movq %rax, %rbp
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x4e00
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x33a0
leaq 0x8619(%rip), %rax      # 0xd420
cmpq %rax, %rbp
je 0x4ed7
movq 0xd08(%rbx), %rax
jmp 0x4f69
leaq 0x10(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x60(%rsp), %r13
movq %r13, %rdi
callq 0x3150
leaq (%rsp,%rax), %rdx
addq $0x60, %rdx
movq %rsp, %rdi
movq %r13, %rsi
callq 0x3d72
leaq 0x85cc(%rip), %rdi      # 0xd418
movq %rsp, %rsi
callq 0x5f64
leaq 0x85c5(%rip), %rcx      # 0xd420
cmpq %rcx, %rax
je 0x4f7c
leaq 0x50(%rsp), %rbp
movq %rbp, -0x10(%rbp)
leaq 0x60(%rsp), %r13
movq %r13, %rdi
callq 0x3150
leaq (%rsp,%rax), %rdx
addq $0x60, %rdx
leaq 0x40(%rsp), %rdi
movq %r13, %rsi
callq 0x3d72
leaq 0x8582(%rip), %rdi      # 0xd418
leaq 0x40(%rsp), %rsi
callq 0x5acc
leaq 0xd0(%rsp), %rsi
movq %rax, %rdi
callq 0x30d0
testl %eax, %eax
sete %r13b
movq 0x40(%rsp), %rdi
cmpq %rbp, %rdi
je 0x4ecd
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x33a0
leaq 0x10(%rsp), %rbp
jmp 0x4f7f
leaq 0x8512(%rip), %rdi      # 0xd3f0
callq 0x3500
movq 0x8(%rbx), %rdi
movq %r13, %rsi
callq 0x32a0
movl %eax, %ebp
movq %rsp, %r13
leaq 0x60(%rsp), %rsi
leaq 0xd0(%rsp), %rdx
movq %r13, %rdi
callq 0x5a18
leaq 0x8508(%rip), %rdi      # 0xd418
movq %r13, %rsi
callq 0x5faa
leaq 0x30(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x4f33
movq 0x30(%rsp), %rsi
incq %rsi
callq 0x33a0
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x4f4e
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x33a0
leaq 0x849b(%rip), %rdi      # 0xd3f0
callq 0x3300
movq 0xd08(%rbx), %rax
testl %ebp, %ebp
je 0x5185
movups 0x4370(%rip), %xmm0     # 0x92e0
movups %xmm0, (%rax)
movl $0x6c6d74, 0x10(%rax)   # imm = 0x6C6D74
jmp 0x4fd6
xorl %r13d, %r13d
movq (%rsp), %rdi
cmpq %rbp, %rdi
je 0x4f95
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x33a0
movq 0xd08(%rbx), %rax
testb %r13b, %r13b
je 0x4fbb
movabsq $0x6c6d74682e656d, %rcx # imm = 0x6C6D74682E656D
movq %rcx, 0x6(%rax)
movabsq $0x656d6f636c65772f, %rcx # imm = 0x656D6F636C65772F
jmp 0x4fd3
movabsq $0x6c6d74682e726f, %rcx # imm = 0x6C6D74682E726F
movq %rcx, 0x7(%rax)
movabsq $0x6f727245676f6c2f, %rcx # imm = 0x6F727245676F6C2F
movq %rcx, (%rax)
movzbl 0x1(%r12), %eax
addl $-0x30, %eax
cmpl $0x7, %eax
ja 0x5023
leaq 0x40b9(%rip), %rcx      # 0x90a4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0xc8, %edi
callq 0x34c0
movq %rax, %r12
movabsq $0x657473696765722f, %rax # imm = 0x657473696765722F
movq %rax, (%r12)
movabsq $0x6c6d74682e7265, %rax # imm = 0x6C6D74682E7265
movq %rax, 0x7(%r12)
jmp 0x50d3
movslq %r15d, %rax
leaq (%r14,%rax), %rdi
movq 0xd08(%rbx), %rsi
movl $0xc7, %ecx
subl %eax, %ecx
movslq %ecx, %rdx
callq 0x31e0
jmp 0x50f7
movl $0xc8, %edi
callq 0x34c0
movq %rax, %r12
movabsq $0x657275746369702f, %rax # imm = 0x657275746369702F
movq %rax, (%r12)
movabsq $0x6c6d74682e6572, %rax # imm = 0x6C6D74682E6572
movq %rax, 0x6(%r12)
jmp 0x50d3
movl $0xc8, %edi
callq 0x34c0
movq %rax, %r12
movabsq $0x6d74682e676f6c2f, %rax # imm = 0x6D74682E676F6C2F
movq %rax, (%r12)
movw $0x6c, 0x8(%r12)
jmp 0x50d3
movl $0xc8, %edi
callq 0x34c0
movq %rax, %r12
movabsq $0x682e6f656469762f, %rax # imm = 0x682E6F656469762F
jmp 0x50c6
movl $0xc8, %edi
callq 0x34c0
movq %rax, %r12
movabsq $0x682e6f696475612f, %rax # imm = 0x682E6F696475612F
movq %rax, (%r12)
movl $0x6c6d74, 0x8(%r12)    # imm = 0x6C6D74
movslq %r15d, %r15
addq %r14, %r15
movq %r12, %rdi
callq 0x3150
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x31e0
movq %r12, %rdi
callq 0x3450
leaq 0xd30(%rbx), %rsi
movq %r14, %rdi
callq 0x3350
testl %eax, %eax
js 0x516e
movl 0xd48(%rbx), %ecx
movl $0x4, %eax
testb $0x4, %cl
je 0x5173
andl $0xf000, %ecx           # imm = 0xF000
movl $0x2, %eax
cmpl $0x4000, %ecx           # imm = 0x4000
je 0x5173
movq %r14, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x32e0
movl %eax, %ebp
movq 0xd60(%rbx), %rsi
xorl %edi, %edi
movl $0x1, %edx
movl $0x2, %ecx
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x3600
movq %rax, 0xd28(%rbx)
movl %ebp, %edi
callq 0x36b0
movl $0x5, %eax
jmp 0x5173
movl $0x3, %eax
addq $0x138, %rsp            # imm = 0x138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movabsq $0x6d74682e676f6c2f, %rcx # imm = 0x6D74682E676F6C2F
movq %rcx, (%rax)
movw $0x6c, 0x8(%rax)
jmp 0x4fd6
movq %rax, %rbx
movq %rsp, %rdi
callq 0x5a96
jmp 0x51ef
movq %rax, %rbx
movq 0x40(%rsp), %rdi
cmpq %rbp, %rdi
je 0x51cd
movq 0x50(%rsp), %rsi
incq %rsi
callq 0x33a0
jmp 0x51cd
jmp 0x51ca
jmp 0x51ca
movq %rax, %rbx
movq (%rsp), %rdi
leaq 0x10(%rsp), %rax
cmpq %rax, %rdi
je 0x51ef
movq 0x10(%rsp), %rsi
incq %rsi
callq 0x33a0
jmp 0x51ef
jmp 0x51ec
movq %rax, %rbx
movq %rbx, %rdi
callq 0x35e0
nop