Skip to content

Retain queries and convert :: paths on redirects to rust-lang.org #1816

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 1 commit into from
Sep 9, 2022
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
7 changes: 7 additions & 0 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -750,6 +750,13 @@ mod test {
assert_redirect("/rustdoc", target, web)?;
assert_redirect("/rustdoc/", target, web)?;

// queries are supported
assert_redirect(
"/std?search=foobar",
"https://doc.rust-lang.org/stable/std/?search=foobar",
web,
)?;

Ok(())
})
}
Expand Down
41 changes: 0 additions & 41 deletions src/web/routes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,6 @@ use iron::{
use router::Router;
use std::collections::HashSet;

pub(super) const DOC_RUST_LANG_ORG_REDIRECTS: &[&str] =
&["alloc", "core", "proc_macro", "std", "test"];

// REFACTOR: Break this into smaller initialization functions
pub(super) fn build_routes() -> Routes {
let mut routes = Routes::new();
Expand Down Expand Up @@ -168,44 +165,6 @@ pub(super) fn build_routes() -> Routes {
super::rustdoc::rustdoc_html_server_handler,
);

for redirect in DOC_RUST_LANG_ORG_REDIRECTS {
routes.internal_page(
&format!("/{}", redirect),
super::rustdoc::RustLangRedirector::new("stable", redirect),
);
routes.internal_page(
&format!("/{}/", redirect),
super::rustdoc::RustLangRedirector::new("stable", redirect),
);
}
// redirect proc-macro to proc_macro
routes.internal_page(
"/proc-macro",
super::rustdoc::RustLangRedirector::new("stable", "proc_macro"),
);
routes.internal_page(
"/proc-macro/",
super::rustdoc::RustLangRedirector::new("stable", "proc_macro"),
);
// redirect rustc to nightly rustc docs
routes.internal_page(
"/rustc",
super::rustdoc::RustLangRedirector::new("nightly", "nightly-rustc"),
);
routes.internal_page(
"/rustc/",
super::rustdoc::RustLangRedirector::new("nightly", "nightly-rustc"),
);
// redirect rustdoc to nightly rustdoc docs
routes.internal_page(
"/rustdoc",
super::rustdoc::RustLangRedirector::new("nightly", "nightly-rustc/rustdoc"),
);
routes.internal_page(
"/rustdoc/",
super::rustdoc::RustLangRedirector::new("nightly", "nightly-rustc/rustdoc"),
);

routes
}

Expand Down
87 changes: 40 additions & 47 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,54 +18,33 @@ use iron::{
status, Handler, IronResult, Request, Response, Url,
};
use lol_html::errors::RewritingError;
use once_cell::sync::Lazy;
use router::Router;
use serde::Serialize;
use std::{fmt::Write, path::Path};

#[derive(Clone)]
pub struct RustLangRedirector {
url: Url,
}

impl RustLangRedirector {
pub fn new(version: &str, target: &str) -> Self {
let url = iron::url::Url::parse(&format!("https://doc.rust-lang.org/{version}/{target}/"))
.expect("failed to parse rust-lang.org doc URL");
let url = Url::from_generic_url(url).expect("failed to convert url::Url to iron::Url");

Self { url }
}
}

impl iron::Handler for RustLangRedirector {
fn handle(&self, _req: &mut Request) -> IronResult<Response> {
Ok(Response::with((status::Found, Redirect(self.url.clone()))))
}
}
use std::{collections::HashMap, fmt::Write, path::Path};

static DOC_RUST_LANG_ORG_REDIRECTS: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
HashMap::from([
("alloc", "stable/alloc"),
("core", "stable/core"),
("proc_macro", "stable/proc_macro"),
("proc-macro", "stable/proc_macro"),
("std", "stable/std"),
("test", "stable/test"),
("rustc", "nightly/nightly-rustc"),
("rustdoc", "nightly/nightly-rustc/rustdoc"),
])
});

/// 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<Response> {
fn redirect_to_doc(
req: &Request,
name: &str,
vers: &str,
target: Option<&str>,
target_name: &str,
mut url_str: String,
permanent: bool,
path_in_crate: Option<&str>,
) -> IronResult<Response> {
let mut url_str = if let Some(target) = target {
format!(
"{}/{}/{}/{}/{}/",
redirect_base(req),
name,
vers,
target,
target_name
)
} else {
format!("{}/{}/{}/{}/", redirect_base(req), name, vers, target_name)
};
if let Some(query) = req.url.query() {
url_str.push('?');
url_str.push_str(query);
Expand All @@ -74,7 +53,7 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
url_str.push_str(path);
}
let url = ctry!(req, Url::parse(&url_str));
let (status_code, max_age) = if vers == "latest" {
let (status_code, max_age) = if permanent {
(status::MovedPermanently, 86400)
} else {
(status::Found, 0)
Expand Down Expand Up @@ -155,6 +134,12 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
Some((krate, path)) => (krate.to_string(), Some(path.to_string())),
None => (crate_name.to_string(), None),
};

if let Some(inner_path) = DOC_RUST_LANG_ORG_REDIRECTS.get(crate_name.as_str()) {
Copy link
Member

Choose a reason for hiding this comment

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

it feels strange to put this into the rustdoc-handler, in theory we could refactor the shared behaviour into separate methods and then have separate handlers.

But since axum forbids overlapping routes contrary to iron which just takes the first matching route, we would have to merge the handlers anyways to a certain extend.

So this is OK with me.

let url = format!("https://doc.rust-lang.org/{inner_path}/");
return redirect_to_doc(req, url, false, path_in_crate.as_deref());
}

let req_version = router.find("version");
let mut target = router.find("target");

Expand Down Expand Up @@ -196,14 +181,15 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {

if has_docs {
rendering_time.step("redirect to doc");
redirect_to_doc(
req,
&crate_name,
&version,
target,
&target_name,
path_in_crate.as_deref(),
)

let base = redirect_base(req);
let url_str = if let Some(target) = target {
format!("{base}/{crate_name}/{version}/{target}/{target_name}/")
} else {
format!("{base}/{crate_name}/{version}/{target_name}/")
};

redirect_to_doc(req, url_str, version == "latest", path_in_crate.as_deref())
} else {
rendering_time.step("redirect to crate");
redirect_to_crate(req, &crate_name, &version)
Expand Down Expand Up @@ -1730,6 +1716,13 @@ mod test {
"/some_random_crate/latest/some_random_crate/?search=some::path",
web,
)?;

assert_redirect(
"/std::some::path",
"https://doc.rust-lang.org/stable/std/?search=some::path",
web,
)?;

Ok(())
})
}
Expand Down