1
0
mirror of https://github.com/fafhrd91/actix-web synced 2024-11-24 08:22:59 +01:00

escape router pattern re

This commit is contained in:
Nikolay Kim 2018-02-19 14:57:57 -08:00
parent ddc82395e8
commit 4d81186059
2 changed files with 16 additions and 8 deletions

View File

@ -383,11 +383,13 @@ 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 router.has_route(&p) { if router.has_route(p) {
let p = p.to_owned(); let mut req = HttpResponse::build(self.redirect);
let p = if !query.is_empty() { p + "?" + query } else { p }; return if !query.is_empty() {
return HttpResponse::build(self.redirect) req.header(header::LOCATION, (p.to_owned() + "?" + query).as_str())
.header(header::LOCATION, p.as_str()) } else {
req.header(header::LOCATION, p)
}
.body(Body::Empty); .body(Body::Empty);
} }
} }

View File

@ -3,7 +3,7 @@ use std::rc::Rc;
use std::hash::{Hash, Hasher}; use std::hash::{Hash, Hasher};
use std::collections::HashMap; use std::collections::HashMap;
use regex::{Regex, RegexSet}; use regex::{Regex, RegexSet, escape};
use error::UrlGenerationError; use error::UrlGenerationError;
use resource::Resource; use resource::Resource;
@ -299,7 +299,7 @@ impl Pattern {
elems.push(PatternElement::Str(el.clone())); elems.push(PatternElement::Str(el.clone()));
el.clear(); el.clear();
} else { } else {
re.push(ch); re.push_str(escape(&ch.to_string()).as_str());
el.push(ch); el.push(ch);
} }
} }
@ -336,6 +336,7 @@ mod tests {
routes.insert(Pattern::new("", "/name/{val}", "^/"), Some(Resource::default())); routes.insert(Pattern::new("", "/name/{val}", "^/"), Some(Resource::default()));
routes.insert(Pattern::new("", "/name/{val}/index.html", "^/"), routes.insert(Pattern::new("", "/name/{val}/index.html", "^/"),
Some(Resource::default())); Some(Resource::default()));
routes.insert(Pattern::new("", "/file/{file}.{ext}", "^/"), Some(Resource::default()));
routes.insert(Pattern::new("", "/v{val}/{val2}/index.html", "^/"), routes.insert(Pattern::new("", "/v{val}/{val2}/index.html", "^/"),
Some(Resource::default())); Some(Resource::default()));
routes.insert(Pattern::new("", "/v/{tail:.*}", "^/"), Some(Resource::default())); routes.insert(Pattern::new("", "/v/{tail:.*}", "^/"), Some(Resource::default()));
@ -355,6 +356,11 @@ mod tests {
assert!(rec.recognize(&mut req).is_some()); assert!(rec.recognize(&mut req).is_some());
assert_eq!(req.match_info().get("val").unwrap(), "value2"); assert_eq!(req.match_info().get("val").unwrap(), "value2");
let mut req = TestRequest::with_uri("/file/file.gz").finish();
assert!(rec.recognize(&mut req).is_some());
assert_eq!(req.match_info().get("file").unwrap(), "file");
assert_eq!(req.match_info().get("ext").unwrap(), "gz");
let mut req = TestRequest::with_uri("/vtest/ttt/index.html").finish(); let mut req = TestRequest::with_uri("/vtest/ttt/index.html").finish();
assert!(rec.recognize(&mut req).is_some()); assert!(rec.recognize(&mut req).is_some());
assert_eq!(req.match_info().get("val").unwrap(), "test"); assert_eq!(req.match_info().get("val").unwrap(), "test");