feat(request): add parse error state

This commit is contained in:
Quinten Mennen 2025-09-24 11:52:31 +02:00
parent e32b963689
commit 7270ee1812
2 changed files with 5 additions and 12 deletions

View File

@ -36,14 +36,6 @@ const std::string &HttpRequest::getBody() const
void HttpRequest::receiveData(const char *data, size_t length) void HttpRequest::receiveData(const char *data, size_t length)
{ {
if (buffer_.size() + length > Http::Protocol::MAX_HEADER_SIZE &&
state_ == State::Headers) // !: This is a define but should be configurable
{
Log::warning("Request size exceeds maximum limit of " + std::to_string(Http::Protocol::MAX_HEADER_SIZE) +
" bytes");
state_ = State::Complete; // Mark as complete to avoid further processing
return;
}
Log::trace(LOCATION); Log::trace(LOCATION);
buffer_.append(data, length); buffer_.append(data, length);
parseBuffer(); parseBuffer();
@ -82,6 +74,7 @@ void HttpRequest::parseBuffer()
case State::Complete: case State::Complete:
Log::debug("HttpRequest::parseBuffer() request is complete"); Log::debug("HttpRequest::parseBuffer() request is complete");
return; // Request is complete return; // Request is complete
case State::ParseError: Log::warning("Parse error occurred, stopping further processing"); return;
} }
} }
} }
@ -110,7 +103,7 @@ bool HttpRequest::parseBufferforRequestLine()
if (parts.size() != 3) if (parts.size() != 3)
{ {
Log::warning("Invalid request line: " + requestLine_); Log::warning("Invalid request line: " + requestLine_);
state_ = State::Complete; // Mark as complete to avoid further processing state_ = State::ParseError; // Mark as complete to avoid further processing
return true; return true;
} }
method_ = parts[0]; method_ = parts[0];
@ -169,7 +162,7 @@ bool HttpRequest::parseBufferforChunkedBody()
catch (const std::exception &e) catch (const std::exception &e)
{ {
Log::warning("Invalid chunk size: " + chunkSizeStr + " (" + e.what() + ")"); Log::warning("Invalid chunk size: " + chunkSizeStr + " (" + e.what() + ")");
state_ = State::Complete; // Mark as complete to avoid further processing state_ = State::ParseError; // Mark as complete to avoid further processing
return true; return true;
} }
if (chunkSize == 0) if (chunkSize == 0)
@ -178,7 +171,6 @@ bool HttpRequest::parseBufferforChunkedBody()
buffer_.erase(0, pos + Http::Protocol::CRLF.size()); buffer_.erase(0, pos + Http::Protocol::CRLF.size());
return true; return true;
} }
// TODO: Copilot added this but I don't understand why it's needed
if (buffer_.size() < pos + Http::Protocol::CRLF.size() + chunkSize + Http::Protocol::CRLF.size()) if (buffer_.size() < pos + Http::Protocol::CRLF.size() + chunkSize + Http::Protocol::CRLF.size())
{ {
Log::debug("Chunked body waiting for more data: " + LOCATION); Log::debug("Chunked body waiting for more data: " + LOCATION);

View File

@ -19,7 +19,8 @@ class HttpRequest
Headers, Headers,
Body, Body,
Chunked, Chunked,
Complete Complete,
ParseError
}; };
HttpRequest(const ServerConfig *serverConfig, const Client *client); HttpRequest(const ServerConfig *serverConfig, const Client *client);