mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 16:02:59 +01:00
remove unsafe from scope impl
This commit is contained in:
parent
c427fd1241
commit
a69c1e3de5
@ -4,7 +4,7 @@ use std::rc::Rc;
|
|||||||
|
|
||||||
use handler::{AsyncResult, FromRequest, Handler, Responder, RouteHandler, WrapHandler};
|
use handler::{AsyncResult, FromRequest, Handler, Responder, RouteHandler, WrapHandler};
|
||||||
use header::ContentEncoding;
|
use header::ContentEncoding;
|
||||||
use http::Method;
|
use http::{Method, StatusCode};
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
use httpresponse::HttpResponse;
|
||||||
use middleware::Middleware;
|
use middleware::Middleware;
|
||||||
@ -49,14 +49,21 @@ impl<S: 'static> PipelineHandler<S> for Inner<S> {
|
|||||||
&mut self, req: HttpRequest<S>, htype: HandlerType,
|
&mut self, req: HttpRequest<S>, htype: HandlerType,
|
||||||
) -> AsyncResult<HttpResponse> {
|
) -> AsyncResult<HttpResponse> {
|
||||||
match htype {
|
match htype {
|
||||||
HandlerType::Normal(idx) => {
|
HandlerType::Normal(idx) => match self.resources[idx].handle(req) {
|
||||||
self.resources[idx].handle(req, Some(&mut self.default))
|
Ok(result) => result,
|
||||||
}
|
Err(req) => match self.default.handle(req) {
|
||||||
|
Ok(result) => result,
|
||||||
|
Err(_) => AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND)),
|
||||||
|
},
|
||||||
|
},
|
||||||
HandlerType::Handler(idx) => match self.handlers[idx] {
|
HandlerType::Handler(idx) => match self.handlers[idx] {
|
||||||
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 => self.default.handle(req, None),
|
HandlerType::Default => match self.default.handle(req) {
|
||||||
|
Ok(result) => result,
|
||||||
|
Err(_) => AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND)),
|
||||||
|
},
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,9 @@
|
|||||||
|
use std::cell::RefCell;
|
||||||
use std::marker::PhantomData;
|
use std::marker::PhantomData;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
use std::cell::RefCell;
|
|
||||||
|
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
use http::{Method, StatusCode};
|
use http::Method;
|
||||||
use smallvec::SmallVec;
|
use smallvec::SmallVec;
|
||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
@ -282,21 +282,18 @@ impl<S: 'static> ResourceHandler<S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn handle(
|
pub(crate) fn handle(
|
||||||
&mut self, mut req: HttpRequest<S>, default: Option<&mut ResourceHandler<S>>,
|
&mut self, mut req: HttpRequest<S>,
|
||||||
) -> AsyncResult<HttpResponse> {
|
) -> Result<AsyncResult<HttpResponse>, HttpRequest<S>> {
|
||||||
for route in &mut self.routes {
|
for route in &mut self.routes {
|
||||||
if route.check(&mut req) {
|
if route.check(&mut req) {
|
||||||
return if self.middlewares.borrow().is_empty() {
|
return if self.middlewares.borrow().is_empty() {
|
||||||
route.handle(req)
|
Ok(route.handle(req))
|
||||||
} else {
|
} else {
|
||||||
route.compose(req, Rc::clone(&self.middlewares))
|
Ok(route.compose(req, Rc::clone(&self.middlewares)))
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if let Some(resource) = default {
|
|
||||||
resource.handle(req, None)
|
Err(req)
|
||||||
} else {
|
|
||||||
AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND))
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
93
src/scope.rs
93
src/scope.rs
@ -7,7 +7,7 @@ use futures::{Async, Future, Poll};
|
|||||||
|
|
||||||
use error::Error;
|
use error::Error;
|
||||||
use handler::{AsyncResult, AsyncResultItem, FromRequest, Responder, RouteHandler};
|
use handler::{AsyncResult, AsyncResultItem, FromRequest, Responder, RouteHandler};
|
||||||
use http::Method;
|
use http::{Method, StatusCode};
|
||||||
use httprequest::HttpRequest;
|
use httprequest::HttpRequest;
|
||||||
use httpresponse::HttpResponse;
|
use httpresponse::HttpResponse;
|
||||||
use middleware::{
|
use middleware::{
|
||||||
@ -18,8 +18,9 @@ use pred::Predicate;
|
|||||||
use resource::ResourceHandler;
|
use resource::ResourceHandler;
|
||||||
use router::Resource;
|
use router::Resource;
|
||||||
|
|
||||||
|
type ScopeResource<S> = Rc<RefCell<ResourceHandler<S>>>;
|
||||||
type Route<S> = UnsafeCell<Box<RouteHandler<S>>>;
|
type Route<S> = UnsafeCell<Box<RouteHandler<S>>>;
|
||||||
type ScopeResources<S> = Rc<Vec<(Resource, Rc<UnsafeCell<ResourceHandler<S>>>)>>;
|
type ScopeResources<S> = Rc<Vec<(Resource, ScopeResource<S>)>>;
|
||||||
type NestedInfo<S> = (Resource, Route<S>, Vec<Box<Predicate<S>>>);
|
type NestedInfo<S> = (Resource, Route<S>, Vec<Box<Predicate<S>>>);
|
||||||
|
|
||||||
/// Resources scope
|
/// Resources scope
|
||||||
@ -57,7 +58,7 @@ pub struct Scope<S: 'static> {
|
|||||||
filters: Vec<Box<Predicate<S>>>,
|
filters: Vec<Box<Predicate<S>>>,
|
||||||
nested: Vec<NestedInfo<S>>,
|
nested: Vec<NestedInfo<S>>,
|
||||||
middlewares: Rc<RefCell<Vec<Box<Middleware<S>>>>>,
|
middlewares: Rc<RefCell<Vec<Box<Middleware<S>>>>>,
|
||||||
default: Rc<UnsafeCell<ResourceHandler<S>>>,
|
default: Option<ScopeResource<S>>,
|
||||||
resources: ScopeResources<S>,
|
resources: ScopeResources<S>,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +72,7 @@ impl<S: 'static> Scope<S> {
|
|||||||
nested: Vec::new(),
|
nested: Vec::new(),
|
||||||
resources: Rc::new(Vec::new()),
|
resources: Rc::new(Vec::new()),
|
||||||
middlewares: Rc::new(RefCell::new(Vec::new())),
|
middlewares: Rc::new(RefCell::new(Vec::new())),
|
||||||
default: Rc::new(UnsafeCell::new(ResourceHandler::default_not_found())),
|
default: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -135,7 +136,7 @@ impl<S: 'static> Scope<S> {
|
|||||||
nested: Vec::new(),
|
nested: Vec::new(),
|
||||||
resources: Rc::new(Vec::new()),
|
resources: Rc::new(Vec::new()),
|
||||||
middlewares: Rc::new(RefCell::new(Vec::new())),
|
middlewares: Rc::new(RefCell::new(Vec::new())),
|
||||||
default: Rc::new(UnsafeCell::new(ResourceHandler::default_not_found())),
|
default: None,
|
||||||
};
|
};
|
||||||
let mut scope = f(scope);
|
let mut scope = f(scope);
|
||||||
|
|
||||||
@ -178,7 +179,7 @@ impl<S: 'static> Scope<S> {
|
|||||||
nested: Vec::new(),
|
nested: Vec::new(),
|
||||||
resources: Rc::new(Vec::new()),
|
resources: Rc::new(Vec::new()),
|
||||||
middlewares: Rc::new(RefCell::new(Vec::new())),
|
middlewares: Rc::new(RefCell::new(Vec::new())),
|
||||||
default: Rc::new(UnsafeCell::new(ResourceHandler::default_not_found())),
|
default: None,
|
||||||
};
|
};
|
||||||
let mut scope = f(scope);
|
let mut scope = f(scope);
|
||||||
|
|
||||||
@ -229,8 +230,7 @@ impl<S: 'static> Scope<S> {
|
|||||||
let slf: &Scope<S> = unsafe { &*(&self as *const _) };
|
let slf: &Scope<S> = unsafe { &*(&self as *const _) };
|
||||||
for &(ref pattern, ref resource) in slf.resources.iter() {
|
for &(ref pattern, ref resource) in slf.resources.iter() {
|
||||||
if pattern.pattern() == path {
|
if pattern.pattern() == path {
|
||||||
let resource = unsafe { &mut *resource.get() };
|
resource.borrow_mut().method(method).with(f);
|
||||||
resource.method(method).with(f);
|
|
||||||
return self;
|
return self;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -245,7 +245,7 @@ impl<S: 'static> Scope<S> {
|
|||||||
);
|
);
|
||||||
Rc::get_mut(&mut self.resources)
|
Rc::get_mut(&mut self.resources)
|
||||||
.expect("Can not use after configuration")
|
.expect("Can not use after configuration")
|
||||||
.push((pattern, Rc::new(UnsafeCell::new(handler))));
|
.push((pattern, Rc::new(RefCell::new(handler))));
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
@ -289,18 +289,21 @@ impl<S: 'static> Scope<S> {
|
|||||||
);
|
);
|
||||||
Rc::get_mut(&mut self.resources)
|
Rc::get_mut(&mut self.resources)
|
||||||
.expect("Can not use after configuration")
|
.expect("Can not use after configuration")
|
||||||
.push((pattern, Rc::new(UnsafeCell::new(handler))));
|
.push((pattern, Rc::new(RefCell::new(handler))));
|
||||||
|
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Default resource to be used if no matching route could be found.
|
/// Default resource to be used if no matching route could be found.
|
||||||
pub fn default_resource<F, R>(self, f: F) -> Scope<S>
|
pub fn default_resource<F, R>(mut self, f: F) -> Scope<S>
|
||||||
where
|
where
|
||||||
F: FnOnce(&mut ResourceHandler<S>) -> R + 'static,
|
F: FnOnce(&mut ResourceHandler<S>) -> R + 'static,
|
||||||
{
|
{
|
||||||
let default = unsafe { &mut *self.default.as_ref().get() };
|
if self.default.is_none() {
|
||||||
f(default);
|
self.default =
|
||||||
|
Some(Rc::new(RefCell::new(ResourceHandler::default_not_found())));
|
||||||
|
}
|
||||||
|
f(&mut *self.default.as_ref().unwrap().borrow_mut());
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -327,17 +330,27 @@ impl<S: 'static> RouteHandler<S> for Scope<S> {
|
|||||||
// recognize resources
|
// recognize resources
|
||||||
for &(ref pattern, ref resource) in self.resources.iter() {
|
for &(ref pattern, ref resource) in self.resources.iter() {
|
||||||
if pattern.match_with_params(&mut req, tail, false) {
|
if pattern.match_with_params(&mut req, tail, false) {
|
||||||
let default = unsafe { &mut *self.default.as_ref().get() };
|
|
||||||
|
|
||||||
if self.middlewares.borrow().is_empty() {
|
if self.middlewares.borrow().is_empty() {
|
||||||
let resource = unsafe { &mut *resource.get() };
|
return match resource.borrow_mut().handle(req) {
|
||||||
return resource.handle(req, Some(default));
|
Ok(result) => result,
|
||||||
|
Err(req) => {
|
||||||
|
if let Some(ref default) = self.default {
|
||||||
|
match default.borrow_mut().handle(req) {
|
||||||
|
Ok(result) => result,
|
||||||
|
Err(_) => AsyncResult::ok(HttpResponse::new(
|
||||||
|
StatusCode::NOT_FOUND,
|
||||||
|
)),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
} else {
|
} else {
|
||||||
return AsyncResult::async(Box::new(Compose::new(
|
return AsyncResult::async(Box::new(Compose::new(
|
||||||
req,
|
req,
|
||||||
Rc::clone(&self.middlewares),
|
Rc::clone(&self.middlewares),
|
||||||
Rc::clone(&resource),
|
Rc::clone(&resource),
|
||||||
Some(Rc::clone(&self.default)),
|
|
||||||
)));
|
)));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -365,16 +378,23 @@ impl<S: 'static> RouteHandler<S> for Scope<S> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// default handler
|
// default handler
|
||||||
let default = unsafe { &mut *self.default.as_ref().get() };
|
|
||||||
if self.middlewares.borrow().is_empty() {
|
if self.middlewares.borrow().is_empty() {
|
||||||
default.handle(req, None)
|
if let Some(ref default) = self.default {
|
||||||
} else {
|
match default.borrow_mut().handle(req) {
|
||||||
|
Ok(result) => result,
|
||||||
|
Err(_) => AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND)),
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
AsyncResult::ok(HttpResponse::new(StatusCode::NOT_FOUND))
|
||||||
|
}
|
||||||
|
} else if let Some(ref default) = self.default {
|
||||||
AsyncResult::async(Box::new(Compose::new(
|
AsyncResult::async(Box::new(Compose::new(
|
||||||
req,
|
req,
|
||||||
Rc::clone(&self.middlewares),
|
Rc::clone(&self.middlewares),
|
||||||
Rc::clone(&self.default),
|
Rc::clone(default),
|
||||||
None,
|
|
||||||
)))
|
)))
|
||||||
|
} else {
|
||||||
|
unimplemented!()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -417,8 +437,7 @@ struct ComposeInfo<S: 'static> {
|
|||||||
count: usize,
|
count: usize,
|
||||||
req: HttpRequest<S>,
|
req: HttpRequest<S>,
|
||||||
mws: Rc<RefCell<Vec<Box<Middleware<S>>>>>,
|
mws: Rc<RefCell<Vec<Box<Middleware<S>>>>>,
|
||||||
default: Option<Rc<UnsafeCell<ResourceHandler<S>>>>,
|
resource: Rc<RefCell<ResourceHandler<S>>>,
|
||||||
resource: Rc<UnsafeCell<ResourceHandler<S>>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
enum ComposeState<S: 'static> {
|
enum ComposeState<S: 'static> {
|
||||||
@ -444,15 +463,13 @@ impl<S: 'static> ComposeState<S> {
|
|||||||
impl<S: 'static> Compose<S> {
|
impl<S: 'static> Compose<S> {
|
||||||
fn new(
|
fn new(
|
||||||
req: HttpRequest<S>, mws: Rc<RefCell<Vec<Box<Middleware<S>>>>>,
|
req: HttpRequest<S>, mws: Rc<RefCell<Vec<Box<Middleware<S>>>>>,
|
||||||
resource: Rc<UnsafeCell<ResourceHandler<S>>>,
|
resource: Rc<RefCell<ResourceHandler<S>>>,
|
||||||
default: Option<Rc<UnsafeCell<ResourceHandler<S>>>>,
|
|
||||||
) -> Self {
|
) -> Self {
|
||||||
let mut info = ComposeInfo {
|
let mut info = ComposeInfo {
|
||||||
count: 0,
|
count: 0,
|
||||||
req,
|
req,
|
||||||
mws,
|
mws,
|
||||||
resource,
|
resource,
|
||||||
default,
|
|
||||||
};
|
};
|
||||||
let state = StartMiddlewares::init(&mut info);
|
let state = StartMiddlewares::init(&mut info);
|
||||||
|
|
||||||
@ -492,12 +509,10 @@ impl<S: 'static> StartMiddlewares<S> {
|
|||||||
let len = info.mws.borrow().len();
|
let len = info.mws.borrow().len();
|
||||||
loop {
|
loop {
|
||||||
if info.count == len {
|
if info.count == len {
|
||||||
let resource = unsafe { &mut *info.resource.get() };
|
let reply = {
|
||||||
let reply = if let Some(ref default) = info.default {
|
let req = info.req.clone();
|
||||||
let d = unsafe { &mut *default.as_ref().get() };
|
let mut resource = info.resource.borrow_mut();
|
||||||
resource.handle(info.req.clone(), Some(d))
|
resource.handle(req).unwrap()
|
||||||
} else {
|
|
||||||
resource.handle(info.req.clone(), None)
|
|
||||||
};
|
};
|
||||||
return WaitingResponse::init(info, reply);
|
return WaitingResponse::init(info, reply);
|
||||||
} else {
|
} else {
|
||||||
@ -531,12 +546,10 @@ impl<S: 'static> StartMiddlewares<S> {
|
|||||||
}
|
}
|
||||||
loop {
|
loop {
|
||||||
if info.count == len {
|
if info.count == len {
|
||||||
let resource = unsafe { &mut *info.resource.get() };
|
let reply = {
|
||||||
let reply = if let Some(ref default) = info.default {
|
let req = info.req.clone();
|
||||||
let d = unsafe { &mut *default.as_ref().get() };
|
let mut resource = info.resource.borrow_mut();
|
||||||
resource.handle(info.req.clone(), Some(d))
|
resource.handle(req).unwrap()
|
||||||
} else {
|
|
||||||
resource.handle(info.req.clone(), None)
|
|
||||||
};
|
};
|
||||||
return Some(WaitingResponse::init(info, reply));
|
return Some(WaitingResponse::init(info, reply));
|
||||||
} else {
|
} else {
|
||||||
|
@ -163,18 +163,18 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
|
|||||||
|
|
||||||
// status line
|
// status line
|
||||||
helpers::write_status_line(version, msg.status().as_u16(), &mut buffer);
|
helpers::write_status_line(version, msg.status().as_u16(), &mut buffer);
|
||||||
SharedBytes::extend_from_slice_(buffer, reason);
|
buffer.extend_from_slice(reason);
|
||||||
|
|
||||||
match body {
|
match body {
|
||||||
Body::Empty => if req.method != Method::HEAD {
|
Body::Empty => if req.method != Method::HEAD {
|
||||||
SharedBytes::put_slice(buffer, b"\r\ncontent-length: 0\r\n");
|
buffer.extend_from_slice(b"\r\ncontent-length: 0\r\n");
|
||||||
} else {
|
} else {
|
||||||
SharedBytes::put_slice(buffer, b"\r\n");
|
buffer.extend_from_slice(b"\r\n");
|
||||||
},
|
},
|
||||||
Body::Binary(ref bytes) => {
|
Body::Binary(ref bytes) => {
|
||||||
helpers::write_content_length(bytes.len(), &mut buffer)
|
helpers::write_content_length(bytes.len(), &mut buffer)
|
||||||
}
|
}
|
||||||
_ => SharedBytes::put_slice(buffer, b"\r\n"),
|
_ => buffer.extend_from_slice(b"\r\n"),
|
||||||
}
|
}
|
||||||
|
|
||||||
// write headers
|
// write headers
|
||||||
@ -218,7 +218,7 @@ impl<T: AsyncWrite, H: 'static> Writer for H1Writer<T, H> {
|
|||||||
self.settings.set_date(&mut buffer);
|
self.settings.set_date(&mut buffer);
|
||||||
} else {
|
} else {
|
||||||
// msg eof
|
// msg eof
|
||||||
SharedBytes::extend_from_slice_(buffer, b"\r\n");
|
buffer.extend_from_slice(b"\r\n");
|
||||||
}
|
}
|
||||||
self.headers_size = buffer.len() as u32;
|
self.headers_size = buffer.len() as u32;
|
||||||
}
|
}
|
||||||
|
@ -1,9 +1,10 @@
|
|||||||
use bytes::{BufMut, BytesMut};
|
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::collections::VecDeque;
|
use std::collections::VecDeque;
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
|
use bytes::BytesMut;
|
||||||
|
|
||||||
use body::Binary;
|
use body::Binary;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
@ -80,31 +81,13 @@ impl SharedBytes {
|
|||||||
#[inline]
|
#[inline]
|
||||||
pub fn extend(&mut self, data: &Binary) {
|
pub fn extend(&mut self, data: &Binary) {
|
||||||
let buf = self.get_mut();
|
let buf = self.get_mut();
|
||||||
let data = data.as_ref();
|
buf.extend_from_slice(data.as_ref());
|
||||||
buf.reserve(data.len());
|
|
||||||
SharedBytes::put_slice(buf, data);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn extend_from_slice(&mut self, data: &[u8]) {
|
pub fn extend_from_slice(&mut self, data: &[u8]) {
|
||||||
let buf = self.get_mut();
|
let buf = self.get_mut();
|
||||||
buf.reserve(data.len());
|
buf.extend_from_slice(data);
|
||||||
SharedBytes::put_slice(buf, data);
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn put_slice(buf: &mut BytesMut, src: &[u8]) {
|
|
||||||
let len = src.len();
|
|
||||||
unsafe {
|
|
||||||
buf.bytes_mut()[..len].copy_from_slice(src);
|
|
||||||
buf.advance_mut(len);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
|
||||||
pub(crate) fn extend_from_slice_(buf: &mut BytesMut, data: &[u8]) {
|
|
||||||
buf.reserve(data.len());
|
|
||||||
SharedBytes::put_slice(buf, data);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user