// File: 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
subq $0x2d0, %rsp            # imm = 0x2D0
movq %rdi, -0x8(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x218(%rbp)
addq $0x10, %rdi
callq 0x1f010
cmpl $0x190, %eax            # imm = 0x190
jl 0x1acf2
movq -0x218(%rbp), %rdi
movq %rdi, %rsi
addq $0x10, %rsi
callq 0x1b510
jmp 0x1b4dd
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x17450
movq %rax, -0x220(%rbp)
jmp 0x1ad0b
leaq -0x61(%rbp), %rdi
movq %rdi, -0x228(%rbp)
callq 0x58f0
movq -0x228(%rbp), %rdx
leaq 0x88c9(%rip), %rsi      # 0x235f2
leaq -0x60(%rbp), %rdi
callq 0x63e0
jmp 0x1ad34
movq -0x220(%rbp), %rdi
leaq -0x60(%rbp), %rsi
callq 0x1f060
movq %rax, -0x230(%rbp)
jmp 0x1ad4d
movq -0x230(%rbp), %rax
movq %rax, -0x30(%rbp)
leaq -0x30(%rbp), %rdi
callq 0x10cb0
movq %rax, %rsi
addq $0x20, %rsi
leaq -0x28(%rbp), %rdi
callq 0x5250
jmp 0x1ad73
leaq -0x60(%rbp), %rdi
callq 0x5a08
leaq -0x61(%rbp), %rdi
callq 0x55c0
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x238(%rbp)
leaq -0x28(%rbp), %rdi
movq %rdi, -0x240(%rbp)
movl $0x3a, %esi
callq 0x56f0
movq -0x240(%rbp), %rsi
movq -0x238(%rbp), %rdx
movq %rax, %rcx
leaq -0x88(%rbp), %rdi
callq 0x5590
jmp 0x1adc4
leaq -0x28(%rbp), %rdi
leaq -0x88(%rbp), %rsi
movq %rsi, -0x258(%rbp)
callq 0x5670
movq -0x258(%rbp), %rdi
callq 0x5a08
movq -0x218(%rbp), %rax
movq (%rax), %rcx
movq %rcx, -0x250(%rbp)
movq 0x8(%rax), %rdi
callq 0x174c0
movq %rax, -0x248(%rbp)
jmp 0x1ae0a
movq -0x248(%rbp), %rdx
leaq -0xf0(%rbp), %rdi
leaq -0x28(%rbp), %rsi
callq 0x1f090
jmp 0x1ae23
movq -0x250(%rbp), %rsi
leaq -0xb0(%rbp), %rdi
leaq -0xf0(%rbp), %rdx
callq 0x21150
jmp 0x1ae3f
leaq -0xf0(%rbp), %rdi
callq 0x10bd0
cmpq $0x0, -0x90(%rbp)
je 0x1b162
movq -0x90(%rbp), %rdi
callq 0xf950
movq %rax, -0x260(%rbp)
jmp 0x1ae6e
movq -0x260(%rbp), %rdi
callq 0x5870
testb $0x1, %al
jne 0x1b162
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x174c0
movq %rax, -0x268(%rbp)
jmp 0x1ae9b
movq -0x268(%rbp), %rsi
leaq -0x110(%rbp), %rdi
callq 0x5250
jmp 0x1aeb0
movq -0x90(%rbp), %rdi
callq 0xf950
movq %rax, -0x270(%rbp)
jmp 0x1aec5
movq -0x270(%rbp), %rdx
movq -0x218(%rbp), %rdi
leaq -0x110(%rbp), %rsi
leaq -0xb0(%rbp), %rcx
callq 0x1bbd0
jmp 0x1aee8
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rax
movq %rax, -0x280(%rbp)
movq -0x90(%rbp), %rdi
callq 0xf880
movq %rax, -0x278(%rbp)
jmp 0x1af0f
movq -0x278(%rbp), %rax
movslq (%rax), %rsi
leaq -0x170(%rbp), %rdi
callq 0x20c10
jmp 0x1af27
leaq 0x8905(%rip), %rsi      # 0x23833
leaq -0x150(%rbp), %rdi
leaq -0x170(%rbp), %rdx
callq 0x6700
jmp 0x1af43
leaq -0x130(%rbp), %rdi
leaq -0x150(%rbp), %rsi
leaq -0x110(%rbp), %rdx
callq 0x16dc0
jmp 0x1af5f
movq -0x280(%rbp), %rdi
leaq -0x130(%rbp), %rsi
callq 0x17500
jmp 0x1af74
leaq -0x130(%rbp), %rdi
callq 0x5a08
leaq -0x150(%rbp), %rdi
callq 0x5a08
leaq -0x170(%rbp), %rdi
callq 0x5a08
movb $0x1, -0x192(%rbp)
movl $0x48, %edi
callq 0x5290
movq %rax, -0x290(%rbp)
leaq -0x191(%rbp), %rdi
movq %rdi, -0x288(%rbp)
callq 0x58f0
movq -0x288(%rbp), %rdx
leaq 0x8886(%rip), %rsi      # 0x23857
leaq -0x190(%rbp), %rdi
callq 0x63e0
jmp 0x1afdf
movq -0x290(%rbp), %rdi
leaq 0x8858(%rip), %rdx      # 0x23845
movl $0x12d, %esi            # imm = 0x12D
leaq -0x190(%rbp), %rcx
callq 0x199d0
jmp 0x1b000
movq -0x290(%rbp), %rdi
movb $0x0, -0x192(%rbp)
leaq 0x15d7b(%rip), %rsi     # 0x30d90
leaq -0x159c(%rip), %rdx     # 0x19a80
callq 0x58a0
jmp 0x1b50c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1b4a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1b05a
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x60(%rbp), %rdi
callq 0x5a08
leaq -0x61(%rbp), %rdi
callq 0x55c0
jmp 0x1b4a7
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1b49e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0xf0(%rbp), %rdi
callq 0x10bd0
jmp 0x1b49e
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1b492
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1b151
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1b0f8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1b0ec
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x130(%rbp), %rdi
callq 0x5a08
leaq -0x150(%rbp), %rdi
callq 0x5a08
leaq -0x170(%rbp), %rdi
callq 0x5a08
jmp 0x1b151
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1b12c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x190(%rbp), %rdi
callq 0x5a08
leaq -0x191(%rbp), %rdi
callq 0x55c0
testb $0x1, -0x192(%rbp)
jne 0x1b143
jmp 0x1b14f
movq -0x290(%rbp), %rdi
callq 0x53f0
jmp 0x1b151
leaq -0x110(%rbp), %rdi
callq 0x5a08
jmp 0x1b492
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x174a0
movl %eax, -0x294(%rbp)
jmp 0x1b17a
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x174c0
movq %rax, -0x2a0(%rbp)
jmp 0x1b193
movq -0x2a0(%rbp), %rsi
leaq -0x1d8(%rbp), %rdi
callq 0x5250
jmp 0x1b1a8
movl -0x294(%rbp), %edx
movq -0x218(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
leaq -0xb0(%rbp), %rcx
leaq -0x1d8(%rbp), %r8
callq 0x1bd40
jmp 0x1b1d1
leaq -0x1d8(%rbp), %rdi
callq 0x5a08
movq -0x90(%rbp), %rsi
leaq -0x1b8(%rbp), %rdi
callq 0x21480
movq %rax, -0x2a8(%rbp)
jmp 0x1b1f9
movq -0x2a8(%rbp), %rax
movq %rax, -0x1e0(%rbp)
cmpq $0x0, -0x1e0(%rbp)
je 0x1b2aa
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x174a0
movl %eax, -0x2ac(%rbp)
jmp 0x1b22d
movl -0x2ac(%rbp), %eax
cmpl $0x0, %eax
je 0x1b25b
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x174a0
movl %eax, -0x2b0(%rbp)
jmp 0x1b250
movl -0x2b0(%rbp), %eax
cmpl $0x2, %eax
jne 0x1b2aa
movq -0x218(%rbp), %rdi
movq -0x1e0(%rbp), %rdx
leaq -0x1b8(%rbp), %rsi
callq 0x1c370
jmp 0x1b277
jmp 0x1b463
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x1d8(%rbp), %rdi
callq 0x5a08
jmp 0x1b492
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1b486
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x174a0
movl %eax, -0x2b4(%rbp)
jmp 0x1b2c2
movl -0x2b4(%rbp), %eax
cmpl $0x0, %eax
je 0x1b2f0
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x174a0
movl %eax, -0x2b8(%rbp)
jmp 0x1b2e5
movl -0x2b8(%rbp), %eax
cmpl $0x1, %eax
jne 0x1b30a
movq -0x218(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0x1c630
jmp 0x1b305
jmp 0x1b461
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x174a0
movl %eax, -0x2bc(%rbp)
jmp 0x1b322
movl -0x2bc(%rbp), %eax
cmpl $0x2, %eax
jne 0x1b347
movq -0x218(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0x1c900
jmp 0x1b342
jmp 0x1b45f
movq -0x218(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x174a0
movl %eax, -0x2c0(%rbp)
jmp 0x1b35f
movl -0x2c0(%rbp), %eax
cmpl $0x3, %eax
jne 0x1b384
movq -0x218(%rbp), %rdi
leaq -0x1b8(%rbp), %rsi
callq 0x1ca80
jmp 0x1b37f
jmp 0x1b45d
movb $0x1, -0x202(%rbp)
movl $0x48, %edi
callq 0x5290
movq %rax, -0x2d0(%rbp)
leaq -0x201(%rbp), %rdi
movq %rdi, -0x2c8(%rbp)
callq 0x58f0
movq -0x2c8(%rbp), %rdx
leaq 0x84ab(%rip), %rsi      # 0x23868
leaq -0x200(%rbp), %rdi
callq 0x63e0
jmp 0x1b3cb
movq -0x2d0(%rbp), %rdi
leaq 0x821e(%rip), %rdx      # 0x235f7
movl $0x190, %esi            # imm = 0x190
leaq -0x200(%rbp), %rcx
callq 0x199d0
jmp 0x1b3ec
movq -0x2d0(%rbp), %rdi
movb $0x0, -0x202(%rbp)
leaq 0x1598f(%rip), %rsi     # 0x30d90
leaq -0x1988(%rip), %rdx     # 0x19a80
callq 0x58a0
jmp 0x1b50c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
jmp 0x1b438
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
leaq -0x200(%rbp), %rdi
callq 0x5a08
leaq -0x201(%rbp), %rdi
callq 0x55c0
testb $0x1, -0x202(%rbp)
jne 0x1b44f
jmp 0x1b45b
movq -0x2d0(%rbp), %rdi
callq 0x53f0
jmp 0x1b486
jmp 0x1b45f
jmp 0x1b461
jmp 0x1b463
leaq -0x1b8(%rbp), %rdi
callq 0x5a08
leaq -0xb0(%rbp), %rdi
callq 0x1f0f0
leaq -0x28(%rbp), %rdi
callq 0x5a08
jmp 0x1b4dd
leaq -0x1b8(%rbp), %rdi
callq 0x5a08
leaq -0xb0(%rbp), %rdi
callq 0x1f0f0
leaq -0x28(%rbp), %rdi
callq 0x5a08
movl -0x3c(%rbp), %eax
movl $0x1, %ecx
cmpl %ecx, %eax
jne 0x1b4fb
movq -0x38(%rbp), %rdi
callq 0x5230
movq -0x218(%rbp), %rdi
movq %rax, -0x210(%rbp)
movq -0x210(%rbp), %rsi
callq 0x1b510
jmp 0x1b4d8
callq 0x5860
addq $0x2d0, %rsp            # imm = 0x2D0
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x38(%rbp)
movl %eax, -0x3c(%rbp)
callq 0x5860
jmp 0x1b4f9
jmp 0x1b4fb
movq -0x38(%rbp), %rdi
callq 0x58e0
movq %rax, %rdi
callq 0x64e0
nopl (%rax)