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

rename RouteInfo

This commit is contained in:
Nikolay Kim 2018-07-15 15:24:27 +06:00
parent da915972c0
commit 9570c1cccd
4 changed files with 66 additions and 70 deletions

View File

@ -92,7 +92,7 @@ impl<S> Handler<S> for NormalizePath {
// merge slashes // merge slashes
let p = self.re_merge.replace_all(req.path(), "/"); let p = self.re_merge.replace_all(req.path(), "/");
if p.len() != req.path().len() { if p.len() != req.path().len() {
if req.route().has_route(p.as_ref()) { if req.resource().has_route(p.as_ref()) {
let p = if !query.is_empty() { let p = if !query.is_empty() {
p + "?" + query p + "?" + query
} else { } else {
@ -105,7 +105,7 @@ impl<S> Handler<S> for NormalizePath {
// merge slashes and append trailing slash // merge slashes and append trailing slash
if self.append && !p.ends_with('/') { if self.append && !p.ends_with('/') {
let p = p.as_ref().to_owned() + "/"; let p = p.as_ref().to_owned() + "/";
if req.route().has_route(&p) { if req.resource().has_route(&p) {
let p = if !query.is_empty() { let p = if !query.is_empty() {
p + "?" + query p + "?" + query
} else { } else {
@ -120,7 +120,7 @@ impl<S> Handler<S> for NormalizePath {
// try to remove trailing slash // try to remove trailing slash
if p.ends_with('/') { if p.ends_with('/') {
let p = p.as_ref().trim_right_matches('/'); let p = p.as_ref().trim_right_matches('/');
if req.route().has_route(p) { if req.resource().has_route(p) {
let mut req = HttpResponse::build(self.redirect); let mut req = HttpResponse::build(self.redirect);
return if !query.is_empty() { return if !query.is_empty() {
req.header( req.header(
@ -135,7 +135,7 @@ impl<S> Handler<S> for NormalizePath {
} else if p.ends_with('/') { } else if p.ends_with('/') {
// try to remove trailing slash // try to remove trailing slash
let p = p.as_ref().trim_right_matches('/'); let p = p.as_ref().trim_right_matches('/');
if req.route().has_route(p) { if req.resource().has_route(p) {
let mut req = HttpResponse::build(self.redirect); let mut req = HttpResponse::build(self.redirect);
return if !query.is_empty() { return if !query.is_empty() {
req.header( req.header(
@ -151,7 +151,7 @@ impl<S> Handler<S> for NormalizePath {
// append trailing slash // append trailing slash
if self.append && !req.path().ends_with('/') { if self.append && !req.path().ends_with('/') {
let p = req.path().to_owned() + "/"; let p = req.path().to_owned() + "/";
if req.route().has_route(&p) { if req.resource().has_route(&p) {
let p = if !query.is_empty() { let p = if !query.is_empty() {
p + "?" + query p + "?" + query
} else { } else {

View File

@ -20,7 +20,7 @@ use httpresponse::{HttpResponse, HttpResponseBuilder};
use info::ConnectionInfo; use info::ConnectionInfo;
use param::Params; use param::Params;
use payload::Payload; use payload::Payload;
use router::{ResourceDef, RouteInfo}; use router::ResourceInfo;
use server::Request; use server::Request;
struct Query(HashMap<String, String>); struct Query(HashMap<String, String>);
@ -30,7 +30,7 @@ struct Cookies(Vec<Cookie<'static>>);
pub struct HttpRequest<S = ()> { pub struct HttpRequest<S = ()> {
req: Option<Request>, req: Option<Request>,
state: Rc<S>, state: Rc<S>,
route: RouteInfo, resource: ResourceInfo,
} }
impl<S> HttpMessage for HttpRequest<S> { impl<S> HttpMessage for HttpRequest<S> {
@ -61,10 +61,12 @@ impl<S> Deref for HttpRequest<S> {
impl<S> HttpRequest<S> { impl<S> HttpRequest<S> {
#[inline] #[inline]
pub(crate) fn new(req: Request, state: Rc<S>, route: RouteInfo) -> HttpRequest<S> { pub(crate) fn new(
req: Request, state: Rc<S>, resource: ResourceInfo,
) -> HttpRequest<S> {
HttpRequest { HttpRequest {
state, state,
route, resource,
req: Some(req), req: Some(req),
} }
} }
@ -75,17 +77,17 @@ impl<S> HttpRequest<S> {
HttpRequest { HttpRequest {
state, state,
req: self.req.as_ref().map(|r| r.clone()), req: self.req.as_ref().map(|r| r.clone()),
route: self.route.clone(), resource: self.resource.clone(),
} }
} }
#[inline] #[inline]
/// Construct new http request with new RouteInfo. /// Construct new http request with new RouteInfo.
pub(crate) fn with_route_info(&self, mut route: RouteInfo) -> HttpRequest<S> { pub(crate) fn with_route_info(&self, mut resource: ResourceInfo) -> HttpRequest<S> {
route.merge(&self.route); resource.merge(&self.resource);
HttpRequest { HttpRequest {
route, resource,
req: self.req.as_ref().map(|r| r.clone()), req: self.req.as_ref().map(|r| r.clone()),
state: self.state.clone(), state: self.state.clone(),
} }
@ -193,7 +195,7 @@ impl<S> HttpRequest<S> {
U: IntoIterator<Item = I>, U: IntoIterator<Item = I>,
I: AsRef<str>, I: AsRef<str>,
{ {
self.route.url_for(&self, name, elements) self.resource.url_for(&self, name, elements)
} }
/// Generate url for named resource /// Generate url for named resource
@ -207,14 +209,8 @@ impl<S> HttpRequest<S> {
/// This method returns reference to current `RouteInfo` object. /// This method returns reference to current `RouteInfo` object.
#[inline] #[inline]
pub fn route(&self) -> &RouteInfo { pub fn resource(&self) -> &ResourceInfo {
&self.route &self.resource
}
/// This method returns reference to matched `Resource` object.
#[inline]
pub fn resource(&self) -> Option<&ResourceDef> {
self.route.resource()
} }
/// Peer socket address /// Peer socket address
@ -300,7 +296,7 @@ impl<S> HttpRequest<S> {
/// access the matched value for that segment. /// access the matched value for that segment.
#[inline] #[inline]
pub fn match_info(&self) -> &Params { pub fn match_info(&self) -> &Params {
&self.route.match_info() &self.resource.match_info()
} }
/// Check if request requires connection upgrade /// Check if request requires connection upgrade
@ -331,7 +327,7 @@ impl<S> Clone for HttpRequest<S> {
HttpRequest { HttpRequest {
req: self.req.as_ref().map(|r| r.clone()), req: self.req.as_ref().map(|r| r.clone()),
state: self.state.clone(), state: self.state.clone(),
route: self.route.clone(), resource: self.resource.clone(),
} }
} }
} }

View File

@ -246,7 +246,7 @@ pub mod dev {
pub use param::{FromParam, Params}; pub use param::{FromParam, Params};
pub use resource::Resource; pub use resource::Resource;
pub use route::Route; pub use route::Route;
pub use router::{ResourceDef, ResourceType, Router}; pub use router::{ResourceDef, ResourceInfo, ResourceType, Router};
} }
pub mod http { pub mod http {

View File

@ -18,7 +18,7 @@ use scope::Scope;
use server::Request; use server::Request;
#[derive(Debug, Copy, Clone, PartialEq)] #[derive(Debug, Copy, Clone, PartialEq)]
pub(crate) enum RouterResource { pub(crate) enum ResourceId {
Default, Default,
Normal(u16), Normal(u16),
} }
@ -43,29 +43,29 @@ pub struct Router<S> {
default: Option<DefaultResource<S>>, default: Option<DefaultResource<S>>,
} }
/// Information about current route /// Information about current resource
#[derive(Clone)] #[derive(Clone)]
pub struct RouteInfo { pub struct ResourceInfo {
router: Rc<Inner>, router: Rc<Inner>,
resource: RouterResource, resource: ResourceId,
params: Params, params: Params,
} }
impl RouteInfo { impl ResourceInfo {
/// Name os the resource /// Name os the resource
#[inline] #[inline]
pub fn name(&self) -> &str { pub fn name(&self) -> &str {
if let RouterResource::Normal(idx) = self.resource { if let ResourceId::Normal(idx) = self.resource {
self.router.patterns[idx as usize].name() self.router.patterns[idx as usize].name()
} else { } else {
"" ""
} }
} }
/// This method returns reference to matched `Resource` object. /// This method returns reference to matched `ResourceDef` object.
#[inline] #[inline]
pub fn resource(&self) -> Option<&ResourceDef> { pub fn rdef(&self) -> Option<&ResourceDef> {
if let RouterResource::Normal(idx) = self.resource { if let ResourceId::Normal(idx) = self.resource {
Some(&self.router.patterns[idx as usize]) Some(&self.router.patterns[idx as usize])
} else { } else {
None None
@ -84,7 +84,7 @@ impl RouteInfo {
} }
#[inline] #[inline]
pub(crate) fn merge(&mut self, info: &RouteInfo) { pub(crate) fn merge(&mut self, info: &ResourceInfo) {
let mut p = info.params.clone(); let mut p = info.params.clone();
p.set_tail(self.params.tail); p.set_tail(self.params.tail);
for item in &self.params.segments { for item in &self.params.segments {
@ -185,32 +185,32 @@ impl<S: 'static> Router<S> {
} }
#[inline] #[inline]
pub(crate) fn route_info_params(&self, idx: u16, params: Params) -> RouteInfo { pub(crate) fn route_info_params(&self, idx: u16, params: Params) -> ResourceInfo {
RouteInfo { ResourceInfo {
params, params,
router: self.defs.clone(), router: self.defs.clone(),
resource: RouterResource::Normal(idx), resource: ResourceId::Normal(idx),
} }
} }
#[cfg(test)] #[cfg(test)]
pub(crate) fn route_info(&self, req: &Request, prefix: u16) -> RouteInfo { pub(crate) fn route_info(&self, req: &Request, prefix: u16) -> ResourceInfo {
let mut params = Params::with_url(req.url()); let mut params = Params::with_url(req.url());
params.set_tail(prefix); params.set_tail(prefix);
RouteInfo { ResourceInfo {
params, params,
router: self.defs.clone(), router: self.defs.clone(),
resource: RouterResource::Default, resource: ResourceId::Default,
} }
} }
#[cfg(test)] #[cfg(test)]
pub(crate) fn default_route_info(&self) -> RouteInfo { pub(crate) fn default_route_info(&self) -> ResourceInfo {
RouteInfo { ResourceInfo {
params: Params::new(), params: Params::new(),
router: self.defs.clone(), router: self.defs.clone(),
resource: RouterResource::Default, resource: ResourceId::Default,
} }
} }
@ -326,9 +326,9 @@ impl<S: 'static> Router<S> {
/// Handle request /// Handle request
pub fn handle(&self, req: &HttpRequest<S>) -> AsyncResult<HttpResponse> { pub fn handle(&self, req: &HttpRequest<S>) -> AsyncResult<HttpResponse> {
let resource = match req.route().resource { let resource = match req.resource().resource {
RouterResource::Normal(idx) => &self.resources[idx as usize], ResourceId::Normal(idx) => &self.resources[idx as usize],
RouterResource::Default => { ResourceId::Default => {
if let Some(ref default) = self.default { if let Some(ref default) = self.default {
if let Some(id) = default.get_route_id(req) { if let Some(id) = default.get_route_id(req) {
return default.handle(id, req); return default.handle(id, req);
@ -356,14 +356,14 @@ impl<S: 'static> Router<S> {
} }
/// Query for matched resource /// Query for matched resource
pub fn recognize(&self, req: &Request, state: &S, tail: usize) -> RouteInfo { pub fn recognize(&self, req: &Request, state: &S, tail: usize) -> ResourceInfo {
self.match_with_params(req, state, tail, true) self.match_with_params(req, state, tail, true)
} }
/// Query for matched resource /// Query for matched resource
pub(crate) fn match_with_params( pub(crate) fn match_with_params(
&self, req: &Request, state: &S, tail: usize, insert: bool, &self, req: &Request, state: &S, tail: usize, insert: bool,
) -> RouteInfo { ) -> ResourceInfo {
if tail <= req.path().len() { if tail <= req.path().len() {
'outer: for (idx, resource) in self.patterns.iter().enumerate() { 'outer: for (idx, resource) in self.patterns.iter().enumerate() {
match resource { match resource {
@ -397,10 +397,10 @@ impl<S: 'static> Router<S> {
} }
} }
} }
RouteInfo { ResourceInfo {
params: Params::new(), params: Params::new(),
router: self.defs.clone(), router: self.defs.clone(),
resource: RouterResource::Default, resource: ResourceId::Default,
} }
} }
} }
@ -806,35 +806,35 @@ mod tests {
let req = TestRequest::with_uri("/name").finish(); let req = TestRequest::with_uri("/name").finish();
let info = router.recognize(&req, &(), 0); let info = router.recognize(&req, &(), 0);
assert_eq!(info.resource, RouterResource::Normal(0)); assert_eq!(info.resource, ResourceId::Normal(0));
assert!(info.match_info().is_empty()); assert!(info.match_info().is_empty());
let req = TestRequest::with_uri("/name/value").finish(); let req = TestRequest::with_uri("/name/value").finish();
let info = router.recognize(&req, &(), 0); let info = router.recognize(&req, &(), 0);
assert_eq!(info.resource, RouterResource::Normal(1)); assert_eq!(info.resource, ResourceId::Normal(1));
assert_eq!(info.match_info().get("val").unwrap(), "value"); assert_eq!(info.match_info().get("val").unwrap(), "value");
assert_eq!(&info.match_info()["val"], "value"); assert_eq!(&info.match_info()["val"], "value");
let req = TestRequest::with_uri("/name/value2/index.html").finish(); let req = TestRequest::with_uri("/name/value2/index.html").finish();
let info = router.recognize(&req, &(), 0); let info = router.recognize(&req, &(), 0);
assert_eq!(info.resource, RouterResource::Normal(2)); assert_eq!(info.resource, ResourceId::Normal(2));
assert_eq!(info.match_info().get("val").unwrap(), "value2"); assert_eq!(info.match_info().get("val").unwrap(), "value2");
let req = TestRequest::with_uri("/file/file.gz").finish(); let req = TestRequest::with_uri("/file/file.gz").finish();
let info = router.recognize(&req, &(), 0); let info = router.recognize(&req, &(), 0);
assert_eq!(info.resource, RouterResource::Normal(3)); assert_eq!(info.resource, ResourceId::Normal(3));
assert_eq!(info.match_info().get("file").unwrap(), "file"); assert_eq!(info.match_info().get("file").unwrap(), "file");
assert_eq!(info.match_info().get("ext").unwrap(), "gz"); assert_eq!(info.match_info().get("ext").unwrap(), "gz");
let req = TestRequest::with_uri("/vtest/ttt/index.html").finish(); let req = TestRequest::with_uri("/vtest/ttt/index.html").finish();
let info = router.recognize(&req, &(), 0); let info = router.recognize(&req, &(), 0);
assert_eq!(info.resource, RouterResource::Normal(4)); assert_eq!(info.resource, ResourceId::Normal(4));
assert_eq!(info.match_info().get("val").unwrap(), "test"); assert_eq!(info.match_info().get("val").unwrap(), "test");
assert_eq!(info.match_info().get("val2").unwrap(), "ttt"); assert_eq!(info.match_info().get("val2").unwrap(), "ttt");
let req = TestRequest::with_uri("/v/blah-blah/index.html").finish(); let req = TestRequest::with_uri("/v/blah-blah/index.html").finish();
let info = router.recognize(&req, &(), 0); let info = router.recognize(&req, &(), 0);
assert_eq!(info.resource, RouterResource::Normal(5)); assert_eq!(info.resource, ResourceId::Normal(5));
assert_eq!( assert_eq!(
info.match_info().get("tail").unwrap(), info.match_info().get("tail").unwrap(),
"blah-blah/index.html" "blah-blah/index.html"
@ -842,12 +842,12 @@ mod tests {
let req = TestRequest::with_uri("/test2/index.html").finish(); let req = TestRequest::with_uri("/test2/index.html").finish();
let info = router.recognize(&req, &(), 0); let info = router.recognize(&req, &(), 0);
assert_eq!(info.resource, RouterResource::Normal(6)); assert_eq!(info.resource, ResourceId::Normal(6));
assert_eq!(info.match_info().get("test").unwrap(), "index"); assert_eq!(info.match_info().get("test").unwrap(), "index");
let req = TestRequest::with_uri("/bbb/index.html").finish(); let req = TestRequest::with_uri("/bbb/index.html").finish();
let info = router.recognize(&req, &(), 0); let info = router.recognize(&req, &(), 0);
assert_eq!(info.resource, RouterResource::Normal(7)); assert_eq!(info.resource, ResourceId::Normal(7));
assert_eq!(info.match_info().get("test").unwrap(), "bbb"); assert_eq!(info.match_info().get("test").unwrap(), "bbb");
} }
@ -859,11 +859,11 @@ mod tests {
let req = TestRequest::with_uri("/index.json").finish(); let req = TestRequest::with_uri("/index.json").finish();
let info = router.recognize(&req, &(), 0); let info = router.recognize(&req, &(), 0);
assert_eq!(info.resource, RouterResource::Normal(0)); assert_eq!(info.resource, ResourceId::Normal(0));
let req = TestRequest::with_uri("/test.json").finish(); let req = TestRequest::with_uri("/test.json").finish();
let info = router.recognize(&req, &(), 0); let info = router.recognize(&req, &(), 0);
assert_eq!(info.resource, RouterResource::Normal(1)); assert_eq!(info.resource, ResourceId::Normal(1));
} }
#[test] #[test]
@ -875,15 +875,15 @@ mod tests {
let req = TestRequest::with_uri("/name").finish(); let req = TestRequest::with_uri("/name").finish();
let info = router.recognize(&req, &(), 5); let info = router.recognize(&req, &(), 5);
assert_eq!(info.resource, RouterResource::Default); assert_eq!(info.resource, ResourceId::Default);
let req = TestRequest::with_uri("/test/name").finish(); let req = TestRequest::with_uri("/test/name").finish();
let info = router.recognize(&req, &(), 5); let info = router.recognize(&req, &(), 5);
assert_eq!(info.resource, RouterResource::Normal(0)); assert_eq!(info.resource, ResourceId::Normal(0));
let req = TestRequest::with_uri("/test/name/value").finish(); let req = TestRequest::with_uri("/test/name/value").finish();
let info = router.recognize(&req, &(), 5); let info = router.recognize(&req, &(), 5);
assert_eq!(info.resource, RouterResource::Normal(1)); assert_eq!(info.resource, ResourceId::Normal(1));
assert_eq!(info.match_info().get("val").unwrap(), "value"); assert_eq!(info.match_info().get("val").unwrap(), "value");
assert_eq!(&info.match_info()["val"], "value"); assert_eq!(&info.match_info()["val"], "value");
@ -895,19 +895,19 @@ mod tests {
let req = TestRequest::with_uri("/name").finish(); let req = TestRequest::with_uri("/name").finish();
let info = router.recognize(&req, &(), 6); let info = router.recognize(&req, &(), 6);
assert_eq!(info.resource, RouterResource::Default); assert_eq!(info.resource, ResourceId::Default);
let req = TestRequest::with_uri("/test2/name").finish(); let req = TestRequest::with_uri("/test2/name").finish();
let info = router.recognize(&req, &(), 6); let info = router.recognize(&req, &(), 6);
assert_eq!(info.resource, RouterResource::Normal(0)); assert_eq!(info.resource, ResourceId::Normal(0));
let req = TestRequest::with_uri("/test2/name-test").finish(); let req = TestRequest::with_uri("/test2/name-test").finish();
let info = router.recognize(&req, &(), 6); let info = router.recognize(&req, &(), 6);
assert_eq!(info.resource, RouterResource::Default); assert_eq!(info.resource, ResourceId::Default);
let req = TestRequest::with_uri("/test2/name/ttt").finish(); let req = TestRequest::with_uri("/test2/name/ttt").finish();
let info = router.recognize(&req, &(), 6); let info = router.recognize(&req, &(), 6);
assert_eq!(info.resource, RouterResource::Normal(1)); assert_eq!(info.resource, ResourceId::Normal(1));
assert_eq!(&info.match_info()["val"], "ttt"); assert_eq!(&info.match_info()["val"], "ttt");
} }
@ -1005,13 +1005,13 @@ mod tests {
let req = TestRequest::with_uri("/index.json").finish(); let req = TestRequest::with_uri("/index.json").finish();
let info = router.recognize(&req, &(), 0); let info = router.recognize(&req, &(), 0);
assert_eq!(info.resource, RouterResource::Normal(0)); assert_eq!(info.resource, ResourceId::Normal(0));
assert_eq!(info.name(), "r1"); assert_eq!(info.name(), "r1");
let req = TestRequest::with_uri("/test.json").finish(); let req = TestRequest::with_uri("/test.json").finish();
let info = router.recognize(&req, &(), 0); let info = router.recognize(&req, &(), 0);
assert_eq!(info.resource, RouterResource::Normal(1)); assert_eq!(info.resource, ResourceId::Normal(1));
assert_eq!(info.name(), "r2"); assert_eq!(info.name(), "r2");
} }
} }