minimal fix for segfault

This commit is contained in:
whaffman 2025-10-21 15:11:39 +02:00
parent 6028e0c1fe
commit 7951478edf
3 changed files with 29 additions and 9 deletions

View File

@ -1,4 +1,5 @@
#include "webserv/handler/CgiHandler.hpp"
#include "webserv/handler/ErrorHandler.hpp"
#include "webserv/socket/ASocket.hpp"
#include "webserv/socket/CgiSocket.hpp"
@ -86,9 +87,16 @@ void Client::request()
});
// server_.responseReady(client_socket_->getFd());
handler_ = router_->handleRequest();
if (handler_ != nullptr)
{
handler_->handle();
}
else
{
ErrorHandler::createErrorResponse(500, *httpResponse_);
}
}
else
{
Log::debug("Received partial request",
{
@ -132,7 +140,6 @@ void Client::poll() const
clientSocket_->setCallback([this]() { respond(); });
server_.writable(clientSocket_->getFd());
}
}
void Client::respond() const

View File

@ -1,4 +1,5 @@
#include "webserv/config/ConfigManager.hpp"
#include "webserv/config/ServerConfig.hpp"
#include "webserv/handler/URI.hpp"
#include <webserv/client/Client.hpp> // for Client
@ -34,8 +35,16 @@ void HttpRequest::setState(State state)
if (state == State::Complete)
{
// TODO: segfault if server does not exist
std::string hostHeader = getHeaders().getHost().value_or("");
ServerConfig *serverConfig = ConfigManager::getInstance().getMatchingServerConfig(hostHeader);
if (hostHeader.empty() || serverConfig == nullptr)
{
Log::error("No matching server config found for host: " + hostHeader);
state_ = State::ParseError;
return;
}
uri_ = std::make_unique<URI>(
*this, *ConfigManager::getInstance().getMatchingServerConfig(getHeaders().getHost().value_or("")));
*this, *serverConfig);
}
state_ = state;
}
@ -93,9 +102,7 @@ void HttpRequest::parseBuffer()
case State::Complete:
Log::debug("HttpRequest::parseBuffer() request is complete");
return; // Request is complete
case State::ParseError:
Log::warning("Parse error occurred, stopping further processing");
return;
case State::ParseError: Log::warning("Parse error occurred, stopping further processing"); return;
}
}
catch (...)

View File

@ -38,6 +38,12 @@ std::unique_ptr<AHandler> Router::handleRequest()
Log::trace(LOCATION);
HttpRequest &request = client_->getHttpRequest();
if (request.getState() == HttpRequest::State::ParseError)
{
Log::error("Router::handleRequest() called with incomplete request");
return nullptr;
}
HttpResponse &response = client_->getHttpResponse();
const std::string &target = request.getTarget();