Http module

Event_Kind

enumSource

What parse found. Loop on the event, not on the number of bytes used: HEAD and END can come with none. The comments say where the data of each kind lives.

ExampleOpen in playground ↗
parser: Parser;
parser_init(*parser, .RESPONSE);
defer parser_free(*parser);

text := "HTTP/1.1 200 OK\r\nContent-Length: 5\r\n\r\nhello";
data: [] u8 = .{ text.count, text.data };

kinds: [..] Event_Kind;
defer array_free(kinds);

body: [..] u8;
defer array_free(body);

while true {
    used, event := parse(*parser, data);
    data = advance(data, used);
    array_add(*kinds, event.kind);

    if event.kind == .BODY array_add(*body, ..event.data);
    if event.kind == .END || event.kind == .NEED_MORE || event.kind == .ERROR break;
}

assert(kinds.count == 3 && kinds[0] == .HEAD && kinds[1] == .BODY && kinds[2] == .END);
assert(parser.head.status == 200);
assert(string.{ body.count, body.data } == "hello");

Members

NEED_MORE
all of the input was used; give more
HEAD
parser.head is filled in
BODY
event.data is a piece of the body (a slice of the caller's input)
END
the message is complete (parser.trailers holds chunked trailers)
SWITCHED
101 or a CONNECT tunnel: the rest of the stream is not HTTP
CLOSED
parse_eof: the connection ended between messages
ERROR
parser.error says why; the parser stays failed

See also