JFLib

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() -> Headers

  • fn get(&self, name: &str) -> Option<&str> the first value of a header

  • fn set(&mut self, name: &str, value: &str) replaces (or adds) a header

  • fn add(&mut self, name: &str, value: &str)

  • fn remove(&mut self, name: &str)

  • fn len(&self) -> usize

  • fn is_empty(&self) -> bool

  • fn has_token(&self, name: &str, token: &str) -> bool whether 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 differ

  • fn chunked(&self) -> bool the body is chunked (Transfer-Encoding)

  • enum Version the protocol version of a message

    • Http10
    • Http11

impl Version

  • fn as_str(self) -> &'static str

  • enum HttpErr why a message could not be read

    • Bad(&'static str) -- malformed (400), with what was wrong
    • HeadTooLarge -- 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) -> u16 the status a server answers with

  • fn text(self) -> &'static str what was wrong, in a few English words (dissector, notes)

  • struct Head a start line and its headers

    • method: String -- a request: its method and target ("GET", "/index.html?x=1"); empty in a response
    • target: String
    • status: u16 -- a response: the status and its reason phrase; 0 in a request
    • reason: String
    • version: Version
    • headers: Headers
    • len: 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) -> bool

  • fn header(&self, name: &str) -> Option<&str>

  • fn keep_alive(&self) -> bool the connection stays open after this message

  • fn path(&self) -> &str the target without its query

  • fn start_line(&self) -> String the start line ("GET / HTTP/1.1", "HTTP/1.1 200 OK")

  • fn keep_alive(v: Version, h: &Headers) -> bool whether the connection stays open after a message

  • enum Framing how a body is delimited

    • None -- no body
    • Length(u64)
    • Chunked
    • UntilClose -- 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 of method (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 skipped

  • fn parse_head(b: &[u8], request: bool) -> Result<Option<Head>, HttpErr> a head at the start of b (Ok(None): incomplete); request picks the kind of start line

  • enum Event what the bytes so far make up

    • More -- more bytes are needed
    • Head(Head) -- the start line and the headers of a message
    • Data(Vec<u8>) -- body bytes as they came (chunk framing removed)
    • End(Headers) -- the message is complete; the trailers of a chunked body
  • struct Parser an incremental parser of the messages of one connection, in one direction

    • max_body: u64 -- the largest body it takes (BodyTooLarge beyond)
    • messages: u64 -- messages completed

impl Parser

  • fn requests() -> Parser a parser of requests (a server reads them)

  • fn responses() -> Parser a parser of responses (a client reads them)

  • fn expect(&mut self, method: &str) a response parser: a request of method went out; its response is next after the ones before (a response to HEAD has no body)

  • fn outstanding(&self) -> usize requests sent and not answered yet

  • fn buffered(&self) -> usize bytes pushed and not used yet

  • fn idle(&self) -> bool between messages with nothing buffered

  • fn in_message(&self) -> bool inside a message (its head came, its end not yet)

  • fn framing(&self) -> Framing the framing of the message being read

  • fn body_len(&self) -> u64 body bytes of the current message so far

  • fn 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 far

  • fn 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 Truncated

  • fn encode_head(h: &Head) -> Vec<u8> a head exactly as given: a request when status is 0, else a response

  • fn 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 trailers

  • fn encode_chunked(body: &[u8], size: usize) -> Vec<u8> a whole body as chunks of size bytes and the last chunk

  • fn reason(status: u16) -> &'static str the reason phrase of a status code

  • fn http_date(unix: i64) -> String the date of a Date header (IMF-fixdate) for seconds since 1970

  • fn mime_of(path: &str) -> &'static str the Content-Type of a file by its extension, in any case (text in UTF-8); what it does not know is application/octet-stream

Source: docs/API.md in the JFLib repository.