1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-05-19 16:58:14 +00:00
actix-web/actix-web/src/http/header/content_type.rs

114 lines
3.4 KiB
Rust
Raw Normal View History

2019-02-07 21:24:24 +00:00
use mime::Mime;
2023-07-17 01:38:12 +00:00
use super::CONTENT_TYPE;
2021-06-25 11:25:50 +00:00
crate::http::header::common_header! {
/// `Content-Type` header, defined
/// in [RFC 7231 §3.1.1.5](https://datatracker.ietf.org/doc/html/rfc7231#section-3.1.1.5)
2019-02-07 21:24:24 +00:00
///
/// The `Content-Type` header field indicates the media type of the
/// associated representation: either the representation enclosed in the
/// message payload or the selected representation, as determined by the
/// message semantics. The indicated media type defines both the data
/// format and how that data is intended to be processed by a recipient,
/// within the scope of the received message semantics, after any content
/// codings indicated by Content-Encoding are decoded.
///
/// Although the `mime` crate allows the mime options to be any slice, this crate
/// forces the use of Vec. This is to make sure the same header can't have more than 1 type. If
/// this is an issue, it's possible to implement `Header` on a custom struct.
///
/// # ABNF
2021-12-02 15:25:39 +00:00
/// ```plain
2019-02-07 21:24:24 +00:00
/// Content-Type = media-type
/// ```
///
/// # Example Values
2019-02-07 21:24:24 +00:00
/// * `text/html; charset=utf-8`
/// * `application/json`
///
/// # Examples
2021-01-15 02:11:10 +00:00
/// ```
/// use actix_web::HttpResponse;
/// use actix_web::http::header::ContentType;
2019-02-07 21:24:24 +00:00
///
/// let mut builder = HttpResponse::Ok();
2021-01-15 02:11:10 +00:00
/// builder.insert_header(
2019-02-07 21:24:24 +00:00
/// ContentType::json()
/// );
/// ```
///
2021-01-15 02:11:10 +00:00
/// ```
/// use actix_web::HttpResponse;
/// use actix_web::http::header::ContentType;
2019-02-07 21:24:24 +00:00
///
/// let mut builder = HttpResponse::Ok();
2021-01-15 02:11:10 +00:00
/// builder.insert_header(
/// ContentType(mime::TEXT_HTML)
2019-02-07 21:24:24 +00:00
/// );
/// ```
(ContentType, CONTENT_TYPE) => [Mime]
test_parse_and_format {
2021-06-25 11:25:50 +00:00
crate::http::header::common_header_test!(
2019-02-07 21:24:24 +00:00
test1,
vec![b"text/html"],
Some(HeaderField(mime::TEXT_HTML)));
}
}
impl ContentType {
2021-10-26 08:24:38 +00:00
/// A constructor to easily create a `Content-Type: application/json`
2019-02-07 21:24:24 +00:00
/// header.
#[inline]
pub fn json() -> ContentType {
ContentType(mime::APPLICATION_JSON)
}
2021-10-26 08:24:38 +00:00
/// A constructor to easily create a `Content-Type: text/plain;
2019-02-07 21:24:24 +00:00
/// charset=utf-8` header.
#[inline]
pub fn plaintext() -> ContentType {
ContentType(mime::TEXT_PLAIN_UTF_8)
}
2021-10-26 08:24:38 +00:00
/// A constructor to easily create a `Content-Type: text/html; charset=utf-8`
/// header.
2019-02-07 21:24:24 +00:00
#[inline]
pub fn html() -> ContentType {
2021-10-26 08:24:38 +00:00
ContentType(mime::TEXT_HTML_UTF_8)
2019-02-07 21:24:24 +00:00
}
2021-10-26 08:24:38 +00:00
/// A constructor to easily create a `Content-Type: text/xml` header.
2019-02-07 21:24:24 +00:00
#[inline]
pub fn xml() -> ContentType {
ContentType(mime::TEXT_XML)
}
2021-10-26 08:24:38 +00:00
/// A constructor to easily create a `Content-Type:
2019-02-07 21:24:24 +00:00
/// application/www-form-url-encoded` header.
#[inline]
pub fn form_url_encoded() -> ContentType {
ContentType(mime::APPLICATION_WWW_FORM_URLENCODED)
}
2021-01-15 02:11:10 +00:00
2021-10-26 08:24:38 +00:00
/// A constructor to easily create a `Content-Type: image/jpeg` header.
2019-02-07 21:24:24 +00:00
#[inline]
pub fn jpeg() -> ContentType {
ContentType(mime::IMAGE_JPEG)
}
2021-10-26 08:24:38 +00:00
/// A constructor to easily create a `Content-Type: image/png` header.
2019-02-07 21:24:24 +00:00
#[inline]
pub fn png() -> ContentType {
ContentType(mime::IMAGE_PNG)
}
2021-10-26 08:24:38 +00:00
/// A constructor to easily create a `Content-Type:
2019-02-07 21:24:24 +00:00
/// application/octet-stream` header.
#[inline]
pub fn octet_stream() -> ContentType {
ContentType(mime::APPLICATION_OCTET_STREAM)
}
}