Skip to content

Commit 715cfdb

Browse files
committed
fastly-invalidation: use explicit surrogate keys everywhere, remove magic
1 parent 834153a commit 715cfdb

File tree

14 files changed

+714
-441
lines changed

14 files changed

+714
-441
lines changed

src/cdn/fastly.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,7 @@ mod tests {
214214
let m = fastly_api
215215
.mock("POST", "/service/test-sid-1/purge")
216216
.match_header(FASTLY_KEY, "test-token")
217-
.match_header(&SURROGATE_KEY, "crate-foo crate-bar")
217+
.match_header(&SURROGATE_KEY, "crate-bar crate-foo")
218218
.with_status(200)
219219
.create_async()
220220
.await;
@@ -251,15 +251,15 @@ mod tests {
251251
let m1 = fastly_api
252252
.mock("POST", "/service/test-sid-1/purge")
253253
.match_header(FASTLY_KEY, "test-token")
254-
.match_header(&SURROGATE_KEY, "crate-foo crate-bar")
254+
.match_header(&SURROGATE_KEY, "crate-bar crate-foo")
255255
.with_status(200)
256256
.create_async()
257257
.await;
258258

259259
let m2 = fastly_api
260260
.mock("POST", "/service/test-sid-2/purge")
261261
.match_header(FASTLY_KEY, "test-token")
262-
.match_header(&SURROGATE_KEY, "crate-foo crate-bar")
262+
.match_header(&SURROGATE_KEY, "crate-bar crate-foo")
263263
.with_status(200)
264264
.create_async()
265265
.await;
@@ -296,7 +296,7 @@ mod tests {
296296
let m = fastly_api
297297
.mock("POST", "/service/test-sid-1/purge")
298298
.match_header(FASTLY_KEY, "test-token")
299-
.match_header(&SURROGATE_KEY, "crate-foo crate-bar")
299+
.match_header(&SURROGATE_KEY, "crate-bar crate-foo")
300300
.with_status(500)
301301
.create_async()
302302
.await;

src/db/types/krate_name.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,12 @@ use std::{io::Write, str::FromStr};
3232
)]
3333
pub struct KrateName(String);
3434

35+
impl From<&KrateName> for KrateName {
36+
fn from(krate_name: &KrateName) -> Self {
37+
krate_name.clone()
38+
}
39+
}
40+
3541
impl FromStr for KrateName {
3642
type Err = anyhow::Error;
3743

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
//! [Docs.rs](https://docs.rs) (formerly cratesfyi) is an open source project to host
22
//! documentation of crates for the Rust Programming Language.
3-
#![allow(clippy::cognitive_complexity)]
3+
#![allow(
4+
clippy::cognitive_complexity,
5+
// TODO: `AxumNope::Redirect(EscapedURI, CachePolicy)` is too big.
6+
clippy::result_large_err,
7+
)]
48

59
pub use self::build_queue::{
610
AsyncBuildQueue, BuildQueue, queue_rebuilds, queue_rebuilds_faulty_rustdoc,

src/test/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use crate::{
1818
web::{
1919
build_axum_app,
2020
cache::{self, TargetCdn, X_RLNG_SOURCE_CDN},
21-
headers::{IfNoneMatch, SURROGATE_CONTROL},
21+
headers::{IfNoneMatch, SURROGATE_CONTROL, SurrogateKeys},
2222
page::TemplateData,
2323
},
2424
};
@@ -76,6 +76,11 @@ pub(crate) fn assert_cache_headers_eq(
7676
response.headers().get(&SURROGATE_CONTROL),
7777
"surrogate control header mismatch"
7878
);
79+
assert_eq!(
80+
expected_headers.surrogate_keys.as_ref(),
81+
response.headers().typed_get::<SurrogateKeys>().as_ref(),
82+
"surrogate key header mismatch"
83+
);
7984
}
8085

8186
pub(crate) trait AxumResponseTestExt {
@@ -113,7 +118,10 @@ impl AxumResponseTestExt for axum::response::Response {
113118
// we emit the wrong cache policy in a handler.
114119
//
115120
// The fastly specifics are tested in web::cache unittests.
116-
assert_cache_headers_eq(self, &cache_policy.render(config, TargetCdn::Fastly));
121+
assert_cache_headers_eq(
122+
self,
123+
&cache_policy.render(config, TargetCdn::Fastly).unwrap(),
124+
);
117125
}
118126

119127
fn error_for_status(self) -> Result<Self>

src/web/build_details.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,14 @@ pub(crate) async fn build_details_handler(
7373
let version = match_version(&mut conn, params.name(), params.req_version())
7474
.await?
7575
.assume_exact_name()?
76-
.into_canonical_req_version_or_else(|version| {
76+
.into_canonical_req_version_or_else(|confirmed_name, version| {
77+
let params = params
78+
.clone()
79+
.with_confirmed_name(Some(confirmed_name))
80+
.with_req_version(version);
7781
AxumNope::Redirect(
78-
params
79-
.clone()
80-
.with_req_version(version)
81-
.build_details_url(id, build_params.filename.as_deref()),
82-
CachePolicy::ForeverInCdn,
82+
params.build_details_url(id, build_params.filename.as_deref()),
83+
CachePolicy::ForeverInCdn(confirmed_name.into()),
8384
)
8485
})?
8586
.into_version();

src/web/builds.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,10 +67,14 @@ pub(crate) async fn build_list_handler(
6767
let version = match_version(&mut conn, params.name(), params.req_version())
6868
.await?
6969
.assume_exact_name()?
70-
.into_canonical_req_version_or_else(|version| {
70+
.into_canonical_req_version_or_else(|confirmed_name, version| {
71+
let params = params
72+
.clone()
73+
.with_confirmed_name(Some(confirmed_name))
74+
.with_req_version(version);
7175
AxumNope::Redirect(
72-
params.clone().with_req_version(version).builds_url(),
73-
CachePolicy::ForeverInCdn,
76+
params.builds_url(),
77+
CachePolicy::ForeverInCdn(confirmed_name.into()),
7478
)
7579
})?
7680
.into_version();

0 commit comments

Comments
 (0)