// File: /workspace/github_cpp_makefile_star2_O2/dentiny[P]HTTP-Cache-Proxy/Proxy.cpp
// Function: Proxy::getResponse(int, int, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::vector<std::vector<char, std::allocator<char>>, std::allocator<std::vector<char, std::allocator<char>>>>&, int, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
void getResponse(int client_id,
					int client_fd, 
					int server_fd, 
					const std::string & url, 
					const std::string & header, 
					std::vector<std::vector<char>> & segment,
					int len,
					const std::string & httpAction)
	{
	    // content-based http response
	    // (1) extract content length from header
	    // (2) keep receiving until total received size exceeds content length(marks end)
	    std::vector<char> buffer(BUFFER_SIZE, '\0');
	    if(header.find("Content-Length") != -1)
	    {
	    	int received_length = len;
	    	int content_length = parser.extractContentLength(header);
	    	while(received_length < content_length)
	    	{
	    		// receive response from server
	    		len = recv(server_fd, &buffer.data()[0], BUFFER_SIZE - 1, 0);
		    	if(len == -1)
		    	{
		    		throw ProxyException("Proxy received from server error");
		    	}
		    	buffer[len] = '\0';
		    	segment.push_back(std::vector<char>(buffer.begin(), buffer.begin() + len));
		    	received_length += len;

		    	// send response to client
		    	bool respond_suc = respondClient(client_fd, buffer, len);
		    	if(!respond_suc)
		    	{
		    		throw ProxyException("Proxy respond to client error");
		    	}
	    	}
	    }

	    // chunk-based http response
	    // keep receiving till the last chunk of the response, which is marked as 0 at the first line of the chunk
	    else if(header.find("chunked") != -1)
	    {
	    	bool isLastBlock = false;
	    	while(!isLastBlock)
	    	{
	    		// receive message from server
	    		len = recv(server_fd, &buffer.data()[0], BUFFER_SIZE - 1, 0);
		    	if(len == -1)
		    	{
		    		throw ProxyException("Proxy received from server error");
		    	}
		    	buffer[len] = '\0';
		    	segment.push_back(std::vector<char>(buffer.begin(), buffer.begin() + len));

		    	// send response to the client
		    	bool respond_suc = respondClient(client_fd, buffer, len);
		    	if(!respond_suc)
		    	{
		    		throw ProxyException("Proxy respond to client error");
		    	}

		    	// the first character of the last block starts with 0
		    	if(buffer[0] == '0')
		    	{
		    		isLastBlock = true;
		    	}
	    	}	
	    }

	    // POST action
	    else
	    {
	    	// keep receiving until the received length is 0, which marks the end of transmission
	    	int received_length = len;
	    	while(true)
	    	{
	    		// receive from server
	    		len = recv(server_fd, &buffer.data()[0], BUFFER_SIZE - 1, 0);
		    	if(len == -1)
		    	{
		    		throw ProxyException("Proxy received from server error");
		    	}
		    	else if(len == 0)
		    	{
		    		break;
		    	}
		    	segment.push_back(std::vector<char>(buffer.begin(), buffer.begin() + len));

		    	// send response to the client
		    	bool respond_suc = respondClient(client_fd, buffer, len);
		    	if(!respond_suc)
		    	{
		    		throw ProxyException("Proxy respond to client error");
		    	}
	    	}
	    }
	    Response response(url, segment, header);
	    parser.parseResponse(response);

	    // write first line of response to log
	    std::string log_content = std::to_string(client_id) + ": Received " + response.first_line + " from " + response.url;
	    logger.log(log_content);
	    log_content = std::to_string(client_id) + ": Responding " + response.first_line;
	    logger.log(log_content);

	    // cache only works for GET http action, and apply on those with no "no-store" attribute
	    if(!response.no_store && httpAction == "GET")
	    {
	    	cache.put(url, response);
	    }

	    // if http action is GET and status code is 200, write it into log
	    if(httpAction == "GET" && response.status_code == 200)
	    {
	    	std::string log_content;
	    	if(response.no_store)
	    	{
	    		log_content = std::to_string(client_id) + ": not cachable bacause no-store in Cache-Control"; 
	    	}
	    	else if(!response.etag.empty() || response.last_modified != 0)
	    	{
	    		log_content = std::to_string(client_id) + ": cached, but requires re-validation"; 
	    	}
	    	else
	    	{
	    		// convert time_t to string
	    		tm * tm = localtime(&response.expiration_time);
	    		char * dt = asctime(tm);
	    		std::string expiration_time(dt);
	 			log_content = std::to_string(client_id) + ": cached, expired at " + expiration_time;
	    	}
	    	logger.log(log_content);
	    }
	}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x308, %rsp            # imm = 0x308
movq %r9, %r13
movq %r8, %rbx
movl %ecx, %ebp
movl %edx, 0x4(%rsp)
movl %esi, 0x64(%rsp)
movq %rdi, %r12
leaq 0x150(%rsp), %rdx
movb $0x0, (%rdx)
leaq 0x8(%rsp), %rdi
leaq 0x88(%rsp), %rcx
movl $0x10000, %esi          # imm = 0x10000
callq 0x563e
leaq 0x2d73(%rip), %rsi      # 0xa46e
movq %r13, %rdi
xorl %edx, %edx
callq 0x3540
cmpq $-0x1, %rax
movq %rbx, 0xb0(%rsp)
movq %r13, 0xa8(%rsp)
je 0x7816
movl 0x348(%rsp), %r15d
movq %r12, %r14
movq %r12, %rdi
movq %r13, %rsi
callq 0x8296
movl %eax, %ebx
leaq 0x150(%rsp), %r12
cmpl %ebx, %r15d
jge 0x7933
movq 0x8(%rsp), %rsi
movl $0xffff, %edx           # imm = 0xFFFF
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x30e0
movq %rax, %r13
cmpl $-0x1, %r13d
je 0x7cf2
movslq %r13d, %rdx
movq 0x8(%rsp), %rax
movb $0x0, (%rax,%rdx)
movq 0x8(%rsp), %rsi
addq %rsi, %rdx
movq %r12, %rdi
leaq 0x88(%rsp), %rcx
callq 0x598e
movq 0x340(%rsp), %rdi
movq %r12, %rsi
callq 0x80cc
movq %r12, %rdi
callq 0x3dda
movq %r14, %rdi
movl 0x4(%rsp), %esi
leaq 0x8(%rsp), %rdx
movl %r13d, %ecx
callq 0x7668
addl %r13d, %r15d
testb %al, %al
jne 0x773f
pushq $0x28
popq %rdi
callq 0x3190
movq %rax, %r14
leaq 0x2ccd(%rip), %rsi      # 0xa49e
leaq 0x2c8(%rsp), %rdi
leaq 0x150(%rsp), %rdx
callq 0x386c
movb $0x1, %bpl
leaq 0x2c8(%rsp), %rsi
movq %r14, %rdi
callq 0x55e8
xorl %ebp, %ebp
leaq 0x8506(%rip), %rsi      # 0xfd08
leaq -0x21eb(%rip), %rdx     # 0x561e
movq %r14, %rdi
callq 0x3490
jmp 0x7e9d
movq %r12, %r14
leaq 0x2c9c(%rip), %rsi      # 0xa4bc
movq %r13, %rdi
xorl %edx, %edx
callq 0x3540
leaq 0x150(%rsp), %rbx
leaq 0x88(%rsp), %r15
leaq 0x8(%rsp), %r12
cmpq $-0x1, %rax
je 0x7918
movq 0x8(%rsp), %rsi
movl $0xffff, %edx           # imm = 0xFFFF
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x30e0
movq %rax, %r13
cmpl $-0x1, %r13d
je 0x7d49
movslq %r13d, %rdx
movq 0x8(%rsp), %rax
movb $0x0, (%rax,%rdx)
movq 0x8(%rsp), %rsi
addq %rsi, %rdx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x598e
movq 0x340(%rsp), %rdi
movq %rbx, %rsi
callq 0x80cc
movq %rbx, %rdi
callq 0x3dda
movq %r14, %rdi
movl 0x4(%rsp), %esi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x7668
testb %al, %al
je 0x7da0
movq 0x8(%rsp), %rax
cmpb $0x30, (%rax)
jne 0x7849
jmp 0x7933
cmpl $-0x1, %r13d
je 0x7df7
movq 0x8(%rsp), %rsi
movslq %r13d, %rdx
addq %rsi, %rdx
movq %rbx, %rdi
movq %r15, %rcx
callq 0x598e
movq 0x340(%rsp), %rdi
movq %rbx, %rsi
callq 0x80cc
movq %rbx, %rdi
callq 0x3dda
movq %r14, %rdi
movl 0x4(%rsp), %esi
movq %r12, %rdx
movl %r13d, %ecx
callq 0x7668
testb %al, %al
je 0x7e4b
movq 0x8(%rsp), %rsi
movl $0xffff, %edx           # imm = 0xFFFF
movl %ebp, %edi
xorl %ecx, %ecx
callq 0x30e0
movq %rax, %r13
testl %r13d, %r13d
jne 0x78c6
leaq 0x130(%rsp), %rdi
movq 0xb0(%rsp), %r15
movq %r15, %rsi
callq 0x33d0
leaq 0xd8(%rsp), %rdi
movq 0x340(%rsp), %rsi
callq 0x6dc2
movl 0x64(%rsp), %ebp
movq 0xa8(%rsp), %rsi
leaq 0x110(%rsp), %rdi
callq 0x33d0
movq %r14, %r13
leaq 0x150(%rsp), %rdi
leaq 0x130(%rsp), %rsi
leaq 0xd8(%rsp), %rdx
leaq 0x110(%rsp), %rcx
callq 0x82fe
leaq 0x110(%rsp), %rdi
callq 0x3150
leaq 0xd8(%rsp), %rdi
callq 0x3c98
leaq 0x130(%rsp), %rdi
callq 0x3150
leaq 0x150(%rsp), %rsi
movq %r13, %rdi
callq 0x83f6
leaq 0xb8(%rsp), %rdi
movl %ebp, %esi
callq 0x4a4e
leaq 0x2a47(%rip), %rdx      # 0xa435
leaq 0x20(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x4a2f
leaq 0x158(%rsp), %r14
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
movq %r14, %rdx
callq 0x4a10
leaq 0x26e9(%rip), %rdx      # 0xa10a
leaq 0x68(%rsp), %rdi
leaq 0x40(%rsp), %rsi
callq 0x4a2f
leaq 0x178(%rsp), %rdx
leaq 0x88(%rsp), %rdi
leaq 0x68(%rsp), %rsi
callq 0x4a10
leaq 0x68(%rsp), %rdi
callq 0x3150
leaq 0x40(%rsp), %rdi
callq 0x3150
leaq 0x20(%rsp), %rdi
callq 0x3150
leaq 0xb8(%rsp), %rdi
callq 0x3150
leaq 0x8(%r13), %r12
leaq 0x88(%rsp), %rsi
movq %r12, %rdi
callq 0x4aa8
leaq 0x20(%rsp), %rdi
movl %ebp, %esi
callq 0x4a4e
leaq 0x29a5(%rip), %rdx      # 0xa441
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x4a2f
leaq 0x68(%rsp), %rdi
leaq 0x40(%rsp), %rsi
movq %r14, %rdx
callq 0x4a10
leaq 0x88(%rsp), %rbx
leaq 0x68(%rsp), %r14
movq %rbx, %rdi
movq %r14, %rsi
callq 0x3370
movq %r14, %rdi
callq 0x3150
leaq 0x40(%rsp), %rdi
callq 0x3150
leaq 0x20(%rsp), %rdi
callq 0x3150
movq %r12, %rdi
movq %rbx, %rsi
callq 0x4aa8
movq 0x350(%rsp), %rbx
cmpb $0x0, 0x208(%rsp)
jne 0x7b3b
leaq 0x2626(%rip), %rsi      # 0xa13b
movq %rbx, %rdi
callq 0x4cf4
testb %al, %al
je 0x7b3b
addq $0x250, %r13            # imm = 0x250
leaq 0x150(%rsp), %rdx
movq %r13, %rdi
movq %r15, %rsi
callq 0x8438
leaq 0x25f9(%rip), %rsi      # 0xa13b
movq %rbx, %rdi
callq 0x4cf4
testb %al, %al
je 0x7cbc
cmpl $0xc8, 0x150(%rsp)
jne 0x7cbc
leaq 0x78(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
cmpb $0x1, 0x208(%rsp)
jne 0x7ba5
leaq 0x20(%rsp), %rdi
movl %ebp, %esi
callq 0x4a4e
leaq 0x2933(%rip), %rdx      # 0xa4c4
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x4a2f
jmp 0x7c81
movq 0x220(%rsp), %rax
orq 0x230(%rsp), %rax
jne 0x7c5f
leaq 0x218(%rsp), %rdi
callq 0x3250
movq %rax, %rdi
callq 0x3110
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rdx
movq %rax, %rsi
callq 0x386c
leaq 0xf0(%rsp), %rdi
movl %ebp, %esi
callq 0x4a4e
leaq 0x2922(%rip), %rdx      # 0xa51a
leaq 0xb8(%rsp), %rdi
leaq 0xf0(%rsp), %rsi
callq 0x4a2f
leaq 0x20(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
leaq 0x40(%rsp), %rdx
callq 0x4a10
leaq 0x68(%rsp), %rdi
leaq 0x20(%rsp), %rbx
movq %rbx, %rsi
callq 0x3370
movq %rbx, %rdi
callq 0x3150
leaq 0xb8(%rsp), %rdi
callq 0x3150
leaq 0xf0(%rsp), %rdi
callq 0x3150
leaq 0x40(%rsp), %rdi
jmp 0x7ca0
leaq 0x20(%rsp), %rdi
movl %ebp, %esi
callq 0x4a4e
leaq 0x2883(%rip), %rdx      # 0xa4f5
leaq 0x40(%rsp), %rdi
leaq 0x20(%rsp), %rsi
callq 0x4a2f
leaq 0x68(%rsp), %rdi
leaq 0x40(%rsp), %rbx
movq %rbx, %rsi
callq 0x3370
movq %rbx, %rdi
callq 0x3150
leaq 0x20(%rsp), %rdi
callq 0x3150
leaq 0x68(%rsp), %rsi
movq %r12, %rdi
callq 0x4aa8
leaq 0x68(%rsp), %rdi
callq 0x3150
leaq 0x88(%rsp), %rdi
callq 0x3150
leaq 0x150(%rsp), %rdi
callq 0x3c54
leaq 0x8(%rsp), %rdi
callq 0x3dda
addq $0x308, %rsp            # imm = 0x308
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x28
popq %rdi
callq 0x3190
movq %rax, %r14
leaq 0x2779(%rip), %rsi      # 0xa47d
leaq 0x2e8(%rsp), %rdi
leaq 0x150(%rsp), %rdx
callq 0x386c
movb $0x1, %bpl
leaq 0x2e8(%rsp), %rsi
movq %r14, %rdi
callq 0x55e8
xorl %ebp, %ebp
leaq 0x7fd3(%rip), %rsi      # 0xfd08
leaq -0x271e(%rip), %rdx     # 0x561e
movq %r14, %rdi
callq 0x3490
jmp 0x7e9d
pushq $0x28
popq %rdi
callq 0x3190
movq %rax, %r14
leaq 0x2722(%rip), %rsi      # 0xa47d
leaq 0x2a8(%rsp), %rdi
leaq 0x150(%rsp), %rdx
callq 0x386c
movb $0x1, %bpl
leaq 0x2a8(%rsp), %rsi
movq %r14, %rdi
callq 0x55e8
xorl %ebp, %ebp
leaq 0x7f7c(%rip), %rsi      # 0xfd08
leaq -0x2775(%rip), %rdx     # 0x561e
movq %r14, %rdi
callq 0x3490
jmp 0x7e9d
pushq $0x28
popq %rdi
callq 0x3190
movq %rax, %r14
leaq 0x26ec(%rip), %rsi      # 0xa49e
leaq 0x288(%rsp), %rdi
leaq 0x150(%rsp), %rdx
callq 0x386c
movb $0x1, %bpl
leaq 0x288(%rsp), %rsi
movq %r14, %rdi
callq 0x55e8
xorl %ebp, %ebp
leaq 0x7f25(%rip), %rsi      # 0xfd08
leaq -0x27cc(%rip), %rdx     # 0x561e
movq %r14, %rdi
callq 0x3490
jmp 0x7e9d
pushq $0x28
popq %rdi
callq 0x3190
movq %rax, %r14
leaq 0x2674(%rip), %rsi      # 0xa47d
leaq 0x268(%rsp), %rdi
leaq 0x150(%rsp), %rdx
callq 0x386c
movb $0x1, %bpl
leaq 0x268(%rsp), %rsi
movq %r14, %rdi
callq 0x55e8
xorl %ebp, %ebp
leaq 0x7ece(%rip), %rsi      # 0xfd08
leaq -0x2823(%rip), %rdx     # 0x561e
movq %r14, %rdi
callq 0x3490
jmp 0x7e9d
pushq $0x28
popq %rdi
callq 0x3190
movq %rax, %r14
leaq 0x2641(%rip), %rsi      # 0xa49e
leaq 0x248(%rsp), %rdi
leaq 0x150(%rsp), %rdx
callq 0x386c
movb $0x1, %bpl
leaq 0x248(%rsp), %rsi
movq %r14, %rdi
callq 0x55e8
xorl %ebp, %ebp
leaq 0x7e7a(%rip), %rsi      # 0xfd08
leaq -0x2877(%rip), %rdx     # 0x561e
movq %r14, %rdi
callq 0x3490
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
callq 0x3150
jmp 0x7eb2
movq %rax, %rbx
leaq 0xf0(%rsp), %rdi
callq 0x3150
jmp 0x7ec6
jmp 0x7ed1
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
jmp 0x7ed9
jmp 0x7ee2
jmp 0x7ee2
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3150
jmp 0x7ee5
jmp 0x7ee2
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
jmp 0x7f03
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x3150
jmp 0x7efe
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3150
jmp 0x7fb5
jmp 0x7fb2
movq %rax, %rbx
leaq 0x68(%rsp), %rdi
callq 0x3150
jmp 0x7f24
movq %rax, %rbx
leaq 0x40(%rsp), %rdi
callq 0x3150
jmp 0x7f33
movq %rax, %rbx
leaq 0x20(%rsp), %rdi
callq 0x3150
jmp 0x7f42
movq %rax, %rbx
leaq 0xb8(%rsp), %rdi
jmp 0x7fbd
jmp 0x7f4e
movq %rax, %rbx
jmp 0x7fc2
movq %rax, %rbx
leaq 0x110(%rsp), %rdi
callq 0x3150
jmp 0x7f68
movq %rax, %rbx
leaq 0xd8(%rsp), %rdi
callq 0x3c98
jmp 0x7f7a
movq %rax, %rbx
leaq 0x130(%rsp), %rdi
callq 0x3150
jmp 0x804b
jmp 0x8048
movq %rax, %rbx
leaq 0x248(%rsp), %rdi
jmp 0x8019
jmp 0x8025
movq %rax, %rbx
leaq 0x268(%rsp), %rdi
jmp 0x8019
jmp 0x8025
movq %rax, %rbx
leaq 0x88(%rsp), %rdi
callq 0x3150
leaq 0x150(%rsp), %rdi
callq 0x3c54
jmp 0x804b
movq %rax, %rbx
leaq 0x288(%rsp), %rdi
jmp 0x8019
jmp 0x8025
movq %rax, %rbx
leaq 0x2a8(%rsp), %rdi
jmp 0x8019
jmp 0x8025
jmp 0x8048
jmp 0x8048
jmp 0x8032
jmp 0x8048
jmp 0x8048
jmp 0x8048
jmp 0x8032
jmp 0x8048
movq %rax, %rbx
leaq 0x2c8(%rsp), %rdi
jmp 0x8019
jmp 0x8025
movq %rax, %rbx
leaq 0x2e8(%rsp), %rdi
callq 0x3150
testb %bpl, %bpl
jne 0x8028
jmp 0x804b
movq %rax, %rbx
movq %r14, %rdi
callq 0x3240
jmp 0x804b
movq %rax, %rbx
leaq 0x150(%rsp), %rdi
callq 0x3dda
jmp 0x804b
jmp 0x8048
jmp 0x8048
movq %rax, %rbx
leaq 0x8(%rsp), %rdi
callq 0x3dda
movq %rbx, %rdi
callq 0x34b0
nop