-
-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Proxito: simplify caching logic #10067
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -156,27 +156,32 @@ def add_cache_headers(self, request, response): | |
""" | ||
Add Cache-Control headers. | ||
|
||
If privacy levels are enabled and the header isn't already present, | ||
set the cache level to private. | ||
Or if the request was from the `X-RTD-Slug` header, | ||
we don't cache the response, since we could be caching a response in another domain. | ||
If the `CDN-Cache-Control` header isn't already present, set the cache | ||
level to public or private, depending if we allow private repos or not. | ||
Or if the request was from the `X-RTD-Slug` header, we don't cache the | ||
response, since we could be caching a response in another domain. | ||
|
||
We use ``CDN-Cache-Control``, to control caching at the CDN level only. | ||
This doesn't affect caching at the browser level (``Cache-Control``). | ||
Comment on lines
164
to
165
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we make this configurable as well? Probably in another PR. |
||
|
||
See https://developers.cloudflare.com/cache/about/cdn-cache-control. | ||
""" | ||
header = "CDN-Cache-Control" | ||
cdn_cache_header = "CDN-Cache-Control" | ||
unresolved_domain = request.unresolved_domain | ||
# Never trust projects resolving from the X-RTD-Slug header, | ||
# we don't want to cache their content on domains from other | ||
# projects, see GHSA-mp38-vprc-7hf5. | ||
if unresolved_domain and unresolved_domain.is_from_http_header: | ||
response.headers[header] = "private" | ||
|
||
if settings.ALLOW_PRIVATE_REPOS: | ||
# Set the key to private only if it hasn't already been set by the view. | ||
response.headers.setdefault(header, "private") | ||
response.headers[cdn_cache_header] = "private" | ||
# SECURITY: Return early, we never want to cache this response. | ||
return | ||
stsewd marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
# Set the key only if it hasn't already been set by the view. | ||
if cdn_cache_header not in response.headers: | ||
default_cache_level = ( | ||
"private" if settings.ALLOW_PRIVATE_REPOS else "public" | ||
) | ||
response.headers[cdn_cache_header] = default_cache_level | ||
|
||
def _set_request_attributes(self, request, unresolved_domain): | ||
""" | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -58,7 +58,8 @@ def test_health_check(self): | |
host = '127.0.0.1' | ||
resp = self.client.get(url, HTTP_HOST=host) | ||
self.assertEqual(resp.status_code, 200) | ||
self.assertEqual(resp.json(), {'status': 200}) | ||
self.assertEqual(resp.json(), {"status": 200}) | ||
self.assertEqual(resp["CDN-Cache-Control"], "private") | ||
|
||
def test_subproject_serving(self): | ||
url = '/projects/subproject/en/latest/awesome.html' | ||
|
@@ -397,7 +398,24 @@ def test_project_nginx_serving_unicode_filename(self): | |
) | ||
|
||
@override_settings(PYTHON_MEDIA=False) | ||
def test_download_files(self): | ||
def test_download_files_public_version(self): | ||
for type_ in DOWNLOADABLE_MEDIA_TYPES: | ||
resp = self.client.get( | ||
f"/_/downloads/en/latest/{type_}/", | ||
HTTP_HOST="project.dev.readthedocs.io", | ||
) | ||
self.assertEqual(resp.status_code, 200) | ||
extension = "zip" if type_ == MEDIA_TYPE_HTMLZIP else type_ | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems like a code smell... Probably should have named |
||
self.assertEqual( | ||
resp["X-Accel-Redirect"], | ||
f"/proxito/media/{type_}/project/latest/project.{extension}", | ||
) | ||
self.assertEqual(resp["CDN-Cache-Control"], "public") | ||
|
||
@override_settings(PYTHON_MEDIA=False, ALLOW_PRIVATE_REPOS=True) | ||
def test_download_files_private_version(self): | ||
self.version.privacy_level = PRIVATE | ||
self.version.save() | ||
for type_ in DOWNLOADABLE_MEDIA_TYPES: | ||
resp = self.client.get( | ||
f"/_/downloads/en/latest/{type_}/", | ||
|
@@ -409,6 +427,7 @@ def test_download_files(self): | |
resp["X-Accel-Redirect"], | ||
f"/proxito/media/{type_}/project/latest/project.{extension}", | ||
) | ||
self.assertEqual(resp["CDN-Cache-Control"], "private") | ||
|
||
@override_settings(PYTHON_MEDIA=False) | ||
def test_invalid_download_files(self): | ||
|
@@ -1460,11 +1479,13 @@ def test_cache_on_private_versions_custom_domain(self): | |
self.domain.save() | ||
self._test_cache_control_header_project(expected_value='private', host=self.domain.domain) | ||
|
||
# HTTPS redirect respects the privacy level of the version. | ||
resp = self.client.get('/en/latest/', secure=False, HTTP_HOST=self.domain.domain) | ||
self.assertEqual(resp['Location'], f'https://{self.domain.domain}/en/latest/') | ||
self.assertEqual(resp.headers['CDN-Cache-Control'], 'private') | ||
self.assertEqual(resp.headers['Cache-Tag'], 'project,project:latest') | ||
# HTTPS redirect can always be cached. | ||
ericholscher marked this conversation as resolved.
Show resolved
Hide resolved
|
||
resp = self.client.get( | ||
"/en/latest/", secure=False, HTTP_HOST=self.domain.domain | ||
) | ||
self.assertEqual(resp["Location"], f"https://{self.domain.domain}/en/latest/") | ||
self.assertEqual(resp.headers["CDN-Cache-Control"], "public") | ||
self.assertEqual(resp.headers["Cache-Tag"], "project,project:latest") | ||
|
||
def test_cache_public_versions(self): | ||
self.project.versions.update(privacy_level=PUBLIC) | ||
|
@@ -1492,15 +1513,18 @@ def test_cache_on_private_versions_custom_domain_subproject(self): | |
self.domain.save() | ||
self._test_cache_control_header_subproject(expected_value='private', host=self.domain.domain) | ||
|
||
# HTTPS redirect respects the privacy level of the version. | ||
# HTTPS redirect can always be cached. | ||
resp = self.client.get( | ||
'/projects/subproject/en/latest/', | ||
secure=False, | ||
HTTP_HOST=self.domain.domain, | ||
) | ||
self.assertEqual(resp['Location'], f'https://{self.domain.domain}/projects/subproject/en/latest/') | ||
self.assertEqual(resp.headers['CDN-Cache-Control'], 'private') | ||
self.assertEqual(resp.headers['Cache-Tag'], 'subproject,subproject:latest') | ||
self.assertEqual( | ||
resp["Location"], | ||
f"https://{self.domain.domain}/projects/subproject/en/latest/", | ||
) | ||
self.assertEqual(resp.headers["CDN-Cache-Control"], "public") | ||
self.assertEqual(resp.headers["Cache-Tag"], "subproject,subproject:latest") | ||
|
||
def test_cache_public_versions_subproject(self): | ||
self.subproject.versions.update(privacy_level=PUBLIC) | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💯