Skip to content

Commit 3ae6893

Browse files
Nemo157Joshua Nelson
authored andcommitted
Remove target_name from search redirect, and deal with redirecting with a specified target
1 parent b09bcf0 commit 3ae6893

File tree

1 file changed

+29
-17
lines changed

1 file changed

+29
-17
lines changed

src/web/rustdoc.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,21 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
7878
req: &Request,
7979
name: &str,
8080
vers: &str,
81+
target: Option<&str>,
8182
target_name: &str,
8283
) -> IronResult<Response> {
83-
let mut url_str = format!("{}/{}/{}/{}/", redirect_base(req), name, vers, target_name,);
84+
let mut url_str = if let Some(target) = target {
85+
format!(
86+
"{}/{}/{}/{}/{}/",
87+
redirect_base(req),
88+
name,
89+
vers,
90+
target,
91+
target_name
92+
)
93+
} else {
94+
format!("{}/{}/{}/{}/", redirect_base(req), name, vers, target_name)
95+
};
8496
if let Some(query) = req.url.query() {
8597
url_str.push('?');
8698
url_str.push_str(query);
@@ -149,6 +161,7 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
149161
.unwrap_or_else(|_| crate_name.into())
150162
.into_owned();
151163
let req_version = router.find("version");
164+
let mut target = router.find("target");
152165

153166
let conn = extension!(req, Pool).get();
154167

@@ -173,16 +186,20 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
173186
let (target_name, has_docs): (String, bool) = {
174187
let rows = ctry!(conn.query(
175188
"SELECT target_name, rustdoc_status
176-
FROM releases
177-
WHERE releases.id = $1",
189+
FROM releases
190+
WHERE releases.id = $1",
178191
&[&id]
179192
));
180193

181194
(rows.get(0).get(0), rows.get(0).get(1))
182195
};
183196

197+
if target == Some("index.html") || target == Some(&target_name) {
198+
target = None;
199+
}
200+
184201
if has_docs {
185-
redirect_to_doc(req, &crate_name, &version, &target_name)
202+
redirect_to_doc(req, &crate_name, &version, target, &target_name)
186203
} else {
187204
redirect_to_crate(req, &crate_name, &version)
188205
}
@@ -351,14 +368,12 @@ fn path_for_version(req_path: &[&str], known_platforms: &[String], conn: &Connec
351368
.expect("paths should be of the form <kind>.<name>.html")
352369
};
353370
// check if req_path[3] is the platform choice or the name of the crate
354-
let concat_path;
355-
let crate_root = if known_platforms.iter().any(|s| s == req_path[3]) && req_path.len() >= 5 {
356-
concat_path = format!("{}/{}", req_path[3], req_path[4]);
357-
&concat_path
358-
} else {
371+
let platform = if known_platforms.iter().any(|s| s == req_path[3]) && req_path.len() >= 5 {
359372
req_path[3]
373+
} else {
374+
""
360375
};
361-
format!("{}?search={}", crate_root, search_item)
376+
format!("{}?search={}", platform, search_item)
362377
}
363378

364379
pub fn target_redirect_handler(req: &mut Request) -> IronResult<Response> {
@@ -641,13 +656,12 @@ mod test {
641656
latest_version_redirect("/dummy/0.1.0/dummy/struct.will-be-deleted.html", &web)?;
642657
// This must be a double redirect to deal with crates that failed to build in the
643658
// latest version
644-
assert_eq!(redirect, "/dummy/0.2.0/dummy?search=will-be-deleted");
659+
assert_eq!(redirect, "/dummy/0.2.0/?search=will-be-deleted");
645660
assert_redirect(
646-
"/dummy/0.2.0/dummy?search=will-be-deleted",
661+
&redirect,
647662
"/dummy/0.2.0/dummy/?search=will-be-deleted",
648663
&web,
649-
)
650-
.unwrap();
664+
)?;
651665

652666
Ok(())
653667
})
@@ -1041,8 +1055,6 @@ mod test {
10411055
.add_target("x86_64-pc-windows-msvc")
10421056
.create()?;
10431057

1044-
// For top-level items on non-default platforms we redirect to the target-specific doc
1045-
// root as the top-level items can't know which target they're for
10461058
assert_platform_links(
10471059
web,
10481060
"/dummy/0.4.0/settings.html",
@@ -1097,7 +1109,7 @@ mod test {
10971109
&[
10981110
(
10991111
"x86_64-pc-windows-msvc",
1100-
"/dummy/0.4.0/x86_64-pc-windows-msvc/dummy?search=DefaultOnly",
1112+
"/dummy/0.4.0/x86_64-pc-windows-msvc/dummy/?search=DefaultOnly",
11011113
),
11021114
(
11031115
"x86_64-unknown-linux-gnu",

0 commit comments

Comments
 (0)