forked from amenesca/Weeaboos-WebServer
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcgiHandler.cpp
More file actions
129 lines (114 loc) · 3.99 KB
/
Copy pathcgiHandler.cpp
File metadata and controls
129 lines (114 loc) · 3.99 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
#include "cgiHandler.hpp"
cgiHandler::cgiHandler() {}
cgiHandler::~cgiHandler() {}
std::string cgiHandler::getScriptFilename(const std::string& requestURI) {
size_t lastSlashPos = requestURI.find_last_of("/");
if (lastSlashPos == std::string::npos)
return (requestURI);
return (requestURI.substr(lastSlashPos + 1));
}
std::string cgiHandler::configCgi(int clientSocket, char **envp)
{
std::vector<const char*> argv;
argv.push_back("/usr/bin/python3");
argv.push_back("./cgi-bin/index.py");
argv.push_back(NULL);
int status;
int pipe_fd[2];
if (pipe(pipe_fd) == -1)
{
std::cerr << "Error creating pipe: " << strerror(errno) << std::endl;
return "";
}
_pid = fork();
if (_pid == -1)
{
std::cerr << "Error on fork: " << strerror(errno) << std::endl;
}
else if (_pid == 0)
{
close(pipe_fd[0]);
dup2(pipe_fd[1], STDIN_FILENO);
dup2(clientSocket, STDOUT_FILENO);
close(pipe_fd[1]);
// Escreva os dados de entrada no pipe
std::string inputData = "Aqui estão os dados de entrada que você deseja passar para o script Python.";
write(pipe_fd[1], inputData.c_str(), inputData.size());
close(pipe_fd[1]); // Feche o descritor de arquivo de escrita após escrever os dados
if (execve(argv[0], const_cast<char* const*>(argv.data()), envp) == -1) {
std::cerr << "Error on execve: " << strerror(errno) << std::endl;
}
}
else
{
close(pipe_fd[1]);
close(clientSocket);
waitpid(_pid, &status, 0);
close(pipe_fd[0]);
}
return "";
}
std::vector<char*> cgiHandler::createEnv(std::map<std::string, std::string> requestHeaders, RequestParser request, Client client) {
std::vector<char*> env;
std::string clientIp;
std::string clientPort;
clientIp = inet_ntoa(client.getClientAddrPointer()->sin_addr);
clientPort = ntohs(client.getClientAddrPointer()->sin_port);
env.push_back(strdup(("CONTENT_TYPE=" + requestHeaders["Content-Type"]).c_str()));
env.push_back(strdup(("CONTENT_LENGTH=" + requestHeaders["Content-Length"]).c_str()));
env.push_back(strdup(("REQUEST_URI=" + request.getUri()).c_str()));
env.push_back(strdup(("SCRIPT_NAME=" + request.getUri().substr(1)).c_str()));
env.push_back(strdup(("SCRIPT_FILENAME=" + getScriptFilename(request.getUri())).c_str()));
env.push_back(strdup(("REMOTE_ADDR=" + clientIp + ":" + clientPort).c_str()));
env.push_back(strdup(("SERVER_NAME=" + requestHeaders["Host"]).c_str()));
env.push_back(strdup(("SERVER_PORT=" + request.getPortNumber()).c_str()));
env.push_back(strdup("AUTH_TYPE=Basic"));
env.push_back(strdup("REQUEST_METHOD=POST"));
env.push_back(strdup("REDIRECT_STATUS=200"));
env.push_back(strdup("DOCUMENT_ROOT=./"));
env.push_back(strdup("GATEWAY_INTERFACE=CGI/1.1"));
env.push_back(strdup("PATH_INFO="));
env.push_back(strdup("PATH_TRANSLATED=.//"));
env.push_back(strdup("QUERY_STRING="));
env.push_back(strdup("SERVER_PROTOCOL=HTTP/1.1"));
env.push_back(strdup("SERVER_SOFTWARE=AMANIX"));
env.push_back(NULL);
return env;
}
std::string cgiHandler::postCgi(RequestParser postRequest, Client client) {
std::cout << "ENTROU AQUI\n";
std::vector<const char*> argv;
std::vector<char*> headerEnv = createEnv(postRequest.getHeaders(), postRequest, client);
argv.push_back("/usr/bin/python3");
argv.push_back("./cgi-bin/index.py");
argv.push_back(NULL);
int status;
int pipe_fd[2];
if (pipe(pipe_fd) == -1)
{
std::cerr << "Error creating pipe: " << strerror(errno) << std::endl;
return "";
}
_pid = fork();
std::cout << "PID: " << _pid << "\n";
if (_pid == -1)
{
std::cerr << "Error on fork: " << strerror(errno) << std::endl;
}
else if (_pid == 0)
{
std::cout << "ENTROU NO ELSE IF DO PID\n";
close(pipe_fd[0]);
close(pipe_fd[1]);
if (execve(argv[0], const_cast<char* const*>(argv.data()), const_cast<char* const*>(headerEnv.data())) == -1) {
std::cerr << "Error on execve: " << strerror(errno) << std::endl;
}
}
else
{
close(pipe_fd[1]);
waitpid(_pid, &status, 0);
close(pipe_fd[0]);
}
return "";
}