1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-28 01:52:57 +01:00

use server settings for scheme and host values

This commit is contained in:
Nikolay Kim 2017-12-08 09:48:53 -08:00
parent 1293619096
commit 774bfc0a86
4 changed files with 47 additions and 9 deletions

View File

@ -8,6 +8,7 @@ use httprequest::HttpRequest;
use channel::{HttpHandler, IntoHttpHandler}; use channel::{HttpHandler, IntoHttpHandler};
use pipeline::Pipeline; use pipeline::Pipeline;
use middlewares::Middleware; use middlewares::Middleware;
use server::ServerSettings;
/// Application /// Application
pub struct HttpApplication<S> { pub struct HttpApplication<S> {
@ -41,6 +42,10 @@ impl<S: 'static> HttpHandler for HttpApplication<S> {
Err(req) Err(req)
} }
} }
fn server_settings(&mut self, settings: ServerSettings) {
self.router.set_server_settings(settings);
}
} }
struct ApplicationParts<S> { struct ApplicationParts<S> {

View File

@ -64,6 +64,13 @@ impl<'a> ConnectionInfo<'a> {
} }
if scheme.is_none() { if scheme.is_none() {
scheme = req.uri().scheme_part().map(|a| a.as_str()); 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(); host = h.to_str().ok();
} }
if host.is_none() { 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());
}
}
} }
} }
} }

View File

@ -8,6 +8,7 @@ use regex::{Regex, RegexSet};
use error::UrlGenerationError; use error::UrlGenerationError;
use resource::Resource; use resource::Resource;
use httprequest::HttpRequest; use httprequest::HttpRequest;
use server::ServerSettings;
/// Interface for application router. /// Interface for application router.
@ -19,6 +20,7 @@ struct Inner<S> {
named: HashMap<String, (Pattern, bool)>, named: HashMap<String, (Pattern, bool)>,
patterns: Vec<Pattern>, patterns: Vec<Pattern>,
resources: Vec<Resource<S>>, resources: Vec<Resource<S>>,
srv: ServerSettings,
} }
impl<S> Router<S> { impl<S> Router<S> {
@ -49,7 +51,12 @@ impl<S> Router<S> {
regset: RegexSet::new(&paths).unwrap(), regset: RegexSet::new(&paths).unwrap(),
named: named, named: named,
patterns: patterns, 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 /// Router prefix
@ -58,6 +65,12 @@ impl<S> Router<S> {
&self.0.prefix &self.0.prefix
} }
/// Server settings
#[inline]
pub fn server_settings(&self) -> &ServerSettings {
&self.0.srv
}
/// Query for matched resource /// Query for matched resource
pub fn recognize(&self, req: &mut HttpRequest<S>) -> Option<&Resource<S>> { pub fn recognize(&self, req: &mut HttpRequest<S>) -> Option<&Resource<S>> {
let mut idx = None; let mut idx = None;

View File

@ -5,8 +5,6 @@ use std::marker::PhantomData;
use actix::dev::*; use actix::dev::*;
use futures::Stream; use futures::Stream;
use http::HttpTryFrom;
use http::header::HeaderValue;
use tokio_io::{AsyncRead, AsyncWrite}; use tokio_io::{AsyncRead, AsyncWrite};
use tokio_core::net::{TcpListener, TcpStream}; use tokio_core::net::{TcpListener, TcpStream};
@ -33,16 +31,26 @@ use channel::{HttpChannel, HttpHandler, IntoHttpHandler};
pub struct ServerSettings { pub struct ServerSettings {
addr: Option<SocketAddr>, addr: Option<SocketAddr>,
secure: bool, secure: bool,
host: Option<HeaderValue>, host: String,
}
impl Default for ServerSettings {
fn default() -> Self {
ServerSettings {
addr: None,
secure: false,
host: "localhost:8080".to_owned(),
}
}
} }
impl ServerSettings { impl ServerSettings {
/// Crate server settings instance /// Crate server settings instance
fn new(addr: Option<SocketAddr>, secure: bool) -> Self { fn new(addr: Option<SocketAddr>, secure: bool) -> Self {
let host = if let Some(ref addr) = addr { let host = if let Some(ref addr) = addr {
HeaderValue::try_from(format!("{}", addr).as_str()).ok() format!("{}", addr)
} else { } else {
None "unknown".to_owned()
}; };
ServerSettings { ServerSettings {
addr: addr, addr: addr,
@ -62,8 +70,8 @@ impl ServerSettings {
} }
/// Returns host header value /// Returns host header value
pub fn host(&self) -> Option<&HeaderValue> { pub fn host(&self) -> &str {
self.host.as_ref() &self.host
} }
} }