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 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
use std::ops::Deref;
use std::str::FromStr;
use std::fmt;
use smallvec::SmallVec;
use either::Either;
use crate::{Header, MediaType};
use crate::ext::IntoCollection;
use crate::parse::parse_accept;
/// The HTTP Accept header.
///
/// An `Accept` header is composed of zero or more media types, each of which
/// may have an optional quality value (a [`QMediaType`]). The header is sent by
/// an HTTP client to describe the formats it accepts as well as the order in
/// which it prefers different formats.
///
/// # Usage
///
/// The Accept header of an incoming request can be retrieved via the
/// [`Request::accept()`] method. The [`preferred()`] method can be used to
/// retrieve the client's preferred media type.
///
/// [`Request::accept()`]: rocket::Request::accept()
/// [`preferred()`]: Accept::preferred()
///
/// An `Accept` type with a single, common media type can be easily constructed
/// via provided associated constants.
///
/// ## Example
///
/// Construct an `Accept` header with a single `application/json` media type:
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::Accept;
///
/// # #[allow(unused_variables)]
/// let accept_json = Accept::JSON;
/// ```
///
/// # Header
///
/// `Accept` implements `Into<Header>`. As such, it can be used in any context
/// where an `Into<Header>` is expected:
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::Accept;
/// use rocket::response::Response;
///
/// let response = Response::build().header(Accept::JSON).finalize();
/// ```
#[derive(Debug, Clone)]
pub struct Accept(pub(crate) AcceptParams);
/// A `MediaType` with an associated quality value.
#[derive(Debug, Clone, PartialEq)]
pub struct QMediaType(pub MediaType, pub Option<f32>);
// NOTE: `Static` is needed for `const` items. Need `const SmallVec::new`.
#[derive(Debug, Clone)]
pub enum AcceptParams {
Static(QMediaType),
Dynamic(SmallVec<[QMediaType; 1]>)
}
macro_rules! accept_constructor {
($($name:ident ($check:ident): $str:expr, $t:expr,
$s:expr $(; $k:expr => $v:expr)*,)+) => {
$(
#[doc="An `Accept` header with the single media type for <b>"]
#[doc=$str] #[doc="</b>: <i>"]
#[doc=$t] #[doc="/"] #[doc=$s]
#[doc="</i>"]
#[allow(non_upper_case_globals)]
pub const $name: Accept = Accept(
AcceptParams::Static(QMediaType(MediaType::$name, None))
);
)+
};
}
impl Accept {
/// Constructs a new `Accept` header from one or more media types.
///
/// The `items` parameter may be of type `QMediaType`, `[QMediaType]`,
/// `&[QMediaType]` or `Vec<QMediaType>`. To prevent additional allocations,
/// prefer to provide inputs of type `QMediaType`, `[QMediaType]`, or
/// `Vec<QMediaType>`.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::{QMediaType, MediaType, Accept};
///
/// // Construct an `Accept` via a `Vec<QMediaType>`.
/// let json_then_html = vec![MediaType::JSON.into(), MediaType::HTML.into()];
/// let accept = Accept::new(json_then_html);
/// assert_eq!(accept.preferred().media_type(), &MediaType::JSON);
///
/// // Construct an `Accept` via an `[QMediaType]`.
/// let accept = Accept::new([MediaType::JSON.into(), MediaType::HTML.into()]);
/// assert_eq!(accept.preferred().media_type(), &MediaType::JSON);
///
/// // Construct an `Accept` via a `QMediaType`.
/// let accept = Accept::new(QMediaType(MediaType::JSON, None));
/// assert_eq!(accept.preferred().media_type(), &MediaType::JSON);
/// ```
#[inline(always)]
pub fn new<T: IntoCollection<QMediaType>>(items: T) -> Accept {
Accept(AcceptParams::Dynamic(items.into_collection()))
}
// TODO: Implement this.
// #[inline(always)]
// pub fn add<M: Into<QMediaType>>(&mut self, media_type: M) {
// self.0.push(media_type.into());
// }
/// Retrieve the client's preferred media type. This method follows [RFC
/// 7231 5.3.2]. If the list of media types is empty, this method returns a
/// media type of any with no quality value: (`*/*`).
///
/// [RFC 7231 5.3.2]: https://tools.ietf.org/html/rfc7231#section-5.3.2
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::{QMediaType, MediaType, Accept};
///
/// let media_types = vec![
/// QMediaType(MediaType::JSON, Some(0.3)),
/// QMediaType(MediaType::HTML, Some(0.9))
/// ];
///
/// let accept = Accept::new(media_types);
/// assert_eq!(accept.preferred().media_type(), &MediaType::HTML);
/// ```
pub fn preferred(&self) -> &QMediaType {
static ANY: QMediaType = QMediaType(MediaType::Any, None);
// See https://tools.ietf.org/html/rfc7231#section-5.3.2.
let mut all = self.iter();
let mut preferred = all.next().unwrap_or(&ANY);
for media_type in all {
if media_type.weight().is_none() && preferred.weight().is_some() {
// Media types without a `q` parameter are preferred.
preferred = media_type;
} else if media_type.weight_or(0.0) > preferred.weight_or(1.0) {
// Prefer media types with a greater weight, but if one doesn't
// have a weight, prefer the one we already have.
preferred = media_type;
} else if media_type.specificity() > preferred.specificity() {
// Prefer more specific media types over less specific ones. IE:
// text/html over application/*.
preferred = media_type;
} else if media_type == preferred {
// Finally, all other things being equal, prefer a media type
// with more parameters over one with fewer. IE: text/html; a=b
// over text/html.
if media_type.params().count() > preferred.params().count() {
preferred = media_type;
}
}
}
preferred
}
/// Retrieve the first media type in `self`, if any.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::{QMediaType, MediaType, Accept};
///
/// let accept = Accept::new(QMediaType(MediaType::XML, None));
/// assert_eq!(accept.first(), Some(&MediaType::XML.into()));
/// ```
#[inline(always)]
pub fn first(&self) -> Option<&QMediaType> {
self.iter().next()
}
/// Returns an iterator over all of the (quality) media types in `self`.
/// Media types are returned in the order in which they appear in the
/// header.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::{QMediaType, MediaType, Accept};
///
/// let qmedia_types = vec![
/// QMediaType(MediaType::JSON, Some(0.3)),
/// QMediaType(MediaType::HTML, Some(0.9))
/// ];
///
/// let accept = Accept::new(qmedia_types.clone());
///
/// let mut iter = accept.iter();
/// assert_eq!(iter.next(), Some(&qmedia_types[0]));
/// assert_eq!(iter.next(), Some(&qmedia_types[1]));
/// assert_eq!(iter.next(), None);
/// ```
#[inline(always)]
pub fn iter(&self) -> impl Iterator<Item=&'_ QMediaType> + '_ {
match self.0 {
AcceptParams::Static(ref val) => Either::Left(Some(val).into_iter()),
AcceptParams::Dynamic(ref vec) => Either::Right(vec.iter())
}
}
/// Returns an iterator over all of the (bare) media types in `self`. Media
/// types are returned in the order in which they appear in the header.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::{QMediaType, MediaType, Accept};
///
/// let qmedia_types = vec![
/// QMediaType(MediaType::JSON, Some(0.3)),
/// QMediaType(MediaType::HTML, Some(0.9))
/// ];
///
/// let accept = Accept::new(qmedia_types.clone());
///
/// let mut iter = accept.media_types();
/// assert_eq!(iter.next(), Some(qmedia_types[0].media_type()));
/// assert_eq!(iter.next(), Some(qmedia_types[1].media_type()));
/// assert_eq!(iter.next(), None);
/// ```
#[inline(always)]
pub fn media_types(&self) -> impl Iterator<Item=&'_ MediaType> + '_ {
self.iter().map(|weighted_mt| weighted_mt.media_type())
}
known_media_types!(accept_constructor);
}
impl<T: IntoCollection<MediaType>> From<T> for Accept {
#[inline(always)]
fn from(items: T) -> Accept {
Accept(AcceptParams::Dynamic(items.mapped(|item| item.into())))
}
}
impl PartialEq for Accept {
fn eq(&self, other: &Accept) -> bool {
self.iter().eq(other.iter())
}
}
impl fmt::Display for Accept {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (i, media_type) in self.iter().enumerate() {
if i >= 1 {
write!(f, ", {}", media_type.0)?;
} else {
write!(f, "{}", media_type.0)?;
}
}
Ok(())
}
}
impl FromStr for Accept {
// Ideally we'd return a `ParseError`, but that requires a lifetime.
type Err = String;
#[inline]
fn from_str(raw: &str) -> Result<Accept, String> {
parse_accept(raw).map_err(|e| e.to_string())
}
}
/// Creates a new `Header` with name `Accept` and the value set to the HTTP
/// rendering of this `Accept` header.
impl From<Accept> for Header<'static> {
#[inline(always)]
fn from(val: Accept) -> Self {
Header::new("Accept", val.to_string())
}
}
impl QMediaType {
/// Retrieve the weight of the media type, if there is any.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::{MediaType, QMediaType};
///
/// let q_type = QMediaType(MediaType::HTML, Some(0.3));
/// assert_eq!(q_type.weight(), Some(0.3));
/// ```
#[inline(always)]
pub fn weight(&self) -> Option<f32> {
self.1
}
/// Retrieve the weight of the media type or a given default value.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::{MediaType, QMediaType};
///
/// let q_type = QMediaType(MediaType::HTML, Some(0.3));
/// assert_eq!(q_type.weight_or(0.9), 0.3);
///
/// let q_type = QMediaType(MediaType::HTML, None);
/// assert_eq!(q_type.weight_or(0.9), 0.9);
/// ```
#[inline(always)]
pub fn weight_or(&self, default: f32) -> f32 {
self.1.unwrap_or(default)
}
/// Borrow the internal `MediaType`.
///
/// # Example
///
/// ```rust
/// # extern crate rocket;
/// use rocket::http::{MediaType, QMediaType};
///
/// let q_type = QMediaType(MediaType::HTML, Some(0.3));
/// assert_eq!(q_type.media_type(), &MediaType::HTML);
/// ```
#[inline(always)]
pub fn media_type(&self) -> &MediaType {
&self.0
}
}
impl From<MediaType> for QMediaType {
#[inline(always)]
fn from(media_type: MediaType) -> QMediaType {
QMediaType(media_type, None)
}
}
impl Deref for QMediaType {
type Target = MediaType;
#[inline(always)]
fn deref(&self) -> &MediaType {
&self.0
}
}
impl Default for AcceptParams {
fn default() -> Self {
AcceptParams::Dynamic(SmallVec::new())
}
}
impl Extend<QMediaType> for AcceptParams {
fn extend<T: IntoIterator<Item = QMediaType>>(&mut self, iter: T) {
match self {
AcceptParams::Static(..) => panic!("can't add to static collection!"),
AcceptParams::Dynamic(ref mut v) => v.extend(iter)
}
}
}
#[cfg(test)]
mod test {
use crate::{Accept, MediaType};
#[track_caller]
fn assert_preference(string: &str, expect: &str) {
let accept: Accept = string.parse().expect("accept string parse");
let expected: MediaType = expect.parse().expect("media type parse");
let preferred = accept.preferred();
let actual = preferred.media_type();
if *actual != expected {
panic!("mismatch for {}: expected {}, got {}", string, expected, actual)
}
}
#[test]
fn test_preferred() {
assert_preference("text/*", "text/*");
assert_preference("text/*, text/html", "text/html");
assert_preference("text/*; q=0.1, text/html", "text/html");
assert_preference("text/*; q=1, text/html", "text/html");
assert_preference("text/html, text/*", "text/html");
assert_preference("text/*, text/html", "text/html");
assert_preference("text/html, text/*; q=1", "text/html");
assert_preference("text/html; q=1, text/html", "text/html");
assert_preference("text/html, text/*; q=0.1", "text/html");
assert_preference("text/html, application/json", "text/html");
assert_preference("text/html, application/json; q=1", "text/html");
assert_preference("application/json; q=1, text/html", "text/html");
assert_preference("text/*, application/json", "application/json");
assert_preference("*/*, text/*", "text/*");
assert_preference("*/*, text/*, text/plain", "text/plain");
assert_preference("a/b; q=0.1, a/b; q=0.2", "a/b; q=0.2");
assert_preference("a/b; q=0.1, b/c; q=0.2", "b/c; q=0.2");
assert_preference("a/b; q=0.5, b/c; q=0.2", "a/b; q=0.5");
assert_preference("a/b; q=0.5, b/c; q=0.2, c/d", "c/d");
assert_preference("a/b; q=0.5; v=1, a/b", "a/b");
assert_preference("a/b; v=1, a/b; v=1; c=2", "a/b; v=1; c=2");
assert_preference("a/b; v=1; c=2, a/b; v=1", "a/b; v=1; c=2");
assert_preference("a/b; q=0.5; v=1, a/b; q=0.5; v=1; c=2", "a/b; q=0.5; v=1; c=2");
assert_preference("a/b; q=0.6; v=1, a/b; q=0.5; v=1; c=2", "a/b; q=0.6; v=1");
}
}