aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--askama/Cargo.toml5
-rw-r--r--askama/src/lib.rs33
-rw-r--r--testing/tests/actix_web.rs10
-rw-r--r--testing/tests/gotham.rs5
4 files changed, 44 insertions, 9 deletions
diff --git a/askama/Cargo.toml b/askama/Cargo.toml
index 2b71d19..2c328d9 100644
--- a/askama/Cargo.toml
+++ b/askama/Cargo.toml
@@ -24,8 +24,8 @@ serde-json = ["askama_shared/serde_json"]
serde-yaml = ["askama_shared/serde_yaml"]
with-iron = ["iron", "askama_derive/iron"]
with-rocket = ["rocket", "askama_derive/rocket"]
-with-actix-web = ["actix-web", "askama_derive/actix-web", "mime_guess", "bytes"]
-with-gotham = ["gotham", "askama_derive/gotham", "hyper", "mime_guess"]
+with-actix-web = ["actix-web", "askama_derive/actix-web", "mime", "mime_guess", "bytes"]
+with-gotham = ["gotham", "askama_derive/gotham", "hyper", "mime", "mime_guess"]
[dependencies]
askama_derive = { version = "0.8.0", path = "../askama_derive" }
@@ -34,6 +34,7 @@ askama_shared = { version = "0.8.0", path = "../askama_shared" }
iron = { version = ">= 0.5, < 0.7", optional = true }
rocket = { version = "0.4", optional = true }
actix-web = { version = "0.7", optional = true }
+mime = { version = "0.3", optional = true }
mime_guess = { version = "2.0.0-alpha", optional = true }
gotham = { version = "0.3", optional = true }
hyper = { version = "0.12", optional = true }
diff --git a/askama/src/lib.rs b/askama/src/lib.rs
index d48a546..ad0c85f 100644
--- a/askama/src/lib.rs
+++ b/askama/src/lib.rs
@@ -514,6 +514,33 @@ pub mod rocket {
}
}
+#[cfg(all(feature = "mime_guess", feature = "mime"))]
+fn get_mime_type(ext: &str) -> mime_guess::Mime {
+ let basic_type = mime_guess::get_mime_type(ext);
+ for (simple, utf_8) in &TEXT_TYPES {
+ if &basic_type == simple {
+ return utf_8.clone();
+ }
+ }
+ basic_type
+}
+
+#[cfg(all(feature = "mime_guess", feature = "mime"))]
+const TEXT_TYPES: [(mime_guess::Mime, mime_guess::Mime); 6] = [
+ (mime::TEXT_PLAIN, mime::TEXT_PLAIN_UTF_8),
+ (mime::TEXT_HTML, mime::TEXT_HTML_UTF_8),
+ (mime::TEXT_CSS, mime::TEXT_CSS_UTF_8),
+ (mime::TEXT_CSV, mime::TEXT_CSV_UTF_8),
+ (
+ mime::TEXT_TAB_SEPARATED_VALUES,
+ mime::TEXT_TAB_SEPARATED_VALUES_UTF_8,
+ ),
+ (
+ mime::APPLICATION_JAVASCRIPT,
+ mime::APPLICATION_JAVASCRIPT_UTF_8,
+ ),
+];
+
#[cfg(feature = "with-actix-web")]
pub mod actix_web {
extern crate actix_web;
@@ -555,7 +582,6 @@ pub mod actix_web {
pub use self::actix_web::{
error::ErrorInternalServerError, Error, HttpRequest, HttpResponse, Responder,
};
- use self::mime_guess::get_mime_type;
pub trait TemplateIntoResponse {
fn into_response(&self) -> Result<HttpResponse, Error>;
@@ -567,7 +593,7 @@ pub mod actix_web {
self.render_into(&mut buffer)
.map_err(|_| ErrorInternalServerError("Template parsing error"))?;
- let ctype = get_mime_type(T::extension().unwrap_or("txt")).to_string();
+ let ctype = super::get_mime_type(T::extension().unwrap_or("txt")).to_string();
Ok(HttpResponse::Ok()
.content_type(ctype.as_str())
.body(buffer.freeze()))
@@ -581,10 +607,9 @@ pub mod gotham {
use gotham::helpers::http::response::{create_empty_response, create_response};
pub use gotham::state::State;
pub use hyper::{Body, Response, StatusCode};
- use mime_guess::get_mime_type;
pub fn respond<T: super::Template>(t: &T, ext: &str) -> Response<Body> {
- let mime_type = get_mime_type(ext).to_string();
+ let mime_type = super::get_mime_type(ext).to_string();
match t.render() {
Ok(body) => Response::builder()
diff --git a/testing/tests/actix_web.rs b/testing/tests/actix_web.rs
index 7f2d835..e434ce3 100644
--- a/testing/tests/actix_web.rs
+++ b/testing/tests/actix_web.rs
@@ -18,7 +18,10 @@ fn test_actix_web() {
let request = srv.get().finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
- assert_eq!(response.headers().get(CONTENT_TYPE).unwrap(), "text/html");
+ assert_eq!(
+ response.headers().get(CONTENT_TYPE).unwrap(),
+ "text/html; charset=utf-8"
+ );
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static("Hello, world!".as_ref()));
@@ -36,7 +39,10 @@ fn test_actix_web_responder() {
let request = srv.get().finish().unwrap();
let response = srv.execute(request.send()).unwrap();
assert!(response.status().is_success());
- assert_eq!(response.headers().get(CONTENT_TYPE).unwrap(), "text/html");
+ assert_eq!(
+ response.headers().get(CONTENT_TYPE).unwrap(),
+ "text/html; charset=utf-8"
+ );
let bytes = srv.execute(response.body()).unwrap();
assert_eq!(bytes, Bytes::from_static("Hello, world!".as_ref()));
diff --git a/testing/tests/gotham.rs b/testing/tests/gotham.rs
index f127d59..225a0ef 100644
--- a/testing/tests/gotham.rs
+++ b/testing/tests/gotham.rs
@@ -31,7 +31,10 @@ fn test_gotham() {
let content_type = headers
.get("content-type")
.expect("Response did not contain content-type header");
- assert_eq!(content_type.to_str().unwrap(), mime::TEXT_HTML.to_string());
+ assert_eq!(
+ content_type.to_str().unwrap(),
+ mime::TEXT_HTML_UTF_8.to_string()
+ );
}
let body = res.read_utf8_body().expect("failed to read response body");