// File: /workspace/github_cpp_makefile_star2_O2/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 $0x4d8, %rsp            # imm = 0x4D8
movq %rcx, %r12
movq %rdx, %r14
movq %rdi, %rbx
leaq 0xb8(%rdx), %rsi
leaq 0x3e8(%rsp), %rdi
callq 0x1625a
leaq 0x4(%rsp), %rcx
andl $0x0, (%rcx)
leaq 0x470(%rsp), %r15
leaq 0x8(%rsp), %rdi
movq %r12, %rsi
movq %r15, %rdx
callq 0x21a22
movl 0x4(%rsp), %ecx
leaq 0x1d8(%rsp), %rdi
leaq 0x8(%rsp), %rsi
pushq $0x1
popq %rdx
callq 0x17b31
leaq 0x8(%rsp), %rdi
leaq 0x1d8(%rsp), %r13
movq %r13, %rsi
callq 0x75f0
movq %r13, %rdi
callq 0x79b8
leaq 0x138(%rsp), %rdi
movl $0xc8, %esi
movq %r15, %rdx
callq 0x2a080
leaq 0x8(%rsp), %rdi
pushq $0x2f
popq %rsi
pushq $-0x1
popq %rdx
callq 0x7970
movq %rax, %r13
leaq 0x1e5f7(%rip), %rsi     # 0x2c828
leaq 0x1d8(%rsp), %rdi
leaq 0xa8(%rsp), %rdx
callq 0x8032
leaq 0x1d8(%rsp), %rsi
movq %r12, %rdi
callq 0x2167a
movq (%rax), %rax
movq 0x18(%rax), %rbp
leaq 0x1d8(%rsp), %rdi
callq 0x79b8
cmpq %rbp, %r13
jne 0xe40e
leaq 0x8(%rsp), %rdi
pushq $0x2e
popq %rsi
pushq $-0x1
popq %rdx
callq 0x7970
cmpq $-0x1, %rax
je 0xe3ba
leaq 0x88(%rsp), %rdi
leaq 0x8(%rsp), %rsi
callq 0x7690
leaq 0x1d8(%rsp), %rdi
leaq 0x88(%rsp), %rsi
callq 0x17e0b
leaq 0x88(%rsp), %rdi
callq 0x79b8
leaq 0x1d8(%rsp), %rsi
movq %r12, %rdi
callq 0x21d4e
testb %al, %al
je 0xe5a7
leaq 0x352d0(%rip), %r13     # 0x435b0
movq 0x8(%r13), %r12
leaq 0xa8(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
pushq $0x1
popq %rdx
pushq $-0x1
popq %rcx
callq 0x7540
leaq 0xa8(%rsp), %rsi
movq %r12, %rdi
callq 0x13334
testq %rax, %rax
je 0xe59a
leaq 0x1de33(%rip), %rsi     # 0x2c152
leaq 0x28(%rsp), %rdi
leaq 0x3(%rsp), %rdx
callq 0x8032
leaq 0x48(%rsp), %rdi
leaq 0x28(%rsp), %rdx
movq %r14, %rsi
callq 0x292c2
movq 0x8(%r13), %r12
leaq 0x68(%rsp), %rdi
leaq 0x1d8(%rsp), %rsi
pushq $0x1
popq %rdx
pushq $-0x1
popq %rcx
callq 0x7540
leaq 0x68(%rsp), %rsi
movq %r12, %rdi
callq 0x134a0
leaq 0x48(%rsp), %r12
movq %r12, %rdi
movq %rax, %rsi
callq 0xe716
movl %eax, %ebp
leaq 0x68(%rsp), %rdi
callq 0x79b8
movq %r12, %rdi
callq 0x79b8
leaq 0x28(%rsp), %rdi
callq 0x79b8
leaq 0xa8(%rsp), %rdi
callq 0x79b8
testb %bpl, %bpl
jne 0xe5a7
leaq 0x1d8(%rsp), %rdi
callq 0x79b8
leaq 0x1d8(%rsp), %rdi
callq 0x75b0
movq 0x8(%rsp), %rdi
leaq 0xa8(%rsp), %rsi
callq 0x7940
testl %eax, %eax
js 0xe423
movl $0xf000, %eax           # imm = 0xF000
andl 0xc0(%rsp), %eax
cmpl $0x8000, %eax           # imm = 0x8000
jne 0xe57b
movq 0x8(%rsp), %rsi
leaq 0x1d8(%rsp), %rdi
pushq $0x38
popq %rdx
callq 0x74d0
jmp 0xe4b0
movq %rbx, %rdi
movl $0x19f, %esi            # imm = 0x19F
movq %r15, %rdx
callq 0x2a080
jmp 0xe5c4
movq 0x8(%rsp), %rsi
leaq 0x1d8(%rsp), %rdi
pushq $0x38
popq %rdx
callq 0x74d0
leaq 0x138(%rsp), %rdi
movl $0xc9, %esi
callq 0x2a818
leaq 0x1dce0(%rip), %rsi     # 0x2c131
leaq 0x48(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x8032
leaq 0x8(%rsp), %r12
pushq $0x2f
popq %rsi
movq %r12, %rdi
xorl %edx, %edx
callq 0x7650
leaq 0x28(%rsp), %rdi
pushq $-0x1
popq %rcx
movq %r12, %rsi
movq %rax, %rdx
callq 0x7540
leaq 0x138(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x2a124
leaq 0x28(%rsp), %rdi
callq 0x79b8
leaq 0x48(%rsp), %rdi
callq 0x79b8
movq 0x1d8(%rsp), %rax
movq -0x18(%rax), %rax
cmpl $0x0, 0x1f8(%rsp,%rax)
jne 0xe57b
leaq 0x258(%rsp), %rdi
callq 0x7130
testb %al, %al
je 0xe57b
leaq 0x1dc43(%rip), %rsi     # 0x2c129
leaq 0x48(%rsp), %rdi
leaq 0x68(%rsp), %rdx
callq 0x8032
leaq 0x8(%rsp), %r15
pushq $0x2f
popq %rsi
movq %r15, %rdi
xorl %edx, %edx
callq 0x7650
leaq 0x28(%rsp), %rdi
pushq $-0x1
popq %rcx
movq %r15, %rsi
movq %rax, %rdx
callq 0x7540
leaq 0x138(%rsp), %rdi
leaq 0x48(%rsp), %rsi
leaq 0x28(%rsp), %rdx
callq 0x2a124
leaq 0x28(%rsp), %rdi
callq 0x79b8
leaq 0x48(%rsp), %rdi
callq 0x79b8
leaq 0x1e8(%rsp), %rdi
addq $0x158, %r14            # imm = 0x158
movq %r14, %rsi
callq 0x74a0
leaq 0x1d8(%rsp), %rdi
callq 0x7070
leaq 0x138(%rsp), %rsi
movq %rbx, %rdi
callq 0x29f62
jmp 0xe58b
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
movq %r15, %rdx
callq 0x2a080
leaq 0x1d8(%rsp), %rdi
callq 0x7110
jmp 0xe5c4
leaq 0xa8(%rsp), %rdi
callq 0x79b8
movq %rbx, %rdi
movl $0x19f, %esi            # imm = 0x19F
movq %r15, %rdx
callq 0x2a080
leaq 0x1d8(%rsp), %rdi
callq 0x79b8
leaq 0x138(%rsp), %rdi
callq 0x29f10
leaq 0x8(%rsp), %rdi
callq 0x79b8
leaq 0x3e8(%rsp), %rdi
callq 0x169e4
movq %rbx, %rax
addq $0x4d8, %rsp            # imm = 0x4D8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xe603
jmp 0xe612
jmp 0xe679
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x79b8
jmp 0xe615
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x79b8
jmp 0xe67c
jmp 0xe679
movq %rax, %r14
leaq 0x68(%rsp), %rdi
callq 0x79b8
jmp 0xe635
movq %rax, %r14
leaq 0x48(%rsp), %rdi
callq 0x79b8
jmp 0xe644
movq %rax, %r14
leaq 0x28(%rsp), %rdi
callq 0x79b8
jmp 0xe655
jmp 0xe652
movq %rax, %r14
leaq 0xa8(%rsp), %rdi
callq 0x79b8
jmp 0xe690
jmp 0xe68d
jmp 0xe69f
movq %rax, %r14
leaq 0x88(%rsp), %rdi
jmp 0xe698
jmp 0xe69f
jmp 0xe69f
movq %rax, %r14
leaq 0x1d8(%rsp), %rdi
callq 0x7110
jmp 0xe6a2
jmp 0xe68d
movq %rax, %r14
leaq 0x1d8(%rsp), %rdi
callq 0x79b8
jmp 0xe6a2
movq %rax, %r14
leaq 0x138(%rsp), %rdi
callq 0x29f10
leaq 0x8(%rsp), %rdi
callq 0x79b8
leaq 0x3e8(%rsp), %rdi
callq 0x169e4
movq %r14, %rdi
callq 0x7860
movq %rax, %r14
jmp 0xe6af
movq %rax, %r14
cmpl $0x1, %edx
jne 0xe6af
movq %r14, %rdi
callq 0x7220
movq %rbx, %rdi
movl $0x194, %esi            # imm = 0x194
movq %r15, %rdx
callq 0x2a080
callq 0x77f0
jmp 0xe5d1
jmp 0xe6ce
movq %rax, %r14
callq 0x77f0
jmp 0xe6af
movq %rax, %rdi
callq 0x8f59
movq %rax, %r14
jmp 0xe6b9