Skip to content

Commit

Permalink
Redirect all local symlinks
Browse files Browse the repository at this point in the history
Before this the redirect happened only for limited patterns e.g. Media and Current
But since there were introduced many links without these patterns (e.g. #523),
it was decided to change behavior.
  • Loading branch information
andrii-suse committed Oct 23, 2024
1 parent 9e803d1 commit 5f1b8ae
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 34 deletions.
55 changes: 32 additions & 23 deletions lib/MirrorCache/WebAPI/Plugin/Dir.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ sub indx {
$success =
_render_hashes($dm)
|| _render_if_not_modified($dm)
|| _render_small($dm)
|| _set_cache_control($dm)
|| _redirect_project_ln_geo($dm)
|| _redirect_ln($dm)
|| _render_small($dm)
|| _redirect_project_geo($dm)
|| _redirect_normalized($dm)
|| _render_stats($dm)
|| _local_render($dm, 0) # check if we should render local
Expand Down Expand Up @@ -224,11 +225,38 @@ sub _render_dir {
}

# this combines similar checks for redirecting as specified in DB links and projects, as well as subsidiaries
sub _redirect_project_ln_geo {
sub _redirect_ln {
my $dm = shift;
my ($path, $trailing_slash) = $dm->path;
return undef if $trailing_slash || !$dm->pedantic;

my $c = $dm->c;
$c->log->error($c->dumper('DIR::render_ln')) if $MCDEBUG;
my ($ln, $etag, $version) = $root->detect_ln_in_the_same_folder($dm->original_path);
my $extra = 1;
unless ($ln) {
($ln, $etag, $version) = $root->detect_ln_in_the_same_folder($path);
$extra = 0;
}
$c->log->error("ln for $path : " . ($ln // 'null')) if $MCDEBUG;
if ($ln) {
# redirect to the symlink
$c->log->error('redirect detected: ' . $ln . ": " . $c->dumper($dm->accept_all, $dm->accept)) if $MCDEBUG;
$c->res->headers->etag($etag) if $etag;
$c->res->headers->add('X-MEDIA-VERSION' => $version) if $version;
$dm->redirect($dm->route . $ln, $extra && ($dm->accept_all || !$dm->accept));
return 1;
}
return undef;
}

# this combines similar checks for redirecting as specified in DB links and projects, as well as subsidiaries
sub _redirect_project_geo {
my $dm = shift;
my ($path, $trailing_slash) = $dm->path;

my $c = $dm->c;
$c->log->error($c->dumper('DIR::render_project_ln_geo')) if $MCDEBUG;
# each project may have a redirect defined in DB, so all requests are redirected for it
unless ($trailing_slash) {
my $redirect = $c->mcproject->redirect($path, $dm->region);
Expand All @@ -237,25 +265,6 @@ sub _redirect_project_ln_geo {
$c->stat->redirect_to_region($dm);
return 1;
}

$c->log->error('pedantic: ' . ($dm->pedantic // 'undef')) if $MCDEBUG;
if ($path =~ m/(GNOME_.*|.*(Media|[C|c]urrent|Next))\.iso(\.sha256(\.asc)?)?/ && $dm->pedantic) {
my ($ln, $etag, $version) = $root->detect_ln_in_the_same_folder($dm->original_path);
my $extra = 1;
unless ($ln) {
($ln, $etag, $version) = $root->detect_ln_in_the_same_folder($path);
$extra = 0;
}
$c->log->error("ln for $path : " . ($ln // 'null')) if $MCDEBUG;
if ($ln) {
# redirect to the symlink
$c->log->error('redirect detected: ' . $ln . ": " . $c->dumper($dm->accept_all, $dm->accept)) if $MCDEBUG;
$c->res->headers->etag($etag) if $etag;
$c->res->headers->add('X-MEDIA-VERSION' => $version) if $version;
$dm->redirect($dm->route . $ln, $extra && ($dm->accept_all || !$dm->accept));
return 1;
}
}
}
return undef if $trailing_slash || $path eq '/' || $dm->mirrorlist;
return undef if $dm->must_render_from_root;
Expand Down Expand Up @@ -779,7 +788,7 @@ sub _render_small {
my $c=$dm->c;
$c->log->error('DIR::render_small1') if $MCDEBUG;
return undef unless ($small_file_size && ($root_nfs || !$root->is_remote));
$dm->_init_path;
# $dm->_init_path;
$c->log->error('DIR::render_small2') if $MCDEBUG;
return undef if ($dm->metalink && !$dm->accept_all) || ($dm->meta4 && !$dm->accept_all) || $dm->mirrorlist || ($dm->zsync && !$dm->accept_all);
$c->log->error('DIR::render_small3') if $MCDEBUG;
Expand Down
21 changes: 13 additions & 8 deletions t/environ/03-headquarter-subsidiaries-hashes.sh
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ for i in 9 6 7 8; do
( cd mc$i/dt/folder1/ && ln -s file4.1.dat file-Media.iso )
( cd mc$i/dt/folder1/ && ln -s file4.1.dat xcurr.dat )
( cd mc$i/dt/folder1/ && ln -s file4.1.dat.zsync xcurr.dat.zsync )
( cd mc$i/dt/folder1/ && ln -s file4.1.dat xfile-NoCloud.qcow2 )
mc$i/backstage/job -e folder_sync -a '["/folder1"]'
mc$i/backstage/shoot
mc$i/backstage/shoot -q hashes
Expand All @@ -87,6 +88,7 @@ for i in 9 6 7 8; do
ext=""
[ -z "$x" ] || ext=.$x
mc$i/curl -I /folder1/file-Media.iso$ext | grep -C 10 302 | grep /folder1/file4.1.dat$ext | grep -v /download/folder1/file4.1.dat
mc$i/curl -I /folder1/xfile-NoCloud.qcow2$ext | grep -C 10 302 | grep /folder1/file4.1.dat$ext | grep -v /download/folder1/file4.1.dat
done
done

Expand All @@ -102,11 +104,11 @@ for i in 6 7 8 9; do
mc$i/backstage/shoot
MIRRORCACHE_HASHES_IMPORT_RETRY_DELAY=$DELAY mc$i/backstage/shoot -q hashes
if test $i != 9; then
test -z $(mc$i/sql "select md5 from hash where file_id=8")
test -z $(mc$i/sql "select md5 from hash where file_id=9")
test -z $(mc$i/sql "select md5 from hash where file_id=10")
else
test $(mc$i/sql "select md5 from hash where file_id=8") == $(mc$i/sql 'select md5 from hash where file_id=9')
test $(mc$i/sql "select md5 from hash where file_id=3") != $(mc$i/sql 'select md5 from hash where file_id=8')
test $(mc$i/sql "select md5 from hash where file_id=9") == $(mc$i/sql 'select md5 from hash where file_id=10')
test $(mc$i/sql "select md5 from hash where file_id=3") != $(mc$i/sql 'select md5 from hash where file_id=9')
fi
done

Expand All @@ -118,9 +120,12 @@ mc9/curl -I /download/folder1/file-Media.iso?metalink | grep 'Location: /downl
mc9/curl -I /download/folder1/file-Media.iso.mirrorlist | grep 'Location: /download/folder1/file4.1.dat\.mirrorlist'
mc9/curl -I /download/folder1/file-Media.iso?mirrorlist | grep 'Location: /download/folder1/file4.1.dat?mirrorlist='

mc9/curl -I /download/folder1/xcurr.dat | grep "Location: http://$na_address/download/folder1/xcurr.dat"
mc9/curl -I /download/folder1/xcurr.dat.metalink | grep "Location: http://$na_address/download/folder1/xcurr.dat.metalink"
mc9/curl -I /download/folder1/xcurr.dat?meta4 | grep "Location: http://$na_address/download/folder1/xcurr.dat?meta4"
# mc9/curl -I /download/folder1/xcurr.dat | grep "Location: http://$na_address/download/folder1/xcurr.dat"
# mc9/curl -I /download/folder1/xcurr.dat.metalink | grep "Location: http://$na_address/download/folder1/xcurr.dat.metalink"
# mc9/curl -I /download/folder1/xcurr.dat?meta4 | grep "Location: http://$na_address/download/folder1/xcurr.dat?meta4"
mc9/curl -I /download/folder1/xcurr.dat | grep 'Location: /download/folder1/file4.1.dat'
mc9/curl -I /download/folder1/xcurr.dat.metalink | grep 'Location: /download/folder1/file4.1.dat.metalink'
mc9/curl -I /download/folder1/xcurr.dat?meta4 | grep 'Location: /download/folder1/file4.1.dat?meta4'
mc9/curl -I /download/folder1/xcurr.dat.mirrorlist | grep '/folder1/file4.1.dat.mirrorlist'


Expand All @@ -137,8 +142,8 @@ mc6/curl -I /download/folder1/xcurr.dat.mirrorlist | grep '/folder1/file4.1.dat\
# now the hashes on subsidiaries should be retried and match the headquarter
for i in 6 7 8; do
mc$i/backstage/shoot -q hashes
test $(mc$i/sql "select md5 from hash where file_id=8") == $(mc9/sql 'select md5 from hash where file_id=9')
test $(mc$i/sql "select md5 from hash where file_id=3") != $(mc9/sql 'select md5 from hash where file_id=9')
test $(mc$i/sql "select md5 from hash where file_id=9") == $(mc9/sql 'select md5 from hash where file_id=10')
test $(mc$i/sql "select md5 from hash where file_id=3") != $(mc9/sql 'select md5 from hash where file_id=10')
done

echo success
4 changes: 1 addition & 3 deletions t/environ/03-headquarter-subsidiaries.sh
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,7 @@ curl --interface $hq_interface -Is http://$hq_address/download/folder1/filehuge1
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
curl --interface $na_interface -Is http://$hq_address/download/folder1/filesmall1.1.dat | grep -i 'cache-control'

echo check content-type
ct=$($mc9/curl -I /download/folder1/file.json | grep Content-Type)
Expand Down

0 comments on commit 5f1b8ae

Please sign in to comment.