From 16055300c760c636399f555ce30c07deff2a6820 Mon Sep 17 00:00:00 2001 From: Yves Fischer Date: Mon, 26 Nov 2018 21:01:09 +0100 Subject: Redo response builder usage - Set correct content-type header - Refactor views to use one base-view --- src/request_handler/mod.rs | 39 ++++++++++++++++++++++++++++----------- 1 file changed, 28 insertions(+), 11 deletions(-) (limited to 'src/request_handler/mod.rs') diff --git a/src/request_handler/mod.rs b/src/request_handler/mod.rs index 2e92821..72e9142 100644 --- a/src/request_handler/mod.rs +++ b/src/request_handler/mod.rs @@ -12,6 +12,7 @@ use std::cell::RefCell; use time; use http::{Request, Response, StatusCode, Method}; +use http::response::Builder; use tokio::prelude::*; use horrorshow; use cookie::{Cookie, CookieBuilder}; @@ -55,12 +56,24 @@ pub struct RequestHandler { routing_table: router::RoutingTable, } -pub(in request_handler) fn make_response(code: StatusCode, body: String) -> Response { - Response::builder().status(code).body(body).unwrap() +pub trait ResponseBuilderExtra { + fn set_defaults(&mut self) -> &mut Self; +} + +impl ResponseBuilderExtra for Builder { + fn set_defaults(&mut self) -> &mut Self { + self + .status(StatusCode::OK) + .header(::http::header::CONTENT_TYPE, "text/html; charset=utf-8") + .header("X-Frame-Options", "DENY") + } } pub(in request_handler) fn error_handler_internal(body: String) -> Response { - Response::builder().status(StatusCode::INTERNAL_SERVER_ERROR).body(body).unwrap() + Response::builder() + .set_defaults() + .status(StatusCode::INTERNAL_SERVER_ERROR) + .body(body).unwrap() } impl HttpHandler for RequestHandler { @@ -71,8 +84,8 @@ impl HttpHandler for RequestHandler { Ok((Route::Logout, rest)) => logout(state, &req, rest), Ok((Route::Check, rest)) => check(state, &req, rest), Err(error) => match error { - router::NoMatchingRoute => - make_response(StatusCode::NOT_FOUND, "Resource not found".to_string()), + router::NoMatchingRoute => Response::builder().set_defaults() + .status(StatusCode::NOT_FOUND).body("Resource not found".to_string()).unwrap(), } } } @@ -111,7 +124,8 @@ fn info<'a>(request_handler: &RequestHandler, state: &super::ApplicationState, } else { views::info(path_rest) }; - Response::builder().body(view).unwrap() + Response::builder().set_defaults() + .body(view).unwrap() } fn login<'a>(state: &super::ApplicationState, req: &Request, path_rest: &'a str, @@ -127,15 +141,15 @@ fn login<'a>(state: &super::ApplicationState, req: &Request, path_rest: & } } +// unimplemented fn logout<'a>(state: &super::ApplicationState, req: &Request, path_rest: &'a str, ) -> Response { let header_infos = match parse_header_infos(req) { Ok(infos) => infos, Err(message) => return error_handler_internal(message), }; - - let body = format!("Rest: {}", path_rest); - Response::builder().body(body.to_string()).unwrap() + Response::builder().set_defaults() + .body(format!("Rest: {}", path_rest)).unwrap() } @@ -145,9 +159,12 @@ fn check<'a>(state: &super::ApplicationState, req: &Request, path_rest: & Err(message) => return error_handler_internal(message), }; if is_logged_in(&header_infos.cookies, &state.cookie_store) { - make_response(StatusCode::OK, "".to_string()) + Response::builder().set_defaults() + .body(Default::default()).unwrap() } else { - make_response(StatusCode::UNAUTHORIZED, "Cookie expired".to_string()) + Response::builder().set_defaults() + .status(StatusCode::UNAUTHORIZED) + .body("Cookie expired".to_string()).unwrap() } } -- cgit v1.2.1