// File: /workspace/github_cpp_makefile_star2_O2/guoxuanhan[P]NetServer/httpsession.cpp
// Function: HttpSession::HttpProcess(_HttpRequestContext const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>&)
void HttpSession::HttpProcess(const HttpRequestContext &httprequestcontext, std::string &responsecontext)
{
    //std::cout << "HttpServer::HttpProcess" << std::endl;
    std::string responsebody;
    std::string errormsg;
    std::string path;
    std::string querystring;

    if("GET" == httprequestcontext.method)
    {
        ;
    }
    else if("POST" == httprequestcontext.method)
    {
        ;
    }
    else
    {
        std::cout << "HttpServer::HttpParser" << std::endl;
        errormsg = "Method Not Implemented";
        HttpError(501, "Method Not Implemented", httprequestcontext, responsecontext);
        return;
    }

    size_t pos = httprequestcontext.url.find("?");
    if(pos != std::string::npos)
    {
        path = httprequestcontext.url.substr(0, pos);
        querystring = httprequestcontext.url.substr(pos+1);
    }
    else
    {
        path = httprequestcontext.url;
    }

    //keepalive判断处理
    std::map<std::string, std::string>::const_iterator iter = httprequestcontext.header.find("Connection");
    if(iter != httprequestcontext.header.end())
    {
        keepalive_ = (iter->second == "Keep-Alive");
    }
    else
    {
        if(httprequestcontext.version == "HTTP/1.1")
        {
            keepalive_ = true;//HTTP/1.1默认长连接
        }
        else
        {
            keepalive_ = false;//HTTP/1.0默认短连接
        }
    }


    if("/" == path)
    {
        path = "/index.html";
    }
    else if("/hello" == path)
    {
        //Wenbbench 测试用
        std::string filetype("text/html");
        responsebody = ("hello world");
        responsecontext += httprequestcontext.version + " 200 OK\r\n";
        responsecontext += "Server: GuoJ's NetServer/0.1\r\n";
        responsecontext += "Content-Type: " + filetype + "; charset=utf-8\r\n";
        if(iter != httprequestcontext.header.end())
        {
            responsecontext += "Connection: " + iter->second + "\r\n";
        }
        responsecontext += "Content-Length: " + std::to_string(responsebody.size()) + "\r\n";
        responsecontext += "\r\n";
        responsecontext += responsebody;
        return;
    }
    else
    {
        ;
    }

    //std::string responsebody;
    path.insert(0,".");
    FILE* fp = NULL;
    if((fp = fopen(path.c_str(), "rb")) == NULL)
    {
        //perror("error fopen");
        //404 NOT FOUND
        HttpError(404, "Not Found", httprequestcontext, responsecontext);
        return;
    }
    else
    {
        char buffer[4096];
        memset(buffer, 0, sizeof(buffer));
        while(fread(buffer, sizeof(buffer), 1, fp) == 1)//可以mmap内存映射优化
        {
            responsebody.append(buffer);
            memset(buffer, 0, sizeof(buffer));
        }
        if(feof(fp))
        {
            responsebody.append(buffer);
        }
        else
        {
            std::cout << "error fread" << std::endl;
        }
        fclose(fp);
    }

    std::string filetype("text/html"); //暂时固定为html
    responsecontext += httprequestcontext.version + " 200 OK\r\n";
    responsecontext += "Server: GuoJ's NetServer/0.1\r\n";
    responsecontext += "Content-Type: " + filetype + "; charset=utf-8\r\n";
    if(iter != httprequestcontext.header.end())
    {
        responsecontext += "Connection: " + iter->second + "\r\n";
    }
    responsecontext += "Content-Length: " + std::to_string(responsebody.size()) + "\r\n";
    responsecontext += "\r\n";
    responsecontext += responsebody;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x1138, %rsp           # imm = 0x1138
movq %rdx, 0x40(%rsp)
movq %rsi, %r14
movq %rdi, %r12
leaq 0x80(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
xorl %ecx, %ecx
movb %cl, (%rax)
leaq 0x120(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
leaq 0x60(%rsp), %rdx
movq %rdx, -0x10(%rdx)
andq $0x0, -0x8(%rdx)
movb %cl, (%rax)
movb %cl, (%rdx)
leaq 0x100(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb %cl, (%rax)
leaq 0x63b2(%rip), %rdi      # 0xeab0
callq 0x92a6
testb %al, %al
jne 0x871e
leaq 0x63a6(%rip), %rdi      # 0xeab4
movq %r14, %rsi
callq 0x92a6
testb %al, %al
je 0x8a8d
leaq 0x20(%r14), %r15
leaq 0x63be(%rip), %rsi      # 0xeae7
movq %r15, %rdi
xorl %edx, %edx
callq 0x45f0
cmpq $-0x1, %rax
je 0x87a9
movq %rax, %r13
leaq 0x130(%rsp), %rdi
movq %r15, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4330
leaq 0x50(%rsp), %rdi
leaq 0x130(%rsp), %rbx
movq %rbx, %rsi
callq 0x43c0
movq %rbx, %rdi
callq 0x4170
incq %r13
leaq 0x130(%rsp), %rdi
pushq $-0x1
popq %rcx
movq %r15, %rsi
movq %r13, %rdx
callq 0x4330
leaq 0xf0(%rsp), %rdi
leaq 0x130(%rsp), %rbx
movq %rbx, %rsi
callq 0x43c0
movq %rbx, %rdi
callq 0x4170
jmp 0x87b6
leaq 0x50(%rsp), %rdi
movq %r15, %rsi
callq 0x41e0
leaq 0x632c(%rip), %rsi      # 0xeae9
leaq 0x130(%rsp), %rdi
movq %rsp, %rdx
callq 0x4130
leaq 0x60(%r14), %rdi
leaq 0x130(%rsp), %rsi
callq 0x97d8
movq %rax, %r15
leaq 0x130(%rsp), %rdi
callq 0x4170
leaq 0x68(%r14), %rax
movq %rax, 0x48(%rsp)
cmpq %rax, %r15
movq %r15, %rdi
cmoveq %r14, %rdi
leaq 0x62f7(%rip), %rax      # 0xeaff
leaq 0x62e5(%rip), %rsi      # 0xeaf4
cmoveq %rax, %rsi
addq $0x40, %rdi
callq 0x92bc
movb %al, 0x158(%r12)
leaq 0x62dd(%rip), %rdi      # 0xeb08
leaq 0x50(%rsp), %rsi
callq 0x92a6
testb %al, %al
je 0x884f
leaq 0x62ca(%rip), %rsi      # 0xeb0a
leaq 0x50(%rsp), %rdi
callq 0x45c0
jmp 0x89f1
leaq 0x62c0(%rip), %rdi      # 0xeb16
leaq 0x50(%rsp), %rsi
callq 0x92a6
testb %al, %al
je 0x89f1
leaq 0x62ae(%rip), %rsi      # 0xeb1d
leaq 0x130(%rsp), %rdi
movq %rsp, %rdx
callq 0x4130
movq 0x40(%rsp), %rbx
leaq 0x629c(%rip), %rsi      # 0xeb27
leaq 0x70(%rsp), %rdi
callq 0x45c0
addq $0x40, %r14
leaq 0x6293(%rip), %rdx      # 0xeb33
movq %rsp, %rdi
movq %r14, %rsi
callq 0x92c9
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x45d0
movq %rsp, %rdi
callq 0x4170
leaq 0x6278(%rip), %rsi      # 0xeb3d
movq %rbx, %rdi
callq 0x45b0
leaq 0x6288(%rip), %rsi      # 0xeb5c
leaq 0x20(%rsp), %rdi
leaq 0x130(%rsp), %rdx
callq 0x9320
leaq 0x627e(%rip), %rdx      # 0xeb6b
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x9301
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x45d0
movq %rsp, %rdi
callq 0x4170
leaq 0x20(%rsp), %rdi
callq 0x4170
cmpq 0x48(%rsp), %r15
je 0x8967
addq $0x40, %r15
leaq 0x6254(%rip), %rsi      # 0xeb7d
leaq 0x20(%rsp), %rdi
movq %r15, %rdx
callq 0x9320
leaq 0x60fc(%rip), %rdx      # 0xea39
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x9301
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x45d0
movq %rsp, %rdi
callq 0x4170
leaq 0x20(%rsp), %rdi
callq 0x4170
movq 0x78(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x93ba
leaq 0x620a(%rip), %rsi      # 0xeb8a
leaq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %rdx
callq 0x9396
leaq 0x60a0(%rip), %rdx      # 0xea39
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x9301
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x45d0
movq %rsp, %rdi
callq 0x4170
leaq 0x20(%rsp), %rdi
callq 0x4170
leaq 0x90(%rsp), %rdi
callq 0x4170
leaq 0x6062(%rip), %rsi      # 0xea39
movq %rbx, %rdi
callq 0x45b0
leaq 0x70(%rsp), %rsi
movq %rbx, %rdi
callq 0x45d0
jmp 0x8cd8
leaq 0x6648(%rip), %rdx      # 0xf040
leaq 0x50(%rsp), %rdi
xorl %esi, %esi
callq 0x40a0
movq 0x50(%rsp), %rdi
leaq 0x618b(%rip), %rsi      # 0xeb9b
callq 0x4390
testq %rax, %rax
je 0x8aff
movq %rax, %r12
leaq 0x130(%rsp), %r13
pushq $0x1
popq %rbp
leaq 0x70(%rsp), %rbx
movl $0x1000, %edx           # imm = 0x1000
movq %r13, %rdi
xorl %esi, %esi
callq 0x41f0
movl $0x1000, %esi           # imm = 0x1000
movq %r13, %rdi
movq %rbp, %rdx
movq %r12, %rcx
callq 0x4470
cmpq $0x1, %rax
jne 0x8a66
movq %rbx, %rdi
movq %r13, %rsi
callq 0x45b0
jmp 0x8a31
movq %r12, %rdi
callq 0x4490
testl %eax, %eax
je 0x8b42
leaq 0x70(%rsp), %rdi
leaq 0x130(%rsp), %rsi
callq 0x45b0
jmp 0x8b5d
movq 0xf524(%rip), %rdi      # 0x17fb8
leaq 0x601e(%rip), %rsi      # 0xeab9
callq 0x4300
movq %rax, %rdi
callq 0x41a0
leaq 0x6021(%rip), %rsi      # 0xead0
leaq 0x110(%rsp), %rdi
callq 0x45c0
leaq 0x600d(%rip), %rsi      # 0xead0
leaq 0xd0(%rsp), %rdi
leaq 0x130(%rsp), %rdx
callq 0x4130
movq 0x40(%rsp), %r8
leaq 0xd0(%rsp), %rdx
movl $0x1f5, %esi            # imm = 0x1F5
movq %r14, %rcx
callq 0x8e30
leaq 0xd0(%rsp), %rdi
jmp 0x8ce0
leaq 0x6098(%rip), %rsi      # 0xeb9e
leaq 0xb0(%rsp), %rdi
leaq 0x130(%rsp), %rdx
callq 0x4130
leaq 0xb0(%rsp), %rdx
movl $0x194, %esi            # imm = 0x194
movq %r14, %rcx
movq 0x40(%rsp), %r8
callq 0x8e30
leaq 0xb0(%rsp), %rdi
jmp 0x8ce0
movq 0xf46f(%rip), %rdi      # 0x17fb8
leaq 0x6058(%rip), %rsi      # 0xeba8
callq 0x4300
movq %rax, %rdi
callq 0x41a0
movq %r12, %rdi
callq 0x4310
leaq 0x5fb1(%rip), %rsi      # 0xeb1d
leaq 0x130(%rsp), %rdi
movq %rsp, %rdx
callq 0x4130
addq $0x40, %r14
leaq 0x5fac(%rip), %rdx      # 0xeb33
movq %rsp, %rdi
movq %r14, %rsi
callq 0x92c9
movq 0x40(%rsp), %rbx
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x45d0
movq %rsp, %rdi
callq 0x4170
leaq 0x5f8c(%rip), %rsi      # 0xeb3d
movq %rbx, %rdi
callq 0x45b0
leaq 0x5f9c(%rip), %rsi      # 0xeb5c
leaq 0x20(%rsp), %rdi
leaq 0x130(%rsp), %rdx
callq 0x9320
leaq 0x5f92(%rip), %rdx      # 0xeb6b
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x9301
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x45d0
movq %rsp, %rdi
callq 0x4170
leaq 0x20(%rsp), %rdi
callq 0x4170
cmpq 0x48(%rsp), %r15
je 0x8c53
addq $0x40, %r15
leaq 0x5f68(%rip), %rsi      # 0xeb7d
leaq 0x20(%rsp), %rdi
movq %r15, %rdx
callq 0x9320
leaq 0x5e10(%rip), %rdx      # 0xea39
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x9301
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x45d0
movq %rsp, %rdi
callq 0x4170
leaq 0x20(%rsp), %rdi
callq 0x4170
movq 0x78(%rsp), %rsi
leaq 0x90(%rsp), %rdi
callq 0x93ba
leaq 0x5f1e(%rip), %rsi      # 0xeb8a
leaq 0x20(%rsp), %rdi
leaq 0x90(%rsp), %rdx
callq 0x9396
leaq 0x5db4(%rip), %rdx      # 0xea39
movq %rsp, %rdi
leaq 0x20(%rsp), %rsi
callq 0x9301
movq %rsp, %rsi
movq %rbx, %rdi
callq 0x45d0
movq %rsp, %rdi
callq 0x4170
leaq 0x20(%rsp), %rdi
callq 0x4170
leaq 0x90(%rsp), %rdi
callq 0x4170
leaq 0x5d76(%rip), %rsi      # 0xea39
movq %rbx, %rdi
callq 0x45b0
leaq 0x70(%rsp), %rsi
movq %rbx, %rdi
callq 0x45d0
leaq 0x130(%rsp), %rdi
callq 0x4170
leaq 0xf0(%rsp), %rdi
callq 0x4170
leaq 0x50(%rsp), %rdi
callq 0x4170
leaq 0x110(%rsp), %rdi
callq 0x4170
leaq 0x70(%rsp), %rdi
callq 0x4170
addq $0x1138, %rsp           # imm = 0x1138
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0x8dab
jmp 0x8db8
jmp 0x8de5
jmp 0x8d80
jmp 0x8d8d
jmp 0x8d9c
jmp 0x8de5
jmp 0x8dab
jmp 0x8db8
jmp 0x8de5
jmp 0x8dc4
jmp 0x8de5
jmp 0x8df7
movq %rax, %rbx
leaq 0xb0(%rsp), %rdi
jmp 0x8df0
jmp 0x8df7
jmp 0x8dab
jmp 0x8db8
jmp 0x8de5
movq %rax, %rbx
leaq 0xd0(%rsp), %rdi
jmp 0x8df0
jmp 0x8df7
jmp 0x8df7
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4170
jmp 0x8d90
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x4170
jmp 0x8d9f
movq %rax, %rbx
leaq 0x90(%rsp), %rdi
jmp 0x8dca
jmp 0x8de5
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4170
jmp 0x8dbb
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
jmp 0x8dca
jmp 0x8de5
movq %rax, %rbx
movq %rsp, %rdi
callq 0x4170
jmp 0x8de8
jmp 0x8de5
jmp 0x8df7
jmp 0x8df7
jmp 0x8df7
jmp 0x8df7
jmp 0x8de5
jmp 0x8de5
jmp 0x8df7
jmp 0x8df7
jmp 0x8df7
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x4170
jmp 0x8dfa
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x4170
leaq 0x50(%rsp), %rdi
callq 0x4170
leaq 0x110(%rsp), %rdi
callq 0x4170
leaq 0x70(%rsp), %rdi
callq 0x4170
movq %rbx, %rdi
callq 0x4570