// File: /workspace/github_cpp_makefile_star2_O2/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 $0x238, %rsp            # imm = 0x238
movq %r8, -0x48(%rbp)
movq %rcx, %r12
movq %rdi, %rbx
movq %rsi, 0x10(%rdi)
movq %rdx, 0x18(%rdi)
leaq 0x20(%rdi), %r14
movq %r14, %rdi
callq 0x44b0
leaq 0x240(%rbx), %rax
movq %rax, 0x230(%rbx)
andq $0x0, 0x238(%rbx)
xorl %eax, %eax
movb %al, 0x240(%rbx)
leaq 0x260(%rbx), %rcx
movq %rcx, 0x250(%rbx)
andq $0x0, 0x258(%rbx)
movb %al, 0x260(%rbx)
leaq 0x280(%rbx), %rcx
movq %rcx, 0x270(%rbx)
andq $0x0, 0x278(%rbx)
movb %al, 0x280(%rbx)
leaq 0x2a0(%rbx), %rcx
movq %rcx, 0x290(%rbx)
andq $0x0, 0x298(%rbx)
leaq 0x230(%rbx), %rcx
movq %rcx, -0x38(%rbp)
leaq 0x250(%rbx), %r13
movb %al, 0x2a0(%rbx)
leaq 0x2c0(%rbx), %rcx
movq %rcx, 0x2b0(%rbx)
andq $0x0, 0x2b8(%rbx)
leaq 0x270(%rbx), %rcx
movq %rcx, -0x50(%rbp)
movb %al, 0x2c0(%rbx)
leaq -0xc8(%rbp), %rcx
movq %rcx, -0x10(%rcx)
andq $0x0, -0x8(%rcx)
movb %al, (%rcx)
xorl %edi, %edi
xorl %esi, %esi
callq 0x4250
testq %rax, %rax
movq %r13, -0x40(%rbp)
je 0xf5e1
movq %rax, %r15
leaq 0x2604(%rip), %rsi      # 0x117af
leaq -0x70(%rbp), %rdi
movq %r12, %rdx
callq 0x5f8b
leaq -0x260(%rbp), %rdi
leaq -0x70(%rbp), %rdx
movq %r15, %rsi
callq 0x4f60
leaq -0x260(%rbp), %r12
movq %r13, %rdi
movq %r12, %rsi
callq 0x44f0
movq %r12, %rdi
callq 0x47d8
leaq -0x70(%rbp), %rdi
callq 0x47d8
leaq 0x293c(%rip), %rsi      # 0x11b30
movq -0x48(%rbp), %r12
movq %r12, %rdi
callq 0x4170
testl %eax, %eax
je 0xf250
leaq 0x292a(%rip), %rsi      # 0x11b35
leaq -0x70(%rbp), %rdi
movq %r12, %rdx
callq 0x5f8b
leaq -0x260(%rbp), %rdi
leaq -0x70(%rbp), %rdx
movq %r15, %rsi
callq 0x4f60
leaq -0x260(%rbp), %r15
movq -0x38(%rbp), %rdi
movq %r15, %rsi
callq 0x44f0
movq %r15, %rdi
callq 0x47d8
leaq -0x70(%rbp), %rdi
callq 0x47d8
jmp 0xf25c
movq -0x38(%rbp), %rdi
movq %r13, %rsi
callq 0x4270
leaq 0x28cd(%rip), %rsi      # 0x11b30
movq -0x38(%rbp), %rdi
callq 0x4170
testl %eax, %eax
je 0xf27f
movq (%r13), %rsi
pushq $0x18
popq %rdx
movq %r14, %rdi
callq 0x43d0
pushq $0x38
popq %rdi
callq 0x4030
movq %rax, (%rbx)
leaq 0x28b2(%rip), %rsi      # 0x11b43
leaq -0x260(%rbp), %rdi
movq %r13, %rdx
callq 0x5f8b
leaq -0x260(%rbp), %r15
movq (%r15), %rdi
callq 0x4740
movq (%rbx), %rcx
movq %rax, (%rcx)
movq %r15, %rdi
callq 0x47d8
leaq 0x288a(%rip), %rdi      # 0x11b4e
callq 0x4740
movq (%rbx), %rcx
movq %rax, 0x8(%rcx)
leaq 0x2890(%rip), %rdi      # 0x11b67
callq 0x4740
movq (%rbx), %rcx
movq %rax, 0x10(%rcx)
movq 0x18(%rbx), %rdi
callq 0xb3a2
movl %eax, %r15d
leaq 0x2885(%rip), %rsi      # 0x11b7b
movq -0x38(%rbp), %rdi
callq 0x4170
testl %r15d, %r15d
je 0xf30c
testl %eax, %eax
je 0xf31b
pushq $0x3
jmp 0xf330
testl %eax, %eax
je 0xf3f4
pushq $0x3
jmp 0xf409
leaq 0x2892(%rip), %rdi      # 0x11bb4
callq 0x4740
movq (%rbx), %rcx
movq %rax, 0x18(%rcx)
pushq $0x4
popq %r13
leaq -0x260(%rbp), %rdi
callq 0x4300
movq 0x18(%rbx), %rdi
callq 0xb38a
leaq -0x250(%rbp), %rdi
movq 0x20(%rax), %rsi
callq 0x42e0
leaq 0x2846(%rip), %rsi      # 0x11ba4
leaq -0xb8(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x4ec8
leaq -0x248(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x4630
leaq -0x70(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
leaq -0x98(%rbp), %rdx
callq 0xa3e1
leaq -0xd8(%rbp), %r15
leaq -0x70(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x44f0
movq %r12, %rdi
callq 0x47d8
leaq -0x98(%rbp), %rdi
callq 0x47d8
leaq -0xb8(%rbp), %rdi
callq 0x47d8
movq (%r15), %rdi
callq 0x4740
movq (%rbx), %rcx
movl %r13d, %edx
incl %r13d
movq %rax, (%rcx,%rdx,8)
leaq -0x260(%rbp), %rdi
callq 0x4350
jmp 0xf564
leaq 0x2788(%rip), %rdi      # 0x11b83
callq 0x4740
movq (%rbx), %rcx
movq %rax, 0x18(%rcx)
pushq $0x4
popq %r13
movq 0x18(%rbx), %rdi
callq 0xb3b6
leaq 0x277b(%rip), %rsi      # 0x11b96
leaq -0x260(%rbp), %rdi
movq %rax, %rdx
callq 0x5f8b
leaq -0xd8(%rbp), %r15
leaq -0x260(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x44f0
movq %r12, %rdi
callq 0x47d8
movq (%r15), %rdi
callq 0x4740
movq (%rbx), %rcx
movq %r13, %rdx
incl %r13d
movq %rdx, -0x78(%rbp)
movl %edx, %edx
movq %rax, (%rcx,%rdx,8)
leaq 0x26c3(%rip), %rsi      # 0x11b30
movq -0x38(%rbp), %rdi
callq 0x4170
testl %eax, %eax
je 0xf564
movq %r14, %rdi
movq %r15, %rsi
callq 0x40b0
movq (%rax), %rcx
movq -0x18(%rcx), %rcx
testb $0x5, 0x20(%rax,%rcx)
jne 0xf4a5
movq -0x50(%rbp), %rdi
movq %r15, %rsi
callq 0x47a0
jmp 0xf47e
leaq -0x260(%rbp), %rdi
callq 0x4300
leaq -0x250(%rbp), %rdi
movq 0x278(%rbx), %rsi
callq 0x42e0
leaq 0x26d9(%rip), %rsi      # 0x11ba4
leaq -0xb8(%rbp), %rdi
leaq -0x29(%rbp), %rdx
callq 0x4ec8
leaq -0x248(%rbp), %rsi
leaq -0x98(%rbp), %rdi
callq 0x4630
leaq -0x70(%rbp), %rdi
leaq -0xb8(%rbp), %rsi
leaq -0x98(%rbp), %rdx
callq 0xa3e1
leaq -0xd8(%rbp), %r15
leaq -0x70(%rbp), %r12
movq %r15, %rdi
movq %r12, %rsi
callq 0x44f0
movq %r12, %rdi
callq 0x47d8
leaq -0x98(%rbp), %rdi
callq 0x47d8
leaq -0xb8(%rbp), %rdi
callq 0x47d8
movq (%r15), %rdi
callq 0x4740
movq (%rbx), %rcx
movq -0x78(%rbp), %r15
addl $0x2, %r15d
movl %r13d, %edx
movq %rax, (%rcx,%rdx,8)
leaq -0x260(%rbp), %rdi
callq 0x4350
movl %r15d, %r13d
movq (%rbx), %rax
movl %r13d, %ecx
andq $0x0, (%rax,%rcx,8)
pushq $0x18
popq %rdi
movq -0x40(%rbp), %r12
callq 0x4030
movq %rax, %r15
movq %rax, 0x8(%rbx)
movq 0x230(%rbx), %rdi
callq 0x4740
movq %rax, (%r15)
leaq 0x2598(%rip), %rsi      # 0x11b30
movq -0x48(%rbp), %rdi
callq 0x4170
testl %eax, %eax
je 0xf5b0
movq (%r12), %rdi
callq 0x4740
jmp 0xf5b2
xorl %eax, %eax
movq 0x8(%rbx), %rcx
movq %rax, 0x8(%rcx)
movq 0x8(%rbx), %rax
andq $0x0, 0x10(%rax)
leaq -0xd8(%rbp), %rdi
callq 0x47d8
addq $0x238, %rsp            # imm = 0x238
popq %rbx
popq %r12
popq %r13
popq %r14
popq %r15
popq %rbp
retq
pushq $0x10
popq %rdi
callq 0x4220
movq %rax, %r15
leaq 0x2511(%rip), %rsi      # 0x11b04
movq %rax, %rdi
callq 0x41c0
movq 0x99ee(%rip), %rsi      # 0x18ff0
movq 0x99bf(%rip), %rdx      # 0x18fc8
movq %r15, %rdi
callq 0x46b0
jmp 0xf628
jmp 0xf639
jmp 0xf666
jmp 0xf666
jmp 0xf67b
movq %rax, %r12
movq %r15, %rdi
callq 0x4330
jmp 0xf67e
movq %rax, %r12
leaq -0x98(%rbp), %rdi
callq 0x47d8
jmp 0xf63c
movq %rax, %r12
leaq -0xb8(%rbp), %rdi
callq 0x47d8
jmp 0xf669
jmp 0xf666
jmp 0xf67b
jmp 0xf656
jmp 0xf67b
jmp 0xf67b
jmp 0xf67b
movq %rax, %r12
leaq -0x70(%rbp), %rdi
callq 0x47d8
jmp 0xf67e
jmp 0xf67b
movq %rax, %r12
leaq -0x260(%rbp), %rdi
callq 0x4350
jmp 0xf67e
jmp 0xf67b
jmp 0xf67b
movq %rax, %r12
addq $0x290, %rbx            # imm = 0x290
leaq -0xd8(%rbp), %rdi
callq 0x47d8
movq %rbx, %rdi
callq 0x8cde
movq -0x50(%rbp), %rdi
callq 0x47d8
movq -0x40(%rbp), %rdi
callq 0x47d8
movq -0x38(%rbp), %rdi
callq 0x47d8
movq %r14, %rdi
callq 0x40e0
movq %r12, %rdi
callq 0x46d0