From 9cf3fef93b38575795e8109b5e70acfe9d4939b8 Mon Sep 17 00:00:00 2001 From: Joshua Nelson Date: Tue, 23 Mar 2021 16:06:56 -0400 Subject: [PATCH] Add hack so that docs from 2018-11-29 are styled correctly See comments in code. --- src/web/routes.rs | 2 +- src/web/rustdoc.rs | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/web/routes.rs b/src/web/routes.rs index 590da0cf6..37757c283 100644 --- a/src/web/routes.rs +++ b/src/web/routes.rs @@ -134,7 +134,7 @@ pub(super) fn build_routes() -> Routes { ); routes.rustdoc_page( "/:crate/:version/:target", - super::rustdoc::rustdoc_redirector_handler, + super::rustdoc::rustdoc_static_file_hack, ); routes.rustdoc_page( "/:crate/:version/:target/", diff --git a/src/web/rustdoc.rs b/src/web/rustdoc.rs index 918ea91e2..4afcfd45f 100644 --- a/src/web/rustdoc.rs +++ b/src/web/rustdoc.rs @@ -43,6 +43,19 @@ impl iron::Handler for RustLangRedirector { } } +pub(super) fn rustdoc_static_file_hack(req: &mut Request) -> IronResult { + // HACK(#1327): If the crate incorrectly used a relative link to `/:crate/:version/static-resource.css`, + // then this will return a redirect instead of a 404, so the `shared_files_handler` won't serve the right file. + // Try to avoid this by only serving a redirect for valid targets. + let router = extension!(req, Router); + let target = router.find("target").unwrap(); + if target.contains(|c: char| !(c.is_ascii_alphanumeric() || c == '_' || c == '-')) { + Err(Nope::ResourceNotFound.into()) + } else { + rustdoc_redirector_handler(req) + } +} + /// Handler called for `/:crate` and `/:crate/:version` URLs. Automatically redirects to the docs /// or crate details page based on whether the given crate version was successfully built. pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult { @@ -1736,4 +1749,16 @@ mod test { Ok(()) }) } + + #[test] + fn static_file_hack() { + wrapper(|env| { + let static_file = "/hexponent/0.3.1/rustdoc-20181129-1.32.0-nightly-3e90a12a8.css"; + let response = env.frontend().get(static_file).send()?; + // make sure there's no redirect + assert_eq!(response.url().path(), static_file); + assert_eq!(response.status(), StatusCode::NOT_FOUND); + Ok(()) + }) + } }