// File: Cristian-Castillo[P]Multithreaded-HTTP-Server/./httpserver_definitions.cpp
// Function: worker_thread(void*)
void *worker_thread(void *args){
    
    struct http_object *node_parser,get_http_content;
    node_parser = &get_http_content;

    int id = *((int *)args); 
	int clientSocket;
    


    while(LIVE){
           
        /* -----------------  Locking  Thread In ------------------------------*/
        clientSocket = -1;
		
		pthread_mutex_lock(&mutex);
		{
			while(items.empty()){
				pthread_cond_wait(&client_in_queue,&mutex);
			}
            /* Take 1 client out of the queue */
            clientSocket = items.front();
            items.pop();
		}
		pthread_mutex_unlock(&mutex);

       
        /* -----------------  UnLocking Thread In ---------------------------- */
    
        char buffer[HEADER_BUFFER];
        size_t bytes_read;
        int msgsize = 0;

        /* read the clients message */
        while((bytes_read = read(clientSocket,buffer+msgsize,sizeof(buffer)-msgsize-1)) > 0){
            msgsize += bytes_read;
            if(msgsize > HEADER_BUFFER-1 || buffer[msgsize-1] == '\n')break;
        }
        buffer[msgsize-1] = 0; /* null terminate message and remove the \n */

        char request[4],file[50],protocol[10];
        sscanf(buffer,"%s %s %s",request,file, protocol);
     
        node_parser->buffer = buffer;
        node_parser->protocol = protocol;
        node_parser->request = request;
        node_parser->file_name = file;
        node_parser->client_connect = clientSocket;
        node_parser->id = id;

        /* ------------------------ REGEX SECTION ---------------------------- */

        bool matcha = f_regex_overkill_parse(node_parser->request,node_parser->file_name,node_parser->protocol);
        
        if(matcha != 1){ f_void_400(node_parser->client_connect); return NULL;}

        /*---------------------------------------------------------------------*/

        /* Get the file name - delimiter dash */
     
        char *theFile = file;
        char *restOfString = theFile;
        char *file_name = strtok_r(restOfString,"/", &restOfString);
        node_parser->file_name = file_name;
        node_parser->buffer = buffer;
        node_parser->client_connect = clientSocket;
        

        /* Master Module Check Box - Thread Dies on failure on failure */
        bool die_on_failure = f_bool_check_vitality(node_parser->file_name,node_parser->protocol,node_parser->request,node_parser->client_connect);
        
        if(die_on_failure == false){
            memset(node_parser,0,sizeof(http_object));
            continue;
        }
 
        /* Tokenizes my string please */
        std::string string_to_parse;
        std::stringstream ss;
        ss << node_parser->file_name;
        ss >> string_to_parse;
        
        /* Fancy hashing */
        std::unordered_map<std::string,int>hashing;

        /* hash id [file n] = id & convert too c-str */
        hashing[string_to_parse.c_str()] = node_parser->id;
        
        /* initialize iterator to search up valid existing id keys */
        std::unordered_map<std::string, int>::iterator lookup;

        for(lookup = hashing.begin(); lookup != hashing.end(); lookup++){ 
            

            if(strcmp(node_parser->request,"PUT") == 0 && strcmp(node_parser->request,"GET") != 0){
                f_void_put_module(buffer,node_parser->file_name,node_parser->client_connect);
                memset(node_parser,0,sizeof(http_object));
                return NULL;
            }

            if(strcmp(node_parser->request,"GET") == 0 && strcmp(node_parser->request,"PUT") != 0){
                f_void_get_module(buffer,node_parser->file_name,node_parser->client_connect);
                memset(node_parser,0,sizeof(http_object));
                return NULL;
            }   
        }
    }
    return NULL;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x1380, %rsp           # imm = 0x1380
movq %rdi, -0x10(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x8d80
leaq -0x98(%rbp), %rax
movq %rax, -0x18(%rbp)
movq -0x10(%rbp), %rax
movl (%rax), %eax
movl %eax, -0x9c(%rbp)
movl $0xffffffff, -0xa0(%rbp) # imm = 0xFFFFFFFF
leaq 0x4780d(%rip), %rdi     # 0x4f8a0
callq 0x56d0
leaq 0x478a9(%rip), %rdi     # 0x4f948
callq 0x8da0
testb $0x1, %al
jne 0x80aa
jmp 0x80bf
leaq 0x47867(%rip), %rdi     # 0x4f918
leaq 0x477e8(%rip), %rsi     # 0x4f8a0
callq 0x5710
jmp 0x8098
leaq 0x47882(%rip), %rdi     # 0x4f948
callq 0x8dc0
movl (%rax), %eax
movl %eax, -0xa0(%rbp)
leaq 0x4786e(%rip), %rdi     # 0x4f948
callq 0x8de0
leaq 0x477ba(%rip), %rdi     # 0x4f8a0
callq 0x53b0
movl $0x0, -0x10ac(%rbp)
movl -0xa0(%rbp), %edi
leaq -0x10a0(%rbp), %rsi
movslq -0x10ac(%rbp), %rax
addq %rax, %rsi
movslq -0x10ac(%rbp), %rax
movl $0x1000, %edx           # imm = 0x1000
subq %rax, %rdx
subq $0x1, %rdx
callq 0x56b0
movq %rax, -0x10a8(%rbp)
cmpq $0x0, %rax
jbe 0x8170
movq -0x10a8(%rbp), %rcx
movslq -0x10ac(%rbp), %rax
addq %rcx, %rax
movl %eax, -0x10ac(%rbp)
cmpl $0xfff, -0x10ac(%rbp)   # imm = 0xFFF
jg 0x816c
movl -0x10ac(%rbp), %eax
subl $0x1, %eax
cltq
movsbl -0x10a0(%rbp,%rax), %eax
cmpl $0xa, %eax
jne 0x816e
jmp 0x8170
jmp 0x80f5
movl -0x10ac(%rbp), %eax
subl $0x1, %eax
cltq
movb $0x0, -0x10a0(%rbp,%rax)
leaq -0x10a0(%rbp), %rdi
leaq -0x10b0(%rbp), %rdx
leaq -0x10f0(%rbp), %rcx
leaq -0x10fa(%rbp), %r8
leaq 0x2d763(%rip), %rsi     # 0x35909
movb $0x0, %al
callq 0x5460
leaq -0x10a0(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
leaq -0x10fa(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x10(%rax)
leaq -0x10b0(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x8(%rax)
leaq -0x10f0(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
movl -0xa0(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x28(%rax)
movl -0x9c(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x30(%rax)
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rdx
callq 0x68d0
andb $0x1, %al
movb %al, -0x10fb(%rbp)
movb -0x10fb(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x1, %eax
je 0x824f
movq -0x18(%rbp), %rax
movl 0x28(%rax), %edi
callq 0x5ef0
movq $0x0, -0x8(%rbp)
jmp 0x8638
leaq -0x10f0(%rbp), %rax
movq %rax, -0x1108(%rbp)
movq -0x1108(%rbp), %rax
movq %rax, -0x1110(%rbp)
movq -0x1110(%rbp), %rdi
leaq 0x2d551(%rip), %rsi     # 0x357ca
leaq -0x1110(%rbp), %rdx
callq 0x5850
movq %rax, -0x1118(%rbp)
movq -0x1118(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, (%rax)
leaq -0x10a0(%rbp), %rcx
movq -0x18(%rbp), %rax
movq %rcx, 0x18(%rax)
movl -0xa0(%rbp), %ecx
movq -0x18(%rbp), %rax
movl %ecx, 0x28(%rax)
movq -0x18(%rbp), %rax
movq (%rax), %rdi
movq -0x18(%rbp), %rax
movq 0x10(%rax), %rsi
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdx
movq -0x18(%rbp), %rax
movl 0x28(%rax), %ecx
callq 0x5bc0
andb $0x1, %al
movb %al, -0x1119(%rbp)
movb -0x1119(%rbp), %al
andb $0x1, %al
movzbl %al, %eax
cmpl $0x0, %eax
jne 0x8306
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x5260
jmp 0x8082
leaq -0x1140(%rbp), %rdi
callq 0x5660
leaq -0x12c8(%rbp), %rdi
callq 0x5350
jmp 0x8320
leaq -0x12b8(%rbp), %rdi
movq -0x18(%rbp), %rax
movq (%rax), %rsi
callq 0x5470
jmp 0x8335
leaq -0x12c8(%rbp), %rdi
leaq -0x1140(%rbp), %rsi
callq 0x55e0
jmp 0x834a
leaq -0x1310(%rbp), %rdi
callq 0x8e00
movq -0x18(%rbp), %rax
movl 0x30(%rax), %eax
movl %eax, -0x1374(%rbp)
leaq -0x1140(%rbp), %rdi
callq 0x51c0
movq %rax, -0x1370(%rbp)
leaq -0x1331(%rbp), %rdi
movq %rdi, -0x1368(%rbp)
callq 0x5810
movq -0x1370(%rbp), %rsi
movq -0x1368(%rbp), %rdx
leaq -0x1330(%rbp), %rdi
callq 0x8e50
jmp 0x83a5
leaq -0x1310(%rbp), %rdi
leaq -0x1330(%rbp), %rsi
callq 0x8e20
movq %rax, -0x1380(%rbp)
jmp 0x83c1
movq -0x1380(%rbp), %rax
movl -0x1374(%rbp), %ecx
movl %ecx, (%rax)
leaq -0x1330(%rbp), %rdi
callq 0x52d0
leaq -0x1331(%rbp), %rdi
callq 0x54d0
leaq -0x1340(%rbp), %rdi
callq 0x8f00
jmp 0x83f6
leaq -0x1310(%rbp), %rdi
callq 0x8f20
movq %rax, -0x1348(%rbp)
movq -0x1348(%rbp), %rax
movq %rax, -0x1340(%rbp)
leaq -0x1310(%rbp), %rdi
callq 0x8f80
movq %rax, -0x1350(%rbp)
leaq -0x1340(%rbp), %rdi
leaq -0x1350(%rbp), %rsi
callq 0x8f50
testb $0x1, %al
jne 0x8446
jmp 0x85d1
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2cd83(%rip), %rsi     # 0x351d8
callq 0x5670
cmpl $0x0, %eax
jne 0x8547
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2cd62(%rip), %rsi     # 0x351d4
callq 0x5670
cmpl $0x0, %eax
je 0x8547
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movl 0x28(%rax), %edx
leaq -0x10a0(%rbp), %rdi
callq 0x7850
jmp 0x8498
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x5260
movq $0x0, -0x8(%rbp)
movl $0x1, -0x1354(%rbp)
jmp 0x85db
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x12d0(%rbp)
movl %eax, -0x12d4(%rbp)
jmp 0x862a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x12d0(%rbp)
movl %eax, -0x12d4(%rbp)
jmp 0x861e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x12d0(%rbp)
movl %eax, -0x12d4(%rbp)
jmp 0x851f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x12d0(%rbp)
movl %eax, -0x12d4(%rbp)
leaq -0x1330(%rbp), %rdi
callq 0x52d0
leaq -0x1331(%rbp), %rdi
callq 0x54d0
jmp 0x8612
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x12d0(%rbp)
movl %eax, -0x12d4(%rbp)
jmp 0x8612
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2cc7e(%rip), %rsi     # 0x351d4
callq 0x5670
cmpl $0x0, %eax
jne 0x85b5
movq -0x18(%rbp), %rax
movq 0x8(%rax), %rdi
leaq 0x2cc69(%rip), %rsi     # 0x351d8
callq 0x5670
cmpl $0x0, %eax
je 0x85b5
movq -0x18(%rbp), %rax
movq (%rax), %rsi
movl 0x28(%rax), %edx
leaq -0x10a0(%rbp), %rdi
callq 0x7ca0
jmp 0x8591
movq -0x18(%rbp), %rdi
xorl %esi, %esi
movl $0x80, %edx
callq 0x5260
movq $0x0, -0x8(%rbp)
movl $0x1, -0x1354(%rbp)
jmp 0x85db
jmp 0x85b7
leaq -0x1340(%rbp), %rdi
xorl %esi, %esi
callq 0x8fb0
movq %rax, -0x1360(%rbp)
jmp 0x8417
movl $0x0, -0x1354(%rbp)
leaq -0x1310(%rbp), %rdi
callq 0x8fe0
leaq -0x12c8(%rbp), %rdi
callq 0x5390
leaq -0x1140(%rbp), %rdi
callq 0x52d0
movl -0x1354(%rbp), %eax
testl %eax, %eax
je 0x860d
jmp 0x860b
jmp 0x8638
jmp 0x8082
leaq -0x1310(%rbp), %rdi
callq 0x8fe0
leaq -0x12c8(%rbp), %rdi
callq 0x5390
leaq -0x1140(%rbp), %rdi
callq 0x52d0
jmp 0x8645
movq -0x8(%rbp), %rax
addq $0x1380, %rsp           # imm = 0x1380
popq %rbp
retq
movq -0x12d0(%rbp), %rdi
callq 0x5800
nopw %cs:(%rax,%rax)