// File: 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
movq %rsp, %rbp
subq $0x310, %rsp            # imm = 0x310
movq %rdi, -0x10(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x258(%rbp)
movq %rax, %rdi
addq $0xc40, %rdi            # imm = 0xC40
movq 0xdf8(%rax), %rsi
callq 0x3430
movq -0x258(%rbp), %rax
movq 0xdf8(%rax), %rdi
callq 0x31b0
movq %rax, %rcx
movq -0x258(%rbp), %rax
movl %ecx, -0x14(%rbp)
movq 0xd08(%rax), %rdi
movl $0x2f, %esi
callq 0x34f0
movq %rax, %rcx
movq -0x258(%rbp), %rax
movq %rcx, -0x20(%rbp)
cmpl $0x1, 0xde4(%rax)
jne 0x6111
movq -0x20(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x32, %eax
je 0x5a6a
movq -0x20(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x33, %eax
jne 0x6111
movq -0x258(%rbp), %rax
movq 0xd08(%rax), %rax
movb 0x1(%rax), %al
movb %al, -0x21(%rbp)
movl $0xc8, %edi
callq 0x35d0
movq %rax, -0x30(%rbp)
movq -0x30(%rbp), %rdi
leaq 0x87bb(%rip), %rsi      # 0xe252
callq 0x3430
movq -0x258(%rbp), %rax
movq -0x30(%rbp), %rdi
movq 0xd08(%rax), %rsi
addq $0x2, %rsi
callq 0x3730
movq -0x258(%rbp), %rdi
addq $0xc40, %rdi            # imm = 0xC40
movslq -0x14(%rbp), %rax
addq %rax, %rdi
movq -0x30(%rbp), %rsi
movl $0xc8, %eax
subl -0x14(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdx
callq 0x3240
movq -0x30(%rbp), %rdi
callq 0x3550
movl $0x5, -0x114(%rbp)
movq -0x258(%rbp), %rax
movq 0xde8(%rax), %rax
movslq -0x114(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x26, %eax
je 0x5b4f
movq -0x258(%rbp), %rax
movq 0xde8(%rax), %rax
movslq -0x114(%rbp), %rcx
movb (%rax,%rcx), %cl
movl -0x114(%rbp), %eax
subl $0x5, %eax
cltq
movb %cl, -0xa0(%rbp,%rax)
movl -0x114(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x114(%rbp)
jmp 0x5af6
movl -0x114(%rbp), %eax
subl $0x5, %eax
cltq
movb $0x0, -0xa0(%rbp,%rax)
movl $0x0, -0x118(%rbp)
movl -0x114(%rbp), %eax
addl $0xa, %eax
movl %eax, -0x114(%rbp)
movq -0x258(%rbp), %rax
movq 0xde8(%rax), %rax
movslq -0x114(%rbp), %rcx
movsbl (%rax,%rcx), %eax
cmpl $0x0, %eax
je 0x5bdf
movq -0x258(%rbp), %rax
movq 0xde8(%rax), %rax
movslq -0x114(%rbp), %rcx
movb (%rax,%rcx), %cl
movslq -0x118(%rbp), %rax
movb %cl, -0x110(%rbp,%rax)
movl -0x114(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x114(%rbp)
movl -0x118(%rbp), %eax
addl $0x1, %eax
movl %eax, -0x118(%rbp)
jmp 0x5b7b
movslq -0x118(%rbp), %rax
movb $0x0, -0x110(%rbp,%rax)
movq -0x20(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x33, %eax
jne 0x5ea0
movl $0xc8, %edi
callq 0x35d0
movq %rax, -0x120(%rbp)
movq -0x120(%rbp), %rdi
leaq 0x868d(%rip), %rsi      # 0xe2ab
callq 0x3430
movq -0x120(%rbp), %rdi
leaq 0x86a8(%rip), %rsi      # 0xe2d9
callq 0x3730
movq -0x120(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
movq %rsi, -0x268(%rbp)
callq 0x3730
movq -0x120(%rbp), %rdi
leaq 0x8678(%rip), %rsi      # 0xe2d6
callq 0x3730
movq -0x120(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x3730
movq -0x120(%rbp), %rdi
leaq 0x8657(%rip), %rsi      # 0xe2db
callq 0x3730
leaq -0x149(%rbp), %rdi
movq %rdi, -0x260(%rbp)
callq 0x3720
movq -0x268(%rbp), %rsi
movq -0x260(%rbp), %rdx
leaq -0x148(%rbp), %rdi
callq 0x3ea0
jmp 0x5cb8
leaq 0xe7f9(%rip), %rdi      # 0x144b8
leaq -0x148(%rbp), %rsi
callq 0x6fb0
movq %rax, -0x270(%rbp)
jmp 0x5cd4
movq -0x270(%rbp), %rax
movq %rax, -0x128(%rbp)
leaq 0xe7cf(%rip), %rdi      # 0x144b8
callq 0x6fe0
movq %rax, -0x168(%rbp)
leaq -0x128(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x6f80
movb %al, -0x271(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x32c0
leaq -0x149(%rbp), %rdi
callq 0x34a0
movb -0x271(%rbp), %al
testb $0x1, %al
jne 0x5d35
jmp 0x5e81
leaq 0xe754(%rip), %rdi      # 0x14490
callq 0x7010
movq -0x258(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x120(%rbp), %rsi
callq 0x3310
movl %eax, -0x16c(%rbp)
leaq -0x1b0(%rbp), %rdi
movq %rdi, -0x290(%rbp)
leaq -0xa0(%rbp), %rsi
leaq -0x110(%rbp), %rdx
callq 0x7080
movq -0x290(%rbp), %rsi
leaq 0xe72b(%rip), %rdi      # 0x144b8
callq 0x7040
movb %dl, -0x281(%rbp)
movq %rax, -0x280(%rbp)
jmp 0x5da1
movb -0x281(%rbp), %al
movq -0x280(%rbp), %rcx
movq %rcx, -0x1c0(%rbp)
movb %al, -0x1b8(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x7160
leaq 0xe6c2(%rip), %rdi      # 0x14490
callq 0x7190
cmpl $0x0, -0x16c(%rbp)
jne 0x5e65
movq -0x258(%rbp), %rax
movq 0xd08(%rax), %rdi
leaq 0x84e9(%rip), %rsi      # 0xe2de
callq 0x3430
jmp 0x5e7f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x158(%rbp)
movl %eax, -0x15c(%rbp)
jmp 0x5e31
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x158(%rbp)
movl %eax, -0x15c(%rbp)
leaq -0x148(%rbp), %rdi
callq 0x32c0
leaq -0x149(%rbp), %rdi
callq 0x34a0
jmp 0x6504
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x158(%rbp)
movl %eax, -0x15c(%rbp)
leaq -0x1b0(%rbp), %rdi
callq 0x7160
jmp 0x6504
movq -0x258(%rbp), %rax
movq 0xd08(%rax), %rdi
leaq 0x846e(%rip), %rsi      # 0xe2e8
callq 0x3430
jmp 0x5e9b
movq -0x258(%rbp), %rax
movq 0xd08(%rax), %rdi
leaq 0x8452(%rip), %rsi      # 0xe2e8
callq 0x3430
jmp 0x610f
movq -0x20(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x32, %eax
jne 0x610d
leaq -0x1e9(%rbp), %rdi
movq %rdi, -0x298(%rbp)
callq 0x3720
movq -0x298(%rbp), %rdx
movb $0x0, -0x21a(%rbp)
movb $0x0, -0x21b(%rbp)
leaq -0x1e8(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x3ea0
jmp 0x5eee
leaq 0xe5c3(%rip), %rdi      # 0x144b8
leaq -0x1e8(%rbp), %rsi
callq 0x6fb0
movq %rax, -0x2a0(%rbp)
jmp 0x5f0a
movq -0x2a0(%rbp), %rax
movq %rax, -0x1c8(%rbp)
leaq 0xe599(%rip), %rdi      # 0x144b8
callq 0x6fe0
movq %rax, -0x1f8(%rbp)
leaq -0x1c8(%rbp), %rdi
leaq -0x1f8(%rbp), %rsi
callq 0x71c0
movb %al, %cl
xorl %eax, %eax
testb $0x1, %cl
movb %al, -0x2a1(%rbp)
jne 0x5f52
jmp 0x5fd4
leaq -0x219(%rbp), %rdi
movq %rdi, -0x2b0(%rbp)
callq 0x3720
movq -0x2b0(%rbp), %rdx
movb $0x1, -0x21a(%rbp)
leaq -0x218(%rbp), %rdi
leaq -0xa0(%rbp), %rsi
callq 0x3ea0
jmp 0x5f88
movb $0x1, -0x21b(%rbp)
leaq 0xe522(%rip), %rdi      # 0x144b8
leaq -0x218(%rbp), %rsi
callq 0x7220
movq %rax, -0x2b8(%rbp)
jmp 0x5fab
movq -0x2b8(%rbp), %rdi
leaq -0x110(%rbp), %rsi
callq 0x71f0
movb %al, -0x2b9(%rbp)
jmp 0x5fc6
movb -0x2b9(%rbp), %al
movb %al, -0x2a1(%rbp)
jmp 0x5fd4
movb -0x2a1(%rbp), %al
movb %al, -0x2ba(%rbp)
testb $0x1, -0x21b(%rbp)
jne 0x5feb
jmp 0x5ff7
leaq -0x218(%rbp), %rdi
callq 0x32c0
testb $0x1, -0x21a(%rbp)
jne 0x6002
jmp 0x600e
leaq -0x219(%rbp), %rdi
callq 0x34a0
leaq -0x1e8(%rbp), %rdi
callq 0x32c0
leaq -0x1e9(%rbp), %rdi
callq 0x34a0
movb -0x2ba(%rbp), %al
testb $0x1, %al
jne 0x6035
jmp 0x60f1
movq -0x258(%rbp), %rax
movq 0xd08(%rax), %rdi
leaq 0x82b2(%rip), %rsi      # 0xe2fc
callq 0x3430
jmp 0x610b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x158(%rbp)
movl %eax, -0x15c(%rbp)
jmp 0x60e0
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x158(%rbp)
movl %eax, -0x15c(%rbp)
jmp 0x60d4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x158(%rbp)
movl %eax, -0x15c(%rbp)
jmp 0x60bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x158(%rbp)
movl %eax, -0x15c(%rbp)
testb $0x1, -0x21b(%rbp)
jne 0x60ad
jmp 0x60b9
leaq -0x218(%rbp), %rdi
callq 0x32c0
jmp 0x60bb
testb $0x1, -0x21a(%rbp)
jne 0x60c6
jmp 0x60d2
leaq -0x219(%rbp), %rdi
callq 0x34a0
jmp 0x60d4
leaq -0x1e8(%rbp), %rdi
callq 0x32c0
leaq -0x1e9(%rbp), %rdi
callq 0x34a0
jmp 0x6504
movq -0x258(%rbp), %rax
movq 0xd08(%rax), %rdi
leaq 0x8204(%rip), %rsi      # 0xe30a
callq 0x3430
jmp 0x610d
jmp 0x610f
jmp 0x6111
movq -0x20(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x30, %eax
jne 0x61a2
movl $0xc8, %edi
callq 0x35d0
movq %rax, -0x228(%rbp)
movq -0x228(%rbp), %rdi
leaq 0x81d8(%rip), %rsi      # 0xe319
callq 0x3430
movq -0x258(%rbp), %rax
addq $0xc40, %rax            # imm = 0xC40
movslq -0x14(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x2d0(%rbp)
movq -0x228(%rbp), %rax
movq %rax, -0x2c8(%rbp)
movq -0x228(%rbp), %rdi
callq 0x31b0
movq -0x2d0(%rbp), %rdi
movq -0x2c8(%rbp), %rsi
movq %rax, %rdx
callq 0x3240
movq -0x228(%rbp), %rdi
callq 0x3550
jmp 0x6419
movq -0x20(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x31, %eax
jne 0x6233
movl $0xc8, %edi
callq 0x35d0
movq %rax, -0x230(%rbp)
movq -0x230(%rbp), %rdi
leaq 0x810c(%rip), %rsi      # 0xe2de
callq 0x3430
movq -0x258(%rbp), %rax
addq $0xc40, %rax            # imm = 0xC40
movslq -0x14(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x2e0(%rbp)
movq -0x230(%rbp), %rax
movq %rax, -0x2d8(%rbp)
movq -0x230(%rbp), %rdi
callq 0x31b0
movq -0x2e0(%rbp), %rdi
movq -0x2d8(%rbp), %rsi
movq %rax, %rdx
callq 0x3240
movq -0x230(%rbp), %rdi
callq 0x3550
jmp 0x6417
movq -0x20(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x35, %eax
jne 0x62c4
movl $0xc8, %edi
callq 0x35d0
movq %rax, -0x238(%rbp)
movq -0x238(%rbp), %rdi
leaq 0x80c5(%rip), %rsi      # 0xe328
callq 0x3430
movq -0x258(%rbp), %rax
addq $0xc40, %rax            # imm = 0xC40
movslq -0x14(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x2f0(%rbp)
movq -0x238(%rbp), %rax
movq %rax, -0x2e8(%rbp)
movq -0x238(%rbp), %rdi
callq 0x31b0
movq -0x2f0(%rbp), %rdi
movq -0x2e8(%rbp), %rsi
movq %rax, %rdx
callq 0x3240
movq -0x238(%rbp), %rdi
callq 0x3550
jmp 0x6415
movq -0x20(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x36, %eax
jne 0x6355
movl $0xc8, %edi
callq 0x35d0
movq %rax, -0x240(%rbp)
movq -0x240(%rbp), %rdi
leaq 0x8042(%rip), %rsi      # 0xe336
callq 0x3430
movq -0x258(%rbp), %rax
addq $0xc40, %rax            # imm = 0xC40
movslq -0x14(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x300(%rbp)
movq -0x240(%rbp), %rax
movq %rax, -0x2f8(%rbp)
movq -0x240(%rbp), %rdi
callq 0x31b0
movq -0x300(%rbp), %rdi
movq -0x2f8(%rbp), %rsi
movq %rax, %rdx
callq 0x3240
movq -0x240(%rbp), %rdi
callq 0x3550
jmp 0x6413
movq -0x20(%rbp), %rax
movsbl 0x1(%rax), %eax
cmpl $0x37, %eax
jne 0x63df
movl $0xc8, %edi
callq 0x35d0
movq %rax, -0x248(%rbp)
movq -0x248(%rbp), %rdi
leaq 0x7fc1(%rip), %rsi      # 0xe342
callq 0x3430
movq -0x258(%rbp), %rax
addq $0xc40, %rax            # imm = 0xC40
movslq -0x14(%rbp), %rcx
addq %rcx, %rax
movq %rax, -0x310(%rbp)
movq -0x248(%rbp), %rax
movq %rax, -0x308(%rbp)
movq -0x248(%rbp), %rdi
callq 0x31b0
movq -0x310(%rbp), %rdi
movq -0x308(%rbp), %rsi
movq %rax, %rdx
callq 0x3240
movq -0x248(%rbp), %rdi
callq 0x3550
jmp 0x6411
movq -0x258(%rbp), %rax
movq %rax, %rdi
addq $0xc40, %rdi            # imm = 0xC40
movslq -0x14(%rbp), %rcx
addq %rcx, %rdi
movq 0xd08(%rax), %rsi
movl $0xc8, %eax
subl -0x14(%rbp), %eax
subl $0x1, %eax
movslq %eax, %rdx
callq 0x3240
jmp 0x6413
jmp 0x6415
jmp 0x6417
jmp 0x6419
movq -0x258(%rbp), %rsi
movq %rsi, %rdi
addq $0xc40, %rdi            # imm = 0xC40
addq $0xd30, %rsi            # imm = 0xD30
callq 0x3410
cmpl $0x0, %eax
jge 0x6447
movl $0x3, -0x4(%rbp)
jmp 0x64f8
movq -0x258(%rbp), %rax
movl 0xd48(%rax), %eax
andl $0x4, %eax
cmpl $0x0, %eax
jne 0x6468
movl $0x4, -0x4(%rbp)
jmp 0x64f8
movq -0x258(%rbp), %rax
movl 0xd48(%rax), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x648a
movl $0x2, -0x4(%rbp)
jmp 0x64f8
movq -0x258(%rbp), %rdi
addq $0xc40, %rdi            # imm = 0xC40
xorl %esi, %esi
movb $0x0, %al
callq 0x3360
movl %eax, %ecx
movq -0x258(%rbp), %rax
movl %ecx, -0x24c(%rbp)
movq 0xd60(%rax), %rsi
movl -0x24c(%rbp), %r8d
xorl %eax, %eax
movl %eax, %r9d
movl $0x1, %edx
movl $0x2, %ecx
movq %r9, %rdi
callq 0x3740
movq %rax, %rcx
movq -0x258(%rbp), %rax
movq %rcx, 0xd28(%rax)
movl -0x24c(%rbp), %edi
callq 0x37e0
movl $0x5, -0x4(%rbp)
movl -0x4(%rbp), %eax
addq $0x310, %rsp            # imm = 0x310
popq %rbp
retq
movq -0x158(%rbp), %rdi
callq 0x3710