diff --git a/src/favicon_handler.rs b/src/favicon_handler.rs index aa66c4631b..5d3d191eb8 100644 --- a/src/favicon_handler.rs +++ b/src/favicon_handler.rs @@ -19,7 +19,7 @@ pub struct FaviconHandler { } impl Middleware for FaviconHandler { - fn invoke<'a, 'b>(&'a self, req: &mut Request<'a, 'a, 'b>, res: Response<'a, net::Fresh>) + fn invoke<'a, 'server>(&'a self, req: &mut Request<'a, 'server>, res: Response<'a, net::Fresh>) -> MiddlewareResult<'a> { if FaviconHandler::is_favicon_request(req) { self.handle_request(req, res) @@ -78,7 +78,7 @@ impl FaviconHandler { } } - pub fn send_favicon<'a, 'b>(&self, req: &Request, mut res: Response<'a>) -> MiddlewareResult<'a> { + pub fn send_favicon<'a, 'server>(&self, req: &Request, mut res: Response<'a>) -> MiddlewareResult<'a> { debug!("{:?} {:?}", req.origin.method, self.icon_path.display()); res.set(MediaType::Ico); res.send(&*self.icon) diff --git a/src/json_body_parser.rs b/src/json_body_parser.rs index b01527a0d5..3f8b0c179e 100644 --- a/src/json_body_parser.rs +++ b/src/json_body_parser.rs @@ -8,7 +8,7 @@ use std::io::{Read, ErrorKind}; // Plugin boilerplate struct JsonBodyParser; impl Key for JsonBodyParser { type Value = String; } -impl<'a, 'b, 'k> Plugin> for JsonBodyParser { +impl<'mw, 'conn> Plugin> for JsonBodyParser { type Error = io::Error; fn eval(req: &mut Request) -> Result { @@ -22,7 +22,7 @@ pub trait JsonBody { fn json_as(&mut self) -> Result; } -impl<'a, 'b, 'k> JsonBody for Request<'a, 'b, 'k> { +impl<'mw, 'conn> JsonBody for Request<'mw, 'conn> { fn json_as(&mut self) -> Result { self.get_ref::().and_then(|parsed| json::decode::(&*parsed).map_err(|err| diff --git a/src/macros/middleware.rs b/src/macros/middleware.rs index 7fdd413664..c13b16cc40 100644 --- a/src/macros/middleware.rs +++ b/src/macros/middleware.rs @@ -38,8 +38,8 @@ macro_rules! _middleware_inner { use $crate::{MiddlewareResult,Responder, Response, Request}; #[inline(always)] - fn restrict<'a, R: Responder>(r: R, res: Response<'a>) - -> MiddlewareResult<'a> { + fn restrict<'mw, R: Responder>(r: R, res: Response<'mw>) + -> MiddlewareResult<'mw> { res.send(r) } @@ -47,9 +47,9 @@ macro_rules! _middleware_inner { // different mutability requirements #[inline(always)] fn restrict_closure(f: F) -> F - where F: for<'r, 'b, 'a> - Fn(&'r mut Request<'a, 'a, 'b>, Response<'a>) - -> MiddlewareResult<'a> + Send + Sync { f } + where F: for<'r, 'mw, 'conn> + Fn(&'r mut Request<'mw, 'conn>, Response<'mw>) + -> MiddlewareResult<'mw> + Send + Sync { f } restrict_closure(move |as_pat!($req), $res_binding| { restrict(as_block!({$($b)+}), $res) diff --git a/src/middleware.rs b/src/middleware.rs index 125b4c3df3..6f86bacdda 100644 --- a/src/middleware.rs +++ b/src/middleware.rs @@ -5,9 +5,9 @@ use hyper::net; pub use self::Action::{Continue, Halt}; -pub type MiddlewareResult<'a> = Result, - Response<'a, net::Streaming>>, - NickelError<'a>>; +pub type MiddlewareResult<'mw> = Result, + Response<'mw, net::Streaming>>, + NickelError<'mw>>; pub enum Action { Continue(T), @@ -17,13 +17,13 @@ pub enum Action { // the usage of + Send is weird here because what we really want is + Static // but that's not possible as of today. We have to use + Send for now. pub trait Middleware: Send + 'static + Sync { - fn invoke<'a, 'b>(&'a self, _req: &mut Request<'a, 'a, 'b>, res: Response<'a, net::Fresh>) -> MiddlewareResult<'a> { + fn invoke<'mw, 'conn>(&'mw self, _req: &mut Request<'mw, 'conn>, res: Response<'mw, net::Fresh>) -> MiddlewareResult<'mw> { Ok(Continue(res)) } } -impl Middleware for T where T: for<'r, 'b, 'a> Fn(&'r mut Request<'a, 'a, 'b>, Response<'a>) -> MiddlewareResult<'a> + Send + Sync + 'static { - fn invoke<'a, 'b>(&'a self, req: &mut Request<'a, 'a, 'b>, res: Response<'a>) -> MiddlewareResult<'a> { +impl Middleware for T where T: for<'r, 'mw, 'conn> Fn(&'r mut Request<'mw, 'conn>, Response<'mw>) -> MiddlewareResult<'mw> + Send + Sync + 'static { + fn invoke<'mw, 'conn>(&'mw self, req: &mut Request<'mw, 'conn>, res: Response<'mw>) -> MiddlewareResult<'mw> { (*self)(req, res) } } @@ -52,7 +52,7 @@ impl MiddlewareStack { self.error_handlers.push(Box::new(handler)); } - pub fn invoke<'a, 'b>(&'a self, mut req: Request<'a, 'a, 'b>, mut res: Response<'a>) { + pub fn invoke<'mw, 'conn>(&'mw self, mut req: Request<'mw, 'conn>, mut res: Response<'mw>) { for handler in self.handlers.iter() { match handler.invoke(&mut req, res) { Ok(Halt(res)) => { diff --git a/src/query_string.rs b/src/query_string.rs index 50068f15bc..19eab05761 100644 --- a/src/query_string.rs +++ b/src/query_string.rs @@ -33,7 +33,7 @@ impl Query { struct QueryStringParser; impl Key for QueryStringParser { type Value = Query; } -impl<'a, 'b, 'k> Plugin> for QueryStringParser { +impl<'mw, 'conn> Plugin> for QueryStringParser { type Error = (); fn eval(req: &mut Request) -> Result { @@ -46,7 +46,7 @@ pub trait QueryString { fn query(&mut self) -> &Query; } -impl<'a, 'b, 'k> QueryString for Request<'a, 'b, 'k> { +impl<'mw, 'conn> QueryString for Request<'mw, 'conn> { fn query(&mut self) -> &Query { self.get_ref::() .ok() diff --git a/src/request.rs b/src/request.rs index 72c280f339..2de6dda19b 100644 --- a/src/request.rs +++ b/src/request.rs @@ -4,18 +4,24 @@ use typemap::TypeMap; use hyper::server::Request as HyperRequest; use hyper::uri::RequestUri::AbsolutePath; -///A container for all the request data -pub struct Request<'a, 'b, 'k: 'a> { +/// A container for all the request data. +/// +/// The lifetime `'mw` represents the lifetime of various bits of +/// middleware state within nickel. It can vary and get shorter. +/// +/// The lifetime `'server` represents the lifetime of data internal to +/// the server. It is fixed and longer than `'mw`. +pub struct Request<'mw, 'server: 'mw> { ///the original `hyper::server::Request` - pub origin: HyperRequest<'a, 'k>, + pub origin: HyperRequest<'mw, 'server>, ///a `HashMap` holding all params with names and values - pub route_result: Option>, + pub route_result: Option>, map: TypeMap } -impl<'a, 'b, 'k> Request<'a, 'b, 'k> { - pub fn from_internal(req: HyperRequest<'a, 'k>) -> Request<'a, 'b, 'k> { +impl<'mw, 'server> Request<'mw, 'server> { + pub fn from_internal(req: HyperRequest<'mw, 'server>) -> Request<'mw, 'server> { Request { origin: req, route_result: None, @@ -35,7 +41,7 @@ impl<'a, 'b, 'k> Request<'a, 'b, 'k> { } } -impl<'a, 'b, 'k> Extensible for Request<'a, 'b, 'k> { +impl<'mw, 'server> Extensible for Request<'mw, 'server> { fn extensions(&self) -> &TypeMap { &self.map } @@ -45,4 +51,4 @@ impl<'a, 'b, 'k> Extensible for Request<'a, 'b, 'k> { } } -impl<'a, 'b, 'k> Pluggable for Request<'a, 'b, 'k> {} +impl<'mw, 'server> Pluggable for Request<'mw, 'server> {} diff --git a/src/router/router.rs b/src/router/router.rs index 488369ede2..ea9500c91d 100644 --- a/src/router/router.rs +++ b/src/router/router.rs @@ -20,12 +20,12 @@ pub struct Route { /// It contains the matched `route` and also a `params` property holding /// a HashMap with the keys being the variable names and the value being the /// evaluated string -pub struct RouteResult<'a> { - pub route: &'a Route, +pub struct RouteResult<'mw> { + pub route: &'mw Route, params: Vec<(String, String)> } -impl<'a> RouteResult<'a> { +impl<'mw> RouteResult<'mw> { pub fn param(&self, key: &str) -> Option<&str> { for &(ref k, ref v) in &self.params { if k == &key { @@ -56,7 +56,7 @@ impl Router { } } - pub fn match_route<'a>(&'a self, method: &Method, path: &str) -> Option> { + pub fn match_route<'mw>(&'mw self, method: &Method, path: &str) -> Option> { self.routes .iter() .find(|item| item.method == *method && item.matcher.is_match(path)) @@ -95,8 +95,8 @@ impl HttpRouter for Router { } impl Middleware for Router { - fn invoke<'a, 'b>(&'a self, req: &mut Request<'a, 'a, 'b>, mut res: Response<'a>) - -> MiddlewareResult<'a> { + fn invoke<'mw, 'conn>(&'mw self, req: &mut Request<'mw, 'conn>, mut res: Response<'mw>) + -> MiddlewareResult<'mw> { debug!("Router::invoke for '{:?}'", req.origin.uri); // Strip off the querystring when matching a route