mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-23 23:51:06 +01:00
use server settings for scheme and host values
This commit is contained in:
parent
1293619096
commit
774bfc0a86
@ -8,6 +8,7 @@ use httprequest::HttpRequest;
|
||||
use channel::{HttpHandler, IntoHttpHandler};
|
||||
use pipeline::Pipeline;
|
||||
use middlewares::Middleware;
|
||||
use server::ServerSettings;
|
||||
|
||||
/// Application
|
||||
pub struct HttpApplication<S> {
|
||||
@ -41,6 +42,10 @@ impl<S: 'static> HttpHandler for HttpApplication<S> {
|
||||
Err(req)
|
||||
}
|
||||
}
|
||||
|
||||
fn server_settings(&mut self, settings: ServerSettings) {
|
||||
self.router.set_server_settings(settings);
|
||||
}
|
||||
}
|
||||
|
||||
struct ApplicationParts<S> {
|
||||
|
14
src/info.rs
14
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());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -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<S> {
|
||||
named: HashMap<String, (Pattern, bool)>,
|
||||
patterns: Vec<Pattern>,
|
||||
resources: Vec<Resource<S>>,
|
||||
srv: ServerSettings,
|
||||
}
|
||||
|
||||
impl<S> Router<S> {
|
||||
@ -49,7 +51,12 @@ impl<S> Router<S> {
|
||||
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<S> Router<S> {
|
||||
&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<S>) -> Option<&Resource<S>> {
|
||||
let mut idx = None;
|
||||
|
@ -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<SocketAddr>,
|
||||
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 {
|
||||
/// Crate server settings instance
|
||||
fn new(addr: Option<SocketAddr>, 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
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user