-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCGI.cpp
More file actions
229 lines (210 loc) · 6.63 KB
/
Copy pathCGI.cpp
File metadata and controls
229 lines (210 loc) · 6.63 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* CGI.cpp :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: sde-silv <sde-silv@student.42berlin.de> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/10/13 16:40:43 by ggiertzu #+# #+# */
/* Updated: 2025/05/24 15:49:55 by sde-silv ### ########.fr */
/* */
/* ************************************************************************** */
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <cerrno>
#include <cstring>
#include "CGI.hpp"
#include "Debug.hpp"
#include "Location.hpp"
#include "HttpHeaders.hpp"
#include "Utils.hpp"
#include <map>
/*
std::map<std::string, std::string> parse_http_headers(std::istringstream& stream, std::size_t bytes_read) {
std::map<std::string, std::string> headers;
std::string line;
std::size_t total_bytes_consumed = 0;
// Read request line (e.g., "GET / HTTP/1.1")
if (std::getline(stream, line) && !line.empty()) {
total_bytes_consumed += line.length() + 1; // +1 for '\n' or '\r\n'
}
// Read headers until an empty line
while (std::getline(stream, line)) {
total_bytes_consumed += line.length() + 1; // include newline char
// Stop if we've gone past the bytes_read limit
if (total_bytes_consumed >= bytes_read) {
break;
}
// An empty line signals end of headers
if (line == "\r" || line.empty()) {
break;
}
// Remove potential '\r' from the end
if (!line.empty() && line.back() == '\r') {
line.pop_back();
}
// Split header line into key and value
ssize_t colon_pos = line.find(':');
if (colon_pos != std::string::npos) {
std::string key = line.substr(0, colon_pos);
std::string value = line.substr(colon_pos + 1);
// Trim whitespace from key and value
key.erase(0, key.find_first_not_of(" \t"));
key.erase(key.find_last_not_of(" \t") + 1);
value.erase(0, value.find_first_not_of(" \t"));
value.erase(value.find_last_not_of(" \t") + 1);
headers[key] = value;
}
}
return headers;
}
*/
/*
// extracts headers and stores them line by line in a vector
std::vector<std::string> extract_headers(std::istringstream &header_stream)
{
std::vector<std::string> headers;
std::string line;
// Read request line (e.g., "GET / HTTP/1.1")
std::getline(header_stream, line);
while (std::getline(header_stream, line)) {
if (line == "\r" || line.empty()) {
break;
}
headers.push_back(line);
}
return headers;
}
std::string &headers_find(std::string key, std::vector<std::string> &headers)
{
for (std::vector<std::string>::iterator it = headers.begin(); it != headers.end(); ++it)
{
if (it->compare(0, key.length(), key) != npos)
{
std::string value = it->substr(key.length()); // skip the key
// trim leading space or colon or both
std::string::size_type start = value.find_first_not_of(": ");
return (start != std::string::npos) ? value.substr(start) : "";
}
}
return "";
}
*/
int g_to;
void sig_handle(int sig)
{
if (sig == SIGALRM)
{
g_to = 1;
debug("[gen_cgi_res]: sig_handler has been called");
}
}
char **get_env(const HttpHeaders &headers) {
char **envp;
//int num_headers;
std::string env_entry;
//num_headers = headers.size();
const std::vector<std::pair<std::string, std::string> >& allHeaders = headers.getAll();
int num_headers = allHeaders.size();
envp = new char*[num_headers + 1];
int i = 0;
for (std::vector<std::pair<std::string, std::string> >::const_iterator it = allHeaders.begin(); it != allHeaders.end(); ++it) {
env_entry = it->first + "=" + it->second;
envp[i] = new char[env_entry.length() + 1];
std::strcpy(envp[i], env_entry.c_str());
i++;
}
envp[i] = 0;
return envp;
}
int gen_cgi_res(const std::string& body, std::ostringstream &res, HttpHeaders &headers, Location &cgiLoc, unsigned int timeout)
{
pid_t pid; // Fork the process
char **envp;
int pipein[2], pipeout[2];
int wstatus;
int nbytes;
char buffer[1024];
std::string cgi_path;
std::ostringstream oss;
struct sigaction s_sg;
pipe(pipeout);
cgi_path = cgiLoc.get_cgi_path();
if ((pid = fork()) == -1) {
debug("[gen_cgi_res]: fork error");
return (1);
}
else if (pid == 0) {
envp = get_env(headers);
// std::cout << std::string(envp[0]); // uncomment
pipe(pipein);
dup2(pipeout[1], STDOUT_FILENO);
close(pipeout[0]);
write(pipein[1], body.c_str(), body.size());
close(pipein[1]);
dup2(pipein[0], STDIN_FILENO);
char *argv[] = {const_cast <char *> (cgi_path.c_str()), NULL};
debug("[gen_cgi_res]: Trying to execute CGI: " + cgi_path);
debug("[gen_cgi_res]: File exists: " + std::string(access(cgi_path.c_str(), F_OK) == 0 ? "yes" : "no"));
debug("[gen_cgi_res]: File is executable: " + std::string(access(cgi_path.c_str(), X_OK) == 0 ? "yes" : "no"));
if (execve(cgi_path.c_str(), argv, envp) == -1)
{
debug("[gen_cgi_res]: execve error");
exit(EXIT_FAILURE);
}
}
// parent process
s_sg.sa_handler = sig_handle;
sigemptyset(&s_sg.sa_mask);
s_sg.sa_flags = 0;
sigaction(SIGALRM, &s_sg, 0);
alarm(timeout);
if (waitpid(pid, &wstatus, 0) == -1) {
while (1)
{
if (errno == EINTR)
{
if (g_to == 1)
{
debug("[gen_cgi_res]: timed out after " + Utils::to_string(timeout) + " seconds");
kill(pid, SIGKILL);
if (waitpid(pid, &wstatus, 0) == -1)
return -1;
else
return 1;
}
continue ;
}
break ;
}
}
if (WIFSIGNALED(wstatus))
{
int code = WTERMSIG(wstatus);
debug("[gen_cgi_res]: error " + std::string(strsignal(code)));
(void)code;
return (1);
}
if (WIFEXITED(wstatus))
{
int code = WEXITSTATUS(wstatus);
if (code)
{
debug("[gen_cgi_res]: exited with status: " + Utils::to_string(code));
return (1);
}
else
{
close(pipeout[1]);
while ((nbytes = read(pipeout[0], buffer, 1024)) > 0) {
oss.write(buffer, nbytes);
}
close(pipeout[0]);
res << oss.str();
debug("[gen_cgi_res]: good script");
return (0);
}
}
return (-1);
}