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§
Source§impl Accept
impl Accept
Sourcepub fn from_headers(headers: impl AsRef<Headers>) -> Result<Option<Self>>
pub fn from_headers(headers: impl AsRef<Headers>) -> Result<Option<Self>>
Create an instance of Accept
from a Headers
instance.
Sourcepub 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.
Sourcepub fn set_wildcard(&mut self, wildcard: bool)
pub fn set_wildcard(&mut self, wildcard: bool)
Set the wildcard directive.
Sourcepub 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.
Sourcepub fn negotiate(&mut self, available: &[Mime]) -> Result<ContentType>
pub fn negotiate(&mut self, available: &[Mime]) -> Result<ContentType>
Determine the most suitable Content-Type
encoding.
§Errors
If no suitable encoding is found, an error with the status of 406
will be returned.
Sourcepub fn name(&self) -> HeaderName
pub fn name(&self) -> HeaderName
Get the HeaderName
.
Sourcepub fn value(&self) -> HeaderValue
pub fn value(&self) -> HeaderValue
Get the HeaderValue
.
Trait Implementations§
Source§impl<'a> IntoIterator for &'a Accept
impl<'a> IntoIterator for &'a Accept
Source§impl<'a> IntoIterator for &'a mut Accept
impl<'a> IntoIterator for &'a mut Accept
Source§impl IntoIterator for Accept
impl IntoIterator for Accept
Source§impl ToHeaderValues for Accept
impl ToHeaderValues for Accept
Source§type Iter = IntoIter<HeaderValue>
type Iter = IntoIter<HeaderValue>
Source§fn to_header_values(&self) -> Result<Self::Iter>
fn to_header_values(&self) -> Result<Self::Iter>
HeaderValues
.