Merge ~cjwatson/lp-archive:always-set-cache-control into lp-archive:main

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: c84d49cd159f5a24a2df86ae56104281485b2448
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/lp-archive:always-set-cache-control
Merge into: lp-archive:main
Diff against target: 73 lines (+15/-11)
2 files modified
lp_archive/archive.py (+6/-3)
tests/test_archive.py (+9/-8)
Reviewer Review Type Date Requested Status
Ines Almeida Approve
Review via email: mp+455045@code.launchpad.net

Commit message

Always set Cache-Control header on redirect responses

Description of the change

It's much easier to fix our nginx configuration to copy the Cache-Control header over to the followed-redirect response if we ensure that the redirect always has a Cache-Control header rather than relying on the one returned by the librarian.

Thanks to Clinton Fung for help puzzling this out.

To post a comment you must log in.
Revision history for this message
Ines Almeida (ines-almeida) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lp_archive/archive.py b/lp_archive/archive.py
2index 3e2e50f..f948c8f 100644
3--- a/lp_archive/archive.py
4+++ b/lp_archive/archive.py
5@@ -76,7 +76,10 @@ def check_auth(archive: str) -> None:
6
7
8 def get_extra_headers(path: str, live_at: datetime | None) -> dict[str, str]:
9- headers = {}
10+ # It's safe to default to long caching even for files in private
11+ # archives, since we always set "Vary: Authorization" (see add_headers
12+ # below).
13+ cache_control = "max-age=31536000"
14 # Non-content-addressed files under dists/ are mutable and may change on
15 # subsequent publisher runs, so if we aren't addressing a snapshot then
16 # we need to tell caches to give them a relatively short expiry time.
17@@ -84,8 +87,8 @@ def get_extra_headers(path: str, live_at: datetime | None) -> dict[str, str]:
18 if live_at is None:
19 path_parts = PurePath(path).parts
20 if path_parts[0] == "dists" and path_parts[2:][-3:-2] != ("by-hash",):
21- headers["Cache-Control"] = "max-age=1800, proxy-revalidate"
22- return headers
23+ cache_control = "max-age=1800, proxy-revalidate"
24+ return {"Cache-Control": cache_control}
25
26
27 def translate(
28diff --git a/tests/test_archive.py b/tests/test_archive.py
29index 11481f7..a40cc6b 100644
30--- a/tests/test_archive.py
31+++ b/tests/test_archive.py
32@@ -306,32 +306,33 @@ def test_translate_cache_control_dists_not_found(client, archive_proxy):
33
34
35 def test_translate_cache_control_dists_at_timestamp(client, archive_proxy):
36- # Non-content-addressed requests to a snapshot do not get a
37- # Cache-Control header, even if they're under dists/.
38+ # Non-content-addressed requests to a snapshot get a Cache-Control
39+ # header with a long expiry time, even if they're under dists/.
40 response = client.get(
41 "/ubuntu/20220101T120000Z/dists/focal/InRelease",
42 headers=[("Host", "snapshot.ubuntu.test")],
43 )
44 assert response.status_code == 307
45- assert "Cache-Control" not in response.headers
46+ assert response.headers["Cache-Control"] == "max-age=31536000"
47
48
49 def test_translate_cache_control_dists_by_hash(client, archive_proxy):
50- # Content-addressed requests under dists/ do not get a Cache-Control
51- # header.
52+ # Content-addressed requests under dists/ get a Cache-Control header
53+ # with a long expiry time.
54 response = client.get(
55 f"/ubuntu/dists/focal/by-hash/SHA256/{'0' * 64}",
56 headers=[("Host", "snapshot.ubuntu.test")],
57 )
58 assert response.status_code == 307
59- assert "Cache-Control" not in response.headers
60+ assert response.headers["Cache-Control"] == "max-age=31536000"
61
62
63 def test_translate_cache_control_not_dists(client, archive_proxy):
64- # Requests not under dists/ do not get a Cache-Control header.
65+ # Requests not under dists/ get a Cache-Control header with a long
66+ # expiry time.
67 response = client.get(
68 "/ubuntu/pool/main/h/hello/hello_1.0-1.deb",
69 headers=[("Host", "snapshot.ubuntu.test")],
70 )
71 assert response.status_code == 307
72- assert "Cache-Control" not in response.headers
73+ assert response.headers["Cache-Control"] == "max-age=31536000"

Subscribers

People subscribed via source and target branches