rocket::http

Struct Accept

pub struct Accept(/* private fields */);
Expand description

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.

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:

use rocket::http::Accept;

let accept_json = Accept::JSON;

Accept implements Into<Header>. As such, it can be used in any context where an Into<Header> is expected:

use rocket::http::Accept;
use rocket::response::Response;

let response = Response::build().header(Accept::JSON).finalize();

Implementations§

§

impl Accept

pub const Any: Accept = _

An Accept header with the single media type for any media type : * / *

pub const Binary: Accept = _

An Accept header with the single media type for binary data : application / octet-stream

pub const HTML: Accept = _

An Accept header with the single media type for HTML : text / html

pub const Plain: Accept = _

An Accept header with the single media type for plain text : text / plain

pub const JSON: Accept = _

An Accept header with the single media type for JSON : application / json

pub const MsgPack: Accept = _

An Accept header with the single media type for MsgPack : application / msgpack

pub const Form: Accept = _

An Accept header with the single media type for forms : application / x-www-form-urlencoded

pub const JavaScript: Accept = _

An Accept header with the single media type for JavaScript : application / javascript

pub const CSS: Accept = _

An Accept header with the single media type for CSS : text / css

pub const FormData: Accept = _

An Accept header with the single media type for multipart form data : multipart / form-data

pub const XML: Accept = _

An Accept header with the single media type for XML : text / xml

pub const CSV: Accept = _

An Accept header with the single media type for CSV : text / csv

pub const PNG: Accept = _

An Accept header with the single media type for PNG : image / png

pub const GIF: Accept = _

An Accept header with the single media type for GIF : image / gif

pub const BMP: Accept = _

An Accept header with the single media type for BMP : image / bmp

pub const JPEG: Accept = _

An Accept header with the single media type for JPEG : image / jpeg

pub const WEBP: Accept = _

An Accept header with the single media type for WEBP : image / webp

pub const SVG: Accept = _

An Accept header with the single media type for SVG : image / svg+xml

pub const Icon: Accept = _

An Accept header with the single media type for Icon : image / x-icon

pub const WEBM: Accept = _

An Accept header with the single media type for WEBM : video / webm

pub const WEBA: Accept = _

An Accept header with the single media type for WEBM Audio : audio / webm

pub const OGG: Accept = _

An Accept header with the single media type for OGG Video : video / ogg

pub const FLAC: Accept = _

An Accept header with the single media type for FLAC : audio / flac

pub const WAV: Accept = _

An Accept header with the single media type for WAV : audio / wav

pub const PDF: Accept = _

An Accept header with the single media type for PDF : application / pdf

pub const TTF: Accept = _

An Accept header with the single media type for TTF : application / font-sfnt

pub const OTF: Accept = _

An Accept header with the single media type for OTF : application / font-sfnt

pub const WOFF: Accept = _

An Accept header with the single media type for WOFF : application / font-woff

pub const WOFF2: Accept = _

An Accept header with the single media type for WOFF2 : font / woff2

pub const JsonApi: Accept = _

An Accept header with the single media type for JSON API : application / vnd.api+json

pub const WASM: Accept = _

An Accept header with the single media type for WASM : application / wasm

pub const TIFF: Accept = _

An Accept header with the single media type for TIFF : image / tiff

pub const AAC: Accept = _

An Accept header with the single media type for AAC Audio : audio / aac

pub const Calendar: Accept = _

An Accept header with the single media type for iCalendar : text / calendar

pub const MPEG: Accept = _

An Accept header with the single media type for MPEG Video : video / mpeg

pub const TAR: Accept = _

An Accept header with the single media type for tape archive : application / x-tar

pub const GZIP: Accept = _

An Accept header with the single media type for gzipped binary : application / gzip

pub const MOV: Accept = _

An Accept header with the single media type for quicktime video : video / quicktime

pub const MP4: Accept = _

An Accept header with the single media type for MPEG4 Video : video / mp4

pub const ZIP: Accept = _

An Accept header with the single media type for ZIP archive : application / zip

pub fn new<T>(items: T) -> Accept

Constructs a new Accept header from one or more media types.

The items parameter may be of type QMediaType, &[QMediaType], or Vec<QMediaType>. To prevent additional allocations, prefer to provide inputs of type QMediaType and Vec<QMediaType>.

§Example
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);

pub fn preferred(&self) -> &QMediaType

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: (*/*).

§Example
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 first(&self) -> Option<&QMediaType>

Retrieve the first media type in self, if any.

§Example
use rocket::http::{QMediaType, MediaType, Accept};

let accept = Accept::new(QMediaType(MediaType::XML, None));
assert_eq!(accept.first(), Some(&MediaType::XML.into()));

pub fn iter<'a>(&'a self) -> impl Iterator<Item = &'a QMediaType> + 'a

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
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);

pub fn media_types<'a>(&'a self) -> impl Iterator<Item = &'a MediaType> + 'a

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
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);

Trait Implementations§

§

impl Clone for Accept

§

fn clone(&self) -> Accept

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
§

impl Debug for Accept

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl Display for Accept

§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
§

impl<T> From<T> for Accept

§

fn from(items: T) -> Accept

Converts to this type from the input type.
Source§

impl<'a, 'r> FromRequest<'a, 'r> for &'a Accept

Source§

type Error = !

The associated error to be returned if derivation fails.
Source§

fn from_request(request: &'a Request<'r>) -> Outcome<Self, Self::Error>

Derives an instance of Self from the incoming request metadata. Read more
§

impl FromStr for Accept

§

type Err = String

The associated error which can be returned from parsing.
§

fn from_str(raw: &str) -> Result<Accept, String>

Parses a string s to return a value of this type. Read more
§

impl Into<Header<'static>> for Accept

Creates a new Header with name Accept and the value set to the HTTP rendering of this Accept header.

§

fn into(self) -> Header<'static>

Converts this type into the (usually inferred) input type.
§

impl PartialEq for Accept

§

fn eq(&self, other: &Accept) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
§

impl StructuralPartialEq for Accept

Auto Trait Implementations§

§

impl Freeze for Accept

§

impl RefUnwindSafe for Accept

§

impl Send for Accept

§

impl Sync for Accept

§

impl Unpin for Accept

§

impl UnwindSafe for Accept

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T, I> AsResult<T, I> for T
where I: Input,

Source§

fn as_result(self) -> Result<T, ParseErr<I>>

Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dst: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dst. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

§

impl<T> IntoCollection<T> for T

§

fn into_collection<A>(self) -> SmallVec<A>
where A: Array<Item = T>,

Converts self into a collection.
§

fn mapped<U, F, A>(self, f: F) -> SmallVec<A>
where F: FnMut(T) -> U, A: Array<Item = U>,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> Typeable for T
where T: Any,

Source§

fn get_type(&self) -> TypeId

Get the TypeId of this object.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V