From acc2fff6558466f01df9c67ba463b54d37d7c534 Mon Sep 17 00:00:00 2001 From: Nikolay Kim Date: Wed, 29 Nov 2017 13:53:52 -0800 Subject: [PATCH] export and simplify HttpHandler trait --- src/application.rs | 16 ++++++++-------- src/channel.rs | 4 +--- src/dev.rs | 3 ++- src/h1.rs | 10 ++++++---- src/h2.rs | 9 ++++++--- src/lib.rs | 2 +- src/staticfiles.rs | 2 +- 7 files changed, 25 insertions(+), 21 deletions(-) diff --git a/src/application.rs b/src/application.rs index 5f5ead242..53236b0ce 100644 --- a/src/application.rs +++ b/src/application.rs @@ -45,13 +45,13 @@ impl Application { impl HttpHandler for Application { - fn prefix(&self) -> &str { - &self.prefix - } - - fn handle(&self, req: HttpRequest) -> Pipeline { - Pipeline::new(req, Rc::clone(&self.middlewares), - &|req: HttpRequest, task: &mut Task| {self.run(req, task)}) + fn handle(&self, req: HttpRequest) -> Result { + if req.path().starts_with(&self.prefix) { + Ok(Pipeline::new(req, Rc::clone(&self.middlewares), + &|req: HttpRequest, task: &mut Task| {self.run(req, task)})) + } else { + Err(req) + } } } @@ -175,7 +175,7 @@ impl ApplicationBuilder where S: 'static { /// /// fn main() { /// let app = Application::default("/") - /// .inline("/test", |req| { + /// .handler("/test", |req| { /// match *req.method() { /// Method::GET => httpcodes::HTTPOk, /// Method::POST => httpcodes::HTTPMethodNotAllowed, diff --git a/src/channel.rs b/src/channel.rs index 36cd7bdc3..a15591c9f 100644 --- a/src/channel.rs +++ b/src/channel.rs @@ -13,10 +13,8 @@ use httprequest::HttpRequest; /// Low level http request handler pub trait HttpHandler: 'static { - /// Http handler prefix - fn prefix(&self) -> &str; /// Handle request - fn handle(&self, req: HttpRequest) -> Pipeline; + fn handle(&self, req: HttpRequest) -> Result; } enum HttpProtocol diff --git a/src/dev.rs b/src/dev.rs index 8ec4a50d1..589ecb13c 100644 --- a/src/dev.rs +++ b/src/dev.rs @@ -11,8 +11,9 @@ // dev specific pub use task::Task; pub use pipeline::Pipeline; +pub use route::Handler; pub use recognizer::RouteRecognizer; -pub use channel::HttpChannel; +pub use channel::{HttpChannel, HttpHandler}; pub use application::ApplicationBuilder; pub use httpresponse::HttpResponseBuilder; diff --git a/src/h1.rs b/src/h1.rs index 335660d95..96293aec6 100644 --- a/src/h1.rs +++ b/src/h1.rs @@ -184,15 +184,17 @@ impl Http1 // start request processing let mut task = None; for h in self.router.iter() { - if req.path().starts_with(h.prefix()) { - task = Some(h.handle(req)); - break + req = match h.handle(req) { + Ok(t) => { + task = Some(t); + break + }, + Err(req) => req, } } self.tasks.push_back( Entry {task: task.unwrap_or_else(|| Pipeline::error(HTTPNotFound)), - //req: UnsafeCell::new(req), eof: false, error: false, finished: false}); diff --git a/src/h2.rs b/src/h2.rs index d3f32e2b9..28deda672 100644 --- a/src/h2.rs +++ b/src/h2.rs @@ -234,9 +234,12 @@ impl Entry { // start request processing let mut task = None; for h in router.iter() { - if req.path().starts_with(h.prefix()) { - task = Some(h.handle(req)); - break + req = match h.handle(req) { + Ok(t) => { + task = Some(t); + break + }, + Err(req) => req, } } diff --git a/src/lib.rs b/src/lib.rs index 2142e5474..c94e67897 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -82,7 +82,7 @@ pub use application::Application; pub use httprequest::{HttpRequest, UrlEncoded}; pub use httpresponse::HttpResponse; pub use payload::{Payload, PayloadItem}; -pub use route::{Frame, Reply, Handler}; +pub use route::{Frame, Reply}; pub use resource::Resource; pub use recognizer::Params; pub use server::HttpServer; diff --git a/src/staticfiles.rs b/src/staticfiles.rs index b4ab7214d..04089b1d9 100644 --- a/src/staticfiles.rs +++ b/src/staticfiles.rs @@ -23,7 +23,7 @@ use httpcodes::{HTTPOk, HTTPNotFound, HTTPForbidden}; /// /// fn main() { /// let app = Application::default("/") -/// .handler("/static", StaticFiles::new(".", true)) +/// .route("/static", StaticFiles::new(".", true)) /// .finish(); /// } /// ```