-
Notifications
You must be signed in to change notification settings - Fork 56
Normalize the request path #90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
use iron::Request; | ||
use std::path::{PathBuf, Path}; | ||
use std::iter::FromIterator; | ||
use std::path::{Component, PathBuf, Path}; | ||
use std::fs::{self, Metadata}; | ||
use std::convert::AsRef; | ||
use url::percent_encoding::percent_decode; | ||
|
@@ -13,15 +14,27 @@ fn decode_percents(string: &&str) -> String { | |
percent_decode(string.as_bytes()).decode_utf8().unwrap().into_owned() | ||
} | ||
|
||
fn normalize_path(path: &Path) -> PathBuf { | ||
path.components().fold(PathBuf::new(), |mut result, p| { | ||
match p { | ||
Component::Normal(x) => { | ||
result.push(x); | ||
result | ||
} | ||
Component::ParentDir => { | ||
result.pop(); | ||
result | ||
}, | ||
_ => result | ||
} | ||
}) | ||
} | ||
|
||
impl RequestedPath { | ||
pub fn new<P: AsRef<Path>>(root_path: P, request: &Request) -> RequestedPath { | ||
let decoded_req_path = PathBuf::from_iter(request.url.path().iter().map(decode_percents)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can just join the request path with the root here, and check if the resulting path is a subpath of the root. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh and a call to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you for the review. Doesn't This PR's approach is inspired by Python's simple builtin httpserver, available via There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Something like this: https://play.rust-lang.org/?gist=ce54238276ab1a9058a0e4fffcde560e&version=stable&backtrace=0 This also fails if the file doesn't exist, which is fine in our case. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If we don't have permission for a file it is nonexistent for our purposes IMO. (BTW the behavior for "Permission Denied" is wrong IMO, bad file permissions rather implies a 500 Internal Server Error. But 404 is fine too I think) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I meant: Suppose we have the following file system:
... and then serve static files from "/web-server-root". In this case, the request, I do not have a strong opinion either. :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. According to servo/rust-url#229 rust-url should normalize such path segments. Haven't tried it though. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thank you. The problem here is that That's the reason we have to normalize path again here after percent-decoding, I am afraid. |
||
let mut result = root_path.as_ref().to_path_buf(); | ||
let path = request.url.path(); | ||
let decoded_req_path = path.iter() | ||
.map(decode_percents) | ||
.filter(|x| x.len() != 0); | ||
result.extend(decoded_req_path); | ||
|
||
result.extend(&normalize_path(&decoded_req_path)); | ||
RequestedPath { path: result } | ||
} | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have tried to find an API to normalize path, but it looks Rust does not have such an API.
ref. https://github.com/rust-lang/rfcs/blob/master/text/0474-path-reform.md