weida_http_codec/lib.rs
1//! HTTP/1.1, HPACK, HTTP/2 framing, QPACK and HTTP/3 framing, sans-I/O.
2//!
3//! This crate is the codec under `weida-http`, the HTTP client and server
4//! library ([0044](../../../docs/decisions/0044-weida-http.md)). It is
5//! **sans-I/O and weida-free**: no sockets, no executor, no dependency, not
6//! even on `weida-core`. What it exports are byte-for-byte encoders and
7//! decoders for the published RFCs, so that it can be checked against their
8//! examples rather than against weida's opinion of them.
9//!
10//! # Shape
11//!
12//! * [`h1`]: HTTP/1.1 request and response heads, body framing, chunked
13//! bodies (RFC 9112).
14//! * [`h2`]: HTTP/2 frames (RFC 9113 §4, §6).
15//! * [`hpack`]: HTTP/2 field compression with its [`huffman`] code
16//! (RFC 7541).
17//! * [`h3`]: HTTP/3 frame headers, SETTINGS and GOAWAY, stream types and
18//! error codes (RFC 9114); [`qpack`]: its field compression with the
19//! static table only (RFC 9204); [`varint`]: QUIC's variable-length
20//! integer (RFC 9000 §16).
21//! * [`fields`]: the field name and value grammar every version shares
22//! (RFC 9110 §5).
23//! * [`uri`]: `http`/`https` URLs for a client, origin-form targets for a
24//! server.
25//! * [`date`]: the IMF-fixdate a server writes into `date`.
26//! * [`Limits`]: the caller's bounds, an argument to every decode.
27//! * [`error`]: why bytes were rejected, split by direction.
28//!
29//! # Reading a stream
30//!
31//! ```
32//! use weida_http_codec::{DecodeError, Limits, h1};
33//!
34//! let wire = b"GET /hello.txt HTTP/1.1\r\nHost: www.example.com\r\n\r\n";
35//!
36//! // A head split across reads is incomplete until its empty line arrives.
37//! assert!(matches!(
38//! h1::decode_request(&wire[..20], Limits::DEFAULT),
39//! Err(DecodeError::Incomplete { .. })
40//! ));
41//!
42//! let (head, used) = h1::decode_request(wire, Limits::DEFAULT).expect("a request");
43//! assert_eq!(used, wire.len());
44//! assert_eq!(head.method, b"GET");
45//! assert_eq!(head.target, b"/hello.txt");
46//! assert_eq!(head.fields[0].value, b"www.example.com");
47//! assert_eq!(h1::request_framing(&head.fields), Ok(h1::Framing::Empty));
48//! ```
49//!
50//! # Bounds
51//!
52//! HTTP declares sizes in ASCII and in prefixed integers: a few octets can
53//! announce a body, a frame or a string of any size, and a head is as long as
54//! the peer keeps writing. Every decode entry point therefore takes [`Limits`]
55//! as an **argument**, and a declared size is refused from the declaration
56//! alone, before anything is reserved; a delimited element (a head, a chunk
57//! line) is refused once the cap's worth of octets has been searched.
58//!
59//! # Where this codec departs from the text
60//!
61//! * **Obsolete line folding is refused in both directions.** RFC 9112 §5.2
62//! lets a recipient of a *response* replace obs-fold with spaces; this
63//! codec refuses it as it does in a request (`"obsolete line folding"`),
64//! so that one parser serves both directions. `tests/golden_vectors.rs`
65//! pins it for both.
66//! * **A transfer coding other than `chunked` is refused.** RFC 9112 §6.1
67//! allows codings such as `gzip, chunked`; a library that applies no
68//! content coding cannot deliver such a body, so the framing functions say
69//! `"transfer coding"` instead of guessing.
70//! * **A bare LF ends a line on input and is never written** (RFC 9112 §2.2
71//! permits the former).
72//!
73//! # Sources
74//!
75//! Every rule comes from `docs/research/http.md`, which cites RFC 9110,
76//! RFC 9112, RFC 9113, RFC 9114, RFC 9204 (June 2022), RFC 7541 (May 2015)
77//! and RFC 9000 (May 2021).
78
79pub mod date;
80pub mod error;
81pub mod fields;
82pub mod h1;
83pub mod h2;
84pub mod h3;
85pub mod hpack;
86pub mod huffman;
87pub mod limits;
88mod prefix;
89pub mod qpack;
90pub mod uri;
91pub mod varint;
92
93pub use error::{DecodeError, EncodeError};
94pub use limits::Limits;