Skip to content

Commit

Permalink
fix(Server): fix compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Lombardo committed May 20, 2024
1 parent 85625b9 commit de698dd
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 2 deletions.
4 changes: 3 additions & 1 deletion conf/webserv_default.conf
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,9 @@ server {

server {
listen 8080;
server_name www.php_site;
server_name www.development_site;
allow_methods GET POST DELETE;
autoindex on;
cgi_ext .cgi .py;
root var/;
}
48 changes: 48 additions & 0 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,54 @@ void Server::buildResponse(Connection &conn, size_t &i, HTTPRequest &request, HT
buildCGIResponse(conn, response);
}

void Server::buildCGIResponse(Connection &conn, HTTPResponse &response)
{
std::cout << RED << "Entering buildCGIResponse" << RESET << std::endl;
std::string cgiOutput;
int *pipeFD;
pipeFD = response.getCGIpipeFD();
char readBuffer[256];
ssize_t bytesRead;
// TODO: this is blokcing - we need to make it non-blocking
// I.e. read 1 buffer and then go back to poll
while ((bytesRead = read(pipeFD[0], readBuffer, sizeof(readBuffer) - 1)) > 0)
{
readBuffer[bytesRead] = '\0';
cgiOutput += readBuffer;
}
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)
{
std::cout << "\033[1;36m" << "Entering writeToClient" << "\033[0m" << std::endl;
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ int main(int argc, char **argv)
if (!config.getErrorMessage().empty())
return 1;

std::cout << config << std::endl; // should be in the DEBUG?
// std::cout << config << std::endl; // should be in the DEBUG?
EventManager eventManager;
Server webserv(config, eventManager);

Expand Down

0 comments on commit de698dd

Please sign in to comment.