Skip to content

Commit 2cd5531

Browse files
committed
add caching to archive download endpoint
1 parent 86d4dad commit 2cd5531

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

src/test/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,20 @@ pub(crate) fn assert_redirect_unchecked(
167167
assert_redirect_common(path, expected_target, web).map(|_| ())
168168
}
169169

170+
/// Makes sure that a URL redirects to a specific page, but doesn't check that the target exists
171+
#[context("expected redirect from {path} to {expected_target}")]
172+
pub(crate) fn assert_redirect_cached_unchecked(
173+
path: &str,
174+
expected_target: &str,
175+
cache_policy: cache::CachePolicy,
176+
web: &TestFrontend,
177+
config: &Config,
178+
) -> Result<()> {
179+
let redirect_response = assert_redirect_common(path, expected_target, web)?;
180+
assert_cache_control(&redirect_response, cache_policy, config);
181+
Ok(())
182+
}
183+
170184
/// Make sure that a URL redirects to a specific page, and that the target exists and is not another redirect
171185
#[context("expected redirect from {path} to {expected_target}")]
172186
pub(crate) fn assert_redirect(path: &str, expected_target: &str, web: &TestFrontend) -> Result<()> {

src/web/rustdoc.rs

Lines changed: 22 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,13 @@ pub fn download_handler(req: &mut Request) -> IronResult<Response> {
760760
ctry!(req, storage.set_public_access(&archive_path, true));
761761
}
762762

763-
Ok(super::redirect(ctry!(
764-
req,
765-
Url::parse(&format!("{}/{}", config.s3_static_root_path, archive_path))
766-
)))
763+
Ok(super::cached_redirect(
764+
ctry!(
765+
req,
766+
Url::parse(&format!("{}/{}", config.s3_static_root_path, archive_path))
767+
),
768+
CachePolicy::ForeverInCdn,
769+
))
767770
}
768771

769772
/// Serves shared resources used by rustdoc-generated documentation.
@@ -2342,10 +2345,9 @@ mod test {
23422345
wrapper(|env| {
23432346
let web = env.frontend();
23442347

2345-
assert_eq!(
2346-
web.get("/crate/dummy/0.1.0/download").send()?.status(),
2347-
StatusCode::NOT_FOUND
2348-
);
2348+
let response = web.get("/crate/dummy/0.1.0/download").send()?;
2349+
assert_cache_control(&response, CachePolicy::NoCaching, &env.config());
2350+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
23492351
Ok(())
23502352
});
23512353
}
@@ -2361,10 +2363,9 @@ mod test {
23612363

23622364
let web = env.frontend();
23632365

2364-
assert_eq!(
2365-
web.get("/crate/dummy/0.1.0/download").send()?.status(),
2366-
StatusCode::NOT_FOUND
2367-
);
2366+
let response = web.get("/crate/dummy/0.1.0/download").send()?;
2367+
assert_cache_control(&response, CachePolicy::NoCaching, &env.config());
2368+
assert_eq!(response.status(), StatusCode::NOT_FOUND);
23682369
Ok(())
23692370
});
23702371
}
@@ -2380,10 +2381,12 @@ mod test {
23802381

23812382
let web = env.frontend();
23822383

2383-
assert_redirect_unchecked(
2384+
assert_redirect_cached_unchecked(
23842385
"/crate/dummy/0.1/download",
23852386
"https://static.docs.rs/rustdoc/dummy/0.1.0.zip",
2387+
CachePolicy::ForeverInCdn,
23862388
web,
2389+
&env.config(),
23872390
)?;
23882391
assert!(env.storage().get_public_access("rustdoc/dummy/0.1.0.zip")?);
23892392
Ok(())
@@ -2405,10 +2408,12 @@ mod test {
24052408
env.storage()
24062409
.set_public_access("rustdoc/dummy/0.1.0.zip", false)?;
24072410

2408-
assert_redirect_unchecked(
2411+
assert_redirect_cached_unchecked(
24092412
"/crate/dummy/0.1.0/download",
24102413
"https://static.docs.rs/rustdoc/dummy/0.1.0.zip",
2414+
CachePolicy::ForeverInCdn,
24112415
web,
2416+
&env.config(),
24122417
)?;
24132418
assert!(env.storage().get_public_access("rustdoc/dummy/0.1.0.zip")?);
24142419
Ok(())
@@ -2432,10 +2437,12 @@ mod test {
24322437

24332438
let web = env.frontend();
24342439

2435-
assert_redirect_unchecked(
2440+
assert_redirect_cached_unchecked(
24362441
"/crate/dummy/latest/download",
24372442
"https://static.docs.rs/rustdoc/dummy/0.2.0.zip",
2443+
CachePolicy::ForeverInCdn,
24382444
web,
2445+
&env.config(),
24392446
)?;
24402447
assert!(env.storage().get_public_access("rustdoc/dummy/0.2.0.zip")?);
24412448
Ok(())

0 commit comments

Comments
 (0)