// File: 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
subq $0x620, %rsp            # imm = 0x620
movq %rdi, -0x8(%rbp)
movq %rsi, -0x10(%rbp)
movq %rdx, -0x18(%rbp)
movq %rcx, -0x20(%rbp)
movq %r8, -0x28(%rbp)
movq -0x8(%rbp), %rdi
movq %rdi, -0x540(%rbp)
movq -0x10(%rbp), %rax
movq %rax, 0x10(%rdi)
movq -0x18(%rbp), %rax
movq %rax, 0x18(%rdi)
addq $0x20, %rdi
movq %rdi, -0x560(%rbp)
callq 0x5630
movq -0x540(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
movq %rdi, -0x558(%rbp)
callq 0x5770
movq -0x540(%rbp), %rdi
addq $0x250, %rdi            # imm = 0x250
movq %rdi, -0x550(%rbp)
callq 0x5770
movq -0x540(%rbp), %rdi
addq $0x270, %rdi            # imm = 0x270
movq %rdi, -0x548(%rbp)
callq 0x5770
movq -0x540(%rbp), %rdi
addq $0x290, %rdi            # imm = 0x290
movq %rdi, -0x538(%rbp)
callq 0x20a60
jmp 0x1f5c4
leaq -0x58(%rbp), %rdi
callq 0x5770
xorl %eax, %eax
movl %eax, %esi
movq %rsi, %rdi
callq 0x52c0
movq %rax, -0x60(%rbp)
cmpq $0x0, %rax
jne 0x1f66a
movl $0x10, %edi
callq 0x5290
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x568(%rbp)
leaq 0x450c(%rip), %rsi      # 0x23b11
callq 0x5210
jmp 0x1f60c
movq -0x568(%rbp), %rdi
movq 0x119d6(%rip), %rsi     # 0x30ff0
movq 0x119a7(%rip), %rdx     # 0x30fc8
callq 0x58a0
jmp 0x200c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x20089
movq -0x568(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x53f0
jmp 0x20074
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x20074
movq -0x60(%rbp), %rax
movq %rax, -0x570(%rbp)
movq -0x20(%rbp), %rdx
leaq 0x413c(%rip), %rsi      # 0x237bc
leaq -0xa0(%rbp), %rdi
callq 0x9a60
jmp 0x1f68e
movq -0x570(%rbp), %rsi
leaq -0x80(%rbp), %rdi
leaq -0xa0(%rbp), %rdx
callq 0x6700
jmp 0x1f6a7
movq -0x540(%rbp), %rdi
addq $0x250, %rdi            # imm = 0x250
leaq -0x80(%rbp), %rsi
callq 0x5670
leaq -0x80(%rbp), %rdi
callq 0x5a08
leaq -0xa0(%rbp), %rdi
callq 0x5a08
movq -0x28(%rbp), %rdi
leaq 0x445f(%rip), %rsi      # 0x23b3d
callq 0x51b0
cmpl $0x0, %eax
je 0x1f79a
movq -0x60(%rbp), %rax
movq %rax, -0x578(%rbp)
movq -0x28(%rbp), %rdx
leaq 0x4440(%rip), %rsi      # 0x23b42
leaq -0xe0(%rbp), %rdi
callq 0x9a60
jmp 0x1f710
movq -0x578(%rbp), %rsi
leaq -0xc0(%rbp), %rdi
leaq -0xe0(%rbp), %rdx
callq 0x6700
jmp 0x1f72c
movq -0x540(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
leaq -0xc0(%rbp), %rsi
callq 0x5670
leaq -0xc0(%rbp), %rdi
callq 0x5a08
leaq -0xe0(%rbp), %rdi
callq 0x5a08
jmp 0x1f7bb
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xa0(%rbp), %rdi
callq 0x5a08
jmp 0x20074
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0xe0(%rbp), %rdi
callq 0x5a08
jmp 0x20074
movq -0x540(%rbp), %rdi
movq %rdi, %rsi
addq $0x250, %rsi            # imm = 0x250
addq $0x230, %rdi            # imm = 0x230
callq 0x5060
jmp 0x1f7b9
jmp 0x1f7bb
movq -0x540(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
leaq 0x436d(%rip), %rsi      # 0x23b3d
callq 0x51b0
cmpl $0x0, %eax
je 0x1f836
movq -0x540(%rbp), %rdi
movq %rdi, %rax
addq $0x20, %rax
movq %rax, -0x590(%rbp)
addq $0x250, %rdi            # imm = 0x250
callq 0x5240
movq %rax, -0x588(%rbp)
movl $0x8, %edi
movl $0x10, %esi
callq 0x20a90
movl %eax, -0x57c(%rbp)
jmp 0x1f819
movl -0x57c(%rbp), %edx
movq -0x588(%rbp), %rsi
movq -0x590(%rbp), %rdi
callq 0x54d0
jmp 0x1f834
jmp 0x1f836
movl $0x38, %edi
callq 0x5030
movq %rax, -0x598(%rbp)
jmp 0x1f849
movq -0x598(%rbp), %rax
movq -0x540(%rbp), %rcx
movq %rax, (%rcx)
cmpq $0x0, %rax
jne 0x1f8c1
movl $0x10, %edi
callq 0x5290
movq %rax, %rdi
movq %rdi, %rax
movq %rax, -0x5a0(%rbp)
leaq 0x42d2(%rip), %rsi      # 0x23b50
callq 0x5210
jmp 0x1f885
movq -0x5a0(%rbp), %rdi
movq 0x1175d(%rip), %rsi     # 0x30ff0
movq 0x1172e(%rip), %rdx     # 0x30fc8
callq 0x58a0
jmp 0x200c2
movq -0x5a0(%rbp), %rdi
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
callq 0x53f0
jmp 0x20074
movq -0x540(%rbp), %rdx
movl $0x0, -0xe4(%rbp)
addq $0x250, %rdx            # imm = 0x250
leaq 0x4287(%rip), %rsi      # 0x23b67
leaq -0x108(%rbp), %rdi
callq 0x9a60
jmp 0x1f8ee
leaq -0x108(%rbp), %rdi
movq %rdi, -0x5b0(%rbp)
callq 0x5240
movq %rax, %rdi
callq 0x5960
movq -0x5b0(%rbp), %rdi
movq %rax, %rdx
movq -0x540(%rbp), %rax
movq (%rax), %rax
movslq -0xe4(%rbp), %rcx
movl %ecx, %esi
incl %esi
movl %esi, -0xe4(%rbp)
movq %rdx, (%rax,%rcx,8)
callq 0x5a08
leaq 0x4234(%rip), %rdi      # 0x23b72
callq 0x5960
movq %rax, %rdx
movq -0x540(%rbp), %rax
movq (%rax), %rax
movslq -0xe4(%rbp), %rcx
movl %ecx, %esi
incl %esi
movl %esi, -0xe4(%rbp)
movq %rdx, (%rax,%rcx,8)
leaq 0x421f(%rip), %rdi      # 0x23b8b
callq 0x5960
movq %rax, %rsi
movq -0x540(%rbp), %rax
movq (%rax), %rcx
movslq -0xe4(%rbp), %rdx
movl %edx, %edi
incl %edi
movl %edi, -0xe4(%rbp)
movq %rsi, (%rcx,%rdx,8)
movq 0x18(%rax), %rdi
callq 0x174a0
movl %eax, -0x5a4(%rbp)
jmp 0x1f9a4
movl -0x5a4(%rbp), %eax
cmpl $0x0, %eax
jne 0x1fc9e
movq -0x540(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
leaq 0x41d7(%rip), %rsi      # 0x23b9f
callq 0x51b0
cmpl $0x0, %eax
jne 0x1fa03
leaq 0x41ce(%rip), %rdi      # 0x23ba7
callq 0x5960
movq %rax, %rdx
movq -0x540(%rbp), %rax
movq (%rax), %rax
movl -0xe4(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0xe4(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
movq -0x540(%rbp), %rax
movq 0x18(%rax), %rdi
callq 0x174e0
movq %rax, -0x5b8(%rbp)
jmp 0x1fa1c
movq -0x5b8(%rbp), %rdx
leaq 0x4190(%rip), %rsi      # 0x23bba
leaq -0x128(%rbp), %rdi
callq 0x9a60
jmp 0x1fa38
leaq -0x58(%rbp), %rdi
leaq -0x128(%rbp), %rsi
callq 0x5670
leaq -0x128(%rbp), %rdi
callq 0x5a08
leaq -0x58(%rbp), %rdi
callq 0x5240
movq %rax, %rdi
callq 0x5960
movq -0x540(%rbp), %rdi
movq %rax, %rdx
movq (%rdi), %rax
movl -0xe4(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0xe4(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
addq $0x230, %rdi            # imm = 0x230
leaq 0x40a5(%rip), %rsi      # 0x23b3d
callq 0x51b0
cmpl $0x0, %eax
je 0x1fc99
jmp 0x1faa8
movq -0x540(%rbp), %rdi
addq $0x20, %rdi
leaq -0x58(%rbp), %rsi
callq 0x50f0
movq %rax, -0x5c0(%rbp)
jmp 0x1fac5
movq -0x5c0(%rbp), %rdi
movq (%rdi), %rax
movq -0x18(%rax), %rax
addq %rax, %rdi
callq 0x56a0
movb %al, -0x5c1(%rbp)
jmp 0x1fae3
movb -0x5c1(%rbp), %al
testb $0x1, %al
jne 0x1faef
jmp 0x1fb0a
movq -0x540(%rbp), %rdi
addq $0x270, %rdi            # imm = 0x270
leaq -0x58(%rbp), %rsi
callq 0x56b0
jmp 0x1fb08
jmp 0x1faa8
leaq -0x2b0(%rbp), %rdi
callq 0x53c0
jmp 0x1fb18
movq -0x540(%rbp), %rdi
leaq -0x2a0(%rbp), %rax
movq %rax, -0x5d0(%rbp)
addq $0x270, %rdi            # imm = 0x270
callq 0x53b0
movq -0x5d0(%rbp), %rdi
movq %rax, %rsi
callq 0x5140
jmp 0x1fb4a
leaq -0x2f1(%rbp), %rdi
movq %rdi, -0x5d8(%rbp)
callq 0x58f0
movq -0x5d8(%rbp), %rdx
leaq 0x405d(%rip), %rsi      # 0x23bc8
leaq -0x2f0(%rbp), %rdi
callq 0x63e0
jmp 0x1fb79
leaq -0x318(%rbp), %rdi
leaq -0x2b0(%rbp), %rsi
callq 0x5530
jmp 0x1fb8e
leaq -0x2d0(%rbp), %rdi
leaq -0x2f0(%rbp), %rsi
leaq -0x318(%rbp), %rdx
callq 0x14710
jmp 0x1fbaa
leaq -0x58(%rbp), %rdi
leaq -0x2d0(%rbp), %rsi
callq 0x5670
leaq -0x2d0(%rbp), %rdi
callq 0x5a08
leaq -0x318(%rbp), %rdi
callq 0x5a08
leaq -0x2f0(%rbp), %rdi
callq 0x5a08
leaq -0x2f1(%rbp), %rdi
callq 0x55c0
leaq -0x58(%rbp), %rdi
callq 0x5240
movq %rax, %rdi
callq 0x5960
movq %rax, %rdx
movq -0x540(%rbp), %rax
movq (%rax), %rax
movl -0xe4(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0xe4(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
leaq -0x2b0(%rbp), %rdi
callq 0x5410
jmp 0x1fc99
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x1fc88
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x1fc7c
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x1fc70
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x318(%rbp), %rdi
callq 0x5a08
leaq -0x2f0(%rbp), %rdi
callq 0x5a08
leaq -0x2f1(%rbp), %rdi
callq 0x55c0
leaq -0x2b0(%rbp), %rdi
callq 0x5410
jmp 0x20074
jmp 0x1fea4
movq -0x540(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
leaq 0x3eec(%rip), %rsi      # 0x23b9f
callq 0x51b0
cmpl $0x0, %eax
jne 0x1fcee
leaq 0x3f14(%rip), %rdi      # 0x23bd8
callq 0x5960
movq %rax, %rdx
movq -0x540(%rbp), %rax
movq (%rax), %rax
movl -0xe4(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0xe4(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
leaq -0x4a0(%rbp), %rdi
callq 0x53c0
jmp 0x1fcfc
movq -0x540(%rbp), %rax
leaq -0x490(%rbp), %rcx
movq %rcx, -0x5e8(%rbp)
movq 0x18(%rax), %rdi
callq 0x17470
movq %rax, -0x5e0(%rbp)
jmp 0x1fd23
movq -0x5e0(%rbp), %rdi
callq 0x19c90
movq %rax, -0x5f0(%rbp)
jmp 0x1fd38
movq -0x5f0(%rbp), %rdi
callq 0x53b0
movq -0x5e8(%rbp), %rdi
movq %rax, %rsi
callq 0x5140
jmp 0x1fd55
leaq -0x4e1(%rbp), %rdi
movq %rdi, -0x5f8(%rbp)
callq 0x58f0
movq -0x5f8(%rbp), %rdx
leaq 0x3e52(%rip), %rsi      # 0x23bc8
leaq -0x4e0(%rbp), %rdi
callq 0x63e0
jmp 0x1fd84
leaq -0x508(%rbp), %rdi
leaq -0x4a0(%rbp), %rsi
callq 0x5530
jmp 0x1fd99
leaq -0x4c0(%rbp), %rdi
leaq -0x4e0(%rbp), %rsi
leaq -0x508(%rbp), %rdx
callq 0x14710
jmp 0x1fdb5
leaq -0x58(%rbp), %rdi
leaq -0x4c0(%rbp), %rsi
callq 0x5670
leaq -0x4c0(%rbp), %rdi
callq 0x5a08
leaq -0x508(%rbp), %rdi
callq 0x5a08
leaq -0x4e0(%rbp), %rdi
callq 0x5a08
leaq -0x4e1(%rbp), %rdi
callq 0x55c0
leaq -0x58(%rbp), %rdi
callq 0x5240
movq %rax, %rdi
callq 0x5960
movq %rax, %rdx
movq -0x540(%rbp), %rax
movq (%rax), %rax
movl -0xe4(%rbp), %ecx
movl %ecx, %esi
addl $0x1, %esi
movl %esi, -0xe4(%rbp)
movslq %ecx, %rcx
movq %rdx, (%rax,%rcx,8)
leaq -0x4a0(%rbp), %rdi
callq 0x5410
jmp 0x1fea4
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x1fe93
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x1fe87
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x1fe7b
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x508(%rbp), %rdi
callq 0x5a08
leaq -0x4e0(%rbp), %rdi
callq 0x5a08
leaq -0x4e1(%rbp), %rdi
callq 0x55c0
leaq -0x4a0(%rbp), %rdi
callq 0x5410
jmp 0x20074
movq -0x540(%rbp), %rax
movq (%rax), %rax
movslq -0xe4(%rbp), %rcx
movl %ecx, %edx
incl %edx
movl %edx, -0xe4(%rbp)
movq $0x0, (%rax,%rcx,8)
movl $0x18, %edi
callq 0x5030
movq %rax, -0x600(%rbp)
jmp 0x1feda
movq -0x600(%rbp), %rax
movq -0x540(%rbp), %rcx
movq %rax, 0x8(%rcx)
cmpq $0x0, %rax
jne 0x1ffd2
movb $0x1, -0x52a(%rbp)
movl $0x48, %edi
callq 0x5290
movq %rax, -0x610(%rbp)
leaq -0x529(%rbp), %rdi
movq %rdi, -0x608(%rbp)
callq 0x58f0
movq -0x608(%rbp), %rdx
leaq 0x3cbd(%rip), %rsi      # 0x23bec
leaq -0x528(%rbp), %rdi
callq 0x63e0
jmp 0x1ff3d
movq -0x610(%rbp), %rdi
leaq 0x3b5d(%rip), %rdx      # 0x23aa8
movl $0x1f4, %esi            # imm = 0x1F4
leaq -0x528(%rbp), %rcx
callq 0x199d0
jmp 0x1ff5e
movq -0x610(%rbp), %rdi
movb $0x0, -0x52a(%rbp)
leaq 0x10e1d(%rip), %rsi     # 0x30d90
leaq -0x64fa(%rip), %rdx     # 0x19a80
callq 0x58a0
jmp 0x200c2
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
jmp 0x1ffaa
movq %rax, %rcx
movl %edx, %eax
movq %rcx, -0x30(%rbp)
movl %eax, -0x34(%rbp)
leaq -0x528(%rbp), %rdi
callq 0x5a08
leaq -0x529(%rbp), %rdi
callq 0x55c0
testb $0x1, -0x52a(%rbp)
jne 0x1ffc1
jmp 0x1ffcd
movq -0x610(%rbp), %rdi
callq 0x53f0
jmp 0x20074
movq -0x540(%rbp), %rdi
addq $0x230, %rdi            # imm = 0x230
callq 0x5240
movq %rax, %rdi
callq 0x5960
movq %rax, %rcx
movq -0x540(%rbp), %rax
movq 0x8(%rax), %rax
movq %rcx, (%rax)
movq -0x28(%rbp), %rdi
leaq 0x3b34(%rip), %rsi      # 0x23b3d
callq 0x51b0
cmpl $0x0, %eax
jne 0x2001e
xorl %eax, %eax
movq %rax, -0x618(%rbp)
jmp 0x20040
movq -0x540(%rbp), %rdi
addq $0x250, %rdi            # imm = 0x250
callq 0x5240
movq %rax, %rdi
callq 0x5960
movq %rax, -0x618(%rbp)
movq -0x540(%rbp), %rax
movq -0x618(%rbp), %rdx
movq 0x8(%rax), %rcx
movq %rdx, 0x8(%rcx)
movq 0x8(%rax), %rax
movq $0x0, 0x10(%rax)
leaq -0x58(%rbp), %rdi
callq 0x5a08
addq $0x620, %rsp            # imm = 0x620
popq %rbp
retq
leaq -0x58(%rbp), %rdi
callq 0x5a08
movq -0x538(%rbp), %rdi
callq 0x10bd0
movq -0x548(%rbp), %rdi
callq 0x5a08
movq -0x550(%rbp), %rdi
callq 0x5a08
movq -0x558(%rbp), %rdi
callq 0x5a08
movq -0x560(%rbp), %rdi
callq 0x5120
movq -0x30(%rbp), %rdi
callq 0x58e0
nopw %cs:(%rax,%rax)