// File: /workspace/github_cpp_makefile_star2_O3/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 0xe843
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 0xeef4
movq 0x8(%rbx), %rdi
callq 0xcab0
movq %rax, %r14
leaq -0x90(%rbp), %r12
movq %r12, -0x10(%r12)
leaq 0x6d85(%rip), %rsi      # 0x155e7
leaq 0x6d82(%rip), %rdx      # 0x155eb
leaq -0xa0(%rbp), %rdi
callq 0x5796
leaq -0xa0(%rbp), %rsi
movq %r14, %rdi
callq 0x1256c
leaq -0xb0(%rbp), %r13
movq %r13, -0x10(%r13)
movq 0x40(%rax), %rsi
movq 0x48(%rax), %rdx
addq %rsi, %rdx
leaq -0xc0(%rbp), %rdi
callq 0x67e2
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0xe8c1
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 0xe918
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x43e0
movq (%rbx), %r14
movq 0x8(%rbx), %rdi
callq 0xcadc
leaq -0xa0(%rbp), %rdi
leaq -0xc0(%rbp), %rsi
movq %rax, %rdx
callq 0x1249a
leaq -0xe8(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
movq %r14, %rsi
callq 0x13a4b
leaq -0x70(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xe969
movq -0x70(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0xe984
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xc8(%rbp), %rdi
testq %rdi, %rdi
je 0xe9a0
callq 0x8c9c
cmpq $0x0, 0x8(%rax)
jne 0xeb34
movq 0x8(%rbx), %rdi
callq 0xcad2
movl %eax, %r14d
movq 0x8(%rbx), %rdi
callq 0xcadc
leaq -0xf8(%rbp), %r15
movq %r15, -0x10(%r15)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
leaq -0x108(%rbp), %rdi
callq 0x67e2
leaq -0xa0(%rbp), %rdi
leaq -0xe8(%rbp), %rcx
leaq -0x108(%rbp), %r8
movq %rbx, %rsi
movl %r14d, %edx
callq 0xf600
movq -0x108(%rbp), %rdi
cmpq %r15, %rdi
je 0xea11
movq -0xf8(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xc8(%rbp), %rsi
leaq -0xa0(%rbp), %rdi
callq 0x13c5b
movq %rax, %r14
testq %rax, %rax
je 0xea5b
movq 0x8(%rbx), %rdi
callq 0xcad2
testl %eax, %eax
je 0xea47
movq 0x8(%rbx), %rdi
callq 0xcad2
cmpl $0x2, %eax
jne 0xea5b
leaq -0xa0(%rbp), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0xf99a
jmp 0xea85
movq 0x8(%rbx), %rdi
callq 0xcad2
testl %eax, %eax
je 0xea76
movq 0x8(%rbx), %rdi
callq 0xcad2
cmpl $0x1, %eax
jne 0xeaec
leaq -0xa0(%rbp), %rsi
movq %rbx, %rdi
callq 0xfbba
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0xeaa0
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0xd8(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xeabf
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xc0(%rbp), %rdi
cmpq %r13, %rdi
je 0xeada
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 0xcad2
cmpl $0x2, %eax
jne 0xeb0e
leaq -0xa0(%rbp), %rsi
movq %rbx, %rdi
callq 0xfdf4
jmp 0xea85
movq 0x8(%rbx), %rdi
callq 0xcad2
cmpl $0x3, %eax
jne 0xec91
leaq -0xa0(%rbp), %rsi
movq %rbx, %rdi
callq 0xff04
jmp 0xea85
movq 0x8(%rbx), %rdi
callq 0xcadc
leaq -0xa0(%rbp), %rdi
movq %r12, (%rdi)
movq (%rax), %rsi
movq 0x8(%rax), %rdx
addq %rsi, %rdx
callq 0x67e2
movq -0xc8(%rbp), %rdi
callq 0x8c9c
leaq -0xa0(%rbp), %rsi
leaq -0xe8(%rbp), %rcx
movq %rax, %rdx
callq 0xf55a
movq 0x8(%rbx), %r14
movq -0xc8(%rbp), %rdi
callq 0x8c4e
movslq (%rax), %rsi
leaq -0x128(%rbp), %rdi
callq 0x136be
leaq 0x6cda(%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 0xc527
leaq -0x60(%rbp), %rsi
movq %r14, %rdi
callq 0xcaf0
leaq -0x50(%rbp), %r15
movq -0x10(%r15), %rdi
cmpq %r15, %rdi
je 0xebed
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x138(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xec0c
movq -0x138(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x118(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xec2b
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 0x6c55(%rip), %rsi      # 0x1589c
leaq 0x6c5e(%rip), %rdx      # 0x158ac
callq 0x5796
movb $0x1, %al
movl %eax, -0x3c(%rbp)
leaq 0x6c2b(%rip), %rdx      # 0x1588a
leaq -0x60(%rbp), %rcx
movq -0x38(%rbp), %rdi
movl $0x12d, %esi            # imm = 0x12D
callq 0xe0d4
movl $0x0, -0x3c(%rbp)
leaq 0xe0f1(%rip), %rsi      # 0x1cd70
leaq -0xb44(%rip), %rdx      # 0xe142
movq -0x38(%rbp), %rdi
callq 0x46a0
jmp 0xecf7
movl $0x48, %edi
callq 0x41f0
movq %rax, %r15
leaq -0x50(%rbp), %rax
movq %rax, -0x10(%rax)
leaq 0x6c00(%rip), %rsi      # 0x158ad
leaq 0x6c14(%rip), %rdx      # 0x158c8
leaq -0x60(%rbp), %rdi
callq 0x5796
movb $0x1, %al
movl %eax, -0x38(%rbp)
leaq 0x6923(%rip), %rdx      # 0x155ec
leaq -0x60(%rbp), %rcx
movq %r15, %rdi
movl $0x190, %esi            # imm = 0x190
callq 0xe0d4
movl $0x0, -0x38(%rbp)
leaq 0xe088(%rip), %rsi      # 0x1cd70
leaq -0xbad(%rip), %rdx      # 0xe142
movq %r15, %rdi
callq 0x46a0
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movq -0x60(%rbp), %rdi
cmpq %r15, %rdi
je 0xed13
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x43e0
cmpb $0x0, -0x3c(%rbp)
jne 0xed25
jmp 0xee64
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movq -0x38(%rbp), %rdi
jmp 0xede1
movq %rdx, -0x30(%rbp)
movq %rax, %r14
leaq -0x50(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xed57
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xed57
movq %rdx, -0x30(%rbp)
movq %rax, %r14
leaq -0x138(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xed7f
movq -0x138(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xed7f
movq %rdx, -0x30(%rbp)
movq %rax, %r14
leaq -0x118(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xee64
movq -0x118(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xee64
jmp 0xee5d
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movq -0x60(%rbp), %rdi
leaq -0x50(%rbp), %rax
cmpq %rax, %rdi
je 0xedcc
movq -0x50(%rbp), %rsi
incq %rsi
callq 0x43e0
cmpb $0x0, -0x38(%rbp)
jne 0xedde
jmp 0xee64
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movq %r15, %rdi
callq 0x42e0
jmp 0xee64
jmp 0xee5d
jmp 0xee54
jmp 0xee54
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movq -0x108(%rbp), %rdi
cmpq %r15, %rdi
je 0xee7f
movq -0xf8(%rbp), %rsi
jmp 0xee77
movq %rdx, -0x30(%rbp)
movq %rax, %r14
leaq -0xa0(%rbp), %rdi
callq 0x9914
jmp 0xee9e
jmp 0xee2f
jmp 0xee23
movq %rdx, -0x30(%rbp)
movq %rax, %r14
jmp 0xeeb9
movq %rdx, -0x30(%rbp)
movq %rax, %r14
jmp 0xee9e
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0xeeb9
movq -0x90(%rbp), %rsi
jmp 0xeeb1
movq %rdx, -0x30(%rbp)
movq %rax, %r14
jmp 0xee7f
movq %rdx, -0x30(%rbp)
movq %rax, %r14
movq -0xa0(%rbp), %rdi
cmpq %r12, %rdi
je 0xee7f
movq -0x90(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0xd8(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xee9e
movq -0xd8(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xc0(%rbp), %rdi
cmpq %r13, %rdi
je 0xeeb9
movq -0xb0(%rbp), %rsi
incq %rsi
callq 0x43e0
cmpl $0x1, -0x30(%rbp)
jne 0xeee4
movq %r14, %rdi
callq 0x41d0
movq %rbx, %rdi
movq %rax, %rsi
callq 0xeef4
callq 0x4660
jmp 0xeada
movq %rax, %r14
callq 0x4660
movq %r14, %rdi
callq 0x46d0
movq %rax, %rdi
callq 0x51c7