// File: /workspace/github_c_makefile_star2_O3/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), %r15
leaq 0x7(%rsp), %r14
movb $0x0, (%r14)
callq 0x3620
leaq 0xb8(%rbx), %rdi
xorl %esi, %esi
callq 0x3690
movq %r15, %rdi
callq 0x3610
movq 0x98(%rbx), %rdi
callq 0x32d0
movq %r15, %rdi
movl $0x2, %esi
callq 0x3650
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x37f0
testq %rax, %rax
je 0x6b85
leaq 0x7(%rsp), %r14
movq 0x98(%rbx), %rdi
movl $0x1, %edx
movq %r14, %rsi
callq 0x3420
movl $0x1, %edx
movq %r15, %rdi
movq %r14, %rsi
callq 0x37f0
testq %rax, %rax
jne 0x6b5c
movq %r15, %rdi
movl $0x2, %esi
callq 0x3790
movq 0x98(%rbx), %rdi
callq 0x3780
movq %r15, %rdi
callq 0x36e0
movq %rbx, %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x643e
movl %eax, %r14d
movq %rbx, %rdi
callq 0x3620
addl $0x6, %r14d
leaq 0x4984(%rip), %rax      # 0xb550
movslq (%rax,%r14,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x1f4, %eax            # imm = 0x1F4
jmp 0x6d70
movl $0x19d, %eax            # imm = 0x19D
jmp 0x6d70
movl $0x1f5, %eax            # imm = 0x1F5
jmp 0x6d70
movl $0xcc, %eax
jmp 0x6d70
leaq 0x40(%rbx), %r13
movq 0x60(%rbx), %r12
testq %r12, %r12
je 0x6cce
leaq 0x20(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x5e86(%rip), %rsi      # 0xcaa4
leaq 0x5e90(%rip), %rdx      # 0xcab5
leaq 0x10(%rsp), %rdi
callq 0x8130
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x80a6
movq %rax, 0x8(%rsp)
movq %r13, %r12
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0x6c5b
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3460
movq 0x60(%rbx), %r13
leaq 0x10(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x5e48(%rip), %rsi      # 0xcab6
leaq 0x5e56(%rip), %rdx      # 0xcacb
callq 0x8130
leaq 0x10(%rsp), %rsi
movq %r13, %rdi
callq 0x80a6
movq %rax, %r13
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0x6ca1
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3460
movq 0x28(%rbx), %rdi
callq 0x3620
movq 0x40(%rbx), %rdi
leaq 0x5de6(%rip), %rsi      # 0xca9b
callq 0x34a0
testl %eax, %eax
je 0x6de7
movq 0x28(%rbx), %rdi
callq 0x3310
movq %r12, %r13
movq 0x28(%rbx), %rax
movq 0x88(%rax), %r14
testq %r14, %r14
je 0x6dc7
movl $0x30, %edi
callq 0x3440
leaq 0x8(%rax), %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%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 *%r14
leaq 0x28(%rbp), %r12
movq %r12, %rdi
callq 0x3620
movq 0x50(%rbp), %rax
movq 0x10(%rax), %rcx
testq %rcx, %rcx
je 0x6d66
addq $0x8, %rax
movq %rax, %rdx
xorl %esi, %esi
cmpq %r15, 0x20(%rcx)
setb %sil
cmovaeq %rcx, %rdx
movq 0x10(%rcx,%rsi,8), %rcx
testq %rcx, %rcx
jne 0x6d3f
cmpq %rax, %rdx
je 0x6d66
cmpq 0x20(%rdx), %r15
jae 0x6f24
movq %r12, %rdi
jmp 0x6dae
movl $0x190, %eax            # imm = 0x190
leaq 0x78(%rbx), %r15
movl %eax, 0x78(%rbx)
movq %rbx, %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x587b
movq %rbx, %rdi
callq 0x3620
leaq 0x40(%rbx), %r14
movq %r14, %rdi
callq 0x6a62
movq %r14, %rdi
callq 0x6a62
movq %r15, %rdi
callq 0x6f7a
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), %r15
movl $0x1f7, 0x78(%rbx)      # imm = 0x1F7
movq %rbx, %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x587b
jmp 0x6f30
movq 0x60(%rbx), %rax
addq $0x8, %rax
cmpq %rax, %r13
je 0x6cc2
movq 0x40(%r13), %rcx
cmpb $0x31, (%rcx)
jne 0x6cc2
cmpb $0x33, 0x1(%rcx)
jne 0x6cc2
cmpb $0x0, 0x2(%rcx)
sete %cl
cmpq %rax, 0x8(%rsp)
setne %al
andb %cl, %al
cmpb $0x1, %al
jne 0x6cc2
movq 0x28(%rbx), %r13
movq 0x50(%rbx), %rsi
movq 0xb0(%r13), %rdi
callq 0x35c0
testl %eax, %eax
jne 0x6cc2
movq %r13, %rdi
callq 0x3310
movl $0x65, 0x78(%rbx)
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r15
movaps %xmm0, (%r15)
movaps %xmm0, 0x10(%r15)
movaps %xmm0, 0x20(%r15)
movaps %xmm0, 0x30(%r15)
movaps %xmm0, 0x40(%r15)
movaps %xmm0, 0x50(%r15)
movaps %xmm0, 0x60(%r15)
movaps %xmm0, 0x70(%r15)
movq 0x8(%rsp), %rax
movq 0x40(%rax), %r14
movq %r14, %rdi
callq 0x31f0
movq %r14, %rdi
movl %eax, %esi
movq %r15, %rdx
callq 0x4675
movl $0x30, %edi
callq 0x3440
movq %rax, %r14
addq $0x8, %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%r14)
movaps %xmm0, (%r14)
movq %rax, 0x18(%r14)
movq %rax, 0x20(%r14)
movq $0x0, 0x28(%r14)
movq %r14, 0x80(%rbx)
leaq 0x46e7(%rip), %rsi      # 0xb5c2
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r15, %rdx
callq 0x7df6
movq %r14, %rdi
movq %r13, %rsi
callq 0x8204
leaq 0x78(%rbx), %r15
leaq 0x10(%rsp), %rdi
callq 0x7dc0
movq %rbx, %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x587b
movq %rbx, %rdi
callq 0x3620
movq %r12, %rdi
jmp 0x6d9e
leaq 0x78(%rbx), %r15
movq %r12, %rdi
callq 0x3310
movq %rbx, %rdi
callq 0x3620
movq %r13, %rdi
jmp 0x6d9e
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x7dc0
jmp 0x6f72
jmp 0x6f53
jmp 0x6f6f
movq %rax, %rbx
movq 0x10(%rsp), %rdi
cmpq %r14, %rdi
je 0x6f72
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x3460
jmp 0x6f72
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3740