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:
parent
f815c1c096
commit
c5e8c1b710
@ -29,7 +29,7 @@ pub struct HttpApplication<S = ()> {
|
|||||||
#[doc(hidden)]
|
#[doc(hidden)]
|
||||||
pub struct Inner<S> {
|
pub struct Inner<S> {
|
||||||
prefix: usize,
|
prefix: usize,
|
||||||
default: ResourceHandler<S>,
|
default: Rc<RefCell<ResourceHandler<S>>>,
|
||||||
encoding: ContentEncoding,
|
encoding: ContentEncoding,
|
||||||
resources: Vec<ResourceHandler<S>>,
|
resources: Vec<ResourceHandler<S>>,
|
||||||
handlers: Vec<PrefixHandlerType<S>>,
|
handlers: Vec<PrefixHandlerType<S>>,
|
||||||
@ -51,7 +51,7 @@ impl<S: 'static> PipelineHandler<S> for Inner<S> {
|
|||||||
match htype {
|
match htype {
|
||||||
HandlerType::Normal(idx) => match self.resources[idx].handle(req) {
|
HandlerType::Normal(idx) => match self.resources[idx].handle(req) {
|
||||||
Ok(result) => result,
|
Ok(result) => result,
|
||||||
Err(req) => match self.default.handle(req) {
|
Err(req) => match self.default.borrow_mut().handle(req) {
|
||||||
Ok(result) => result,
|
Ok(result) => result,
|
||||||
Err(_) => AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND)),
|
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::Handler(_, ref mut hnd) => hnd.handle(req),
|
||||||
PrefixHandlerType::Scope(_, 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,
|
Ok(result) => result,
|
||||||
Err(_) => AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND)),
|
Err(_) => AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND)),
|
||||||
},
|
},
|
||||||
@ -172,7 +172,7 @@ struct ApplicationParts<S> {
|
|||||||
state: S,
|
state: S,
|
||||||
prefix: String,
|
prefix: String,
|
||||||
settings: ServerSettings,
|
settings: ServerSettings,
|
||||||
default: ResourceHandler<S>,
|
default: Rc<RefCell<ResourceHandler<S>>>,
|
||||||
resources: Vec<(Resource, Option<ResourceHandler<S>>)>,
|
resources: Vec<(Resource, Option<ResourceHandler<S>>)>,
|
||||||
handlers: Vec<PrefixHandlerType<S>>,
|
handlers: Vec<PrefixHandlerType<S>>,
|
||||||
external: HashMap<String, Resource>,
|
external: HashMap<String, Resource>,
|
||||||
@ -223,7 +223,7 @@ where
|
|||||||
state,
|
state,
|
||||||
prefix: "/".to_owned(),
|
prefix: "/".to_owned(),
|
||||||
settings: ServerSettings::default(),
|
settings: ServerSettings::default(),
|
||||||
default: ResourceHandler::default_not_found(),
|
default: Rc::new(RefCell::new(ResourceHandler::default_not_found())),
|
||||||
resources: Vec::new(),
|
resources: Vec::new(),
|
||||||
handlers: Vec::new(),
|
handlers: Vec::new(),
|
||||||
external: HashMap::new(),
|
external: HashMap::new(),
|
||||||
@ -473,7 +473,7 @@ where
|
|||||||
{
|
{
|
||||||
{
|
{
|
||||||
let parts = self.parts.as_mut().expect("Use after finish");
|
let parts = self.parts.as_mut().expect("Use after finish");
|
||||||
f(&mut parts.default);
|
f(&mut parts.default.borrow_mut());
|
||||||
}
|
}
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -614,7 +614,7 @@ where
|
|||||||
|
|
||||||
/// Finish application configuration and create `HttpHandler` object.
|
/// Finish application configuration and create `HttpHandler` object.
|
||||||
pub fn finish(&mut self) -> HttpApplication<S> {
|
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 = parts.prefix.trim().trim_right_matches('/');
|
||||||
let (prefix, prefix_len) = if prefix.is_empty() {
|
let (prefix, prefix_len) = if prefix.is_empty() {
|
||||||
("/".to_owned(), 0)
|
("/".to_owned(), 0)
|
||||||
@ -627,11 +627,19 @@ where
|
|||||||
resources.push((pattern, None));
|
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 (router, resources) = Router::new(&prefix, parts.settings, resources);
|
||||||
|
|
||||||
let inner = Rc::new(RefCell::new(Inner {
|
let inner = Rc::new(RefCell::new(Inner {
|
||||||
prefix: prefix_len,
|
prefix: prefix_len,
|
||||||
default: parts.default,
|
default: Rc::clone(&parts.default),
|
||||||
encoding: parts.encoding,
|
encoding: parts.encoding,
|
||||||
handlers: parts.handlers,
|
handlers: parts.handlers,
|
||||||
resources,
|
resources,
|
||||||
|
@ -1,5 +1,7 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::ops::Deref;
|
use std::ops::Deref;
|
||||||
|
use std::rc::Rc;
|
||||||
|
|
||||||
use futures::future::{err, ok, Future};
|
use futures::future::{err, ok, Future};
|
||||||
use futures::{Async, Poll};
|
use futures::{Async, Poll};
|
||||||
@ -8,6 +10,7 @@ use error::Error;
|
|||||||
use http::StatusCode;
|
use http::StatusCode;
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
use httpresponse::HttpResponse;
|
||||||
|
use resource::ResourceHandler;
|
||||||
|
|
||||||
/// Trait defines object that could be registered as route handler
|
/// Trait defines object that could be registered as route handler
|
||||||
#[allow(unused_variables)]
|
#[allow(unused_variables)]
|
||||||
@ -403,6 +406,14 @@ where
|
|||||||
// /// Trait defines object that could be registered as resource route
|
// /// Trait defines object that could be registered as resource route
|
||||||
pub(crate) trait RouteHandler<S>: 'static {
|
pub(crate) trait RouteHandler<S>: 'static {
|
||||||
fn handle(&mut self, req: HttpRequest<S>) -> AsyncResult<HttpResponse>;
|
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
|
/// Route handler wrapper for Handler
|
||||||
|
@ -405,6 +405,14 @@ impl<S: 'static> RouteHandler<S> for Scope<S> {
|
|||||||
unimplemented!()
|
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> {
|
struct Wrapper<S: 'static> {
|
||||||
|
Loading…
Reference in New Issue
Block a user