// File: 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
subq $0x1e0, %rsp            # imm = 0x1E0
movq %r8, -0x1b0(%rbp)
movq %rdi, -0x1a8(%rbp)
movq %rdi, %rax
movq %rax, -0x1a0(%rbp)
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movl %edx, -0x14(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x10(%rbp), %rax
movq %rax, -0x198(%rbp)
movb $0x1, -0x29(%rbp)
movq -0x20(%rbp), %rax
cmpq $0x0, 0x20(%rax)
jne 0x1c026
movq -0x1b0(%rbp), %rdi
callq 0x5870
testb $0x1, %al
jne 0x1be01
movq -0x1b0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x59f0
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x1be01
movq -0x1b0(%rbp), %rdi
callq 0x5480
movq %rax, -0xd0(%rbp)
leaq -0xc8(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
callq 0x125e0
movq -0x1b0(%rbp), %rdi
movq -0xc8(%rbp), %rsi
movl $0x2e, %edx
callq 0x5980
movq %rax, -0xd8(%rbp)
movq -0x1b0(%rbp), %rdi
callq 0x5240
movq %rax, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x54c0
cmpl $-0x1, %eax
jne 0x1bf06
movb $0x0, -0x29(%rbp)
xorl %eax, %eax
testb $0x1, %al
jne 0x1bf06
cmpl $0x2, -0x14(%rbp)
je 0x1bf06
movb $0x1, -0x105(%rbp)
movl $0x48, %edi
callq 0x5290
movq -0x1b0(%rbp), %rdx
movq %rax, -0x1b8(%rbp)
leaq 0x7b76(%rip), %rsi      # 0x239d9
leaq -0xf8(%rbp), %rdi
callq 0x9a60
jmp 0x1be71
movq -0x1b8(%rbp), %rdi
leaq 0x7a05(%rip), %rdx      # 0x23884
movl $0x194, %esi            # imm = 0x194
leaq -0xf8(%rbp), %rcx
callq 0x199d0
jmp 0x1be92
movq -0x1b8(%rbp), %rdi
movb $0x0, -0x105(%rbp)
leaq 0x14ee9(%rip), %rsi     # 0x30d90
leaq -0x242e(%rip), %rdx     # 0x19a80
callq 0x58a0
jmp 0x1c361
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
jmp 0x1beea
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
leaq -0xf8(%rbp), %rdi
callq 0x5a08
testb $0x1, -0x105(%rbp)
jne 0x1bef5
jmp 0x1bf01
movq -0x1b8(%rbp), %rdi
callq 0x53f0
jmp 0x1c355
testb $0x1, -0x29(%rbp)
je 0x1c00e
movl -0xa8(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x1c00e
movb $0x1, -0x12a(%rbp)
movl $0x48, %edi
callq 0x5290
movq %rax, -0x1c8(%rbp)
leaq -0x129(%rbp), %rdi
movq %rdi, -0x1c0(%rbp)
callq 0x58f0
movq -0x1c0(%rbp), %rdx
leaq 0x7abc(%rip), %rsi      # 0x23a1b
leaq -0x128(%rbp), %rdi
callq 0x63e0
jmp 0x1bf6d
movq -0x1c8(%rbp), %rdi
leaq 0x7a00(%rip), %rdx      # 0x2397b
movl $0x193, %esi            # imm = 0x193
leaq -0x128(%rbp), %rcx
callq 0x199d0
jmp 0x1bf8e
movq -0x1c8(%rbp), %rdi
movb $0x0, -0x12a(%rbp)
leaq 0x14ded(%rip), %rsi     # 0x30d90
leaq -0x252a(%rip), %rdx     # 0x19a80
callq 0x58a0
jmp 0x1c361
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
jmp 0x1bfe6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x5a08
leaq -0x129(%rbp), %rdi
callq 0x55c0
testb $0x1, -0x12a(%rbp)
jne 0x1bffd
jmp 0x1c009
movq -0x1c8(%rbp), %rdi
callq 0x53f0
jmp 0x1c355
movq -0x1b0(%rbp), %rsi
movq -0x1a8(%rbp), %rdi
callq 0x5330
jmp 0x1c345
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xf890
movq %rax, %rdi
callq 0x5870
testb $0x1, %al
jne 0x1c089
cmpl $0x2, -0x14(%rbp)
jne 0x1c060
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xf8b0
movq %rax, %rdi
callq 0x5870
testb $0x1, %al
jne 0x1c060
jmp 0x1c089
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xf890
movq -0x198(%rbp), %rdi
movq -0x1b0(%rbp), %rsi
movq %rax, %rdx
movq -0x20(%rbp), %rcx
callq 0x1bbd0
jmp 0x1c0d1
cmpl $0x2, -0x14(%rbp)
jne 0x1c0cf
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xf8b0
movq %rax, %rdi
callq 0x5870
testb $0x1, %al
jne 0x1c0cf
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xf8b0
movq -0x198(%rbp), %rdi
movq -0x1b0(%rbp), %rsi
movq %rax, %rdx
movq -0x20(%rbp), %rcx
callq 0x1bbd0
jmp 0x1c0d1
movq -0x1b0(%rbp), %rdi
callq 0x5870
testb $0x1, %al
jne 0x1c13e
movq -0x1b0(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x59f0
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x1c13e
movq -0x1b0(%rbp), %rdi
callq 0x5480
movq %rax, -0x140(%rbp)
leaq -0x138(%rbp), %rdi
leaq -0x140(%rbp), %rsi
callq 0x125e0
movq -0x1b0(%rbp), %rdi
movq -0x138(%rbp), %rsi
movl $0x2e, %edx
callq 0x5980
movq %rax, -0x148(%rbp)
movq -0x1b0(%rbp), %rdi
callq 0x5240
movq %rax, %rdi
leaq -0xc0(%rbp), %rsi
callq 0x54c0
cmpl $-0x1, %eax
jne 0x1c243
movb $0x0, -0x29(%rbp)
xorl %eax, %eax
testb $0x1, %al
jne 0x1c243
cmpl $0x2, -0x14(%rbp)
je 0x1c243
movb $0x1, -0x169(%rbp)
movl $0x48, %edi
callq 0x5290
movq -0x1b0(%rbp), %rdx
movq %rax, -0x1d0(%rbp)
leaq 0x78d8(%rip), %rsi      # 0x23a78
leaq -0x168(%rbp), %rdi
callq 0x9a60
jmp 0x1c1ae
movq -0x1d0(%rbp), %rdi
leaq 0x76c8(%rip), %rdx      # 0x23884
movl $0x194, %esi            # imm = 0x194
leaq -0x168(%rbp), %rcx
callq 0x199d0
jmp 0x1c1cf
movq -0x1d0(%rbp), %rdi
movb $0x0, -0x169(%rbp)
leaq 0x14bac(%rip), %rsi     # 0x30d90
leaq -0x276b(%rip), %rdx     # 0x19a80
callq 0x58a0
jmp 0x1c361
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
jmp 0x1c227
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x100(%rbp)
movl %eax, -0x104(%rbp)
leaq -0x168(%rbp), %rdi
callq 0x5a08
testb $0x1, -0x169(%rbp)
jne 0x1c232
jmp 0x1c23e
movq -0x1d0(%rbp), %rdi
callq 0x53f0
jmp 0x1c355
testb $0x1, -0x29(%rbp)
je 0x1c2c4
movl -0xa8(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x1c2c4
cmpl $0x0, -0x14(%rbp)
je 0x1c267
cmpl $0x1, -0x14(%rbp)
jne 0x1c279
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xf930
testb $0x1, (%rax)
jne 0x1c2c4
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xf8f0
movq -0x198(%rbp), %rsi
movq -0x1b0(%rbp), %rdx
movq %rax, %rcx
leaq -0x190(%rbp), %rdi
callq 0x1e6c0
movq -0x1b0(%rbp), %rdi
leaq -0x190(%rbp), %rsi
callq 0x5670
leaq -0x190(%rbp), %rdi
callq 0x5a08
jmp 0x1c307
testb $0x1, -0x29(%rbp)
je 0x1c305
movl -0xa8(%rbp), %eax
andl $0xf000, %eax           # imm = 0xF000
cmpl $0x4000, %eax           # imm = 0x4000
jne 0x1c305
cmpl $0x0, -0x14(%rbp)
je 0x1c2e8
cmpl $0x1, -0x14(%rbp)
jne 0x1c305
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xf930
testb $0x1, (%rax)
je 0x1c305
movq -0x198(%rbp), %rax
movb $0x1, 0x78(%rax)
jmp 0x1c307
movl -0x14(%rbp), %eax
movl %eax, -0x1d4(%rbp)
movq -0x20(%rbp), %rax
movq 0x20(%rax), %rdi
callq 0xf8d0
movq -0x198(%rbp), %rdi
movl -0x1d4(%rbp), %esi
movq %rax, %rdx
callq 0x1e8d0
movq -0x1a8(%rbp), %rdi
movq -0x1b0(%rbp), %rsi
callq 0x5330
movq -0x1a0(%rbp), %rax
addq $0x1e0, %rsp            # imm = 0x1E0
popq %rbp
retq
movq -0x100(%rbp), %rdi
callq 0x58e0
nopw %cs:(%rax,%rax)