// File: /workspace/github_cpp_makefile_star2_O3/llefranc[P]42_webserv/srcs/server/Response.cpp
// Function: Response::reconstructFullURI(int, std::pair<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const, Location const*> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>>)
std::string Response::reconstructFullURI(int method,
		const std::pair<const std::string, const Location*>& loc, std::string uri)
{
	bool fileExist = true;
	struct stat infFile;


	// zero location block match the URI so the URI isn't modified
	if (!loc.second)
	{
        // Need to add a '.' for relative path access
        if (!uri.empty() && uri[0] == '/')
            uri.insert(uri.begin(), '.');
    
		// Checking if the file exist or if it's a directory. Case POST method, no 404 because it can create the file.
		if (stat(uri.c_str(), &infFile) == -1 && !(fileExist = false) && method != POST)
			throw StatusLine(404, REASON_404, "case no match with location block in reconstructlFullURI method: " + uri);
		if (fileExist && S_ISDIR(infFile.st_mode))
			throw StatusLine(403, REASON_403, "trying to access a directory case no match with"
					" location block in reconstructlFullURI method");

		return uri;
	}

	// Replacing the part of the URI that matched with the root path if there is one existing
	if (!loc.second->getRoot().empty() && !(method == POST && !loc.second->getUploadPath().empty()))
		replaceLocInUri(&uri, loc.second->getRoot(), loc.first);

	else if (method == POST && !loc.second->getUploadPath().empty())
		replaceLocInUri(&uri, loc.second->getUploadPath(), loc.first);
	
	// If no root in location block, or root doesn't start with a '.', need to add it to find the file using
	// relative path
	if (!uri.empty() && uri[0] == '/')
		uri.insert(uri.begin(), '.');

	// Checking if the path after root substitution is correct, and if it's a directory trying
	// to add indexs. Case POST method, no 404 because it can create the file.
	if (stat(uri.c_str(), &infFile) == -1 && !(fileExist = false) && method != POST)
		throw StatusLine(404, REASON_404, "reconstructFullURI method: " + uri);

    // Case we match a directory and an autoindex isn't set. We try all the possible indexs, if none
    // works addIndex throw a 403 error StatusLine object
	if (fileExist && S_ISDIR(infFile.st_mode) && !((method == GET || method == HEAD) && loc.second->getAutoIndex()))
		uri = addIndex(uri, loc.second->getIndex());

    else if (fileExist && S_ISDIR(infFile.st_mode) && ((method == GET || method == HEAD) && loc.second->getAutoIndex()))
    {
	    _autoIndex = true;
    }

	checkMethods(method, loc.second->getMethods());

	return uri;
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0xb8, %rsp
movq %r8, %rbx
movl %edx, %r15d
movq %rdi, %r14
movq 0x20(%rcx), %rdi
testq %rdi, %rdi
je 0xf666
movq %rcx, %r12
movq %rsi, %r13
callq 0x8c58
cmpq $0x0, 0x8(%rax)
je 0xf734
cmpl $0x2, %r15d
jne 0xf657
movq 0x20(%r12), %rdi
callq 0x8c62
cmpq $0x0, 0x8(%rax)
jne 0xf73a
movq 0x20(%r12), %rdi
callq 0x8c58
jmp 0xf755
cmpq $0x0, 0x8(%rbx)
movq (%rbx), %rdi
je 0xf68f
cmpb $0x2f, (%rdi)
jne 0xf68f
movl $0x1, %ecx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
movl $0x2e, %r8d
callq 0x4750
movq (%rbx), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x4360
cmpl $-0x1, %eax
setne %cl
cmpl $0x2, %r15d
sete %dl
orb %cl, %dl
je 0xf8eb
cmpl $-0x1, %eax
je 0xf844
movl $0xf000, %eax           # imm = 0xF000
andl -0xc0(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0xf844
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq -0x38(%rbp), %r15
movq %r15, -0x10(%r15)
leaq 0x6375(%rip), %rsi      # 0x15a60
leaq 0x63ca(%rip), %rdx      # 0x15abc
leaq -0x48(%rbp), %rdi
callq 0x5796
movb $0x1, %r12b
leaq 0x62bb(%rip), %rdx      # 0x159c0
leaq -0x48(%rbp), %rcx
movq %r14, %rdi
movl $0x193, %esi            # imm = 0x193
callq 0xe0d4
xorl %r12d, %r12d
leaq 0xd650(%rip), %rsi      # 0x1cd70
leaq -0x15e5(%rip), %rdx     # 0xe142
movq %r14, %rdi
callq 0x46a0
jmp 0xf93f
cmpl $0x2, %r15d
jne 0xf763
movq 0x20(%r12), %rdi
callq 0x8c62
cmpq $0x0, 0x8(%rax)
je 0xf763
movq 0x20(%r12), %rdi
callq 0x8c62
movq %rbx, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0xf55a
cmpq $0x0, 0x8(%rbx)
movq (%rbx), %rdi
je 0xf78c
cmpb $0x2f, (%rdi)
jne 0xf78c
movl $0x1, %ecx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
movl $0x2e, %r8d
callq 0x4750
movq (%rbx), %rdi
leaq -0xd8(%rbp), %rsi
callq 0x4360
cmpl $-0x1, %eax
setne %cl
cmpl $0x2, %r15d
sete %dl
orb %cl, %dl
je 0xf895
cmpl $-0x1, %eax
je 0xf82f
movl $0xf000, %eax           # imm = 0xF000
andl -0xc0(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0xf82f
cmpl $0x1, %r15d
ja 0xf7ef
movq 0x20(%r12), %rdi
callq 0x8c92
cmpb $0x0, (%rax)
je 0xf7ef
movq 0x20(%r12), %rdi
callq 0x8c92
cmpb $0x1, (%rax)
jne 0xf82f
movb $0x1, 0x78(%r13)
jmp 0xf82f
movq 0x20(%r12), %rdi
callq 0x8c76
leaq -0x48(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rdx
movq %rax, %rcx
callq 0x11fde
movq %rbx, %rdi
movq %r13, %rsi
callq 0x44d0
movq (%r13), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xf82f
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
movq 0x20(%r12), %rdi
callq 0x8c6c
movl %r15d, %esi
movq %rax, %rdx
callq 0x120ee
leaq 0x10(%r14), %rcx
movq %rcx, (%r14)
movq (%rbx), %rdx
leaq 0x10(%rbx), %rax
cmpq %rax, %rdx
je 0xf863
movq %rdx, (%r14)
movq (%rax), %rcx
movq %rcx, 0x10(%r14)
jmp 0xf869
movups (%rax), %xmm0
movups %xmm0, (%rcx)
movq 0x8(%rbx), %rcx
movq %rcx, 0x8(%r14)
movq %rax, (%rbx)
movq $0x0, 0x8(%rbx)
movb $0x0, 0x10(%rbx)
movq %r14, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq 0x6214(%rip), %rsi      # 0x15abd
leaq -0x48(%rbp), %rdi
movq %rbx, %rdx
callq 0x643f
movb $0x1, %r15b
leaq 0x600a(%rip), %rdx      # 0x158c9
leaq -0x48(%rbp), %rcx
movq %r14, %rdi
movl $0x194, %esi            # imm = 0x194
callq 0xe0d4
xorl %r15d, %r15d
leaq 0xd496(%rip), %rsi      # 0x1cd70
leaq -0x179f(%rip), %rdx     # 0xe142
movq %r14, %rdi
callq 0x46a0
jmp 0xf93f
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq 0x611f(%rip), %rsi      # 0x15a1e
leaq -0x48(%rbp), %rdi
movq %rbx, %rdx
callq 0x643f
movb $0x1, %r15b
leaq 0x5fb4(%rip), %rdx      # 0x158c9
leaq -0x48(%rbp), %rcx
movq %r14, %rdi
movl $0x194, %esi            # imm = 0x194
callq 0xe0d4
xorl %r15d, %r15d
leaq 0xd440(%rip), %rsi      # 0x1cd70
leaq -0x17f5(%rip), %rdx     # 0xe142
movq %r14, %rdi
callq 0x46a0
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r15, %rdi
je 0xf957
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
testb %r12b, %r12b
jne 0xf98a
jmp 0xf992
jmp 0xf987
jmp 0xf964
jmp 0xf987
movq %rax, %rbx
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xf980
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
testb %r15b, %r15b
jne 0xf98a
jmp 0xf992
movq %rax, %rbx
movq %r14, %rdi
callq 0x42e0
movq %rbx, %rdi
callq 0x46d0