// File: /workspace/github_cpp_makefile_star2_O1/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 0xf6c9
movq %rcx, %r12
movq %rsi, %r13
callq 0x8c28
cmpq $0x0, 0x8(%rax)
je 0xf69b
cmpl $0x2, %r15d
jne 0xf798
movq 0x20(%r12), %rdi
callq 0x8c32
cmpq $0x0, 0x8(%rax)
je 0xf798
cmpl $0x2, %r15d
jne 0xf7b0
movq 0x20(%r12), %rdi
callq 0x8c32
cmpq $0x0, 0x8(%rax)
je 0xf7b0
movq 0x20(%r12), %rdi
callq 0x8c32
jmp 0xf7a2
cmpq $0x0, 0x8(%rbx)
je 0xf6ef
movq (%rbx), %rax
cmpb $0x2f, (%rax)
jne 0xf6ef
movl $0x1, %ecx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
movl $0x2e, %r8d
callq 0x4750
movq (%rbx), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x4360
cmpl $-0x1, %eax
setne %cl
cmpl $0x2, %r15d
sete %dl
orb %cl, %dl
je 0xf95d
cmpl $-0x1, %eax
je 0xf8b6
movl $0xf000, %eax           # imm = 0xF000
andl -0xc8(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0xf8b6
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq -0x38(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x6311(%rip), %rsi      # 0x15a60
leaq 0x6366(%rip), %rdx      # 0x15abc
leaq -0x48(%rbp), %rdi
callq 0x5790
movb $0x1, %r15b
leaq 0x6257(%rip), %rdx      # 0x159c0
leaq -0x48(%rbp), %rcx
movq %r14, %rdi
movl $0x193, %esi            # imm = 0x193
callq 0xe106
xorl %r15d, %r15d
leaq 0xd5ec(%rip), %rsi      # 0x1cd70
leaq -0x1617(%rip), %rdx     # 0xe174
movq %r14, %rdi
callq 0x46a0
jmp 0xf9b1
movq 0x20(%r12), %rdi
callq 0x8c28
movq %rbx, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0xf5ac
cmpq $0x0, 0x8(%rbx)
je 0xf7d6
movq (%rbx), %rax
cmpb $0x2f, (%rax)
jne 0xf7d6
movl $0x1, %ecx
movq %rbx, %rdi
xorl %esi, %esi
xorl %edx, %edx
movl $0x2e, %r8d
callq 0x4750
movq (%rbx), %rdi
leaq -0xe0(%rbp), %rsi
callq 0x4360
cmpl $-0x1, %eax
setne %cl
cmpl $0x2, %r15d
sete %dl
orb %cl, %dl
je 0xf907
cmpl $-0x1, %eax
je 0xf8a1
movq %r13, -0x50(%rbp)
movl $0xf000, %r13d          # imm = 0xF000
andl -0xc8(%rbp), %r13d
cmpl $0x4000, %r13d          # imm = 0x4000
jne 0xf832
cmpl $0x1, %r15d
ja 0xf861
movq 0x20(%r12), %rdi
callq 0x8c62
cmpb $0x0, (%rax)
je 0xf861
cmpl $0x4000, %r13d          # imm = 0x4000
setne %al
cmpl $0x2, %r15d
setae %cl
orb %al, %cl
movq -0x50(%rbp), %r13
jne 0xf8a1
movq 0x20(%r12), %rdi
callq 0x8c62
cmpb $0x1, (%rax)
jne 0xf8a1
movb $0x1, 0x78(%r13)
jmp 0xf8a1
movq 0x20(%r12), %rdi
callq 0x8c46
leaq -0x48(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rdx
movq %rax, %rcx
callq 0x12072
movq %rbx, %rdi
movq %r13, %rsi
callq 0x44d0
movq (%r13), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xf8a1
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
movq 0x20(%r12), %rdi
callq 0x8c3c
movl %r15d, %esi
movq %rax, %rdx
callq 0x12186
leaq 0x10(%r14), %rcx
movq %rcx, (%r14)
movq (%rbx), %rdx
leaq 0x10(%rbx), %rax
cmpq %rax, %rdx
je 0xf8d5
movq %rdx, (%r14)
movq (%rax), %rcx
movq %rcx, 0x10(%r14)
jmp 0xf8db
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 0x61a2(%rip), %rsi      # 0x15abd
leaq -0x48(%rbp), %rdi
movq %rbx, %rdx
callq 0x6400
movb $0x1, %r15b
leaq 0x5f98(%rip), %rdx      # 0x158c9
leaq -0x48(%rbp), %rcx
movq %r14, %rdi
movl $0x194, %esi            # imm = 0x194
callq 0xe106
xorl %r15d, %r15d
leaq 0xd424(%rip), %rsi      # 0x1cd70
leaq -0x17df(%rip), %rdx     # 0xe174
movq %r14, %rdi
callq 0x46a0
jmp 0xf9b1
movl $0x48, %edi
callq 0x41f0
movq %rax, %r14
leaq 0x60ad(%rip), %rsi      # 0x15a1e
leaq -0x48(%rbp), %rdi
movq %rbx, %rdx
callq 0x6400
movb $0x1, %r15b
leaq 0x5f42(%rip), %rdx      # 0x158c9
leaq -0x48(%rbp), %rcx
movq %r14, %rdi
movl $0x194, %esi            # imm = 0x194
callq 0xe106
xorl %r15d, %r15d
leaq 0xd3ce(%rip), %rsi      # 0x1cd70
leaq -0x1835(%rip), %rdx     # 0xe174
movq %r14, %rdi
callq 0x46a0
movq %rax, %rbx
movq -0x48(%rbp), %rdi
cmpq %r12, %rdi
jne 0xf9d5
jmp 0xf9e9
jmp 0xf9e3
jmp 0xf9c5
jmp 0xf9e3
movq %rax, %rbx
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xf9e9
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xf9e9
movq %rax, %rbx
movb $0x1, %r15b
testb %r15b, %r15b
je 0xf9f6
movq %r14, %rdi
callq 0x42e0
movq %rbx, %rdi
callq 0x46d0