// File: llefranc[P]42_webserv/srcs/server/Response.cpp
// Function: Response::fillError(StatusLine const&)
void Response::fillError(const StatusLine& sta)
{    
    if (sta.getCode() != _staLine.getCode())
    {
	    _staLine = sta;
	}
	
	_buffer.clear();
	
	// Filling buffer with error code + some basic headers
	fillStatusLine(sta);
	fillServerHeader();
	fillDateHeader();

    // Case HTTP redirection
    if (_staLine.getCode() == 301)
	{
        fillLocationHeader(_req->getPath());
	}

	// Value of host header field in request
	const std::string* hostField = &_req->getHeaders().find("host")->second;
	const std::string hostValue(hostField->substr(0, hostField->find(':')));

	// Looking in each virtual server names if one match host header field value, if
    // not using default server
	const ServerInfo* servMatch = findVirtServ(_infoVirServs, hostValue);
    if (!servMatch)
	{
        servMatch = &_infoVirServs->front();
	}
	
	std::string pathError;
	std::string errorCodeHTML = "/" + convertNbToString(sta.getCode()) + ".html";

    // Custom error pages if set in config file
	if (!servMatch->getError().empty())
	{
        // Adding relative file access if not well filled in config file
        pathError = (servMatch->getError()[0] == '/') ? 
                "." + servMatch->getError() + errorCodeHTML: servMatch->getError() + errorCodeHTML;

		struct stat infFile;
		if (stat(pathError.c_str(), &infFile) == -1)
			pathError = DEFAULT_PATH_ERROR_PAGES + errorCodeHTML;
	}
    
	else
		pathError = DEFAULT_PATH_ERROR_PAGES + errorCodeHTML;

    // Filling buffer
	FileParser body(pathError.c_str(), true);

	fillContentlengthHeader(convertNbToString(body.getRequestFileSize()));
	_buffer += CLRF + body.getRequestFile();
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
subq $0x610, %rsp            # imm = 0x610
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rax
movq %rax, -0x588(%rbp)
movq -0x10(%rbp), %rdi
callq 0x1f010
movq -0x588(%rbp), %rdi
movl %eax, -0x57c(%rbp)
addq $0x10, %rdi
callq 0x1f010
movl %eax, %ecx
movl -0x57c(%rbp), %eax
cmpl %ecx, %eax
je 0x1b5ab
movq -0x10(%rbp), %rsi
leaq -0x58(%rbp), %rdi
movq %rdi, -0x590(%rbp)
callq 0x1ef70
movq -0x588(%rbp), %rdi
movq -0x590(%rbp), %rsi
addq $0x10, %rdi
callq 0x1efe0
jmp 0x1b586
leaq -0x58(%rbp), %rdi
callq 0x19a80
jmp 0x1b5ab
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x58(%rbp), %rdi
callq 0x19a80
jmp 0x1bbc6
movq -0x588(%rbp), %rdi
addq $0x58, %rdi
callq 0x5700
movq -0x588(%rbp), %rdi
movq -0x10(%rbp), %rsi
callq 0x1cb90
movq -0x588(%rbp), %rdi
callq 0x1cdc0
movq -0x588(%rbp), %rdi
callq 0x1cdf0
movq -0x588(%rbp), %rdi
addq $0x10, %rdi
callq 0x1f010
cmpl $0x12d, %eax            # imm = 0x12D
jne 0x1b619
movq -0x588(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x174c0
movq -0x588(%rbp), %rdi
movq %rax, %rsi
callq 0x1e4b0
movq -0x588(%rbp), %rax
movq 0x8(%rax), %rdi
callq 0x17450
movq %rax, -0x5a0(%rbp)
leaq -0x99(%rbp), %rdi
movq %rdi, -0x598(%rbp)
callq 0x58f0
movq -0x598(%rbp), %rdx
leaq 0x7fa1(%rip), %rsi      # 0x235f2
leaq -0x98(%rbp), %rdi
callq 0x63e0
jmp 0x1b65f
movq -0x5a0(%rbp), %rdi
leaq -0x98(%rbp), %rsi
callq 0x1f060
movq %rax, -0x5a8(%rbp)
jmp 0x1b67b
movq -0x5a8(%rbp), %rax
movq %rax, -0x78(%rbp)
leaq -0x78(%rbp), %rdi
callq 0x10cb0
addq $0x20, %rax
movq %rax, -0x5d0(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x5a08
leaq -0x99(%rbp), %rdi
callq 0x55c0
movq -0x5d0(%rbp), %rax
movq %rax, -0x70(%rbp)
movq -0x70(%rbp), %rdi
movq %rdi, -0x5c8(%rbp)
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x5c0(%rbp)
movl $0x3a, %esi
callq 0x56f0
movq -0x5c8(%rbp), %rsi
movq -0x5c0(%rbp), %rdx
movq %rax, %rcx
leaq -0xc0(%rbp), %rdi
movq %rdi, -0x5b8(%rbp)
callq 0x5590
movq -0x588(%rbp), %rax
movq -0x5b8(%rbp), %rsi
movq (%rax), %rdi
callq 0x21300
movq %rax, -0x5b0(%rbp)
jmp 0x1b720
movq -0x5b0(%rbp), %rax
movq %rax, -0xc8(%rbp)
cmpq $0x0, -0xc8(%rbp)
jne 0x1b798
movq -0x588(%rbp), %rax
movq (%rax), %rdi
callq 0x19f00
movq %rax, -0xc8(%rbp)
jmp 0x1b798
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1b776
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x98(%rbp), %rdi
callq 0x5a08
leaq -0x99(%rbp), %rdi
callq 0x55c0
jmp 0x1bbc6
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1bbba
leaq -0xe8(%rbp), %rdi
callq 0x5770
movq -0x10(%rbp), %rdi
callq 0x1f010
movl %eax, -0x5d4(%rbp)
jmp 0x1b7b5
movl -0x5d4(%rbp), %eax
movslq %eax, %rsi
leaq -0x148(%rbp), %rdi
callq 0x20c10
jmp 0x1b7cc
leaq 0x7fe9(%rip), %rsi      # 0x237bc
leaq -0x128(%rbp), %rdi
leaq -0x148(%rbp), %rdx
callq 0x6700
jmp 0x1b7e8
leaq 0x82a5(%rip), %rdx      # 0x23a94
leaq -0x108(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x66b0
jmp 0x1b804
leaq -0x128(%rbp), %rdi
callq 0x5a08
leaq -0x148(%rbp), %rdi
callq 0x5a08
movq -0xc8(%rbp), %rdi
callq 0x8d30
movq %rax, -0x5e0(%rbp)
jmp 0x1b831
movq -0x5e0(%rbp), %rdi
callq 0x5870
testb $0x1, %al
jne 0x1ba1f
movq -0xc8(%rbp), %rdi
movb $0x0, -0x189(%rbp)
callq 0x8d30
movq %rax, -0x5e8(%rbp)
jmp 0x1b861
movq -0x5e8(%rbp), %rdi
xorl %eax, %eax
movl %eax, %esi
callq 0x55e0
movsbl (%rax), %eax
cmpl $0x2f, %eax
jne 0x1b8cf
movq -0xc8(%rbp), %rdi
callq 0x8d30
movq %rax, -0x5f0(%rbp)
jmp 0x1b88e
movq -0x5f0(%rbp), %rdx
leaq 0x7f2a(%rip), %rsi      # 0x237c6
leaq -0x188(%rbp), %rdi
callq 0x9a60
jmp 0x1b8aa
movb $0x1, -0x189(%rbp)
leaq -0x168(%rbp), %rdi
leaq -0x188(%rbp), %rsi
leaq -0x108(%rbp), %rdx
callq 0x16dc0
jmp 0x1b8cd
jmp 0x1b902
movq -0xc8(%rbp), %rdi
callq 0x8d30
movq %rax, -0x5f8(%rbp)
jmp 0x1b8e4
movq -0x5f8(%rbp), %rsi
leaq -0x168(%rbp), %rdi
leaq -0x108(%rbp), %rdx
callq 0x1f1d0
jmp 0x1b900
jmp 0x1b902
leaq -0xe8(%rbp), %rdi
leaq -0x168(%rbp), %rsi
callq 0x5670
leaq -0x168(%rbp), %rdi
callq 0x5a08
testb $0x1, -0x189(%rbp)
jne 0x1b92c
jmp 0x1b938
leaq -0x188(%rbp), %rdi
callq 0x5a08
leaq -0xe8(%rbp), %rdi
callq 0x5240
movq %rax, %rdi
leaq -0x220(%rbp), %rsi
callq 0x54c0
cmpl $-0x1, %eax
jne 0x1ba1d
leaq 0x8137(%rip), %rsi      # 0x23a9a
leaq -0x240(%rbp), %rdi
leaq -0x108(%rbp), %rdx
callq 0x9a60
jmp 0x1b978
leaq -0xe8(%rbp), %rdi
leaq -0x240(%rbp), %rsi
callq 0x5670
leaq -0x240(%rbp), %rdi
callq 0x5a08
jmp 0x1ba1d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1bbae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1b9d3
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x128(%rbp), %rdi
callq 0x5a08
leaq -0x148(%rbp), %rdi
callq 0x5a08
jmp 0x1bbae
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1bba2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
testb $0x1, -0x189(%rbp)
jne 0x1ba0c
jmp 0x1ba18
leaq -0x188(%rbp), %rdi
callq 0x5a08
jmp 0x1bba2
jmp 0x1ba5a
leaq 0x8074(%rip), %rsi      # 0x23a9a
leaq -0x260(%rbp), %rdi
leaq -0x108(%rbp), %rdx
callq 0x9a60
jmp 0x1ba3b
leaq -0xe8(%rbp), %rdi
leaq -0x260(%rbp), %rsi
callq 0x5670
leaq -0x260(%rbp), %rdi
callq 0x5a08
leaq -0xe8(%rbp), %rdi
callq 0x5240
movq %rax, %rsi
leaq -0x538(%rbp), %rdi
movl $0x1, %edx
callq 0xb170
jmp 0x1ba7c
leaq -0x538(%rbp), %rdi
callq 0xb810
movq %rax, -0x600(%rbp)
jmp 0x1ba91
movq -0x600(%rbp), %rsi
leaq -0x558(%rbp), %rdi
callq 0x20c10
jmp 0x1baa6
movq -0x588(%rbp), %rdi
leaq -0x558(%rbp), %rsi
callq 0x1d230
jmp 0x1babb
leaq -0x558(%rbp), %rdi
callq 0x5a08
leaq -0x538(%rbp), %rdi
callq 0xb630
movq %rax, -0x608(%rbp)
jmp 0x1badc
movq -0x608(%rbp), %rdx
leaq 0x818b(%rip), %rsi      # 0x23c75
leaq -0x578(%rbp), %rdi
callq 0x9a60
jmp 0x1baf8
movq -0x588(%rbp), %rdi
addq $0x58, %rdi
leaq -0x578(%rbp), %rsi
callq 0x56b0
jmp 0x1bb11
leaq -0x578(%rbp), %rdi
callq 0x5a08
leaq -0x538(%rbp), %rdi
callq 0xb5a0
leaq -0x108(%rbp), %rdi
callq 0x5a08
leaq -0xe8(%rbp), %rdi
callq 0x5a08
leaq -0xc0(%rbp), %rdi
callq 0x5a08
addq $0x610, %rsp            # imm = 0x610
popq %rbp
retq
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
jmp 0x1bb96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x558(%rbp), %rdi
callq 0x5a08
jmp 0x1bb96
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x60(%rbp)
movl %eax, -0x64(%rbp)
leaq -0x578(%rbp), %rdi
callq 0x5a08
leaq -0x538(%rbp), %rdi
callq 0xb5a0
leaq -0x108(%rbp), %rdi
callq 0x5a08
leaq -0xe8(%rbp), %rdi
callq 0x5a08
leaq -0xc0(%rbp), %rdi
callq 0x5a08
movq -0x60(%rbp), %rdi
callq 0x58e0
nop