netpkt/http.rs#
Source: lib/netpkt/http.rs
http -- HTTP/1.1 messages byte for byte without I/O: an incremental parser that takes the
bytes as they arrive and says when it needs more, and the encoders of requests, responses
and chunks (RFC 9110, 9112), with the wire rules of lib/netio/http.rs
Headers header lines in order; names compare case-insensitively: get / set /
add / remove / has_token / content_length / chunked
Version, Head a start line with its headers: a request (method, target) or a
response (status, reason); Head::keep_alive / path
Parser::requests() / responses()
push(bytes) -> Event: More (more bytes are needed), Head, Data (body
bytes as they came), End (the message is complete, with the trailers
of a chunked body); poll() gives the next event of bytes pushed before
(pipelined messages), finish() at the end of the connection (a
response body without a length ends there); expect(method) tells a
response parser which request it answers (HEAD: no body); idle,
buffered, framing, max_body
Framing how a body is delimited: None, Length(n), Chunked, UntilClose;
request_framing / response_framing after RFC 9112 6.3
keep_alive(version, headers) the connection stays open: HTTP/1.1 unless "Connection:
close", HTTP/1.0 only with "Connection: keep-alive"
encode_request / encode_response / encode_head / encode_chunk / encode_chunked /
encode_last_chunk Content-Length or chunked as netio writes them; lines whose value has
CR or LF (or whose name has CR, LF or ':') are left out
parse_head(bytes, request) one head on its own (the dissector): Ok(Some(head)), Ok(None)
while it is incomplete; start_kind(bytes) tells a request from a
response from anything else
reason(status), http_date(unix seconds) ("Tue, 20 Oct 2026 12:00:00 GMT")
mime_of(path) the Content-Type a web server gives a file by its extension
Limits: a head of at most 16 KiB (start line and header lines with their line ends) and 100
header lines (431), a request target of at most 8 KiB (414), chunk-size lines of at most 1024
bytes, bodies up to max_body (413). Obsolete line folding, a header name with spaces or
control characters, a line that is not UTF-8, a Content-Length that is no number or differs
from another one, a request with both Transfer-Encoding and Content-Length, and a
Transfer-Encoding other than chunked are errors (400); a request of a version other than
HTTP/1.0 and HTTP/1.1 is 505. Up to four empty lines before a start line are skipped; LF
alone ends a line too. A response to HEAD, a 1xx, 204 or 304 response has no body; a
response without a length runs until the connection ends; a request without one has none.
MAX_HEAD: usize = 16 * 1024
MAX_HEADERS: usize = 100
MAX_TARGET: usize = 8 * 1024
MAX_CHUNK_LINE: usize = 1024
/// the body limit of a new parser
MAX_BODY: u64 = 64 << 20
/// the last chunk of a chunked body without trailers
LAST_CHUNK: &[u8] = b"0\r\n\r\n"
struct Headers(pub Vec<(String, String)>)header lines; names compare case-insensitively
impl Headers
fn new() -> Headersfn get(&self, name: &str) -> Option<&str>the first value of a headerfn set(&mut self, name: &str, value: &str)replaces (or adds) a headerfn add(&mut self, name: &str, value: &str)fn remove(&mut self, name: &str)fn len(&self) -> usizefn is_empty(&self) -> boolfn has_token(&self, name: &str, token: &str) -> boolwhether a comma-separated header names a token ("Connection: close")fn content_length(&self) -> Result<Option<u64>, HttpErr>the Content-Length: None without one, an error when it is no number or two differfn chunked(&self) -> boolthe body is chunked (Transfer-Encoding)enum Versionthe protocol version of a messageHttp10Http11
impl Version
fn as_str(self) -> &'static strenum HttpErrwhy a message could not be readBad(&'static str)-- malformed (400), with what was wrongHeadTooLarge-- a head over 16 KiB or with more than 100 header lines (431)UriTooLong-- a request target over 8 KiB (414)Version-- a request of a version other than HTTP/1.0 and HTTP/1.1 (505)BodyTooLarge-- a body over the limit (413)Truncated-- the connection ended in the middle of a message
impl HttpErr
fn status(self) -> u16the status a server answers withfn text(self) -> &'static strwhat was wrong, in a few English words (dissector, notes)struct Heada start line and its headersmethod: String-- a request: its method and target ("GET", "/index.html?x=1"); empty in a responsetarget: Stringstatus: u16-- a response: the status and its reason phrase; 0 in a requestreason: Stringversion: Versionheaders: Headerslen: usize-- the bytes of the head up to and including the empty line (with skipped empty lines before the start line)
impl Head
fn is_request(&self) -> boolfn header(&self, name: &str) -> Option<&str>fn keep_alive(&self) -> boolthe connection stays open after this messagefn path(&self) -> &strthe target without its queryfn start_line(&self) -> Stringthe start line ("GET / HTTP/1.1", "HTTP/1.1 200 OK")fn keep_alive(v: Version, h: &Headers) -> boolwhether the connection stays open after a messageenum Framinghow a body is delimitedNone-- no bodyLength(u64)ChunkedUntilClose-- until the connection ends (responses only)
fn request_framing(h: &Headers) -> Result<Framing, HttpErr>the framing of a request body (RFC 9112 6.3; netio's server rejects TE with CL)fn response_framing(status: u16, h: &Headers, method: &str) -> Result<Framing, HttpErr>the framing of a response to a request ofmethod(RFC 9112 6.3)fn start_kind(b: &[u8]) -> Option<bool>what a message starts with: Some(true) a request ("GET "), Some(false) a response ("HTTP/1."), None something else (a continuation, another protocol); empty lines before it are skippedfn parse_head(b: &[u8], request: bool) -> Result<Option<Head>, HttpErr>a head at the start ofb(Ok(None): incomplete);requestpicks the kind of start lineenum Eventwhat the bytes so far make upMore-- more bytes are neededHead(Head)-- the start line and the headers of a messageData(Vec<u8>)-- body bytes as they came (chunk framing removed)End(Headers)-- the message is complete; the trailers of a chunked body
struct Parseran incremental parser of the messages of one connection, in one directionmax_body: u64-- the largest body it takes (BodyTooLarge beyond)messages: u64-- messages completed
impl Parser
fn requests() -> Parsera parser of requests (a server reads them)fn responses() -> Parsera parser of responses (a client reads them)fn expect(&mut self, method: &str)a response parser: a request ofmethodwent out; its response is next after the ones before (a response to HEAD has no body)fn outstanding(&self) -> usizerequests sent and not answered yetfn buffered(&self) -> usizebytes pushed and not used yetfn idle(&self) -> boolbetween messages with nothing bufferedfn in_message(&self) -> boolinside a message (its head came, its end not yet)fn framing(&self) -> Framingthe framing of the message being readfn body_len(&self) -> u64body bytes of the current message so farfn push(&mut self, data: &[u8]) -> Result<Event, HttpErr>takes more bytes and gives the first event they complete (More: nothing yet)fn poll(&mut self) -> Result<Event, HttpErr>the next event of the bytes pushed so farfn finish(&mut self) -> Result<Event, HttpErr>the connection ended: a body that runs until the close is complete (End); between messages with nothing buffered More (a clean end); inside a message Truncatedfn encode_head(h: &Head) -> Vec<u8>a head exactly as given: a request whenstatusis 0, else a responsefn encode_request(method: &str, target: &str, version: Version, h: &Headers, body: &[u8]) -> Vec<u8>a request; Content-Length when it has a body or the method is POST, PUT or PATCH (unless the headers say chunked: then the body goes as one chunk)fn encode_response(version: Version, status: u16, h: &Headers, body: &[u8], head_only: bool) -> Vec<u8>a response with its body (only the head for HEAD requests): Content-Length for every status from 200 on but 204 and 304, unless the headers say chunked (then the body goes as one chunk and the last chunk)fn encode_chunk(data: &[u8]) -> Vec<u8>one chunk; nothing for empty data (an empty chunk would end the body)fn encode_last_chunk(trailers: &Headers) -> Vec<u8>the last chunk with trailersfn encode_chunked(body: &[u8], size: usize) -> Vec<u8>a whole body as chunks ofsizebytes and the last chunkfn reason(status: u16) -> &'static strthe reason phrase of a status codefn http_date(unix: i64) -> Stringthe date of a Date header (IMF-fixdate) for seconds since 1970fn mime_of(path: &str) -> &'static strthe Content-Type of a file by its extension, in any case (text in UTF-8); what it does not know is application/octet-stream