actix_web/http/header/
content_type.rs

1use mime::Mime;
2
3use super::CONTENT_TYPE;
4
5crate::http::header::common_header! {
6    /// `Content-Type` header, defined in [RFC 9110 §8.3].
7    ///
8    /// The `Content-Type` header field indicates the media type of the associated representation:
9    /// either the representation enclosed in the message payload or the selected representation,
10    /// as determined by the message semantics. The indicated media type defines both the data
11    /// format and how that data is intended to be processed by a recipient, within the scope of the
12    /// received message semantics, after any content codings indicated by Content-Encoding are
13    /// decoded.
14    ///
15    /// Although the `mime` crate allows the mime options to be any slice, this crate forces the use
16    /// of Vec. This is to make sure the same header can't have more than 1 type. If this is an
17    /// issue, it's possible to implement `Header` on a custom struct.
18    ///
19    /// # ABNF
20    ///
21    /// ```plain
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    /// use actix_web::{http::header::ContentType, HttpResponse};
34    ///
35    /// let res_json = HttpResponse::Ok()
36    ///     .insert_header(ContentType::json());
37    ///
38    /// let res_html = HttpResponse::Ok()
39    ///     .insert_header(ContentType(mime::TEXT_HTML));
40    /// ```
41    ///
42    /// [RFC 9110 §8.3]: https://datatracker.ietf.org/doc/html/rfc9110#section-8.3
43    (ContentType, CONTENT_TYPE) => [Mime]
44
45    test_parse_and_format {
46        crate::http::header::common_header_test!(
47            test_text_html,
48            [b"text/html"],
49            Some(HeaderField(mime::TEXT_HTML)));
50        crate::http::header::common_header_test!(
51            test_image_star,
52            [b"image/*"],
53            Some(HeaderField(mime::IMAGE_STAR)));
54
55    }
56}
57
58impl ContentType {
59    /// Constructs a `Content-Type: application/json` header.
60    #[inline]
61    pub fn json() -> ContentType {
62        ContentType(mime::APPLICATION_JSON)
63    }
64
65    /// Constructs a `Content-Type: text/plain; charset=utf-8` header.
66    #[inline]
67    pub fn plaintext() -> ContentType {
68        ContentType(mime::TEXT_PLAIN_UTF_8)
69    }
70
71    /// Constructs a `Content-Type: text/html; charset=utf-8` header.
72    #[inline]
73    pub fn html() -> ContentType {
74        ContentType(mime::TEXT_HTML_UTF_8)
75    }
76
77    /// Constructs a `Content-Type: text/xml` header.
78    #[inline]
79    pub fn xml() -> ContentType {
80        ContentType(mime::TEXT_XML)
81    }
82
83    /// Constructs a `Content-Type: application/www-form-url-encoded` header.
84    #[inline]
85    pub fn form_url_encoded() -> ContentType {
86        ContentType(mime::APPLICATION_WWW_FORM_URLENCODED)
87    }
88
89    /// Constructs a `Content-Type: image/jpeg` header.
90    #[inline]
91    pub fn jpeg() -> ContentType {
92        ContentType(mime::IMAGE_JPEG)
93    }
94
95    /// Constructs a `Content-Type: image/png` header.
96    #[inline]
97    pub fn png() -> ContentType {
98        ContentType(mime::IMAGE_PNG)
99    }
100
101    /// Constructs a `Content-Type: application/octet-stream` header.
102    #[inline]
103    pub fn octet_stream() -> ContentType {
104        ContentType(mime::APPLICATION_OCTET_STREAM)
105    }
106}