From 332d741f212eb2aac7bfb2eec1df1f33bfb46a05 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Thu, 6 Jan 2022 14:30:46 +0100 Subject: [PATCH] Use Template::MIME_TYPE instead of extension --- askama_actix/src/lib.rs | 11 ++++------- askama_axum/src/lib.rs | 6 +++--- askama_gotham/src/lib.rs | 6 +++--- askama_mendes/src/lib.rs | 19 ++++++------------- askama_mendes/tests/basic.rs | 2 +- askama_rocket/src/lib.rs | 7 +++---- askama_tide/src/lib.rs | 10 +++------- askama_warp/src/lib.rs | 7 ++----- 8 files changed, 25 insertions(+), 43 deletions(-) diff --git a/askama_actix/src/lib.rs b/askama_actix/src/lib.rs index c4317c427..59957f4be 100644 --- a/askama_actix/src/lib.rs +++ b/askama_actix/src/lib.rs @@ -5,9 +5,9 @@ use std::fmt; use actix_web::body::BoxBody; +use actix_web::http::header::HeaderValue; use actix_web::http::StatusCode; use actix_web::{HttpResponse, HttpResponseBuilder, ResponseError}; -use askama::mime::extension_to_mime_type; pub use askama::*; /// Newtype to let askama::Error implement actix_web::ResponseError. @@ -36,12 +36,9 @@ pub trait TemplateToResponse { impl TemplateToResponse for T { fn to_response(&self) -> HttpResponse { match self.render() { - Ok(buffer) => { - let ctype = extension_to_mime_type(T::EXTENSION.unwrap_or("txt")); - HttpResponseBuilder::new(StatusCode::OK) - .content_type(ctype) - .body(buffer) - } + Ok(buffer) => HttpResponseBuilder::new(StatusCode::OK) + .content_type(HeaderValue::from_static(T::MIME_TYPE)) + .body(buffer), Err(err) => HttpResponse::from_error(ActixError(err)), } } diff --git a/askama_axum/src/lib.rs b/askama_axum/src/lib.rs index 21217ff29..104867499 100644 --- a/askama_axum/src/lib.rs +++ b/askama_axum/src/lib.rs @@ -10,13 +10,13 @@ pub use http::Response; use http::StatusCode; use http_body::{Empty, Full}; -pub fn into_response(t: &T, ext: &str) -> Response { +pub fn into_response(t: &T, _ext: &str) -> Response { match t.render() { Ok(body) => Response::builder() .status(StatusCode::OK) .header( - "content-type", - askama::mime::extension_to_mime_type(ext).to_string(), + http::header::CONTENT_TYPE, + http::HeaderValue::from_static(T::MIME_TYPE), ) .body(body::boxed(Full::from(body))) .unwrap(), diff --git a/askama_gotham/src/lib.rs b/askama_gotham/src/lib.rs index 1241c9c06..c2c933da4 100644 --- a/askama_gotham/src/lib.rs +++ b/askama_gotham/src/lib.rs @@ -8,13 +8,13 @@ pub use gotham::handler::IntoResponse; pub use gotham::state::State; pub use hyper::{Body, Response, StatusCode}; -pub fn respond(t: &T, ext: &str) -> Response { +pub fn respond(t: &T, _ext: &str) -> Response { match t.render() { Ok(body) => Response::builder() .status(StatusCode::OK) .header( - "content-type", - mime::extension_to_mime_type(ext).to_string(), + hyper::header::CONTENT_TYPE, + hyper::header::HeaderValue::from_static(T::MIME_TYPE), ) .body(body.into()) .unwrap(), diff --git a/askama_mendes/src/lib.rs b/askama_mendes/src/lib.rs index baaa49fd5..d1ccb2c5a 100644 --- a/askama_mendes/src/lib.rs +++ b/askama_mendes/src/lib.rs @@ -2,13 +2,10 @@ #![deny(elided_lifetimes_in_paths)] #![deny(unreachable_pub)] -use std::convert::TryFrom; - use mendes::application::{Application, Responder}; use mendes::http::header::{HeaderValue, CONTENT_LENGTH, CONTENT_TYPE}; use mendes::http::request::Parts; use mendes::http::Response; -use mime_guess::MimeGuess; pub use askama::*; @@ -16,7 +13,7 @@ pub fn into_response( app: &A, req: &Parts, t: &T, - ext: Option<&str>, + _ext: Option<&str>, ) -> Response where A: Application, @@ -29,13 +26,9 @@ where Err(e) => return >::from(e).into_response(app, req), }; - let mut builder = Response::builder(); - builder = builder.header(CONTENT_LENGTH, content.len()); - if let Some(ext) = ext { - if let Some(ty) = MimeGuess::from_ext(ext).first() { - builder = builder.header(CONTENT_TYPE, HeaderValue::try_from(ty.as_ref()).unwrap()); - } - } - - builder.body(content.into()).unwrap() + Response::builder() + .header(CONTENT_LENGTH, content.len()) + .header(CONTENT_TYPE, HeaderValue::from_static(T::MIME_TYPE)) + .body(content.into()) + .unwrap() } diff --git a/askama_mendes/tests/basic.rs b/askama_mendes/tests/basic.rs index 2a07c1380..e2f5b2525 100644 --- a/askama_mendes/tests/basic.rs +++ b/askama_mendes/tests/basic.rs @@ -19,7 +19,7 @@ async fn test() { rsp.headers .get("content-type") .and_then(|hv| hv.to_str().ok()), - Some("text/plain") + Some("text/plain; charset=utf-8") ); assert_eq!(to_bytes(body).await.unwrap(), &b"Hello, world!"[..]); } diff --git a/askama_rocket/src/lib.rs b/askama_rocket/src/lib.rs index 33bd7792d..f10ba871d 100644 --- a/askama_rocket/src/lib.rs +++ b/askama_rocket/src/lib.rs @@ -5,16 +5,15 @@ use std::io::Cursor; pub use askama::*; -use rocket::http::{ContentType, Status}; +use rocket::http::{Header, Status}; pub use rocket::request::Request; use rocket::response::Response; pub use rocket::response::{Responder, Result}; -pub fn respond(t: &T, ext: &str) -> Result<'static> { +pub fn respond(t: &T, _ext: &str) -> Result<'static> { let rsp = t.render().map_err(|_| Status::InternalServerError)?; - let ctype = ContentType::from_extension(ext).ok_or(Status::InternalServerError)?; Response::build() - .header(ctype) + .header(Header::new("content-type", T::MIME_TYPE)) .sized_body(Cursor::new(rsp)) .ok() } diff --git a/askama_tide/src/lib.rs b/askama_tide/src/lib.rs index 2854cb6b6..be597bba1 100644 --- a/askama_tide/src/lib.rs +++ b/askama_tide/src/lib.rs @@ -6,16 +6,12 @@ pub use askama; pub use tide; use askama::*; -use tide::{http::Mime, Body, Response}; +use tide::{Body, Response}; -pub fn try_into_body(t: &T, ext: &str) -> Result { +pub fn try_into_body(t: &T, _ext: &str) -> Result { let string = t.render()?; let mut body = Body::from_string(string); - - if let Some(mime) = Mime::from_extension(ext) { - body.set_mime(mime); - } - + body.set_mime(T::MIME_TYPE); Ok(body) } diff --git a/askama_warp/src/lib.rs b/askama_warp/src/lib.rs index 4220efcad..b2f038744 100644 --- a/askama_warp/src/lib.rs +++ b/askama_warp/src/lib.rs @@ -9,14 +9,11 @@ use warp::http::{self, header, StatusCode}; use warp::hyper::Body; use warp::reply::Response; -pub fn reply(t: &T, ext: &str) -> Response { +pub fn reply(t: &T, _ext: &str) -> Response { match t.render() { Ok(body) => http::Response::builder() .status(StatusCode::OK) - .header( - header::CONTENT_TYPE, - mime::extension_to_mime_type(ext).to_string(), - ) + .header(header::CONTENT_TYPE, T::MIME_TYPE) .body(body.into()), Err(_) => http::Response::builder() .status(StatusCode::INTERNAL_SERVER_ERROR)