Skip to content
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

Add cache-control options #405

Merged
merged 1 commit into from
Aug 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions lib/MirrorCache/Datamodule.pm
Original file line number Diff line number Diff line change
Expand Up @@ -578,9 +578,15 @@ sub _init_path($self) {
$self->_pedantic($pedantic) if defined $pedantic;

$self->agent; # parse headers
$self->must_render_from_root(1)
if ( $self->accept_all || !$self->extra )
&& $path =~ m/.*\/(repodata\/repomd.xml[^\/]*|media\.1\/(media|products)|content|.*\.sha256(\.asc)|Release(.key|.gpg)?|InRelease|Packages(.gz)?|Sources(.gz)?|.*_Arch\.(files|db|key)(\.(sig|tar\.gz(\.sig)?))?|(files|primary|other).xml.gz|[Pp]ackages(\.[A-Z][A-Z])?\.(xz|gz)|gpg-pubkey.*\.asc|CHECKSUMS)$/;
if (
( $self->accept_all || !$self->extra )
&& $path =~ m/.*\/(repodata\/repomd.xml[^\/]*|media\.1\/(media|products)|content|.*\.sha256(\.asc)|Release(.key|.gpg)?|InRelease|Packages(.gz)?|Sources(.gz)?|.*_Arch\.(files|db|key)(\.(sig|tar\.gz(\.sig)?))?|(files|primary|other).xml.gz|[Pp]ackages(\.[A-Z][A-Z])?\.(xz|gz)|gpg-pubkey.*\.asc|CHECKSUMS(.asc)?)$/
) {
$self->must_render_from_root(1);
my $time = ~time() & 0xff;

$self->c->res->headers->cache_control("public, max-age=$time, must-revalidate");
}

my ($ext) = $path =~ /([^.]+)$/;
my $mime = '';
Expand Down
7 changes: 7 additions & 0 deletions lib/MirrorCache/WebAPI/Plugin/Dir.pm
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ sub indx {
$success =
_render_hashes($dm)
|| _render_small($dm)
|| _set_cache_control($dm)
|| _redirect_project_ln_geo($dm)
|| _redirect_normalized($dm)
|| _render_stats($dm)
Expand Down Expand Up @@ -670,6 +671,12 @@ sub _render_small {
return 1;
}

# if we don't render file directly - we set max-age to short value, because redirect or metalink may change
sub _set_cache_control {
shift->c->res->headers->cache_control('public, max-age=300, must-revalidate');
return undef;
}

sub _render_hashes {
my $dm = shift;
my $c = $dm->c;
Expand Down
8 changes: 8 additions & 0 deletions t/environ/02-files.sh
Original file line number Diff line number Diff line change
Expand Up @@ -194,3 +194,11 @@ done
# test case insensitive:
$mc/curl -I /download/folder1/file1.1.dat | grep '302 Found'
$mc/curl -I /download/Folder1/file1.1.DAT | grep '200 OK'

echo check cache control
$mc/curl -I -H "Accept: */*, application/metalink+xml" /download/Folder1/repodata/repomd.xml | grep Cache-Control
$mc/curl -I /download/folder1/file1.1.dat | grep Cache-Control
$mc/curl -I /download/Folder1/file1.1.DAT | grep Cache-Control



9 changes: 9 additions & 0 deletions t/environ/03-headquarter-subsidiaries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -84,11 +84,20 @@ curl --interface $na_interface -Is http://$hq_address/download/folder1/filesmall
echo check huge files are redirected to FAKEURL
curl --interface $hq_interface -Is http://$hq_address/download/folder1/filehuge1.1.dat | grep "Location: http://$FAKEURL/folder1/filehuge1.1.dat"

echo test cache-control
curl --interface $na_interface -Is http://$hq_address/download/folder1/filebig1.1.dat | grep -i 'cache-control'
curl --interface $na_interface -Is http://$hq_address/download/folder1/filehuge1.1.dat | grep -i 'cache-control'
rc=0
curl --interface $na_interface -Is http://$hq_address/download/folder1/filesmall1.1.dat | grep -i 'cache-control' || rc=$?
test $rc -gt 0

echo check content-type
ct=$($mc9/curl -I /download/folder1/file.json | grep Content-Type)
[[ "$ct" =~ application/json ]]
ct=$($mc9/curl -I /folder1/file.json | grep Content-Type)
[[ "$ct" =~ application/json ]]

echo check file listiing
$mc9/curl /download/folder1/ | grep file.json
$mc9/curl /folder1/ | grep file.json

Expand Down
Loading