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
//! HTTP content negotiation — pick the best 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(...),
//! }
//! ```
//!
//! Parses RFC 7231 `Accept` (with `q=` quality values) and picks the
//! highest-priority format that the server can produce.
/// Pick the best media type the server can produce, given the client's
/// `Accept` header value and the list of types the server supports.
///
/// `accept` is the raw header value (e.g. `"application/json,text/html;q=0.9,*/*;q=0.5"`).
/// `available` is the ordered list of types the server can serve.
///
/// Matching rules:
/// - Exact match wins over wildcard match
/// - Higher q-value wins over lower
/// - Server's `available` order breaks ties at equal q
/// - Returns `None` only when `available` is empty or no client preference matches
/// (a `*/*` client preference will always match the first available type)
#[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 → first available
return Some(available[0].as_ref());
}
let prefs = parse_accept(accept);
let mut best: Option<(usize, f32)> = None; // (server-index, score)
for (idx, srv) in available.iter().enumerate() {
let srv_str = srv.as_ref();
for pref in &prefs {
let score = match_score(pref, srv_str);
if let Some(s) = score {
let beats = match best {
None => true,
Some((_, prev_score)) => s > prev_score,
};
if beats {
best = Some((idx, s));
}
break; // server's order matters at equal q — stop at first match
}
}
}
best.map(|(idx, _)| available[idx].as_ref())
}
#[derive(Debug, Clone)]
struct AcceptPref {
type_: String,
subtype: String,
q: f32,
}
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 = 1.0;
for kv in parts {
if let Some(rest) = kv.strip_prefix("q=") {
if let Ok(parsed) = rest.parse::<f32>() {
q = parsed;
}
}
}
Some(AcceptPref {
type_: type_.to_ascii_lowercase(),
subtype: subtype.to_ascii_lowercase(),
q,
})
})
.collect()
}
/// Return the q-value of the match, or `None` if `pref` doesn't match `srv_type`.
fn match_score(pref: &AcceptPref, srv_type: &str) -> Option<f32> {
let (s_type, s_subtype) = srv_type.split_once('/')?;
let s_type = s_type.to_ascii_lowercase();
let s_subtype = s_subtype.to_ascii_lowercase();
let type_matches = pref.type_ == "*" || pref.type_ == s_type;
let subtype_matches = pref.subtype == "*" || pref.subtype == s_subtype;
if type_matches && subtype_matches {
// Slight bonus for exact (non-wildcard) matches so `text/html`
// beats `*/*` at the same q-value.
let exact_bonus = match (pref.type_ == "*", pref.subtype == "*") {
(false, false) => 0.0001, // most specific
(false, true) => 0.00005,
_ => 0.0,
};
Some(pref.q + exact_bonus)
} else {
None
}
}
#[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";
// Server prefers JSON, falls back to HTML
assert_eq!(
negotiate(header, &["application/json", "text/html"]),
Some("text/html"),
);
}
#[test]
fn exact_type_beats_wildcard_at_same_q() {
// "text/html" (q=1.0 exact) should beat "*/*" (q=1.0 wildcard)
assert_eq!(
negotiate("text/html,*/*", &["application/json", "text/html"]),
Some("text/html"),
);
}
}