1
0
mirror of https://github.com/actix/actix-extras.git synced 2024-11-28 09:42:40 +01:00

Propagate default resources to underlying scopes

This commit is contained in:
Josh Leeb-du Toit 2018-06-21 18:17:27 +10:00
parent f815c1c096
commit c5e8c1b710
3 changed files with 35 additions and 8 deletions

View File

@ -29,7 +29,7 @@ pub struct HttpApplication<S = ()> {
#[doc(hidden)]
pub struct Inner<S> {
prefix: usize,
default: ResourceHandler<S>,
default: Rc<RefCell<ResourceHandler<S>>>,
encoding: ContentEncoding,
resources: Vec<ResourceHandler<S>>,
handlers: Vec<PrefixHandlerType<S>>,
@ -51,7 +51,7 @@ impl<S: 'static> PipelineHandler<S> for Inner<S> {
match htype {
HandlerType::Normal(idx) => match self.resources[idx].handle(req) {
Ok(result) => result,
Err(req) => match self.default.handle(req) {
Err(req) => match self.default.borrow_mut().handle(req) {
Ok(result) => result,
Err(_) => AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND)),
},
@ -60,7 +60,7 @@ impl<S: 'static> PipelineHandler<S> for Inner<S> {
PrefixHandlerType::Handler(_, ref mut hnd) => hnd.handle(req),
PrefixHandlerType::Scope(_, ref mut hnd, _) => hnd.handle(req),
},
HandlerType::Default => match self.default.handle(req) {
HandlerType::Default => match self.default.borrow_mut().handle(req) {
Ok(result) => result,
Err(_) => AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND)),
},
@ -172,7 +172,7 @@ struct ApplicationParts<S> {
state: S,
prefix: String,
settings: ServerSettings,
default: ResourceHandler<S>,
default: Rc<RefCell<ResourceHandler<S>>>,
resources: Vec<(Resource, Option<ResourceHandler<S>>)>,
handlers: Vec<PrefixHandlerType<S>>,
external: HashMap<String, Resource>,
@ -223,7 +223,7 @@ where
state,
prefix: "/".to_owned(),
settings: ServerSettings::default(),
default: ResourceHandler::default_not_found(),
default: Rc::new(RefCell::new(ResourceHandler::default_not_found())),
resources: Vec::new(),
handlers: Vec::new(),
external: HashMap::new(),
@ -473,7 +473,7 @@ where
{
{
let parts = self.parts.as_mut().expect("Use after finish");
f(&mut parts.default);
f(&mut parts.default.borrow_mut());
}
self
}
@ -614,7 +614,7 @@ where
/// Finish application configuration and create `HttpHandler` object.
pub fn finish(&mut self) -> HttpApplication<S> {
let parts = self.parts.take().expect("Use after finish");
let mut parts = self.parts.take().expect("Use after finish");
let prefix = parts.prefix.trim().trim_right_matches('/');
let (prefix, prefix_len) = if prefix.is_empty() {
("/".to_owned(), 0)
@ -627,11 +627,19 @@ where
resources.push((pattern, None));
}
for ref mut handler in parts.handlers.iter_mut() {
if let PrefixHandlerType::Scope(_, ref mut route_handler, _) = handler {
if !route_handler.has_default_resource() {
route_handler.default_resource(Rc::clone(&parts.default));
}
};
}
let (router, resources) = Router::new(&prefix, parts.settings, resources);
let inner = Rc::new(RefCell::new(Inner {
prefix: prefix_len,
default: parts.default,
default: Rc::clone(&parts.default),
encoding: parts.encoding,
handlers: parts.handlers,
resources,

View File

@ -1,5 +1,7 @@
use std::cell::RefCell;
use std::marker::PhantomData;
use std::ops::Deref;
use std::rc::Rc;
use futures::future::{err, ok, Future};
use futures::{Async, Poll};
@ -8,6 +10,7 @@ use error::Error;
use http::StatusCode;
use httprequest::HttpRequest;
use httpresponse::HttpResponse;
use resource::ResourceHandler;
/// Trait defines object that could be registered as route handler
#[allow(unused_variables)]
@ -403,6 +406,14 @@ where
// /// Trait defines object that could be registered as resource route
pub(crate) trait RouteHandler<S>: 'static {
fn handle(&mut self, req: HttpRequest<S>) -> AsyncResult<HttpResponse>;
fn has_default_resource(&self) -> bool {
false
}
fn default_resource(&mut self, default: Rc<RefCell<ResourceHandler<S>>>) {
unimplemented!()
}
}
/// Route handler wrapper for Handler

View File

@ -405,6 +405,14 @@ impl<S: 'static> RouteHandler<S> for Scope<S> {
unimplemented!()
}
}
fn has_default_resource(&self) -> bool {
self.default.is_some()
}
fn default_resource(&mut self, default: ScopeResource<S>) {
self.default = Some(default);
}
}
struct Wrapper<S: 'static> {