// File: 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
movq %rsp, %rbp
subq $0x6b0, %rsp            # imm = 0x6B0
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq -0x8(%rbp), %rdi
addq $0x20, %rdi
callq 0x92b0
movq %rax, %rsi
leaq -0x218(%rbp), %rdi
movl $0x8, %edx
callq 0x9b80
leaq -0x3a0(%rbp), %rdi
callq 0x9490
jmp 0x20e62
leaq -0x3d0(%rbp), %rdi
callq 0x98c0
leaq -0x218(%rbp), %rdi
callq 0x99f0
movb %al, -0x5d2(%rbp)
jmp 0x20e82
movb -0x5d2(%rbp), %al
testb $0x1, %al
jne 0x20f2c
jmp 0x20e92
movl $0x30, %edi
callq 0x9340
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x5e0(%rbp)
leaq 0x49d92(%rip), %rsi     # 0x6ac42
movl $0x1f4, %edx            # imm = 0x1F4
callq 0x21920
jmp 0x20ebc
movq -0x5e0(%rbp), %rdi
leaq 0x6d876(%rip), %rsi     # 0x8e740
leaq 0xaef(%rip), %rdx       # 0x219c0
callq 0x9aa0
jmp 0x218f9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
jmp 0x218e1
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
jmp 0x218c9
movq -0x5e0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
callq 0x94c0
jmp 0x218c9
leaq -0x390(%rbp), %rax
movq %rax, -0x5f0(%rbp)
leaq -0x218(%rbp), %rdi
callq 0x99a0
movq %rax, -0x5e8(%rbp)
jmp 0x20f4f
movq -0x5e8(%rbp), %rsi
movq -0x5f0(%rbp), %rdi
callq 0x9160
jmp 0x20f64
leaq -0x3f8(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
callq 0x9660
jmp 0x20f79
leaq -0x3d0(%rbp), %rdi
leaq -0x3f8(%rbp), %rsi
movq %rsi, -0x5f8(%rbp)
callq 0x97e0
movq -0x5f8(%rbp), %rdi
callq 0x9c98
leaq -0x218(%rbp), %rdi
callq 0x9150
jmp 0x20fad
leaq -0x3d0(%rbp), %rdi
leaq 0x4ccf3(%rip), %rsi     # 0x6dcae
xorl %eax, %eax
movl %eax, %edx
callq 0x9c60
cmpq $-0x1, %rax
je 0x217dd
leaq -0x419(%rbp), %rdi
movq %rdi, -0x600(%rbp)
callq 0x9b10
movq -0x600(%rbp), %rdx
leaq 0x49d81(%rip), %rsi     # 0x6ad70
leaq -0x418(%rbp), %rdi
callq 0xa780
jmp 0x20ffd
leaq -0x3a0(%rbp), %rdi
leaq -0x418(%rbp), %rsi
callq 0x9750
jmp 0x21012
leaq -0x418(%rbp), %rdi
callq 0x9c98
leaq -0x419(%rbp), %rdi
callq 0x9700
leaq -0x3d0(%rbp), %rdi
leaq 0x49c24(%rip), %rsi     # 0x6ac5c
xorl %eax, %eax
movl %eax, %edx
callq 0x9c60
cmpq $-0x1, %rax
je 0x21216
leaq -0x390(%rbp), %rax
movq %rax, -0x618(%rbp)
leaq -0x3d0(%rbp), %rdi
movq %rdi, -0x610(%rbp)
movl $0x20, %esi
movl $0x8, %edx
movq %rdx, -0x608(%rbp)
callq 0x9860
movq -0x610(%rbp), %rsi
movq -0x608(%rbp), %rdx
movq %rax, %rcx
addq $-0x8, %rcx
leaq -0x440(%rbp), %rdi
callq 0x96b0
jmp 0x210a0
movq -0x618(%rbp), %rdi
leaq -0x440(%rbp), %rsi
callq 0x95d0
jmp 0x210b5
leaq -0x440(%rbp), %rdi
callq 0x9c98
leaq -0x3a0(%rbp), %rdi
leaq -0x3d4(%rbp), %rsi
callq 0x9440
jmp 0x210d6
movq -0x10(%rbp), %rdi
movl -0x3d4(%rbp), %esi
callq 0x64560
jmp 0x210e7
cmpl $0x190, -0x3d4(%rbp)    # imm = 0x190
jl 0x21214
leaq -0x480(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
callq 0x9660
jmp 0x2110c
leaq 0x49b52(%rip), %rsi     # 0x6ac65
leaq -0x460(%rbp), %rdi
leaq -0x480(%rbp), %rdx
callq 0x14790
jmp 0x21128
xorl %eax, %eax
movl %eax, %esi
leaq -0x460(%rbp), %rdi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x138a0
jmp 0x21144
leaq -0x460(%rbp), %rdi
callq 0x9c98
leaq -0x480(%rbp), %rdi
callq 0x9c98
movl $0x1, -0x484(%rbp)
jmp 0x2189c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
jmp 0x2119d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
leaq -0x418(%rbp), %rdi
callq 0x9c98
leaq -0x419(%rbp), %rdi
callq 0x9700
jmp 0x218c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
leaq -0x440(%rbp), %rdi
callq 0x9c98
jmp 0x218c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
jmp 0x21203
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
leaq -0x460(%rbp), %rdi
callq 0x9c98
leaq -0x480(%rbp), %rdi
callq 0x9c98
jmp 0x218c9
jmp 0x21216
jmp 0x21218
leaq -0x3d0(%rbp), %rdi
leaq 0x4ca33(%rip), %rsi     # 0x6dc59
xorl %eax, %eax
movl %eax, %edx
callq 0x9c60
movq %rax, -0x620(%rbp)
leaq -0x3d0(%rbp), %rdi
leaq 0x4ca6a(%rip), %rsi     # 0x6dcae
xorl %eax, %eax
movl %eax, %edx
callq 0x9c60
movq %rax, %rcx
movq -0x620(%rbp), %rax
cmpq %rcx, %rax
jae 0x214a8
leaq 0x4c9f2(%rip), %rsi     # 0x6dc59
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x628(%rbp)
leaq -0x3d0(%rbp), %rdi
movq %rdi, -0x630(%rbp)
callq 0x9c60
movq -0x630(%rbp), %rsi
movq -0x628(%rbp), %rdx
movq %rax, -0x490(%rbp)
movq -0x490(%rbp), %rcx
leaq -0x4b0(%rbp), %rdi
callq 0x96b0
jmp 0x212af
leaq 0x4a3f7(%rip), %rsi     # 0x6b6ad
leaq -0x4b0(%rbp), %rdi
callq 0xce40
movb %al, -0x631(%rbp)
jmp 0x212ca
leaq -0x4b0(%rbp), %rdi
callq 0x9c98
movb -0x631(%rbp), %al
testb $0x1, %al
jne 0x212e5
jmp 0x21409
movq -0x10(%rbp), %rax
movq %rax, -0x640(%rbp)
movq -0x490(%rbp), %rcx
xorl %eax, %eax
movl %eax, %edx
leaq -0x4d0(%rbp), %rdi
leaq -0x3d0(%rbp), %rsi
callq 0x96b0
jmp 0x21310
movq -0x490(%rbp), %rax
addq $0x2, %rax
movq %rax, -0x648(%rbp)
leaq 0x4c987(%rip), %rsi     # 0x6dcb0
xorl %eax, %eax
movl %eax, %edx
leaq -0x3d0(%rbp), %rdi
movq %rdi, -0x650(%rbp)
callq 0x9c60
movq -0x650(%rbp), %rsi
movq -0x648(%rbp), %rdx
movq %rax, %rcx
movq -0x490(%rbp), %rax
subq %rax, %rcx
addq $-0x2, %rcx
leaq -0x4f0(%rbp), %rdi
callq 0x96b0
jmp 0x2136d
movq -0x640(%rbp), %rdi
leaq -0x4d0(%rbp), %rsi
leaq -0x4f0(%rbp), %rdx
callq 0x63770
jmp 0x21389
leaq -0x4f0(%rbp), %rdi
callq 0x9c98
leaq -0x4d0(%rbp), %rdi
callq 0x9c98
jmp 0x21409
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
leaq -0x4b0(%rbp), %rdi
callq 0x9c98
jmp 0x218c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
jmp 0x213f8
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
leaq -0x4f0(%rbp), %rdi
callq 0x9c98
leaq -0x4d0(%rbp), %rdi
callq 0x9c98
jmp 0x218c9
leaq 0x4c8a0(%rip), %rsi     # 0x6dcb0
xorl %eax, %eax
movl %eax, %edx
movq %rdx, -0x658(%rbp)
leaq -0x3d0(%rbp), %rdi
movq %rdi, -0x660(%rbp)
callq 0x9c60
movq -0x660(%rbp), %rdi
movq -0x658(%rbp), %rsi
movq %rax, %rdx
callq 0x9180
jmp 0x21446
leaq -0x3d0(%rbp), %rdi
leaq 0x4c805(%rip), %rsi     # 0x6dc59
xorl %eax, %eax
movl %eax, %edx
callq 0x9c60
movq %rax, -0x668(%rbp)
leaq -0x3d0(%rbp), %rdi
leaq 0x4c83c(%rip), %rsi     # 0x6dcae
xorl %eax, %eax
movl %eax, %edx
callq 0x9c60
movq %rax, %rcx
movq -0x668(%rbp), %rax
cmpq %rcx, %rax
jae 0x214a3
xorl %eax, %eax
movl %eax, %esi
leaq -0x3d0(%rbp), %rdi
movl $0x2, %edx
callq 0x9180
jmp 0x214a1
jmp 0x214a3
jmp 0x21218
xorl %eax, %eax
movl %eax, %esi
leaq -0x3d0(%rbp), %rdi
movl $0x4, %edx
callq 0x9180
jmp 0x214bf
movq -0x3a0(%rbp), %rax
movq -0x18(%rax), %rax
leaq -0x3a0(%rbp,%rax), %rdi
xorl %esi, %esi
callq 0x9a70
jmp 0x214db
leaq -0x511(%rbp), %rdi
movq %rdi, -0x670(%rbp)
callq 0x9b10
movq -0x670(%rbp), %rdx
leaq 0x49874(%rip), %rsi     # 0x6ad70
leaq -0x510(%rbp), %rdi
callq 0xa780
jmp 0x2150a
leaq -0x3a0(%rbp), %rdi
leaq -0x510(%rbp), %rsi
callq 0x9750
jmp 0x2151f
leaq -0x510(%rbp), %rdi
callq 0x9c98
leaq -0x511(%rbp), %rdi
callq 0x9700
leaq -0x390(%rbp), %rax
movq %rax, -0x678(%rbp)
leaq -0x3d0(%rbp), %rdi
callq 0x9480
movq -0x678(%rbp), %rdi
movq %rax, %rsi
callq 0x9190
jmp 0x21562
movq -0x10(%rbp), %rax
movq %rax, -0x688(%rbp)
leaq -0x539(%rbp), %rdi
movq %rdi, -0x680(%rbp)
callq 0x9b10
movq -0x680(%rbp), %rdx
leaq 0x48b76(%rip), %rsi     # 0x6a104
leaq -0x538(%rbp), %rdi
callq 0xa780
jmp 0x2159c
leaq -0x560(%rbp), %rdi
leaq -0x3a0(%rbp), %rsi
callq 0x9660
jmp 0x215b1
movq -0x688(%rbp), %rdi
leaq -0x538(%rbp), %rsi
leaq -0x560(%rbp), %rdx
callq 0x63770
jmp 0x215cd
leaq -0x560(%rbp), %rdi
callq 0x9c98
leaq -0x538(%rbp), %rdi
callq 0x9c98
leaq -0x539(%rbp), %rdi
callq 0x9700
movq -0x10(%rbp), %rdi
callq 0x63a60
jmp 0x215fc
movq -0x10(%rbp), %rax
movq %rax, -0x698(%rbp)
leaq -0x581(%rbp), %rdi
movq %rdi, -0x690(%rbp)
callq 0x9b10
movq -0x690(%rbp), %rdx
leaq 0x48c24(%rip), %rsi     # 0x6a24c
leaq -0x580(%rbp), %rdi
callq 0xa780
jmp 0x21636
leaq -0x5a9(%rbp), %rdi
movq %rdi, -0x6a0(%rbp)
callq 0x9b10
movq -0x6a0(%rbp), %rdx
leaq 0x4955a(%rip), %rsi     # 0x6abb1
leaq -0x5a8(%rbp), %rdi
callq 0xa780
jmp 0x21665
movq -0x698(%rbp), %rdi
leaq -0x580(%rbp), %rsi
leaq -0x5a8(%rbp), %rdx
callq 0x63770
jmp 0x21681
leaq -0x5a8(%rbp), %rdi
callq 0x9c98
leaq -0x5a9(%rbp), %rdi
callq 0x9700
leaq -0x580(%rbp), %rdi
callq 0x9c98
leaq -0x581(%rbp), %rdi
callq 0x9700
movq -0x10(%rbp), %rdi
leaq -0x3d0(%rbp), %rsi
callq 0x645d0
jmp 0x216c3
jmp 0x21892
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
jmp 0x216fa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
leaq -0x510(%rbp), %rdi
callq 0x9c98
leaq -0x511(%rbp), %rdi
callq 0x9700
jmp 0x218c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
jmp 0x2175d
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
jmp 0x21751
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
leaq -0x560(%rbp), %rdi
callq 0x9c98
leaq -0x538(%rbp), %rdi
callq 0x9c98
leaq -0x539(%rbp), %rdi
callq 0x9700
jmp 0x218c9
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
jmp 0x217cc
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
jmp 0x217b4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
leaq -0x5a8(%rbp), %rdi
callq 0x9c98
leaq -0x5a9(%rbp), %rdi
callq 0x9700
leaq -0x580(%rbp), %rdi
callq 0x9c98
leaq -0x581(%rbp), %rdi
callq 0x9700
jmp 0x218c9
leaq -0x5d1(%rbp), %rdi
movq %rdi, -0x6a8(%rbp)
callq 0x9b10
movq -0x6a8(%rbp), %rdx
leaq 0x4947c(%rip), %rsi     # 0x6ac7a
leaq -0x5d0(%rbp), %rdi
callq 0xa780
jmp 0x2180c
xorl %eax, %eax
movl %eax, %esi
leaq -0x5d0(%rbp), %rdi
movl $0x3, %edx
movl $0x1, %ecx
callq 0x138a0
jmp 0x21828
leaq -0x5d0(%rbp), %rdi
callq 0x9c98
leaq -0x5d1(%rbp), %rdi
callq 0x9700
movq -0x10(%rbp), %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0x64560
jmp 0x21850
jmp 0x21892
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
jmp 0x21884
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x3a8(%rbp)
movl %eax, -0x3ac(%rbp)
leaq -0x5d0(%rbp), %rdi
callq 0x9c98
leaq -0x5d1(%rbp), %rdi
callq 0x9700
jmp 0x218c9
movl $0x0, -0x484(%rbp)
leaq -0x3d0(%rbp), %rdi
callq 0x9c98
leaq -0x3a0(%rbp), %rdi
callq 0x94e0
leaq -0x218(%rbp), %rdi
callq 0x91d0
addq $0x6b0, %rsp            # imm = 0x6B0
popq %rbp
retq
leaq -0x3d0(%rbp), %rdi
callq 0x9c98
leaq -0x3a0(%rbp), %rdi
callq 0x94e0
leaq -0x218(%rbp), %rdi
callq 0x91d0
movq -0x3a8(%rbp), %rdi
callq 0x9af0
nopl (%rax)