1
0
Fork 0
mirror of https://github.com/actix/actix-web.git synced 2024-11-23 18:11:11 +00:00

calculate prefix dynamicly

This commit is contained in:
Nikolay Kim 2018-07-15 16:24:22 +06:00
parent b759dddf5a
commit 42d3e86941
4 changed files with 38 additions and 37 deletions

View file

@ -462,14 +462,16 @@ mod tests {
let mut resource = Resource::new(ResourceDef::new("/user/{name}.html"));
resource.name("index");
let mut router = Router::<()>::new();
router.set_prefix("/prefix/");
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("/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);
let url = req.url_for("index", &["test"]);
assert_eq!(
@ -483,14 +485,15 @@ mod tests {
let mut resource = Resource::new(ResourceDef::new("/index.html"));
resource.name("index");
let mut router = Router::<()>::new();
router.set_prefix("/prefix/");
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("/prefix/index.html"));
let req = TestRequest::default()
let req = TestRequest::with_uri("/prefix/test")
.prefix(7)
.header(header::HOST, "www.rust-lang.org")
.finish_with_router(router);
let url = req.url_for_static("index");

View file

@ -49,6 +49,7 @@ pub struct ResourceInfo {
router: Rc<Inner>,
resource: ResourceId,
params: Params,
prefix: u16,
}
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.
///
/// Params is a container for url parameters.
@ -91,6 +96,7 @@ impl ResourceInfo {
p.add(item.0.clone(), item.1.clone());
}
self.prefix = info.params.tail;
self.params = p;
}
@ -106,7 +112,8 @@ impl ResourceInfo {
I: AsRef<str>,
{
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('/') {
let conn = req.connection_info();
Ok(Url::parse(&format!(
@ -142,8 +149,6 @@ impl ResourceInfo {
}
struct Inner {
prefix: String,
prefix_len: usize,
named: HashMap<String, ResourceDef>,
patterns: Vec<ResourceDef>,
}
@ -158,8 +163,6 @@ impl<S: 'static> Router<S> {
pub(crate) fn new() -> Self {
Router {
defs: Rc::new(Inner {
prefix: String::new(),
prefix_len: 0,
named: HashMap::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]
pub(crate) fn route_info_params(&self, idx: u16, params: Params) -> ResourceInfo {
ResourceInfo {
params,
prefix: 0,
router: self.defs.clone(),
resource: ResourceId::Normal(idx),
}
@ -200,6 +189,7 @@ impl<S: 'static> Router<S> {
ResourceInfo {
params,
prefix: 0,
router: self.defs.clone(),
resource: ResourceId::Default,
}
@ -211,6 +201,7 @@ impl<S: 'static> Router<S> {
params: Params::new(),
router: self.defs.clone(),
resource: ResourceId::Default,
prefix: 0,
}
}
@ -391,6 +382,7 @@ impl<S: 'static> Router<S> {
}
}
ResourceInfo {
prefix: tail as u16,
params: Params::new(),
router: self.defs.clone(),
resource: ResourceId::Default,
@ -851,7 +843,6 @@ mod tests {
#[test]
fn test_recognizer_with_prefix() {
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/{val}")));
@ -871,7 +862,6 @@ mod tests {
// same patterns
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/{val}")));

View file

@ -543,11 +543,6 @@ mod tests {
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 {}

View file

@ -417,6 +417,7 @@ pub struct TestRequest<S> {
params: Params,
cookies: Option<Vec<Cookie<'static>>>,
payload: Option<Payload>,
prefix: u16,
}
impl Default for TestRequest<()> {
@ -430,6 +431,7 @@ impl Default for TestRequest<()> {
params: Params::new(),
cookies: None,
payload: None,
prefix: 0,
}
}
}
@ -467,6 +469,7 @@ impl<S: 'static> TestRequest<S> {
params: Params::new(),
cookies: None,
payload: None,
prefix: 0,
}
}
@ -527,6 +530,12 @@ impl<S: 'static> TestRequest<S> {
self
}
/// Set request's prefix
pub fn prefix(mut self, prefix: u16) -> Self {
self.prefix = prefix;
self
}
/// Complete request creation and generate `HttpRequest` instance
pub fn finish(self) -> HttpRequest<S> {
let TestRequest {
@ -538,6 +547,7 @@ impl<S: 'static> TestRequest<S> {
mut params,
cookies,
payload,
prefix,
} = self;
let router = Router::<()>::new();
@ -552,9 +562,10 @@ impl<S: 'static> TestRequest<S> {
*inner.payload.borrow_mut() = payload;
}
params.set_url(req.url().clone());
let mut info = router.route_info_params(0, params);
info.set_prefix(prefix);
let mut req =
HttpRequest::new(req, Rc::new(state), router.route_info_params(0, params));
let mut req = HttpRequest::new(req, Rc::new(state), info);
req.set_cookies(cookies);
req
}
@ -571,6 +582,7 @@ impl<S: 'static> TestRequest<S> {
mut params,
cookies,
payload,
prefix,
} = self;
let pool = RequestPool::pool(ServerSettings::default());
@ -584,8 +596,9 @@ impl<S: 'static> TestRequest<S> {
*inner.payload.borrow_mut() = payload;
}
params.set_url(req.url().clone());
let mut req =
HttpRequest::new(req, Rc::new(state), router.route_info_params(0, params));
let mut info = 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
}