// File: /workspace/github_cpp_makefile_star2_O1/llefranc[P]42_webserv/srcs/server/Cgi.cpp
// Function: CGI::CGI(Body*, Request*, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char>> const&)
CGI::CGI(Body *body, Request *req, const std::string &realUri, const std::string& exec) 
			: _emptyBody(body), _req(req)
{	
	std::string tmpBuf;
	
	char* pwd;
	if (!(pwd = getcwd(NULL, 0)))
		throw std::runtime_error("Error in getcwd command in cgi constructor\n");

	_realUri = pwd + ("/" + realUri);

	if (exec.compare(".cgi"))
		_exec = pwd + (CGI_PATH + exec);
	else
		_exec = _realUri;
	
	if (_exec.compare(".cgi"))
		_openArgfile.open(_realUri.c_str());

	// ** set environment variable for the CGI **
	// GET : QUERY_STRING + PATH_INFO 
	// POST : PATH_INFO + CONTENT_length 
	if ((_envvar = new char*[7]) == NULL)
		throw std::runtime_error("Error on a cgi malloc\n");
		
	int i = 0;
	_envvar[i++] = strdup(("PATH_INFO=" + _realUri).c_str());
	_envvar[i++] = strdup("SERVER_PROTOCOL=HTTP/1.1");

    // used for php-cgi
	_envvar[i++] = strdup("REDIRECT_STATUS=200");
	
	if (_req->getMethod() == GET){

        // stupid bug in php-cgi
        if (!_exec.compare("php-cgi"))
            _envvar[i++] = strdup("REQUEST_METHOD=GET");
		
		tmpBuf = "QUERY_STRING=" + _req->getQuery();
		_envvar[i++] = strdup(tmpBuf.c_str());

		if (_exec.compare(".cgi"))
		{
			while (std::getline(_openArgfile, tmpBuf))
				_getBuffFile += tmpBuf;
			
			std::stringstream intToString;
			intToString << _getBuffFile.size();
			
			tmpBuf = std::string("CONTENT_LENGTH=") + intToString.str();
			_envvar[i++] = strdup(tmpBuf.c_str());
		}

	}
	
	else {
        
        // stupid bug in php-cgi
        if (!_exec.compare("php-cgi"))
            _envvar[i++] = strdup("REQUEST_METHOD=POST");	
		
		std::stringstream intToString;
		intToString << _req->getBody().getBody().size();
		tmpBuf = std::string("CONTENT_LENGTH=") + intToString.str();
		_envvar[i++] = strdup(tmpBuf.c_str());
	}
	
	_envvar[i++] = NULL;

	// ** Set args for execve **
	if ((_args = new char*[3]) == NULL)
		throw StatusLine(500, REASON_500, "malloc failed in CGI constructor");
		
	_args[0] = strdup(_exec.c_str());
	_args[1] = (!exec.compare(".cgi")) ? NULL : strdup(_realUri.c_str());
	_args[2] = NULL;
	
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r13
pushq %r12
pushq %rbx
subq $0x248, %rsp            # imm = 0x248
movq %r8, -0xe0(%rbp)
movq %rcx, %r15
movq %rdi, %r14
movq %rsi, 0x10(%rdi)
movq %rdx, 0x18(%rdi)
leaq 0x20(%rdi), %rbx
movq %rbx, %rdi
callq 0x4490
leaq 0x230(%r14), %rax
movq %rax, -0x50(%rbp)
leaq 0x240(%r14), %rax
movq %rax, -0xd0(%rbp)
movq %rax, 0x230(%r14)
xorl %eax, %eax
movq %rax, 0x238(%r14)
movb %al, 0x240(%r14)
leaq 0x250(%r14), %rcx
movq %rcx, -0x58(%rbp)
leaq 0x260(%r14), %rcx
movq %rcx, -0xc8(%rbp)
movq %rcx, 0x250(%r14)
movq %rax, 0x258(%r14)
movb %al, 0x260(%r14)
leaq 0x270(%r14), %rcx
movq %rcx, -0xd8(%rbp)
leaq 0x280(%r14), %rcx
movq %rcx, -0xc0(%rbp)
movq %rcx, 0x270(%r14)
movq %rax, 0x278(%r14)
movb %al, 0x280(%r14)
leaq 0x2a0(%r14), %rcx
movq %rcx, 0x290(%r14)
movq %rax, 0x298(%r14)
movb %al, 0x2a0(%r14)
leaq 0x2c0(%r14), %rcx
movq %rcx, 0x2b0(%r14)
movq %rax, 0x2b8(%r14)
movb %al, 0x2c0(%r14)
leaq -0xa8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
movq %rax, -0x8(%rcx)
movb %al, (%rcx)
xorl %edi, %edi
xorl %esi, %esi
callq 0x4210
testq %rax, %rax
je 0x12f4e
movq %rax, %r12
leaq 0x2fc9(%rip), %rsi      # 0x157b6
leaq -0x48(%rbp), %rdi
movq %r15, %rdx
callq 0x6400
movq %r12, %rdi
callq 0x41e0
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r12, %rcx
movq %rax, %r8
callq 0x4440
leaq -0x260(%rbp), %r13
movq %r13, -0x10(%r13)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x12841
movq %rdx, -0x270(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x260(%rbp)
jmp 0x12849
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
leaq -0x270(%rbp), %r15
movq %rdx, 0x8(%r15)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq -0x58(%rbp), %rdi
movq %r15, %rsi
callq 0x44d0
movq (%r15), %rdi
cmpq %r13, %rdi
je 0x1288a
movq -0x260(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
movq -0xe0(%rbp), %r15
je 0x128aa
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq 0x32d1(%rip), %rsi      # 0x15b82
movq %r15, %rdi
callq 0x4130
testl %eax, %eax
je 0x12914
leaq 0x32c3(%rip), %rsi      # 0x15b87
leaq -0x48(%rbp), %rdi
movq %r15, %rdx
callq 0x6400
movq %r12, %rdi
callq 0x41e0
leaq -0x48(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
movq %r12, %rcx
movq %rax, %r8
callq 0x4440
movq %r13, -0x270(%rbp)
movq (%rax), %rdx
movq %rax, %rcx
addq $0x10, %rcx
cmpq %rcx, %rdx
je 0x12923
movq %rdx, -0x270(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x260(%rbp)
jmp 0x1292b
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x4240
jmp 0x12985
movups (%rcx), %xmm0
movups %xmm0, (%r13)
movq 0x8(%rax), %rdx
leaq -0x270(%rbp), %r15
movq %rdx, 0x8(%r15)
movq %rcx, (%rax)
movq $0x0, 0x8(%rax)
movb $0x0, 0x10(%rax)
movq -0x50(%rbp), %rdi
movq %r15, %rsi
callq 0x44d0
movq (%r15), %rdi
cmpq %r13, %rdi
je 0x1296c
movq -0x260(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x12985
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq 0x31f6(%rip), %rsi      # 0x15b82
movq -0x50(%rbp), %rdi
callq 0x4130
testl %eax, %eax
je 0x129ad
movq -0x58(%rbp), %rax
movq (%rax), %rsi
movq %rbx, %rdi
movl $0x18, %edx
callq 0x4370
movl $0x38, %edi
callq 0x4030
movq %rax, (%r14)
leaq 0x31d4(%rip), %rsi      # 0x15b95
leaq -0x270(%rbp), %rdi
movq -0x58(%rbp), %rdx
callq 0x6400
movq -0x270(%rbp), %rdi
callq 0x4730
movq (%r14), %rcx
movq %rax, (%rcx)
movq -0x270(%rbp), %rdi
cmpq %r13, %rdi
je 0x129fe
movq -0x260(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq 0x319b(%rip), %rdi      # 0x15ba0
callq 0x4730
movq (%r14), %rcx
movq %rax, 0x8(%rcx)
leaq 0x31a1(%rip), %rdi      # 0x15bb9
callq 0x4730
movq (%r14), %rcx
movq %rax, 0x10(%rcx)
movq 0x18(%r14), %rdi
callq 0xca92
movl %eax, %r15d
leaq 0x3196(%rip), %rsi      # 0x15bcd
movq -0x50(%rbp), %rdi
callq 0x4130
testl %r15d, %r15d
je 0x12b23
movl $0x3, %r12d
testl %eax, %eax
jne 0x12a6c
leaq 0x31ac(%rip), %rdi      # 0x15c06
callq 0x4730
movq (%r14), %rcx
movq %rax, 0x18(%rcx)
movl $0x4, %r12d
leaq -0x270(%rbp), %rdi
callq 0x42b0
movq 0x18(%r14), %rdi
callq 0xca7a
leaq -0x260(%rbp), %rdi
movq 0x20(%rax), %rsi
callq 0x4290
leaq -0x88(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0x3153(%rip), %rsi      # 0x15bf6
leaq 0x315b(%rip), %rdx      # 0x15c05
leaq -0x98(%rbp), %rdi
callq 0x5790
leaq -0x258(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x4610
movq -0x98(%rbp), %rcx
movq -0x90(%rbp), %r8
movq -0x70(%rbp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %esi
cmpq %r13, %rcx
je 0x12aed
movq -0x88(%rbp), %rsi
cmpq %rsi, %rax
jbe 0x12b0e
leaq -0x68(%rbp), %rdi
movl $0xf, %esi
cmpq %rdi, -0x10(%rdi)
je 0x12b05
movq -0x68(%rbp), %rsi
cmpq %rsi, %rax
jbe 0x12cd3
movq -0x78(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x4150
jmp 0x12ce0
movl $0x3, %r12d
testl %eax, %eax
jne 0x12b46
leaq 0x30a1(%rip), %rdi      # 0x15bd5
callq 0x4730
movq (%r14), %rcx
movq %rax, 0x18(%rcx)
movl $0x4, %r12d
movq 0x18(%r14), %rdi
callq 0xcaa6
leaq 0x3092(%rip), %rsi      # 0x15be8
leaq -0x270(%rbp), %rdi
movq %rax, %rdx
callq 0x6400
leaq -0xb8(%rbp), %rdi
leaq -0x270(%rbp), %r15
movq %r15, %rsi
callq 0x44d0
movq (%r15), %rdi
cmpq %r13, %rdi
je 0x12b92
movq -0x260(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xb8(%rbp), %rdi
callq 0x4730
movq (%r14), %rcx
movq %r12, %rdx
incl %r12d
movq %rdx, -0xe8(%rbp)
movl %edx, %edx
movq %rax, (%rcx,%rdx,8)
leaq 0x2fc7(%rip), %rsi      # 0x15b82
movq -0x50(%rbp), %rdi
callq 0x4130
testl %eax, %eax
movq -0xd8(%rbp), %r13
je 0x12eb1
leaq -0xb8(%rbp), %r15
movq (%rbx), %rax
movq -0x18(%rax), %rdi
addq %rbx, %rdi
movl $0xa, %esi
callq 0x43b0
movsbl %al, %edx
movq %rbx, %rdi
movq %r15, %rsi
callq 0x4740
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0x12c22
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq %r13, %rdi
callq 0x4150
jmp 0x12bda
leaq -0x270(%rbp), %rdi
callq 0x42b0
leaq -0x260(%rbp), %rdi
movq 0x278(%r14), %rsi
callq 0x4290
leaq -0x88(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0x2fa3(%rip), %rsi      # 0x15bf6
leaq 0x2fab(%rip), %rdx      # 0x15c05
leaq -0x98(%rbp), %rdi
callq 0x5790
leaq -0x258(%rbp), %rsi
leaq -0x78(%rbp), %rdi
callq 0x4610
movq -0x98(%rbp), %rcx
movq -0x90(%rbp), %r8
movq -0x70(%rbp), %rdx
leaq (%rdx,%r8), %rax
movl $0xf, %esi
cmpq %r13, %rcx
je 0x12c9d
movq -0x88(%rbp), %rsi
cmpq %rsi, %rax
jbe 0x12cbe
leaq -0x68(%rbp), %rdi
movl $0xf, %esi
cmpq %rdi, -0x10(%rdi)
je 0x12cb5
movq -0x68(%rbp), %rsi
cmpq %rsi, %rax
jbe 0x12dbf
movq -0x78(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x4150
jmp 0x12dcc
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4440
leaq -0x38(%rbp), %rsi
movq %rsi, -0x48(%rbp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
je 0x12d01
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0x12d07
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, -0x40(%rbp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq -0xb8(%rbp), %rdi
leaq -0x48(%rbp), %r15
movq %r15, %rsi
callq 0x44d0
movq (%r15), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x12d4e
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x68(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12d67
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x98(%rbp), %rdi
cmpq %r13, %rdi
je 0x12d82
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xb8(%rbp), %rdi
callq 0x4730
movq (%r14), %rcx
movl %r12d, %edx
incl %r12d
movq %rax, (%rcx,%rdx,8)
movq 0xa1ee(%rip), %rsi      # 0x1cf90
leaq -0x270(%rbp), %rdi
callq 0x42f0
leaq -0x1f0(%rbp), %rdi
callq 0x4180
jmp 0x12eb1
leaq -0x78(%rbp), %rdi
xorl %esi, %esi
xorl %edx, %edx
callq 0x4440
leaq -0x38(%rbp), %rsi
movq %rsi, -0x48(%rbp)
movq (%rax), %rdx
leaq 0x10(%rax), %rcx
cmpq %rcx, %rdx
je 0x12ded
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0x12df3
movups (%rcx), %xmm0
movups %xmm0, (%rsi)
movq %rax, %rdx
addq $0x8, %rdx
movq 0x8(%rax), %rsi
movq %rsi, -0x40(%rbp)
movq %rcx, (%rax)
movq $0x0, (%rdx)
movb $0x0, (%rcx)
leaq -0xb8(%rbp), %rdi
leaq -0x48(%rbp), %r15
movq %r15, %rsi
callq 0x44d0
movq (%r15), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x12e3a
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x68(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12e53
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x98(%rbp), %rdi
cmpq %r13, %rdi
je 0x12e6e
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0xb8(%rbp), %rdi
callq 0x4730
movq (%r14), %rcx
movq -0xe8(%rbp), %r15
addl $0x2, %r15d
movl %r12d, %edx
movq %rax, (%rcx,%rdx,8)
movq 0xa0fa(%rip), %rsi      # 0x1cf90
leaq -0x270(%rbp), %rdi
callq 0x42f0
leaq -0x1f0(%rbp), %rdi
callq 0x4180
movl %r15d, %r12d
movq (%r14), %rax
movl %r12d, %ecx
movq $0x0, (%rax,%rcx,8)
movl $0x18, %edi
callq 0x4030
movq %rax, %r15
movq %rax, 0x8(%r14)
movq 0x230(%r14), %rdi
callq 0x4730
movq %rax, (%r15)
leaq 0x2c9c(%rip), %rsi      # 0x15b82
movq -0xe0(%rbp), %rdi
callq 0x4130
testl %eax, %eax
je 0x12f04
movq -0x58(%rbp), %rax
movq (%rax), %rdi
callq 0x4730
jmp 0x12f06
xorl %eax, %eax
leaq -0xa8(%rbp), %rdx
movq 0x8(%r14), %rcx
movq %rax, 0x8(%rcx)
movq 0x8(%r14), %rax
movq $0x0, 0x10(%rax)
movq -0xb8(%rbp), %rdi
cmpq %rdx, %rdi
je 0x12f3c
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x43e0
addq $0x248, %rsp            # imm = 0x248
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
movl $0x10, %edi
callq 0x41f0
movq %rax, %r12
leaq 0x2bf4(%rip), %rsi      # 0x15b56
movq %rax, %rdi
callq 0x41b0
movq 0xa077(%rip), %rsi      # 0x1cfe8
movq 0xa040(%rip), %rdx      # 0x1cfb8
movq %r12, %rdi
callq 0x46a0
jmp 0x12fa3
jmp 0x12fc1
jmp 0x13018
jmp 0x13018
jmp 0x13040
movq %rax, %r15
movq %r12, %rdi
callq 0x42e0
jmp 0x13043
movq %rax, %r15
leaq -0x68(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12fc4
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0x12fc4
movq %rax, %r15
movq -0x98(%rbp), %rdi
cmpq %r13, %rdi
je 0x1301b
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0x1301b
jmp 0x13018
jmp 0x13040
movq %rax, %r15
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
jmp 0x13003
jmp 0x13040
jmp 0x13040
jmp 0x13040
movq %rax, %r15
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x13043
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0x13043
jmp 0x13040
movq %rax, %r15
movq 0x9f6e(%rip), %rsi      # 0x1cf90
leaq -0x270(%rbp), %rdi
callq 0x42f0
leaq -0x1f0(%rbp), %rdi
callq 0x4180
jmp 0x13043
jmp 0x13040
jmp 0x13040
movq %rax, %r15
addq $0x290, %r14            # imm = 0x290
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x1306c
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x43e0
movq %r14, %rdi
callq 0x98a0
movq -0xd8(%rbp), %rax
movq (%rax), %rdi
cmpq -0xc0(%rbp), %rdi
je 0x13099
movq -0xc0(%rbp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x43e0
movq -0x58(%rbp), %rax
movq (%rax), %rdi
cmpq -0xc8(%rbp), %rdi
je 0x130bb
movq -0xc8(%rbp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x43e0
movq -0x50(%rbp), %rax
movq (%rax), %rdi
cmpq -0xd0(%rbp), %rdi
je 0x130dd
movq -0xd0(%rbp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x43e0
movq %rbx, %rdi
callq 0x40c0
movq %r15, %rdi
callq 0x46d0
nop