mirror of
https://github.com/actix/actix-extras.git
synced 2024-11-24 07:53:00 +01:00
calculate prefix dynamicly
This commit is contained in:
parent
b759dddf5a
commit
42d3e86941
@ -462,14 +462,16 @@ mod tests {
|
|||||||
let mut resource = Resource::new(ResourceDef::new("/user/{name}.html"));
|
let mut resource = Resource::new(ResourceDef::new("/user/{name}.html"));
|
||||||
resource.name("index");
|
resource.name("index");
|
||||||
let mut router = Router::<()>::new();
|
let mut router = Router::<()>::new();
|
||||||
router.set_prefix("/prefix/");
|
|
||||||
router.register_resource(resource);
|
router.register_resource(resource);
|
||||||
|
|
||||||
let info = router.default_route_info();
|
let mut info = router.default_route_info();
|
||||||
|
info.set_prefix(7);
|
||||||
assert!(info.has_route("/user/test.html"));
|
assert!(info.has_route("/user/test.html"));
|
||||||
assert!(!info.has_route("/prefix/user/test.html"));
|
assert!(!info.has_route("/prefix/user/test.html"));
|
||||||
|
|
||||||
let req = TestRequest::with_header(header::HOST, "www.rust-lang.org")
|
let req = TestRequest::with_uri("/prefix/test")
|
||||||
|
.prefix(7)
|
||||||
|
.header(header::HOST, "www.rust-lang.org")
|
||||||
.finish_with_router(router);
|
.finish_with_router(router);
|
||||||
let url = req.url_for("index", &["test"]);
|
let url = req.url_for("index", &["test"]);
|
||||||
assert_eq!(
|
assert_eq!(
|
||||||
@ -483,14 +485,15 @@ mod tests {
|
|||||||
let mut resource = Resource::new(ResourceDef::new("/index.html"));
|
let mut resource = Resource::new(ResourceDef::new("/index.html"));
|
||||||
resource.name("index");
|
resource.name("index");
|
||||||
let mut router = Router::<()>::new();
|
let mut router = Router::<()>::new();
|
||||||
router.set_prefix("/prefix/");
|
|
||||||
router.register_resource(resource);
|
router.register_resource(resource);
|
||||||
|
|
||||||
let info = router.default_route_info();
|
let mut info = router.default_route_info();
|
||||||
|
info.set_prefix(7);
|
||||||
assert!(info.has_route("/index.html"));
|
assert!(info.has_route("/index.html"));
|
||||||
assert!(!info.has_route("/prefix/index.html"));
|
assert!(!info.has_route("/prefix/index.html"));
|
||||||
|
|
||||||
let req = TestRequest::default()
|
let req = TestRequest::with_uri("/prefix/test")
|
||||||
|
.prefix(7)
|
||||||
.header(header::HOST, "www.rust-lang.org")
|
.header(header::HOST, "www.rust-lang.org")
|
||||||
.finish_with_router(router);
|
.finish_with_router(router);
|
||||||
let url = req.url_for_static("index");
|
let url = req.url_for_static("index");
|
||||||
|
@ -49,6 +49,7 @@ pub struct ResourceInfo {
|
|||||||
router: Rc<Inner>,
|
router: Rc<Inner>,
|
||||||
resource: ResourceId,
|
resource: ResourceId,
|
||||||
params: Params,
|
params: Params,
|
||||||
|
prefix: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ResourceInfo {
|
impl ResourceInfo {
|
||||||
@ -72,6 +73,10 @@ impl ResourceInfo {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub(crate) fn set_prefix(&mut self, prefix: u16) {
|
||||||
|
self.prefix = prefix;
|
||||||
|
}
|
||||||
|
|
||||||
/// Get a reference to the Params object.
|
/// Get a reference to the Params object.
|
||||||
///
|
///
|
||||||
/// Params is a container for url parameters.
|
/// Params is a container for url parameters.
|
||||||
@ -91,6 +96,7 @@ impl ResourceInfo {
|
|||||||
p.add(item.0.clone(), item.1.clone());
|
p.add(item.0.clone(), item.1.clone());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
self.prefix = info.params.tail;
|
||||||
self.params = p;
|
self.params = p;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -106,7 +112,8 @@ impl ResourceInfo {
|
|||||||
I: AsRef<str>,
|
I: AsRef<str>,
|
||||||
{
|
{
|
||||||
if let Some(pattern) = self.router.named.get(name) {
|
if let Some(pattern) = self.router.named.get(name) {
|
||||||
let path = pattern.resource_path(elements, &self.router.prefix)?;
|
let path =
|
||||||
|
pattern.resource_path(elements, &req.path()[..(self.prefix as usize)])?;
|
||||||
if path.starts_with('/') {
|
if path.starts_with('/') {
|
||||||
let conn = req.connection_info();
|
let conn = req.connection_info();
|
||||||
Ok(Url::parse(&format!(
|
Ok(Url::parse(&format!(
|
||||||
@ -142,8 +149,6 @@ impl ResourceInfo {
|
|||||||
}
|
}
|
||||||
|
|
||||||
struct Inner {
|
struct Inner {
|
||||||
prefix: String,
|
|
||||||
prefix_len: usize,
|
|
||||||
named: HashMap<String, ResourceDef>,
|
named: HashMap<String, ResourceDef>,
|
||||||
patterns: Vec<ResourceDef>,
|
patterns: Vec<ResourceDef>,
|
||||||
}
|
}
|
||||||
@ -158,8 +163,6 @@ impl<S: 'static> Router<S> {
|
|||||||
pub(crate) fn new() -> Self {
|
pub(crate) fn new() -> Self {
|
||||||
Router {
|
Router {
|
||||||
defs: Rc::new(Inner {
|
defs: Rc::new(Inner {
|
||||||
prefix: String::new(),
|
|
||||||
prefix_len: 0,
|
|
||||||
named: HashMap::new(),
|
named: HashMap::new(),
|
||||||
patterns: Vec::new(),
|
patterns: Vec::new(),
|
||||||
}),
|
}),
|
||||||
@ -169,25 +172,11 @@ impl<S: 'static> Router<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Router prefix
|
|
||||||
#[inline]
|
|
||||||
pub fn prefix(&self) -> &str {
|
|
||||||
&self.defs.prefix
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Set router prefix
|
|
||||||
#[inline]
|
|
||||||
pub fn set_prefix(&mut self, prefix: &str) {
|
|
||||||
let prefix = prefix.trim().trim_right_matches('/').to_owned();
|
|
||||||
let inner = Rc::get_mut(&mut self.defs).unwrap();
|
|
||||||
inner.prefix_len = prefix.len();
|
|
||||||
inner.prefix = prefix;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[inline]
|
#[inline]
|
||||||
pub(crate) fn route_info_params(&self, idx: u16, params: Params) -> ResourceInfo {
|
pub(crate) fn route_info_params(&self, idx: u16, params: Params) -> ResourceInfo {
|
||||||
ResourceInfo {
|
ResourceInfo {
|
||||||
params,
|
params,
|
||||||
|
prefix: 0,
|
||||||
router: self.defs.clone(),
|
router: self.defs.clone(),
|
||||||
resource: ResourceId::Normal(idx),
|
resource: ResourceId::Normal(idx),
|
||||||
}
|
}
|
||||||
@ -200,6 +189,7 @@ impl<S: 'static> Router<S> {
|
|||||||
|
|
||||||
ResourceInfo {
|
ResourceInfo {
|
||||||
params,
|
params,
|
||||||
|
prefix: 0,
|
||||||
router: self.defs.clone(),
|
router: self.defs.clone(),
|
||||||
resource: ResourceId::Default,
|
resource: ResourceId::Default,
|
||||||
}
|
}
|
||||||
@ -211,6 +201,7 @@ impl<S: 'static> Router<S> {
|
|||||||
params: Params::new(),
|
params: Params::new(),
|
||||||
router: self.defs.clone(),
|
router: self.defs.clone(),
|
||||||
resource: ResourceId::Default,
|
resource: ResourceId::Default,
|
||||||
|
prefix: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -391,6 +382,7 @@ impl<S: 'static> Router<S> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ResourceInfo {
|
ResourceInfo {
|
||||||
|
prefix: tail as u16,
|
||||||
params: Params::new(),
|
params: Params::new(),
|
||||||
router: self.defs.clone(),
|
router: self.defs.clone(),
|
||||||
resource: ResourceId::Default,
|
resource: ResourceId::Default,
|
||||||
@ -851,7 +843,6 @@ mod tests {
|
|||||||
#[test]
|
#[test]
|
||||||
fn test_recognizer_with_prefix() {
|
fn test_recognizer_with_prefix() {
|
||||||
let mut router = Router::<()>::new();
|
let mut router = Router::<()>::new();
|
||||||
router.set_prefix("/test");
|
|
||||||
router.register_resource(Resource::new(ResourceDef::new("/name")));
|
router.register_resource(Resource::new(ResourceDef::new("/name")));
|
||||||
router.register_resource(Resource::new(ResourceDef::new("/name/{val}")));
|
router.register_resource(Resource::new(ResourceDef::new("/name/{val}")));
|
||||||
|
|
||||||
@ -871,7 +862,6 @@ mod tests {
|
|||||||
|
|
||||||
// same patterns
|
// same patterns
|
||||||
let mut router = Router::<()>::new();
|
let mut router = Router::<()>::new();
|
||||||
router.set_prefix("/test2");
|
|
||||||
router.register_resource(Resource::new(ResourceDef::new("/name")));
|
router.register_resource(Resource::new(ResourceDef::new("/name")));
|
||||||
router.register_resource(Resource::new(ResourceDef::new("/name/{val}")));
|
router.register_resource(Resource::new(ResourceDef::new("/name/{val}")));
|
||||||
|
|
||||||
|
@ -543,11 +543,6 @@ mod tests {
|
|||||||
err: None,
|
err: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
fn feed_data(&mut self, data: &'static str) {
|
|
||||||
let mut b = BytesMut::from(self.buf.as_ref());
|
|
||||||
b.extend(data.as_bytes());
|
|
||||||
self.buf = b.take().freeze();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AsyncRead for Buffer {}
|
impl AsyncRead for Buffer {}
|
||||||
|
21
src/test.rs
21
src/test.rs
@ -417,6 +417,7 @@ pub struct TestRequest<S> {
|
|||||||
params: Params,
|
params: Params,
|
||||||
cookies: Option<Vec<Cookie<'static>>>,
|
cookies: Option<Vec<Cookie<'static>>>,
|
||||||
payload: Option<Payload>,
|
payload: Option<Payload>,
|
||||||
|
prefix: u16,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for TestRequest<()> {
|
impl Default for TestRequest<()> {
|
||||||
@ -430,6 +431,7 @@ impl Default for TestRequest<()> {
|
|||||||
params: Params::new(),
|
params: Params::new(),
|
||||||
cookies: None,
|
cookies: None,
|
||||||
payload: None,
|
payload: None,
|
||||||
|
prefix: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -467,6 +469,7 @@ impl<S: 'static> TestRequest<S> {
|
|||||||
params: Params::new(),
|
params: Params::new(),
|
||||||
cookies: None,
|
cookies: None,
|
||||||
payload: None,
|
payload: None,
|
||||||
|
prefix: 0,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -527,6 +530,12 @@ impl<S: 'static> TestRequest<S> {
|
|||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set request's prefix
|
||||||
|
pub fn prefix(mut self, prefix: u16) -> Self {
|
||||||
|
self.prefix = prefix;
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// Complete request creation and generate `HttpRequest` instance
|
/// Complete request creation and generate `HttpRequest` instance
|
||||||
pub fn finish(self) -> HttpRequest<S> {
|
pub fn finish(self) -> HttpRequest<S> {
|
||||||
let TestRequest {
|
let TestRequest {
|
||||||
@ -538,6 +547,7 @@ impl<S: 'static> TestRequest<S> {
|
|||||||
mut params,
|
mut params,
|
||||||
cookies,
|
cookies,
|
||||||
payload,
|
payload,
|
||||||
|
prefix,
|
||||||
} = self;
|
} = self;
|
||||||
let router = Router::<()>::new();
|
let router = Router::<()>::new();
|
||||||
|
|
||||||
@ -552,9 +562,10 @@ impl<S: 'static> TestRequest<S> {
|
|||||||
*inner.payload.borrow_mut() = payload;
|
*inner.payload.borrow_mut() = payload;
|
||||||
}
|
}
|
||||||
params.set_url(req.url().clone());
|
params.set_url(req.url().clone());
|
||||||
|
let mut info = router.route_info_params(0, params);
|
||||||
|
info.set_prefix(prefix);
|
||||||
|
|
||||||
let mut req =
|
let mut req = HttpRequest::new(req, Rc::new(state), info);
|
||||||
HttpRequest::new(req, Rc::new(state), router.route_info_params(0, params));
|
|
||||||
req.set_cookies(cookies);
|
req.set_cookies(cookies);
|
||||||
req
|
req
|
||||||
}
|
}
|
||||||
@ -571,6 +582,7 @@ impl<S: 'static> TestRequest<S> {
|
|||||||
mut params,
|
mut params,
|
||||||
cookies,
|
cookies,
|
||||||
payload,
|
payload,
|
||||||
|
prefix,
|
||||||
} = self;
|
} = self;
|
||||||
|
|
||||||
let pool = RequestPool::pool(ServerSettings::default());
|
let pool = RequestPool::pool(ServerSettings::default());
|
||||||
@ -584,8 +596,9 @@ impl<S: 'static> TestRequest<S> {
|
|||||||
*inner.payload.borrow_mut() = payload;
|
*inner.payload.borrow_mut() = payload;
|
||||||
}
|
}
|
||||||
params.set_url(req.url().clone());
|
params.set_url(req.url().clone());
|
||||||
let mut req =
|
let mut info = router.route_info_params(0, params);
|
||||||
HttpRequest::new(req, Rc::new(state), router.route_info_params(0, params));
|
info.set_prefix(prefix);
|
||||||
|
let mut req = HttpRequest::new(req, Rc::new(state), info);
|
||||||
req.set_cookies(cookies);
|
req.set_cookies(cookies);
|
||||||
req
|
req
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user