mirror of
https://github.com/servo/servo.git
synced 2025-07-25 08:10:21 +01:00
Improve and fix default_path cookie algorithm
* Previously, the function returned an owned String, which is not necessary, so now it returns a slice * Steps have now been documented/labeled * The last step of the algorithm was incorrect; it would only slice the path if the "/" was the last character, which is not what the spec says. The spec says to slice up until (but not including) the last "/". Also added a regression test for this.
This commit is contained in:
parent
5dd43bf84c
commit
679dd679a7
1 changed files with 16 additions and 9 deletions
|
@ -71,7 +71,7 @@ impl Cookie {
|
||||||
if path.is_empty() || path.char_at(0) != '/' {
|
if path.is_empty() || path.char_at(0) != '/' {
|
||||||
let url_path = request.serialize_path();
|
let url_path = request.serialize_path();
|
||||||
let url_path = url_path.as_ref().map(|path| &**path);
|
let url_path = url_path.as_ref().map(|path| &**path);
|
||||||
path = Cookie::default_path(url_path.unwrap_or(""));
|
path = Cookie::default_path(url_path.unwrap_or("")).to_owned();
|
||||||
}
|
}
|
||||||
cookie.path = Some(path);
|
cookie.path = Some(path);
|
||||||
|
|
||||||
|
@ -96,15 +96,21 @@ impl Cookie {
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://tools.ietf.org/html/rfc6265#section-5.1.4
|
// http://tools.ietf.org/html/rfc6265#section-5.1.4
|
||||||
fn default_path(request_path: &str) -> String {
|
fn default_path(request_path: &str) -> &str {
|
||||||
if request_path == "" || request_path.char_at(0) != '/' ||
|
// Step 2
|
||||||
request_path.chars().filter(|&c| c == '/').count() == 1 {
|
if request_path.is_empty() || !request_path.starts_with("/") {
|
||||||
"/".to_owned()
|
return "/";
|
||||||
} else if request_path.ends_with("/") {
|
|
||||||
request_path[..request_path.len() - 1].to_owned()
|
|
||||||
} else {
|
|
||||||
request_path.to_owned()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Step 3
|
||||||
|
let rightmost_slash_idx = request_path.rfind("/").unwrap();
|
||||||
|
if rightmost_slash_idx == 0 {
|
||||||
|
// There's only one slash; it's the first character
|
||||||
|
return "/";
|
||||||
|
}
|
||||||
|
|
||||||
|
// Step 4
|
||||||
|
&request_path[..rightmost_slash_idx]
|
||||||
}
|
}
|
||||||
|
|
||||||
// http://tools.ietf.org/html/rfc6265#section-5.1.4
|
// http://tools.ietf.org/html/rfc6265#section-5.1.4
|
||||||
|
@ -180,6 +186,7 @@ fn test_domain_match() {
|
||||||
#[test]
|
#[test]
|
||||||
fn test_default_path() {
|
fn test_default_path() {
|
||||||
assert!(&*Cookie::default_path("/foo/bar/baz/") == "/foo/bar/baz");
|
assert!(&*Cookie::default_path("/foo/bar/baz/") == "/foo/bar/baz");
|
||||||
|
assert!(&*Cookie::default_path("/foo/bar/baz") == "/foo/bar");
|
||||||
assert!(&*Cookie::default_path("/foo/") == "/foo");
|
assert!(&*Cookie::default_path("/foo/") == "/foo");
|
||||||
assert!(&*Cookie::default_path("/foo") == "/");
|
assert!(&*Cookie::default_path("/foo") == "/");
|
||||||
assert!(&*Cookie::default_path("/") == "/");
|
assert!(&*Cookie::default_path("/") == "/");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue