// File: /workspace/github_cpp_makefile_star2_O2/llefranc[P]42_webserv/srcs/server/Cgi.cpp
// Function: CGI::executeCGI()
void CGI::executeCGI()
{
	int fdOut[2];
	int fdIN[2];
	
	if (pipe(fdOut) < 0 || pipe(fdIN) < 0)
		throw StatusLine(500, REASON_500, "pipe failed in executeCGI method");
	
	pid_t pid = fork();
	
	if (!pid){
		
		// stdout is now a copy of fdOut[1] and in case post method, stdin is a copy of fdIn[0]
		dup2(fdOut[1], STDOUT_FILENO);
		close(fdOut[0]);
		close(fdOut[1]);

		dup2(fdIN[0], STDIN_FILENO);
		close(fdIN[0]);
		close(fdIN[1]);

		// change the repo into where the program is
		chdir(_path_info.first.c_str());
	
		if (execve(_args[0], _args, _envvar) < 0){
			exit(EXECVE_FAIL);
		}
	
	}
	else if (pid > 0){
		
		close(fdOut[1]);
		if (_req->getMethod() == POST){
			if (write(fdIN[1], _req->getBody().getBody().c_str(), _req->getBody().getBody().size()) < 0)
				throw StatusLine(500, REASON_500, "write failed in executeCGI method");
		}
		
		else{
			if (write(fdIN[1], _getBuffFile.c_str(), _getBuffFile.size()) < 0)
				throw StatusLine(500, REASON_500, "write failed in executeCGI method");
		}
		
		close(fdIN[1]);
		close(fdIN[0]);
		
        // Checking if execve correctly worked
        int status = 0;
        waitpid(pid, &status, 0);
        if (WIFEXITED(status) && WEXITSTATUS(status) == EXECVE_FAIL)
            throw StatusLine(500, REASON_500, "execve failed in executeCGI method");
        
		char buf[CGI_PIPE_BUFFER_SIZE + 1] = {0};
		std::string msgbody;	
		while (read(fdOut[0], buf, CGI_PIPE_BUFFER_SIZE) > 0)
		{
			msgbody += buf;
			memset(buf, 0, CGI_PIPE_BUFFER_SIZE + 1);
		}
		msgbody += buf;

		close(fdOut[0]);

		_emptyBody->setBuff(msgbody);
		
		// remove the header part of the cgi output
		_emptyBody->setSize(msgbody.size() - (msgbody.find("\r\n\r\n") + 4)); 
	}
	else{
		close(fdOut[1]); close(fdOut[0]);
		if (_req->getMethod() == POST) { close(fdIN[0]); close(fdIN[1]); }
		throw StatusLine(500, REASON_500, "fork failed executeCGI method");
	}
}

// Assembly:
pushq %rbp
movq %rsp, %rbp
pushq %r15
pushq %r14
pushq %r12
pushq %rbx
subq $0x840, %rsp            # imm = 0x840
movq %rdi, %rbx
leaq -0x30(%rbp), %rdi
callq 0x4570
testl %eax, %eax
js 0xf9dd
leaq -0x28(%rbp), %rdi
callq 0x4570
testl %eax, %eax
js 0xf9dd
callq 0x4150
testl %eax, %eax
je 0xf860
movl %eax, %r14d
movl -0x2c(%rbp), %edi
callq 0x47b0
testl %r14d, %r14d
jle 0xfa3b
movq 0x18(%rbx), %rdi
callq 0xb3a2
movl -0x24(%rbp), %r15d
cmpl $0x2, %eax
jne 0xf8c2
movq 0x18(%rbx), %rdi
callq 0xb38a
movq 0x18(%rax), %r12
movq 0x18(%rbx), %rdi
callq 0xb38a
movq 0x20(%rax), %rdx
movl %r15d, %edi
movq %r12, %rsi
callq 0x41a0
testq %rax, %rax
jns 0xf8e1
pushq $0x48
popq %rdi
callq 0x4220
movq %rax, %r14
leaq 0x23d5(%rip), %rsi      # 0x11be9
leaq -0x860(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x4ec8
movb $0x1, %r15b
leaq 0x226d(%rip), %rdx      # 0x11a9b
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xc540
xorl %r15d, %r15d
leaq 0x9544(%rip), %rsi      # 0x18d90
leaq -0x32bd(%rip), %rdx     # 0xc596
movq %r14, %rdi
callq 0x46b0
jmp 0xfb73
movl -0x2c(%rbp), %edi
pushq $0x1
popq %rsi
callq 0x43f0
movl -0x30(%rbp), %edi
callq 0x47b0
movl -0x2c(%rbp), %edi
callq 0x47b0
movl -0x28(%rbp), %edi
xorl %esi, %esi
callq 0x43f0
movl -0x28(%rbp), %edi
callq 0x47b0
movl -0x24(%rbp), %edi
callq 0x47b0
movq 0x290(%rbx), %rdi
callq 0x45d0
movq (%rbx), %rdx
movq 0x8(%rbx), %rsi
movq (%rsi), %rdi
callq 0x46a0
testl %eax, %eax
jns 0xf9cd
movl $0xff, %edi
callq 0x44e0
movq 0x270(%rbx), %rsi
movq 0x278(%rbx), %rdx
movl %r15d, %edi
callq 0x41a0
testq %rax, %rax
js 0xfb1a
movl -0x24(%rbp), %edi
callq 0x47b0
movl -0x28(%rbp), %edi
callq 0x47b0
leaq -0x54(%rbp), %r15
andl $0x0, (%r15)
movl %r14d, %edi
movq %r15, %rsi
xorl %edx, %edx
callq 0x4370
movl $0xff7f, %eax           # imm = 0xFF7F
andl (%r15), %eax
cmpl $0xff00, %eax           # imm = 0xFF00
je 0xfabf
leaq -0x860(%rbp), %r14
movl $0x7ff, %edx            # imm = 0x7FF
movq %r14, %rdi
xorl %esi, %esi
callq 0x4280
leaq -0x40(%rbp), %rax
movq %rax, -0x10(%rax)
andq $0x0, -0x8(%rax)
movb $0x0, (%rax)
leaq -0x50(%rbp), %r15
movl -0x30(%rbp), %edi
movl $0x7fe, %edx            # imm = 0x7FE
movq %r14, %rsi
callq 0x45b0
testq %rax, %rax
jle 0xf974
movq %r15, %rdi
movq %r14, %rsi
callq 0x4770
movl $0x7ff, %edx            # imm = 0x7FF
movq %r14, %rdi
xorl %esi, %esi
callq 0x4280
jmp 0xf943
leaq -0x50(%rbp), %rdi
leaq -0x860(%rbp), %rsi
callq 0x4770
movl -0x30(%rbp), %edi
callq 0x47b0
movq 0x10(%rbx), %rdi
addq $0x18, %rdi
leaq -0x50(%rbp), %rsi
callq 0x4270
movq 0x10(%rbx), %r14
leaq -0x50(%rbp), %rbx
movq 0x8(%rbx), %r15
leaq 0x227e(%rip), %rsi      # 0x11c2e
movq %rbx, %rdi
xorl %edx, %edx
callq 0x47c0
subq %rax, %r15
addq $-0x4, %r15
movq %r15, 0x8(%r14)
movq %rbx, %rdi
callq 0x47d8
addq $0x840, %rsp            # imm = 0x840
popq %rbx
popq %r12
popq %r14
popq %r15
popq %rbp
retq
pushq $0x48
popq %rdi
callq 0x4220
movq %rax, %r14
leaq 0x21d9(%rip), %rsi      # 0x11bc8
leaq -0x860(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x4ec8
movb $0x1, %r15b
leaq 0x2092(%rip), %rdx      # 0x11a9b
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xc540
xorl %r15d, %r15d
leaq 0x9369(%rip), %rsi      # 0x18d90
leaq -0x3498(%rip), %rdx     # 0xc596
movq %r14, %rdi
callq 0x46b0
jmp 0xfb73
movl -0x30(%rbp), %edi
callq 0x47b0
movq 0x18(%rbx), %rdi
callq 0xb3a2
cmpl $0x2, %eax
jne 0xfa61
movl -0x28(%rbp), %edi
callq 0x47b0
movl -0x24(%rbp), %edi
callq 0x47b0
pushq $0x48
popq %rdi
callq 0x4220
movq %rax, %r14
leaq 0x21c0(%rip), %rsi      # 0x11c33
leaq -0x860(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x4ec8
movb $0x1, %r15b
leaq 0x200e(%rip), %rdx      # 0x11a9b
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xc540
xorl %r15d, %r15d
leaq 0x92e5(%rip), %rsi      # 0x18d90
leaq -0x351c(%rip), %rdx     # 0xc596
movq %r14, %rdi
callq 0x46b0
jmp 0xfb73
pushq $0x48
popq %rdi
callq 0x4220
movq %rax, %r14
leaq 0x213a(%rip), %rsi      # 0x11c0b
leaq -0x860(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x4ec8
movb $0x1, %r15b
leaq 0x1fb0(%rip), %rdx      # 0x11a9b
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xc540
xorl %r15d, %r15d
leaq 0x9287(%rip), %rsi      # 0x18d90
leaq -0x357a(%rip), %rdx     # 0xc596
movq %r14, %rdi
callq 0x46b0
jmp 0xfb73
pushq $0x48
popq %rdi
callq 0x4220
movq %rax, %r14
leaq 0x20bd(%rip), %rsi      # 0x11be9
leaq -0x860(%rbp), %rdi
leaq -0x50(%rbp), %rdx
callq 0x4ec8
movb $0x1, %r15b
leaq 0x1f55(%rip), %rdx      # 0x11a9b
leaq -0x860(%rbp), %rcx
movq %r14, %rdi
movl $0x1f4, %esi            # imm = 0x1F4
callq 0xc540
xorl %r15d, %r15d
leaq 0x922c(%rip), %rsi      # 0x18d90
leaq -0x35d5(%rip), %rdx     # 0xc596
movq %r14, %rdi
callq 0x46b0
jmp 0xfb83
jmp 0xfb83
jmp 0xfb99
jmp 0xfb99
jmp 0xfb83
jmp 0xfb99
jmp 0xfb83
jmp 0xfb99
movq %rax, %rbx
leaq -0x860(%rbp), %rdi
callq 0x47d8
testb %r15b, %r15b
jne 0xfb9c
jmp 0xfbb4
movq %rax, %rbx
movq %r14, %rdi
callq 0x4330
jmp 0xfbb4
jmp 0xfba8
movq %rax, %rbx
leaq -0x50(%rbp), %rdi
callq 0x47d8
movq %rbx, %rdi
callq 0x46d0