Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

HTTP Parser

Incremental HTTP/1.x parser used by the HTTP gclasses. Feeds bytes from a gbuffer_t and invokes callbacks on each complete header, body chunk, and message.

Source code:

ghttp_parser_create()

Creates and initializes a new GHTTP_PARSER instance for parsing HTTP messages. The parser processes incoming HTTP data and triggers events when headers, body, or complete messages are received.

GHTTP_PARSER *ghttp_parser_create(
    hgobj gobj,
    llhttp_type_t type,
    gobj_event_t on_header_event,
    gobj_event_t on_body_event,
    gobj_event_t on_message_event,
    BOOL send_event
);

Parameters

KeyTypeDescription
gobjhgobjThe parent GObj that will handle the parsed HTTP events.
typeenum http_parser_typeThe type of HTTP parser (HTTP_REQUEST, HTTP_RESPONSE, or HTTP_BOTH).
on_header_eventgobj_event_tThe event triggered when HTTP headers are fully received.
on_body_eventgobj_event_tThe event triggered when a portion of the HTTP body is received.
on_message_eventgobj_event_tThe event triggered when the entire HTTP message is received.
send_eventBOOLIf TRUE, events are sent using gobj_send_event(); otherwise, they are published using gobj_publish_event().

Returns

A pointer to the newly created GHTTP_PARSER instance, or NULL if memory allocation fails.

Notes

The returned GHTTP_PARSER instance must be destroyed using ghttp_parser_destroy() when no longer needed.


API break (7.3.0): ghttp_parser_reset() has been removed from the public API. Calling it from inside an llhttp callback (for example, on_message_complete) corrupted llhttp’s internal state machine and silently swallowed pipelined HTTP/1.1 messages. Callers that need a fresh parser for a new connection should destroy the current parser and create a new one via ghttp_parser_create() — see c_prot_http_sr::ac_connected, c_prot_http_cl::ac_connected and c_websocket::ac_connected for the canonical pattern.


ghttp_parser_destroy()

Releases all resources associated with a GHTTP_PARSER instance, including allocated memory and internal buffers.

void ghttp_parser_destroy(GHTTP_PARSER *parser);

Parameters

KeyTypeDescription
parserGHTTP_PARSER *Pointer to the GHTTP_PARSER instance to be destroyed.

Returns

This function does not return a value.

Notes

After calling ghttp_parser_destroy(), the parser pointer should not be used as it becomes invalid.


ghttp_parser_received()

Parses an HTTP message from the given buffer using the ghttp_parser_create() instance.

int ghttp_parser_received(
    GHTTP_PARSER *parser,
    char *bf,
    size_t len
);

Parameters

KeyTypeDescription
parserGHTTP_PARSER *Pointer to the GHTTP_PARSER instance handling the HTTP parsing.
bfchar *Pointer to the buffer containing the HTTP message data.
lensize_tNumber of bytes available in bf for parsing.

Returns

Returns the number of bytes successfully parsed. Returns -1 if an error occurs during parsing.

Notes

[‘This function uses http_parser_execute() to process the HTTP message.’, ‘If the parser encounters an error, it logs the issue and returns -1.’, ‘The function checks for upgrade requests and handles them accordingly.’]


ghttp_parser_finish()

Signals end-of-stream to the underlying llhttp parser (the TCP peer closed the socket). Required to complete HTTP messages whose terminator is the connection close itself — HTTP/1.0 responses without Content-Length, and HTTP/1.1 responses with neither Content-Length nor Transfer-Encoding: chunked. Without this call, on_message_complete would never fire for such messages and the subscriber would miss EV_ON_MESSAGE.

int ghttp_parser_finish(GHTTP_PARSER *parser);

Parameters

KeyTypeDescription
parserGHTTP_PARSER *Pointer to the GHTTP_PARSER instance.

Returns

0 on success, -1 on protocol error (e.g. a partial message was in flight when the peer closed).

Notes

Call it from the gobj’s disconnect handler (ac_disconnected / mt_stop). Never call it from inside an llhttp callback.