Skip to content

Commit

Permalink
Take advantage of Dist.content_headers_for() to no-cache repomd and f…
Browse files Browse the repository at this point in the history
…riends.

Requires pulpcore support release in core/3.28.

fixes pulp#2947.
  • Loading branch information
ggainey committed Jul 11, 2023
1 parent 53636c0 commit fa2b1b9
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES/2947.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Added NOCACHE_LIST config to enable specifying files to be served with a no-cache header.

By default, repomd.xml, repomd.key, and repomd.key.asc are served with
Cache-control: no-cache.
9 changes: 9 additions & 0 deletions pulp_rpm/app/models/repository.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import os
import textwrap

from gettext import gettext as _
Expand Down Expand Up @@ -490,6 +491,14 @@ def content_handler(self, path):

return Response(body=val)

def content_headers_for(self, path):
"""Return per-file http-headers."""
headers = super().content_headers_for(path)
base = os.path.basename(path) # path.strip("/").split("/")[-1]
if base in settings.NOCACHE_LIST:
headers.update({"Cache-Control": "no-cache"})
return headers

def content_handler_list_directory(self, rel_path):
"""Return the extra dir entries."""
retval = set()
Expand Down
1 change: 1 addition & 0 deletions pulp_rpm/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@
KEEP_CHANGELOG_LIMIT = 10
SOLVER_DEBUG_LOGS = True
RPM_METADATA_USE_REPO_PACKAGE_TIME = False
NOCACHE_LIST = ["repomd.xml", "repomd.xml.asc", "repomd.xml.key"]
21 changes: 21 additions & 0 deletions pulp_rpm/tests/functional/api/test_consume_content.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,24 @@ def test_config_dot_repo(

if has_signing_service:
assert f"gpgkey={distribution.base_url}repodata/repomd.xml.key" in content


@pytest.mark.parallel
def test_repomd_headers(
create_distribution,
http_get_headers,
):
"""Test if repomd.xml is returned with Cache-control: no-cache header."""
distribution = create_distribution(gpgcheck=1, repo_gpgcheck=1, has_signing_service=True)
assert (
http_get_headers(f"{distribution.base_url}repodata/repomd.xml").get("Cache-control", "")
== "no-cache"
)
assert (
not http_get_headers(f"{distribution.base_url}config.repo").get("Cache-control", "")
== "no-cache"
)
assert (
http_get_headers(f"{distribution.base_url}repodata/repomd.xml.key").get("Cache-control", "")
== "no-cache"
)
16 changes: 16 additions & 0 deletions pulp_rpm/tests/functional/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
import asyncio
import json
import uuid
from tempfile import NamedTemporaryFile

import aiohttp
import pytest

from pulpcore.client.pulp_rpm import (
Expand Down Expand Up @@ -342,3 +344,17 @@ def _cleanup_domains(
assert content_api_client.list(pulp_domain=domain.name).count == 0

return _cleanup_domains


@pytest.fixture(scope="session")
def http_get_headers():
def _http_get_headers(url, **kwargs):
async def _send_request():
async with aiohttp.ClientSession(raise_for_status=True) as session:
async with session.get(url) as response:
return response.headers

headers = asyncio.run(_send_request())
return headers

return _http_get_headers
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ django_readonly_field~=1.1.1
jsonschema>=4.6,<5.0
libcomps>=0.1.15.post1,<0.2
productmd~=1.33.0
pulpcore>=3.25.0,<3.40
pulpcore>=3.28.0,<3.40
solv~=0.7.21
aiohttp_xmlrpc~=1.5.0

0 comments on commit fa2b1b9

Please sign in to comment.