HTTP module

Parser

HTTP request/response parser using the llhttp library.

Parser

#include <icy/http/parser.h>

HTTP request/response parser using the llhttp library.

Public Methods

ReturnNameDescription
ParserCreates a response parser. The response object is populated as data is parsed.
ParserCreates a request parser. The request object is populated as data is parsed.
ParserCreates a parser of the given type without binding a message object.
ParserDeleted constructor.
ParserDeleted constructor.
ParseResultparseFeeds a buffer of raw HTTP data into the parser.
voidresetReset the internal state (reinitialises llhttp). Safe to call externally, NOT from inside llhttp callbacks.
voidresetStateReset internal flags without reinitialising llhttp. Safe to call from inside llhttp callbacks (e.g. on_message_begin).
boolcomplete constReturns true if parsing is complete, either in success or error.
boolupgrade constReturns true if the connection should be upgraded.
llhttp_type_ttype const inlineReturns the parser type (HTTP_REQUEST or HTTP_RESPONSE).
voidsetRequestBinds an HTTP request object to populate during parsing. Must only be called when no message is currently set and type is HTTP_REQUEST.
voidsetResponseBinds an HTTP response object to populate during parsing. Must only be called when no message is currently set and type is HTTP_RESPONSE.
voidsetObserverSets the observer that receives parser events.
voidclearMessageClear request/response pointers so they can be re-set. Used when resetting a pooled connection for reuse.
http::Message *messageReturns the currently bound message (request or response), or nullptr.
ParserObserver *observer constReturns the current parser observer, or nullptr if none is set.

Parser

Parser(http::Response * response)

Creates a response parser. The response object is populated as data is parsed.

Parameters

  • response HTTP response object to populate.

Parser

Parser(http::Request * request)

Creates a request parser. The request object is populated as data is parsed.

Parameters

  • request HTTP request object to populate.

Parser

Parser(llhttp_type_t type)

Creates a parser of the given type without binding a message object.

Parameters

  • type Either HTTP_REQUEST or HTTP_RESPONSE.

Parser

Parser(const Parser &) = delete

Deleted constructor.


Parser

Parser(Parser &&) = delete

Deleted constructor.


parse

ParseResult parse(const char * data, size_t length)

Feeds a buffer of raw HTTP data into the parser.

May be called multiple times for streaming data. The parser state persists between calls. On completion or error, the observer is notified.

Parameters

  • data Pointer to the input data buffer.

  • length Number of bytes in the buffer.

Returns

Structured parse result including bytes consumed and terminal state.


reset

void reset()

Reset the internal state (reinitialises llhttp). Safe to call externally, NOT from inside llhttp callbacks.


resetState

void resetState()

Reset internal flags without reinitialising llhttp. Safe to call from inside llhttp callbacks (e.g. on_message_begin).


complete

const

bool complete() const

Returns true if parsing is complete, either in success or error.


upgrade

const

bool upgrade() const

Returns true if the connection should be upgraded.


type

const inline

inline llhttp_type_t type() const

Returns the parser type (HTTP_REQUEST or HTTP_RESPONSE).


setRequest

void setRequest(http::Request * request)

Binds an HTTP request object to populate during parsing. Must only be called when no message is currently set and type is HTTP_REQUEST.

Parameters

  • request The request object to populate.

setResponse

void setResponse(http::Response * response)

Binds an HTTP response object to populate during parsing. Must only be called when no message is currently set and type is HTTP_RESPONSE.

Parameters

  • response The response object to populate.

setObserver

void setObserver(ParserObserver * observer)

Sets the observer that receives parser events.

Parameters

  • observer Observer to notify. May be nullptr to clear.

clearMessage

void clearMessage()

Clear request/response pointers so they can be re-set. Used when resetting a pooled connection for reuse.


message

http::Message * message()

Returns the currently bound message (request or response), or nullptr.


observer

const

ParserObserver * observer() const

Returns the current parser observer, or nullptr if none is set.

Protected Attributes

ReturnNameDescription
ParserObserver *_observer
http::Request *_request
http::Response *_response
http::Message *_message
llhttp_t_parser
llhttp_settings_t_settings
llhttp_type_t_type
bool_wasHeaderValue
std::string_lastHeaderField
std::string_lastHeaderValue
bool_complete
bool_upgrade
Error_error
ParseResult_lastResult
MessageScratch_scratch

_observer

ParserObserver * _observer

_request

http::Request * _request

_response

http::Response * _response

_message

http::Message * _message

_parser

llhttp_t _parser

_settings

llhttp_settings_t _settings

_type

llhttp_type_t _type

_wasHeaderValue

bool _wasHeaderValue

_lastHeaderField

std::string _lastHeaderField

_lastHeaderValue

std::string _lastHeaderValue

_complete

bool _complete

_upgrade

bool _upgrade

_error

Error _error

_lastResult

ParseResult _lastResult

_scratch

MessageScratch _scratch

Protected Methods

ReturnNameDescription
voidinit
voidclearBoundMessage
voidstoreHeader
voidapplyScratchToBoundMessage
voidonHeaderCallbacks.
voidonHeadersEnd
voidonBody
voidonMessageEnd
voidonError

init

void init()

clearBoundMessage

void clearBoundMessage()

storeHeader

void storeHeader(std::string name, std::string value)

applyScratchToBoundMessage

void applyScratchToBoundMessage()

onHeader

void onHeader(std::string name, std::string value)

Callbacks.


onHeadersEnd

void onHeadersEnd()

onBody

void onBody(const char * buf, size_t len)

onMessageEnd

void onMessageEnd()

onError

void onError(llhttp_errno_t errnum, const std::string & message)