From 08526f9c87b5225957886dbdf1498c11959b348a Mon Sep 17 00:00:00 2001 From: Zexbe Date: Sat, 1 Feb 2020 19:01:55 -0800 Subject: [PATCH 1/3] Make missing CRATESFYI_PREFIX easier to debug --- src/web/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/web/mod.rs b/src/web/mod.rs index cf35f8336..985705bfb 100644 --- a/src/web/mod.rs +++ b/src/web/mod.rs @@ -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("CRATESFYI_PREFIX environment variable does not exists")).join("public_html"); let static_handler = Static::new(prefix) .cache(Duration::from_secs(STATIC_FILE_CACHE_DURATION)); From 777b37cad5ce0812a184f6dcf6603e85ced3cd26 Mon Sep 17 00:00:00 2001 From: Zexbe Date: Sat, 1 Feb 2020 19:03:02 -0800 Subject: [PATCH 2/3] Fallback in more situations without baskslash Try to make links like this work http://127.0.0.1:3000/hyper/0.13.2/hyper/client/conn --- src/web/rustdoc.rs | 19 +++++++++++++------ 1 file changed, 13 insertions(+), 6 deletions(-) diff --git a/src/web/rustdoc.rs b/src/web/rustdoc.rs index 07492fd62..d560e20be 100644 --- a/src/web/rustdoc.rs +++ b/src/web/rustdoc.rs @@ -238,15 +238,18 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult { return Ok(super::redirect(canonical)); } - let path = { - let mut path = req_path.join("/"); - if path.ends_with('/') { + if let Some(&end) = req_path.last(){ + if end == "" { req_path.pop(); // get rid of empty string - path.push_str("index.html"); req_path.push("index.html"); + } else if !end.contains('.') { + req_path.push("index.html"); + } else { + // page might already have extension, so do nothing } - path - }; + } + + let path = req_path.join("/"); let file = match File::from_path(&conn, &path) { Some(f) => f, @@ -440,6 +443,7 @@ mod test { .build_result_successful(true) .rustdoc_file("settings.html", b"some data") .rustdoc_file("all.html", b"some data 2") + .rustdoc_file("some_module/other_module/index.html", b"some data 2") .create()?; db.fake_release() .name("buggy").version("0.2.0") @@ -448,8 +452,10 @@ mod test { let web = env.frontend(); assert_success("/", web)?; assert_success("/crate/buggy/0.1.0/", web)?; + assert_success("/crate/buggy/0.1.0/", 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/some_module/other_module", web)?; Ok(()) }); } @@ -484,6 +490,7 @@ mod test { .rustdoc_file("dummy/index.html", b"some content") .rustdoc_file("all.html", b"html") .default_target(target).create()?; + let base = "/dummy/0.3.0/dummy/"; assert_success(base, web)?; assert_redirect("/dummy/0.3.0/x86_64-unknown-linux-gnu/dummy/", base, web)?; From d561b6c808c9ff87ef52f085168e39c22d48c6da Mon Sep 17 00:00:00 2001 From: Zexbe Date: Sun, 2 Feb 2020 01:18:32 -0800 Subject: [PATCH 3/3] Fix, and Fallback with ext-less files --- src/web/rustdoc.rs | 45 ++++++++++++++++++++++++--------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/src/web/rustdoc.rs b/src/web/rustdoc.rs index d560e20be..deebfaf24 100644 --- a/src/web/rustdoc.rs +++ b/src/web/rustdoc.rs @@ -20,6 +20,7 @@ use time; use iron::Handler; use utils; +const DIR_DEFAULT_FILE: &str = "index.html"; #[derive(Debug)] struct RustdocPage { @@ -238,29 +239,28 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult { return Ok(super::redirect(canonical)); } - if let Some(&end) = req_path.last(){ - if end == "" { - req_path.pop(); // get rid of empty string - req_path.push("index.html"); - } else if !end.contains('.') { - req_path.push("index.html"); - } else { - // page might already have extension, so do nothing - } + let mut path = req_path.join("/"); + + //If it is a directory, we default to looking + if path.ends_with("/") { + path.push_str(DIR_DEFAULT_FILE); + req_path.push(DIR_DEFAULT_FILE); } - let path = req_path.join("/"); + if !path.ends_with(".html") { + if let Some(file) = File::from_path(&conn, &path) { + return Ok(file.serve()); + }; + path.push_str("/"); + path.push_str(DIR_DEFAULT_FILE); + req_path.push(DIR_DEFAULT_FILE); + }; let file = match File::from_path(&conn, &path) { - Some(f) => f, + Some(file) => file, None => return Err(IronError::new(Nope::ResourceNotFound, status::NotFound)), }; - // serve file directly if it's not html - if !path.ends_with(".html") { - return Ok(file.serve()); - } - let mut content = RustdocPage::default(); let file_content = ctry!(String::from_utf8(file.0.content)); @@ -318,7 +318,7 @@ fn path_for_version(req_path: &[&str], target_name: &str, conn: &Connection) -> return req_path[3..].join("/"); } // this page doesn't exist in the latest version - let search_item = if *req_path.last().unwrap() == "index.html" { + let search_item = if *req_path.last().unwrap() == DIR_DEFAULT_FILE { // this is a module req_path[req_path.len() - 2] } else { @@ -441,9 +441,11 @@ mod test { db.fake_release() .name("buggy").version("0.1.0") .build_result_successful(true) - .rustdoc_file("settings.html", b"some data") + .rustdoc_file("settings.html", b"some setting data") .rustdoc_file("all.html", b"some data 2") - .rustdoc_file("some_module/other_module/index.html", b"some data 2") + .rustdoc_file("file_without_ext", b"some data 3") + .rustdoc_file("some_module/directory/index.html", b"some data 3") + .rustdoc_file("some_module/file_without_ext", b"some data 3") .create()?; db.fake_release() .name("buggy").version("0.2.0") @@ -451,11 +453,12 @@ mod test { .create()?; let web = env.frontend(); assert_success("/", web)?; - assert_success("/crate/buggy/0.1.0/", web)?; + assert_success("/crate/buggy/0.1.0", web)?; assert_success("/crate/buggy/0.1.0/", 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/some_module/other_module", web)?; + assert_success("/buggy/0.1.0/some_module/file_without_ext", web)?; + assert_success("/buggy/0.1.0/some_module/directory", web)?; Ok(()) }); }