// File: /workspace/github_c_makefile_star2_O2/sszllzss[P]webSocket_Libevent_ThreadPool/ev_httpd.cpp
// Function: accept_request(httpChilent_t*)
int accept_request(struct httpChilent_t * client)
{
    size_t numchars;
    char * buf;
    size_t i, j;
    struct evbuffer * evbuff = NULL;
    if(client == NULL)
        return REQUEST_ERROR;
    pthread_mutex_lock(&client->lock);
    gettimeofday(&client->final_optime, NULL);
    evbuff = client->receive_evbuff; 
    evbuffer_lock(client->receive_evbuff);
    buf = evbuffer_readln(evbuff, &numchars, EVBUFFER_EOL_CRLF);
    evbuffer_unlock(client->receive_evbuff);
    if(buf == NULL)
    {
        pthread_mutex_unlock(&client->lock);
        return REQUEST_NOCONTENT;
    }
    client->request.method = (char *)malloc(METHOD_MAX_LEN + 1);
    client->request.url = (char *)malloc(URL_MAX_LEN + 1);
    client->request.http_version = (char *)malloc(HTTPVERSION_MAX_LEN + 1);
    if(client->request.method == NULL || client->request.url == NULL || client->request.http_version == NULL)
    {
        pthread_mutex_unlock(&client->lock);
        return REQUEST_INTERNAL;
    }


    i = 0; j = 0;
    while (!ISspace(buf[j]) && (i < METHOD_MAX_LEN) && (j < numchars))
    {
        client->request.method[i] = buf[j];
        i++; j++;
    }
    if(j == numchars)
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_BADREQUEST;        
    }


    client->request.method[i] = '\0';

    if (strcasecmp(client->request.method, "GET") && strcasecmp(client->request.method, "POST"))
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_NOTIMPLEMENTED;
    }




    i = 0;
    while (ISspace(buf[j]) && (j < numchars) )
        j++;
    if(j == numchars)
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_BADREQUEST;        
    }
    while (!ISspace(buf[j]) && (i < URL_MAX_LEN) && (j < numchars))
    {
        client->request.url[i] = buf[j];
        i++; j++;
    }
    if(j == numchars)
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_BADREQUEST;        
    }


    client->request.url[i] = '\0';

    if (strcasecmp(client->request.method, "GET") == 0)
    {
        client->request.query_string = client->request.url;
        while ((*client->request.query_string != '?') && (*client->request.query_string != '\0'))
            client->request.query_string++;
        if (*client->request.query_string == '?')
        {
            *client->request.query_string = '\0';
            client->request.query_string++;
        }
    }
    else
    {
        if(strstr(client->request.url, "?"))
        {
            http_request_free(&client->request);
            pthread_mutex_unlock(&client->lock);
            return REQUEST_BADREQUEST;        
        }
    }



    i = 0;
    while (ISspace(buf[j]) && (j < numchars) )
        j++;
    if(j == numchars)
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_BADREQUEST;        
    }
    while (!ISspace(buf[j]) && (i < HTTPVERSION_MAX_LEN) && (j < numchars))
    {
        client->request.http_version[i] = buf[j];
        i++; j++;
    }
    if(strlen(client->request.http_version) < 8)
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_BADREQUEST;        
    }
    free(buf);
    while(1)
    {
        evbuffer_lock(client->receive_evbuff);
        buf = evbuffer_readln(evbuff, &numchars, EVBUFFER_EOL_CRLF);
        evbuffer_unlock(client->receive_evbuff);
        if(buf == NULL)
            break;
        else if(strcasecmp(client->request.method, "POST") == 0 &&  numchars == 0)
        {
            client->request.request_data_len = evbuffer_get_length(evbuff);
            client->request.request_data = (char *)malloc(client->request.request_data_len);
            if(client->request.request_data == NULL)
            {
                http_request_free(&client->request);
                pthread_mutex_unlock(&client->lock);
                return REQUEST_INTERNAL;        
            }
            evbuffer_lock(client->receive_evbuff);
            evbuffer_remove(evbuff, client->request.request_data, client->request.request_data_len);
            evbuffer_unlock(client->receive_evbuff);
            break;
        }
        else if(numchars == 0)
        {
            break;
        }
        if( client->request.HeadParameter == NULL)
            client->request.HeadParameter = new HeadParameterMap();
        char * key = buf;
        char * v = strchr(buf,':');
        if(v == NULL)
        {
            http_request_free(&client->request);
            pthread_mutex_unlock(&client->lock);
            return REQUEST_BADREQUEST;        
        }
        *v = '\0';
        v++;
        i = 0;j = 0;
        while (ISspace(*key) && (j < numchars) )
        {
            j++;
            key++;
        }
        while (!ISspace(key[i]) && (i < strlen(key)))
        {
            i++; j++;
        }
        key[i] = '\0';
        i = 0;j = 0;
        numchars = v - buf;
        while (ISspace(*v) && (j < numchars) )
        {
            j++;
            v++;
        }
        while (!ISspace(v[i]) && (i < strlen(v)))
        {
            i++; j++;
        }
        v[i] = '\0';

        client->request.HeadParameter->insert(std::pair<STRING, STRING>(key, v));    
    }

    if(strcasecmp(client->request.method, "POST") == 0 && client->request.request_data == NULL)
    {
        http_request_free(&client->request);
        pthread_mutex_unlock(&client->lock);
        return REQUEST_BADREQUEST;        
    }
    pthread_mutex_unlock(&client->lock);

    return 0;

}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x78, %rsp
testq %rdi, %rdi
je 0x59db
movq %rdi, %rbx
callq 0x3650
leaq 0xb8(%rbx), %rdi
xorl %esi, %esi
callq 0x36c0
movq 0x98(%rbx), %r14
movq %r14, %rdi
callq 0x32e0
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rdx
movq %r14, %rdi
callq 0x34d0
movq %rax, %r12
movq 0x98(%rbx), %rdi
callq 0x37b0
testq %r12, %r12
je 0x59e2
pushq $0x7e
popq %r15
movq %r15, %rdi
callq 0x35d0
movq %rax, %r13
movq %rax, 0x40(%rbx)
movq %r15, %rdi
callq 0x35d0
movq %rax, %r15
movq %rax, 0x50(%rbx)
pushq $0xb
popq %rdi
callq 0x35d0
movq %rax, 0x58(%rbx)
testq %r13, %r13
je 0x59cc
testq %r15, %r15
sete %cl
testq %rax, %rax
sete %al
orb %cl, %al
je 0x59f1
movq %rbx, %rdi
callq 0x3320
pushq $-0x5
jmp 0x5bfc
pushq $-0x1
jmp 0x5bfc
movq %rbx, %rdi
callq 0x3320
pushq $-0x2
jmp 0x5bfc
movq %r14, 0x30(%rsp)
leaq 0x40(%rbx), %r15
xorl %ebp, %ebp
movb (%r12,%rbp), %r14b
movsbl %r14b, %edi
callq 0x30a0
movq 0x8(%rsp), %rcx
testl %eax, %eax
jne 0x5a29
cmpq $0x7c, %rbp
ja 0x5a29
cmpq %rcx, %rbp
jae 0x5a29
movq (%r15), %rax
movb %r14b, (%rax,%rbp)
incq %rbp
jmp 0x59fc
cmpq %rcx, %rbp
jne 0x5a36
movq %r15, %rdi
jmp 0x5bed
movq (%r15), %rax
movb $0x0, (%rax,%rbp)
movq (%r15), %r13
leaq 0x6054(%rip), %rsi      # 0xba9b
movq %r13, %rdi
callq 0x34c0
testl %eax, %eax
je 0x5a7d
leaq 0x6045(%rip), %rsi      # 0xba9f
movq %r13, %rdi
callq 0x34c0
testl %eax, %eax
je 0x5a7d
movq %r15, %rdi
callq 0x5e5d
movq %rbx, %rdi
callq 0x3320
pushq $-0x6
jmp 0x5bfc
movq %r15, 0x10(%rsp)
addq $-0x2, %rbp
movq 0x8(%rsp), %r14
movb 0x2(%r12,%rbp), %r15b
movsbl %r15b, %edi
callq 0x30a0
leaq 0x1(%rbp), %r13
testl %eax, %eax
je 0x5aad
addq $0x2, %rbp
cmpq %r14, %rbp
movq %r13, %rbp
jb 0x5a8b
movq %r13, %rax
subq %r14, %rax
cmpq $-0x1, %rax
je 0x5be8
xorl %ebp, %ebp
movsbl %r15b, %edi
callq 0x30a0
testl %eax, %eax
jne 0x5afd
cmpq $0x7c, %rbp
ja 0x5afd
leaq 0x1(,%rbp), %rax
addq %r13, %rax
cmpq %r14, %rax
jae 0x5afd
movq 0x50(%rbx), %rax
movb %r15b, (%rax,%rbp)
leaq (%r12,%rbp), %rax
incq %rbp
movb 0x2(%r13,%rax), %r15b
movq 0x8(%rsp), %r14
jmp 0x5abf
movq %rbp, %rax
subq %r14, %rax
addq %r13, %rax
cmpq $-0x1, %rax
je 0x5be8
movq 0x50(%rbx), %rax
movb $0x0, (%rax,%rbp)
movq 0x40(%rbx), %rdi
leaq 0x5f78(%rip), %rsi      # 0xba9b
callq 0x34c0
movq 0x50(%rbx), %rdi
testl %eax, %eax
movq 0x10(%rsp), %r15
je 0x5b4b
pushq $0x3f
popq %rsi
callq 0x31c0
testq %rax, %rax
jne 0x5a2e
jmp 0x5b62
incq %rdi
movq %rdi, 0x48(%rbx)
movzbl (%rdi), %eax
testl %eax, %eax
je 0x5b62
cmpl $0x3f, %eax
jne 0x5b48
movb $0x0, (%rdi)
incq 0x48(%rbx)
addq %rbp, %r13
leaq (%r12,%r13), %rbp
incq %rbp
movq 0x8(%rsp), %rax
movq %rax, 0x18(%rsp)
incq %r13
movb (%rbp), %r14b
movsbl %r14b, %edi
callq 0x30a0
incq %rbp
testl %eax, %eax
je 0x5b94
cmpq 0x18(%rsp), %r13
jb 0x5b76
cmpq %r13, 0x18(%rsp)
je 0x5a2e
xorl %r15d, %r15d
movsbl %r14b, %edi
callq 0x30a0
movq 0x58(%rbx), %rdi
testl %eax, %eax
jne 0x5bdd
cmpq $0x9, %r15
ja 0x5bdd
cmpq 0x18(%rsp), %r13
jae 0x5bdd
movb %r14b, (%rdi,%r15)
incq %r15
incq %r13
movb (%rbp), %r14b
movq 0x8(%rsp), %rax
movq %rax, 0x18(%rsp)
incq %rbp
jmp 0x5ba2
callq 0x31f0
cmpq $0x7, %rax
ja 0x5c0c
movq 0x10(%rsp), %rdi
callq 0x5e5d
movq %rbx, %rdi
callq 0x3320
pushq $-0x3
popq %rax
addq $0x78, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %r12, %rdi
callq 0x3520
movq 0x30(%rsp), %r14
movq 0x10(%rsp), %r15
movq 0x98(%rbx), %rdi
callq 0x32e0
movq %r14, %rdi
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x34d0
movq %rax, %r12
movq 0x98(%rbx), %rdi
callq 0x37b0
testq %r12, %r12
je 0x5e0e
movq (%r15), %rdi
leaq 0x5e43(%rip), %rsi      # 0xba9f
callq 0x34c0
testl %eax, %eax
sete %cl
movq 0x8(%rsp), %rax
testq %rax, %rax
sete %dl
andb %cl, %dl
cmpb $0x1, %dl
je 0x5dcc
testq %rax, %rax
je 0x5e0e
cmpq $0x0, 0x60(%rbx)
jne 0x5cb3
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, 0x60(%rbx)
movq %r12, 0x28(%rsp)
movq %r12, %rdi
pushq $0x3a
popq %rsi
callq 0x31c0
movq %rax, 0x20(%rsp)
testq %rax, %rax
je 0x5a2e
movb $0x0, (%rax)
incq 0x20(%rsp)
movq 0x8(%rsp), %r14
movq 0x28(%rsp), %rbp
xorl %r13d, %r13d
movb (%rbp), %r15b
movsbl %r15b, %edi
callq 0x30a0
testl %eax, %eax
je 0x5d09
cmpq %r14, %r13
jae 0x5d09
incq %r13
incq %rbp
movq %rbp, 0x28(%rsp)
jmp 0x5ce6
xorl %r14d, %r14d
leaq 0x38(%rsp), %r13
movsbl %r15b, %edi
callq 0x30a0
testl %eax, %eax
jne 0x5d35
movq %rbp, %rdi
callq 0x31f0
cmpq %rax, %r14
jae 0x5d35
movb 0x1(%rbp,%r14), %r15b
incq %r14
jmp 0x5d11
movb $0x0, (%rbp,%r14)
movq 0x20(%rsp), %rbp
movq %rbp, %r14
subq %r12, %r14
movq %r14, 0x8(%rsp)
xorl %r12d, %r12d
movb (%rbp), %r15b
movsbl %r15b, %edi
callq 0x30a0
testl %eax, %eax
je 0x5d71
cmpq %r14, %r12
jae 0x5d71
incq %r12
incq %rbp
movq %rbp, 0x20(%rsp)
jmp 0x5d4e
xorl %r14d, %r14d
movsbl %r15b, %edi
callq 0x30a0
testl %eax, %eax
jne 0x5d98
movq %rbp, %rdi
callq 0x31f0
cmpq %rax, %r14
jae 0x5d98
movb 0x1(%rbp,%r14), %r15b
incq %r14
jmp 0x5d74
movb $0x0, (%rbp,%r14)
movq 0x60(%rbx), %r12
movq %r13, %rdi
leaq 0x28(%rsp), %rsi
leaq 0x20(%rsp), %rdx
callq 0x742e
movq %r12, %rdi
movq %r13, %rsi
callq 0x78a2
movq %r13, %rdi
callq 0x7472
jmp 0x5c14
movq %r14, %rdi
callq 0x34b0
movl %eax, 0x70(%rbx)
movl %eax, %edi
callq 0x35d0
movq %rax, 0x68(%rbx)
testq %rax, %rax
je 0x5e3b
movq 0x98(%rbx), %rdi
callq 0x32e0
movq 0x68(%rbx), %rsi
movl 0x70(%rbx), %edx
movq %r14, %rdi
callq 0x3560
movq 0x98(%rbx), %rdi
callq 0x37b0
movq (%r15), %rdi
leaq 0x5c87(%rip), %rsi      # 0xba9f
callq 0x34c0
testl %eax, %eax
jne 0x5e2c
cmpq $0x0, 0x68(%rbx)
je 0x5a2e
movq %rbx, %rdi
callq 0x3320
xorl %eax, %eax
jmp 0x5bfd
movq %r15, %rdi
callq 0x5e5d
jmp 0x59cc
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x7472
movq %rbx, %rdi
callq 0x3770