Skip to content

Fix http redirects #365

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
Jun 4, 2019
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
8 changes: 3 additions & 5 deletions src/web/crate_details.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@


use super::pool::Pool;
use super::{MetaData, duration_to_str, match_version, render_markdown, MatchVersion};
use super::{MetaData, duration_to_str, match_version, render_markdown, MatchVersion, redirect_base};
use super::error::Nope;
use super::page::Page;
use iron::prelude::*;
Expand Down Expand Up @@ -240,10 +240,8 @@ pub fn crate_details_handler(req: &mut Request) -> IronResult<Response> {
.to_resp("crate_details")
}
MatchVersion::Semver(version) => {
let url = ctry!(Url::parse(&format!("{}://{}:{}/crate/{}/{}",
req.url.scheme(),
req.url.host(),
req.url.port(),
let url = ctry!(Url::parse(&format!("{}/crate/{}/{}",
redirect_base(req),
name,
version)[..]));

Expand Down
23 changes: 19 additions & 4 deletions src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,24 @@ fn redirect(url: Url) -> Response {
resp
}

pub fn redirect_base(req: &Request) -> String {
// Try to get the scheme from CloudFront first, and then from iron
let scheme = req.headers
.get_raw("cloudfront-forwarded-proto")
.and_then(|values| values.get(0))
.and_then(|value| std::str::from_utf8(value).ok())
.filter(|proto| *proto == "http" || *proto == "https")
.unwrap_or_else(|| req.url.scheme());

// Only include the port if it's needed
let port = req.url.port();
if port == 80 {
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't this be 80 or 443?

Copy link
Member Author

Choose a reason for hiding this comment

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

Uh, probably yes, but it shouldn't cause any issue with our production setup, so fixing it is not that high priority.

Copy link
Member

Choose a reason for hiding this comment

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

I think only 80 is fine since CloudFront is sending requests to 80.

format!("{}://{}", scheme, req.url.host())
} else {
format!("{}://{}:{}", scheme, req.url.host(), port)
}
}

fn style_css_handler(_: &mut Request) -> IronResult<Response> {
let mut response = Response::with((status::Ok, STYLE_CSS));
let cache = vec![CacheDirective::Public,
Expand Down Expand Up @@ -495,10 +513,7 @@ fn ico_handler(req: &mut Request) -> IronResult<Response> {
} else {
// if we're looking for something like "favicon-20190317-1.35.0-nightly-c82834e2b.ico",
// redirect to the plain one so that the above branch can trigger with the correct filename
let url = ctry!(Url::parse(&format!("{}://{}:{}/favicon.ico",
req.url.scheme(),
req.url.host(),
req.url.port())[..]));
let url = ctry!(Url::parse(&format!("{}/favicon.ico", redirect_base(req))[..]));

Ok(redirect(url))
}
Expand Down
20 changes: 7 additions & 13 deletions src/web/releases.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Releases web handlers


use super::{duration_to_str, match_version};
use super::{duration_to_str, match_version, redirect_base};
use super::error::Nope;
use super::page::Page;
use super::pool::Pool;
Expand Down Expand Up @@ -468,10 +468,8 @@ pub fn search_handler(req: &mut Request) -> IronResult<Response> {
let name: String = rows.get(0).get(0);
let version: String = rows.get(0).get(1);
let target_name: String = rows.get(0).get(2);
let url = ctry!(Url::parse(&format!("{}://{}:{}/{}/{}/{}",
req.url.scheme(),
req.url.host(),
req.url.port(),
let url = ctry!(Url::parse(&format!("{}/{}/{}/{}",
redirect_base(req),
name,
version,
target_name)));
Expand Down Expand Up @@ -504,17 +502,13 @@ pub fn search_handler(req: &mut Request) -> IronResult<Response> {
}
};
let url = if rustdoc_status {
ctry!(Url::parse(&format!("{}://{}:{}/{}/{}",
req.url.scheme(),
req.url.host(),
req.url.port(),
ctry!(Url::parse(&format!("{}/{}/{}",
redirect_base(req),
query,
version)[..]))
} else {
ctry!(Url::parse(&format!("{}://{}:{}/crate/{}/{}",
req.url.scheme(),
req.url.host(),
req.url.port(),
ctry!(Url::parse(&format!("{}/crate/{}/{}",
redirect_base(req),
query,
version)[..]))
};
Expand Down
26 changes: 9 additions & 17 deletions src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use super::pool::Pool;
use super::file::File;
use super::latest_version;
use super::{latest_version, redirect_base};
use super::crate_details::CrateDetails;
use iron::prelude::*;
use iron::{status, Url};
Expand Down Expand Up @@ -75,10 +75,8 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
vers: &str,
target_name: &str)
-> IronResult<Response> {
let url = ctry!(Url::parse(&format!("{}://{}:{}/{}/{}/{}/",
req.url.scheme(),
req.url.host(),
req.url.port(),
let url = ctry!(Url::parse(&format!("{}/{}/{}/{}/",
redirect_base(req),
name,
vers,
target_name)[..]));
Expand All @@ -92,10 +90,8 @@ pub fn rustdoc_redirector_handler(req: &mut Request) -> IronResult<Response> {
name: &str,
vers: &str)
-> IronResult<Response> {
let url = ctry!(Url::parse(&format!("{}://{}:{}/crate/{}/{}",
req.url.scheme(),
req.url.host(),
req.url.port(),
let url = ctry!(Url::parse(&format!("{}/crate/{}/{}",
redirect_base(req),
name,
vers)[..]));

Expand Down Expand Up @@ -188,10 +184,8 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
// to prevent cloudfront caching the wrong artifacts on URLs with loose semver
// versions, redirect the browser to the returned version instead of loading it
// immediately
let url = ctry!(Url::parse(&format!("{}://{}:{}/{}/{}/{}",
req.url.scheme(),
req.url.host(),
req.url.port(),
let url = ctry!(Url::parse(&format!("{}/{}/{}/{}",
redirect_base(req),
name,
v,
req_path.join("/"))[..]));
Expand Down Expand Up @@ -297,10 +291,8 @@ pub fn badge_handler(req: &mut Request) -> IronResult<Response> {
}
}
MatchVersion::Semver(version) => {
let url = ctry!(Url::parse(&format!("{}://{}:{}/{}/badge.svg?version={}",
req.url.scheme(),
req.url.host(),
req.url.port(),
let url = ctry!(Url::parse(&format!("{}/{}/badge.svg?version={}",
redirect_base(req),
name,
version)[..]));

Expand Down