-
Notifications
You must be signed in to change notification settings - Fork 0
/
cgi.cpp
28 lines (26 loc) · 901 Bytes
/
cgi.cpp
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
#include "cgi.hpp"
#include <string>
#include <stdio.h>
#include "response.hpp"
std::string get_cgi(t_request request) {
char buffer[1024];
std::string result;
int question_mark_pos = request.headers["Request-URI"].find('?');
std::string query = request.headers["Request-URI"].substr(question_mark_pos + 1, request.headers["Request-URI"].size() - question_mark_pos - 1);
std::string command = "QUERY_STRING=\"" + query + "\" python3 tests";
command.append(request.headers["Request-URI"].substr(0, question_mark_pos));
FILE* pipe = popen(command.c_str(), "r"); //command is passed through the shell
if (!pipe) {
return get_response("", 500, 0);
}
try {
while (fgets(buffer, sizeof buffer, pipe) != NULL) {
result += buffer;
}
} catch (...) {
pclose(pipe);
return get_response("", 500, 0);
}
pclose(pipe);
return result;
}