// File: dentiny[P]HTTP-Cache-Proxy/Proxy.cpp
// Function: Proxy::checkCaching(int, int, int, Request&)
bool checkCaching(int client_id, int client_fd, int server_fd, Request & request)
	{
		// (1) url not exist in the cache
		const std::string url = request.url;
		if(!cache.existsUrl(url))
		{
			std::string log_content = std::to_string(client_id) + ": not in cache";
			logger.log(log_content);
			return false;
		}
		Response response = cache.get(url);

		// (2) check expiration time
		// note: expiration time = response time + max-age 
		// if there's no no-cache mark in the response, and the response hasn't been expired, directly fetch it from cache
		time_t cur_time = time(NULL);
		if(cur_time <= response.expiration_time && !response.no_cache)
		{
			std::string log_content = std::to_string(client_id) + ": in cache, valid";
			logger.log(log_content);
			respondCached(client_fd, url);
			return true;
		}

		// (3) check e-tag
		// if these's etag, request If-None-Match tag to the web server
		// format: insert "If-None-Match: etag value" into the content 
		if(!response.etag.empty())
		{
			// write to log
			std::string log_content = std::to_string(client_id) + ": in cache, requires validation";
			logger.log(log_content);

			// create If-None-Match section
			std::string if_none_match = "\r\nIf-None-Match: " + response.etag;

			// insert the section into the request
			std::vector<char> content_to_send = insertSectionToContent(request.content, if_none_match);

			// resend and check the status code
			try
			{
				resendCheckStatus(client_id, client_fd, server_fd, content_to_send, url);
			}
			catch(std::exception & e)
			{
				throw e;
			}

			// has resolved re-validation, updated cache and resending
			return true;
		}

		// (4) check last-modified
		// if there's no etag, but there is last-modified
		else if(response.last_modified != 0)
		{
			// write to log
			std::string log_content = std::to_string(client_id) + ": in cache, requires validation";
			logger.log(log_content);

			// create If-Modified-Since section
  			std::string if_modified_since = "\r\nIf-Modified-Since: " + response.kv["Last-Modified"];

  			// insert the section into the request
  			std::vector<char> content_to_send = insertSectionToContent(request.content, if_modified_since);

  			// resend and check the status code
			try
			{
				resendCheckStatus(client_id, client_fd, server_fd, content_to_send, url);
			}
			catch(std::exception & e)
			{
				throw e;
			}

			// has resolved re-validation, updated cache and resending
			return true;
		}

		// convert expiration time to string
		tm * tm = localtime(&response.expiration_time);
		char * dt = asctime(tm);
		std::string expiration(dt);

		// write to log
		std::string log_content = std::to_string(client_id) + ": in cache, but expired at "  + expiration;
		logger.log(log_content);

		return false;
	}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x3f0, %rsp            # imm = 0x3F0
movq %rdi, -0x10(%rbp)
movl %esi, -0x14(%rbp)
movl %edx, -0x18(%rbp)
movl %ecx, -0x1c(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x3b8(%rbp)
movq -0x28(%rbp), %rsi
addq $0x60, %rsi
leaq -0x48(%rbp), %rdi
movq %rdi, -0x3b0(%rbp)
callq 0x4150
movq -0x3b8(%rbp), %rdi
movq -0x3b0(%rbp), %rsi
addq $0x250, %rdi            # imm = 0x250
callq 0xb2a0
movb %al, -0x3a1(%rbp)
jmp 0x8921
movb -0x3a1(%rbp), %al
testb $0x1, %al
jne 0x89e1
jmp 0x8931
movl -0x14(%rbp), %esi
leaq -0x98(%rbp), %rdi
callq 0x7bb0
jmp 0x8942
leaq 0x10971(%rip), %rdx     # 0x192ba
leaq -0x78(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x7b60
jmp 0x895b
leaq -0x98(%rbp), %rdi
callq 0x4250
movq -0x3b8(%rbp), %rdi
addq $0x8, %rdi
leaq -0x78(%rbp), %rsi
callq 0x7cd0
jmp 0x897d
movb $0x0, -0x1(%rbp)
movl $0x1, -0x9c(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x4250
jmp 0x913e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x9155
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x4250
jmp 0x9155
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x4250
jmp 0x9155
movq -0x3b8(%rbp), %rsi
addq $0x250, %rsi            # imm = 0x250
leaq -0x198(%rbp), %rdi
leaq -0x48(%rbp), %rdx
callq 0xb340
jmp 0x8a01
xorl %eax, %eax
movl %eax, %edi
callq 0x4390
movq %rax, -0x1a0(%rbp)
movq -0x1a0(%rbp), %rax
cmpq -0xd0(%rbp), %rax
jg 0x8b03
testb $0x1, -0xdf(%rbp)
jne 0x8b03
movl -0x14(%rbp), %esi
leaq -0x1e0(%rbp), %rdi
callq 0x7bb0
jmp 0x8a43
leaq 0x1087f(%rip), %rdx     # 0x192c9
leaq -0x1c0(%rbp), %rdi
leaq -0x1e0(%rbp), %rsi
callq 0x7b60
jmp 0x8a5f
leaq -0x1e0(%rbp), %rdi
callq 0x4250
movq -0x3b8(%rbp), %rdi
addq $0x8, %rdi
leaq -0x1c0(%rbp), %rsi
callq 0x7cd0
jmp 0x8a84
movq -0x3b8(%rbp), %rdi
movl -0x18(%rbp), %esi
leaq -0x48(%rbp), %rdx
callq 0xb510
jmp 0x8a99
movb $0x1, -0x1(%rbp)
movl $0x1, -0x9c(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x4250
jmp 0x9122
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x9130
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x1e0(%rbp), %rdi
callq 0x4250
jmp 0x9130
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x1c0(%rbp), %rdi
callq 0x4250
jmp 0x9130
leaq -0x198(%rbp), %rdi
addq $0xd8, %rdi
callq 0x45f0
testb $0x1, %al
jne 0x8cf9
movl -0x14(%rbp), %esi
leaq -0x220(%rbp), %rdi
callq 0x7bb0
jmp 0x8b2f
leaq 0x107a5(%rip), %rdx     # 0x192db
leaq -0x200(%rbp), %rdi
leaq -0x220(%rbp), %rsi
callq 0x7b60
jmp 0x8b4b
leaq -0x220(%rbp), %rdi
callq 0x4250
movq -0x3b8(%rbp), %rdi
addq $0x8, %rdi
leaq -0x200(%rbp), %rsi
callq 0x7cd0
jmp 0x8b70
leaq -0xc0(%rbp), %rdx
leaq 0x1077d(%rip), %rsi     # 0x192fb
leaq -0x240(%rbp), %rdi
callq 0xb780
jmp 0x8b8c
movq -0x3b8(%rbp), %rsi
movq -0x28(%rbp), %rdx
addq $0xc0, %rdx
leaq -0x258(%rbp), %rdi
leaq -0x240(%rbp), %rcx
callq 0xb890
jmp 0x8bb3
movq -0x3b8(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
leaq -0x258(%rbp), %r8
leaq -0x48(%rbp), %r9
callq 0xba90
jmp 0x8bd5
jmp 0x8c99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x220(%rbp), %rdi
callq 0x4250
jmp 0x9130
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x8ce8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x8cdc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x8cd0
movq -0x50(%rbp), %rdi
callq 0x4120
movq %rax, -0x260(%rbp)
movl $0x8, %edi
callq 0x41b0
movq %rax, %rdi
movq %rdi, -0x3c0(%rbp)
movq -0x260(%rbp), %rsi
callq 0x7db0
movq -0x3c0(%rbp), %rdi
movq 0x1b35d(%rip), %rsi     # 0x23fd0
movq 0x1b376(%rip), %rdx     # 0x23ff0
callq 0x4610
jmp 0x916f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
callq 0x45d0
jmp 0x8c97
jmp 0x8cd0
movb $0x1, -0x1(%rbp)
movl $0x1, -0x9c(%rbp)
leaq -0x258(%rbp), %rdi
callq 0x6130
leaq -0x240(%rbp), %rdi
callq 0x4250
leaq -0x200(%rbp), %rdi
callq 0x4250
jmp 0x9122
leaq -0x258(%rbp), %rdi
callq 0x6130
leaq -0x240(%rbp), %rdi
callq 0x4250
leaq -0x200(%rbp), %rdi
callq 0x4250
jmp 0x9130
cmpq $0x0, -0xc8(%rbp)
je 0x8f8a
movl -0x14(%rbp), %esi
leaq -0x2a0(%rbp), %rdi
callq 0x7bb0
jmp 0x8d18
leaq 0x105bc(%rip), %rdx     # 0x192db
leaq -0x280(%rbp), %rdi
leaq -0x2a0(%rbp), %rsi
callq 0x7b60
jmp 0x8d34
leaq -0x2a0(%rbp), %rdi
callq 0x4250
movq -0x3b8(%rbp), %rdi
addq $0x8, %rdi
leaq -0x280(%rbp), %rsi
callq 0x7cd0
jmp 0x8d59
leaq -0x118(%rbp), %rax
movq %rax, -0x3d0(%rbp)
leaq -0x2e1(%rbp), %rdi
movq %rdi, -0x3c8(%rbp)
callq 0x4650
movq -0x3c8(%rbp), %rdx
leaq 0x1059b(%rip), %rsi     # 0x19323
leaq -0x2e0(%rbp), %rdi
callq 0x4bf0
jmp 0x8d96
movq -0x3d0(%rbp), %rdi
leaq -0x2e0(%rbp), %rsi
callq 0xc4c0
movq %rax, -0x3d8(%rbp)
jmp 0x8db2
movq -0x3d8(%rbp), %rdx
leaq 0x1054d(%rip), %rsi     # 0x1930d
leaq -0x2c0(%rbp), %rdi
callq 0xb780
jmp 0x8dce
leaq -0x2e0(%rbp), %rdi
callq 0x4250
leaq -0x2e1(%rbp), %rdi
callq 0x4420
movq -0x3b8(%rbp), %rsi
movq -0x28(%rbp), %rdx
addq $0xc0, %rdx
leaq -0x300(%rbp), %rdi
leaq -0x2c0(%rbp), %rcx
callq 0xb890
jmp 0x8e0d
movq -0x3b8(%rbp), %rdi
movl -0x14(%rbp), %esi
movl -0x18(%rbp), %edx
movl -0x1c(%rbp), %ecx
leaq -0x300(%rbp), %r8
leaq -0x48(%rbp), %r9
callq 0xba90
jmp 0x8e2f
jmp 0x8f2a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x2a0(%rbp), %rdi
callq 0x4250
jmp 0x9130
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x8f79
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x8e88
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x2e0(%rbp), %rdi
callq 0x4250
leaq -0x2e1(%rbp), %rdi
callq 0x4420
jmp 0x8f79
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x8f6d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
movl -0x54(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x8f61
movq -0x50(%rbp), %rdi
callq 0x4120
movq %rax, -0x308(%rbp)
movl $0x8, %edi
callq 0x41b0
movq %rax, %rdi
movq %rdi, -0x3e0(%rbp)
movq -0x308(%rbp), %rsi
callq 0x7db0
movq -0x3e0(%rbp), %rdi
movq 0x1b0cc(%rip), %rsi     # 0x23fd0
movq 0x1b0e5(%rip), %rdx     # 0x23ff0
callq 0x4610
jmp 0x916f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
callq 0x45d0
jmp 0x8f28
jmp 0x8f61
movb $0x1, -0x1(%rbp)
movl $0x1, -0x9c(%rbp)
leaq -0x300(%rbp), %rdi
callq 0x6130
leaq -0x2c0(%rbp), %rdi
callq 0x4250
leaq -0x280(%rbp), %rdi
callq 0x4250
jmp 0x9122
leaq -0x300(%rbp), %rdi
callq 0x6130
leaq -0x2c0(%rbp), %rdi
callq 0x4250
leaq -0x280(%rbp), %rdi
callq 0x4250
jmp 0x9130
jmp 0x8f8c
leaq -0xd0(%rbp), %rdi
callq 0x42e0
movq %rax, -0x310(%rbp)
movq -0x310(%rbp), %rdi
callq 0x4140
movq %rax, -0x318(%rbp)
movq -0x318(%rbp), %rax
movq %rax, -0x3f0(%rbp)
leaq -0x339(%rbp), %rdi
movq %rdi, -0x3e8(%rbp)
callq 0x4650
movq -0x3f0(%rbp), %rsi
movq -0x3e8(%rbp), %rdx
leaq -0x338(%rbp), %rdi
callq 0x4bf0
jmp 0x8fef
leaq -0x339(%rbp), %rdi
callq 0x4420
movl -0x14(%rbp), %esi
leaq -0x3a0(%rbp), %rdi
callq 0x7bb0
jmp 0x900c
leaq 0x1031e(%rip), %rdx     # 0x19331
leaq -0x380(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
callq 0x7b60
jmp 0x9028
leaq -0x360(%rbp), %rdi
leaq -0x380(%rbp), %rsi
leaq -0x338(%rbp), %rdx
callq 0x7b10
jmp 0x9044
leaq -0x380(%rbp), %rdi
callq 0x4250
leaq -0x3a0(%rbp), %rdi
callq 0x4250
movq -0x3b8(%rbp), %rdi
addq $0x8, %rdi
leaq -0x360(%rbp), %rsi
callq 0x7cd0
jmp 0x9075
movb $0x0, -0x1(%rbp)
movl $0x1, -0x9c(%rbp)
leaq -0x360(%rbp), %rdi
callq 0x4250
leaq -0x338(%rbp), %rdi
callq 0x4250
jmp 0x9122
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x339(%rbp), %rdi
callq 0x4420
jmp 0x9130
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x9114
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
jmp 0x90ee
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x380(%rbp), %rdi
callq 0x4250
leaq -0x3a0(%rbp), %rdi
callq 0x4250
jmp 0x9114
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x50(%rbp)
movl %eax, -0x54(%rbp)
leaq -0x360(%rbp), %rdi
callq 0x4250
leaq -0x338(%rbp), %rdi
callq 0x4250
jmp 0x9130
leaq -0x198(%rbp), %rdi
callq 0x5970
jmp 0x913e
leaq -0x198(%rbp), %rdi
callq 0x5970
jmp 0x9155
leaq -0x48(%rbp), %rdi
callq 0x4250
movb -0x1(%rbp), %al
andb $0x1, %al
addq $0x3f0, %rsp            # imm = 0x3F0
popq %rbp
retq
leaq -0x48(%rbp), %rdi
callq 0x4250
movq -0x50(%rbp), %rdi
callq 0x4640
movq %rax, %rdi
callq 0x52e0
nop