// File: 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
movq %rsp, %rbp
subq $0x710, %rsp            # imm = 0x710
movq %rdi, -0x660(%rbp)
movq %rdi, %rax
movq %rax, -0x658(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x18(%rbp), %rsi
addq $0xb8, %rsi
leaq -0x118(%rbp), %rdi
callq 0x2e3a0
movl $0x0, -0x11c(%rbp)
movq -0x20(%rbp), %rsi
leaq -0x90(%rbp), %rdx
leaq -0x140(%rbp), %rdi
leaq -0x11c(%rbp), %rcx
callq 0x4cd00
jmp 0x1a061
movl -0x11c(%rbp), %ecx
leaq -0x170(%rbp), %rdi
leaq -0x140(%rbp), %rsi
movl $0x1, %edx
callq 0x32290
jmp 0x1a081
leaq -0x140(%rbp), %rdi
leaq -0x170(%rbp), %rsi
callq 0x97e0
leaq -0x170(%rbp), %rdi
callq 0x9c98
jmp 0x1a159
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1ab52
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
movl -0x14c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x1ab46
movq -0x148(%rbp), %rdi
callq 0x92a0
movq -0x660(%rbp), %rdi
movq %rax, -0x178(%rbp)
leaq -0x90(%rbp), %rdx
movl $0x194, %esi            # imm = 0x194
callq 0x636c0
jmp 0x1a10e
movl $0x1, -0x17c(%rbp)
callq 0x9a40
jmp 0x1a138
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
callq 0x9a40
jmp 0x1a154
jmp 0x1ab1e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1ab46
jmp 0x1ab46
leaq -0x90(%rbp), %rdx
leaq -0x220(%rbp), %rdi
movl $0xc8, %esi
callq 0x636c0
jmp 0x1a173
leaq -0x140(%rbp), %rdi
movl $0x2f, %esi
movq $-0x1, %rdx
callq 0x9c40
movq %rax, -0x678(%rbp)
movq -0x20(%rbp), %rax
movq %rax, -0x670(%rbp)
leaq -0x241(%rbp), %rdi
movq %rdi, -0x668(%rbp)
callq 0x9b10
movq -0x668(%rbp), %rdx
leaq 0x5067a(%rip), %rsi     # 0x6a838
leaq -0x240(%rbp), %rdi
callq 0xa780
jmp 0x1a1cc
movq -0x670(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x4c710
movq %rax, -0x680(%rbp)
jmp 0x1a1e8
movq -0x680(%rbp), %rdi
callq 0x14310
movq %rax, %rdi
callq 0x9480
movq %rax, -0x688(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x9c98
leaq -0x241(%rbp), %rdi
callq 0x9700
movq -0x688(%rbp), %rcx
movq -0x678(%rbp), %rax
cmpq %rcx, %rax
je 0x1a2b5
movq -0x660(%rbp), %rdi
leaq -0x90(%rbp), %rdx
movl $0x19f, %esi            # imm = 0x19F
callq 0x636c0
jmp 0x1a24c
movl $0x1, -0x17c(%rbp)
jmp 0x1ab02
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1a28d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
leaq -0x240(%rbp), %rdi
callq 0x9c98
leaq -0x241(%rbp), %rdi
callq 0x9700
jmp 0x1ab10
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1ab10
leaq -0x140(%rbp), %rdi
movl $0x2e, %esi
movq $-0x1, %rdx
callq 0x9c40
cmpq $-0x1, %rax
je 0x1a6b8
leaq -0x288(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x92c0
jmp 0x1a2ec
leaq -0x268(%rbp), %rdi
leaq -0x288(%rbp), %rsi
callq 0x32930
jmp 0x1a301
leaq -0x288(%rbp), %rdi
callq 0x9c98
movq -0x20(%rbp), %rdi
movb $0x0, -0x2a9(%rbp)
movb $0x0, -0x2f2(%rbp)
movb $0x0, -0x2f3(%rbp)
movb $0x0, -0x2f4(%rbp)
movb $0x0, -0x319(%rbp)
leaq -0x268(%rbp), %rsi
callq 0x4d430
movb %al, -0x689(%rbp)
jmp 0x1a348
movb -0x689(%rbp), %cl
movb $0x1, %al
testb $0x1, %cl
movb %al, -0x68a(%rbp)
jne 0x1a360
jmp 0x1a4a9
leaq 0x753b9(%rip), %rax     # 0x8f720
movq 0x8(%rax), %rax
movq %rax, -0x698(%rbp)
leaq -0x2a8(%rbp), %rdi
leaq -0x268(%rbp), %rsi
movl $0x1, %edx
movq $-0x1, %rcx
callq 0x96b0
jmp 0x1a393
movq -0x698(%rbp), %rdi
movb $0x1, -0x2a9(%rbp)
leaq -0x2a8(%rbp), %rsi
callq 0x261d0
movq %rax, -0x6a0(%rbp)
jmp 0x1a3b6
movq -0x6a0(%rbp), %rcx
movb $0x1, %al
cmpq $0x0, %rcx
movb %al, -0x68a(%rbp)
je 0x1a4a9
movq -0x18(%rbp), %rax
movq %rax, -0x6b0(%rbp)
leaq -0x2f1(%rbp), %rdi
movq %rdi, -0x6a8(%rbp)
callq 0x9b10
movq -0x6a8(%rbp), %rdx
movb $0x1, -0x2f2(%rbp)
leaq 0x4fd50(%rip), %rsi     # 0x6a152
leaq -0x2f0(%rbp), %rdi
callq 0xa780
jmp 0x1a410
movq -0x6b0(%rbp), %rsi
movb $0x1, -0x2f3(%rbp)
leaq -0x2d0(%rbp), %rdi
leaq -0x2f0(%rbp), %rdx
callq 0x60df0
jmp 0x1a433
movb $0x1, -0x2f4(%rbp)
leaq 0x752df(%rip), %rax     # 0x8f720
movq 0x8(%rax), %rax
movq %rax, -0x6b8(%rbp)
leaq -0x318(%rbp), %rdi
leaq -0x268(%rbp), %rsi
movl $0x1, %edx
movq $-0x1, %rcx
callq 0x96b0
jmp 0x1a46d
movq -0x6b8(%rbp), %rdi
movb $0x1, -0x319(%rbp)
leaq -0x318(%rbp), %rsi
callq 0x26470
movq %rax, -0x6c0(%rbp)
jmp 0x1a490
movq -0x6c0(%rbp), %rsi
leaq -0x2d0(%rbp), %rdi
callq 0x1ab80
movb %al, -0x68a(%rbp)
movb -0x68a(%rbp), %al
movb %al, -0x6c1(%rbp)
testb $0x1, -0x319(%rbp)
jne 0x1a4c0
jmp 0x1a4cc
leaq -0x318(%rbp), %rdi
callq 0x9c98
testb $0x1, -0x2f4(%rbp)
jne 0x1a4d7
jmp 0x1a4e3
leaq -0x2d0(%rbp), %rdi
callq 0x9c98
testb $0x1, -0x2f3(%rbp)
jne 0x1a4ee
jmp 0x1a4fa
leaq -0x2f0(%rbp), %rdi
callq 0x9c98
testb $0x1, -0x2f2(%rbp)
jne 0x1a505
jmp 0x1a511
leaq -0x2f1(%rbp), %rdi
callq 0x9700
testb $0x1, -0x2a9(%rbp)
jne 0x1a51c
jmp 0x1a528
leaq -0x2a8(%rbp), %rdi
callq 0x9c98
movb -0x6c1(%rbp), %al
testb $0x1, %al
jne 0x1a537
jmp 0x1a67f
movq -0x660(%rbp), %rdi
leaq -0x90(%rbp), %rdx
movl $0x19f, %esi            # imm = 0x19F
callq 0x636c0
jmp 0x1a551
movl $0x1, -0x17c(%rbp)
jmp 0x1a689
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
leaq -0x288(%rbp), %rdi
callq 0x9c98
jmp 0x1ab10
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1a6a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1a666
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1a64d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1a634
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1a61b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
testb $0x1, -0x319(%rbp)
jne 0x1a60d
jmp 0x1a619
leaq -0x318(%rbp), %rdi
callq 0x9c98
jmp 0x1a61b
testb $0x1, -0x2f4(%rbp)
jne 0x1a626
jmp 0x1a632
leaq -0x2d0(%rbp), %rdi
callq 0x9c98
jmp 0x1a634
testb $0x1, -0x2f3(%rbp)
jne 0x1a63f
jmp 0x1a64b
leaq -0x2f0(%rbp), %rdi
callq 0x9c98
jmp 0x1a64d
testb $0x1, -0x2f2(%rbp)
jne 0x1a658
jmp 0x1a664
leaq -0x2f1(%rbp), %rdi
callq 0x9700
jmp 0x1a666
testb $0x1, -0x2a9(%rbp)
jne 0x1a671
jmp 0x1a67d
leaq -0x2a8(%rbp), %rdi
callq 0x9c98
jmp 0x1a6a7
movl $0x0, -0x17c(%rbp)
leaq -0x268(%rbp), %rdi
callq 0x9c98
movl -0x17c(%rbp), %eax
testl %eax, %eax
jne 0x1ab02
jmp 0x1a6a5
jmp 0x1a6b8
leaq -0x268(%rbp), %rdi
callq 0x9c98
jmp 0x1ab10
leaq -0x530(%rbp), %rdi
callq 0x97a0
jmp 0x1a6c6
leaq -0x140(%rbp), %rdi
callq 0x92b0
movq %rax, %rdi
leaq -0x5c0(%rbp), %rsi
callq 0x9c10
cmpl $0x0, %eax
jge 0x1a883
leaq -0x140(%rbp), %rdi
callq 0x92b0
movq %rax, -0x6d0(%rbp)
movl $0x8, %edi
movl $0x10, %esi
callq 0x14730
movl %eax, -0x6c8(%rbp)
jmp 0x1a714
movl -0x6c8(%rbp), %edi
movl $0x20, %esi
callq 0x14730
movl %eax, -0x6d4(%rbp)
jmp 0x1a72c
movl -0x6d4(%rbp), %edx
movq -0x6d0(%rbp), %rsi
leaq -0x530(%rbp), %rdi
callq 0x9610
jmp 0x1a747
leaq -0x220(%rbp), %rdi
movl $0xc9, %esi
callq 0x64560
jmp 0x1a75a
leaq -0x5e1(%rbp), %rdi
movq %rdi, -0x6e0(%rbp)
callq 0x9b10
movq -0x6e0(%rbp), %rdx
leaq 0x4f9b6(%rip), %rsi     # 0x6a131
leaq -0x5e0(%rbp), %rdi
callq 0xa780
jmp 0x1a789
xorl %eax, %eax
movl %eax, %edx
leaq -0x140(%rbp), %rdi
movq %rdi, -0x6e8(%rbp)
movl $0x2f, %esi
callq 0x9860
movq -0x6e8(%rbp), %rsi
movq %rax, %rdx
leaq -0x608(%rbp), %rdi
movq $-0x1, %rcx
callq 0x96b0
jmp 0x1a7c4
leaq -0x220(%rbp), %rdi
leaq -0x5e0(%rbp), %rsi
leaq -0x608(%rbp), %rdx
callq 0x63770
jmp 0x1a7e0
leaq -0x608(%rbp), %rdi
callq 0x9c98
leaq -0x5e0(%rbp), %rdi
callq 0x9c98
leaq -0x5e1(%rbp), %rdi
callq 0x9700
jmp 0x1a91f
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1aaf4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1a872
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1a866
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
leaq -0x608(%rbp), %rdi
callq 0x9c98
leaq -0x5e0(%rbp), %rdi
callq 0x9c98
leaq -0x5e1(%rbp), %rdi
callq 0x9700
jmp 0x1aaf4
movl -0x5a8(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x8000, %eax           # imm = 0x8000
je 0x1a8be
movq -0x660(%rbp), %rdi
leaq -0x90(%rbp), %rdx
movl $0x1f4, %esi            # imm = 0x1F4
callq 0x636c0
jmp 0x1a8af
movl $0x1, -0x17c(%rbp)
jmp 0x1aae6
leaq -0x140(%rbp), %rdi
callq 0x92b0
movq %rax, -0x6f8(%rbp)
movl $0x8, %edi
movl $0x10, %esi
callq 0x14730
movl %eax, -0x6ec(%rbp)
jmp 0x1a8e8
movl -0x6ec(%rbp), %edi
movl $0x20, %esi
callq 0x14730
movl %eax, -0x6fc(%rbp)
jmp 0x1a900
movl -0x6fc(%rbp), %edx
movq -0x6f8(%rbp), %rsi
leaq -0x530(%rbp), %rdi
callq 0x9610
jmp 0x1a91b
jmp 0x1a91d
jmp 0x1a91f
movq -0x530(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x530(%rbp,%rax), %rdi
callq 0x9a80
movb %al, -0x6fd(%rbp)
jmp 0x1a93f
movb -0x6fd(%rbp), %al
testb $0x1, %al
jne 0x1a94b
jmp 0x1a96b
leaq -0x530(%rbp), %rdi
callq 0x9ac0
movb %al, -0x6fe(%rbp)
jmp 0x1a95f
movb -0x6fe(%rbp), %al
testb $0x1, %al
jne 0x1a994
jmp 0x1a96b
movq -0x660(%rbp), %rdi
leaq -0x90(%rbp), %rdx
movl $0x1f4, %esi            # imm = 0x1F4
callq 0x636c0
jmp 0x1a985
movl $0x1, -0x17c(%rbp)
jmp 0x1aae6
leaq -0x629(%rbp), %rdi
movq %rdi, -0x708(%rbp)
callq 0x9b10
movq -0x708(%rbp), %rdx
leaq 0x4f774(%rip), %rsi     # 0x6a129
leaq -0x628(%rbp), %rdi
callq 0xa780
jmp 0x1a9c3
xorl %eax, %eax
movl %eax, %edx
leaq -0x140(%rbp), %rdi
movq %rdi, -0x710(%rbp)
movl $0x2f, %esi
callq 0x9860
movq -0x710(%rbp), %rsi
movq %rax, %rdx
leaq -0x650(%rbp), %rdi
movq $-0x1, %rcx
callq 0x96b0
jmp 0x1a9fe
leaq -0x220(%rbp), %rdi
leaq -0x628(%rbp), %rsi
leaq -0x650(%rbp), %rdx
callq 0x63770
jmp 0x1aa1a
leaq -0x650(%rbp), %rdi
callq 0x9c98
leaq -0x628(%rbp), %rdi
callq 0x9c98
leaq -0x629(%rbp), %rdi
callq 0x9700
leaq -0x520(%rbp), %rdi
movq -0x18(%rbp), %rsi
addq $0x158, %rsi            # imm = 0x158
callq 0x95d0
jmp 0x1aa57
leaq -0x530(%rbp), %rdi
callq 0x90a0
jmp 0x1aa65
movq -0x660(%rbp), %rdi
leaq -0x220(%rbp), %rsi
callq 0x63570
jmp 0x1aa7a
movl $0x1, -0x17c(%rbp)
jmp 0x1aae6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1aad8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
jmp 0x1aacc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x148(%rbp)
movl %eax, -0x14c(%rbp)
leaq -0x650(%rbp), %rdi
callq 0x9c98
leaq -0x628(%rbp), %rdi
callq 0x9c98
leaq -0x629(%rbp), %rdi
callq 0x9700
jmp 0x1aaf4
leaq -0x530(%rbp), %rdi
callq 0x9170
jmp 0x1ab02
leaq -0x530(%rbp), %rdi
callq 0x9170
jmp 0x1ab10
leaq -0x220(%rbp), %rdi
callq 0x634e0
jmp 0x1ab1e
leaq -0x220(%rbp), %rdi
callq 0x634e0
jmp 0x1ab46
leaq -0x140(%rbp), %rdi
callq 0x9c98
leaq -0x118(%rbp), %rdi
callq 0x2efd0
movq -0x658(%rbp), %rax
addq $0x710, %rsp            # imm = 0x710
popq %rbp
retq
leaq -0x140(%rbp), %rdi
callq 0x9c98
leaq -0x118(%rbp), %rdi
callq 0x2efd0
movq -0x148(%rbp), %rdi
callq 0x9af0
movq %rax, %rdi
callq 0xaa90
nopw %cs:(%rax,%rax)
nopl (%rax)