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 parameter METALINK_GREEDY #300

Merged
merged 2 commits into from
Sep 21, 2022
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
1 change: 1 addition & 0 deletions lib/MirrorCache/Datamodule.pm
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ sub redirect($self, $url) {
}

sub _init_headers($self) {
$self->_agent('');
my $headers = $self->c->req->headers;
return unless $headers;
$self->_agent($headers->user_agent ? $headers->user_agent : '');
Expand Down
10 changes: 9 additions & 1 deletion lib/MirrorCache/WebAPI/Plugin/Dir.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ sub register {
return $app;
}

# Assume UserAgent of such pattern never lists directories and only needs to render files
my $PACKAGE_MANAGER_PATTERN = 'ZYpp .*|Debian APT.*|libdnf.*|osc.*';

sub indx {
my $c = shift;
my $reqpath = $c->req->url->path;
Expand Down Expand Up @@ -75,6 +78,11 @@ sub indx {
|| _render_from_db($dm)
|| _local_render($dm, 1); # check if we should render local when metalink cannot be provided

if ($dm->agent =~ qr/$PACKAGE_MANAGER_PATTERN/) {
my ($path, $trailing_slash) = $dm->path;
return $root->render_file($dm, $path . $trailing_slash);
}

my $tx = $c->render_later->tx;
my $rendered;
my $handle_error = sub {
Expand Down Expand Up @@ -366,7 +374,7 @@ sub _guess_what_to_render {
# this should happen only if $url is a valid file or non-existing path
return $root->render_file($dm, $path . $trailing_slash);
})->catch(sub {
my $res = $root->render_file($dm, $path . $trailing_slash);
$root->render_file($dm, $path . $trailing_slash);
my $msg = "Error while guessing how to render $url: ";
if (1 == scalar(@_)) {
$msg = $msg . $_[0];
Expand Down
13 changes: 10 additions & 3 deletions lib/MirrorCache/WebAPI/Plugin/RenderFileFromMirror.pm
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,9 @@ sub register {
return $app;
}

# metalink should not include root url in mirror list if mirror count exceeds METALINK_GREEDY parameter
my $METALINK_GREEDY = int( $ENV{MIRRORCACHE_METALINK_GREEDY} // 0 ) // 0;

sub _build_metalink() {
my (
$dm, $path, $file, $country, $region, $mirrors_country,
Expand Down Expand Up @@ -423,9 +426,13 @@ sub _build_metalink() {
return if $root_included and !$print;

$writer->comment("File origin location: ") if $print;
$writer->startTag('url', type => substr($rooturl,0,$colon), location => uc($dm->root_country), preference => $preference);
$writer->characters($rooturl . $fullname);
$writer->endTag('url');
if ($METALINK_GREEDY && $METALINK_GREEDY <= (100 - $preference)) {
$writer->comment($rooturl . $fullname);
} else {
$writer->startTag('url', type => substr($rooturl,0,$colon), location => uc($dm->root_country), preference => $preference);
$writer->characters($rooturl . $fullname);
$writer->endTag('url');
}
$root_included = 1;
$preference--;
};
Expand Down
10 changes: 9 additions & 1 deletion t/environ/01-smoke-mirror-hasall-remote.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ mc=$(environ mc $(pwd))

$mc/gen_env \
MIRRORCACHE_ROOT=http://$($ap9/print_address) \
MIRRORCACHE_METALINK_GREEDY=3 \
MIRRORCACHE_REDIRECT=$FAKEURL2

$mc/start
Expand All @@ -31,7 +32,7 @@ $ap7/curl /folder1/ | grep file1.1.dat

$ap8/start
$ap8/curl /folder1/ | grep file1.1.dat

rm $ap7/dt/folder1/file2.1.dat # remove a file from ap7

$mc/sql "insert into server(hostname,urldir,enabled,country,region) select '$($ap7/print_address)','','t','us','na'"
$mc/sql "insert into server(hostname,urldir,enabled,country,region) select '$($ap8/print_address)','','t','de','eu'"
Expand All @@ -55,4 +56,11 @@ $mc/curl /download/folder1/file1.1.dat.metalink | grep "${FAKEURL2}"/folder1/fil
rc=0
$mc/curl -I /download/folder1/file1.1.dat?"COUNTRY=it&PEDANTIC=1" | grep "${FAKEURL}" || rc=$?
test $rc -gt 0

echo When METALINK_GREEDY is set, REDIRECT url will appear only as comment if mirror count exceeds value of METALINK_GREEDY
$mc/curl /download/folder1/file1.1.dat.metalink | grep -A1 'File origin' | grep '<!-- http://'${FAKEURL2}'/folder1/file1.1.dat -->'

echo for file2 REDIRECT still appears in metalink from folder2, because mirror count doesnt exceed value of METALINK_GREEDY
$mc/curl /download/folder1/file2.1.dat.metalink | grep -A1 'File origin' | grep '<url type="http" location="" preference="98">http://'${FAKEURL2}'/folder1/file2.1.dat</url>'

echo success