// File: /workspace/github_cpp_makefile_star2_O2/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 0xd584
movq %rcx, %r12
movq %rsi, %r13
callq 0x83bc
cmpq $0x0, 0x8(%rax)
je 0xd63f
cmpl $0x2, %r15d
jne 0xd575
movq 0x20(%r12), %rdi
callq 0x83c6
cmpq $0x0, 0x8(%rax)
jne 0xd645
movq 0x20(%r12), %rdi
callq 0x83bc
jmp 0xd660
cmpq $0x0, 0x8(%rbx)
movq (%rbx), %rax
je 0xd5a4
cmpb $0x2f, (%rax)
jne 0xd5a4
pushq $0x2e
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4760
movq (%rbx), %rax
leaq -0xe0(%rbp), %rsi
movq %rax, %rdi
callq 0x43c0
cmpl $-0x1, %eax
setne %cl
cmpl $0x2, %r15d
sete %dl
orb %cl, %dl
je 0xd7ac
cmpl $-0x1, %eax
je 0xd738
movl $0xf000, %eax           # imm = 0xF000
andl -0xc8(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0xd738
pushq $0x48
popq %rdi
callq 0x4220
movq %rax, %r14
leaq 0x4415(%rip), %rsi      # 0x11a0e
leaq -0x50(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x4ec8
movb $0x1, %r15b
leaq 0x435e(%rip), %rdx      # 0x1196e
leaq -0x50(%rbp), %rcx
movq %r14, %rdi
movl $0x193, %esi            # imm = 0x193
callq 0xc540
xorl %r15d, %r15d
leaq 0xb765(%rip), %rsi      # 0x18d90
leaq -0x109c(%rip), %rdx     # 0xc596
movq %r14, %rdi
callq 0x46b0
jmp 0xd7fe
cmpl $0x2, %r15d
jne 0xd66e
movq 0x20(%r12), %rdi
callq 0x83c6
cmpq $0x0, 0x8(%rax)
je 0xd66e
movq 0x20(%r12), %rdi
callq 0x83c6
movq %rbx, %rsi
movq %rax, %rdx
movq %r12, %rcx
callq 0xd4a8
cmpq $0x0, 0x8(%rbx)
movq (%rbx), %rax
je 0xd68e
cmpb $0x2f, (%rax)
jne 0xd68e
pushq $0x2e
popq %rdx
movq %rbx, %rdi
movq %rax, %rsi
callq 0x4760
movq (%rbx), %rax
leaq -0xe0(%rbp), %rsi
movq %rax, %rdi
callq 0x43c0
cmpl $-0x1, %eax
setne %cl
cmpl $0x2, %r15d
sete %dl
orb %cl, %dl
je 0xd758
cmpl $-0x1, %eax
je 0xd723
movl $0xf000, %eax           # imm = 0xF000
andl -0xc8(%rbp), %eax
cmpl $0x4000, %eax           # imm = 0x4000
jne 0xd723
cmpl $0x1, %r15d
ja 0xd6f4
movq 0x20(%r12), %rdi
callq 0x83f6
cmpb $0x0, (%rax)
je 0xd6f4
movq 0x20(%r12), %rdi
callq 0x83f6
cmpb $0x1, (%rax)
jne 0xd723
movb $0x1, 0x78(%r13)
jmp 0xd723
movq 0x20(%r12), %rdi
callq 0x83da
leaq -0x50(%rbp), %r13
movq %r13, %rdi
movq %rbx, %rdx
movq %rax, %rcx
callq 0xeb9a
movq %rbx, %rdi
movq %r13, %rsi
callq 0x44f0
movq %r13, %rdi
callq 0x47d8
movq 0x20(%r12), %rdi
callq 0x83d0
movl %r15d, %esi
movq %rax, %rdx
callq 0xec80
movq %r14, %rdi
movq %rbx, %rsi
callq 0x4520
movq %r14, %rax
addq $0xb8, %rsp
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x48
popq %rdi
callq 0x4220
movq %rax, %r14
leaq 0x4301(%rip), %rsi      # 0x11a6b
leaq -0x50(%rbp), %rdi
movq %rbx, %rdx
callq 0x5f8b
movb $0x1, %r15b
leaq 0x40f7(%rip), %rdx      # 0x11877
leaq -0x50(%rbp), %rcx
movq %r14, %rdi
movl $0x194, %esi            # imm = 0x194
callq 0xc540
xorl %r15d, %r15d
leaq 0xb5f5(%rip), %rsi      # 0x18d90
leaq -0x120c(%rip), %rdx     # 0xc596
movq %r14, %rdi
callq 0x46b0
jmp 0xd7fe
pushq $0x48
popq %rdi
callq 0x4220
movq %rax, %r14
leaq 0x420e(%rip), %rsi      # 0x119cc
leaq -0x50(%rbp), %rdi
movq %rbx, %rdx
callq 0x5f8b
movb $0x1, %r15b
leaq 0x40a3(%rip), %rdx      # 0x11877
leaq -0x50(%rbp), %rcx
movq %r14, %rdi
movl $0x194, %esi            # imm = 0x194
callq 0xc540
xorl %r15d, %r15d
leaq 0xb5a1(%rip), %rsi      # 0x18d90
leaq -0x1260(%rip), %rdx     # 0xc596
movq %r14, %rdi
callq 0x46b0
jmp 0xd806
jmp 0xd819
jmp 0xd806
jmp 0xd819
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x47d8
testb %r15b, %r15b
jne 0xd81c
jmp 0xd824
movq %rax, %rbx
movq %r14, %rdi
callq 0x4330
movq %rbx, %rdi
callq 0x46d0