Skip to content
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

crates.io: Add support for download URLs without placeholders #365

Merged
merged 2 commits into from
Dec 7, 2023
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
10 changes: 10 additions & 0 deletions terragrunt/modules/crates-io/cloudfront-functions/static-router.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,15 @@ function handler(event) {
request.uri = request.uri.replace("+", "%2B");
}

// cargo versions before 1.24 don't support placeholders in the `dl` field
// of the index, so we need to rewrite the download URL to point to the
// crate file instead.
var match = request.uri.match(/^\/crates\/([^\/]+)\/([^\/]+)\/download$/);
if (match) {
var crate = match[1];
var version = match[2];
request.uri = `/crates/${crate}/${crate}-${version}.crate`;
}

return request;
}
8 changes: 8 additions & 0 deletions terragrunt/modules/crates-io/compute-static/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions terragrunt/modules/crates-io/compute-static/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ derive_builder = "0.12.0"
fastly = "0.9.1"
log = "0.4.17"
log-fastly = "0.9.1"
once_cell = "1.18.0"
regex = "1.7.1"
serde = { version = "1.0.152", features = ["derive"] }
serde_json = "1.0.92"
time = { version = "0.3.17", features = ["serde-human-readable"] }
56 changes: 56 additions & 0 deletions terragrunt/modules/crates-io/compute-static/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use fastly::http::{Method, StatusCode};
use fastly::{Error, Request, Response};
use log::{info, warn, LevelFilter};
use log_fastly::Logger;
use once_cell::sync::Lazy;
use regex::Regex;
use serde_json::json;
use time::OffsetDateTime;

Expand Down Expand Up @@ -74,6 +76,7 @@ fn handle_request(config: &Config, mut request: Request) -> Result<Response, Err

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

// Database dump is too big to cache on Fastly
if request.get_url_str().ends_with("db-dump.tar.gz") {
Expand Down Expand Up @@ -127,6 +130,28 @@ fn rewrite_urls_with_plus_character(request: &mut Request) {
}
}

/// Rewrite `/crates/{crate}/{version}/download` URLs to
/// `/crates/{crate}/{crate}-{version}.crate`
///
/// cargo versions before 1.24 don't support placeholders in the `dl` field
/// of the index, so we need to rewrite the download URL to point to the
/// crate file instead.
fn rewrite_download_urls(request: &mut Request) {
static RE: Lazy<Regex> = Lazy::new(|| {
Regex::new(r"^/crates/(?P<crate>[^/]+)/(?P<version>[^/]+)/download$").unwrap()
});
jdno marked this conversation as resolved.
Show resolved Hide resolved

let url = request.get_url_mut();
let path = url.path();

if let Some(captures) = RE.captures(path) {
let krate = captures.name("crate").unwrap().as_str();
Copy link
Member

Choose a reason for hiding this comment

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

Unwrapping here seems dangerous. I'm not sure how much debugging information we'll get if we just fail hard. Instead, I think it is better to change the return type of the function to Result<(), Error> and handle the error gracefully. And if we log an error with error!, we'll get to see that in the service logs.

Copy link
Member Author

Choose a reason for hiding this comment

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

hmm, can you think of a scenario where the unwrap would panic? the outer if let ensures that the regex matches and both of the captures are non-optional so I think unwrap is safe here for this regex 🤔

Copy link
Member

Choose a reason for hiding this comment

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

That's a good argument. I don't think this can panic in its current form, but I'm also somewhat paranoid about changes in the future and other people assuming, based on this example, that it's okay to unwrap in this binary.

let version = captures.name("version").unwrap().as_str();
let new_path = format!("/crates/{krate}/{krate}-{version}.crate");
url.set_path(&new_path);
}
}

/// Redirect request to CloudFront
///
/// As of early 2023, certain files are too large to be served through Fastly. One of those is the
Expand Down Expand Up @@ -199,3 +224,34 @@ fn build_and_send_log(log_line: LogLineV1Builder, config: &Config) {
}
};
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_rewrite_download_urls() {
fn test(url: &str, expected: &str) {
let mut request = Request::get(url);
rewrite_download_urls(&mut request);
assert_eq!(request.get_url_str(), expected);
}

test(
"https://static.crates.io/unrelated",
"https://static.crates.io/unrelated",
);
test(
"https://static.crates.io/crates/serde/serde-1.0.0.crate",
"https://static.crates.io/crates/serde/serde-1.0.0.crate",
);
test(
"https://static.crates.io/crates/serde/1.0.0/download",
"https://static.crates.io/crates/serde/serde-1.0.0.crate",
);
test(
"https://static.crates.io/crates/serde/1.0.0-alpha.1+foo-bar/download",
"https://static.crates.io/crates/serde/serde-1.0.0-alpha.1+foo-bar.crate",
);
}
}