Webserv
Loading...
Searching...
No Matches
http::RequestParser Class Reference

#include <RequestParser.hpp>

Public Types

enum  State {
  READING_HEADERS , HEADERS_READY , READING_BODY , REQUEST_READY ,
  ERROR
}

Public Member Functions

 RequestParser (Request &req, size_t maxHeaderSize)
void reset ()
 Resets the parser and the associated Request object for reuse.
State feed (char const *chunk, size_t size)
 Feeds a chunk of raw socket data into the parser's state machine.
State proceedReadingBody ()
 Resumes parsing after the HEADERS_READY pause.
State state () const
HttpStatus errorStatus () const
bool hasLeftoverData () const

Private Member Functions

State parseHeaders ()
 Internal: Tries to find and parse the full header block.
State parseBody ()
 Internal: Tries to consume data from the buffer for the body.
void handleChunkedBody ()
void handleContentLengthBody ()
bool writeToBodyFile (const std::string &data)
State setError (HttpStatus status)
 Sets the parser to an error state and updates the Request's status.
void setRequestReady ()

Private Attributes

State state_
HttpStatus errorStatus_
std::string buffer_
size_t maxHeaderSize_
size_t maxContentSize_
size_t contentLength_
size_t bytesWrittenToBody_
bool isContentChunked_
Requestrequest_
ChunkedBodyParser chunkParser_

Member Enumeration Documentation

◆ State

Enumerator
READING_HEADERS 
HEADERS_READY 
READING_BODY 
REQUEST_READY 
ERROR 

Constructor & Destructor Documentation

◆ RequestParser()

http::RequestParser::RequestParser ( Request & req,
size_t maxHeaderSize )
Parameters
requestThe Request object context to populate.
maxHeaderSizeHard limit on header block size (prevents OOM).

Member Function Documentation

◆ errorStatus()

HttpStatus http::RequestParser::errorStatus ( ) const

◆ feed()

RequestParser::State http::RequestParser::feed ( char const * chunk,
size_t size )

Feeds a chunk of raw socket data into the parser's state machine.

Parameters
chunkPointer to the data buffer.
sizeNumber of bytes in the buffer.
Returns
The new state of the parser after consuming the chunk.

◆ handleChunkedBody()

void http::RequestParser::handleChunkedBody ( )
private

◆ handleContentLengthBody()

void http::RequestParser::handleContentLengthBody ( )
private

◆ hasLeftoverData()

bool http::RequestParser::hasLeftoverData ( ) const
inline

◆ parseBody()

RequestParser::State http::RequestParser::parseBody ( )
private

Internal: Tries to consume data from the buffer for the body.

Returns
New state (READING_BODY or REQUEST_READY).

◆ parseHeaders()

RequestParser::State http::RequestParser::parseHeaders ( )
private

Internal: Tries to find and parse the full header block.

Returns
New state (READING_HEADERS or HEADERS_READY).

◆ proceedReadingBody()

RequestParser::State http::RequestParser::proceedReadingBody ( )

Resumes parsing after the HEADERS_READY pause.

This tells the parser to check the body-related headers (Content-Length, Transfer-Encoding) and, crucially, the policy from the (now-routed) Request object to prepare for body reading.

Precondition
Call this only when state is HEADERS_READY.
Returns
The new state (e.g., READING_BODY or REQUEST_READY if no body).

◆ reset()

void http::RequestParser::reset ( )

Resets the parser and the associated Request object for reuse.

◆ setError()

RequestParser::State http::RequestParser::setError ( HttpStatus status)
private

Sets the parser to an error state and updates the Request's status.

◆ setRequestReady()

void http::RequestParser::setRequestReady ( )
private

◆ state()

RequestParser::State http::RequestParser::state ( ) const

◆ writeToBodyFile()

bool http::RequestParser::writeToBodyFile ( const std::string & data)
private

Member Data Documentation

◆ buffer_

std::string http::RequestParser::buffer_
private

◆ bytesWrittenToBody_

size_t http::RequestParser::bytesWrittenToBody_
private

◆ chunkParser_

ChunkedBodyParser http::RequestParser::chunkParser_
private

◆ contentLength_

size_t http::RequestParser::contentLength_
private

◆ errorStatus_

HttpStatus http::RequestParser::errorStatus_
private

◆ isContentChunked_

bool http::RequestParser::isContentChunked_
private

◆ maxContentSize_

size_t http::RequestParser::maxContentSize_
private

◆ maxHeaderSize_

size_t http::RequestParser::maxHeaderSize_
private

◆ request_

Request& http::RequestParser::request_
private

◆ state_

State http::RequestParser::state_
private

The documentation for this class was generated from the following files: