From 9f65271f73e1466541e225d41768f6b39f2c024a Mon Sep 17 00:00:00 2001 From: Rob Ede Date: Sat, 9 Jan 2021 05:56:16 +0000 Subject: [PATCH] use mime types in content type method --- actix-http/CHANGES.md | 3 +++ actix-http/src/response.rs | 19 +++++++++---------- src/lib.rs | 3 ++- src/types/either.rs | 2 +- src/types/form.rs | 22 ++++++++++------------ src/types/json.rs | 2 +- 6 files changed, 26 insertions(+), 25 deletions(-) diff --git a/actix-http/CHANGES.md b/actix-http/CHANGES.md index 6abd0ba76..5dbe7362b 100644 --- a/actix-http/CHANGES.md +++ b/actix-http/CHANGES.md @@ -1,6 +1,9 @@ # Changes ## Unreleased - 2021-xx-xx +* `Response::content_type` now takes an `impl IntoHeaderValue` to support `mime` types. [#????] + +[#????]: https://github.com/actix/actix-web/pull/???? ## 3.0.0-beta.1 - 2021-01-07 diff --git a/actix-http/src/response.rs b/actix-http/src/response.rs index df2f5be50..0a1f2cfd2 100644 --- a/actix-http/src/response.rs +++ b/actix-http/src/response.rs @@ -481,15 +481,14 @@ impl ResponseBuilder { self } - /// Set response content type + /// Set response content type. #[inline] pub fn content_type(&mut self, value: V) -> &mut Self where - HeaderValue: TryFrom, - >::Error: Into, + V: IntoHeaderValue, { if let Some(parts) = parts(&mut self.head, &self.err) { - match HeaderValue::try_from(value) { + match value.try_into() { Ok(value) => { parts.headers.insert(header::CONTENT_TYPE, value); } @@ -802,7 +801,7 @@ impl From for Response { impl From<&'static str> for Response { fn from(val: &'static str) -> Self { Response::Ok() - .content_type("text/plain; charset=utf-8") + .content_type(mime::TEXT_PLAIN_UTF_8) .body(val) } } @@ -810,7 +809,7 @@ impl From<&'static str> for Response { impl From<&'static [u8]> for Response { fn from(val: &'static [u8]) -> Self { Response::Ok() - .content_type("application/octet-stream") + .content_type(mime::APPLICATION_OCTET_STREAM) .body(val) } } @@ -818,7 +817,7 @@ impl From<&'static [u8]> for Response { impl From for Response { fn from(val: String) -> Self { Response::Ok() - .content_type("text/plain; charset=utf-8") + .content_type(mime::TEXT_PLAIN_UTF_8) .body(val) } } @@ -826,7 +825,7 @@ impl From for Response { impl<'a> From<&'a String> for Response { fn from(val: &'a String) -> Self { Response::Ok() - .content_type("text/plain; charset=utf-8") + .content_type(mime::TEXT_PLAIN_UTF_8) .body(val) } } @@ -834,7 +833,7 @@ impl<'a> From<&'a String> for Response { impl From for Response { fn from(val: Bytes) -> Self { Response::Ok() - .content_type("application/octet-stream") + .content_type(mime::APPLICATION_OCTET_STREAM) .body(val) } } @@ -842,7 +841,7 @@ impl From for Response { impl From for Response { fn from(val: BytesMut) -> Self { Response::Ok() - .content_type("application/octet-stream") + .content_type(mime::APPLICATION_OCTET_STREAM) .body(val) } } diff --git a/src/lib.rs b/src/lib.rs index cee266811..fa4e70aec 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -6,7 +6,8 @@ //! use actix_web::{get, web, App, HttpServer, Responder}; //! //! #[get("/{id}/{name}/index.html")] -//! async fn index(web::Path((id, name)): web::Path<(u32, String)>) -> impl Responder { +//! async fn index(path: web::Path<(u32, String)>) -> impl Responder { +//! let (id, name) = path.into_inner(); //! format!("Hello {}! id:{}", name, id) //! } //! diff --git a/src/types/either.rs b/src/types/either.rs index be4be7a22..229ad69a0 100644 --- a/src/types/either.rs +++ b/src/types/either.rs @@ -61,7 +61,7 @@ use crate::{ /// // respond with Right variant /// Either::Right( /// Ok(HttpResponse::Ok() -/// .content_type("text/html") +/// .content_type(mime::TEXT_HTML) /// .body("

Hello!

")) /// ) /// } diff --git a/src/types/form.rs b/src/types/form.rs index 145f994ad..b5bf07596 100644 --- a/src/types/form.rs +++ b/src/types/form.rs @@ -9,7 +9,7 @@ use std::{ task::{Context, Poll}, }; -use actix_http::{Error, HttpMessage, Payload}; +use actix_http::Payload; use bytes::BytesMut; use encoding_rs::{Encoding, UTF_8}; use futures_util::{ @@ -21,13 +21,8 @@ use serde::{de::DeserializeOwned, Serialize}; #[cfg(feature = "compress")] use crate::dev::Decompress; use crate::{ - error::UrlencodedError, - extract::FromRequest, - http::{ - header::{ContentType, CONTENT_LENGTH}, - StatusCode, - }, - web, HttpRequest, HttpResponse, Responder, + error::UrlencodedError, extract::FromRequest, http::header::CONTENT_LENGTH, web, + Error, HttpMessage, HttpRequest, HttpResponse, Responder, }; /// URL encoded payload extractor and responder. @@ -161,8 +156,8 @@ impl fmt::Display for Form { impl Responder for Form { fn respond_to(self, _: &HttpRequest) -> HttpResponse { match serde_urlencoded::to_string(&self.0) { - Ok(body) => HttpResponse::build(StatusCode::OK) - .set(ContentType::form_url_encoded()) + Ok(body) => HttpResponse::Ok() + .content_type(mime::APPLICATION_WWW_FORM_URLENCODED) .body(body), Err(err) => HttpResponse::from_error(err.into()), } @@ -372,7 +367,10 @@ mod tests { use serde::{Deserialize, Serialize}; use super::*; - use crate::http::header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE}; + use crate::http::{ + header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE}, + StatusCode, + }; use crate::test::TestRequest; #[derive(Deserialize, Serialize, Debug, PartialEq)] @@ -509,6 +507,6 @@ mod tests { assert!(s.is_err()); let err_str = s.err().unwrap().to_string(); - assert!(err_str.contains("Urlencoded payload size is bigger")); + assert!(err_str.starts_with("URL encoded payload is larger")); } } diff --git a/src/types/json.rs b/src/types/json.rs index 388538f75..edfb775f3 100644 --- a/src/types/json.rs +++ b/src/types/json.rs @@ -121,7 +121,7 @@ impl Responder for Json { fn respond_to(self, _: &HttpRequest) -> HttpResponse { match serde_json::to_string(&self.0) { Ok(body) => HttpResponse::Ok() - .content_type("application/json") + .content_type(mime::APPLICATION_JSON) .body(body), Err(err) => HttpResponse::from_error(err.into()), }