pub struct Accept { /* private fields */ }
Expand description
Client header advertising which media types the client is able to understand.
Using content negotiation, the server then selects one of the proposals, uses
it and informs the client of its choice with the Content-Type
response
header. Browsers set adequate values for this header depending on the context
where the request is done: when fetching a CSS stylesheet a different value
is set for the request than when fetching an image, video or a script.
Specifications
Examples
use http_types::content::{Accept, MediaTypeProposal};
use http_types::{mime, Response};
let mut accept = Accept::new();
accept.push(MediaTypeProposal::new(mime::HTML, Some(0.8))?);
accept.push(MediaTypeProposal::new(mime::XML, Some(0.4))?);
accept.push(mime::PLAIN);
let mut res = Response::new(200);
let content_type = accept.negotiate(&[mime::XML])?;
content_type.apply(&mut res);
assert_eq!(res["Content-Type"], "application/xml;charset=utf-8");
Implementations
impl Accept
impl Accept
pub fn from_headers(
headers: impl AsRef<Headers>
) -> Result<Option<Accept>, Error>
pub fn from_headers(
headers: impl AsRef<Headers>
) -> Result<Option<Accept>, Error>
Create an instance of Accept
from a Headers
instance.
pub fn push(&mut self, prop: impl Into<MediaTypeProposal>)
pub fn push(&mut self, prop: impl Into<MediaTypeProposal>)
Push a directive into the list of entries.
pub fn set_wildcard(&mut self, wildcard: bool)
pub fn set_wildcard(&mut self, wildcard: bool)
Set the wildcard directive.
pub fn sort(&mut self)
pub fn sort(&mut self)
Sort the header directives by weight.
Headers with a higher q=
value will be returned first. If two
directives have the same weight, the directive that was declared later
will be returned first.
pub fn negotiate(&mut self, available: &[Mime]) -> Result<ContentType, Error>
pub fn negotiate(&mut self, available: &[Mime]) -> Result<ContentType, Error>
Determine the most suitable Content-Type
encoding.
Errors
If no suitable encoding is found, an error with the status of 406
will be returned.
pub fn name(&self) -> HeaderName
pub fn name(&self) -> HeaderName
Get the HeaderName
.
pub fn value(&self) -> HeaderValue
pub fn value(&self) -> HeaderValue
Get the HeaderValue
.
Trait Implementations
impl<'a> IntoIterator for &'a Accept
impl<'a> IntoIterator for &'a Accept
type Item = &'a MediaTypeProposal
type Item = &'a MediaTypeProposal
The type of the elements being iterated over.
fn into_iter(self) -> <&'a Accept as IntoIterator>::IntoIter
fn into_iter(self) -> <&'a Accept as IntoIterator>::IntoIter
Creates an iterator from a value. Read more
impl IntoIterator for Accept
impl IntoIterator for Accept
type Item = MediaTypeProposal
type Item = MediaTypeProposal
The type of the elements being iterated over.
fn into_iter(self) -> <Accept as IntoIterator>::IntoIter
fn into_iter(self) -> <Accept as IntoIterator>::IntoIter
Creates an iterator from a value. Read more
impl<'a> IntoIterator for &'a mut Accept
impl<'a> IntoIterator for &'a mut Accept
type Item = &'a mut MediaTypeProposal
type Item = &'a mut MediaTypeProposal
The type of the elements being iterated over.
fn into_iter(self) -> <&'a mut Accept as IntoIterator>::IntoIter
fn into_iter(self) -> <&'a mut Accept as IntoIterator>::IntoIter
Creates an iterator from a value. Read more
impl ToHeaderValues for Accept
impl ToHeaderValues for Accept
type Iter = IntoIter<HeaderValue>
type Iter = IntoIter<HeaderValue>
Returned iterator over header values which this type may correspond to.
fn to_header_values(&self) -> Result<<Accept as ToHeaderValues>::Iter, Error>
fn to_header_values(&self) -> Result<<Accept as ToHeaderValues>::Iter, Error>
Converts this object to an iterator of resolved HeaderValues
.
Auto Trait Implementations
impl RefUnwindSafe for Accept
impl Send for Accept
impl Sync for Accept
impl Unpin for Accept
impl UnwindSafe for Accept
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more