#HTTP/1.x Parser
#define FIO_HTTP1_PARSER
#include FIO_INCLUDE_FILESmall parser, sharp teeth. Define FIO_HTTP1_PARSER to add the static
HTTP/1.x request / response parser used by the HTTP layer. It performs no heap
allocations, stores only parser state, and reports parsed data through callbacks
implemented by the including translation unit.
Nearby context: [IO and HTTP overview](./400 io-overview.md), the higher-level [HTTP module header](./439 http.h), and the neighboring [WebSocket parser header](./004 websocket parser.h).
#What Gets Added
FIO_HTTP1_PARSER exposes:
fio_http1_parser_s— parser state.FIO_HTTP1_PARSER_INIT— zero-initializer / reset value.fio_http1_parse— incremental parser entry point.- parser state helpers:
fio_http1_parser_is_emptyfio_http1_parser_is_on_headerfio_http1_parser_is_on_bodyfio_http1_expectedfio_http1_parser_skip_bodyfio_http1_parser_skips_body
- parse result / expected-body constants:
FIO_HTTP1_PARSER_ERRORFIO_HTTP1_EXPECTED_CHUNKED
- required user callbacks named
fio_http1_on_*.
The implementation also declares internal parsing stages named with
fio_http1___...; these are private implementation details.
#Parser State
#fio_http1_parser_s
typedef struct fio_http1_parser_s fio_http1_parser_s;
struct fio_http1_parser_s {
int (*fn)(fio_http1_parser_s *, fio_buf_info_s *, void *);
size_t expected;
size_t flags;
};The parser state is intentionally tiny: one function pointer for the current
state-machine stage, one expected byte counter / sentinel, and a flags
word for per-message state bits (e.g., an accepted Expect: 100-continue).
Parser state is never inferred from function pointer identity, so linker
identical-code-folding (MSVC / lld /OPT:ICF) cannot alter parsing behavior.
Treat all fields as opaque. Allocate the struct wherever it fits your lifetime
(stack, connection object, arena, etc.), initialize it with
FIO_HTTP1_PARSER_INIT, and use the helper functions to inspect state.
#FIO_HTTP1_PARSER_INIT
#define FIO_HTTP1_PARSER_INIT ((fio_http1_parser_s){0})Zero-initializes a parser:
fio_http1_parser_s parser = FIO_HTTP1_PARSER_INIT;The parser also resets itself to this empty state after a complete message is
reported with fio_http1_on_complete.
#Parsing API
#fio_http1_parse
FIO_SFUNC size_t fio_http1_parse(fio_http1_parser_s *p,
fio_buf_info_s buf,
void *udata);Parses as much HTTP/1.x data as currently possible and invokes callbacks as fields are discovered.
pis the parser state.bufis the current readable bytes.udatais passed unchanged to every callback.
Returns the number of bytes consumed from buf, or
FIO_HTTP1_PARSER_ERROR ((size_t)-1) on parse / callback error.
A successful return may consume fewer bytes than supplied. Any unconsumed bytes belong to a later parse step or to the next HTTP message on a keep-alive connection.
If the parser needs more bytes before it can make progress, it returns
successfully with the bytes consumed so far, which can be 0.
#State Helpers
FIO_IFUNC size_t fio_http1_parser_is_empty(fio_http1_parser_s *p);
FIO_IFUNC size_t fio_http1_parser_is_on_header(fio_http1_parser_s *p);
FIO_IFUNC size_t fio_http1_parser_is_on_body(fio_http1_parser_s *p);
FIO_IFUNC size_t fio_http1_expected(fio_http1_parser_s *p);
FIO_IFUNC void fio_http1_parser_skip_body(fio_http1_parser_s *p);
FIO_IFUNC size_t fio_http1_parser_skips_body(fio_http1_parser_s *p);
FIO_IFUNC int fio_http1_version_is_legacy(fio_buf_info_s version);fio_http1_parser_is_emptyreturns non-zero when the parser is waiting for a new request / response line.fio_http1_parser_is_on_headerreturns non-zero while reading regular headers or chunked trailer headers.fio_http1_parser_is_on_bodyreturns non-zero while reading a known-length body or while the chunked parser is ready to read the next chunk frame. During a split chunk payload, the internal chunk-read stage may report false even though body bytes are still being drained.fio_http1_expectedreturns the parser's current expected byte count, returnsFIO_HTTP1_EXPECTED_CHUNKEDafter a chunked body is detected and before the next chunk-size line is parsed, and returns0when the parser's internal state marks the message as having no body. During chunked payload delivery, it may expose the current chunk size / remaining chunk bytes.fio_http1_parser_skip_bodymarks the current message as having no body, regardless of anycontent-length/transfer-encodingheaders. Call it from a parser callback before the headers end (normallyon_status), e.g., when parsing the response to aHEADrequest — the parser can't know the request method. The mark is cleared when the message completes. See Messages Without a Body below.fio_http1_parser_skips_bodyreturns non-zero when the current message has no body. It is known as soon as the first line is parsed (before any header callback). Test it before reserving body space. See Messages Without a Body.fio_http1_version_is_legacyreturns non-zero if a version string (as passed tofio_http1_on_version) is exactlyHTTP/1.0(case-sensitive). HTTP/1.0 messages are not persistent without akeep-aliveconnection option and never use chunked transfer coding (RFC 9112 §9.3, §6.1). Every other version string gets HTTP/1.1 semantics. HTTP/0.9 is unsupported: its version-less request line is rejected, and aHTTP/0.9version string is treated as HTTP/1.1 (i.e.,hostis required).
#Constants
#define FIO_HTTP1_PARSER_ERROR ((size_t)-1)
#define FIO_HTTP1_EXPECTED_CHUNKED ((size_t)(-2))FIO_HTTP1_PARSER_ERROR is the error return value from fio_http1_parse.
After a parse error, close / discard the stream state rather than attempting to
recover the same parser instance.
FIO_HTTP1_EXPECTED_CHUNKED is the parser's sentinel after
transfer-encoding: chunked is accepted and before a chunk-size line is parsed.
Once chunk parsing starts, fio_http1_expected may instead report the current
chunk size or remaining chunk bytes.
The header also defines FIO___HTTP1_BODY_NOT_ALLOWED as an internal sentinel
for methods / states where a body should not be read. User code should rely on
fio_http1_expected(p) == 0 instead of using that internal macro.
#Callback Contract
The parser declares these callbacks as static prototypes. The including
translation unit must define them.
For portable user code, every int callback should return only 0 to continue
or -1 to reject the parse.
The parser's internal checks are not identical for every callback:
fio_http1_on_method,fio_http1_on_url,fio_http1_on_version,fio_http1_on_status, andfio_http1_on_body_chunktreat any non-zero return as a parse error.fio_http1_on_headerduring normal headers andfio_http1_on_header_content_lengthreject only an exact-1return.fio_http1_on_headerduring chunked trailers is passed through; negative values become parse errors and positive values stop the current parse as incomplete.fio_http1_on_expectis special: any non-zero return rejects the expectation, resets the parser, and stops the current parse without callingfio_http1_on_complete.
All fio_buf_info_s values point into the buf memory passed to
fio_http1_parse. They are not NUL-terminated unless the input happened to be.
Copy or retain the data before the callback returns if it must outlive the input
buffer.
Important: the parser lowercases header names in-place. Feed it writable memory, not a string literal or read-only mapping.
#Completion
static void fio_http1_on_complete(void *udata);Called after the request / response line, headers, and any body have been fully parsed. The parser is reset before this callback is invoked, so it is ready for the next message on the same connection.
#Request Line Callbacks
static int fio_http1_on_method(fio_buf_info_s method, void *udata);
static int fio_http1_on_url(fio_buf_info_s path, void *udata);
static int fio_http1_on_version(fio_buf_info_s version, void *udata);For request lines, the parser calls the callbacks in this order:
fio_http1_on_methodfio_http1_on_urlfio_http1_on_version
The version slice is clamped to at most 14 bytes. GET, HEAD, and OPTIONS
are recognized case-insensitively and mark the parser as not expecting a body;
body-bearing headers for these methods conflict with that marker and are
rejected.
#Response Line Callbacks
static int fio_http1_on_version(fio_buf_info_s version, void *udata);
static int fio_http1_on_status(size_t istatus,
fio_buf_info_s status,
void *udata);For response lines, the parser calls fio_http1_on_version first and then
fio_http1_on_status.
istatus is parsed from the numeric status token. status is the remaining
status text slice after the numeric token. The version slice is clamped to at
most 14 bytes.
The parser decides whether the first line is a response by checking whether the second token starts with a decimal digit.
Responses with a 1xx, 204, or 304 status are automatically marked as
having no body (see Messages Without a Body). The parser does not skip
interim 1xx responses: each one completes as a separate message, and callers
decide whether to wait for the final response.
#Headers
static int fio_http1_on_header(fio_buf_info_s name,
fio_buf_info_s value,
void *udata);
static int fio_http1_on_header_content_length(fio_buf_info_s name,
fio_buf_info_s value,
size_t content_length,
void *udata);For ordinary headers, fio_http1_on_header receives:
namelowercased in-place.valuetrimmed of leading and trailing spaces / tabs.- an empty value as
{ .buf = NULL, .len = 0 }.
Header names must contain a valid : separator and may not contain the
forbidden characters encoded by the parser. NUL bytes in header values are
rejected.
content-length is special:
- empty values are rejected;
- non-decimal / overflowing values are rejected;
- values colliding with internal sentinels are rejected;
- duplicate
content-lengthheaders must agree; - conflicting
content-lengthand finaltransfer-encoding: chunkedare rejected; - the first non-zero accepted value calls
fio_http1_on_header_content_lengthinstead of the generic header callback; a repeated matching value is accepted without calling the content-length callback again.
A content-length: 0 value marks the message as having no body and does not
call fio_http1_on_header_content_length.
transfer-encoding is also special. Its field lines form a single coding list
(RFC 9110 §5.3), and chunked (case-insensitive) must be the final coding,
applied exactly once (RFC 9112 §6.3, §7). Anything else can't be framed safely,
so it's rejected rather than reading the body as the next message (request
smuggling):
- the parser switches to chunked body decoding when a field line ends with the
chunkedtoken; - if the value is exactly
chunked, no generic header callback is made; - if other transfer-coding text appears before the final
chunkedtoken, the finalchunkedtoken and adjacent separators are stripped before the remaining value is passed tofio_http1_on_header(i.e.,gzip, chunkedis framed as chunked and forwardstransfer-encoding: gzip; decoding it is up to the parser user, since a501response is only a SHOULD under RFC 9112 §6.1); - a field line without a final
chunkedtoken (i.e.,gzip) is forwarded unchanged; a later field line may still end the list withchunked; - rejected: a list that doesn't end with
chunkedonce the headers end (i.e.,gzip,identity,chunked, gzip, an empty value, or achunkedline followed by any othertransfer-encodingline), a repeatedchunkedcoding (chunked, chunked), malformed separators before the finalchunkedtoken, acontent-lengthheader, and requests usingGET,HEAD, orOPTIONS.
Requests must carry exactly one host header, as required by RFC 9112 §3.2:
- an HTTP/1.1 request without a
hostheader is rejected once the headers end; - a request with more than one
hostheader line is rejected (for any version); HTTP/1.0requests may omithost(seefio_http1_version_is_legacy);- responses aren't affected. The
hostheader itself is passed tofio_http1_on_header.
expect is special when its value is exactly 100-continue. Any other
Expect value is rejected.
#Messages Without a Body
The parser identifies bodyless messages from the first line, before any header
callback runs, and reports them through fio_http1_parser_skips_body:
- requests using
GET,HEAD, orOPTIONS(a non-zerocontent-lengthon these is still rejected, as described above); - responses with a
1xx/204/304status (RFC 9112 §6.3); - messages marked with
fio_http1_parser_skip_body(i.e., aHEADresponse, marked fromon_status).
fio_http1_parser_skips_body is also true after a content-length: 0 header.
A bodyless response completes at the empty line after its headers, and any bytes that follow belong to the next message. Body framing is ignored:
content-lengthis still validated and reported tofio_http1_on_header_content_length, but it is informational. Callbacks must testfio_http1_parser_skips_bodybefore reserving space or enforcing body size limits (aHEADresponse may describe a large body that never arrives);transfer-encodingandexpectare forwarded unchanged tofio_http1_on_header.
#Expect: 100-continue
static int fio_http1_on_expect(void *udata);Called once, after headers, when an accepted Expect: 100-continue header
requires a post-header decision and a body may follow (a Content-Length body
or transfer-encoding: chunked). It is not called for messages that cannot
carry a body (e.g., GET / HEAD / OPTIONS or a zero Content-Length),
since there is nothing to continue (RFC 9110 §10.1.1).
Return 0 to continue into the body / completion flow. Return non-zero to reset
the parser and stop the current parse without calling fio_http1_on_complete.
#Body Chunks
static int fio_http1_on_body_chunk(fio_buf_info_s chunk, void *udata);Called with decoded body bytes.
For Content-Length bodies, callback chunks follow the supplied input chunks
and sum to the accepted content length.
For chunked bodies, framing bytes are removed before callback delivery. Large or split HTTP chunks may be delivered through more than one callback if the input arrives in smaller pieces.
#Parser Flow
start line
├─ request -> method -> url -> version
└─ response -> version -> status
headers
├─ no body / body not allowed -> complete (incl. 1xx/204/304, skip_body)
├─ content-length body -> body chunks -> complete
└─ chunked body -> chunk chunks -> trailers -> completeDetails worth keeping in mind:
- Leading spaces,
\r, and\nbefore the first line are skipped. - First lines shorter than the parser's minimum accepted shape are rejected.
- NUL bytes in the first line are rejected.
- Header and first-line parsing waits for a newline before making progress.
- The parser accepts
\nline endings and handles an optional preceding\r. - Chunk size lines are hexadecimal, capped by the implementation, and do not support chunk extensions.
- A zero-size chunk either completes immediately when followed by an empty line or enters trailer parsing.
#Chunked Trailers
Allowed trailer headers are reported through fio_http1_on_header after the
body's terminating zero-size chunk.
The parser rejects the following trailer names:
authorizationcache-controlcontent-encodingcontent-lengthcontent-rangecontent-typeexpecthostmax-forwardsset-cookietetrailertransfer-encoding
#Ownership and Lifetime
- The parser allocates no memory.
- The parser does not copy callback data.
- The parser mutates header names in the input buffer to lowercase.
- Callback slices are valid only while the input buffer remains valid and unchanged.
udatais never owned by the parser; it is simply forwarded.- The parser state may live inside a connection object and be reused for keep-alive messages. It resets automatically on complete messages.
- After
FIO_HTTP1_PARSER_ERROR, discard the parser / connection state.
#Minimal Skeleton
#define FIO_HTTP1_PARSER
#include FIO_INCLUDE_FILE
static int fio_http1_on_method(fio_buf_info_s method, void *udata) {
(void)method;
(void)udata;
return 0;
}
static int fio_http1_on_url(fio_buf_info_s path, void *udata) {
(void)path;
(void)udata;
return 0;
}
static int fio_http1_on_version(fio_buf_info_s version, void *udata) {
(void)version;
(void)udata;
return 0;
}
static int fio_http1_on_status(size_t status_code,
fio_buf_info_s status,
void *udata) {
(void)status_code;
(void)status;
(void)udata;
return 0;
}
static int fio_http1_on_header(fio_buf_info_s name,
fio_buf_info_s value,
void *udata) {
(void)name;
(void)value;
(void)udata;
return 0;
}
static int fio_http1_on_header_content_length(fio_buf_info_s name,
fio_buf_info_s value,
size_t content_length,
void *udata) {
(void)name;
(void)value;
(void)udata;
return content_length > (1UL << 20) ? -1 : 0;
}
static int fio_http1_on_expect(void *udata) {
(void)udata;
return 0;
}
static int fio_http1_on_body_chunk(fio_buf_info_s chunk, void *udata) {
(void)chunk;
(void)udata;
return 0;
}
static void fio_http1_on_complete(void *udata) {
(void)udata;
}
size_t parse_some_http(char *data, size_t len, void *udata) {
fio_http1_parser_s parser = FIO_HTTP1_PARSER_INIT;
return fio_http1_parse(&parser, FIO_BUF_INFO2(data, len), udata);
}For real incremental parsing, keep fio_http1_parser_s with the connection and
preserve / retry unconsumed bytes when fio_http1_parse returns less than the
available buffer length.