// File: /workspace/github_cpp_makefile_star2_O3/mbourand[P]webserv/srcs/Methods/PutMethod.cpp
// Function: PutMethod::process(Request const&, ConfigContext const&, ServerSocket const&)
Response PutMethod::process(const Request& request, const ConfigContext& config, const ServerSocket&)
{
	URL		url(request._url._path);
	int		base_depth = 0;

	std::string realPath = config.rootPath(url._path, base_depth);
	try
	{
		realPath = ft::simplify_path(realPath, true, base_depth);
	}
	catch (std::exception& e)
	{
		return Response(404, url._path);
	}

	Response response(200, url._path);
	if (realPath.rfind('/') != config.getParam("root").front().size()) // if no ext or target is subfoler inside uploads
		return Response(415, url._path);

	if (realPath.rfind('.') != std::string::npos)
	{
		std::string extension = ft::get_extension(realPath);
		if (!config.can_be_uploaded(extension) || !g_webserv.file_formatname->GetNode(extension.substr(1))
		|| request.getHeaderValue("Content-Type") != g_webserv.file_formatname->Lookup(extension.substr(1)))
			return Response(415, url._path);
	}
	std::fstream file;
	struct stat	file_stats;
	if (lstat(realPath.c_str(), &file_stats) < 0)
	{
		file.open(realPath.c_str(), std::fstream::in | std::fstream::out | std::fstream::trunc);
		response.setCode(201);
		response.addHeader("Location", realPath.substr(realPath.find('/')));
	}
	else if (!S_ISREG(file_stats.st_mode))
		return Response(500, url._path);
	else
		file.open(realPath.c_str(), std::fstream::in | std::fstream::out | std::fstream::trunc);
	if (!file.good() || !file.is_open())
		return Response(500, url._path);
	response.addHeader("Content-Location", realPath.substr(realPath.find('/')));
	file << request._body;
	file.close();
	return (response);
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x4e8, %rsp            # imm = 0x4E8
movq %rcx, %r12
movq %rdi, %rbx
movq %rdx, 0x8(%rsp)
leaq 0xb8(%rdx), %rsi
leaq 0x3f8(%rsp), %rdi
callq 0x1aede
leaq 0x74(%rsp), %rcx
movl $0x0, (%rcx)
leaq 0x480(%rsp), %r15
leaq 0x50(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2b9a8
movl 0x74(%rsp), %ecx
leaq 0xb8(%rsp), %rdi
leaq 0x50(%rsp), %rsi
movl $0x1, %edx
callq 0x1d26b
leaq 0x50(%rsp), %rdi
leaq 0xb8(%rsp), %r13
movq %r13, %rsi
callq 0x7590
movq (%r13), %rdi
leaq 0xc8(%rsp), %rbp
cmpq %rbp, %rdi
je 0x10f24
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x358(%rsp), %rdi
movl $0xc8, %esi
movq %r15, %rdx
callq 0x36f58
leaq 0x50(%rsp), %rdi
movl $0x2f, %esi
movq $-0x1, %rdx
callq 0x7900
movq %rax, %r13
leaq 0xb8(%rsp), %rdi
movq %rbp, (%rdi)
leaq 0x29910(%rip), %rsi     # 0x3a874
leaq 0x2990d(%rip), %rdx     # 0x3a878
callq 0x80f4
leaq 0xb8(%rsp), %rsi
movq %r12, %rdi
callq 0x2b17e
movq (%rax), %rax
movq 0x18(%rax), %r14
movq 0xb8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x10fa4
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x74b0
cmpq %r14, %r13
jne 0x1110b
leaq 0x50(%rsp), %rdi
movl $0x2e, %esi
movq $-0x1, %rdx
callq 0x7900
cmpq $-0x1, %rax
je 0x111f4
leaq 0x88(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x50(%rsp), %rsi
movq 0x58(%rsp), %rdx
addq %rsi, %rdx
leaq 0x78(%rsp), %rdi
callq 0x8a92
leaq 0xb8(%rsp), %rdi
leaq 0x78(%rsp), %rsi
callq 0x1d5fc
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x1101c
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0xb8(%rsp), %rsi
movq %r12, %rdi
callq 0x2be82
testb %al, %al
je 0x111a1
leaq 0x3d52d(%rip), %r13     # 0x4e568
movq 0x8(%r13), %r12
leaq 0x2c8(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
movl $0x1, %edx
movq $-0x1, %rcx
callq 0x74d0
leaq 0x2c8(%rsp), %rsi
movq %r12, %rdi
callq 0x17fd4
testq %rax, %rax
je 0x11120
leaq 0x40(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x290c9(%rip), %rsi     # 0x3a152
leaq 0x290ce(%rip), %rdx     # 0x3a15e
leaq 0x30(%rsp), %rdi
callq 0x80f4
leaq 0x10(%rsp), %rdi
leaq 0x30(%rsp), %rdx
movq 0x8(%rsp), %rsi
callq 0x35bbe
movq 0x8(%r13), %r12
leaq 0x98(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
movl $0x1, %edx
movq $-0x1, %rcx
callq 0x74d0
leaq 0x98(%rsp), %rsi
movq %r12, %rdi
callq 0x181d8
movq 0x18(%rsp), %rdx
movb $0x1, %r12b
cmpq 0x8(%rax), %rdx
jne 0x11128
testq %rdx, %rdx
je 0x11125
movq (%rax), %rsi
movq 0x10(%rsp), %rdi
callq 0x74c0
testl %eax, %eax
setne %r12b
jmp 0x11128
movq %rbx, %rdi
movl $0x19f, %esi            # imm = 0x19F
movq %r15, %rdx
callq 0x36f58
jmp 0x11429
movb $0x1, %r12b
jmp 0x1117b
xorl %r12d, %r12d
leaq 0xa8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11149
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11164
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x1117b
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x2d8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1119c
movq 0x2d8(%rsp), %rsi
incq %rsi
callq 0x74b0
testb %r12b, %r12b
je 0x111d7
movq %rbx, %rdi
movl $0x19f, %esi            # imm = 0x19F
movq %r15, %rdx
callq 0x36f58
movq 0xb8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x11429
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x11429
movq 0xb8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x111f4
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0xb8(%rsp), %rdi
callq 0x7550
movq 0x50(%rsp), %rdi
leaq 0x2c8(%rsp), %rsi
callq 0x78e0
testl %eax, %eax
js 0x1124a
movl $0xf000, %eax           # imm = 0xF000
andl 0x2e0(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x1140c
movq 0x50(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
movl $0x38, %edx
callq 0x7450
jmp 0x11308
movq 0x50(%rsp), %rsi
leaq 0xb8(%rsp), %rdi
movl $0x38, %edx
callq 0x7450
leaq 0x358(%rsp), %rdi
movl $0xc9, %esi
callq 0x37aee
leaq 0x20(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x28eae(%rip), %rsi     # 0x3a131
leaq 0x28eaf(%rip), %rdx     # 0x3a139
leaq 0x10(%rsp), %rdi
callq 0x80f4
leaq 0x50(%rsp), %r12
movq %r12, %rdi
movl $0x2f, %esi
xorl %edx, %edx
callq 0x75c0
leaq 0x30(%rsp), %rdi
movq %r12, %rsi
movq %rax, %rdx
movq $-0x1, %rcx
callq 0x74d0
leaq 0x358(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x37020
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x112f1
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x11308
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0xb8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0xd8(%rsp,%rax)
jne 0x1140c
leaq 0x138(%rsp), %rdi
callq 0x7130
testb %al, %al
je 0x1140c
leaq 0x20(%rsp), %r12
movq %r12, -0x10(%r12)
leaq 0x28de1(%rip), %rsi     # 0x3a129
leaq 0x28dea(%rip), %rdx     # 0x3a139
leaq 0x10(%rsp), %rdi
callq 0x80f4
leaq 0x50(%rsp), %r15
movq %r15, %rdi
movl $0x2f, %esi
xorl %edx, %edx
callq 0x75c0
leaq 0x30(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
movq $-0x1, %rcx
callq 0x74d0
leaq 0x358(%rsp), %rdi
leaq 0x10(%rsp), %rsi
leaq 0x30(%rsp), %rdx
callq 0x37020
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x113b6
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
je 0x113cd
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0xc8(%rsp), %rdi
movq 0x8(%rsp), %rax
movq 0x158(%rax), %rsi
movq 0x160(%rax), %rdx
callq 0x7510
leaq 0xb8(%rsp), %rdi
callq 0x7080
leaq 0x358(%rsp), %rsi
movq %rbx, %rdi
callq 0x36e1a
jmp 0x1141c
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
movq %r15, %rdx
callq 0x36f58
leaq 0xb8(%rsp), %rdi
callq 0x7110
leaq 0x358(%rsp), %rdi
callq 0x36d9a
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11451
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x3f8(%rsp), %rdi
callq 0x1b836
movq %rbx, %rax
addq $0x4e8, %rsp            # imm = 0x4E8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq %rax, 0x8(%rsp)
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1149a
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x1149a
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
cmpq %r12, %rdi
jne 0x1154d
jmp 0x115ae
jmp 0x115a9
movq %rax, 0x8(%rsp)
leaq 0xa8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x114df
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x114df
movq %rax, 0x8(%rsp)
leaq 0x20(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11501
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x11501
movq %rax, 0x8(%rsp)
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x11563
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x11563
jmp 0x1155e
movq %rax, 0x8(%rsp)
leaq 0x40(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11543
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x11543
movq %rax, 0x8(%rsp)
movq 0x10(%rsp), %rdi
cmpq %r13, %rdi
je 0x115ae
movq 0x20(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x115ae
jmp 0x115a9
movq %rax, 0x8(%rsp)
leaq 0x2d8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x115c2
movq 0x2d8(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x115c2
jmp 0x115bd
jmp 0x115e1
movq %rax, 0x8(%rsp)
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x115e6
movq 0x88(%rsp), %rsi
jmp 0x115d7
jmp 0x115e1
jmp 0x115e1
jmp 0x115bd
movq %rax, 0x8(%rsp)
leaq 0xb8(%rsp), %rdi
callq 0x7110
jmp 0x115e6
movq %rax, 0x8(%rsp)
movq 0xb8(%rsp), %rdi
cmpq %rbp, %rdi
je 0x115e6
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x115e6
movq %rax, 0x8(%rsp)
leaq 0x358(%rsp), %rdi
callq 0x36d9a
leaq 0x60(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11660
movq 0x60(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x11660
movq %rax, 0x8(%rsp)
jmp 0x115f3
movq %rax, 0x8(%rsp)
cmpl $0x1, %edx
jne 0x115f3
movq 0x8(%rsp), %rdi
callq 0x7210
movq %rbx, %rdi
movl $0x194, %esi            # imm = 0x194
movq %r15, %rdx
callq 0x36f58
callq 0x7780
jmp 0x11436
jmp 0x11610
movq %rax, 0x8(%rsp)
callq 0x7780
jmp 0x115f3
movq %rax, %rdi
callq 0x96aa
movq %rax, 0x8(%rsp)
leaq 0x3f8(%rsp), %rdi
callq 0x1b836
movq 0x8(%rsp), %rdi
callq 0x7810
nop