Skip to content

Improve handling of urls missing / #584

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

Merged
merged 3 commits into from
Feb 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ impl CratesfyiHandler {

let shared_resources = Self::chain(&pool_factory, rustdoc::SharedResourceHandler);
let router_chain = Self::chain(&pool_factory, routes.iron_router());
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX").unwrap()).join("public_html");
let prefix = PathBuf::from(env::var("CRATESFYI_PREFIX").expect("the CRATESFYI_PREFIX environment variable is not set")).join("public_html");
let static_handler = Static::new(prefix)
.cache(Duration::from_secs(STATIC_FILE_CACHE_DURATION));

Expand Down
24 changes: 21 additions & 3 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
return Ok(super::redirect(canonical));
}

let path = {
let mut path = {
let mut path = req_path.join("/");
if path.ends_with('/') {
req_path.pop(); // get rid of empty string
Expand All @@ -250,7 +250,15 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {

let file = match File::from_path(&conn, &path) {
Some(f) => f,
None => return Err(IronError::new(Nope::ResourceNotFound, status::NotFound)),
None => {
// If it fails, we try again with /index.html at the end
path.push_str("/index.html");
req_path.push("index.html");
match File::from_path(&conn, &path) {
Some(f) => f,
None => return Err(IronError::new(Nope::ResourceNotFound, status::NotFound)),
}
},
};

// serve file directly if it's not html
Expand Down Expand Up @@ -439,7 +447,11 @@ mod test {
.name("buggy").version("0.1.0")
.build_result_successful(true)
.rustdoc_file("settings.html", b"some data")
.rustdoc_file("directory_1/index.html", b"some data 1")
.rustdoc_file("directory_2.html/index.html", b"some data 1")
.rustdoc_file("all.html", b"some data 2")
.rustdoc_file("directory_3/.gitignore", b"*.ext")
.rustdoc_file("directory_4/empty_file_no_ext", b"")
.create()?;
db.fake_release()
.name("buggy").version("0.2.0")
Expand All @@ -448,8 +460,12 @@ mod test {
let web = env.frontend();
assert_success("/", web)?;
assert_success("/crate/buggy/0.1.0/", web)?;
assert_success("/buggy/0.1.0/directory_1/index.html", web)?;
assert_success("/buggy/0.1.0/directory_2.html/index.html", web)?;
assert_success("/buggy/0.1.0/directory_3/.gitignore", web)?;
assert_success("/buggy/0.1.0/settings.html", web)?;
assert_success("/buggy/0.1.0/all.html", web)?;
assert_success("/buggy/0.1.0/directory_4/empty_file_no_ext", web)?;
Ok(())
});
}
Expand Down Expand Up @@ -488,7 +504,9 @@ mod test {
assert_success(base, web)?;
assert_redirect("/dummy/0.3.0/x86_64-unknown-linux-gnu/dummy/", base, web)?;
assert_redirect("/dummy/0.3.0/x86_64-unknown-linux-gnu/all.html", "/dummy/0.3.0/all.html", web)?;
assert_redirect("/dummy/0.3.0/", base, web)
assert_redirect("/dummy/0.3.0/", base, web)?;
assert_redirect("/dummy/0.3.0/index.html",base, web)?;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch! I forgot about this when I added the test the first time.

Ok(())
});
}
}