1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
//! Pick a response format from the client's `Accept` header.
//!
//! ## Quick start
//!
//! ```ignore
//! use rustango::content_negotiation::negotiate;
//!
//! let format = negotiate(
//! accept_header,
//! &["application/json", "text/html", "text/plain"],
//! );
//! match format {
//! Some("application/json") => render_json(...),
//! Some("text/html") => render_html(...),
//! _ => render_plaintext(...),
//! }
//! ```
//!
//! It reads RFC 7231 `Accept` headers, including `q=` values.
/// Choose the media type to send back.
///
/// `accept` is the raw header, such as
/// `"application/json,text/html;q=0.9,*/*;q=0.5"`. `available` is
/// what your handler can produce, best first.
///
/// Each type takes the `q` of the most specific range that matches it
/// (RFC 9110 §12.5.1), whatever the header order; `q=0` means "not
/// acceptable". A higher `q` wins; at the same `q` an exact match beats
/// a wildcard; after that the order of `available` decides. Returns
/// `None` when `available` is empty or nothing on offer is acceptable.
#[must_use]
pub fn negotiate<'a, S: AsRef<str>>(accept: &str, available: &'a [S]) -> Option<&'a str> {
if available.is_empty() {
return None;
}
if accept.trim().is_empty() {
// No Accept header, so take our own first choice.
return Some(available[0].as_ref());
}
let prefs = parse_accept(accept);
// (server index, q in thousandths, specificity); `>` keeps the first on ties.
let mut best: Option<(usize, u16, u8)> = None;
for (idx, srv) in available.iter().enumerate() {
let Some((q, spec)) = prefs
.iter()
.filter_map(|p| specificity(p, srv.as_ref()).map(|spec| (p.q, spec)))
.max_by_key(|&(_, spec)| spec)
else {
continue;
};
if q == 0 {
continue;
}
if best.is_none_or(|(_, bq, bs)| (q, spec) > (bq, bs)) {
best = Some((idx, q, spec));
}
}
best.map(|(idx, _, _)| available[idx].as_ref())
}
#[derive(Debug, Clone)]
struct AcceptPref {
type_: String,
subtype: String,
/// `q` in thousandths, the precision RFC 9110 allows.
q: u16,
}
fn parse_accept(header: &str) -> Vec<AcceptPref> {
header
.split(',')
.filter_map(|raw| {
let mut parts = raw.split(';').map(str::trim);
let media = parts.next()?;
let (type_, subtype) = media.split_once('/')?;
let mut q = 1000;
for kv in parts {
if let Some(rest) = kv.strip_prefix("q=").or_else(|| kv.strip_prefix("Q=")) {
// A non-finite q is as unusable as an unparsable one.
if let Some(parsed) = rest.trim().parse::<f32>().ok().filter(|p| p.is_finite())
{
q = (parsed.clamp(0.0, 1.0) * 1000.0).round() as u16;
}
}
}
Some(AcceptPref {
type_: type_.to_ascii_lowercase(),
subtype: subtype.to_ascii_lowercase(),
q,
})
})
.collect()
}
/// How specific `pref` is for `srv_type` (2 exact, 1 `type/*`, 0
/// `*/*`), or `None` when it does not match.
fn specificity(pref: &AcceptPref, srv_type: &str) -> Option<u8> {
let (s_type, s_subtype) = srv_type.split_once('/')?;
let type_matches = pref.type_ == "*" || pref.type_.eq_ignore_ascii_case(s_type);
let subtype_matches = pref.subtype == "*" || pref.subtype.eq_ignore_ascii_case(s_subtype);
if !(type_matches && subtype_matches) {
return None;
}
Some(match (pref.type_ == "*", pref.subtype == "*") {
(false, false) => 2,
(false, true) => 1,
_ => 0,
})
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn exact_match() {
assert_eq!(
negotiate("application/json", &["application/json", "text/html"]),
Some("application/json"),
);
}
#[test]
fn highest_q_wins() {
assert_eq!(
negotiate(
"text/html;q=0.5,application/json;q=0.9",
&["application/json", "text/html"],
),
Some("application/json"),
);
}
#[test]
fn wildcard_falls_back_to_first_available() {
assert_eq!(
negotiate("*/*", &["application/json", "text/html"]),
Some("application/json"),
);
}
#[test]
fn type_wildcard_matches() {
assert_eq!(
negotiate("text/*", &["application/json", "text/html"]),
Some("text/html"),
);
}
#[test]
fn no_match_returns_none() {
assert_eq!(
negotiate("application/xml", &["application/json", "text/html"]),
None,
);
}
#[test]
fn empty_accept_picks_first_available() {
assert_eq!(
negotiate("", &["application/json", "text/html"]),
Some("application/json"),
);
}
#[test]
fn empty_available_returns_none() {
let empty: &[&str] = &[];
assert_eq!(negotiate("application/json", empty), None);
}
#[test]
fn case_insensitive_match() {
assert_eq!(
negotiate("APPLICATION/JSON", &["application/json"]),
Some("application/json"),
);
}
#[test]
fn complex_real_world_browser_accept() {
let header = "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8";
// We list JSON first, but the browser asks for HTML at q=1.
assert_eq!(
negotiate(header, &["application/json", "text/html"]),
Some("text/html"),
);
}
/// `q=0` refuses a type, and the most specific range sets its q,
/// not the first one listed (#1957).
#[test]
fn q_zero_refuses_and_specificity_beats_header_order() {
assert_eq!(
negotiate("application/json;q=0", &["application/json"]),
None
);
assert_eq!(
negotiate(
"*/*, application/json;q=0",
&["application/json", "text/html"]
),
Some("text/html"),
);
assert_eq!(
negotiate("text/*;q=0.1, text/html", &["text/plain", "text/html"]),
Some("text/html"),
);
}
/// A NaN or infinite q keeps the default q, like an unparsable one.
#[test]
fn non_finite_q_keeps_the_default() {
for q in ["NaN", "-inf", "inf"] {
assert_eq!(
negotiate(
&format!("text/html;q=0.5, application/json;q={q}"),
&["text/html", "application/json"]
),
Some("application/json"),
"q={q}"
);
}
}
#[test]
fn exact_type_beats_wildcard_at_same_q() {
// Exact `text/html` beats `*/*`, both at q=1.0.
assert_eq!(
negotiate("text/html,*/*", &["application/json", "text/html"]),
Some("text/html"),
);
}
}