Skip to content

Commit bf5fecc

Browse files
jyn514Joshua Nelson
authored andcommitted
Redirect when missing a trailing slash
1 parent cefdebb commit bf5fecc

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

src/web/rustdoc.rs

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,11 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
305305
path.push_str("/index.html");
306306
req_path.push("index.html");
307307

308-
File::from_path(&storage, &path, &config)
309-
.map_err(|_| IronError::new(Nope::ResourceNotFound, status::NotFound))?
308+
return if ctry!(req, storage.exists(&path)) {
309+
redirect(&name, &version, &req_path[3..])
310+
} else {
311+
Err(IronError::new(Nope::ResourceNotFound, status::NotFound))
312+
};
310313
};
311314

312315
// Serve non-html files directly
@@ -1387,7 +1390,7 @@ mod test {
13871390

13881391
#[test]
13891392
// regression test for https://github.com/rust-lang/docs.rs/issues/856
1390-
fn test_no_trailing_slash() {
1393+
fn test_no_trailing_target_slash() {
13911394
wrapper(|env| {
13921395
env.fake_release().name("dummy").version("0.1.0").create()?;
13931396
let web = env.frontend();
@@ -1493,4 +1496,20 @@ mod test {
14931496
Ok(())
14941497
})
14951498
}
1499+
1500+
#[test]
1501+
fn test_no_trailing_rustdoc_slash() {
1502+
wrapper(|env| {
1503+
env.fake_release()
1504+
.name("tokio")
1505+
.version("0.2.21")
1506+
.rustdoc_file("tokio/time/index.html", b"content")
1507+
.create()?;
1508+
assert_redirect(
1509+
"/tokio/0.2.21/tokio/time",
1510+
"/tokio/0.2.21/tokio/time/index.html",
1511+
env.frontend(),
1512+
)
1513+
})
1514+
}
14961515
}

0 commit comments

Comments
 (0)