-
Notifications
You must be signed in to change notification settings - Fork 209
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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}; | ||
|
@@ -167,6 +167,18 @@ impl Handler for MainHandler { | |
} | ||
} | ||
|
||
fn pass_iron_errors_with_redirect(e: IronError) -> IronResult<Response> { | ||
// 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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why will there still be a response if there's an error? Also, why does this also handle MovedPermanently? That seems like a change in behavior. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure I follow. In our own code we do redirects by returning an For this specific case (URL without slash at the end has a match, see the iron source), iron returns an Since for us the redirect is fine, and not an error, I want to convert this. ( of course I could be missing something) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, I think I see - we have our own error handler on top of iron's built-in error handlers. And our error handler expects to recognized every possible error, that's what the last |
||
} else { | ||
Err(e) | ||
} | ||
} | ||
|
||
// This is kind of a mess. | ||
// | ||
// Almost all files should be served through the `router_handler`; eventually | ||
|
@@ -183,6 +195,7 @@ impl Handler for MainHandler { | |
self.shared_resource_handler | ||
.handle(req) | ||
.or_else(|e| if_404(e, || self.router_handler.handle(req))) | ||
.or_else(pass_iron_errors_with_redirect) | ||
.or_else(|e| { | ||
let err = if let Some(err) = e.error.downcast_ref::<error::Nope>() { | ||
*err | ||
|
@@ -740,6 +753,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| { | ||
|
Uh oh!
There was an error while loading. Please reload this page.