Skip to content

Commit

Permalink
fix(CGI): correct behaviuor in case of an error in CGI(permission den…
Browse files Browse the repository at this point in the history
…ied)
  • Loading branch information
dantol29 committed May 22, 2024
1 parent fb77a93 commit 5df3a79
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 23 deletions.
16 changes: 8 additions & 8 deletions src/CGIHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,14 +130,14 @@ bool CGIHandler::executeCGI(const MetaVariables &env, HTTPResponse &response)
else if (pid == 0)
{
// clang-format off
std::vector<std::pair<int, int> > pipes = _eventManager.getPipeFDs();
std::cerr << "CGIHandler: pipes: " << pipes.size() << std::endl;
for (std::vector<std::pair<int, int> >::const_iterator it = pipes.begin(); it != pipes.end(); ++it)
{
std::cerr << GREEN << "CLOSING: " << (*it).first << ", " << (*it).second << RESET << std::endl;
close((*it).first);
close((*it).second);
}
// std::vector<std::pair<int, int> > pipes = _eventManager.getPipeFDs();
// std::cerr << "CGIHandler: pipes: " << pipes.size() << std::endl;
// for (std::vector<std::pair<int, int> >::const_iterator it = pipes.begin(); it != pipes.end(); ++it)
// {
// std::cerr << GREEN << "CLOSING: " << (*it).first << ", " << (*it).second << RESET << std::endl;
// close((*it).first);
// close((*it).second);
// }
// clang-format on
close(pipeFD[0]);
dup2(pipeFD[1], STDOUT_FILENO);
Expand Down
21 changes: 7 additions & 14 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,11 @@ void Server::buildResponse(Connection &conn, size_t &i, HTTPRequest &request, HT
response.setIsCGI(false);
}
else
return (buildCGIResponse(conn, response));
{
readCGIPipe(conn, response);
if (response.getStatusCode() < 300)
return;
}
}

ServerBlock serverBlock;
Expand Down Expand Up @@ -350,9 +354,9 @@ void Server::buildResponse(Connection &conn, size_t &i, HTTPRequest &request, HT
}
}

void Server::buildCGIResponse(Connection &conn, HTTPResponse &response)
void Server::readCGIPipe(Connection &conn, HTTPResponse &response)
{
std::cout << RED << "Entering buildCGIResponse" << RESET << std::endl;
std::cout << RED << "Entering readCGIPipe" << RESET << std::endl;
std::string cgiOutput;
int *pipeFD;
pipeFD = response.getCGIpipeFD();
Expand Down Expand Up @@ -389,34 +393,23 @@ void Server::buildCGIResponse(Connection &conn, HTTPResponse &response)
close(pipeFD[0]);

int status = conn.getCGIExitStatus();
//
// if (waitedPid == -1)
// {
// perror("waitpid");
// return "HTTP/1.1 500 Internal Server Error\r\nContent-Length: 0\r\n\r\n";
// }

if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
{
response.setStatusCode(500, "Internal Server Error");
conn.setHasDataToSend(true);
response.setIsCGI(false);
// TODO: should we set other flags here?
return;
}

if (cgiOutput.empty())
{
response.setStatusCode(500, "Internal Server Error");
conn.setHasDataToSend(true);
response.setIsCGI(false);
return;
}
response.CGIStringToResponse(cgiOutput);
response.setIsCGI(false);
conn.setHasDataToSend(true);

// return cgiOutput;
}

void Server::writeToClient(Connection &conn, size_t &i, HTTPResponse &response)
Expand Down
2 changes: 1 addition & 1 deletion src/Server.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class Server
void readFromClient(Connection &conn, size_t &i, Parser &parser, HTTPRequest &request, HTTPResponse &response);
void handlePostRequest(Connection &conn, Parser &parser, HTTPRequest &request, HTTPResponse &response);
void buildResponse(Connection &conn, size_t &i, HTTPRequest &request, HTTPResponse &response);
void buildCGIResponse(Connection &conn, HTTPResponse &response);
void readCGIPipe(Connection &conn, HTTPResponse &response);
void writeToClient(Connection &conn, size_t &i, HTTPResponse &response);
void closeClientConnection(Connection &conn, size_t &i);

Expand Down
Empty file modified var/www.development_site/cgi-bin/hello_py.cgi
100755 → 100644
Empty file.

0 comments on commit 5df3a79

Please sign in to comment.