diff --git a/guide/src/qs_10.md b/guide/src/qs_10.md index 8974f241d..8cf8be0d8 100644 --- a/guide/src/qs_10.md +++ b/guide/src/qs_10.md @@ -34,19 +34,19 @@ impl Middleware for Headers { /// Method is called when request is ready. It may return /// future, which should resolve before next middleware get called. - fn start(&self, req: &mut HttpRequest) -> Started { + fn start(&self, req: &mut HttpRequest) -> Result { req.headers_mut().insert( header::CONTENT_TYPE, header::HeaderValue::from_static("text/plain")); - Started::Done + Ok(Started::Done) } /// Method is called when handler returns response, /// but before sending http message to peer. - fn response(&self, req: &mut HttpRequest, mut resp: HttpResponse) -> Response { + fn response(&self, req: &mut HttpRequest, mut resp: HttpResponse) -> Result { resp.headers_mut().insert( header::HeaderName::try_from("X-VERSION").unwrap(), header::HeaderValue::from_static("0.2")); - Response::Done(resp) + Ok(Response::Done(resp)) } } diff --git a/src/middleware/cors.rs b/src/middleware/cors.rs index 42822813c..73e25d3bd 100644 --- a/src/middleware/cors.rs +++ b/src/middleware/cors.rs @@ -5,7 +5,7 @@ use std::collections::HashSet; use http::{self, Method, HttpTryFrom, Uri}; use http::header::{self, HeaderName}; -use error::ResponseError; +use error::{Result, ResponseError}; use httprequest::HttpRequest; use httpresponse::HttpResponse; use middleware::{Middleware, Response, Started}; @@ -152,17 +152,16 @@ impl Cors { impl Middleware for Cors { - fn start(&self, req: &mut HttpRequest) -> Started { + fn start(&self, req: &mut HttpRequest) -> Result { if Method::OPTIONS == *req.method() { - if let Err(err) = self.validate_origin(req) { - return Started::Err(err.into()) - } + self.validate_origin(req)?; + self.validate_allowed_method(req)?; } - Started::Done + Ok(Started::Done) } - fn response(&self, _: &mut HttpRequest, mut resp: HttpResponse) -> Response { - Response::Done(resp) + fn response(&self, _: &mut HttpRequest, mut resp: HttpResponse) -> Result { + Ok(Response::Done(resp)) } } diff --git a/src/middleware/defaultheaders.rs b/src/middleware/defaultheaders.rs index a013b7a57..4e4686ca8 100644 --- a/src/middleware/defaultheaders.rs +++ b/src/middleware/defaultheaders.rs @@ -2,6 +2,7 @@ use http::{HeaderMap, HttpTryFrom}; use http::header::{HeaderName, HeaderValue, CONTENT_TYPE}; +use error::Result; use httprequest::HttpRequest; use httpresponse::HttpResponse; use middleware::{Response, Middleware}; @@ -40,7 +41,7 @@ impl DefaultHeaders { impl Middleware for DefaultHeaders { - fn response(&self, _: &mut HttpRequest, mut resp: HttpResponse) -> Response { + fn response(&self, _: &mut HttpRequest, mut resp: HttpResponse) -> Result { for (key, value) in self.headers.iter() { if !resp.headers().contains_key(key) { resp.headers_mut().insert(key, value.clone()); @@ -51,7 +52,7 @@ impl Middleware for DefaultHeaders { resp.headers_mut().insert( CONTENT_TYPE, HeaderValue::from_static("application/octet-stream")); } - Response::Done(resp) + Ok(Response::Done(resp)) } } @@ -113,14 +114,14 @@ mod tests { let resp = HttpResponse::Ok().finish().unwrap(); let resp = match mw.response(&mut req, resp) { - Response::Done(resp) => resp, + Ok(Response::Done(resp)) => resp, _ => panic!(), }; assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0001"); let resp = HttpResponse::Ok().header(CONTENT_TYPE, "0002").finish().unwrap(); let resp = match mw.response(&mut req, resp) { - Response::Done(resp) => resp, + Ok(Response::Done(resp)) => resp, _ => panic!(), }; assert_eq!(resp.headers().get(CONTENT_TYPE).unwrap(), "0002"); diff --git a/src/middleware/logger.rs b/src/middleware/logger.rs index f7c008c1c..898aa0e02 100644 --- a/src/middleware/logger.rs +++ b/src/middleware/logger.rs @@ -7,6 +7,7 @@ use libc; use time; use regex::Regex; +use error::Result; use httprequest::HttpRequest; use httpresponse::HttpResponse; use middleware::{Middleware, Started, Finished}; @@ -101,9 +102,9 @@ impl Logger { impl Middleware for Logger { - fn start(&self, req: &mut HttpRequest) -> Started { + fn start(&self, req: &mut HttpRequest) -> Result { req.extensions().insert(StartTime(time::now())); - Started::Done + Ok(Started::Done) } fn finish(&self, req: &mut HttpRequest, resp: &HttpResponse) -> Finished { @@ -305,7 +306,7 @@ mod tests { .force_close().body(Body::Empty).unwrap(); match logger.start(&mut req) { - Started::Done => (), + Ok(Started::Done) => (), _ => panic!(), }; match logger.finish(&mut req, &resp) { diff --git a/src/middleware/mod.rs b/src/middleware/mod.rs index 48564889b..4270c477b 100644 --- a/src/middleware/mod.rs +++ b/src/middleware/mod.rs @@ -1,7 +1,7 @@ //! Middlewares use futures::Future; -use error::Error; +use error::{Error, Result}; use httprequest::HttpRequest; use httpresponse::HttpResponse; @@ -18,8 +18,6 @@ pub use self::session::{RequestSession, Session, SessionImpl, SessionBackend, Se pub enum Started { /// Execution completed Done, - /// Moddleware error - Err(Error), /// New http response got generated. If middleware generates response /// handler execution halts. Response(HttpResponse), @@ -29,8 +27,6 @@ pub enum Started { /// Middleware execution result pub enum Response { - /// Moddleware error - Err(Error), /// New http response got generated Done(HttpResponse), /// Result is a future that resolves to a new http response @@ -51,14 +47,14 @@ pub trait Middleware: 'static { /// Method is called when request is ready. It may return /// future, which should resolve before next middleware get called. - fn start(&self, req: &mut HttpRequest) -> Started { - Started::Done + fn start(&self, req: &mut HttpRequest) -> Result { + Ok(Started::Done) } /// Method is called when handler returns response, /// but before sending http message to peer. - fn response(&self, req: &mut HttpRequest, resp: HttpResponse) -> Response { - Response::Done(resp) + fn response(&self, req: &mut HttpRequest, resp: HttpResponse) -> Result { + Ok(Response::Done(resp)) } /// Method is called after body stream get sent to peer. diff --git a/src/middleware/session.rs b/src/middleware/session.rs index e1959b4e6..9a2604dbc 100644 --- a/src/middleware/session.rs +++ b/src/middleware/session.rs @@ -141,7 +141,7 @@ impl> SessionStorage { impl> Middleware for SessionStorage { - fn start(&self, req: &mut HttpRequest) -> Started { + fn start(&self, req: &mut HttpRequest) -> Result { let mut req = req.clone(); let fut = self.0.from_request(&mut req) @@ -154,14 +154,14 @@ impl> Middleware for SessionStorage { Err(err) => FutErr(err) } }); - Started::Future(Box::new(fut)) + Ok(Started::Future(Box::new(fut))) } - fn response(&self, req: &mut HttpRequest, resp: HttpResponse) -> Response { + fn response(&self, req: &mut HttpRequest, resp: HttpResponse) -> Result { if let Some(s_box) = req.extensions().remove::>() { s_box.0.write(resp) } else { - Response::Done(resp) + Ok(Response::Done(resp)) } } } @@ -179,7 +179,7 @@ pub trait SessionImpl: 'static { fn clear(&mut self); /// Write session to storage backend. - fn write(&self, resp: HttpResponse) -> Response; + fn write(&self, resp: HttpResponse) -> Result; } /// Session's storage backend trait definition. @@ -205,8 +205,8 @@ impl SessionImpl for DummySessionImpl { fn set(&mut self, key: &str, value: String) {} fn remove(&mut self, key: &str) {} fn clear(&mut self) {} - fn write(&self, resp: HttpResponse) -> Response { - Response::Done(resp) + fn write(&self, resp: HttpResponse) -> Result { + Ok(Response::Done(resp)) } } @@ -255,11 +255,11 @@ impl SessionImpl for CookieSession { self.state.clear() } - fn write(&self, mut resp: HttpResponse) -> Response { + fn write(&self, mut resp: HttpResponse) -> Result { if self.changed { let _ = self.inner.set_cookie(&mut resp, &self.state); } - Response::Done(resp) + Ok(Response::Done(resp)) } } diff --git a/src/pipeline.rs b/src/pipeline.rs index 9873958b1..975b0e710 100644 --- a/src/pipeline.rs +++ b/src/pipeline.rs @@ -290,11 +290,11 @@ impl> StartMiddlewares { return WaitingResponse::init(info, reply) } else { match info.mws[info.count].start(&mut info.req) { - Started::Done => + Ok(Started::Done) => info.count += 1, - Started::Response(resp) => + Ok(Started::Response(resp)) => return RunMiddlewares::init(info, resp), - Started::Future(mut fut) => + Ok(Started::Future(mut fut)) => match fut.poll() { Ok(Async::NotReady) => return PipelineState::Starting(StartMiddlewares { @@ -310,7 +310,7 @@ impl> StartMiddlewares { Err(err) => return ProcessResponse::init(err.into()), }, - Started::Err(err) => + Err(err) => return ProcessResponse::init(err.into()), } } @@ -335,16 +335,16 @@ impl> StartMiddlewares { } else { loop { match info.mws[info.count].start(info.req_mut()) { - Started::Done => + Ok(Started::Done) => info.count += 1, - Started::Response(resp) => { + Ok(Started::Response(resp)) => { return Ok(RunMiddlewares::init(info, resp)); }, - Started::Future(fut) => { + Ok(Started::Future(fut)) => { self.fut = Some(fut); continue 'outer }, - Started::Err(err) => + Err(err) => return Ok(ProcessResponse::init(err.into())) } } @@ -411,11 +411,11 @@ impl RunMiddlewares { loop { resp = match info.mws[curr].response(info.req_mut(), resp) { - Response::Err(err) => { + Err(err) => { info.count = curr + 1; return ProcessResponse::init(err.into()) } - Response::Done(r) => { + Ok(Response::Done(r)) => { curr += 1; if curr == len { return ProcessResponse::init(r) @@ -423,7 +423,7 @@ impl RunMiddlewares { r } }, - Response::Future(fut) => { + Ok(Response::Future(fut)) => { return PipelineState::RunMiddlewares( RunMiddlewares { curr: curr, fut: Some(fut), _s: PhantomData, _h: PhantomData }) @@ -455,13 +455,13 @@ impl RunMiddlewares { return Ok(ProcessResponse::init(resp)); } else { match info.mws[self.curr].response(info.req_mut(), resp) { - Response::Err(err) => + Err(err) => return Ok(ProcessResponse::init(err.into())), - Response::Done(r) => { + Ok(Response::Done(r)) => { self.curr += 1; resp = r }, - Response::Future(fut) => { + Ok(Response::Future(fut)) => { self.fut = Some(fut); break }, diff --git a/src/route.rs b/src/route.rs index acef0fd44..2779bd693 100644 --- a/src/route.rs +++ b/src/route.rs @@ -227,11 +227,11 @@ impl StartMiddlewares { return WaitingResponse::init(info, reply) } else { match info.mws[info.count].start(&mut info.req) { - MiddlewareStarted::Done => + Ok(MiddlewareStarted::Done) => info.count += 1, - MiddlewareStarted::Response(resp) => + Ok(MiddlewareStarted::Response(resp)) => return RunMiddlewares::init(info, resp), - MiddlewareStarted::Future(mut fut) => + Ok(MiddlewareStarted::Future(mut fut)) => match fut.poll() { Ok(Async::NotReady) => return ComposeState::Starting(StartMiddlewares { @@ -246,7 +246,7 @@ impl StartMiddlewares { Err(err) => return Response::init(err.into()), }, - MiddlewareStarted::Err(err) => + Err(err) => return Response::init(err.into()), } } @@ -271,16 +271,16 @@ impl StartMiddlewares { } else { loop { match info.mws[info.count].start(&mut info.req) { - MiddlewareStarted::Done => + Ok(MiddlewareStarted::Done) => info.count += 1, - MiddlewareStarted::Response(resp) => { + Ok(MiddlewareStarted::Response(resp)) => { return Some(RunMiddlewares::init(info, resp)); }, - MiddlewareStarted::Future(fut) => { + Ok(MiddlewareStarted::Future(fut)) => { self.fut = Some(fut); continue 'outer }, - MiddlewareStarted::Err(err) => + Err(err) => return Some(Response::init(err.into())) } } @@ -339,11 +339,11 @@ impl RunMiddlewares { loop { resp = match info.mws[curr].response(&mut info.req, resp) { - MiddlewareResponse::Err(err) => { + Err(err) => { info.count = curr + 1; return Response::init(err.into()) }, - MiddlewareResponse::Done(r) => { + Ok(MiddlewareResponse::Done(r)) => { curr += 1; if curr == len { return Response::init(r) @@ -351,7 +351,7 @@ impl RunMiddlewares { r } }, - MiddlewareResponse::Future(fut) => { + Ok(MiddlewareResponse::Future(fut)) => { return ComposeState::RunMiddlewares( RunMiddlewares { curr: curr, fut: Some(fut), _s: PhantomData }) }, @@ -382,13 +382,13 @@ impl RunMiddlewares { return Some(Response::init(resp)); } else { match info.mws[self.curr].response(&mut info.req, resp) { - MiddlewareResponse::Err(err) => + Err(err) => return Some(Response::init(err.into())), - MiddlewareResponse::Done(r) => { + Ok(MiddlewareResponse::Done(r)) => { self.curr += 1; resp = r }, - MiddlewareResponse::Future(fut) => { + Ok(MiddlewareResponse::Future(fut)) => { self.fut = Some(fut); break }, diff --git a/tests/test_server.rs b/tests/test_server.rs index 1399879ba..ab78527e5 100644 --- a/tests/test_server.rs +++ b/tests/test_server.rs @@ -97,14 +97,14 @@ struct MiddlewareTest { } impl middleware::Middleware for MiddlewareTest { - fn start(&self, _: &mut HttpRequest) -> middleware::Started { + fn start(&self, _: &mut HttpRequest) -> Result { self.start.store(self.start.load(Ordering::Relaxed) + 1, Ordering::Relaxed); - middleware::Started::Done + Ok(middleware::Started::Done) } - fn response(&self, _: &mut HttpRequest, resp: HttpResponse) -> middleware::Response { + fn response(&self, _: &mut HttpRequest, resp: HttpResponse) -> Result { self.response.store(self.response.load(Ordering::Relaxed) + 1, Ordering::Relaxed); - middleware::Response::Done(resp) + Ok(middleware::Response::Done(resp)) } fn finish(&self, _: &mut HttpRequest, _: &HttpResponse) -> middleware::Finished {