Struct rocket::http::ContentType
[−]
[src]
pub struct ContentType { pub ttype: UncasedAscii<'static>, pub subtype: UncasedAscii<'static>, pub params: Option<UncasedAscii<'static>>, }
Representation of HTTP Content-Types.
Usage
ContentTypes
should rarely be created directly. Instead, an associated
constant should be used; one is declared for most commonly used content
types.
Example
A Content-Type of text/html; charset=utf-8
can be insantiated via the
HTML
constant:
use rocket::http::ContentType; let html = ContentType::HTML;
Header
ContentType
implements Into<Header>
. As such, it can be used in any
context where an Into<Header>
is expected:
use rocket::http::ContentType; use rocket::response::Response; let response = Response::build().header(ContentType::HTML).finalize();
Fields
ttype: UncasedAscii<'static>
The "type" component of the Content-Type.
subtype: UncasedAscii<'static>
The "subtype" component of the Content-Type.
params: Option<UncasedAscii<'static>>
Semicolon-seperated parameters associated with the Content-Type.
Methods
impl ContentType
[src]
const Any: ContentType
Any: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("*"),}, subtype: UncasedAscii{string: Cow::Borrowed("*"),}, params: None,}
ContentType for any : * / *
const HTML: ContentType
HTML: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("text"),}, subtype: UncasedAscii{string: Cow::Borrowed("html"),}, params: Some(UncasedAscii{string: Cow::Borrowed("charset=utf-8"),}),}
ContentType for HTML : text / html ; charset=utf-8
const Plain: ContentType
Plain: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("text"),}, subtype: UncasedAscii{string: Cow::Borrowed("plain"),}, params: Some(UncasedAscii{string: Cow::Borrowed("charset=utf-8"),}),}
ContentType for Plain : text / plain ; charset=utf-8
const JSON: ContentType
JSON: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("application"),}, subtype: UncasedAscii{string: Cow::Borrowed("json"),}, params: None,}
ContentType for JSON : application / json
const Form: ContentType
Form: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("application"),}, subtype: UncasedAscii{string: Cow::Borrowed("x-www-form-urlencoded"),}, params: None,}
ContentType for form : application / x-www-form-urlencoded
const JavaScript: ContentType
JavaScript: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("application"),}, subtype: UncasedAscii{string: Cow::Borrowed("javascript"),}, params: None,}
ContentType for JavaScript : application / javascript
const CSS: ContentType
CSS: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("text"),}, subtype: UncasedAscii{string: Cow::Borrowed("css"),}, params: Some(UncasedAscii{string: Cow::Borrowed("charset=utf-8"),}),}
ContentType for CSS : text / css ; charset=utf-8
const DataForm: ContentType
DataForm: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("multipart"),}, subtype: UncasedAscii{string: Cow::Borrowed("form-data"),}, params: None,}
ContentType for data form : multipart / form-data
const XML: ContentType
XML: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("text"),}, subtype: UncasedAscii{string: Cow::Borrowed("xml"),}, params: Some(UncasedAscii{string: Cow::Borrowed("charset=utf-8"),}),}
ContentType for XML : text / xml ; charset=utf-8
const CSV: ContentType
CSV: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("text"),}, subtype: UncasedAscii{string: Cow::Borrowed("csv"),}, params: Some(UncasedAscii{string: Cow::Borrowed("charset=utf-8"),}),}
ContentType for CSV : text / csv ; charset=utf-8
const PNG: ContentType
PNG: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("image"),}, subtype: UncasedAscii{string: Cow::Borrowed("png"),}, params: None,}
ContentType for PNG : image / png
const GIF: ContentType
GIF: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("image"),}, subtype: UncasedAscii{string: Cow::Borrowed("gif"),}, params: None,}
ContentType for GIF : image / gif
const BMP: ContentType
BMP: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("image"),}, subtype: UncasedAscii{string: Cow::Borrowed("bmp"),}, params: None,}
ContentType for BMP : image / bmp
const JPEG: ContentType
JPEG: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("image"),}, subtype: UncasedAscii{string: Cow::Borrowed("jpeg"),}, params: None,}
ContentType for JPEG : image / jpeg
const WEBP: ContentType
WEBP: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("image"),}, subtype: UncasedAscii{string: Cow::Borrowed("webp"),}, params: None,}
ContentType for WEBP : image / webp
const SVG: ContentType
SVG: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("image"),}, subtype: UncasedAscii{string: Cow::Borrowed("svg+xml"),}, params: None,}
ContentType for SVG : image / svg+xml
const PDF: ContentType
PDF: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("application"),}, subtype: UncasedAscii{string: Cow::Borrowed("pdf"),}, params: None,}
ContentType for PDF : application / pdf
const TTF: ContentType
TTF: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("application"),}, subtype: UncasedAscii{string: Cow::Borrowed("font-sfnt"),}, params: None,}
ContentType for TTF : application / font-sfnt
const OTF: ContentType
OTF: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("application"),}, subtype: UncasedAscii{string: Cow::Borrowed("font-sfnt"),}, params: None,}
ContentType for OTF : application / font-sfnt
const WOFF: ContentType
WOFF: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("application"),}, subtype: UncasedAscii{string: Cow::Borrowed("font-woff"),}, params: None,}
ContentType for WOFF : application / font-woff
const WOFF2: ContentType
WOFF2: ContentType = ContentType{ttype: UncasedAscii{string: Cow::Borrowed("font"),}, subtype: UncasedAscii{string: Cow::Borrowed("woff2"),}, params: None,}
ContentType for WOFF2 : font / woff2
fn is_known(&self) -> bool
Returns true
if this ContentType is known to Rocket, that is,
there is an associated constant for self
.
fn is_any(&self) -> bool
Returns true
if self
is a
any
ContentType:
*
/
*
.
Paramaters are not taken into account when doing this check.
fn is_html(&self) -> bool
Returns true
if self
is a
HTML
ContentType:
text
/
html
.
Paramaters are not taken into account when doing this check.
fn is_plain(&self) -> bool
Returns true
if self
is a
Plain
ContentType:
text
/
plain
.
Paramaters are not taken into account when doing this check.
fn is_json(&self) -> bool
Returns true
if self
is a
JSON
ContentType:
application
/
json
.
Paramaters are not taken into account when doing this check.
fn is_form(&self) -> bool
Returns true
if self
is a
form
ContentType:
application
/
x-www-form-urlencoded
.
Paramaters are not taken into account when doing this check.
fn is_javascript(&self) -> bool
Returns true
if self
is a
JavaScript
ContentType:
application
/
javascript
.
Paramaters are not taken into account when doing this check.
fn is_css(&self) -> bool
Returns true
if self
is a
CSS
ContentType:
text
/
css
.
Paramaters are not taken into account when doing this check.
fn is_data_form(&self) -> bool
Returns true
if self
is a
data form
ContentType:
multipart
/
form-data
.
Paramaters are not taken into account when doing this check.
fn is_xml(&self) -> bool
Returns true
if self
is a
XML
ContentType:
text
/
xml
.
Paramaters are not taken into account when doing this check.
fn is_csv(&self) -> bool
Returns true
if self
is a
CSV
ContentType:
text
/
csv
.
Paramaters are not taken into account when doing this check.
fn is_png(&self) -> bool
Returns true
if self
is a
PNG
ContentType:
image
/
png
.
Paramaters are not taken into account when doing this check.
fn is_gif(&self) -> bool
Returns true
if self
is a
GIF
ContentType:
image
/
gif
.
Paramaters are not taken into account when doing this check.
fn is_bmp(&self) -> bool
Returns true
if self
is a
BMP
ContentType:
image
/
bmp
.
Paramaters are not taken into account when doing this check.
fn is_jpeg(&self) -> bool
Returns true
if self
is a
JPEG
ContentType:
image
/
jpeg
.
Paramaters are not taken into account when doing this check.
fn is_webp(&self) -> bool
Returns true
if self
is a
WEBP
ContentType:
image
/
webp
.
Paramaters are not taken into account when doing this check.
fn is_svg(&self) -> bool
Returns true
if self
is a
SVG
ContentType:
image
/
svg+xml
.
Paramaters are not taken into account when doing this check.
fn is_pdf(&self) -> bool
Returns true
if self
is a
PDF
ContentType:
application
/
pdf
.
Paramaters are not taken into account when doing this check.
fn is_ttf(&self) -> bool
Returns true
if self
is a
TTF
ContentType:
application
/
font-sfnt
.
Paramaters are not taken into account when doing this check.
fn is_otf(&self) -> bool
Returns true
if self
is a
OTF
ContentType:
application
/
font-sfnt
.
Paramaters are not taken into account when doing this check.
fn is_woff(&self) -> bool
Returns true
if self
is a
WOFF
ContentType:
application
/
font-woff
.
Paramaters are not taken into account when doing this check.
fn is_woff2(&self) -> bool
Returns true
if self
is a
WOFF2
ContentType:
font
/
woff2
.
Paramaters are not taken into account when doing this check.
fn from_extension(ext: &str) -> ContentType
Returns the Content-Type associated with the extension ext
. Not all
extensions are recognized. If an extensions is not recognized, then this
method returns a ContentType of Any
. The currently recognized
extensions are: txt, html, htm, xml, js, css, json, png, gif, bmp, jpeg,
jpg, and pdf.
Example
A recognized content type:
use rocket::http::ContentType; let xml = ContentType::from_extension("xml"); assert!(xml.is_xml());
An unrecognized content type:
use rocket::http::ContentType; let foo = ContentType::from_extension("foo"); assert!(foo.is_any());
fn new<T, S>(ttype: T, subtype: S) -> ContentType where
T: Into<Cow<'static, str>>,
S: Into<Cow<'static, str>>,
T: Into<Cow<'static, str>>,
S: Into<Cow<'static, str>>,
Creates a new ContentType
with type ttype
and subtype subtype
.
This should only be used to construct uncommon Content-Types or custom
Content-Types. Use an associated constant for common Content-Types.
Example
Create a custom application/x-person
Content-Type:
use rocket::http::ContentType; let custom = ContentType::new("application", "x-person"); assert_eq!(custom.to_string(), "application/x-person".to_string());
fn with_params<T, S, P>(ttype: T, subtype: S, params: Option<P>) -> ContentType where
T: Into<Cow<'static, str>>,
S: Into<Cow<'static, str>>,
P: Into<Cow<'static, str>>,
T: Into<Cow<'static, str>>,
S: Into<Cow<'static, str>>,
P: Into<Cow<'static, str>>,
Creates a new ContentType
with type ttype
, subtype subtype
, and
optionally parameters params
, a semicolon-seperated list of
parameters. This should be only to construct uncommon Content-Types or
custom Content-Types. Use an associated constant for common
Content-Types.
Example
Create a custom application/x-id; id=1
Content-Type:
use rocket::http::ContentType; let id = ContentType::with_params("application", "x-id", Some("id=1")); assert_eq!(id.to_string(), "application/x-id; id=1".to_string());
fn params<'a>(&'a self) -> impl Iterator<Item = (&'a str, &'a str)> + 'a
Returns an iterator over the (key, value) pairs of the Content-Type's parameter list. The iterator will be empty if the Content-Type has no parameters.
Example
The ContentType::Plain
type has one parameter: charset=utf-8
:
use rocket::http::ContentType; let plain = ContentType::Plain; let plain_params: Vec<_> = plain.params().collect(); assert_eq!(plain_params, vec![("charset", "utf-8")]);
The ContentType::PNG
type has no parameters:
use rocket::http::ContentType; let png = ContentType::PNG; assert_eq!(png.params().count(), 0);
Trait Implementations
impl Debug for ContentType
[src]
impl Clone for ContentType
[src]
fn clone(&self) -> ContentType
Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0
Performs copy-assignment from source
. Read more
impl PartialEq for ContentType
[src]
fn eq(&self, __arg_0: &ContentType) -> bool
This method tests for self
and other
values to be equal, and is used by ==
. Read more
fn ne(&self, __arg_0: &ContentType) -> bool
This method tests for !=
.
impl Hash for ContentType
[src]
fn hash<__H: Hasher>(&self, __arg_0: &mut __H)
Feeds this value into the given [Hasher
]. Read more
fn hash_slice<H>(data: &[Self], state: &mut H) where
H: Hasher,
1.3.0
H: Hasher,
Feeds a slice of this type into the given [Hasher
]. Read more
impl Default for ContentType
[src]
fn default() -> ContentType
Returns a ContentType of Any
, or */*
.
impl FromStr for ContentType
[src]
type Err = &'static str
The associated error which can be returned from parsing.
fn from_str(raw: &str) -> Result<ContentType, &'static str>
Parses a ContentType from a given Content-Type header value.
Examples
Parsing an application/json
:
use std::str::FromStr; use rocket::http::ContentType; let json = ContentType::from_str("application/json").unwrap(); assert!(json.is_known()); assert_eq!(json, ContentType::JSON);
Parsing a content-type extension:
use std::str::FromStr; use rocket::http::ContentType; let custom = ContentType::from_str("application/x-custom").unwrap(); assert!(!custom.is_known()); assert_eq!(custom.ttype, "application"); assert_eq!(custom.subtype, "x-custom");
Parsing an invalid Content-Type value:
use std::str::FromStr; use rocket::http::ContentType; let custom = ContentType::from_str("application//x-custom"); assert!(custom.is_err());
impl Display for ContentType
[src]
fn fmt(&self, f: &mut Formatter) -> Result
Formats the ContentType as an HTTP Content-Type value.
Example
use rocket::http::ContentType; let ct = format!("{}", ContentType::JSON); assert_eq!(ct, "application/json");
impl Into<Header<'static>> for ContentType
[src]
Creates a new Header
with name Content-Type
and the value set to the
HTTP rendering of this Content-Type.