Skip to content

fix fallback search when a resource is not found #2690

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
Dec 16, 2024
Merged
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
83 changes: 49 additions & 34 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,30 +472,38 @@ pub(crate) async fn rustdoc_html_server_handler(
{
debug!("got error serving {}: {}", storage_path, err);
}
// If it fails, we try again with /index.html at the end
storage_path.push_str("/index.html");
req_path.push("index.html");

return if storage
.rustdoc_file_exists(
&params.name,
&krate.version.to_string(),
krate.latest_build_id,
&storage_path,
krate.archive_storage,
)
.await?
{
redirect(
&params.name,
&krate.version,
&req_path,
CachePolicy::ForeverInCdn,
)
} else if req_path.first().map_or(false, |p| p.contains('-')) {
// If it fails, we try again with /index.html at the end
let mut storage_path = storage_path.clone();
storage_path.push_str("/index.html");

let mut req_path = req_path.clone();
req_path.push("index.html");

if storage
.rustdoc_file_exists(
&params.name,
&krate.version.to_string(),
krate.latest_build_id,
&storage_path,
krate.archive_storage,
)
.await?
{
return redirect(
&params.name,
&krate.version,
&req_path,
CachePolicy::ForeverInCdn,
);
}
}

if req_path.first().map_or(false, |p| p.contains('-')) {
// This is a target, not a module; it may not have been built.
// Redirect to the default target and show a search page instead of a hard 404.
Ok(axum_cached_redirect(
return Ok(axum_cached_redirect(
encode_url_path(&format!(
"/crate/{}/{}/target-redirect/{}",
params.name,
Expand All @@ -504,21 +512,28 @@ pub(crate) async fn rustdoc_html_server_handler(
)),
CachePolicy::ForeverInCdn,
)?
.into_response())
} else {
if storage_path == format!("{}/index.html", krate.target_name.expect("we check rustdoc_status = true above, and with docs we have target_name")) {
error!(
krate = params.name,
version = krate.version.to_string(),
original_path = original_path.as_ref(),
storage_path,
"Couldn't find crate documentation root on storage.
Something is wrong with the build."
.into_response());
}

if storage_path
== format!(
"{}/index.html",
krate.target_name.expect(
"we check rustdoc_status = true above, and with docs we have target_name"
)
}
)
{
error!(
krate = params.name,
version = krate.version.to_string(),
original_path = original_path.as_ref(),
storage_path,
"Couldn't find crate documentation root on storage.
Something is wrong with the build."
)
}

Err(AxumNope::ResourceNotFound)
};
return Err(AxumNope::ResourceNotFound);
}
};

Expand Down Expand Up @@ -2899,7 +2914,7 @@ mod test {

web.assert_redirect_cached_unchecked(
"/clap/2.24.0/i686-pc-windows-gnu/clap/which%20is%20a%20part%20of%20%5B%60Display%60%5D",
"/crate/clap/2.24.0/target-redirect/i686-pc-windows-gnu/clap/which%20is%20a%20part%20of%20[%60Display%60]/index.html",
"/crate/clap/2.24.0/target-redirect/i686-pc-windows-gnu/clap/which%20is%20a%20part%20of%20[%60Display%60]",
CachePolicy::ForeverInCdn,
&env.config(),
).await?;
Expand Down
Loading