// File: /workspace/github_cpp_makefile_star2_O3/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 0x12e94
movq %rax, %r12
leaq 0x3081(%rip), %rsi      # 0x157b6
leaq -0x48(%rbp), %rdi
movq %r15, %rdx
callq 0x643f
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 0x12789
movq %rdx, -0x270(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x260(%rbp)
jmp 0x12791
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 0x127d2
movq -0x260(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
movq -0xe0(%rbp), %r15
je 0x127f2
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq 0x3389(%rip), %rsi      # 0x15b82
movq %r15, %rdi
callq 0x4130
testl %eax, %eax
je 0x1285c
leaq 0x337b(%rip), %rsi      # 0x15b87
leaq -0x48(%rbp), %rdi
movq %r15, %rdx
callq 0x643f
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 0x1286b
movq %rdx, -0x270(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x260(%rbp)
jmp 0x12873
movq -0x50(%rbp), %rdi
movq -0x58(%rbp), %rsi
callq 0x4240
jmp 0x128cd
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 0x128b4
movq -0x260(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
cmpq %rax, %rdi
je 0x128cd
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq 0x32ae(%rip), %rsi      # 0x15b82
movq -0x50(%rbp), %rdi
callq 0x4130
testl %eax, %eax
je 0x128f5
movq -0x58(%rbp), %rax
movq (%rax), %rsi
movq %rbx, %rdi
movl $0x18, %edx
callq 0x4370
movl $0x38, %edi
callq 0x4030
movq %rax, (%r14)
leaq 0x328c(%rip), %rsi      # 0x15b95
leaq -0x270(%rbp), %rdi
movq -0x58(%rbp), %rdx
callq 0x643f
movq -0x270(%rbp), %rdi
callq 0x4730
movq (%r14), %rcx
movq %rax, (%rcx)
movq -0x270(%rbp), %rdi
cmpq %r13, %rdi
je 0x12946
movq -0x260(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq 0x3253(%rip), %rdi      # 0x15ba0
callq 0x4730
movq (%r14), %rcx
movq %rax, 0x8(%rcx)
leaq 0x3259(%rip), %rdi      # 0x15bb9
callq 0x4730
movq (%r14), %rcx
movq %rax, 0x10(%rcx)
movq 0x18(%r14), %rdi
callq 0xcad2
movl %eax, %r15d
leaq 0x324e(%rip), %rsi      # 0x15bcd
movq -0x50(%rbp), %rdi
callq 0x4130
testl %r15d, %r15d
je 0x12a6a
movl $0x3, %r12d
testl %eax, %eax
jne 0x129b4
leaq 0x3264(%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 0xcaba
leaq -0x260(%rbp), %rdi
movq 0x20(%rax), %rsi
callq 0x4290
leaq -0x88(%rbp), %r13
movq %r13, -0x10(%r13)
leaq 0x320b(%rip), %rsi      # 0x15bf6
leaq 0x3213(%rip), %rdx      # 0x15c05
leaq -0x98(%rbp), %rdi
callq 0x5796
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, %edi
cmpq %r13, %rcx
je 0x12a35
movq -0x88(%rbp), %rdi
movq -0x78(%rbp), %rsi
cmpq %rdi, %rax
jbe 0x12a59
leaq -0x68(%rbp), %r9
movl $0xf, %edi
cmpq %r9, %rsi
je 0x12a50
movq -0x68(%rbp), %rdi
cmpq %rdi, %rax
jbe 0x12c19
leaq -0x98(%rbp), %rdi
callq 0x4150
jmp 0x12c26
movl $0x3, %r12d
testl %eax, %eax
jne 0x12a8d
leaq 0x315a(%rip), %rdi      # 0x15bd5
callq 0x4730
movq (%r14), %rcx
movq %rax, 0x18(%rcx)
movl $0x4, %r12d
movq 0x18(%r14), %rdi
callq 0xcae6
leaq 0x314b(%rip), %rsi      # 0x15be8
leaq -0x270(%rbp), %rdi
movq %rax, %rdx
callq 0x643f
leaq -0xb8(%rbp), %rdi
leaq -0x270(%rbp), %r15
movq %r15, %rsi
callq 0x44d0
movq (%r15), %rdi
cmpq %r13, %rdi
je 0x12ad9
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 0x3080(%rip), %rsi      # 0x15b82
movq -0x50(%rbp), %rdi
callq 0x4130
testl %eax, %eax
movq -0xd8(%rbp), %r13
je 0x12df7
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 0x12b69
movq -0xb8(%rbp), %rsi
movq -0xb0(%rbp), %rdx
movq %r13, %rdi
callq 0x4150
jmp 0x12b21
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 0x305c(%rip), %rsi      # 0x15bf6
leaq 0x3064(%rip), %rdx      # 0x15c05
leaq -0x98(%rbp), %rdi
callq 0x5796
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, %edi
cmpq %r13, %rcx
je 0x12be4
movq -0x88(%rbp), %rdi
movq -0x78(%rbp), %rsi
cmpq %rdi, %rax
jbe 0x12c08
leaq -0x68(%rbp), %r9
movl $0xf, %edi
cmpq %r9, %rsi
je 0x12bff
movq -0x68(%rbp), %rdi
cmpq %rdi, %rax
jbe 0x12d05
leaq -0x98(%rbp), %rdi
callq 0x4150
jmp 0x12d12
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 0x12c47
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0x12c4d
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 0x12c94
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x68(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12cad
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x98(%rbp), %rdi
cmpq %r13, %rdi
je 0x12cc8
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 0xa2a8(%rip), %rsi      # 0x1cf90
leaq -0x270(%rbp), %rdi
callq 0x42f0
leaq -0x1f0(%rbp), %rdi
callq 0x4180
jmp 0x12df7
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 0x12d33
movq %rdx, -0x48(%rbp)
movq (%rcx), %rdx
movq %rdx, -0x38(%rbp)
jmp 0x12d39
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 0x12d80
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
leaq -0x68(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12d99
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x43e0
movq -0x98(%rbp), %rdi
cmpq %r13, %rdi
je 0x12db4
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 0xa1b4(%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 0x2d56(%rip), %rsi      # 0x15b82
movq -0xe0(%rbp), %rdi
callq 0x4130
testl %eax, %eax
je 0x12e4a
movq -0x58(%rbp), %rax
movq (%rax), %rdi
callq 0x4730
jmp 0x12e4c
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 0x12e82
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 0x2cae(%rip), %rsi      # 0x15b56
movq %rax, %rdi
callq 0x41b0
movq 0xa131(%rip), %rsi      # 0x1cfe8
movq 0xa0fa(%rip), %rdx      # 0x1cfb8
movq %r12, %rdi
callq 0x46a0
jmp 0x12ee9
jmp 0x12f07
jmp 0x12f5e
jmp 0x12f5e
jmp 0x12f86
movq %rax, %r15
movq %r12, %rdi
callq 0x42e0
jmp 0x12f89
movq %rax, %r15
leaq -0x68(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12f0a
movq -0x68(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0x12f0a
movq %rax, %r15
movq -0x98(%rbp), %rdi
cmpq %r13, %rdi
je 0x12f61
movq -0x88(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0x12f61
jmp 0x12f5e
jmp 0x12f86
movq %rax, %r15
movq -0x48(%rbp), %rdi
leaq -0x38(%rbp), %rax
jmp 0x12f49
jmp 0x12f86
jmp 0x12f86
jmp 0x12f86
movq %rax, %r15
leaq -0x38(%rbp), %rax
movq -0x10(%rax), %rdi
cmpq %rax, %rdi
je 0x12f89
movq -0x38(%rbp), %rsi
incq %rsi
callq 0x43e0
jmp 0x12f89
jmp 0x12f86
movq %rax, %r15
movq 0xa028(%rip), %rsi      # 0x1cf90
leaq -0x270(%rbp), %rdi
callq 0x42f0
leaq -0x1f0(%rbp), %rdi
callq 0x4180
jmp 0x12f89
jmp 0x12f86
jmp 0x12f86
movq %rax, %r15
addq $0x290, %r14            # imm = 0x290
movq -0xb8(%rbp), %rdi
leaq -0xa8(%rbp), %rax
cmpq %rax, %rdi
je 0x12fb2
movq -0xa8(%rbp), %rsi
incq %rsi
callq 0x43e0
movq %r14, %rdi
callq 0x9914
movq -0xd8(%rbp), %rax
movq (%rax), %rdi
cmpq -0xc0(%rbp), %rdi
je 0x12fdf
movq -0xc0(%rbp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x43e0
movq -0x58(%rbp), %rax
movq (%rax), %rdi
cmpq -0xc8(%rbp), %rdi
je 0x13001
movq -0xc8(%rbp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x43e0
movq -0x50(%rbp), %rax
movq (%rax), %rdi
cmpq -0xd0(%rbp), %rdi
je 0x13023
movq -0xd0(%rbp), %rax
movq (%rax), %rsi
incq %rsi
callq 0x43e0
movq %rbx, %rdi
callq 0x40c0
movq %r15, %rdi
callq 0x46d0
nop