// File: /workspace/github_cpp_makefile_star2_O2/mbourand[P]webserv/srcs/Methods/PostMethod.cpp
// Function: PostMethod::process(Request const&, ConfigContext const&, ServerSocket const&)
Response PostMethod::process(const Request& request, const ConfigContext& config, const ServerSocket& socket)
{
	const URL& url = request._url;
	int base_depth = 0;
	Response response(200, url._path); //change code on failure
	std::string realPath = config.rootPath(url._path, base_depth);
	try
	{
		realPath = ft::simplify_path(realPath, true, base_depth);
	}
	catch (std::exception& e)
	{
		return (response.setCode(404));
	}
	if (ft::is_directory(realPath))
	{
		try
		{
			std::string index = "/" + config.getParam("index").front();
			realPath += index;
		}
		catch (std::exception& e)
		{}
	}
	std::list<std::string> splitted = ft::split(realPath, "/");
	std::string extension = ft::get_extension(splitted.back());
	if ((!extension.empty() && config.getCGIExtensions().find(extension) != config.getCGIExtensions().end()) || (realPath.find(config.getParam("cgi_dir").front()) == 0))
	{
		try
		{
			CGI	cgi(request, config, socket, realPath);
			cgi.process(response);
		}
		catch(const CGI::CGIException &e)
		{
			Logger::print(e.what(), NULL, ERROR, NORMAL);
			response.setCode(e.code());
		}
	}
	else
	{
		std::fstream file(realPath.c_str());
		if (!file.good() || !file.is_open())
		{
			if (errno == ENOENT || errno == ENOTDIR)
				return Logger::print("File not found", response.setCode(404), ERROR, VERBOSE);
			if (errno == EACCES || errno == EISDIR)
				return Logger::print("Permission denied", response.setCode(403), ERROR, VERBOSE);
			return Logger::print("Unexpected error while trying to open file: " + std::string(strerror(errno)), response.setCode(500), ERROR, VERBOSE);
		}
		std::string content((std::istreambuf_iterator<char>(file)), (std::istreambuf_iterator<char>()));
		std::ostringstream convert;
		struct stat file_stats;
		lstat(realPath.c_str(), &file_stats);
		convert << file_stats.st_size;
		response.addHeader("Content-Length", convert.str());
		convert.str("");
		convert << file_stats.st_mtime;
		struct tm	time;
		strptime(std::string(convert.str()).c_str(), "%s", &time);
		if (time.tm_gmtoff > 0) // convert to GMT
			file_stats.st_mtim.tv_sec -= time.tm_gmtoff;
		if (time.tm_isdst) // substract Daylight saving time
			file_stats.st_mtim.tv_sec -= 3600;
		convert.str("");
		convert << file_stats.st_mtime;
		strptime(std::string(convert.str()).c_str(), "%s", &time);
		char		buffer[1024];
		strftime(buffer, sizeof(buffer), "%a, %d %b %Y %H:%M:%S GMT", &time);
		response.addHeader("Last-Modified", buffer);
		convert.str("");
		response.addDateHeader();
		response.addHeader("Server", "Webserv");
		t_hnode	*hnode = g_webserv.file_formatname->GetNode(realPath.substr(realPath.find_last_of('.') + 1));
		if (hnode != NULL)
			response.addHeader("Content-Type", hnode->value + "; charset=UTF-8");
		response.setBody(content);
	}
	return response;
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xc58, %rsp            # imm = 0xC58
movq %r8, %r15
movq %rcx, %r12
movq %rdx, %r13
movq %rdi, %rbx
leaq 0x2c(%rsp), %r14
andl $0x0, (%r14)
leaq 0xb8(%rdx), %rbp
leaq 0x1c0(%rsp), %rdi
movl $0xc8, %esi
movq %rbp, %rdx
callq 0x2a080
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
movq %rbp, %rdx
movq %r14, %rcx
callq 0x21a22
movl 0x2c(%rsp), %ecx
leaq 0x260(%rsp), %rdi
leaq 0x50(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x17b31
leaq 0x50(%rsp), %r14
leaq 0x260(%rsp), %rbp
movq %r14, %rdi
movq %rbp, %rsi
callq 0x75f0
movq %rbp, %rdi
callq 0x79b8
leaq 0x1a0(%rsp), %rdi
movq %r14, %rsi
callq 0x7690
leaq 0x1a0(%rsp), %rdi
callq 0x17813
movl %eax, %ebp
leaq 0x1a0(%rsp), %rdi
callq 0x79b8
testb %bpl, %bpl
je 0xd63c
leaq 0x21723(%rip), %rsi     # 0x2ecf3
leaq 0x6f0(%rsp), %rdi
leaq 0xae0(%rsp), %rdx
callq 0x8032
leaq 0x6f0(%rsp), %rsi
movq %r12, %rdi
callq 0x2167a
movq (%rax), %rdx
addq $0x10, %rdx
leaq 0x1f445(%rip), %rsi     # 0x2ca48
leaq 0x260(%rsp), %rdi
callq 0xc154
leaq 0x6f0(%rsp), %rdi
callq 0x79b8
leaq 0x50(%rsp), %rdi
leaq 0x260(%rsp), %rsi
callq 0x7950
leaq 0x260(%rsp), %rdi
callq 0x79b8
leaq 0x1f405(%rip), %rsi     # 0x2ca48
leaq 0x260(%rsp), %rdi
leaq 0x6f0(%rsp), %rdx
callq 0x8032
leaq 0x70(%rsp), %rdi
leaq 0x50(%rsp), %rsi
leaq 0x260(%rsp), %rdx
callq 0x16f10
leaq 0x260(%rsp), %rdi
callq 0x79b8
movq 0x78(%rsp), %rsi
addq $0x10, %rsi
leaq 0x180(%rsp), %rdi
callq 0x7690
leaq 0xa8(%rsp), %rdi
leaq 0x180(%rsp), %rsi
callq 0x17e0b
leaq 0x180(%rsp), %rdi
callq 0x79b8
cmpq $0x0, 0xb0(%rsp)
je 0xd6ef
movq %r12, %rdi
callq 0x21648
leaq 0xa8(%rsp), %rsi
movq %rax, %rdi
callq 0xcdc8
movq %rax, %r14
movq %r12, %rdi
callq 0x21648
addq $0x8, %rax
cmpq %rax, %r14
jne 0xdb7a
leaq 0x1efcb(%rip), %rsi     # 0x2c6c1
leaq 0x260(%rsp), %rdi
leaq 0x6f0(%rsp), %rdx
callq 0x8032
leaq 0x260(%rsp), %rsi
movq %r12, %rdi
callq 0x2167a
movq (%rax), %rsi
addq $0x10, %rsi
leaq 0x50(%rsp), %rdi
xorl %edx, %edx
callq 0x7990
movq %rax, %r14
leaq 0x260(%rsp), %rdi
callq 0x79b8
testq %r14, %r14
je 0xdb7a
movq 0x50(%rsp), %rsi
leaq 0x6f0(%rsp), %rdi
pushq $0x18
popq %rdx
callq 0x7660
movq 0x6f0(%rsp), %rax
movq -0x18(%rax), %r14
cmpl $0x0, 0x710(%rsp,%r14)
jne 0xdbcc
leaq 0x770(%rsp), %rdi
callq 0x7130
testb %al, %al
je 0xdbcc
movq 0x7d8(%rsp,%r14), %rsi
leaq 0x98(%rsp), %rax
movq %rax, -0x10(%rax)
leaq 0x88(%rsp), %rdi
pushq $-0x1
popq %rdx
xorl %ecx, %ecx
movl %edx, %r8d
callq 0xcee8
leaq 0xae0(%rsp), %rdi
callq 0x7670
movq 0x50(%rsp), %rdi
leaq 0x660(%rsp), %r14
movq %r14, %rsi
callq 0x7940
movq 0x30(%r14), %rsi
leaq 0xae0(%rsp), %rdi
callq 0x74f0
leaq 0x1e915(%rip), %rsi     # 0x2c104
leaq 0x260(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x8032
leaq 0xae8(%rsp), %r14
leaq 0xc8(%rsp), %rdi
movq %r14, %rsi
callq 0x7780
leaq 0x1c0(%rsp), %rdi
leaq 0x260(%rsp), %rsi
leaq 0xc8(%rsp), %rdx
callq 0x2a124
leaq 0xc8(%rsp), %rdi
callq 0x79b8
leaq 0x260(%rsp), %rdi
callq 0x79b8
leaq 0x1f4f8(%rip), %rsi     # 0x2cd4f
leaq 0x260(%rsp), %rdi
leaq 0xc8(%rsp), %rdx
callq 0x8032
leaq 0x260(%rsp), %rsi
movq %r14, %rdi
callq 0x7720
leaq 0x260(%rsp), %rdi
callq 0x79b8
movq 0x6b8(%rsp), %r15
leaq 0xae0(%rsp), %rdi
movq %r15, %rsi
callq 0x74f0
leaq 0x260(%rsp), %rdi
movq %r14, %rsi
callq 0x7780
leaq 0x260(%rsp), %r12
movq (%r12), %rdi
leaq 0x1ea95(%rip), %rsi     # 0x2c359
leaq 0xc8(%rsp), %r13
movq %r13, %rdx
callq 0x70a0
movq %r12, %rdi
callq 0x79b8
movq 0x28(%r13), %rax
testq %rax, %rax
jle 0xd8f0
subq %rax, %r15
movq %r15, 0x6b8(%rsp)
cmpl $0x0, 0xe8(%rsp)
je 0xd909
addq $-0xe10, %r15           # imm = 0xF1F0
movq %r15, 0x6b8(%rsp)
leaq 0x1f43f(%rip), %rsi     # 0x2cd4f
leaq 0x260(%rsp), %rdi
leaq 0x8(%rsp), %rdx
callq 0x8032
leaq 0x260(%rsp), %rsi
movq %r14, %rdi
callq 0x7720
leaq 0x260(%rsp), %rdi
callq 0x79b8
leaq 0xae0(%rsp), %rdi
movq %r15, %rsi
callq 0x74f0
leaq 0x260(%rsp), %rdi
movq %r14, %rsi
callq 0x7780
leaq 0x260(%rsp), %r15
movq (%r15), %rdi
leaq 0x1e9e8(%rip), %rsi     # 0x2c359
leaq 0xc8(%rsp), %r12
movq %r12, %rdx
callq 0x70a0
movq %r15, %rdi
callq 0x79b8
leaq 0x1ed7e(%rip), %rdx     # 0x2c70e
leaq 0x260(%rsp), %rdi
movl $0x400, %esi            # imm = 0x400
movq %r12, %rcx
callq 0x7440
leaq 0x1e81f(%rip), %rsi     # 0x2c1cb
leaq 0x8(%rsp), %rdi
leaq 0x6(%rsp), %rdx
callq 0x8032
leaq 0x30(%rsp), %rdi
leaq 0x260(%rsp), %rsi
leaq 0x7(%rsp), %rdx
callq 0x8032
leaq 0x1c0(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x2a124
leaq 0x30(%rsp), %rdi
callq 0x79b8
leaq 0x8(%rsp), %rdi
callq 0x79b8
leaq 0x1f34b(%rip), %rsi     # 0x2cd4f
leaq 0x8(%rsp), %rdi
leaq 0x30(%rsp), %rdx
callq 0x8032
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x7720
leaq 0x8(%rsp), %rdi
callq 0x79b8
leaq 0x1c0(%rsp), %rdi
callq 0x2a2f0
leaq 0x1e80e(%rip), %rsi     # 0x2c24c
leaq 0x8(%rsp), %rdi
leaq 0x6(%rsp), %rdx
callq 0x8032
leaq 0x1f13c(%rip), %rsi     # 0x2cb90
leaq 0x30(%rsp), %rdi
leaq 0x7(%rsp), %rdx
callq 0x8032
leaq 0x1c0(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x2a124
leaq 0x30(%rsp), %rdi
callq 0x79b8
leaq 0x8(%rsp), %rdi
callq 0x79b8
leaq 0x35b1b(%rip), %rax     # 0x435b0
movq 0x8(%rax), %r14
leaq 0x50(%rsp), %r15
pushq $0x2e
popq %rsi
pushq $-0x1
popq %r12
movq %r15, %rdi
movq %r12, %rdx
callq 0x7970
leaq 0x1(%rax), %rdx
leaq 0x8(%rsp), %rdi
movq %r15, %rsi
movq %r12, %rcx
callq 0x7540
leaq 0x8(%rsp), %rsi
movq %r14, %rdi
callq 0x13334
movq %rax, %r14
leaq 0x8(%rsp), %rdi
callq 0x79b8
testq %r14, %r14
je 0xdb3c
leaq 0x1e668(%rip), %rsi     # 0x2c152
leaq 0x8(%rsp), %rdi
leaq 0x6(%rsp), %rdx
callq 0x8032
addq $0x20, %r14
leaq 0x1eb9c(%rip), %rdx     # 0x2c6a0
leaq 0x30(%rsp), %rdi
movq %r14, %rsi
callq 0xc1e9
leaq 0x1c0(%rsp), %rdi
leaq 0x8(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x2a124
leaq 0x30(%rsp), %rdi
callq 0x79b8
leaq 0x8(%rsp), %rdi
callq 0x79b8
leaq 0x1c0(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x2a82e
leaq 0xae0(%rsp), %rdi
callq 0x7140
leaq 0x88(%rsp), %rdi
callq 0x79b8
leaq 0x6f0(%rsp), %rdi
callq 0x7110
jmp 0xdbb7
leaq 0x260(%rsp), %rdi
leaq 0x50(%rsp), %r8
movq %r13, %rsi
movq %r12, %rdx
movq %r15, %rcx
callq 0xfd8c
leaq 0x260(%rsp), %rdi
leaq 0x1c0(%rsp), %rsi
callq 0x11968
leaq 0x260(%rsp), %rdi
callq 0x11460
leaq 0x1c0(%rsp), %rsi
movq %rbx, %rdi
callq 0x29f62
jmp 0xdd88
callq 0x7060
movl (%rax), %edi
cmpl $0x2, %edi
je 0xdbef
cmpl $0xd, %edi
je 0xdc65
cmpl $0x15, %edi
je 0xdc65
cmpl $0x14, %edi
jne 0xdcdb
leaq 0x1ead3(%rip), %rsi     # 0x2c6c9
leaq 0x140(%rsp), %rdi
leaq 0x260(%rsp), %rdx
callq 0x8032
leaq 0x1c0(%rsp), %rdi
movl $0x194, %esi            # imm = 0x194
callq 0x2a818
leaq 0xa40(%rsp), %rdi
movq %rax, %rsi
callq 0x29f62
leaq 0x140(%rsp), %rsi
leaq 0xa40(%rsp), %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
xorl %r8d, %r8d
callq 0xc0bc
leaq 0xa40(%rsp), %rdi
callq 0x29f10
leaq 0x140(%rsp), %rdi
jmp 0xdd76
leaq 0x1e704(%rip), %rsi     # 0x2c370
leaq 0x120(%rsp), %rdi
leaq 0x260(%rsp), %rdx
callq 0x8032
leaq 0x1c0(%rsp), %rdi
movl $0x193, %esi            # imm = 0x193
callq 0x2a818
leaq 0x9a0(%rsp), %rdi
movq %rax, %rsi
callq 0x29f62
leaq 0x120(%rsp), %rsi
leaq 0x9a0(%rsp), %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
xorl %r8d, %r8d
callq 0xc0bc
leaq 0x9a0(%rsp), %rdi
callq 0x29f10
leaq 0x120(%rsp), %rdi
jmp 0xdd76
callq 0x71d0
leaq 0x260(%rsp), %rdi
leaq 0xae0(%rsp), %rdx
movq %rax, %rsi
callq 0x8032
leaq 0x1e9e2(%rip), %rsi     # 0x2c6e1
leaq 0x100(%rsp), %rdi
leaq 0x260(%rsp), %rdx
callq 0xc30e
leaq 0x1c0(%rsp), %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0x2a818
leaq 0x900(%rsp), %rdi
movq %rax, %rsi
callq 0x29f62
leaq 0x100(%rsp), %rsi
leaq 0x900(%rsp), %rdx
pushq $0x3
popq %rcx
movq %rbx, %rdi
xorl %r8d, %r8d
callq 0xc0bc
leaq 0x900(%rsp), %rdi
callq 0x29f10
leaq 0x100(%rsp), %rdi
callq 0x79b8
leaq 0x260(%rsp), %rdi
callq 0x79b8
leaq 0x6f0(%rsp), %rdi
callq 0x7110
leaq 0xa8(%rsp), %rdi
callq 0x79b8
leaq 0x70(%rsp), %rdi
callq 0x8f64
leaq 0x50(%rsp), %rdi
callq 0x79b8
leaq 0x1c0(%rsp), %rdi
callq 0x29f10
movq %rbx, %rax
addq $0xc58, %rsp            # imm = 0xC58
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xde17
jmp 0xde26
jmp 0xdeb5
movq %rax, %r14
leaq 0x900(%rsp), %rdi
callq 0x29f10
jmp 0xde97
movq %rax, %r14
jmp 0xdea4
jmp 0xdeb0
jmp 0xdeb5
jmp 0xde26
jmp 0xdeb5
jmp 0xde17
jmp 0xde26
jmp 0xdeb5
jmp 0xdeb5
jmp 0xde26
jmp 0xdeb5
movq %rax, %r14
leaq 0x30(%rsp), %rdi
callq 0x79b8
jmp 0xde29
movq %rax, %r14
leaq 0x8(%rsp), %rdi
jmp 0xde60
jmp 0xdeb5
jmp 0xdeb5
jmp 0xdeb5
jmp 0xde55
jmp 0xdeb5
jmp 0xdeb5
jmp 0xde55
jmp 0xdeb5
movq %rax, %r14
leaq 0xc8(%rsp), %rdi
callq 0x79b8
jmp 0xde58
movq %rax, %r14
leaq 0x260(%rsp), %rdi
callq 0x79b8
jmp 0xdeb8
jmp 0xdeb5
movq %rax, %r14
jmp 0xdec5
jmp 0xdeb0
movq %rax, %r14
leaq 0xa40(%rsp), %rdi
callq 0x29f10
jmp 0xdee7
movq %rax, %r14
leaq 0x9a0(%rsp), %rdi
callq 0x29f10
jmp 0xdef4
movq %rax, %r14
leaq 0x100(%rsp), %rdi
callq 0x79b8
leaq 0x260(%rsp), %rdi
jmp 0xdecd
jmp 0xdeb0
movq %rax, %r14
jmp 0xded2
movq %rax, %r14
leaq 0xae0(%rsp), %rdi
callq 0x7140
leaq 0x88(%rsp), %rdi
callq 0x79b8
leaq 0x6f0(%rsp), %rdi
callq 0x7110
jmp 0xe0c1
movq %rax, %r14
leaq 0x140(%rsp), %rdi
jmp 0xdecd
movq %rax, %r14
leaq 0x120(%rsp), %rdi
jmp 0xdecd
jmp 0xe0be
movq %rdx, %rbp
movq %rax, %r14
leaq 0x260(%rsp), %rdi
jmp 0xe026
movq %rdx, %rbp
movq %rax, %r14
jmp 0xe02b
movq %rax, %r14
leaq 0x260(%rsp), %rdi
callq 0x79b8
jmp 0xe0c1
jmp 0xe0be
movq %rdx, %r15
movq %rax, %r14
leaq 0x260(%rsp), %rdi
callq 0x11460
jmp 0xdf56
movq %rdx, %r15
movq %rax, %r14
cmpl $0x2, %r15d
jne 0xe0c1
movq %r14, %rdi
callq 0x7220
movq %rax, %r14
movq (%rax), %rax
movq %r14, %rdi
callq *0x10(%rax)
leaq 0x160(%rsp), %rdi
leaq 0x260(%rsp), %rdx
movq %rax, %rsi
callq 0x8032
leaq 0x160(%rsp), %rdi
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %esi, %esi
callq 0xbe10
leaq 0x160(%rsp), %rdi
callq 0x79b8
movq (%r14), %rax
movq %r14, %rdi
callq *0x18(%rax)
leaq 0x1c0(%rsp), %rdi
movl %eax, %esi
callq 0x2a818
callq 0x77f0
jmp 0xdbb7
jmp 0xe0be
jmp 0xdfe9
movq %rax, %r14
leaq 0x160(%rsp), %rdi
callq 0x79b8
jmp 0xdfec
movq %rax, %r14
callq 0x77f0
jmp 0xe0c1
jmp 0xe0be
jmp 0xe0be
movq %rax, %r14
leaq 0x180(%rsp), %rdi
jmp 0xe0c9
movq %rax, %r14
jmp 0xe0ce
movq %rdx, %rbp
movq %rax, %r14
leaq 0x6f0(%rsp), %rdi
callq 0x79b8
cmpl $0x1, %ebp
jne 0xe0d8
movq %r14, %rdi
callq 0x7220
callq 0x77f0
jmp 0xd63c
jmp 0xe069
movq %rax, %r14
leaq 0x260(%rsp), %rdi
jmp 0xe062
jmp 0xe069
movq %rax, %r14
leaq 0x1a0(%rsp), %rdi
callq 0x79b8
jmp 0xe0d8
movq %rax, %r14
jmp 0xe0d8
movq %rax, %r14
cmpl $0x1, %edx
jne 0xe0d8
movq %r14, %rdi
callq 0x7220
leaq 0x1c0(%rsp), %rdi
movl $0x194, %esi            # imm = 0x194
callq 0x2a818
movq %rbx, %rdi
movq %rax, %rsi
callq 0x29f62
callq 0x77f0
jmp 0xdd9f
jmp 0xe069
movq %rax, %r14
callq 0x77f0
jmp 0xe0d8
movq %rax, %rdi
callq 0x8f59
movq %rax, %r14
jmp 0xe0e2
movq %rax, %r14
leaq 0xa8(%rsp), %rdi
callq 0x79b8
leaq 0x70(%rsp), %rdi
callq 0x8f64
leaq 0x50(%rsp), %rdi
callq 0x79b8
leaq 0x1c0(%rsp), %rdi
callq 0x29f10
movq %r14, %rdi
callq 0x7860
nop