hyperx/header/common/
content_type.rs

1use mime::{self, Mime};
2
3header! {
4    /// `Content-Type` header, defined in
5    /// [RFC7231](http://tools.ietf.org/html/rfc7231#section-3.1.1.5)
6    ///
7    /// The `Content-Type` header field indicates the media type of the
8    /// associated representation: either the representation enclosed in the
9    /// message payload or the selected representation, as determined by the
10    /// message semantics.  The indicated media type defines both the data
11    /// format and how that data is intended to be processed by a recipient,
12    /// within the scope of the received message semantics, after any content
13    /// codings indicated by Content-Encoding are decoded.
14    ///
15    /// Although the `mime` crate allows the mime options to be any slice, this crate
16    /// forces the use of Vec. This is to make sure the same header can't have more than 1 type. If
17    /// this is an issue, it's possible to implement `Header` on a custom struct.
18    ///
19    /// # ABNF
20    ///
21    /// ```text
22    /// Content-Type = media-type
23    /// ```
24    ///
25    /// # Example values
26    ///
27    /// * `text/html; charset=utf-8`
28    /// * `application/json`
29    ///
30    /// # Examples
31    ///
32    /// ```
33    /// # extern crate http;
34    /// use hyperx::header::{ContentType, TypedHeaders};
35    ///
36    /// let mut headers = http::HeaderMap::new();
37    ///
38    /// headers.encode(
39    ///     &ContentType::json()
40    /// );
41    /// ```
42    ///
43    /// ```
44    /// # extern crate http;
45    /// use hyperx::header::{ContentType, TypedHeaders};
46    /// use hyperx::mime;
47    ///
48    /// let mut headers = http::HeaderMap::new();
49    ///
50    /// headers.encode(
51    ///     &ContentType(mime::TEXT_HTML)
52    /// );
53    /// ```
54    (ContentType, "Content-Type") => danger [Mime]
55
56    test_content_type {
57        test_header!(
58            test1,
59            vec![b"text/html"],
60            Some(HeaderField(TEXT_HTML)));
61    }
62}
63
64impl ContentType {
65    /// A constructor  to easily create a `Content-Type: application/json` header.
66    #[inline]
67    pub fn json() -> ContentType {
68        ContentType(mime::APPLICATION_JSON)
69    }
70
71    // Kind of deprecated, `text()` and `text_utf8()` are better.
72    // But don't bother with an actual #[deprecated], because the whole
73    // header system is changing in 0.12 anyways.
74    #[doc(hidden)]
75    pub fn plaintext() -> ContentType {
76        ContentType(mime::TEXT_PLAIN_UTF_8)
77    }
78
79    /// A constructor  to easily create a `Content-Type: text/plain` header.
80    #[inline]
81    pub fn text() -> ContentType {
82        ContentType(mime::TEXT_PLAIN)
83    }
84
85    /// A constructor  to easily create a `Content-Type: text/plain; charset=utf-8` header.
86    #[inline]
87    pub fn text_utf8() -> ContentType {
88        ContentType(mime::TEXT_PLAIN_UTF_8)
89    }
90
91    /// A constructor  to easily create a `Content-Type: text/html` header.
92    #[inline]
93    pub fn html() -> ContentType {
94        ContentType(mime::TEXT_HTML)
95    }
96
97    /// A constructor  to easily create a `Content-Type: text/xml` header.
98    #[inline]
99    pub fn xml() -> ContentType {
100        ContentType(mime::TEXT_XML)
101    }
102
103    /// A constructor  to easily create a `Content-Type: application/www-form-url-encoded` header.
104    #[inline]
105    pub fn form_url_encoded() -> ContentType {
106        ContentType(mime::APPLICATION_WWW_FORM_URLENCODED)
107    }
108    /// A constructor  to easily create a `Content-Type: image/jpeg` header.
109    #[inline]
110    pub fn jpeg() -> ContentType {
111        ContentType(mime::IMAGE_JPEG)
112    }
113
114    /// A constructor  to easily create a `Content-Type: image/png` header.
115    #[inline]
116    pub fn png() -> ContentType {
117        ContentType(mime::IMAGE_PNG)
118    }
119
120    /// A constructor  to easily create a `Content-Type: application/octet-stream` header.
121    #[inline]
122    pub fn octet_stream() -> ContentType {
123        ContentType(mime::APPLICATION_OCTET_STREAM)
124    }
125}
126
127impl Eq for ContentType {}
128
129bench_header!(bench, ContentType, { vec![b"application/json".to_vec()] });
130
131standard_header!(ContentType, CONTENT_TYPE);