Skip to content

Commit 82fa638

Browse files
authored
Merge pull request #469 from rust-lang/feat-serve-version-downloads-index.html
feat: serve version downloads `index.html`
2 parents 24e1ff8 + 61e8d37 commit 82fa638

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
585e9aabc4a5387af5c7aaebd68b095c1fcf89af
1+
fe7f3b92fbe04cd9d36bf36992f653ce27c7e8c7

terragrunt/modules/crates-io/cloudfront-functions/static-router.js

+16
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,16 @@
11
function handler(event) {
22
var request = event.request;
3+
var versionDownloads ='/archive/version-downloads/';
34

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

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

2026
return request;
2127
}
28+
29+
function permanentRedirect(destination) {
30+
return {
31+
statusCode: 301,
32+
statusDescription: 'Moved Permanently',
33+
headers: {
34+
'location': { value: destination },
35+
},
36+
};
37+
}

terragrunt/modules/crates-io/compute-static/src/main.rs

+30-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
use fastly::http::{Method, StatusCode, Version};
1+
use fastly::convert::ToHeaderValue;
2+
use fastly::http::{header, Method, StatusCode, Version};
23
use fastly::{Error, Request, Response};
34
use log::{info, warn, LevelFilter};
45
use log_fastly::Logger;
@@ -16,6 +17,7 @@ mod log_line;
1617

1718
const DATADOG_APP: &str = "crates.io";
1819
const DATADOG_SERVICE: &str = "static.crates.io";
20+
const VERSION_DOWNLOADS: &str = "/archive/version-downloads/";
1921

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

115+
if request.get_url().path() == "/archive/version-downloads" {
116+
let mut destination = request.get_url().clone();
117+
destination.set_path(VERSION_DOWNLOADS);
118+
119+
return Ok(permanent_redirect(destination));
120+
}
121+
113122
set_ttl(config, &mut request);
114123
rewrite_urls_with_plus_character(&mut request);
115124
rewrite_download_urls(&mut request);
125+
rewrite_version_downloads_urls(&mut request);
116126

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

137+
fn permanent_redirect(destination: impl ToHeaderValue) -> Response {
138+
Response::new()
139+
.with_status(StatusCode::PERMANENT_REDIRECT)
140+
.with_header(header::LOCATION, destination)
141+
}
142+
127143
/// Limit HTTP methods
128144
///
129145
/// Clients are only allowed to request resources using GET and HEAD requests. If any other HTTP
@@ -168,6 +184,19 @@ fn rewrite_urls_with_plus_character(request: &mut Request) {
168184
}
169185
}
170186

187+
/// Rewrite `/archive/version-downloads/` URLs to `/archive/version-downloads/index.html`
188+
///
189+
/// In this way, users can see what files are available for download.
190+
fn rewrite_version_downloads_urls(request: &mut Request) {
191+
let url = request.get_url_mut();
192+
let path = url.path();
193+
194+
if path == VERSION_DOWNLOADS {
195+
let new_path = format!("{path}index.html");
196+
url.set_path(&new_path);
197+
}
198+
}
199+
171200
/// Rewrite `/crates/{crate}/{version}/download` URLs to
172201
/// `/crates/{crate}/{crate}-{version}.crate`
173202
///

0 commit comments

Comments
 (0)