// File: /workspace/github_cpp_makefile_star2_O1/llefranc[P]42_webserv/srcs/server/Response.cpp
// Function: Response::fillBuffer()
void Response::fillBuffer()
{
	// If an error occured during request parsing
	if (_staLine.getCode() >= 400)
		return fillError(_staLine);

	try
	{
		// Keeping only host name and removing port
		std::string hostName(_req->getHeaders().find("host")->second);
		hostName = hostName.substr(0, hostName.find(':'));
		
		// Looking for the location block matching the URI. If returns NULL, then no appropriate block was found
		// and no additionnal configuration (index, root...) will change the URI
		std::pair<const std::string, const Location*> loc = locationSearcher(_infoVirServs,
				std::pair<std::string, std::string>(hostName, _req->getPath()));
		
		#if DEBUG
			if (loc.second)
				loc.second->printLocation(loc.first);
			else
				std::cout << "LOCATION: no match\n";
		#endif

        // Doing an HTTP redirection (301) if redirect field filled in matched location block
        if (loc.second && !loc.second->getRedirect().empty())
        {
            // Replacing location name in the URI with the redirect string set in config file
            std::string redirectedUri = _req->getPath();
            replaceLocInUri(&redirectedUri, loc.second->getRedirect(), loc.first);

            // Replacing previous requested URI with redirected URI for next client request
            // (Location header in 301 response will be set with this URI)
            _req->setPath(std::string("http://localhost:" + 
                    convertNbToString(loc.second->getPort()) + redirectedUri));

            throw StatusLine(301, REASON_301, "http redirection");
        }

        // Modifying URI with root and index directive if any, checking for the allowed methods
		std::string realUri = reconstructFullURI(_req->getMethod(), loc, _req->getPath());

        // Checking if the targeted file is a CGI based on his extension
		std::string *cgiName = getCgiExecutableName(realUri, loc.second);

        // Execute the appropriate method and fills the response buffer with status line + 
        // headers + body (if any). If an error occurs during this process, it will throw 
        // a StatusLine object with the appropriate error code.
		if (cgiName && (_req->getMethod() == GET || _req->getMethod() == POST))
			execCgi(realUri, cgiName);
		else if (_req->getMethod() == GET || _req->getMethod() == HEAD)
            execGet(realUri);
		else if (_req->getMethod() == POST)
            execPost(realUri);
		else if (_req->getMethod() == DELETE)
            execDelete(realUri);

		else
			throw (StatusLine(400, REASON_400, "request method do not exist"));
	}

	// If an error occured during the reponse creation
	catch (const StatusLine& errorStaLine)
	{
		fillError(errorStaLine);
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x128, %rsp            # imm = 0x128
movq %rdi, %rbx
cmpl $0x190, 0x10(%rdi)      # imm = 0x190
jl 0xe877
leaq 0x10(%rbx), %rsi
movq %rbx, %rdi
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xef34
movq 0x8(%rbx), %rdi
callq 0xca70
movq %rax, %r14
leaq -0x90(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x6d51(%rip), %rsi      # 0x155e7
leaq 0x6d4e(%rip), %rdx      # 0x155eb
leaq -0xa0(%rbp), %rdi
callq 0x5790
leaq -0xa0(%rbp), %rsi
movq %r14, %rdi
callq 0x12624
leaq -0xb0(%rbp), %r13
movq %r13, -0x10(%r13)
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
addq %rsi, %rdx
leaq -0xc0(%rbp), %rdi
callq 0x67a2
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0xe8f5
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0xc0(%rbp), %r14
movq %r14, %rdi
movl $0x3a, %esi
xorl %edx, %edx
callq 0x4500
leaq -0xa0(%rbp), %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4410
leaq -0xc0(%rbp), %rdi
leaq -0xa0(%rbp), %r14
movq %r14, %rsi
callq 0x44d0
movq (%r14), %rdi
cmpq %r12, %rdi
je 0xe94c
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x43e0
movq (%rbx), %r14
movq 0x8(%rbx), %rdi
callq 0xca9c
leaq -0xa0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
movq %rax, %rdx
callq 0x12552
leaq -0xe8(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
movq %r14, %rsi
callq 0x13b25
leaq -0x70(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xe99d
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0xe9b8
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xc8(%rbp), %rdi
testq %rdi, %rdi
je 0xe9d4
callq 0x8c6c
cmpq $0x0, 0x8(%rax)
jne 0xeb68
movq 0x8(%rbx), %rdi
callq 0xca92
movl %eax, %r14d
movq 0x8(%rbx), %rdi
callq 0xca9c
leaq -0xf8(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x108(%rbp), %rdi
callq 0x67a2
leaq -0xa0(%rbp), %rdi
leaq -0xe8(%rbp), %rcx
leaq -0x108(%rbp), %r8
movq %rbx, %rsi
movl %r14d, %edx
callq 0xf644
movq -0x108(%rbp), %rdi
cmpq %r15, %rdi
je 0xea45
movq -0xf8(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xc8(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x13d7b
movq %rax, %r14
testq %rax, %rax
je 0xea8f
movq 0x8(%rbx), %rdi
callq 0xca92
testl %eax, %eax
je 0xea7b
movq 0x8(%rbx), %rdi
callq 0xca92
cmpl $0x2, %eax
jne 0xea8f
leaq -0xa0(%rbp), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0xf9fe
jmp 0xeab9
movq 0x8(%rbx), %rdi
callq 0xca92
testl %eax, %eax
je 0xeaaa
movq 0x8(%rbx), %rdi
callq 0xca92
cmpl $0x1, %eax
jne 0xeb20
leaq -0xa0(%rbp), %rsi
movq %rbx, %rdi
callq 0xfc20
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0xead4
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0xd8(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xeaf3
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xc0(%rbp), %rdi
cmpq %r13, %rdi
je 0xeb0e
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x43e0
addq $0x128, %rsp            # imm = 0x128
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rdi
callq 0xca92
cmpl $0x2, %eax
jne 0xeb42
leaq -0xa0(%rbp), %rsi
movq %rbx, %rdi
callq 0xfe5a
jmp 0xeab9
movq 0x8(%rbx), %rdi
callq 0xca92
cmpl $0x3, %eax
jne 0xecc5
leaq -0xa0(%rbp), %rsi
movq %rbx, %rdi
callq 0xff6a
jmp 0xeab9
movq 0x8(%rbx), %rdi
callq 0xca9c
leaq -0xa0(%rbp), %rdi
movq %r12, (%rdi)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
callq 0x67a2
movq -0xc8(%rbp), %rdi
callq 0x8c6c
leaq -0xa0(%rbp), %rsi
leaq -0xe8(%rbp), %rcx
movq %rax, %rdx
callq 0xf5ac
movq 0x8(%rbx), %r14
movq -0xc8(%rbp), %rdi
callq 0x8c1e
movslq (%rax), %rsi
leaq -0x128(%rbp), %rdi
callq 0x1378a
leaq 0x6ca6(%rip), %rsi      # 0x15878
leaq -0x148(%rbp), %rdi
leaq -0x128(%rbp), %rdx
callq 0x51d5
leaq -0x60(%rbp), %rdi
leaq -0x148(%rbp), %rsi
leaq -0xa0(%rbp), %rdx
callq 0xc4e7
leaq -0x60(%rbp), %rsi
movq %r14, %rdi
callq 0xcab0
leaq -0x50(%rbp), %r15
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0xec21
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x138(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xec40
movq -0x138(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x118(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xec5f
movq -0x118(%rbp), %rsi
incq %rsi
callq 0x43e0
movl $0x48, %edi
callq 0x41f0
movq %rax, -0x38(%rbp)
leaq -0x60(%rbp), %rdi
movq %r15, (%rdi)
leaq 0x6c21(%rip), %rsi      # 0x1589c
leaq 0x6c2a(%rip), %rdx      # 0x158ac
callq 0x5790
movb $0x1, %al
movl %eax, -0x3c(%rbp)
leaq 0x6bf7(%rip), %rdx      # 0x1588a
leaq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rdi
movl $0x12d, %esi            # imm = 0x12D
callq 0xe106
movl $0x0, -0x3c(%rbp)
leaq 0xe0bd(%rip), %rsi      # 0x1cd70
leaq -0xb46(%rip), %rdx      # 0xe174
movq -0x38(%rbp), %rdi
callq 0x46a0
jmp 0xed2b
movl $0x48, %edi
callq 0x41f0
movq %rax, %r15
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x6bcc(%rip), %rsi      # 0x158ad
leaq 0x6be0(%rip), %rdx      # 0x158c8
leaq -0x60(%rbp), %rdi
callq 0x5790
movb $0x1, %al
movl %eax, -0x38(%rbp)
leaq 0x68ef(%rip), %rdx      # 0x155ec
leaq -0x60(%rbp), %rcx
movq %r15, %rdi
movl $0x190, %esi            # imm = 0x190
callq 0xe106
movl $0x0, -0x38(%rbp)
leaq 0xe054(%rip), %rsi      # 0x1cd70
leaq -0xbaf(%rip), %rdx      # 0xe174
movq %r15, %rdi
callq 0x46a0
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movq -0x60(%rbp), %rdi
cmpq %r15, %rdi
je 0xed55
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xed55
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movb $0x1, %al
movl %eax, -0x3c(%rbp)
cmpb $0x0, -0x3c(%rbp)
je 0xeea4
movq -0x38(%rbp), %rdi
jmp 0xee21
movq %rdx, -0x30(%rbp)
movq %rax, %r14
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xed91
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xed91
movq %rdx, -0x30(%rbp)
movq %rax, %r14
leaq -0x138(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xedb9
movq -0x138(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xedb9
movq %rdx, -0x30(%rbp)
movq %rax, %r14
leaq -0x118(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xeea4
movq -0x118(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xeea4
jmp 0xee9d
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0xee14
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xee14
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movb $0x1, %al
movl %eax, -0x38(%rbp)
cmpb $0x0, -0x38(%rbp)
je 0xeea4
movq %r15, %rdi
callq 0x42e0
jmp 0xeea4
jmp 0xee9d
jmp 0xee94
jmp 0xee94
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movq -0x108(%rbp), %rdi
cmpq %r15, %rdi
je 0xeebf
movq -0xf8(%rbp), %rsi
jmp 0xeeb7
movq %rdx, -0x30(%rbp)
movq %rax, %r14
leaq -0xa0(%rbp), %rdi
callq 0x98a0
jmp 0xeede
jmp 0xee6f
jmp 0xee63
movq %rdx, -0x30(%rbp)
movq %rax, %r14
jmp 0xeef9
movq %rdx, -0x30(%rbp)
movq %rax, %r14
jmp 0xeede
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0xeef9
movq -0x90(%rbp), %rsi
jmp 0xeef1
movq %rdx, -0x30(%rbp)
movq %rax, %r14
jmp 0xeebf
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0xeebf
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0xd8(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xeede
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xc0(%rbp), %rdi
cmpq %r13, %rdi
je 0xeef9
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x43e0
cmpl $0x1, -0x30(%rbp)
jne 0xef24
movq %r14, %rdi
callq 0x41d0
movq %rbx, %rdi
movq %rax, %rsi
callq 0xef34
callq 0x4660
jmp 0xeb0e
movq %rax, %r14
callq 0x4660
movq %r14, %rdi
callq 0x46d0
movq %rax, %rdi
callq 0x51c7