feat: handle cgi exit status

This commit is contained in:
Quinten 2025-10-30 15:58:33 +01:00
parent 4a41b678c3
commit 2e59cb779d
3 changed files with 12 additions and 10 deletions

View File

@ -177,7 +177,7 @@ void CgiHandler::parseCgiOutput()
Log::debug("CGI output headers not complete yet");
return;
}
if (header.substr(static_cast<long>(headerEnd), 2) == "\r\n")
{
headerSeperatorSize = 4;
@ -190,7 +190,7 @@ void CgiHandler::parseCgiOutput()
parseCgiHeaders(headers);
buffer_.erase(buffer_.begin(), buffer_.begin() + static_cast<long>(headerEnd) + headerSeperatorSize);
parseCgiBody();
finalizeCgiResponse();
}
void CgiHandler::parseCgiHeaders(std::string &headers)
@ -265,13 +265,13 @@ void CgiHandler::handleTimeout()
// cancelTimer();
}
void CgiHandler::parseCgiBody()
void CgiHandler::finalizeCgiResponse()
{
Log::trace(LOCATION);
auto status = response_.getHeaders().get("Status");
if (cgiProcess_->getExitCode() > 0 && !status.empty())
wait();
if (cgiProcess_->getExitCode() > 0 && status.empty())
{
response_.setStatus(500);
}
@ -288,4 +288,4 @@ void CgiHandler::appendToBuffer(const char *data, size_t length)
{
Log::trace(LOCATION);
buffer_.insert(buffer_.end(), data, data + length);
}
}

View File

@ -45,7 +45,7 @@ class CgiHandler : public AHandler
std::unique_ptr<CgiSocket> cgiStdErr_;
void parseCgiOutput();
void parseCgiHeaders(std::string &headers);
void parseCgiBody();
void finalizeCgiResponse();
void appendToBuffer(const char *data, size_t length);
int pid_ = -1;

View File

@ -18,7 +18,8 @@
#include <sys/wait.h> // for waitpid, WNOHANG
#include <unistd.h> // for close, dup2, pipe2, execve, fork, STDERR_FILENO, STDIN_FILENO, STDOUT_FILENO
CgiProcess::CgiProcess(const HttpRequest &request, CgiHandler &handler) : request_(request), handler_(handler), pid_(-1), status_(-1)
CgiProcess::CgiProcess(const HttpRequest &request, CgiHandler &handler)
: request_(request), handler_(handler), pid_(-1), status_(-1)
{
if (!request_.getUri().isCgi())
{
@ -133,8 +134,9 @@ void CgiProcess::wait() noexcept
return;
}
Log::debug("CGI process with PID " + std::to_string(pid_) + " has terminated with status " + std::to_string(status));
status_ = status;
Log::debug("CGI process with PID " + std::to_string(pid_) + " has terminated with status "
+ std::to_string(WEXITSTATUS(status)));
status_ = WEXITSTATUS(status);
pid_ = -1;
}
}