explicitly set content-type to text/html; charset=utf-8

This commit is contained in:
Edward Shen 2020-04-17 13:23:00 -04:00
parent 8cdd216b39
commit 9cf01f5991
Signed by: edward
GPG key ID: 19182661E818369F
2 changed files with 26 additions and 19 deletions

View file

@ -4,7 +4,7 @@ use crate::config::{read_config, Route, RouteGroup};
use actix_web::{middleware::Logger, App, HttpServer}; use actix_web::{middleware::Logger, App, HttpServer};
use clap::{crate_authors, crate_version, load_yaml, App as ClapApp}; use clap::{crate_authors, crate_version, load_yaml, App as ClapApp};
use error::BunBunError; use error::BunBunError;
use handlebars::{Handlebars}; use handlebars::Handlebars;
use hotwatch::{Event, Hotwatch}; use hotwatch::{Event, Hotwatch};
use log::{debug, info, trace, warn}; use log::{debug, info, trace, warn};
use std::cmp::min; use std::cmp::min;
@ -118,7 +118,9 @@ fn cache_routes(groups: &[RouteGroup]) -> HashMap<String, Route> {
fn compile_templates() -> Handlebars { fn compile_templates() -> Handlebars {
let mut handlebars = Handlebars::new(); let mut handlebars = Handlebars::new();
handlebars.set_strict_mode(true); 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 { macro_rules! register_template {
[ $( $template:expr ),* ] => { [ $( $template:expr ),* ] => {
$( $(

View file

@ -1,4 +1,5 @@
use crate::{template_args, BunBunError, Route, State}; use crate::{template_args, BunBunError, Route, State};
use actix_web::http::header::ContentType;
use actix_web::web::{Data, Query}; use actix_web::web::{Data, Query};
use actix_web::{get, http::header}; use actix_web::{get, http::header};
use actix_web::{HttpRequest, HttpResponse, Responder}; use actix_web::{HttpRequest, HttpResponse, Responder};
@ -26,16 +27,18 @@ type StateData = Data<Arc<RwLock<State>>>;
#[get("/")] #[get("/")]
pub async fn index(data: StateData, req: HttpRequest) -> impl Responder { pub async fn index(data: StateData, req: HttpRequest) -> impl Responder {
let data = data.read().unwrap(); let data = data.read().unwrap();
HttpResponse::Ok().body( HttpResponse::Ok()
req .set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
.app_data::<Handlebars>() .body(
.unwrap() req
.render( .app_data::<Handlebars>()
"index", .unwrap()
&template_args::hostname(data.public_address.clone()), .render(
) "index",
.unwrap(), &template_args::hostname(data.public_address.clone()),
) )
.unwrap(),
)
} }
#[get("/bunbunsearch.xml")] #[get("/bunbunsearch.xml")]
@ -61,13 +64,15 @@ pub async fn opensearch(data: StateData, req: HttpRequest) -> impl Responder {
#[get("/ls")] #[get("/ls")]
pub async fn list(data: StateData, req: HttpRequest) -> impl Responder { pub async fn list(data: StateData, req: HttpRequest) -> impl Responder {
let data = data.read().unwrap(); let data = data.read().unwrap();
HttpResponse::Ok().body( HttpResponse::Ok()
req .set_header(header::CONTENT_TYPE, "text/html; charset=utf-8")
.app_data::<Handlebars>() .body(
.unwrap() req
.render("list", &data.groups) .app_data::<Handlebars>()
.unwrap(), .unwrap()
) .render("list", &data.groups)
.unwrap(),
)
} }
#[derive(Deserialize)] #[derive(Deserialize)]