Skip to main content

Module dictionary

Module dictionary 

Source
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§

DictionaryEncoding
A dictionary-compressed content coding.

Constants§

DCB_MAGIC
The first bytes of a dcb body, before the hash.
DCZ_MAGIC
The first bytes of a dcz body (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 is dictionary’s, and stops past max_len decoded bytes. None on any mismatch or error.
encode
body compressed against dictionary, with the magic and hash in front. None when the coding isn’t compiled in, either input is over MAX_SIZE, or the encoder fails.
hash
The hash that names dictionary.
mark_encoded
The headers of a body in encoding: Content-Encoding, and Vary on Accept-Encoding and Available-Dictionary.
negotiate
The dictionary coding to use: the one in PREFERENCE with the highest non-zero weight in Accept-Encoding (a * doesn’t count — a client asks for these by name), compiled in.
parse_available_dictionary
An Available-Dictionary value: a structured-field byte sequence :<base64>: (parameters after it ignored) holding a 32-byte hash.

Type Aliases§

DictionaryHash
A dictionary’s SHA-256, as Available-Dictionary names it.