Parser
Parser
#include <icy/http/parser.h>HTTP request/response parser using the llhttp library.
Public Methods
| Return | Name | Description |
|---|---|---|
Parser | Creates a response parser. The response object is populated as data is parsed. | |
Parser | Creates a request parser. The request object is populated as data is parsed. | |
Parser | Creates a parser of the given type without binding a message object. | |
Parser | Deleted constructor. | |
Parser | Deleted constructor. | |
ParseResult | parse | Feeds a buffer of raw HTTP data into the parser. |
void | reset | Reset the internal state (reinitialises llhttp). Safe to call externally, NOT from inside llhttp callbacks. |
void | resetState | Reset internal flags without reinitialising llhttp. Safe to call from inside llhttp callbacks (e.g. on_message_begin). |
bool | complete const | Returns true if parsing is complete, either in success or error. |
bool | upgrade const | Returns true if the connection should be upgraded. |
llhttp_type_t | type const inline | Returns the parser type (HTTP_REQUEST or HTTP_RESPONSE). |
void | setRequest | Binds an HTTP request object to populate during parsing. Must only be called when no message is currently set and type is HTTP_REQUEST. |
void | setResponse | Binds an HTTP response object to populate during parsing. Must only be called when no message is currently set and type is HTTP_RESPONSE. |
void | setObserver | Sets the observer that receives parser events. |
void | clearMessage | Clear request/response pointers so they can be re-set. Used when resetting a pooled connection for reuse. |
http::Message * | message | Returns the currently bound message (request or response), or nullptr. |
ParserObserver * | observer const | Returns 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
responseHTTP response object to populate.
Parser
Parser(http::Request * request)Creates a request parser. The request object is populated as data is parsed.
Parameters
requestHTTP request object to populate.
Parser
Parser(llhttp_type_t type)Creates a parser of the given type without binding a message object.
Parameters
typeEither HTTP_REQUEST or HTTP_RESPONSE.
Parser
Parser(const Parser &) = deleteDeleted constructor.
Parser
Parser(Parser &&) = deleteDeleted 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
dataPointer to the input data buffer.lengthNumber 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() constReturns true if parsing is complete, either in success or error.
upgrade
const
bool upgrade() constReturns true if the connection should be upgraded.
type
const inline
inline llhttp_type_t type() constReturns 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
requestThe 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
responseThe response object to populate.
setObserver
void setObserver(ParserObserver * observer)Sets the observer that receives parser events.
Parameters
observerObserver 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() constReturns the current parser observer, or nullptr if none is set.
Protected Attributes
| Return | Name | Description |
|---|---|---|
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 _scratchProtected Methods
| Return | Name | Description |
|---|---|---|
void | init | |
void | clearBoundMessage | |
void | storeHeader | |
void | applyScratchToBoundMessage | |
void | onHeader | Callbacks. |
void | onHeadersEnd | |
void | onBody | |
void | onMessageEnd | |
void | onError |
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)