diff --git a/src/application.rs b/src/application.rs index 0c2b376c3..899cd453b 100644 --- a/src/application.rs +++ b/src/application.rs @@ -8,6 +8,7 @@ use httprequest::HttpRequest; use channel::{HttpHandler, IntoHttpHandler}; use pipeline::Pipeline; use middlewares::Middleware; +use server::ServerSettings; /// Application pub struct HttpApplication { @@ -41,6 +42,10 @@ impl HttpHandler for HttpApplication { Err(req) } } + + fn server_settings(&mut self, settings: ServerSettings) { + self.router.set_server_settings(settings); + } } struct ApplicationParts { diff --git a/src/info.rs b/src/info.rs index a02ffc6ad..a5a01c539 100644 --- a/src/info.rs +++ b/src/info.rs @@ -64,6 +64,13 @@ impl<'a> ConnectionInfo<'a> { } if scheme.is_none() { scheme = req.uri().scheme_part().map(|a| a.as_str()); + if scheme.is_none() { + if let Some(ref router) = req.router() { + if router.server_settings().secure() { + scheme = Some("https") + } + } + } } } @@ -79,7 +86,12 @@ impl<'a> ConnectionInfo<'a> { host = h.to_str().ok(); } if host.is_none() { - host = req.uri().authority_part().map(|a| a.as_str()) + host = req.uri().authority_part().map(|a| a.as_str()); + if host.is_none() { + if let Some(ref router) = req.router() { + host = Some(router.server_settings().host()); + } + } } } } diff --git a/src/router.rs b/src/router.rs index 3d03e11a0..b6e1313dc 100644 --- a/src/router.rs +++ b/src/router.rs @@ -8,6 +8,7 @@ use regex::{Regex, RegexSet}; use error::UrlGenerationError; use resource::Resource; use httprequest::HttpRequest; +use server::ServerSettings; /// Interface for application router. @@ -19,6 +20,7 @@ struct Inner { named: HashMap, patterns: Vec, resources: Vec>, + srv: ServerSettings, } impl Router { @@ -49,7 +51,12 @@ impl Router { regset: RegexSet::new(&paths).unwrap(), named: named, patterns: patterns, - resources: resources })) + resources: resources, + srv: ServerSettings::default() })) + } + + pub(crate) fn set_server_settings(&mut self, settings: ServerSettings) { + Rc::get_mut(&mut self.0).unwrap().srv = settings; } /// Router prefix @@ -58,6 +65,12 @@ impl Router { &self.0.prefix } + /// Server settings + #[inline] + pub fn server_settings(&self) -> &ServerSettings { + &self.0.srv + } + /// Query for matched resource pub fn recognize(&self, req: &mut HttpRequest) -> Option<&Resource> { let mut idx = None; diff --git a/src/server.rs b/src/server.rs index 8b8656182..98e602428 100644 --- a/src/server.rs +++ b/src/server.rs @@ -5,8 +5,6 @@ use std::marker::PhantomData; use actix::dev::*; use futures::Stream; -use http::HttpTryFrom; -use http::header::HeaderValue; use tokio_io::{AsyncRead, AsyncWrite}; use tokio_core::net::{TcpListener, TcpStream}; @@ -33,16 +31,26 @@ use channel::{HttpChannel, HttpHandler, IntoHttpHandler}; pub struct ServerSettings { addr: Option, secure: bool, - host: Option, + host: String, +} + +impl Default for ServerSettings { + fn default() -> Self { + ServerSettings { + addr: None, + secure: false, + host: "localhost:8080".to_owned(), + } + } } impl ServerSettings { /// Crate server settings instance fn new(addr: Option, secure: bool) -> Self { let host = if let Some(ref addr) = addr { - HeaderValue::try_from(format!("{}", addr).as_str()).ok() + format!("{}", addr) } else { - None + "unknown".to_owned() }; ServerSettings { addr: addr, @@ -62,8 +70,8 @@ impl ServerSettings { } /// Returns host header value - pub fn host(&self) -> Option<&HeaderValue> { - self.host.as_ref() + pub fn host(&self) -> &str { + &self.host } }