// File: /workspace/github_cpp_makefile_star2_O2/mbourand[P]webserv/srcs/Utils/CGI.cpp
// Function: CGI::process(Response&)
void	CGI::process(Response& response)
{
	std::ifstream		inputFileStream(m_tmpfilename.c_str());
	std::stringstream	convert;
	std::string			content;
	int					status;

	if (!inputFileStream.is_open())
		throw CGI::CGIException("Couldn't read tmp buffer.", 500);
	convert << inputFileStream.rdbuf();
	content = convert.str();
	inputFileStream.close();
	if (content.find("\r\n\r\n") != std::string::npos)
	{
		convert.str("");
		if (content.find("Status: ") != std::string::npos)
		{
			convert << content.substr(8, content.find(' ', 8) - 8);
			convert >> status;
			response.setCode(status);
			if (status >= 400)
			{
				Logger::print("CGI returned status "+convert.str(), NULL, ERROR, NORMAL);
				return;
			}
		}
		while (content.find(": ") < content.find("\r\n\r\n"))
		{
			size_t found = content.find(": ");
			if (content.substr(0, found) != "Status")
			response.addHeader(content.substr(0, found), content.substr(found + 2, (content.find("\r\n")) - found - 2));
			content.erase(0, content.find("\r\n"));
			if (content.find(": ") < content.find("\r\n\r\n"))
				content.erase(0, 2);
		}
		content.erase(0, 4);
		convert.clear();
		convert.str("");
		convert << content.size();
		response.addHeader("Content-Length", convert.str());
		response.addDateHeader();
		response.addHeader("Server", "Webserv");
		response.setBody(content);
	}
	else
	{
		Logger::print("CGI produced invalid headers.", NULL, ERROR, NORMAL);
		response.setCode(500);
	}
}

// Assembly:
pushq %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x448, %rsp            # imm = 0x448
movq %rsi, %rbx
movq 0x20(%rdi), %rsi
leaq 0x240(%rsp), %rdi
pushq $0x8
popq %rdx
callq 0x78d0
leaq 0xb8(%rsp), %r12
movq %r12, %rdi
callq 0x73a0
leaq 0x48(%rsp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq 0x2b8(%rsp), %rdi
callq 0x7130
testb %al, %al
je 0x11e25
leaq 0xc8(%rsp), %rdi
leaq 0x250(%rsp), %rsi
callq 0x7100
leaq 0xd0(%rsp), %r14
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x7780
leaq 0x38(%rsp), %rdi
leaq 0x10(%rsp), %r13
movq %r13, %rsi
callq 0x75f0
movq %r13, %rdi
callq 0x79b8
leaq 0x240(%rsp), %rdi
callq 0x70f0
leaq 0x1de60(%rip), %rsi     # 0x2f87e
leaq 0x38(%rsp), %rdi
xorl %edx, %edx
callq 0x7980
cmpq $-0x1, %rax
je 0x11db0
leaq 0x1b314(%rip), %rsi     # 0x2cd4f
leaq 0x10(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x8032
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x7720
leaq 0x10(%rsp), %rdi
callq 0x79b8
leaq 0x1b1d3(%rip), %rsi     # 0x2cc3b
leaq 0x38(%rsp), %rdi
xorl %edx, %edx
callq 0x7980
cmpq $-0x1, %rax
je 0x11b43
leaq 0x38(%rsp), %r13
pushq $0x20
popq %rsi
pushq $0x8
popq %rbp
movq %r13, %rdi
movq %rbp, %rdx
callq 0x7650
leaq -0x8(%rax), %rcx
leaq 0x10(%rsp), %rdi
movq %r13, %rsi
movq %rbp, %rdx
callq 0x7540
leaq 0x10(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x74a0
leaq 0x10(%rsp), %rdi
callq 0x79b8
leaq 0xb8(%rsp), %rdi
leaq 0x34(%rsp), %rsi
callq 0x7360
movl 0x34(%rsp), %esi
movq %rbx, %rdi
callq 0x2a818
cmpl $0x190, 0x34(%rsp)      # imm = 0x190
jl 0x11b43
leaq 0x10(%rsp), %rdi
movq %r14, %rsi
callq 0x7780
leaq 0x1b144(%rip), %rsi     # 0x2cc44
leaq 0x98(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0xc30e
leaq 0x98(%rsp), %rdi
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %esi, %esi
callq 0xbe10
leaq 0x98(%rsp), %rdi
callq 0x79b8
leaq 0x10(%rsp), %rdi
callq 0x79b8
jmp 0x11def
leaq 0x1dcdf(%rip), %rsi     # 0x2f829
leaq 0x38(%rsp), %r13
movq %r13, %rdi
xorl %edx, %edx
callq 0x7980
movq %rax, %rbp
leaq 0x1dd1b(%rip), %rsi     # 0x2f87e
movq %r13, %rdi
xorl %edx, %edx
callq 0x7980
cmpq %rax, %rbp
jae 0x11c9a
leaq 0x1dcac(%rip), %rsi     # 0x2f829
leaq 0x38(%rsp), %rbp
movq %rbp, %rdi
xorl %edx, %edx
callq 0x7980
movq %rax, %r13
leaq 0x10(%rsp), %rdi
movq %rbp, %rsi
xorl %edx, %edx
movq %rax, %rcx
callq 0x7540
leaq 0x1b6dd(%rip), %rsi     # 0x2d285
leaq 0x10(%rsp), %rbp
movq %rbp, %rdi
callq 0x8f4f
movl %eax, %r15d
movq %rbp, %rdi
callq 0x79b8
testb %r15b, %r15b
je 0x11c30
leaq 0x10(%rsp), %rdi
leaq 0x38(%rsp), %rsi
xorl %edx, %edx
movq %r13, %rcx
callq 0x7540
leaq 0x2(%r13), %r15
leaq 0x1dc9c(%rip), %rsi     # 0x2f880
leaq 0x38(%rsp), %rbp
movq %rbp, %rdi
xorl %edx, %edx
callq 0x7980
subq %r13, %rax
leaq -0x2(%rax), %rcx
leaq 0x58(%rsp), %rdi
movq %rbp, %rsi
movq %r15, %rdx
callq 0x7540
leaq 0x10(%rsp), %rsi
leaq 0x58(%rsp), %rdx
movq %rbx, %rdi
callq 0x2a124
leaq 0x58(%rsp), %rdi
callq 0x79b8
leaq 0x10(%rsp), %rdi
callq 0x79b8
leaq 0x1dc49(%rip), %rsi     # 0x2f880
leaq 0x38(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x7980
movq %r15, %rdi
xorl %esi, %esi
movq %rax, %rdx
callq 0x7120
leaq 0x1dbcf(%rip), %rsi     # 0x2f829
leaq 0x38(%rsp), %r15
movq %r15, %rdi
xorl %edx, %edx
callq 0x7980
movq %rax, %r13
leaq 0x1dc0b(%rip), %rsi     # 0x2f87e
movq %r15, %rdi
xorl %edx, %edx
callq 0x7980
cmpq %rax, %r13
jae 0x11b43
leaq 0x38(%rsp), %rdi
pushq $0x2
popq %rdx
xorl %esi, %esi
callq 0x7120
jmp 0x11b43
leaq 0x38(%rsp), %rdi
pushq $0x4
popq %rdx
xorl %esi, %esi
callq 0x7120
movq 0xb8(%rsp), %rax
addq -0x18(%rax), %r12
movq %r12, %rdi
xorl %esi, %esi
callq 0x7810
leaq 0x1b089(%rip), %rsi     # 0x2cd4f
leaq 0x10(%rsp), %rdi
leaq 0x58(%rsp), %rdx
callq 0x8032
leaq 0x10(%rsp), %rsi
movq %r14, %rdi
callq 0x7720
leaq 0x10(%rsp), %rdi
callq 0x79b8
movq 0x40(%rsp), %rsi
leaq 0xc8(%rsp), %rdi
callq 0x7370
leaq 0x1a3ff(%rip), %rsi     # 0x2c104
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x8032
leaq 0x58(%rsp), %rdi
movq %r14, %rsi
callq 0x7780
leaq 0x10(%rsp), %rsi
leaq 0x58(%rsp), %rdx
movq %rbx, %rdi
callq 0x2a124
leaq 0x58(%rsp), %rdi
callq 0x79b8
leaq 0x10(%rsp), %rdi
callq 0x79b8
movq %rbx, %rdi
callq 0x2a2f0
leaq 0x1a4f6(%rip), %rsi     # 0x2c24c
leaq 0x10(%rsp), %rdi
leaq 0xf(%rsp), %rdx
callq 0x8032
leaq 0x1ae24(%rip), %rsi     # 0x2cb90
leaq 0x58(%rsp), %rdi
leaq 0x33(%rsp), %rdx
callq 0x8032
leaq 0x10(%rsp), %rsi
leaq 0x58(%rsp), %rdx
movq %rbx, %rdi
callq 0x2a124
leaq 0x58(%rsp), %rdi
callq 0x79b8
leaq 0x10(%rsp), %rdi
callq 0x79b8
leaq 0x38(%rsp), %rsi
movq %rbx, %rdi
callq 0x2a82e
jmp 0x11def
leaq 0x1aea2(%rip), %rsi     # 0x2cc59
leaq 0x78(%rsp), %rdi
leaq 0x10(%rsp), %rdx
callq 0x8032
leaq 0x78(%rsp), %rdi
pushq $0x3
popq %rdx
pushq $0x1
popq %rcx
xorl %esi, %esi
callq 0xbe10
leaq 0x78(%rsp), %rdi
callq 0x79b8
movq %rbx, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0x2a818
leaq 0x38(%rsp), %rdi
callq 0x79b8
leaq 0xb8(%rsp), %rdi
callq 0x73f0
leaq 0x240(%rsp), %rdi
callq 0x7170
addq $0x448, %rsp            # imm = 0x448
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x30
popq %rdi
callq 0x7290
movq %rax, %r14
leaq 0x1adea(%rip), %rsi     # 0x2cc21
movq %rax, %rdi
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x11ef6
leaq 0x30905(%rip), %rsi     # 0x42750
leaq 0xe8(%rip), %rdx        # 0x11f3a
movq %r14, %rdi
callq 0x7830
movq %rax, %rbx
leaq 0x98(%rsp), %rdi
jmp 0x11eaf
jmp 0x11eb6
jmp 0x11ec7
movq %rax, %rbx
leaq 0x78(%rsp), %rdi
jmp 0x11ebe
jmp 0x11ec7
jmp 0x11eb6
jmp 0x11ec7
jmp 0x11ea7
jmp 0x11eb6
jmp 0x11ec7
jmp 0x11ea7
jmp 0x11eb6
jmp 0x11ec7
jmp 0x11eb6
jmp 0x11ec7
movq %rax, %rbx
movq %r14, %rdi
callq 0x73d0
jmp 0x11eca
jmp 0x11eb6
jmp 0x11ec7
jmp 0x11ec7
movq %rax, %rbx
jmp 0x11ee1
jmp 0x11ec7
jmp 0x11ec7
movq %rax, %rbx
leaq 0x58(%rsp), %rdi
callq 0x79b8
jmp 0x11eb9
movq %rax, %rbx
leaq 0x10(%rsp), %rdi
callq 0x79b8
jmp 0x11eca
jmp 0x11ec7
movq %rax, %rbx
leaq 0x38(%rsp), %rdi
callq 0x79b8
leaq 0xb8(%rsp), %rdi
callq 0x73f0
leaq 0x240(%rsp), %rdi
callq 0x7170
movq %rbx, %rdi
callq 0x7860