Skip to content

Commit

Permalink
Merge pull request #469 from rust-lang/feat-serve-version-downloads-i…
Browse files Browse the repository at this point in the history
…ndex.html

feat: serve version downloads `index.html`
  • Loading branch information
MarcoIeni authored Aug 20, 2024
2 parents 24e1ff8 + 61e8d37 commit 82fa638
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 2 deletions.
2 changes: 1 addition & 1 deletion terragrunt/accounts/legacy/crates-io-prod/deployed-ref
Original file line number Diff line number Diff line change
@@ -1 +1 @@
585e9aabc4a5387af5c7aaebd68b095c1fcf89af
fe7f3b92fbe04cd9d36bf36992f653ce27c7e8c7
16 changes: 16 additions & 0 deletions terragrunt/modules/crates-io/cloudfront-functions/static-router.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
function handler(event) {
var request = event.request;
var versionDownloads ='/archive/version-downloads/';

// URL-encode the `+` character in the request URI
// See more: https://github.com/rust-lang/crates.io/issues/4891
if (request.uri.includes("+")) {
request.uri = request.uri.replace("+", "%2B");
} else if (request.uri === versionDownloads) {
request.uri += 'index.html';
return request;
} else if (request.uri === '/archive/version-downloads') {
return permanentRedirect(versionDownloads);
}

// cargo versions before 1.24 don't support placeholders in the `dl` field
Expand All @@ -19,3 +25,13 @@ function handler(event) {

return request;
}

function permanentRedirect(destination) {
return {
statusCode: 301,
statusDescription: 'Moved Permanently',
headers: {
'location': { value: destination },
},
};
}
31 changes: 30 additions & 1 deletion terragrunt/modules/crates-io/compute-static/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use fastly::http::{Method, StatusCode, Version};
use fastly::convert::ToHeaderValue;
use fastly::http::{header, Method, StatusCode, Version};
use fastly::{Error, Request, Response};
use log::{info, warn, LevelFilter};
use log_fastly::Logger;
Expand All @@ -16,6 +17,7 @@ mod log_line;

const DATADOG_APP: &str = "crates.io";
const DATADOG_SERVICE: &str = "static.crates.io";
const VERSION_DOWNLOADS: &str = "/archive/version-downloads/";

#[fastly::main]
fn main(request: Request) -> Result<Response, Error> {
Expand Down Expand Up @@ -110,9 +112,17 @@ fn handle_request(config: &Config, mut request: Request) -> Result<Response, Err
return Ok(response);
}

if request.get_url().path() == "/archive/version-downloads" {
let mut destination = request.get_url().clone();
destination.set_path(VERSION_DOWNLOADS);

return Ok(permanent_redirect(destination));
}

set_ttl(config, &mut request);
rewrite_urls_with_plus_character(&mut request);
rewrite_download_urls(&mut request);
rewrite_version_downloads_urls(&mut request);

// Database dump is too big to cache on Fastly
if request.get_url_str().ends_with("db-dump.tar.gz") {
Expand All @@ -124,6 +134,12 @@ fn handle_request(config: &Config, mut request: Request) -> Result<Response, Err
}
}

fn permanent_redirect(destination: impl ToHeaderValue) -> Response {
Response::new()
.with_status(StatusCode::PERMANENT_REDIRECT)
.with_header(header::LOCATION, destination)
}

/// Limit HTTP methods
///
/// Clients are only allowed to request resources using GET and HEAD requests. If any other HTTP
Expand Down Expand Up @@ -168,6 +184,19 @@ fn rewrite_urls_with_plus_character(request: &mut Request) {
}
}

/// Rewrite `/archive/version-downloads/` URLs to `/archive/version-downloads/index.html`
///
/// In this way, users can see what files are available for download.
fn rewrite_version_downloads_urls(request: &mut Request) {
let url = request.get_url_mut();
let path = url.path();

if path == VERSION_DOWNLOADS {
let new_path = format!("{path}index.html");
url.set_path(&new_path);
}
}

/// Rewrite `/crates/{crate}/{version}/download` URLs to
/// `/crates/{crate}/{crate}-{version}.crate`
///
Expand Down

0 comments on commit 82fa638

Please sign in to comment.