From 9cf01f59913b860ad21027f1f3b261c434dd2333 Mon Sep 17 00:00:00 2001 From: Edward Shen Date: Fri, 17 Apr 2020 13:23:00 -0400 Subject: [PATCH] explicitly set content-type to text/html; charset=utf-8 --- src/main.rs | 6 ++++-- src/routes.rs | 39 ++++++++++++++++++++++----------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/main.rs b/src/main.rs index 2e543aa..a1294d9 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,7 +4,7 @@ use crate::config::{read_config, Route, RouteGroup}; use actix_web::{middleware::Logger, App, HttpServer}; use clap::{crate_authors, crate_version, load_yaml, App as ClapApp}; use error::BunBunError; -use handlebars::{Handlebars}; +use handlebars::Handlebars; use hotwatch::{Event, Hotwatch}; use log::{debug, info, trace, warn}; use std::cmp::min; @@ -118,7 +118,9 @@ fn cache_routes(groups: &[RouteGroup]) -> HashMap { fn compile_templates() -> Handlebars { let mut handlebars = Handlebars::new(); handlebars.set_strict_mode(true); - handlebars.register_partial("bunbun_version", env!("CARGO_PKG_VERSION")).unwrap(); + handlebars + .register_partial("bunbun_version", env!("CARGO_PKG_VERSION")) + .unwrap(); macro_rules! register_template { [ $( $template:expr ),* ] => { $( diff --git a/src/routes.rs b/src/routes.rs index 9a7e713..f25ec69 100644 --- a/src/routes.rs +++ b/src/routes.rs @@ -1,4 +1,5 @@ use crate::{template_args, BunBunError, Route, State}; +use actix_web::http::header::ContentType; use actix_web::web::{Data, Query}; use actix_web::{get, http::header}; use actix_web::{HttpRequest, HttpResponse, Responder}; @@ -26,16 +27,18 @@ type StateData = Data>>; #[get("/")] pub async fn index(data: StateData, req: HttpRequest) -> impl Responder { let data = data.read().unwrap(); - HttpResponse::Ok().body( - req - .app_data::() - .unwrap() - .render( - "index", - &template_args::hostname(data.public_address.clone()), - ) - .unwrap(), - ) + HttpResponse::Ok() + .set_header(header::CONTENT_TYPE, "text/html; charset=utf-8") + .body( + req + .app_data::() + .unwrap() + .render( + "index", + &template_args::hostname(data.public_address.clone()), + ) + .unwrap(), + ) } #[get("/bunbunsearch.xml")] @@ -61,13 +64,15 @@ pub async fn opensearch(data: StateData, req: HttpRequest) -> impl Responder { #[get("/ls")] pub async fn list(data: StateData, req: HttpRequest) -> impl Responder { let data = data.read().unwrap(); - HttpResponse::Ok().body( - req - .app_data::() - .unwrap() - .render("list", &data.groups) - .unwrap(), - ) + HttpResponse::Ok() + .set_header(header::CONTENT_TYPE, "text/html; charset=utf-8") + .body( + req + .app_data::() + .unwrap() + .render("list", &data.groups) + .unwrap(), + ) } #[derive(Deserialize)]