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

The "type" component of the Content-Type.

The "subtype" component of the Content-Type.

Semicolon-seperated parameters associated with the Content-Type.

Methods

impl ContentType
[src]

ContentType for any : * / *

ContentType for HTML : text / html ; charset=utf-8

ContentType for Plain : text / plain ; charset=utf-8

ContentType for JSON : application / json

ContentType for form : application / x-www-form-urlencoded

ContentType for JavaScript : application / javascript

ContentType for CSS : text / css ; charset=utf-8

ContentType for data form : multipart / form-data

ContentType for XML : text / xml ; charset=utf-8

ContentType for CSV : text / csv ; charset=utf-8

ContentType for PNG : image / png

ContentType for GIF : image / gif

ContentType for BMP : image / bmp

ContentType for JPEG : image / jpeg

ContentType for WEBP : image / webp

ContentType for SVG : image / svg+xml

ContentType for PDF : application / pdf

ContentType for TTF : application / font-sfnt

ContentType for OTF : application / font-sfnt

ContentType for WOFF : application / font-woff

ContentType for WOFF2 : font / woff2

Returns true if this ContentType is known to Rocket, that is, there is an associated constant for self.

Returns true if self is a any ContentType: * / * .

Paramaters are not taken into account when doing this check.

Returns true if self is a HTML ContentType: text / html .

Paramaters are not taken into account when doing this check.

Returns true if self is a Plain ContentType: text / plain .

Paramaters are not taken into account when doing this check.

Returns true if self is a JSON ContentType: application / json .

Paramaters are not taken into account when doing this check.

Returns true if self is a form ContentType: application / x-www-form-urlencoded .

Paramaters are not taken into account when doing this check.

Returns true if self is a JavaScript ContentType: application / javascript .

Paramaters are not taken into account when doing this check.

Returns true if self is a CSS ContentType: text / css .

Paramaters are not taken into account when doing this check.

Returns true if self is a data form ContentType: multipart / form-data .

Paramaters are not taken into account when doing this check.

Returns true if self is a XML ContentType: text / xml .

Paramaters are not taken into account when doing this check.

Returns true if self is a CSV ContentType: text / csv .

Paramaters are not taken into account when doing this check.

Returns true if self is a PNG ContentType: image / png .

Paramaters are not taken into account when doing this check.

Returns true if self is a GIF ContentType: image / gif .

Paramaters are not taken into account when doing this check.

Returns true if self is a BMP ContentType: image / bmp .

Paramaters are not taken into account when doing this check.

Returns true if self is a JPEG ContentType: image / jpeg .

Paramaters are not taken into account when doing this check.

Returns true if self is a WEBP ContentType: image / webp .

Paramaters are not taken into account when doing this check.

Returns true if self is a SVG ContentType: image / svg+xml .

Paramaters are not taken into account when doing this check.

Returns true if self is a PDF ContentType: application / pdf .

Paramaters are not taken into account when doing this check.

Returns true if self is a TTF ContentType: application / font-sfnt .

Paramaters are not taken into account when doing this check.

Returns true if self is a OTF ContentType: application / font-sfnt .

Paramaters are not taken into account when doing this check.

Returns true if self is a WOFF ContentType: application / font-woff .

Paramaters are not taken into account when doing this check.

Returns true if self is a WOFF2 ContentType: font / woff2 .

Paramaters are not taken into account when doing this check.

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

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

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

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]

Formats the value using the given formatter.

impl Clone for ContentType
[src]

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

impl PartialEq for ContentType
[src]

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

impl Hash for ContentType
[src]

Feeds this value into the state given, updating the hasher as necessary.

Feeds a slice of this type into the state provided.

impl Default for ContentType
[src]

Returns a ContentType of Any, or */*.

impl FromStr for ContentType
[src]

The associated error which can be returned from parsing.

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]

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.

Performs the conversion.

impl<'a, 'r> FromRequest<'a, 'r> for ContentType
[src]

The associated error to be returned if derivation fails.

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