// File: /workspace/github_c_makefile_star2_O2/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 %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xd0, %rsp
movq %rdi, %rbx
movq 0x28(%rdi), %r13
movq 0x30(%rdi), %r14
movq %r14, 0x8(%rsp)
leaq 0x7(%rsp), %r15
movb $0x0, (%r15)
callq 0x3650
leaq 0xb8(%rbx), %rdi
xorl %esi, %esi
callq 0x36c0
movq %r14, %rdi
callq 0x3640
movq 0x98(%rbx), %rdi
callq 0x32e0
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0x3680
pushq $0x1
popq %r12
movq %r14, %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3820
testq %rax, %rax
je 0x5f4f
movq 0x98(%rbx), %rdi
movq %r15, %rsi
movq %r12, %rdx
callq 0x3430
jmp 0x5f28
pushq $0x2
popq %rsi
movq %r14, %rdi
callq 0x37c0
movq 0x98(%rbx), %rdi
callq 0x37b0
movq %r14, %rdi
callq 0x3710
movq %rbx, %rdi
callq 0x3320
movq %rbx, %rdi
callq 0x5929
movl %eax, %r14d
movq %rbx, %rdi
callq 0x3650
addl $0x6, %r14d
leaq 0x45bc(%rip), %rax      # 0xa550
movslq (%rax,%r14,4), %rcx
addq %rax, %rcx
jmpq *%rcx
movl $0x1f4, %eax            # imm = 0x1F4
jmp 0x60ee
movl $0x19d, %eax            # imm = 0x19D
jmp 0x60ee
movl $0x1f5, %eax            # imm = 0x1F5
jmp 0x60ee
movl $0xcc, %eax
jmp 0x60ee
leaq 0x40(%rbx), %r14
movq 0x60(%rbx), %r15
testq %r15, %r15
je 0x6064
leaq 0x5ac7(%rip), %rsi      # 0xbaa4
leaq 0x10(%rsp), %rdi
leaq 0x6(%rsp), %rdx
callq 0x73f2
leaq 0x10(%rsp), %rsi
movq %r15, %rdi
callq 0x7734
movq %rax, %r15
leaq 0x10(%rsp), %rdi
callq 0x3200
movq 0x60(%rbx), %r12
leaq 0x5aa5(%rip), %rsi      # 0xbab6
leaq 0x10(%rsp), %rdi
leaq 0x6(%rsp), %rdx
callq 0x73f2
leaq 0x10(%rsp), %rsi
movq %r12, %rdi
callq 0x7734
movq %rax, %r12
leaq 0x10(%rsp), %rdi
callq 0x3200
movq 0x28(%rbx), %rdi
callq 0x3650
movq 0x40(%rbx), %rdi
leaq 0x5a4d(%rip), %rsi      # 0xba9b
callq 0x34c0
testl %eax, %eax
je 0x6169
movq 0x28(%rbx), %rdi
callq 0x3320
movq 0x28(%rbx), %rax
movq 0x88(%rax), %r15
testq %r15, %r15
je 0x6144
pushq $0x30
popq %rdi
callq 0x3450
leaq 0x8(%rax), %rcx
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%rax)
movaps %xmm0, (%rax)
movq %rcx, 0x18(%rax)
movq %rcx, 0x20(%rax)
andq $0x0, 0x28(%rax)
movq %rax, 0x80(%rbx)
movq %rbx, %rdi
callq 0x3320
movq %rbx, %rdi
callq *%r15
leaq 0x28(%r13), %r12
movq %r12, %rdi
callq 0x3650
movq 0x50(%r13), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7a9a
movq 0x50(%r13), %rcx
addq $0x8, %rcx
cmpq %rax, %rcx
je 0x6289
leaq 0x78(%rbx), %r15
movq %r12, %rdi
callq 0x3320
jmp 0x615f
movl $0x190, %eax            # imm = 0x190
leaq 0x78(%rbx), %r15
movl %eax, 0x78(%rbx)
movq %rbx, %rdi
callq 0x3320
movq %rbx, %rdi
callq 0x4deb
movq %rbx, %rdi
callq 0x3650
leaq 0x40(%rbx), %r14
movq %r14, %rdi
callq 0x5e5d
movq %r14, %rdi
callq 0x5e5d
movq %r15, %rdi
callq 0x62be
movq %rbx, %rdi
callq 0x3320
xorl %eax, %eax
addq $0xd0, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
retq
leaq 0x78(%rbx), %r15
movl $0x1f7, 0x78(%rbx)      # imm = 0x1F7
movq %rbx, %rdi
callq 0x3320
movq %rbx, %rdi
callq 0x4deb
movq %rbx, %rdi
callq 0x3650
jmp 0x6119
movq 0x60(%rbx), %rax
addq $0x8, %rax
cmpq %rax, %r12
je 0x605b
movq 0x40(%r12), %rcx
cmpb $0x31, (%rcx)
jne 0x605b
cmpb $0x33, 0x1(%rcx)
jne 0x605b
cmpb $0x0, 0x2(%rcx)
sete %cl
cmpq %rax, %r15
setne %al
andb %cl, %al
cmpb $0x1, %al
jne 0x605b
movq 0x28(%rbx), %r12
movq 0x50(%rbx), %rsi
movq 0xb0(%r12), %rdi
callq 0x35f0
testl %eax, %eax
jne 0x605b
movq %r12, %rdi
callq 0x3320
movl $0x65, 0x78(%rbx)
xorps %xmm0, %xmm0
leaq 0x50(%rsp), %r12
movaps %xmm0, (%r12)
movaps %xmm0, 0x10(%r12)
movaps %xmm0, 0x20(%r12)
movaps %xmm0, 0x30(%r12)
movaps %xmm0, 0x40(%r12)
movaps %xmm0, 0x50(%r12)
movaps %xmm0, 0x60(%r12)
movaps %xmm0, 0x70(%r12)
movq 0x40(%r15), %r15
movq %r15, %rdi
callq 0x31f0
movq %r15, %rdi
movl %eax, %esi
movq %r12, %rdx
callq 0x3c90
pushq $0x30
popq %rdi
callq 0x3450
movq %rax, %r15
addq $0x8, %rax
xorps %xmm0, %xmm0
movaps %xmm0, 0x10(%r15)
movaps %xmm0, (%r15)
movq %rax, 0x18(%r15)
movq %rax, 0x20(%r15)
andq $0x0, 0x28(%r15)
movq %r15, 0x80(%rbx)
leaq 0x4367(%rip), %rsi      # 0xa5c2
leaq 0x10(%rsp), %r13
movq %r13, %rdi
movq %r12, %rdx
callq 0x7488
movq %r15, %rdi
movq %r13, %rsi
callq 0x78a2
leaq 0x78(%rbx), %r15
leaq 0x10(%rsp), %rdi
callq 0x7472
jmp 0x614f
movq %r12, %rbx
jmp 0x6129
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x7472
jmp 0x62b6
jmp 0x62a4
jmp 0x62b3
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x3200
jmp 0x62b6
movq %rax, %rbx
movq %rbx, %rdi
callq 0x3770