1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-26 03:21:08 +00:00

export and simplify HttpHandler trait

This commit is contained in:
Nikolay Kim 2017-11-29 13:53:52 -08:00
parent 427566b90d
commit acc2fff655
7 changed files with 25 additions and 21 deletions

View file

@ -45,13 +45,13 @@ impl<S: 'static> Application<S> {
impl<S: 'static> HttpHandler for Application<S> { impl<S: 'static> HttpHandler for Application<S> {
fn prefix(&self) -> &str { fn handle(&self, req: HttpRequest) -> Result<Pipeline, HttpRequest> {
&self.prefix if req.path().starts_with(&self.prefix) {
} Ok(Pipeline::new(req, Rc::clone(&self.middlewares),
&|req: HttpRequest, task: &mut Task| {self.run(req, task)}))
fn handle(&self, req: HttpRequest) -> Pipeline { } else {
Pipeline::new(req, Rc::clone(&self.middlewares), Err(req)
&|req: HttpRequest, task: &mut Task| {self.run(req, task)}) }
} }
} }
@ -175,7 +175,7 @@ impl<S> ApplicationBuilder<S> where S: 'static {
/// ///
/// fn main() { /// fn main() {
/// let app = Application::default("/") /// let app = Application::default("/")
/// .inline("/test", |req| { /// .handler("/test", |req| {
/// match *req.method() { /// match *req.method() {
/// Method::GET => httpcodes::HTTPOk, /// Method::GET => httpcodes::HTTPOk,
/// Method::POST => httpcodes::HTTPMethodNotAllowed, /// Method::POST => httpcodes::HTTPMethodNotAllowed,

View file

@ -13,10 +13,8 @@ use httprequest::HttpRequest;
/// Low level http request handler /// Low level http request handler
pub trait HttpHandler: 'static { pub trait HttpHandler: 'static {
/// Http handler prefix
fn prefix(&self) -> &str;
/// Handle request /// Handle request
fn handle(&self, req: HttpRequest) -> Pipeline; fn handle(&self, req: HttpRequest) -> Result<Pipeline, HttpRequest>;
} }
enum HttpProtocol<T, H> enum HttpProtocol<T, H>

View file

@ -11,8 +11,9 @@
// dev specific // dev specific
pub use task::Task; pub use task::Task;
pub use pipeline::Pipeline; pub use pipeline::Pipeline;
pub use route::Handler;
pub use recognizer::RouteRecognizer; pub use recognizer::RouteRecognizer;
pub use channel::HttpChannel; pub use channel::{HttpChannel, HttpHandler};
pub use application::ApplicationBuilder; pub use application::ApplicationBuilder;
pub use httpresponse::HttpResponseBuilder; pub use httpresponse::HttpResponseBuilder;

View file

@ -184,15 +184,17 @@ impl<T, H> Http1<T, H>
// start request processing // start request processing
let mut task = None; let mut task = None;
for h in self.router.iter() { for h in self.router.iter() {
if req.path().starts_with(h.prefix()) { req = match h.handle(req) {
task = Some(h.handle(req)); Ok(t) => {
break task = Some(t);
break
},
Err(req) => req,
} }
} }
self.tasks.push_back( self.tasks.push_back(
Entry {task: task.unwrap_or_else(|| Pipeline::error(HTTPNotFound)), Entry {task: task.unwrap_or_else(|| Pipeline::error(HTTPNotFound)),
//req: UnsafeCell::new(req),
eof: false, eof: false,
error: false, error: false,
finished: false}); finished: false});

View file

@ -234,9 +234,12 @@ impl Entry {
// start request processing // start request processing
let mut task = None; let mut task = None;
for h in router.iter() { for h in router.iter() {
if req.path().starts_with(h.prefix()) { req = match h.handle(req) {
task = Some(h.handle(req)); Ok(t) => {
break task = Some(t);
break
},
Err(req) => req,
} }
} }

View file

@ -82,7 +82,7 @@ pub use application::Application;
pub use httprequest::{HttpRequest, UrlEncoded}; pub use httprequest::{HttpRequest, UrlEncoded};
pub use httpresponse::HttpResponse; pub use httpresponse::HttpResponse;
pub use payload::{Payload, PayloadItem}; pub use payload::{Payload, PayloadItem};
pub use route::{Frame, Reply, Handler}; pub use route::{Frame, Reply};
pub use resource::Resource; pub use resource::Resource;
pub use recognizer::Params; pub use recognizer::Params;
pub use server::HttpServer; pub use server::HttpServer;

View file

@ -23,7 +23,7 @@ use httpcodes::{HTTPOk, HTTPNotFound, HTTPForbidden};
/// ///
/// fn main() { /// fn main() {
/// let app = Application::default("/") /// let app = Application::default("/")
/// .handler("/static", StaticFiles::new(".", true)) /// .route("/static", StaticFiles::new(".", true))
/// .finish(); /// .finish();
/// } /// }
/// ``` /// ```