negotiator
HTTP content negotiation for Rust.
Pick the response representation a client prefers from the Accept, Accept-Charset,
Accept-Encoding, and Accept-Language request headers — quality values, wildcards,
language prefix matching, and all.
negotiator is a faithful Rust port of the widely-used
negotiator npm package (the engine behind
Express's req.accepts*), which has no Rust equivalent.
- Zero dependencies
#- Differential-tested against the reference
negotiatorimplementation across all four negotiations
Install
[]
= "0.1"
Usage
use Negotiator;
let n = new
.accept
.accept_language
.accept_encoding;
// Pick the best of what the server can produce.
assert_eq!;
assert_eq!;
assert_eq!;
// Or list everything the client accepts, in order of preference.
assert_eq!;
You can also call the free functions directly:
use preferred_languages;
assert_eq!;
Semantics
Each negotiation parses the relevant header into quality-weighted entries and ranks the
server's available options by quality, then specificity (an exact match beats a
wildcard; en-US beats en beats *), then the header and option order — exactly as the
npm package does.
- A header argument of
Nonemeans the header is absent, which RFC 7231 treats as accept everything (*/*/*).Some("")means an explicit empty header (accept nothing).Accept-Encodingis the exception: a missing or empty header leaves only the implicitidentityencoding. preferred_encodingsalways considers an implicitidentityencoding (at the lowest advertised quality) unless the header listsidentityor*, and accepts an optionalpreferredlist to bias quality ties toward a server-preferred order.
Note on case folding
The reference uses JavaScript's toLowerCase; this crate folds case with ASCII rules,
which is identical for the ASCII tokens that appear in these headers (media types,
charsets, encodings, language tags) and differs only on exotic non-ASCII input.
Contributors ✨
This project follows the all-contributors specification. Contributions of any kind are welcome — code, docs, bug reports, ideas, reviews! See the emoji key for how each contribution is recognized, and open a PR or issue to get involved.
Thanks goes to these wonderful people:
License
Licensed under either of MIT or Apache-2.0 at your option.