Expand description
Dictionary compression (RFC 9842, 18.12.0, #38): a response compressed against a resource the client already has — usually the previous version of the same script or stylesheet — so only the difference crosses the wire.
The client lists the dictionary it holds in Available-Dictionary
(:<base64 SHA-256>:) and accepts dcz (zstd, the dictionary as a raw
prefix) or dcb (brotli, the dictionary as a custom dictionary). The
encoded body starts with a fixed magic and the dictionary’s hash, then
the compressed stream.
use rust_web_server::compression::dictionary::{decode, encode, DictionaryEncoding};
use rust_web_server::compression::Level;
let v1 = b"function greet(name) { return 'Hello, ' + name; }".repeat(20);
let v2 = b"function greet(name) { return 'Hello there, ' + name; }".repeat(20);
let delta = encode(DictionaryEncoding::Dcz, &v2, &v1, Level::Static).unwrap();
assert!(delta.len() < 100);
assert_eq!(decode(DictionaryEncoding::Dcz, &delta, &v1, 1 << 20).unwrap(), v2);Static files use this automatically when compression_dictionaries is
set; see the Compression docs.
Enums§
- Dictionary
Encoding - A dictionary-compressed content coding.
Constants§
- DCB_
MAGIC - The first bytes of a
dcbbody, before the hash. - DCZ_
MAGIC - The first bytes of a
dczbody (a zstd skippable frame), before the hash. - MAX_
SIZE - The largest dictionary or body encoded here (1 MiB each). Together they fit the 8 MiB window browsers support.
- PREFERENCE
- On equal weight: zstd (less CPU, and a dictionary of any size), then brotli.
Functions§
- available_
dictionary - The hash in this request’s
Available-Dictionary, if any. - decode
- The original of an
encoded body: checks the magic and that the hash isdictionary’s, and stops pastmax_lendecoded bytes.Noneon any mismatch or error. - encode
bodycompressed againstdictionary, with the magic and hash in front.Nonewhen the coding isn’t compiled in, either input is overMAX_SIZE, or the encoder fails.- hash
- The hash that names
dictionary. - mark_
encoded - The headers of a body in
encoding:Content-Encoding, andVaryonAccept-EncodingandAvailable-Dictionary. - negotiate
- The dictionary coding to use: the one in
PREFERENCEwith the highest non-zero weight inAccept-Encoding(a*doesn’t count — a client asks for these by name), compiled in. - parse_
available_ dictionary - An
Available-Dictionaryvalue: a structured-field byte sequence:<base64>:(parameters after it ignored) holding a 32-byte hash.
Type Aliases§
- Dictionary
Hash - A dictionary’s SHA-256, as
Available-Dictionarynames it.