diff --git a/src/application.rs b/src/application.rs index 925dbb014..04c150520 100644 --- a/src/application.rs +++ b/src/application.rs @@ -37,12 +37,11 @@ impl PipelineHandler for Inner { } } +#[cfg(test)] impl HttpApplication { - #[cfg(test)] pub(crate) fn run(&mut self, req: HttpRequest) -> Reply { self.inner.borrow_mut().handle(req) } - #[cfg(test)] pub(crate) fn prepare_request(&self, req: HttpRequest) -> HttpRequest { req.with_state(Rc::clone(&self.state), self.router.clone()) } @@ -60,15 +59,12 @@ impl HttpHandler for HttpApplication { Err(req) } } - - fn server_settings(&mut self, settings: ServerSettings) { - self.router.set_server_settings(settings); - } } struct ApplicationParts { state: S, prefix: String, + settings: ServerSettings, default: Resource, resources: HashMap>>, external: HashMap, @@ -89,6 +85,7 @@ impl Application<()> { parts: Some(ApplicationParts { state: (), prefix: "/".to_owned(), + settings: ServerSettings::default(), default: Resource::default_not_found(), resources: HashMap::new(), external: HashMap::new(), @@ -116,6 +113,7 @@ impl Application where S: 'static { parts: Some(ApplicationParts { state: state, prefix: "/".to_owned(), + settings: ServerSettings::default(), default: Resource::default_not_found(), resources: HashMap::new(), external: HashMap::new(), @@ -279,7 +277,7 @@ impl Application where S: 'static { resources.insert(pattern, None); } - let (router, resources) = Router::new(prefix, resources); + let (router, resources) = Router::new(prefix, parts.settings, resources); let inner = Rc::new(RefCell::new( Inner { @@ -301,7 +299,11 @@ impl Application where S: 'static { impl IntoHttpHandler for Application { type Handler = HttpApplication; - fn into_handler(mut self) -> HttpApplication { + fn into_handler(mut self, settings: ServerSettings) -> HttpApplication { + { + let parts = self.parts.as_mut().expect("Use after finish"); + parts.settings = settings; + } self.finish() } } @@ -309,7 +311,11 @@ impl IntoHttpHandler for Application { impl<'a, S: 'static> IntoHttpHandler for &'a mut Application { type Handler = HttpApplication; - fn into_handler(self) -> HttpApplication { + fn into_handler(self, settings: ServerSettings) -> HttpApplication { + { + let parts = self.parts.as_mut().expect("Use after finish"); + parts.settings = settings; + } self.finish() } } diff --git a/src/channel.rs b/src/channel.rs index 01c18a527..37bbe771b 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -19,9 +19,6 @@ pub trait HttpHandler: 'static { /// Handle request fn handle(&mut self, req: HttpRequest) -> Result, HttpRequest>; - - /// Set server settings - fn server_settings(&mut self, settings: ServerSettings) {} } pub trait HttpHandlerTask { @@ -39,13 +36,13 @@ pub trait IntoHttpHandler { type Handler: HttpHandler; /// Convert into `HttpHandler` object. - fn into_handler(self) -> Self::Handler; + fn into_handler(self, settings: ServerSettings) -> Self::Handler; } impl IntoHttpHandler for T { type Handler = T; - fn into_handler(self) -> Self::Handler { + fn into_handler(self, _: ServerSettings) -> Self::Handler { self } } diff --git a/src/router.rs b/src/router.rs index e5fc091fb..2300dce55 100644 --- a/src/router.rs +++ b/src/router.rs @@ -24,8 +24,9 @@ struct Inner { impl Router { /// Create new router - pub fn new(prefix: &str, map: HashMap>>) - -> (Router, Vec>) + pub fn new(prefix: &str, + settings: ServerSettings, + map: HashMap>>) -> (Router, Vec>) { let prefix = prefix.trim().trim_right_matches('/').to_owned(); let mut named = HashMap::new(); @@ -51,14 +52,7 @@ impl Router { regset: RegexSet::new(&paths).unwrap(), named: named, patterns: patterns, - srv: ServerSettings::default() })), resources) - } - - #[allow(mutable_transmutes)] - pub(crate) fn set_server_settings(&mut self, settings: ServerSettings) { - let inner: &Inner = self.0.as_ref(); - let inner: &mut Inner = unsafe{mem::transmute(inner)}; - inner.srv = settings; + srv: settings })), resources) } /// Router prefix diff --git a/src/server.rs b/src/server.rs index b5c2e8184..7394585ac 100644 --- a/src/server.rs +++ b/src/server.rs @@ -268,11 +268,9 @@ impl HttpServer let ka = self.keep_alive; let factory = Arc::clone(&self.factory); let addr = Arbiter::start(move |ctx: &mut Context<_>| { - let mut apps: Vec<_> = (*factory)() - .into_iter().map(|h| h.into_handler()).collect(); - for app in &mut apps { - app.server_settings(s.clone()); - } + let apps: Vec<_> = (*factory)() + .into_iter() + .map(|h| h.into_handler(s.clone())).collect(); ctx.add_stream(rx); Worker::new(apps, h, ka) }); @@ -482,10 +480,9 @@ impl HttpServer // set server settings let addr: net::SocketAddr = "127.0.0.1:8080".parse().unwrap(); let settings = ServerSettings::new(Some(addr), &self.host, secure); - let mut apps: Vec<_> = (*self.factory)().into_iter().map(|h| h.into_handler()).collect(); - for app in &mut apps { - app.server_settings(settings.clone()); - } + let apps: Vec<_> = (*self.factory)() + .into_iter() + .map(|h| h.into_handler(settings.clone())).collect(); self.h = Some(Rc::new(WorkerSettings::new(apps, self.keep_alive))); // start server diff --git a/src/test.rs b/src/test.rs index 11c03f35e..88c2044f5 100644 --- a/src/test.rs +++ b/src/test.rs @@ -16,7 +16,7 @@ use tokio_core::reactor::Core; use net2::TcpBuilder; use error::Error; -use server::HttpServer; +use server::{HttpServer, ServerSettings}; use handler::{Handler, Responder, ReplyItem}; use channel::{HttpHandler, IntoHttpHandler}; use middleware::Middleware; @@ -199,8 +199,8 @@ impl TestApp { impl IntoHttpHandler for TestApp { type Handler = HttpApplication; - fn into_handler(self) -> HttpApplication { - self.app.unwrap().finish() + fn into_handler(mut self, settings: ServerSettings) -> HttpApplication { + self.app.take().unwrap().into_handler(settings) } } @@ -347,7 +347,7 @@ impl TestRequest { let req = HttpRequest::new(method, uri, version, headers, payload); req.as_mut().cookies = cookies; req.as_mut().params = params; - let (router, _) = Router::new::("/", HashMap::new()); + let (router, _) = Router::new::("/", ServerSettings::default(), HashMap::new()); req.with_state(Rc::new(state), router) }