Struct rocket::http::ContentType
[−]
[src]
pub struct ContentType { pub ttype: Cow<'static, str>, pub subtype: Cow<'static, str>, pub params: Option<Cow<'static, str>>, }
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: Cow<'static, str>
The "type" component of the Content-Type.
subtype: Cow<'static, str>
The "subtype" component of the Content-Type.
params: Option<Cow<'static, str>>
Semicolon-seperated parameters associated with the Content-Type.
Methods
impl ContentType
[src]
const Any: ContentType = ContentType{ttype: Cow::Borrowed("*"),
subtype: Cow::Borrowed("*"),
params: None,}
ContentType for any : * / *
const HTML: ContentType = ContentType{ttype: Cow::Borrowed("text"),
subtype: Cow::Borrowed("html"),
params: Some(Cow::Borrowed("charset=utf-8")),}
ContentType for HTML : text / html ; charset=utf-8
const Plain: ContentType = ContentType{ttype: Cow::Borrowed("text"),
subtype: Cow::Borrowed("plain"),
params: Some(Cow::Borrowed("charset=utf-8")),}
ContentType for Plain : text / plain ; charset=utf-8
const JSON: ContentType = ContentType{ttype: Cow::Borrowed("application"),
subtype: Cow::Borrowed("json"),
params: None,}
ContentType for JSON : application / json
const Form: ContentType = ContentType{ttype: Cow::Borrowed("application"),
subtype: Cow::Borrowed("x-www-form-urlencoded"),
params: None,}
ContentType for form : application / x-www-form-urlencoded
const JavaScript: ContentType = ContentType{ttype: Cow::Borrowed("application"),
subtype: Cow::Borrowed("javascript"),
params: None,}
ContentType for JavaScript : application / javascript
const CSS: ContentType = ContentType{ttype: Cow::Borrowed("text"),
subtype: Cow::Borrowed("css"),
params: Some(Cow::Borrowed("charset=utf-8")),}
ContentType for CSS : text / css ; charset=utf-8
const DataForm: ContentType = ContentType{ttype: Cow::Borrowed("multipart"),
subtype: Cow::Borrowed("form-data"),
params: None,}
ContentType for data form : multipart / form-data
const XML: ContentType = ContentType{ttype: Cow::Borrowed("text"),
subtype: Cow::Borrowed("xml"),
params: Some(Cow::Borrowed("charset=utf-8")),}
ContentType for XML : text / xml ; charset=utf-8
const CSV: ContentType = ContentType{ttype: Cow::Borrowed("text"),
subtype: Cow::Borrowed("csv"),
params: Some(Cow::Borrowed("charset=utf-8")),}
ContentType for CSV : text / csv ; charset=utf-8
const PNG: ContentType = ContentType{ttype: Cow::Borrowed("image"),
subtype: Cow::Borrowed("png"),
params: None,}
ContentType for PNG : image / png
const GIF: ContentType = ContentType{ttype: Cow::Borrowed("image"),
subtype: Cow::Borrowed("gif"),
params: None,}
ContentType for GIF : image / gif
const BMP: ContentType = ContentType{ttype: Cow::Borrowed("image"),
subtype: Cow::Borrowed("bmp"),
params: None,}
ContentType for BMP : image / bmp
const JPEG: ContentType = ContentType{ttype: Cow::Borrowed("image"),
subtype: Cow::Borrowed("jpeg"),
params: None,}
ContentType for JPEG : image / jpeg
const PDF: ContentType = ContentType{ttype: Cow::Borrowed("application"),
subtype: Cow::Borrowed("pdf"),
params: None,}
ContentType for PDF : application / pdf
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_pdf(&self) -> bool
Returns true
if self
is a
PDF
ContentType:
application
/
pdf
.
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>>
Creates a new ContentType
with type ttype
and subtype subtype
.
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-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>>
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());
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 state given, updating the hasher as necessary.
fn hash_slice<H>(data: &[Self], state: &mut H) where H: Hasher
1.3.0
Feeds a slice of this type into the state provided.
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_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.