HTTP module

Parser

HTTP request/response parser using the llhttp library.

#include <icy/http/parser.h>

class Parser

Defined in src/http/include/icy/http/parser.h:55

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.
~Parser
ParserDeleted constructor.
Parser &operator=Deleted assignment operator.
ParserDeleted constructor.
Parser &operator=Deleted assignment operator.
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)

Defined in src/http/include/icy/http/parser.h:72

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

Parameters

ParameterTypeDescription
responsehttp::Response *HTTP response object to populate.

Parser

Parser(http::Request * request)

Defined in src/http/include/icy/http/parser.h:76

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

Parameters

ParameterTypeDescription
requesthttp::Request *HTTP request object to populate.

Parser

Parser(llhttp_type_t type)

Defined in src/http/include/icy/http/parser.h:80

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

Parameters

ParameterTypeDescription
typellhttp_type_tEither HTTP_REQUEST or HTTP_RESPONSE.

~Parser

~Parser()

Defined in src/http/include/icy/http/parser.h:81


Parser

Parser(const Parser &) = delete

Defined in src/http/include/icy/http/parser.h:83

Deleted constructor.


operator=

Parser & operator=(const Parser &) = delete

Defined in src/http/include/icy/http/parser.h:84

Deleted assignment operator.


Parser

Parser(Parser &&) = delete

Defined in src/http/include/icy/http/parser.h:85

Deleted constructor.


operator=

Parser & operator=(Parser &&) = delete

Defined in src/http/include/icy/http/parser.h:86

Deleted assignment operator.


parse

ParseResult parse(const char * data, size_t length)

Defined in src/http/include/icy/http/parser.h:95

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.

Returns

Structured parse result including bytes consumed and terminal state.

Parameters

ParameterTypeDescription
dataconst char *Pointer to the input data buffer.
lengthsize_tNumber of bytes in the buffer.

reset

void reset()

Defined in src/http/include/icy/http/parser.h:99

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


resetState

void resetState()

Defined in src/http/include/icy/http/parser.h:103

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


complete

const

bool complete() const

Defined in src/http/include/icy/http/parser.h:107

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


upgrade

const

bool upgrade() const

Defined in src/http/include/icy/http/parser.h:110

Returns true if the connection should be upgraded.


type

const inline

inline llhttp_type_t type() const

Defined in src/http/include/icy/http/parser.h:113

Returns the parser type (HTTP_REQUEST or HTTP_RESPONSE).


setRequest

void setRequest(http::Request * request)

Defined in src/http/include/icy/http/parser.h:118

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

ParameterTypeDescription
requesthttp::Request *The request object to populate.

setResponse

void setResponse(http::Response * response)

Defined in src/http/include/icy/http/parser.h:123

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

ParameterTypeDescription
responsehttp::Response *The response object to populate.

setObserver

void setObserver(ParserObserver * observer)

Defined in src/http/include/icy/http/parser.h:127

Sets the observer that receives parser events.

Parameters

ParameterTypeDescription
observerParserObserver *Observer to notify. May be nullptr to clear.

clearMessage

void clearMessage()

Defined in src/http/include/icy/http/parser.h:131

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


message

http::Message * message()

Defined in src/http/include/icy/http/parser.h:134

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


observer

const

ParserObserver * observer() const

Defined in src/http/include/icy/http/parser.h:137

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

Protected Attributes


_observer

ParserObserver * _observer

Type: ParserObserver *

Defined in src/http/include/icy/http/parser.h:185


_request

http::Request * _request

Type: http::Request *

Defined in src/http/include/icy/http/parser.h:186


_response

http::Response * _response

Type: http::Response *

Defined in src/http/include/icy/http/parser.h:187


_message

http::Message * _message

Type: http::Message *

Defined in src/http/include/icy/http/parser.h:188


_parser

llhttp_t _parser

Defined in src/http/include/icy/http/parser.h:190


_settings

llhttp_settings_t _settings

Defined in src/http/include/icy/http/parser.h:191


_type

llhttp_type_t _type

Defined in src/http/include/icy/http/parser.h:192


_wasHeaderValue

bool _wasHeaderValue

Defined in src/http/include/icy/http/parser.h:194


_lastHeaderField

std::string _lastHeaderField

Defined in src/http/include/icy/http/parser.h:195


_lastHeaderValue

std::string _lastHeaderValue

Defined in src/http/include/icy/http/parser.h:196


_complete

bool _complete

Defined in src/http/include/icy/http/parser.h:198


_upgrade

bool _upgrade

Defined in src/http/include/icy/http/parser.h:199


_error

Error _error

Type: Error

Defined in src/http/include/icy/http/parser.h:201


_lastResult

ParseResult _lastResult

Type: ParseResult

Defined in src/http/include/icy/http/parser.h:202


_scratch

MessageScratch _scratch

Type: MessageScratch

Defined in src/http/include/icy/http/parser.h:203

Protected Methods

ReturnNameDescription
voidinit
voidclearBoundMessage
voidstoreHeader
voidapplyScratchToBoundMessage
voidonHeaderCallbacks.
voidonHeadersEnd
voidonBody
voidonMessageEnd
voidonError

init

void init()

Defined in src/http/include/icy/http/parser.h:162


clearBoundMessage

void clearBoundMessage()

Defined in src/http/include/icy/http/parser.h:163


storeHeader

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

Defined in src/http/include/icy/http/parser.h:164


applyScratchToBoundMessage

void applyScratchToBoundMessage()

Defined in src/http/include/icy/http/parser.h:165


onHeader

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

Defined in src/http/include/icy/http/parser.h:168

Callbacks.


onHeadersEnd

void onHeadersEnd()

Defined in src/http/include/icy/http/parser.h:169


onBody

void onBody(const char * buf, size_t len)

Defined in src/http/include/icy/http/parser.h:170


onMessageEnd

void onMessageEnd()

Defined in src/http/include/icy/http/parser.h:171


onError

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

Defined in src/http/include/icy/http/parser.h:172