mirror of
https://github.com/actix/actix-web.git
synced 2024-11-25 19:11:10 +00:00
export and simplify HttpHandler trait
This commit is contained in:
parent
427566b90d
commit
acc2fff655
7 changed files with 25 additions and 21 deletions
|
@ -45,13 +45,13 @@ impl<S: 'static> Application<S> {
|
|||
|
||||
impl<S: 'static> HttpHandler for Application<S> {
|
||||
|
||||
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<Pipeline, HttpRequest> {
|
||||
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<S> ApplicationBuilder<S> 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,
|
||||
|
|
|
@ -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<Pipeline, HttpRequest>;
|
||||
}
|
||||
|
||||
enum HttpProtocol<T, H>
|
||||
|
|
|
@ -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;
|
||||
|
|
10
src/h1.rs
10
src/h1.rs
|
@ -184,15 +184,17 @@ impl<T, H> Http1<T, H>
|
|||
// 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});
|
||||
|
|
|
@ -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,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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();
|
||||
/// }
|
||||
/// ```
|
||||
|
|
Loading…
Reference in a new issue