PicoHTTPParser Documentation

repository·master·Indexed 24 days ago

https://github.com/h2o/picohttpparser

A lightweight, stateless, and high-performance HTTP request/response parser designed for minimal memory allocation. It provides functions for parsing requests via phr_parse_request, responses via phr_parse_response, and headers via phr_parse_headers, as well as in-place chunked-encoding decoding using phr_decode_chunked.

Tokens
439
Snippets
0
Records
4
Agent score
35%

What's inside picohttpparser

  1. How PicoHTTPParser works

    master

    PicoHTTPParser is a tiny, primitive, and fast HTTP request/response parser.

    Key architectural characteristics:

    • Stateless: The parser does not maintain internal state between calls.
    • Zero-allocation: It does not allocate memory itself. Instead, it accepts a pointer to a buffer and an output structure, then sets the pointers within that structure to point to the relevant portions of the provided buffer.
    • In-place processing: For certain operations like chunked decoding, data is processed in-place.
  2. Decode chunked-encoding data with phr_decode_chunked

    master

    Use phr_decode_chunked to decode data received via chunked-encoding. The decoding happens in-place within the provided buffer.

    Configuration:

    • Use the phr_chunked_decoder structure to manage the decoding state.
    • Set decoder.consume_trailer = 1 to automatically discard the trailing header. If set to 0, the application is responsible for calling phr_parse_headers to parse the trailing header manually.

    Return Values:

    • > 0: Successfully decoded some data (returns the number of bytes processed).
    • -1: Parse error.
    • -2: More data is needed to complete the current chunk.
  3. Parse HTTP responses and headers with phr_parse_response and phr_parse_headers

    master

    The library provides two additional functions for handling responses:

    • phr_parse_response: Parses an entire HTTP response.
    • phr_parse_headers: Parses only the HTTP headers.

    These functions follow a similar interface and logic to phr_parse_request.

  4. Parse an HTTP request with phr_parse_request

    master

    Use phr_parse_request to parse an incoming HTTP request from a buffer. The function populates pointers for the method, path, and HTTP minor version, and fills an array of phr_header structures.

    Return Values:

    • > 0: Successfully parsed the request (returns the number of bytes parsed).
    • -1: Parse error.
    • -2: The request is incomplete (more data is needed).