Skip to content

Commit

Permalink
fix(compile): fix compilation errors
Browse files Browse the repository at this point in the history
  • Loading branch information
dantol29 committed May 23, 2024
1 parent 3ad01a5 commit 2f25aaa
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 35 deletions.
16 changes: 8 additions & 8 deletions src/CGIHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,24 @@ bool CGIHandler::executeCGI(const MetaVariables &env, HTTPResponse &response)
response.setIsCGI(true);
response.setCGIpipeFD(pipeFD);

std::cout << "PIPE SAVED: "<< *response.getCGIpipeFD() << std::endl;
Debug::log("PIPE SAVED: " + toString(*response.getCGIpipeFD()), Debug::CGI);

close(pipeFD[1]);
EventData data = {1, pid, pipeFD[0], pipeFD[1]}; // Assuming 1 is the event type for CGI started

_eventManager.emit(data); // Emit event indicating a CGI process has started

_connection.addCGI(pid);
std::cout << GREEN << _connection.getCGIPid() << RESET << std::endl;
Debug::log("CGIHandler: CGI PID: " + toString(pid), Debug::CGI);

// clang-format off
std::vector<std::pair<int, int> > pipes = _eventManager.getPipeFDs();
for (std::vector<std::pair<int, int> >::const_iterator it = pipes.begin(); it != pipes.end(); ++it)
{
std::cout << GREEN << "CGIHandler: pipeFDs: " << (*it).first << RESET << std::endl;
}
// std::vector<std::pair<int, int> > pipes = _eventManager.getPipeFDs();
// for (std::vector<std::pair<int, int> >::const_iterator it = pipes.begin(); it != pipes.end(); ++it)
// {
// std::cout << GREEN << "CGIHandler: pipeFDs: " << (*it).first << RESET << std::endl;
// }
// clang-format on
std::cout << RED << "Exiting CGIHandler::executeCGI with true" << RESET << std::endl;
Debug::log("CGIHandler: Waiting for CGI to finish", Debug::CGI);
return true;
}

Expand Down
37 changes: 11 additions & 26 deletions src/Server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ void Server::startPollEventLoop()
while (1)
{
if (_hasCGI)
timeout = 1000; // 1 seconds
timeout = 500; // 0.5 seconds
else if (_clientCounter > 0)
{
std::cout << BLUE << "Client counter: " << _clientCounter << RESET << std::endl;
timeout = 5000; // 15 seconds
Debug::log("Client counter: " + toString(_clientCounter), Debug::SERVER);
timeout = 15000; // 15 seconds
}
else
timeout = -1;
Expand All @@ -85,8 +85,8 @@ void Server::startPollEventLoop()
" Waiting for new connection or Polling +++++++++++++++" + toString(RESET), Debug::SERVER);
int ret = poll(_FDs.data(), _FDs.size(), timeout);
pollCounter++;
printFrame("POLL EVENT DETECTED", true);
printConnections("AFTER POLL", _FDs, _connections, true);
//printFrame("POLL EVENT DETECTED", true);
//printConnections("AFTER POLL", _FDs, _connections, true);
if (ret > 0)
{
size_t originalSize = _FDs.size();
Expand Down Expand Up @@ -164,7 +164,7 @@ void Server::waitCGI()

double elapsed = difftime(time(NULL), _connections[i].getCGIStartTime());
Debug::log("Elapsed time: " + toString(elapsed) + " seconds", Debug::CGI);
if (_connections[i].getHasCGI() && elapsed > 1)
if (_connections[i].getHasCGI() && elapsed > 10) // 10 seconds
{
Debug::log("CGI timed out", Debug::NORMAL);

Expand Down Expand Up @@ -371,7 +371,7 @@ void Server::readCGIPipe(Connection &conn, HTTPResponse &response)
ssize_t bytesRead;

bytesRead = read(pipeFD[0], readBuffer, CGI_BUFFER_SIZE - 1);
std::cout << "Bytes read: " << bytesRead << std::endl;
Debug::log("Bytes read: " + toString(bytesRead), Debug::CGI);
if (bytesRead > 0)
{
readBuffer[bytesRead] = '\0';
Expand Down Expand Up @@ -485,8 +485,7 @@ void Server::handleConnection(Connection &conn, size_t &i)

if (conn.getHasReadSocket() && !conn.getHasFinishedReading())
return;
}
std::cout << request << std::endl;

if (!conn.getCanBeClosed() && !conn.getHasDataToSend())
buildResponse(conn, i, request, response);
// MInd that after the last read from the pipe of the CGI getHasReadSocket will be false but we will have a read
Expand All @@ -496,11 +495,6 @@ void Server::handleConnection(Connection &conn, size_t &i)

if (conn.getCanBeClosed())
closeClientConnection(conn, i);

// Validate the CGI pipe file descriptors before accessingjj
// TODO: following line get an overflow on mac
// std::cout << BLUE << *response.getCGIpipeFD() << RESET << std::endl;
std::cout << RED << "Exiting handleConnection" << RESET << std::endl;
}

/*** Private Methods ***/
Expand Down Expand Up @@ -754,8 +748,6 @@ void Server::acceptNewConnection(Connection &conn)
_FDs.push_back(newSocketPoll);
_connections.push_back(newConnection);
++_clientCounter;
std::cout << newConnection.getHasFinishedReading() << std::endl;
std::cout << _connections.back().getHasFinishedReading() << std::endl;
/* end together */
if (VERBOSE)
{
Expand Down Expand Up @@ -819,21 +811,19 @@ void Server::handleClientSocketError(int clientFD, size_t &i)
perror("poll client socket error");
}

// Is not the socket timeout, but the poll timeout
void Server::handleSocketTimeoutIfAny()
{
// Is not the socket timeout, but the poll timeout
std::cout << "Timeout occurred!" << std::endl;

// loop through the connections and check for timeout
for (size_t i = 0; i < _FDs.size(); i++)
{
if (_connections[i].getType() == SERVER || _connections[i].getStartTime() == 0)
continue;

double elapsed = difftime(time(NULL), _connections[i].getStartTime());
if (elapsed > 3)
if (elapsed > 10000) // 10 seconds
{
std::cout << RED << "Elapsed time: " << elapsed << " seconds" << RESET << std::endl;
Debug::log("Elapsed time: " + toString(elapsed) + " seconds", Debug::SERVER);
// We have to send a 408 Request Timeout
_connections[i].getResponse().setStatusCode(408, "Request Timeout");
buildResponse(_connections[i], i, _connections[i].getRequest(), _connections[i].getResponse());
Expand Down Expand Up @@ -973,11 +963,6 @@ void Server::findLocationBlock(HTTPRequest &request, ServerBlock &serverBlock, D
void Server::addPipeFDs(int pipe0, int pipe1)
{
_pipeFDs.push_back(std::make_pair(pipe0, pipe1));
// print the pipe fds
for (size_t i = 0; i < _pipeFDs.size(); i++)
{
std::cout << PURPLE << "Pipe FDs: " << _pipeFDs[i].first << RESET << std::endl;
}
}

// clang-format off
Expand Down
2 changes: 1 addition & 1 deletion src/events/EventManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void EventManager::emit(const EventData &eventData)
{
ServerEventListener *serverEventListener = dynamic_cast<ServerEventListener *>(*it);
(*it)->handleEvent(eventData);
std::cout << RED << serverEventListener->getServer().getCGICounter() << RESET << std::endl;
//std::cout << RED << serverEventListener->getServer().getCGICounter() << RESET << std::endl;
_pipeFDs = serverEventListener->getServer().getPipeFDs();
}
}
Expand Down

0 comments on commit 2f25aaa

Please sign in to comment.