// File: /workspace/github_cpp_makefile_star2_O1/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 $0x548, %rsp            # imm = 0x548
movq %rcx, %r12
movq %rdi, %rbx
movq %rdx, 0x50(%rsp)
leaq 0xb8(%rdx), %rsi
leaq 0x3c8(%rsp), %rdi
callq 0x1b1b6
leaq 0x2c(%rsp), %rcx
movl $0x0, (%rcx)
leaq 0x450(%rsp), %r15
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x2bd04
movl 0x2c(%rsp), %ecx
leaq 0x118(%rsp), %rdi
leaq 0x8(%rsp), %rsi
movl $0x1, %edx
callq 0x1d582
leaq 0x8(%rsp), %rdi
leaq 0x118(%rsp), %r13
movq %r13, %rsi
callq 0x7590
movq (%r13), %rdi
leaq 0x128(%rsp), %r14
cmpq %r14, %rdi
je 0x10f9c
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x328(%rsp), %rdi
movl $0xc8, %esi
movq %r15, %rdx
callq 0x37384
leaq 0x8(%rsp), %rdi
movl $0x2f, %esi
movq $-0x1, %rdx
callq 0x7900
movq %rax, %r13
leaq 0x118(%rsp), %rdi
movq %r14, (%rdi)
leaq 0x29898(%rip), %rsi     # 0x3a874
leaq 0x29895(%rip), %rdx     # 0x3a878
callq 0x80f4
leaq 0x118(%rsp), %rsi
movq %r12, %rdi
callq 0x2b4c6
movq (%rax), %rax
movq 0x18(%rax), %rbp
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x1101c
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x74b0
cmpq %rbp, %r13
jne 0x111ac
leaq 0x8(%rsp), %rdi
movl $0x2e, %esi
movq $-0x1, %rdx
callq 0x7900
cmpq $-0x1, %rax
je 0x112ab
leaq 0xc8(%rsp), %r13
movq %r13, -0x10(%r13)
movq 0x8(%rsp), %rsi
movq 0x10(%rsp), %rdx
addq %rsi, %rdx
leaq 0xb8(%rsp), %rdi
callq 0x8a8a
leaq 0x118(%rsp), %rdi
leaq 0xb8(%rsp), %rsi
callq 0x1d914
movq 0xb8(%rsp), %rdi
cmpq %r13, %rdi
je 0x1109d
movq 0xc8(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x118(%rsp), %rsi
movq %r12, %rdi
callq 0x2c1cc
movl %eax, %ebp
movb $0x1, %r13b
testb %al, %al
je 0x111c1
leaq 0x3d4a7(%rip), %rax     # 0x4e568
movq 0x8(%rax), %r12
leaq 0xf8(%rsp), %rdi
leaq 0x118(%rsp), %rsi
movl $0x1, %edx
movq $-0x1, %rcx
callq 0x74d0
leaq 0xf8(%rsp), %rsi
movq %r12, %rdi
callq 0x180c8
testq %rax, %rax
je 0x111c1
leaq 0x88(%rsp), %r13
movq %r13, -0x10(%r13)
leaq 0x29040(%rip), %rsi     # 0x3a152
leaq 0x29045(%rip), %rdx     # 0x3a15e
leaq 0x78(%rsp), %rdi
callq 0x80f4
leaq 0x98(%rsp), %rdi
leaq 0x78(%rsp), %rdx
movq 0x50(%rsp), %rsi
callq 0x35fd4
leaq 0x3d427(%rip), %rax     # 0x4e568
movq 0x8(%rax), %r12
leaq 0xd8(%rsp), %rdi
leaq 0x118(%rsp), %rsi
movl $0x1, %edx
movq $-0x1, %rcx
callq 0x74d0
leaq 0xd8(%rsp), %rsi
movq %r12, %rdi
callq 0x18302
movq 0xa0(%rsp), %rdx
movb $0x1, %r12b
cmpq 0x8(%rax), %rdx
jne 0x11529
testq %rdx, %rdx
je 0x11531
movq (%rax), %rsi
movq 0x98(%rsp), %rdi
callq 0x74c0
testl %eax, %eax
setne %r13b
jmp 0x111c4
movq %rbx, %rdi
movl $0x19f, %esi            # imm = 0x19F
movq %r15, %rdx
callq 0x37384
jmp 0x114df
xorl %r12d, %r12d
testb %r12b, %r12b
je 0x1122c
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x111ea
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0xa8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1120b
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x88(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1122c
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x74b0
testb %bpl, %bpl
je 0x11252
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11252
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x74b0
testb %r13b, %r13b
je 0x1128e
movq %rbx, %rdi
movl $0x19f, %esi            # imm = 0x19F
movq %r15, %rdx
callq 0x37384
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x11284
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x74b0
testb %r13b, %r13b
je 0x112ab
jmp 0x114df
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x112ab
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x118(%rsp), %rdi
callq 0x7550
movq 0x8(%rsp), %rdi
leaq 0x4b8(%rsp), %rsi
callq 0x78e0
testl %eax, %eax
js 0x11301
movl $0xf000, %eax           # imm = 0xF000
andl 0x4d0(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0x114c2
movq 0x8(%rsp), %rsi
leaq 0x118(%rsp), %rdi
movl $0x38, %edx
callq 0x7450
jmp 0x113bf
movq 0x8(%rsp), %rsi
leaq 0x118(%rsp), %rdi
movl $0x38, %edx
callq 0x7450
leaq 0x328(%rsp), %rdi
movl $0xc9, %esi
callq 0x37f1c
leaq 0x40(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x28df7(%rip), %rsi     # 0x3a131
leaq 0x28df8(%rip), %rdx     # 0x3a139
leaq 0x30(%rsp), %rdi
callq 0x80f4
leaq 0x8(%rsp), %r12
movq %r12, %rdi
movl $0x2f, %esi
xorl %edx, %edx
callq 0x75c0
leaq 0x58(%rsp), %rdi
movq %r12, %rsi
movq %rax, %rdx
movq $-0x1, %rcx
callq 0x74d0
leaq 0x328(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0x3744c
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x113a8
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x113bf
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x118(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x138(%rsp,%rax)
jne 0x114c2
leaq 0x198(%rsp), %rdi
callq 0x7130
testb %al, %al
je 0x114c2
leaq 0x40(%rsp), %r14
movq %r14, -0x10(%r14)
leaq 0x28d2b(%rip), %rsi     # 0x3a129
leaq 0x28d34(%rip), %rdx     # 0x3a139
leaq 0x30(%rsp), %rdi
callq 0x80f4
leaq 0x8(%rsp), %r15
movq %r15, %rdi
movl $0x2f, %esi
xorl %edx, %edx
callq 0x75c0
leaq 0x58(%rsp), %rdi
movq %r15, %rsi
movq %rax, %rdx
movq $-0x1, %rcx
callq 0x74d0
leaq 0x328(%rsp), %rdi
leaq 0x30(%rsp), %rsi
leaq 0x58(%rsp), %rdx
callq 0x3744c
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x1146c
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x74b0
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x11483
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x128(%rsp), %rdi
movq 0x50(%rsp), %rax
movq 0x158(%rax), %rsi
movq 0x160(%rax), %rdx
callq 0x7510
leaq 0x118(%rsp), %rdi
callq 0x7080
leaq 0x328(%rsp), %rsi
movq %rbx, %rdi
callq 0x37246
jmp 0x114d2
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
movq %r15, %rdx
callq 0x37384
leaq 0x118(%rsp), %rdi
callq 0x7110
leaq 0x328(%rsp), %rdi
callq 0x371c6
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11507
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
leaq 0x3c8(%rsp), %rdi
callq 0x1bb28
movq %rbx, %rax
addq $0x548, %rsp            # imm = 0x548
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movb $0x1, %r13b
jmp 0x111c4
xorl %r13d, %r13d
jmp 0x111c4
jmp 0x115e1
jmp 0x11601
jmp 0x11641
movq %rax, %r12
leaq 0xe8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11571
movq 0xe8(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x11571
movq %rax, %r12
leaq 0xa8(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11597
movq 0xa8(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x11597
movq %rax, %r12
movq 0x78(%rsp), %rdi
cmpq %r13, %rdi
je 0x115b8
movq 0x88(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x115b8
jmp 0x115b5
movq %rax, %r12
leaq 0x108(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11656
movq 0x108(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x11656
jmp 0x11653
movq %rax, %r12
leaq 0x68(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x11604
movq 0x68(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x11604
movq %rax, %r12
movq 0x30(%rsp), %rdi
cmpq %r14, %rdi
je 0x11644
movq 0x40(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x11644
jmp 0x11641
movq %rax, %r12
movq 0xb8(%rsp), %rdi
cmpq %r13, %rdi
je 0x11678
movq 0xc8(%rsp), %rsi
jmp 0x1166b
jmp 0x11675
jmp 0x11675
jmp 0x11653
jmp 0x11675
movq %rax, %r12
leaq 0x118(%rsp), %rdi
callq 0x7110
jmp 0x11678
movq %rax, %r12
movq 0x118(%rsp), %rdi
cmpq %r14, %rdi
je 0x11678
movq 0x128(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x11678
movq %rax, %r12
leaq 0x328(%rsp), %rdi
callq 0x371c6
leaq 0x18(%rsp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x116e8
movq 0x18(%rsp), %rsi
incq %rsi
callq 0x74b0
jmp 0x116e8
movq %rax, %r12
jmp 0x11685
movq %rax, %r12
cmpl $0x1, %edx
jne 0x11685
movq %r12, %rdi
callq 0x7210
movq %rbx, %rdi
movl $0x194, %esi            # imm = 0x194
movq %r15, %rdx
callq 0x37384
callq 0x7780
jmp 0x114ec
jmp 0x116a2
movq %rax, %r12
callq 0x7780
jmp 0x11685
movq %rax, %rdi
callq 0x96cc
movq %rax, %r12
leaq 0x3c8(%rsp), %rdi
callq 0x1bb28
movq %r12, %rdi
callq 0x7810
nopl (%rax)