// File: /workspace/github_cpp_makefile_star2_O3/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
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x3c8, %rsp            # imm = 0x3C8
movq %rsi, %r12
movq %rdi, %r14
movl (%rsi), %eax
cmpl 0x10(%rdi), %eax
je 0xef5b
leaq -0x3f0(%rbp), %rbx
movq %rbx, %rdi
movq %r12, %rsi
callq 0x12422
movl 0x10(%r14), %eax
movl (%rbx), %ecx
movl %ecx, 0x10(%r14)
movl %eax, (%rbx)
leaq 0x18(%r14), %rdi
leaq -0x3e8(%rbp), %rsi
callq 0x42d0
leaq 0x38(%r14), %rdi
leaq -0x3c8(%rbp), %rsi
callq 0x42d0
movq %rbx, %rdi
callq 0xe142
leaq 0x58(%r14), %r13
movq $0x0, 0x60(%r14)
movq 0x58(%r14), %rax
movb $0x0, (%rax)
movq %r14, %rdi
movq %r12, %rsi
callq 0xffe4
leaq 0x6975(%rip), %rsi      # 0x158f5
movq %r13, %rdi
callq 0x4760
movq %r14, %rdi
callq 0x10304
cmpl $0x12d, 0x10(%r14)      # imm = 0x12D
jne 0xefae
movq 0x8(%r14), %rdi
callq 0xcadc
movq %r14, %rdi
movq %rax, %rsi
callq 0x11d9e
movq 0x8(%r14), %rdi
callq 0xcab0
movq %rax, %r15
leaq -0x398(%rbp), %rbx
movq %rbx, -0x10(%rbx)
leaq 0x661b(%rip), %rsi      # 0x155e7
leaq 0x6618(%rip), %rdx      # 0x155eb
leaq -0x3a8(%rbp), %rdi
callq 0x5796
leaq -0x3a8(%rbp), %rsi
movq %r15, %rdi
callq 0x1256c
movq %rax, %r15
movq %r13, -0xb0(%rbp)
addq $0x40, %r15
movq -0x3a8(%rbp), %rdi
cmpq %rbx, %rdi
je 0xf017
movq -0x398(%rbp), %rsi
incq %rsi
callq 0x43e0
movq %r15, %rdi
movl $0x3a, %esi
xorl %edx, %edx
callq 0x4500
leaq -0xd0(%rbp), %r13
movq %r13, %rdi
movq %r15, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x4410
movq (%r14), %rdi
movq %r13, %rsi
callq 0x13b82
movq %rax, %r15
testq %rax, %rax
jne 0xf056
movq (%r14), %rax
movq (%rax), %r15
leaq -0x98(%rbp), %r13
movq %r13, -0x10(%r13)
movq $0x0, -0x8(%r13)
movb $0x0, (%r13)
movslq (%r12), %rsi
leaq -0x88(%rbp), %rdi
callq 0x136be
leaq 0x6731(%rip), %rcx      # 0x157b6
leaq -0x88(%rbp), %rdi
movl $0x1, %r8d
xorl %esi, %esi
xorl %edx, %edx
callq 0x4440
movq %rbx, -0x3a8(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xf0c4
movq %rdx, -0x3a8(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x398(%rbp)
jmp 0xf0ca
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq 0x8(%rax), %rdx
leaq -0x3a8(%rbp), %rdi
movq %rdx, 0x8(%rdi)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq 0x69ea(%rip), %rsi      # 0x15ad9
callq 0x4760
leaq -0x58(%rbp), %rsi
movq %rsi, -0x10(%rsi)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xf118
movq %rdx, -0x68(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x58(%rbp)
jmp 0xf11e
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq 0x8(%rax), %rdx
movq %rdx, -0x60(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq -0x3a8(%rbp), %rdi
cmpq %rbx, %rdi
je 0xf150
movq -0x398(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x78(%rbp), %r12
movq -0x10(%r12), %rdi
cmpq %r12, %rdi
je 0xf16a
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x43e0
movq %r15, %rdi
callq 0x5ba2
cmpq $0x0, 0x8(%rax)
je 0xf1ec
movq %r15, %rdi
callq 0x5ba2
movq (%rax), %rax
movb (%rax), %r13b
cmpb $0x2f, %r13b
jne 0xf231
movq %r15, %rdi
callq 0x5ba2
leaq 0x6620(%rip), %rsi      # 0x157c0
leaq -0x88(%rbp), %rdi
movq %rax, %rdx
callq 0x643f
movq -0x68(%rbp), %rsi
movq -0x60(%rbp), %rdx
leaq -0x88(%rbp), %rdi
callq 0x4150
movq %rbx, -0x3a8(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0xf24e
movq %rdx, -0x3a8(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x398(%rbp)
jmp 0xf254
leaq 0x68ec(%rip), %rsi      # 0x15adf
leaq -0x3a8(%rbp), %rdi
leaq -0x68(%rbp), %rdx
callq 0x643f
leaq -0xa8(%rbp), %rdi
leaq -0x3a8(%rbp), %r15
movq %r15, %rsi
callq 0x44d0
movq (%r15), %rdi
cmpq %rbx, %rdi
je 0xf317
movq -0x398(%rbp), %rsi
jmp 0xf30f
movq %r15, %rdi
callq 0x5ba2
leaq -0x3a8(%rbp), %rdi
leaq -0x68(%rbp), %rdx
movq %rax, %rsi
callq 0x12507
jmp 0xf26e
movups (%rcx), %xmm0
movups %xmm0, (%rbx)
movq 0x8(%rax), %rdx
movq %rdx, -0x3a0(%rbp)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
leaq -0xa8(%rbp), %rdi
leaq -0x3a8(%rbp), %r15
movq %r15, %rsi
callq 0x44d0
movq (%r15), %rdi
cmpq %rbx, %rdi
je 0xf29b
movq -0x398(%rbp), %rsi
incq %rsi
callq 0x43e0
cmpb $0x2f, %r13b
leaq -0x98(%rbp), %r13
jne 0xf2c0
movq -0x88(%rbp), %rdi
cmpq %r12, %rdi
je 0xf2c0
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xa8(%rbp), %rdi
leaq -0x3a8(%rbp), %rsi
callq 0x4360
cmpl $-0x1, %eax
jne 0xf317
leaq 0x6800(%rip), %rsi      # 0x15adf
leaq -0x48(%rbp), %rdi
leaq -0x68(%rbp), %rdx
callq 0x643f
leaq -0xa8(%rbp), %rdi
leaq -0x48(%rbp), %r15
movq %r15, %rsi
callq 0x44d0
movq (%r15), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0xf317
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xa8(%rbp), %rsi
leaq -0x3a8(%rbp), %rdi
movl $0x1, %edx
callq 0x6ad0
leaq -0x3a8(%rbp), %rdi
callq 0x71ec
leaq -0x48(%rbp), %rdi
movq %rax, %rsi
callq 0x136be
leaq -0x48(%rbp), %rsi
movq %r14, %rdi
callq 0x10a86
leaq -0x38(%rbp), %rbx
movq -0x10(%rbx), %rdi
cmpq %rbx, %rdi
je 0xf36c
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x3a8(%rbp), %rdi
callq 0x704e
leaq 0x6903(%rip), %rsi      # 0x15c82
leaq -0x48(%rbp), %rdi
movq %rax, %rdx
callq 0x643f
movq -0x48(%rbp), %rsi
movq -0x40(%rbp), %rdx
movq -0xb0(%rbp), %rdi
callq 0x4150
movq -0x48(%rbp), %rdi
cmpq %rbx, %rdi
je 0xf3b4
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x3a8(%rbp), %rdi
callq 0x6fb8
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xf3d9
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xa8(%rbp), %rdi
cmpq %r13, %rdi
je 0xf3f4
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0xc0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xf413
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x43e0
addq $0x3c8, %rsp            # imm = 0x3C8
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
jmp 0xf484
movq %rax, %r14
movq -0x88(%rbp), %rdi
cmpq %r12, %rdi
je 0xf4f7
movq -0x78(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xf4f7
jmp 0xf484
jmp 0xf484
jmp 0xf484
jmp 0xf484
movq %rax, %r14
movq -0x48(%rbp), %rdi
cmpq %rbx, %rdi
jne 0xf474
jmp 0xf4eb
movq %rax, %r14
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xf4eb
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xf4eb
jmp 0xf484
movq %rax, %r14
jmp 0xf4f7
movq %rax, %r14
movq -0x3a8(%rbp), %rdi
cmpq %rbx, %rdi
je 0xf4ac
movq -0x398(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0xf4ac
movq %rax, %r14
leaq -0x78(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xf510
movq -0x78(%rbp), %rsi
jmp 0xf508
movq %rax, %r14
jmp 0xf510
movq %rax, %r14
jmp 0xf532
movq %rax, %r14
movq -0x3a8(%rbp), %rdi
cmpq %rbx, %rdi
je 0xf551
movq -0x398(%rbp), %rsi
jmp 0xf549
movq %rax, %r14
jmp 0xf551
jmp 0xf4e8
movq %rax, %r14
leaq -0x3a8(%rbp), %rdi
callq 0x6fb8
movq -0x68(%rbp), %rdi
leaq -0x58(%rbp), %rax
cmpq %rax, %rdi
je 0xf510
movq -0x58(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xa8(%rbp), %rdi
leaq -0x98(%rbp), %rax
cmpq %rax, %rdi
je 0xf532
movq -0x98(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0xc0(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0xf551
movq -0xc0(%rbp), %rsi
incq %rsi
callq 0x43e0
movq %r14, %rdi
callq 0x46d0
nop