fix: cgi exe

This commit is contained in:
Talyx
2022-02-15 20:49:09 +03:00
parent c21aae0b1d
commit f7f1793722
15 changed files with 79 additions and 41 deletions

View File

@@ -5,26 +5,26 @@
body_size_limit = 100000000 body_size_limit = 100000000
[[server.location]] [[server.location]]
location = "/" location = "/"
root = "www/tester/" root = "tester/"
methods = ["GET"] methods = ["GET"]
autoindex = true autoindex = true
directory_file = "index.html" directory_file = "index.html"
[[server.location]] [[server.location]]
location = "/put_test" location = "/put_test"
root = "www/tester/" root = "tester/"
methods = ["PUT"] methods = ["PUT"]
autoindex = true autoindex = true
upload_dir = "www/tester/upload_here/" upload_dir = "tester/upload_here/"
[[server.location]] [[server.location]]
location = "*.bla" location = "*.bla"
cgi_pass = "/usr/bin/php-cgi" cgi_pass = "ubuntu_cgi_tester"
[[server.location]] [[server.location]]
location = "/post_body" location = "/post_body"
root = "www/tester/" root = "tester/"
methods = ["POST"] methods = ["POST"]
body_size_limit = 100 body_size_limit = 100
[[server.location]] [[server.location]]
location = "/directory" location = "/directory"
methods = ["GET"] methods = ["GET"]
root = "www/tester/YoupiBanane/" root = "tester/YoupiBanane/"
directory_file = "youpi.bad_extension" directory_file = "youpi.bad_extension"

View File

@@ -30,6 +30,7 @@
#include <sys/time.h> #include <sys/time.h>
#include <arpa/inet.h> #include <arpa/inet.h>
#include <wait.h> #include <wait.h>
#include <exception>
#include <netinet/in.h> #include <netinet/in.h>
#include <fcntl.h> #include <fcntl.h>

View File

@@ -81,8 +81,9 @@ std::string CgiHandle::executeCgi()
int sO; int sO;
int byte_read = 1; int byte_read = 1;
std::string body; std::string body;
std::string &reqBody = _request.getBody();
argv[0] = const_cast<char *>(_response.getCgiPass().data()); argv[0] = const_cast<char *>(_response._fullURI.data());
argv[1] = NULL; argv[1] = NULL;
try try
{ {
@@ -92,7 +93,6 @@ std::string CgiHandle::executeCgi()
{ {
std::cerr << RED << e.what() << RESET << '\n'; std::cerr << RED << e.what() << RESET << '\n';
} }
printenv(env);
sI = dup(STDIN_FILENO); sI = dup(STDIN_FILENO);
sO = dup(STDOUT_FILENO); sO = dup(STDOUT_FILENO);
@@ -100,9 +100,7 @@ std::string CgiHandle::executeCgi()
FILE *fOt = tmpfile(); FILE *fOt = tmpfile();
long fdin = fileno(fIn); long fdin = fileno(fIn);
long fdOut = fileno(fOt); long fdOut = fileno(fOt);
DBOUT << BLUE << "in CGI exe" << ENDL; write(fdin, reqBody.data(), reqBody.size());
// DBOUT << "BODY[\n" << _request.getBody() << "\n]" << ENDL;
write(fdin, _request.getBody().data(), _request.getBody().size());
lseek(fdin, 0, SEEK_SET); lseek(fdin, 0, SEEK_SET);
pid = fork(); pid = fork();
if (pid == -1) if (pid == -1)
@@ -115,7 +113,7 @@ std::string CgiHandle::executeCgi()
dup2(fdOut, STDOUT_FILENO); dup2(fdOut, STDOUT_FILENO);
execve(_response.getCgiPass().c_str(), argv, env); execve(_response.getCgiPass().c_str(), argv, env);
std::cerr << RED << "Execve error." << RESET << ENDL; std::cerr << RED << "Execve error." << RESET << ENDL;
write(STDOUT_FILENO, "Status: 500\r\n\r\n", 15); write(STDOUT_FILENO, "Status: 500\r\n\r\n", 15);
} }
else else
{ {
@@ -124,9 +122,10 @@ std::string CgiHandle::executeCgi()
waitpid(-1, NULL, 0); waitpid(-1, NULL, 0);
lseek(fdOut, 0, SEEK_SET); lseek(fdOut, 0, SEEK_SET);
byte_read = 1;
while (byte_read) while (byte_read)
{ {
bzero(buffer, BUFFSIZE); memset(buffer, 0, BUFFSIZE + 1);
byte_read = read(fdOut, buffer, BUFFSIZE); byte_read = read(fdOut, buffer, BUFFSIZE);
body.append(buffer, byte_read); body.append(buffer, byte_read);
} }
@@ -147,7 +146,7 @@ void CgiHandle::initEnvVariables()
{ {
std::map<std::string, std::string>::iterator it; std::map<std::string, std::string>::iterator it;
std::map<std::string, std::string> tmp1 = _request.getClientFields(); std::map<std::string, std::string> tmp1 = _request.getClientFields();
_scriptName = _response.getFullURI().substr(_response.getFullURI().rfind("/")); _scriptName = _response._fullURI.substr(_response._fullURI.rfind("/"));
it = tmp1.find("content_type"); it = tmp1.find("content_type");
if (it != tmp1.end()) if (it != tmp1.end())
_variable["AUTH TYPE"] = it->second; _variable["AUTH TYPE"] = it->second;
@@ -158,10 +157,10 @@ void CgiHandle::initEnvVariables()
_variable["CONTENT_TYPE"] = ""; _variable["CONTENT_TYPE"] = "";
_variable["GATEWAY_INTERFACE"] = std::string("CGI/1.1"); _variable["GATEWAY_INTERFACE"] = std::string("CGI/1.1");
_variable["SCRIPT_NAME"] = _response.getFullURI(); _variable["SCRIPT_NAME"] = _response._fullURI;
_variable["SCRIPT_FILENAME"] = _response.getFullURI(); _variable["SCRIPT_FILENAME"] = _response._fullURI;
_variable["PATH_TRANSLATED"] = _response.getFullURI(); _variable["PATH_TRANSLATED"] = _request.getURI();
_variable["REQUEST_URI"] = _request.getURI(); _variable["REQUEST_URI"] = _request.getURI();
_variable["PATH_INFO"] = _request.getURI(); _variable["PATH_INFO"] = _request.getURI();
@@ -184,7 +183,7 @@ void CgiHandle::initEnvVariables()
tmp = getEnvFormat(it->first); tmp = getEnvFormat(it->first);
_variable["HTTP_" + tmp] = it->second; _variable["HTTP_" + tmp] = it->second;
} }
printSSmap(_variable); // printSSmap(_variable);
} }
CgiHandle::~CgiHandle() CgiHandle::~CgiHandle()

View File

@@ -162,9 +162,6 @@ std::string Client::generateRespons(void)
_to_send_char = new char[len + 1]; _to_send_char = new char[len + 1];
std::memcpy(_to_send_char, _toSend.c_str(), len + 1); std::memcpy(_to_send_char, _toSend.c_str(), len + 1);
// DBOUT << "len = " << len << ENDL;
// DBOUT << "strlen = " << strlen(_to_send_char) << ENDL;
// DBOUT << "content_lenth = " << _request.getContentLength() << ENDL;
return (_toSend); return (_toSend);
} }

View File

@@ -36,6 +36,21 @@ int isDir(std::string path)
return (-1); return (-1);
return (-1); return (-1);
} }
void copyLocation(location *dest, location *src)
{
dest->autoindex = src->autoindex;
dest->cgi_pass = src->cgi_pass;
dest->clientBodySize = src->clientBodySize;
dest->directoryFile = src->directoryFile;
dest->location = src->location;
dest->methods = src->methods;
dest->redirect = src->redirect;
dest->root = src->root;
dest->uploadAccept = src->uploadAccept;
dest->uploadDir = src->uploadDir;
}
int Config::calcLen(std::string &s1, std::string &s2) int Config::calcLen(std::string &s1, std::string &s2)
{ {
unsigned long len = 0; unsigned long len = 0;
@@ -68,7 +83,12 @@ location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
tmp = *it; tmp = *it;
tryLocation = URI.substr(0, tryLen); tryLocation = URI.substr(0, tryLen);
if (tmp->location == tryLocation) if (tmp->location == tryLocation)
{
tmp = new location;
copyLocation(tmp, *it);
step_1.push_back(tmp); step_1.push_back(tmp);
break;
}
it++; it++;
} }
if (!step_1.empty()) if (!step_1.empty())
@@ -91,6 +111,8 @@ location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
suffix1 = tmp->location.substr(2); suffix1 = tmp->location.substr(2);
if (suffix1 == suffix) if (suffix1 == suffix)
{ {
tmp = new location;
copyLocation(tmp, *it);
step_2.push_back(tmp); step_2.push_back(tmp);
break; break;
} }
@@ -104,6 +126,7 @@ location *Config::getLocation(std::vector<location *> &arr, std::string &URI)
if(!step_2[1]->cgi_pass.empty()) if(!step_2[1]->cgi_pass.empty())
{ {
step_2[0]->cgi_pass = step_2[1]->cgi_pass; step_2[0]->cgi_pass = step_2[1]->cgi_pass;
delete step_2[1];
} }
return (step_2[0]); return (step_2[0]);
} }

View File

@@ -107,11 +107,11 @@ ssize_t Response::getMaxBodySize(void)
{ {
return (_maxBodySize); return (_maxBodySize);
} }
//-------------------------------------------------File--------------------------------------- //-------------------------------------------------File---------------------------------------
void Response::OpenResponseFile(const char *path) void Response::OpenResponseFile(const char *path)
{ {
DBOUT << "in OPEN RESPONSE FILE " << path << ENDL;
std::stringstream ss; std::stringstream ss;
std::ifstream file(path, std::ifstream::in); std::ifstream file(path, std::ifstream::in);
@@ -213,11 +213,8 @@ std::string Response::getFullURI(void)
} }
else else
{ {
DBOUT << "location" << _location->location << ENDL;
tmp = _request.getURI().substr(len); tmp = _request.getURI().substr(len);
DBOUT << "tmp1 " << RED << tmp << ENDL;
tmp = _location->root + tmp; tmp = _location->root + tmp;
DBOUT << "tmp2" << RED << tmp << ENDL;
} }
if (_request.isDir(tmp) == 0) if (_request.isDir(tmp) == 0)
{ {
@@ -327,16 +324,9 @@ void Response::generate2(serverListen &l)
_method = _request.getMethod(); _method = _request.getMethod();
_maxBodySize = (_location->clientBodySize > 0) ? _location->clientBodySize : _config->getClientBodySize(); _maxBodySize = (_location->clientBodySize > 0) ? _location->clientBodySize : _config->getClientBodySize();
if (_maxBodySize > 0) if (_maxBodySize > 0)
_code = (_request.getRecved() > _maxBodySize) ? 413 : _code; _code = (_request.getBody().size() > (unsigned long)_maxBodySize) ? 413 : _code;
DBOUT << BLUE << "max size" << _maxBodySize << ENDL;
DBOUT << BLUE << "_location size" << _location->clientBodySize << ENDL;
DBOUT << BLUE << "_config sieze" << _config->getClientBodySize() << ENDL;
DBOUT << BLUE << "req size " << _request.getContentLength() << ENDL;
DBOUT << BLUE << "recv size " << _request.getRecved() << ENDL;
} }
DBOUT << "fullURI " << _fullURI << ENDL;
DBOUT << RED << "code is " << _code << ENDL;
if (_request.badCode(_code) || (!allowedMethod(_method) && _location->cgi_pass.empty()) || isRedirect()) if (_request.badCode(_code) || (!allowedMethod(_method) && _location->cgi_pass.empty()) || isRedirect())
{ {
invalidClient(); invalidClient();
@@ -384,6 +374,7 @@ void Response::methodGet(void)
CgiHandle cgi(_request, *this); CgiHandle cgi(_request, *this);
_body = cgi.executeCgi(); _body = cgi.executeCgi();
unsigned long pos = _body.find("\r\n\r\n"); unsigned long pos = _body.find("\r\n\r\n");
if (pos != std::string::npos) if (pos != std::string::npos)
{ {
@@ -410,6 +401,7 @@ void Response::methodPost(void)
CgiHandle cgi(_request, *this); CgiHandle cgi(_request, *this);
_body = cgi.executeCgi(); _body = cgi.executeCgi();
DBOUT << "CGI SIZE BODY " << _body.size() << ENDL;
unsigned long pos = _body.find("\r\n\r\n"); unsigned long pos = _body.find("\r\n\r\n");
if (pos != std::string::npos) if (pos != std::string::npos)
{ {
@@ -558,4 +550,5 @@ std::string Response::getErrorPage(int code)
Response::~Response() Response::~Response()
{ {
delete _location;
} }

View File

@@ -20,7 +20,6 @@ private:
std::map<int, std::string> _errorPages; std::map<int, std::string> _errorPages;
bool _Autoindex; bool _Autoindex;
serverListen _hostPort; serverListen _hostPort;
std::string _fullURI;
std::string _method; std::string _method;
private: private:
@@ -73,6 +72,7 @@ public:
void setData(Request, ServerConfig *); void setData(Request, ServerConfig *);
void setData(Request &, ServerConfig *, location *location); void setData(Request &, ServerConfig *, location *location);
public: public:
std::string _fullURI;
void OpenResponseFile(const char *path); void OpenResponseFile(const char *path);
void OpenErrorFile(int code); void OpenErrorFile(int code);
void initErrorCode(void); void initErrorCode(void);

View File

@@ -9,12 +9,20 @@ int main(int argc, char **argv)
(void)argv; (void)argv;
Server server; Server server;
char *path = (char *)"config/real.toml";
try
{
if (argv[1] != NULL)
server.readConfig(argv[1]);
else
server.readConfig(path);
server.start();
}
catch(const std::exception& e)
{
std::cerr << RED << e.what() << '\n' << ENDL;
}
server.end();
server.readConfig(argv[1]);
server.setupConfig();
// server.start();
server.end();
return (0);
} }

BIN
ubuntu_cgi_tester Executable file

Binary file not shown.

17
www/script/index2.php Normal file
View File

@@ -0,0 +1,17 @@
<?php
header('Content-type: image/png', true);
$entityBody = file_get_contents('php://input');
echo($entityBody);
// phpinfo();
// echo ("Content-type:text/html\r\n\r\n");
// echo ("<html>\n");
// echo ("<head>\n");
// echo ("<title>Hello World - First CGI Program</title>\n");
// echo ("</head>\n");
// echo ("<body>\n");
// echo ("<h2>Hello World! This is my first CGI program</h2>\n");
// echo ("</body>\n");
// echo ("</html>\n");
?>