// File: /workspace/github_c_makefile_star2_O1/sszllzss[P]webSocket_Libevent_ThreadPool/ev_httpd.cpp
// Function: httpd_handler_thread(void*)
void *httpd_handler_thread(void *arg)
{
    struct httpChilent_t *client = (struct httpChilent_t *)arg;
    struct httpServer_t *httpServer = client->httpServer;
    struct bufferevent * bev = client->bev;
    char respondKey[128];
    char c = 0;
    pthread_mutex_lock(&client->lock);
    gettimeofday(&client->final_optime, NULL);
    do
    {

        bufferevent_lock(bev);
        evbuffer_lock(client->receive_evbuff);
        bufferevent_disable(bev, EV_READ);
        while(1)
        {
            if(bufferevent_read(bev,&c,1) == 0 )
            {
                break;
            }
            evbuffer_add(client->receive_evbuff, &c , 1);
        }
        bufferevent_enable(bev, EV_READ);
        evbuffer_unlock(client->receive_evbuff);
        bufferevent_unlock(bev);


        pthread_mutex_unlock(&client->lock);
        int ret = accept_request(client);
        pthread_mutex_lock(&client->lock);
        if(ret != 0)
        {
            switch(ret)
            {
            case REQUEST_ERROR:
                client->resqonse.resqonseCode = HTTP_INTERNAL;
                break;
            case REQUEST_NOCONTENT:
                client->resqonse.resqonseCode = HTTP_NOCONTENT;
                break;
            case REQUEST_BADREQUEST:
                client->resqonse.resqonseCode = HTTP_BADREQUEST;
                break;
            case REQUEST_ENTITYTOOLARGE:
                client->resqonse.resqonseCode = HTTP_ENTITYTOOLARGE;
                break;
            case REQUEST_INTERNAL:
                client->resqonse.resqonseCode = HTTP_INTERNAL;
                break;
            case REQUEST_NOTIMPLEMENTED:
                client->resqonse.resqonseCode = HTTP_NOTIMPLEMENTED;
                break;
            default:
                client->resqonse.resqonseCode = HTTP_INTERNAL;
                break;
            }
            pthread_mutex_unlock(&client->lock);
            resqonse(client);
            pthread_mutex_lock(&client->lock);
            http_request_free(&client->request);
            http_request_free(&client->request);
            http_resqonse_free(&client->resqonse);
        }
        else
        {
            if(client->request.HeadParameter != NULL)
            {
                HeadParameterMap::iterator v = client->request.HeadParameter->find("Sec-WebSocket-Key");
                HeadParameterMap::iterator v1 = client->request.HeadParameter->find("Sec-WebSocket-Version");
                pthread_mutex_lock(&client->httpServer->lock);
                if(strcasecmp(client->request.method, "GET") == 0  && v1 != client->request.HeadParameter->end() && strcmp(v1->second.c_str(), "13") == 0  && v != client->request.HeadParameter->end() && strcmp(client->httpServer->webSocket_url, client->request.url) == 0)
                {
                    pthread_mutex_unlock(&client->httpServer->lock);
                    client->resqonse.resqonseCode = HTTP_SWITCHING_PROTOCOLS;
                    memset(respondKey, 0, sizeof(respondKey));
                    webSocket_buildRespondShakeKey((unsigned char *) v->second.c_str(), strlen(v->second.c_str()),( unsigned char *)respondKey);
                    client->resqonse.HeadParameter = new HeadParameterMap();
                    if(client->resqonse.HeadParameter == NULL)
                    {
                        client->resqonse.resqonseCode = HTTP_INTERNAL;
                        pthread_mutex_unlock(&client->lock);
                        resqonse(client);
                        pthread_mutex_lock(&client->lock);
                        http_request_free(&client->request);
                        http_resqonse_free(&client->resqonse);
                        break;
                    }
                    client->resqonse.HeadParameter->insert(HaedParameteMapPairi("Sec-WebSocket-Accept", respondKey));
                    pthread_mutex_unlock(&client->lock);
                    resqonse(client);
                    pthread_mutex_lock(&client->lock);
                    http_request_free(&client->request);
                    http_resqonse_free(&client->resqonse);
                    
                    break;
                }
                pthread_mutex_unlock(&client->httpServer->lock);
            }
            if(client->httpServer->handler != NULL)
            {
                httpd_handler_t handler;
                handler = client->httpServer->handler;
                client->resqonse.HeadParameter = new HeadParameterMap();
                pthread_mutex_unlock(&client->lock);
                handler(client);
                pthread_mutex_lock(&httpServer->clientMap_lock);
                BufferevMap::iterator v = httpServer->clientMap->find(bev);    
                if(httpServer->clientMap->end() == v)
                {
                    pthread_mutex_unlock(&httpServer->clientMap_lock);
                    return NULL;
                }
                pthread_mutex_unlock(&httpServer->clientMap_lock);
                pthread_mutex_lock(&client->lock);
                http_request_free(&client->request);
                http_resqonse_free(&client->resqonse);
            }
            else
            {
                //503错误
                client->resqonse.resqonseCode = HTTP_SERVUNAVAIL;
                pthread_mutex_unlock(&client->lock);
                resqonse(client);
                pthread_mutex_lock(&client->lock);
                http_request_free(&client->request);
                http_resqonse_free(&client->resqonse);

            }
        }
    }while(0);
    pthread_mutex_unlock(&client->lock);
    return NULL;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd8, %rsp
movq %rdi, %rbx
movq 0x28(%rdi), %rbp
movq 0x30(%rdi), %r14
leaq 0x7(%rsp), %r15
movb $0x0, (%r15)
callq 0x3620
leaq 0xb8(%rbx), %rdi
xorl %esi, %esi
callq 0x3690
movq %r14, %rdi
callq 0x3610
movq 0x98(%rbx), %rdi
callq 0x32d0
movq %r14, %rdi
movl $0x2, %esi
callq 0x3650
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x37f0
testq %rax, %rax
je 0x6b29
leaq 0x7(%rsp), %r15
movq 0x98(%rbx), %rdi
movl $0x1, %edx
movq %r15, %rsi
callq 0x3420
movl $0x1, %edx
movq %r14, %rdi
movq %r15, %rsi
callq 0x37f0
testq %rax, %rax
jne 0x6b00
movq %r14, %rdi
movl $0x2, %esi
callq 0x3790
movq 0x98(%rbx), %rdi
callq 0x3780
movq %r14, %rdi
callq 0x36e0
movq %rbx, %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x63cd
movl %eax, %r15d
movq %rbx, %rdi
callq 0x3620
addl $0x6, %r15d
leaq 0x49e0(%rip), %rax      # 0xb550
movslq (%rax,%r15,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x1f4, %eax            # imm = 0x1F4
jmp 0x6d56
movl $0x19d, %eax            # imm = 0x19D
jmp 0x6d56
movl $0x1f5, %eax            # imm = 0x1F5
jmp 0x6d56
movl $0xcc, %eax
jmp 0x6d56
leaq 0x40(%rbx), %rax
movq %rax, 0x8(%rsp)
movq 0x60(%rbx), %r12
testq %r12, %r12
je 0x6c7b
leaq 0x20(%rsp), %r15
movq %r15, -0x10(%r15)
leaq 0x5edd(%rip), %rsi      # 0xcaa4
leaq 0x5ee7(%rip), %rdx      # 0xcab5
leaq 0x10(%rsp), %rdi
callq 0x819a
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x8110
movq %rax, %r12
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x6bff
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3460
movq 0x60(%rbx), %r13
leaq 0x10(%rsp), %rdi
movq %r15, (%rdi)
leaq 0x5ea4(%rip), %rsi      # 0xcab6
leaq 0x5eb2(%rip), %rdx      # 0xcacb
callq 0x819a
leaq 0x10(%rsp), %rsi
movq %r13, %rdi
callq 0x8110
movq %rax, %r13
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x6c45
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3460
movq 0x28(%rbx), %rdi
callq 0x3620
movq 0x40(%rbx), %rdi
leaq 0x5e42(%rip), %rsi      # 0xca9b
callq 0x34a0
testl %eax, %eax
je 0x6ddd
movq 0x28(%rbx), %rdi
callq 0x3310
xorl %eax, %eax
movb $0x1, %cl
testb %cl, %cl
je 0x6d93
movq 0x28(%rbx), %rax
movq 0x88(%rax), %r15
testq %r15, %r15
je 0x6db3
movl $0x30, %edi
callq 0x3440
leaq 0x8(%rax), %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, 0x20(%rax)
movaps %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
movq $0x0, 0x28(%rax)
movq %rax, 0x80(%rbx)
movq %rbx, %rdi
callq 0x3310
movq %rbx, %rdi
callq *%r15
leaq 0x28(%rbp), %r12
movq %r12, %rdi
callq 0x3620
movq 0x50(%rbp), %r15
movq 0x10(%r15), %rcx
addq $0x8, %r15
movq %r15, %rax
testq %rcx, %rcx
je 0x6d0a
movq %r15, %rax
xorl %edx, %edx
cmpq %r14, 0x20(%rcx)
setb %dl
cmovaeq %rcx, %rax
movq 0x10(%rcx,%rdx,8), %rcx
testq %rcx, %rcx
jne 0x6cf3
movq %r15, %r13
cmpq %r15, %rax
je 0x6d1d
cmpq 0x20(%rax), %r14
cmovbq %r15, %rax
movq %rax, %r13
cmpq %r13, %r15
je 0x6f2e
leaq 0x78(%rbx), %r14
movq %r12, %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x3620
movq 0x8(%rsp), %rdi
callq 0x6a06
movq %r14, %rdi
callq 0x6f83
jmp 0x6f36
movl $0x190, %eax            # imm = 0x190
leaq 0x78(%rbx), %r14
movl %eax, 0x78(%rbx)
movq %rbx, %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x57d7
movq %rbx, %rdi
callq 0x3620
leaq 0x40(%rbx), %r15
movq %r15, %rdi
callq 0x6a06
movq %r15, %rdi
callq 0x6a06
movq %r14, %rdi
callq 0x6f83
xorl %eax, %eax
testb $0x1, %al
jne 0x6d9f
movq %rbx, %rdi
callq 0x3310
xorl %eax, %eax
addq $0xd8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
leaq 0x78(%rbx), %r14
movl $0x1f7, 0x78(%rbx)      # imm = 0x1F7
movq %rbx, %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x57d7
movq %rbx, %rdi
callq 0x3620
movq 0x8(%rsp), %rdi
jmp 0x6d84
movq 0x60(%rbx), %r15
addq $0x8, %r15
cmpq %r15, %r13
je 0x6c66
movq 0x40(%r13), %rdi
leaq 0x5cd3(%rip), %rsi      # 0xcacc
callq 0x35c0
testl %eax, %eax
sete %al
cmpq %r15, %r12
setne %cl
andb %al, %cl
cmpb $0x1, %cl
jne 0x6c66
movq 0x28(%rbx), %r13
movq 0x50(%rbx), %rsi
movq 0xb0(%r13), %rdi
callq 0x35c0
testl %eax, %eax
jne 0x6c66
movq %r13, %rdi
callq 0x3310
movl $0x65, 0x78(%rbx)
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r13
movaps %xmm0, (%r13)
movaps %xmm0, 0x10(%r13)
movaps %xmm0, 0x20(%r13)
movaps %xmm0, 0x30(%r13)
movaps %xmm0, 0x40(%r13)
movaps %xmm0, 0x50(%r13)
movaps %xmm0, 0x60(%r13)
movaps %xmm0, 0x70(%r13)
movq 0x40(%r12), %r12
movq %r12, %rdi
callq 0x31f0
movq %r12, %rdi
movl %eax, %esi
movq %r13, %rdx
callq 0x4583
movl $0x30, %edi
callq 0x3440
movq %rax, %r12
addq $0x8, %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%r12)
movaps %xmm0, 0x20(%r12)
movaps %xmm0, (%r12)
movq %rax, 0x18(%r12)
movq %rax, 0x20(%r12)
movq $0x0, 0x28(%r12)
movq %r12, 0x80(%rbx)
leaq 0x46f3(%rip), %rsi      # 0xb5c2
leaq 0x10(%rsp), %r15
movq %r15, %rdi
movq %r13, %rdx
callq 0x7e5a
movq %r12, %rdi
movq %r15, %rsi
callq 0x8268
leaq 0x78(%rbx), %r12
leaq 0x10(%rsp), %rdi
callq 0x7e24
movq %rbx, %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x57d7
movq %rbx, %rdi
callq 0x3620
movq 0x8(%rsp), %rdi
callq 0x6a06
movq %r12, %rdi
callq 0x6f83
movl $0x2, %eax
xorl %ecx, %ecx
jmp 0x6c73
movq %r12, %rdi
callq 0x3310
cmpq %r13, %r15
jne 0x6d91
movl $0x1, %eax
jmp 0x6d93
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x7e24
jmp 0x6f7b
jmp 0x6f5c
jmp 0x6f78
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r15, %rdi
je 0x6f7b
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3460
jmp 0x6f7b
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3740