-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCGI.cpp
More file actions
80 lines (67 loc) · 2.59 KB
/
CGI.cpp
File metadata and controls
80 lines (67 loc) · 2.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* CGI.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: fpalumbo <fpalumbo@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/09/04 15:43:59 by jfazi #+# #+# */
/* Updated: 2024/09/26 14:08:51 by fpalumbo ### ########.fr */
/* */
/* ************************************************************************** */
#include "CGI.hpp"
static void ft_alarm(int sig)
{
cerr << sig << endl;
}
void CGI::launchCgi(string filePath, SOCKET sock, Parsconfig config)
{
int tube[2];
pid_t pid;
if (pipe(tube) == -1)
Response response(CODE_STATUS_500, config.getError500(), sock, config);
pid = fork();
switch (pid)
{
case -1: {
close(tube[0]);
close(tube[1]);
Response response(CODE_STATUS_500, config.getError500(), sock, config); }
return;
case 0:
{
close(tube[0]);
dup2(tube[1], STDOUT_FILENO);
char *env[] = {
const_cast<char *>("QUERY_STRING=name=jfazi"),
const_cast<char *>("SERVER_PROTOCOL=HTTP/1.1"),
const_cast<char *>("SERVER_SOFTWARE=webserv/1.0"),
const_cast<char *>("CONTENT_LENGTH=-1"),
const_cast<char *>("GATEWAY_INTERFACE=CGI/1.1"),
NULL};
char *const *nll = NULL;
signal(SIGALRM, ft_alarm);
alarm(5);
execle(filePath.c_str(), filePath.c_str(), nll, env);
close(tube[1]);
_exit(EXIT_FAILURE);
}
break;
default:
close(tube[1]);
char buffer[1000];
ssize_t bytesRead = read(tube[0], buffer, sizeof(buffer));
close(tube[0]);
if (bytesRead == -1)
Response response(CODE_STATUS_500, config.getError500(), sock, config);
int status;
waitpid(pid, &status, 0);
if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
Response response(CODE_STATUS_500, config.getError500(), sock, config);
else
Response response(CODE_STATUS_200, sock, config, string(buffer, bytesRead));
break;
}
}
CGI::CGI() {}
CGI::~CGI() {}