// File: /workspace/github_cpp_makefile_star2_O2/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 $0x108, %rsp            # imm = 0x108
movq %rdi, %rbx
cmpl $0x190, 0x10(%rdi)      # imm = 0x190
jl 0xcc43
leaq 0x10(%rbx), %rsi
movq %rbx, %rdi
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
jmp 0xd0d8
movq 0x8(%rbx), %rdi
callq 0xb380
movq %rax, %r14
leaq 0x498a(%rip), %rsi      # 0x115e0
leaq -0x68(%rbp), %rdi
leaq -0xb0(%rbp), %rdx
callq 0x4ec8
leaq -0x68(%rbp), %rsi
movq %r14, %rdi
callq 0xf010
addq $0x40, %rax
leaq -0xf0(%rbp), %rdi
movq %rax, %rsi
callq 0x4580
leaq -0x68(%rbp), %rdi
callq 0x47d8
leaq -0xf0(%rbp), %r14
pushq $0x3a
popq %rsi
movq %r14, %rdi
xorl %edx, %edx
callq 0x4540
leaq -0x68(%rbp), %rdi
movq %r14, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4460
leaq -0xf0(%rbp), %rdi
leaq -0x68(%rbp), %r14
movq %r14, %rsi
callq 0x44f0
movq %r14, %rdi
callq 0x47d8
movq (%rbx), %r14
movq 0x8(%rbx), %rdi
callq 0xb3ac
leaq -0x68(%rbp), %rdi
leaq -0xf0(%rbp), %rsi
movq %rax, %rdx
callq 0xef68
leaq -0xb0(%rbp), %rdi
leaq -0x68(%rbp), %rdx
movq %r14, %rsi
callq 0xff89
leaq -0x68(%rbp), %rdi
callq 0x8cde
movq -0x90(%rbp), %rdi
testq %rdi, %rdi
je 0xcd25
callq 0x8400
cmpq $0x0, 0x8(%rax)
jne 0xce4c
movq 0x8(%rbx), %rdi
callq 0xb3a2
movl %eax, %r14d
movq 0x8(%rbx), %rdi
callq 0xb3ac
leaq -0x110(%rbp), %rdi
movq %rax, %rsi
callq 0x4580
leaq -0x68(%rbp), %rdi
leaq -0xb0(%rbp), %rcx
leaq -0x110(%rbp), %r8
movq %rbx, %rsi
movl %r14d, %edx
callq 0xd51e
leaq -0x110(%rbp), %rdi
callq 0x47d8
movq -0x90(%rbp), %rsi
leaq -0x68(%rbp), %rdi
callq 0x10167
movq %rax, %r14
testq %rax, %rax
je 0xcdb6
movq 0x8(%rbx), %rdi
callq 0xb3a2
testl %eax, %eax
je 0xcda5
movq 0x8(%rbx), %rdi
callq 0xb3a2
cmpl $0x2, %eax
jne 0xcdb6
leaq -0x68(%rbp), %rsi
movq %rbx, %rdi
movq %r14, %rdx
callq 0xd82c
jmp 0xcddd
movq 0x8(%rbx), %rdi
callq 0xb3a2
testl %eax, %eax
je 0xcdd1
movq 0x8(%rbx), %rdi
callq 0xb3a2
cmpl $0x1, %eax
jne 0xce10
leaq -0x68(%rbp), %rsi
movq %rbx, %rdi
callq 0xd9da
leaq -0x68(%rbp), %rdi
callq 0x47d8
leaq -0xb0(%rbp), %rdi
callq 0x47d8
leaq -0xf0(%rbp), %rdi
callq 0x47d8
addq $0x108, %rsp            # imm = 0x108
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movq 0x8(%rbx), %rdi
callq 0xb3a2
cmpl $0x2, %eax
jne 0xce2c
leaq -0x68(%rbp), %rsi
movq %rbx, %rdi
callq 0xdb82
jmp 0xcddd
movq 0x8(%rbx), %rdi
callq 0xb3a2
cmpl $0x3, %eax
jne 0xcf61
leaq -0x68(%rbp), %rsi
movq %rbx, %rdi
callq 0xdc4c
jmp 0xcddd
movq 0x8(%rbx), %rdi
callq 0xb3ac
leaq -0x68(%rbp), %rdi
movq %rax, %rsi
callq 0x4580
movq -0x90(%rbp), %rdi
callq 0x8400
leaq -0x68(%rbp), %rsi
leaq -0xb0(%rbp), %rcx
movq %rax, %rdx
callq 0xd4a8
movq 0x8(%rbx), %r14
movq -0x90(%rbp), %rdi
callq 0x83b2
movslq (%rax), %rsi
leaq -0x130(%rbp), %rdi
callq 0xfc62
leaq 0x4980(%rip), %rsi      # 0x11826
leaq -0xd0(%rbp), %rdi
leaq -0x130(%rbp), %rdx
callq 0x4f60
leaq -0x88(%rbp), %rdi
leaq -0xd0(%rbp), %rsi
leaq -0x68(%rbp), %rdx
callq 0xaf33
leaq -0x88(%rbp), %rsi
movq %r14, %rdi
callq 0xb3c0
leaq -0x88(%rbp), %rdi
callq 0x47d8
leaq -0xd0(%rbp), %rdi
callq 0x47d8
leaq -0x130(%rbp), %rdi
callq 0x47d8
pushq $0x48
popq %rdi
callq 0x4220
movq %rax, %r12
leaq 0x4935(%rip), %rsi      # 0x1184a
leaq -0x88(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
callq 0x4ec8
movb $0x1, %r13b
leaq 0x4906(%rip), %rdx      # 0x11838
leaq -0x88(%rbp), %rcx
movq %r12, %rdi
movl $0x12d, %esi            # imm = 0x12D
callq 0xc540
xorl %r13d, %r13d
leaq 0xbe40(%rip), %rsi      # 0x18d90
leaq -0x9c1(%rip), %rdx      # 0xc596
movq %r12, %rdi
callq 0x46b0
jmp 0xcfbd
pushq $0x48
popq %rdi
callq 0x4220
movq %rax, %r12
leaq 0x48e8(%rip), %rsi      # 0x1185b
leaq -0x88(%rbp), %rdi
leaq -0xd0(%rbp), %rdx
callq 0x4ec8
movb $0x1, %r13b
leaq 0x4655(%rip), %rdx      # 0x115e5
leaq -0x88(%rbp), %rcx
movq %r12, %rdi
movl $0x190, %esi            # imm = 0x190
callq 0xc540
xorl %r13d, %r13d
leaq 0xbde2(%rip), %rsi      # 0x18d90
leaq -0xa1f(%rip), %rdx      # 0xc596
movq %r12, %rdi
callq 0x46b0
jmp 0xcfff
jmp 0xd018
movq %rdx, %r14
movq %rax, %r15
leaq -0x88(%rbp), %rdi
callq 0x47d8
jmp 0xcfdb
movq %rdx, %r14
movq %rax, %r15
leaq -0xd0(%rbp), %rdi
callq 0x47d8
jmp 0xcfef
movq %rdx, %r14
movq %rax, %r15
leaq -0x130(%rbp), %rdi
callq 0x47d8
jmp 0xd07c
jmp 0xd076
movq %rdx, %r14
movq %rax, %r15
leaq -0x88(%rbp), %rdi
callq 0x47d8
testb %r13b, %r13b
jne 0xd01e
jmp 0xd07c
movq %rdx, %r14
movq %rax, %r15
movq %r12, %rdi
callq 0x4330
jmp 0xd07c
jmp 0xd076
jmp 0xd06e
jmp 0xd06e
movq %rdx, %r14
movq %rax, %r15
leaq -0x110(%rbp), %rdi
jmp 0xd080
movq %rdx, %r14
movq %rax, %r15
leaq -0x68(%rbp), %rdi
callq 0x8cde
jmp 0xd091
jmp 0xd05a
jmp 0xd052
movq %rdx, %r14
movq %rax, %r15
jmp 0xd09d
movq %rdx, %r14
movq %rax, %r15
jmp 0xd091
movq %rdx, %r14
movq %rax, %r15
leaq -0x68(%rbp), %rdi
jmp 0xd098
movq %rdx, %r14
movq %rax, %r15
jmp 0xd085
movq %rdx, %r14
movq %rax, %r15
leaq -0x68(%rbp), %rdi
callq 0x47d8
leaq -0xb0(%rbp), %rdi
callq 0x47d8
leaq -0xf0(%rbp), %rdi
callq 0x47d8
cmpl $0x1, %r14d
jne 0xd0c8
movq %r15, %rdi
callq 0x41e0
movq %rbx, %rdi
movq %rax, %rsi
callq 0xd0d8
callq 0x4680
jmp 0xcdfe
movq %rax, %r15
callq 0x4680
movq %r15, %rdi
callq 0x46d0
movq %rax, %rdi
callq 0x4f03