Skip to content

fixes #1409: internal server error with two slashes at the end of the URL #1410

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 2 commits into from
Jun 5, 2021
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
27 changes: 26 additions & 1 deletion src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ use iron::{
};
use page::TemplateData;
use postgres::Client;
use router::NoRoute;
use router::{NoRoute, TrailingSlash};
use semver::{Version, VersionReq};
use serde::Serialize;
use std::{borrow::Cow, fmt, net::SocketAddr, sync::Arc};
Expand Down Expand Up @@ -183,6 +183,17 @@ impl Handler for MainHandler {
self.shared_resource_handler
.handle(req)
.or_else(|e| if_404(e, || self.router_handler.handle(req)))
.or_else(|e| {
// in some cases the iron router will return a redirect as an `IronError`.
// Here we convert these into an `Ok(Response)`.
if e.error.downcast_ref::<TrailingSlash>().is_some()
|| e.response.status == Some(status::MovedPermanently)
{
Ok(e.response)
} else {
Err(e)
}
})
.or_else(|e| {
let err = if let Some(err) = e.error.downcast_ref::<error::Nope>() {
*err
Expand Down Expand Up @@ -740,6 +751,20 @@ mod test {
})
}

#[test]
fn double_slash_does_redirect_and_remove_slash() {
wrapper(|env| {
env.fake_release()
.name("bat")
.version("0.2.0")
.create()
.unwrap();
let web = env.frontend();
assert_redirect("/bat//", "/bat/0.2.0/bat/", web)?;
Ok(())
})
}

#[test]
fn binary_docs_redirect_to_crate() {
wrapper(|env| {
Expand Down