// File: /workspace/github_cpp_makefile_star2_O2/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 $0x148, %rsp            # imm = 0x148
movq %rdi, %rbx
leaq 0xc40(%rdi), %r14
movq 0xdf8(%rdi), %rsi
movq %r14, %rdi
callq 0x33a0
movq 0xdf8(%rbx), %rdi
callq 0x3160
movq %rax, %r15
movq 0xd08(%rbx), %r13
pushq $0x2f
popq %rsi
movq %r13, %rdi
callq 0x3420
movq %rax, %r12
cmpl $0x1, 0xde4(%rbx)
movabsq $0x6d74682e676f6c2f, %rbp # imm = 0x6D74682E676F6C2F
jne 0x4c96
movb 0x1(%r12), %al
andb $-0x2, %al
cmpb $0x32, %al
jne 0x4c96
movl $0xc8, %edi
callq 0x3500
movq %rax, %rbp
movw $0x2f, (%rax)
addq $0x2, %r13
movq %rax, %rdi
movq %r13, %rsi
callq 0x3640
movslq %r15d, %rax
leaq (%r14,%rax), %rdi
movl $0xc7, %ecx
subl %eax, %ecx
movslq %ecx, %rdx
movq %rbp, %rsi
callq 0x3200
movq %rbp, %rdi
callq 0x3480
movq 0xde8(%rbx), %rax
xorl %edx, %edx
movabsq $0x100000000, %rsi      # imm = 0x100000000
xorl %ecx, %ecx
movb 0x5(%rax,%rcx), %dil
cmpb $0x26, %dil
je 0x4a07
movb %dil, 0x70(%rsp,%rcx)
incq %rcx
addq %rsi, %rdx
jmp 0x49ef
sarq $0x20, %rdx
movb $0x0, 0x70(%rsp,%rdx)
xorl %edx, %edx
movabsq $0x6d74682e676f6c2f, %rbp # imm = 0x6D74682E676F6C2F
leaq (%rax,%rdx), %rsi
movb 0xf(%rcx,%rsi), %sil
testb %sil, %sil
je 0x4a37
movb %sil, 0xe0(%rsp,%rdx)
incq %rdx
jmp 0x4a1c
movl %edx, %eax
movb $0x0, 0xe0(%rsp,%rax)
movzbl 0x1(%r12), %eax
cmpl $0x32, %eax
je 0x4b2e
cmpl $0x33, %eax
jne 0x4c96
movl $0xc8, %edi
callq 0x3500
movq %rax, %r13
movups 0x3836(%rip), %xmm0     # 0x82a3
movups %xmm0, (%rax)
movups 0x383c(%rip), %xmm0     # 0x82b3
movups %xmm0, 0x10(%rax)
movups 0x383c(%rip), %xmm0     # 0x82be
movups %xmm0, 0x1b(%rax)
movq %rax, %rdi
callq 0x3160
movw $0x27, (%r13,%rax)
leaq 0x70(%rsp), %rsi
movq %r13, %rdi
callq 0x3640
movq %r13, %rdi
callq 0x3160
xorl %ebp, %ebp
movb %bpl, 0x4(%r13,%rax)
movl $0x27202c27, (%r13,%rax) # imm = 0x27202C27
leaq 0xe0(%rsp), %rsi
movq %r13, %rdi
callq 0x3640
movq %r13, %rdi
callq 0x3160
movb %bpl, 0x2(%r13,%rax)
movw $0x2927, (%r13,%rax)    # imm = 0x2927
leaq 0x10(%rsp), %rdi
leaq 0x50(%rsp), %rdx
leaq 0x70(%rsp), %rsi
callq 0x3bd0
leaq 0x7945(%rip), %rdi      # 0xc440
leaq 0x10(%rsp), %rsi
callq 0x59ee
movq %rax, %rbp
leaq 0x10(%rsp), %rdi
callq 0x3190
leaq 0x792f(%rip), %rax      # 0xc448
cmpq %rax, %rbp
je 0x4be5
movq 0xd08(%rbx), %rax
jmp 0x4c4d
leaq 0x10(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0xf(%rsp), %rdx
callq 0x3bd0
leaq 0x78f7(%rip), %rdi      # 0xc440
leaq 0x10(%rsp), %rsi
callq 0x59ee
leaq 0x78ee(%rip), %rcx      # 0xc448
cmpq %rcx, %rax
je 0x4c6a
leaq 0x50(%rsp), %rdi
leaq 0x70(%rsp), %rsi
leaq 0xe(%rsp), %rdx
callq 0x3bd0
leaq 0x78c2(%rip), %rdi      # 0xc440
leaq 0x50(%rsp), %rsi
callq 0x553c
leaq 0xe0(%rsp), %rsi
movq %rax, %rdi
callq 0x552f
movl %eax, %ebp
leaq 0x50(%rsp), %rdi
callq 0x3190
leaq 0x10(%rsp), %rdi
callq 0x3190
testb %bpl, %bpl
movabsq $0x6d74682e676f6c2f, %rbp # imm = 0x6D74682E676F6C2F
je 0x4c74
movq 0xd08(%rbx), %rax
movabsq $0x6c6d74682e656d, %rcx # imm = 0x6C6D74682E656D
movq %rcx, 0x6(%rax)
movabsq $0x656d6f636c65772f, %rcx # imm = 0x656D6F636C65772F
jmp 0x4c93
leaq 0x782c(%rip), %rdi      # 0xc418
callq 0x54be
movq 0x8(%rbx), %rdi
movq %r13, %rsi
callq 0x32c0
movl %eax, %ebp
leaq 0x10(%rsp), %r13
leaq 0x70(%rsp), %rsi
leaq 0xe0(%rsp), %rdx
movq %r13, %rdi
callq 0x54cc
leaq 0x7820(%rip), %rdi      # 0xc440
movq %r13, %rsi
callq 0x5a2c
leaq 0x10(%rsp), %rdi
callq 0x550c
leaq 0x77df(%rip), %rdi      # 0xc418
callq 0x5522
movq 0xd08(%rbx), %rax
testl %ebp, %ebp
je 0x4e33
movups 0x368c(%rip), %xmm0     # 0x82e0
movups %xmm0, (%rax)
movl $0x6c6d74, 0x10(%rax)   # imm = 0x6C6D74
movabsq $0x6d74682e676f6c2f, %rbp # imm = 0x6D74682E676F6C2F
jmp 0x4c96
leaq 0x10(%rsp), %rdi
callq 0x3190
movq 0xd08(%rbx), %rax
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 0x4ce3
leaq 0x33f9(%rip), %rcx      # 0x80a4
movslq (%rcx,%rax,4), %rax
addq %rcx, %rax
jmpq *%rax
movl $0xc8, %edi
callq 0x3500
movq %rax, %r12
movabsq $0x657473696765722f, %rax # imm = 0x657473696765722F
movq %rax, (%r12)
movabsq $0x6c6d74682e7265, %rax # imm = 0x6C6D74682E7265
movq %rax, 0x7(%r12)
jmp 0x4d8f
movslq %r15d, %rax
leaq (%r14,%rax), %rdi
movq 0xd08(%rbx), %rsi
movl $0xc7, %ecx
subl %eax, %ecx
movslq %ecx, %rdx
callq 0x3200
jmp 0x4db3
movl $0xc8, %edi
callq 0x3500
movq %rax, %r12
movabsq $0x657275746369702f, %rax # imm = 0x657275746369702F
movq %rax, (%r12)
movabsq $0x6c6d74682e6572, %rax # imm = 0x6C6D74682E6572
movq %rax, 0x6(%r12)
jmp 0x4d8f
movl $0xc8, %edi
callq 0x3500
movq %rax, %r12
movq %rbp, (%rax)
movw $0x6c, 0x8(%rax)
movslq %r15d, %r15
addq %r14, %r15
movq %rax, %rdi
jmp 0x4d98
movl $0xc8, %edi
callq 0x3500
movq %rax, %r12
movabsq $0x682e6f656469762f, %rax # imm = 0x682E6F656469762F
jmp 0x4d82
movl $0xc8, %edi
callq 0x3500
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 0x3160
movq %r15, %rdi
movq %r12, %rsi
movq %rax, %rdx
callq 0x3200
movq %r12, %rdi
callq 0x3480
leaq 0xd30(%rbx), %rsi
movq %r14, %rdi
callq 0x3380
testl %eax, %eax
js 0x4dd4
movl 0xd48(%rbx), %eax
testb $0x4, %al
jne 0x4dd8
pushq $0x4
jmp 0x4e20
pushq $0x3
jmp 0x4e20
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x4de8
pushq $0x2
jmp 0x4e20
movq %r14, %rdi
xorl %esi, %esi
xorl %eax, %eax
callq 0x3300
movl %eax, %ebp
movq 0xd60(%rbx), %rsi
pushq $0x1
popq %rdx
pushq $0x2
popq %rcx
xorl %edi, %edi
movl %eax, %r8d
xorl %r9d, %r9d
callq 0x3650
movq %rax, 0xd28(%rbx)
movl %ebp, %edi
callq 0x3700
pushq $0x5
popq %rax
addq $0x148, %rsp            # imm = 0x148
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movabsq $0x6d74682e676f6c2f, %rbp # imm = 0x6D74682E676F6C2F
movq %rbp, (%rax)
movw $0x6c, 0x8(%rax)
jmp 0x4c96
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x550c
jmp 0x4e81
movq %rax, %rbx
leaq 0x50(%rsp), %rdi
callq 0x3190
jmp 0x4e70
jmp 0x4e6d
jmp 0x4e6d
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3190
jmp 0x4e81
jmp 0x4e7e
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3630
nop