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

Consider links when redirecting to the root #374

Merged
merged 1 commit into from
Apr 18, 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
11 changes: 6 additions & 5 deletions lib/MirrorCache/WebAPI/Plugin/Dir.pm
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ sub _render_from_db {
$dirname = $dm->root_subtree . ($folder_or_pattern? $path : $f->dirname) unless $dirname;
$c->log->error($c->dumper('dirname:', $dirname, 'path:', $path, 'trail:', $trailing_slash)) if $MCDEBUG;
if (my $folder = $rsFolder->find_folder_or_redirect($dirname)) {
$c->log->error('found redirect : ', $folder->{pathto}) if $MCDEBUG && $folder->{pathto};
$c->log->error("found redirect : $dirname -> ", $folder->{pathto}) if $MCDEBUG && $folder->{pathto};
# return $dm->redirect($folder->{pathto} . $trailing_slash) if $folder->{pathto};
my $folder_path = $folder->{pathto} ? $folder->{pathto} : $folder->{path};
return $c->render(status => 404, text => "path {$path} not found!!") unless $folder_path;
Expand All @@ -323,6 +323,7 @@ sub _render_from_db {
} else {
$realpath_subtree = $root->realpath($dm->root_subtree . ($folder_or_pattern? $path : $f->dirname)) // $dirname;
}
$c->log->error('RENDER - REALPATH_SUBTREE : ', $realpath_subtree) if $MCDEBUG;
if ($dirname eq $realpath_subtree) {
if ($dirname eq $f->dirname || $folder_or_pattern) {
$dm->folder_id($folder->{id});
Expand All @@ -331,17 +332,17 @@ sub _render_from_db {
}
} else {
my $another_folder = $rsFolder->find({path => $realpath_subtree});
$c->log->error($c->dumper('another_folder:', $another_folder->{id})) if $MCDEBUG;
$c->log->error($c->dumper('RENDER - another_folder:', $another_folder->id)) if $MCDEBUG;
return undef unless $another_folder || $it_must_be_folder; # nothing found, proceed to _guess_what_to_render
$dm->folder_id($another_folder->{id}) if $another_folder;
$dm->real_folder_id($another_folder->id) if $another_folder;
if ($folder->{id}) {
$dm->real_folder_id($folder->{id});
$dm->folder_id($folder->{id});
$dm->folder_sync_last($folder->{sync_last});
$dm->folder_scan_last($folder->{scan_last});
}
}
if ($it_must_be_folder && !$file_pattern_in_folder) {
$dm->real_folder_id($folder->{id}) if $folder->{id};
$dm->folder_id($folder->{id}) if $folder->{id};
return $c->mirrorcache->render_dir_mirrorlist($path, $dm) if $dm->mirrorlist;
return _render_dir($dm, $path, $rsFolder);
}
Expand Down
22 changes: 13 additions & 9 deletions lib/MirrorCache/WebAPI/Plugin/RenderFileFromMirror.pm
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,12 @@ sub register {
my $f = Mojo::File->new($filepath);
my $dirname = $f->dirname;
my $realfolder_id = $dm->real_folder_id;
$c->log->error($c->dumper('RENDER REAL ID: ', $realfolder_id)) if $MCDEBUG && $realfolder_id;
my $realdirname;
unless ($realfolder_id) {
$realdirname = $root->realpath($f->dirname);
$realdirname = $dirname unless $realdirname;
}
$realdirname = $dirname unless $realdirname;
my $basename = $f->basename;
$basename = $file->{name} if $file;

Expand All @@ -108,11 +109,14 @@ sub register {
}
$schema->resultset('Folder')->set_wanted($folder_id) if $need_update;
}
if (!$realfolder_id && $realdirname ne $dirname) {
if ($realfolder_id) {
my $realfolder = $schema->resultset('Folder')->find({id => $realfolder_id});
$realdirname = $realfolder->path if $realfolder;
} elsif (($realdirname // $dirname) ne $dirname) {
my $realfolder = $schema->resultset('Folder')->find({path => $realdirname});
$realfolder_id = $realfolder->id if $realfolder;
$c->log->error($c->dumper('RENDER FOLDER REAL', $realfolder_id ? $realfolder_id : 'NULL')) if $MCDEBUG;
}
$c->log->error($c->dumper('RENDER FOLDER REAL', $realfolder_id ? $realfolder_id : 'NULL')) if $MCDEBUG;
if ($folder || $realfolder_id) {
my $fldid = ($realfolder_id? $realfolder_id : $folder_id);
$folder_id = $fldid unless $folder_id;
Expand Down Expand Up @@ -159,7 +163,7 @@ sub register {
}
if ($dm->torrent || $dm->zsync || $dm->magnet) {
if ($baseurl) {
$fullurl = $baseurl . '/' . $filepath;
$fullurl = $baseurl . '/' . (($folder && $folder->path)? $folder->path : $realdirname) . '/' . $basename;
} else {
($fullurl = $c->req->url->to_abs->to_string) =~ s/\.(torrent|zsync|magnet)$//;
}
Expand Down Expand Up @@ -216,18 +220,18 @@ sub register {
$origin = $origin . ":" . $originurl->port if $originurl->port && $originurl->port != "80";
$origin = $origin . $dm->route;
}
$origin = $origin . $filepath;
$origin = $origin . (($folder && $folder->path)? $folder->path : $realdirname) . '/' . $basename;
my $xml;
if ($dm->meta4) {
$xml = _build_meta4(
$dm, (($folder && $folder->path)? $folder->path : $realdirname), $file, $country, $region, \@mirrors_country, \@mirrors_region,
$dm, $realdirname, $file, $country, $region, \@mirrors_country, \@mirrors_region,
\@mirrors_rest, $origin, 'MirrorCache', $baseurl);
$c->res->headers->content_disposition('attachment; filename="' .$basename. '.meta4"');
$c->render(data => $xml, format => 'meta4');
return 1;
}
$xml = _build_metalink(
$dm, (($folder && $folder->path)? $folder->path : $realdirname), $file, $country, $region, \@mirrors_country, \@mirrors_region,
$dm, $realdirname, $file, $country, $region, \@mirrors_country, \@mirrors_region,
\@mirrors_rest, $origin, 'MirrorCache', $baseurl);
$c->res->headers->content_disposition('attachment; filename="' .$basename. '.metalink"');
$c->render(data => $xml, format => 'metalink');
Expand All @@ -241,7 +245,7 @@ sub register {
if ($country and !$dm->avoid_countries || !(grep { $country eq $_ } $dm->avoid_countries)) {
for my $m (@mirrors_country) {
push @mirrordata,
{
{
url => $m->{url},
hostname => $m->{hostname},
location => uc($m->{country}),
Expand Down Expand Up @@ -301,7 +305,7 @@ sub register {
my $redirect = $root->redirect($dm, $filepath);
if ($redirect) {
$fileorigin = $redirect;
$fileoriginpath = ($folder? $folder->path : $realdirname) . '/' . $file->{name};
$fileoriginpath = $realdirname . '/' . $file->{name};
} else {
my $url = $c->req->url->to_abs;
$fileorigin = $dm->scheme . '://' . $url->host;
Expand Down
1 change: 1 addition & 0 deletions t/environ/02-files-hashes-import.sh
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,6 @@ mc6/curl -I /link1/child1/child1.1.dat | grep -E "$($ap4/print_address)|$($ap3/p
mc6/curl /link1/child1/child1.1.dat.meta4 \
| grep -C20 $($ap3/print_address)/link1/child1/child1.1.dat \
| grep -C20 $($ap3/print_address)/folder1/child1/child1.1.dat \
| grep -C20 $($mc9/print_address)/folder1/child1/child1.1.dat \

echo success
8 changes: 4 additions & 4 deletions t/environ/15-local-symlink-subtree-rootredirect-vpn.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ $mc/backstage/shoot
$mc/backstage/job mirror_scan_schedule
$mc/backstage/shoot

$mcsub/curl /tool/v1/file1.1.dat.metalink | grep -C10 notexists.mirror1.cc/folder1/file1.1.dat | grep -C10 notexists.mirror2.cc/updates/tool/v1/file1.1.dat | grep testhost.com/updates/tool/v1/file1.1.dat
$mcsub/curl /tool/v1/file1.1.dat.metalink | grep -C10 notexists.mirror1.cc/folder1/file1.1.dat | grep -C10 notexists.mirror2.cc/updates/tool/v1/file1.1.dat | grep testhost.com/folder1/file1.1.dat

rc=0
$mcsub/curl /tool/v1/file1.1.dat.metalink | grep $($ap7/print_address) || rc=$?
Expand All @@ -68,10 +68,10 @@ rc=0
$mcsub/curl /tool/v1/file1.1.dat.metalink | grep $($ap8/print_address) || rc=$?
test $rc -gt 0

$mcsub/curl /tool/v2/file1.1.dat.metalink | grep -C20 notexists.mirror2.cc/updates/tool/v2/file1.1.dat | grep testhost.com/updates/tool/v2/file1.1.dat
$mcsub/curl /tool/v2/file1.1.dat.metalink | grep -C20 notexists.mirror2.cc/updates/tool/v2/file1.1.dat | grep testhost.com/folder2/file1.1.dat


$mcsub/curl /tool/v1/file1.1.dat.mirrorlist | grep -C30 notexists.mirror1.cc/folder1/file1.1.dat | grep -C30 notexists.mirror2.cc/updates/tool/v1/file1.1.dat | grep testhost.com/updates/tool/v1/file1.1.dat
$mcsub/curl /tool/v1/file1.1.dat.mirrorlist | grep -C30 notexists.mirror1.cc/folder1/file1.1.dat | grep -C30 notexists.mirror2.cc/updates/tool/v1/file1.1.dat | grep testhost.com/folder1/file1.1.dat

rc=0
$mcsub/curl /tool/v1/file1.1.dat.mirrorlist | grep $($ap7/print_address) || rc=$?
Expand All @@ -80,7 +80,7 @@ rc=0
$mcsub/curl /tool/v1/file1.1.dat.mirrorlist | grep $($ap8/print_address) || rc=$?
test $rc -gt 0

$mcsub/curl /tool/v2/file1.1.dat.mirrorlist | grep -C30 notexists.mirror2.cc/updates/tool/v2/file1.1.dat | grep testhost.com/updates/tool/v2/file1.1.dat
$mcsub/curl /tool/v2/file1.1.dat.mirrorlist | grep -C30 notexists.mirror2.cc/updates/tool/v2/file1.1.dat | grep testhost.com/folder2/file1.1.dat

# test request from ROOT_COUNTRY is just served
$mc/curl -i /download/folder1/file1.1.dat?COUNTRY=us | grep '200 OK'
8 changes: 4 additions & 4 deletions t/environ/15-local-symlink-subtree-rootredirect.sh
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ $mc/backstage/shoot
$mc/backstage/job mirror_scan_schedule
$mc/backstage/shoot

$mcsub/curl /tool/v1/file1.1.dat.metalink | grep -C10 $($ap7/print_address)/folder1/file1.1.dat | grep -C10 $($ap8/print_address)/updates/tool/v1/file1.1.dat | grep testhost.com/updates/tool/v1/file1.1.dat
$mcsub/curl /tool/v2/file1.1.dat.metalink | grep -C10 $($ap8/print_address)/updates/tool/v2/file1.1.dat | grep testhost.com/updates/tool/v2/file1.1.dat
$mcsub/curl /tool/v1/file1.1.dat.metalink | grep -C10 $($ap7/print_address)/folder1/file1.1.dat | grep -C10 $($ap8/print_address)/updates/tool/v1/file1.1.dat | grep testhost.com/folder1/file1.1.dat
$mcsub/curl /tool/v2/file1.1.dat.metalink | grep -C10 $($ap8/print_address)/updates/tool/v2/file1.1.dat | grep testhost.com/folder2/file1.1.dat

$mcsub/curl /tool/v1/file1.1.dat.mirrorlist | grep -C30 $($ap7/print_address)/folder1/file1.1.dat | grep -C30 $($ap8/print_address)/updates/tool/v1/file1.1.dat | grep testhost.com/updates/tool/v1/file1.1.dat
$mcsub/curl /tool/v2/file1.1.dat.mirrorlist | grep -C30 $($ap8/print_address)/updates/tool/v2/file1.1.dat | grep testhost.com/updates/tool/v2/file1.1.dat
$mcsub/curl /tool/v1/file1.1.dat.mirrorlist | grep -C30 $($ap7/print_address)/folder1/file1.1.dat | grep -C30 $($ap8/print_address)/updates/tool/v1/file1.1.dat | grep testhost.com/folder1/file1.1.dat
$mcsub/curl /tool/v2/file1.1.dat.mirrorlist | grep -C30 $($ap8/print_address)/updates/tool/v2/file1.1.dat | grep testhost.com/folder2/file1.1.dat

# test request from ROOT_COUNTRY is just served
$mc/curl -i /download/folder1/file1.1.dat?COUNTRY=us | grep '200 OK'
78 changes: 78 additions & 0 deletions t/environ/23-folder-metalink.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!lib/test-in-container-environ.sh
set -exo pipefail

mc=$(environ mc $(pwd))

# $mc/gen_env MIRRORCACHE_BRANDING=openSUSE

$mc/start

ap8=$(environ ap8)
ap7=$(environ ap7)
ap6=$(environ ap6)
ap5=$(environ ap5)
ap4=$(environ ap4)
ap3=$(environ ap3)

for x in $mc $ap7 $ap8 $ap6 $ap5 $ap4 $ap3; do
mkdir -p $x/dt/{folder1,folder2,folder3}
mkdir -p $x/dt/project1/{folder1,folder2,folder3}
mkdir -p $x/dt/project2/{folder1,folder2,folder3}
echo $x/dt/{folder1,folder2,folder3}/{file1.1,file2.1}.dat | xargs -n 1 touch
echo $x/dt/project1/{folder1,folder2,folder3}/{file1.1,file2.1}.dat | xargs -n 1 touch
echo $x/dt/project2/{folder1,folder2,folder3}/{file1.1,file2.1}.dat | xargs -n 1 touch
done

$ap3/start
$ap4/start
$ap5/start
$ap6/start
$ap7/start
$ap8/start

# remove some files and folders
rm $ap7/dt/project1/folder2/file2.1.dat
rm $ap7/dt/project2/folder2/file2.1.dat
rm -r $ap5/dt/project2/folder2/
rm -r $ap5/dt/project1/
rm -r $ap4/dt/project2/

$mc/sql "insert into server(hostname,sponsor,sponsor_url,urldir,enabled,country,region) select '$($ap6/print_address)','sponsor1','www.sponsor.org','','t','us','na'"
$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'"
$mc/sql "insert into server(hostname,urldir,enabled,country,region) select '$($ap5/print_address)','','t','cn','as'"
$mc/sql "insert into server(hostname,urldir,enabled,country,region) select '$($ap4/print_address)','','t','jp','as'"
$mc/sql "insert into server(hostname,urldir,enabled,country,region) select '$($ap3/print_address)','','f','jp','as'"

$mc/sql "insert into project(name,path,etalon) select '2.0 1','/project2/folder1', 3"
$mc/sql "insert into project(name,path,etalon) select 'proj1','/project1', 3"
$mc/sql "insert into project(name,path,etalon) select '2.0 2','/project2/folder2', 3"

$mc/backstage/job -e folder_sync -a '["/project1/folder1"]'
$mc/backstage/job -e mirror_scan -a '["/project1/folder1"]'
$mc/backstage/job -e folder_sync -a '["/project1/folder2"]'
$mc/backstage/job -e mirror_scan -a '["/project1/folder2"]'
$mc/backstage/job -e folder_sync -a '["/project2/folder1"]'
$mc/backstage/job -e mirror_scan -a '["/project2/folder1"]'
$mc/backstage/job -e folder_sync -a '["/project2/folder2"]'
$mc/backstage/job -e mirror_scan -a '["/project2/folder2"]'
$mc/backstage/shoot

$mc/backstage/job -e report -a '["once"]'
$mc/backstage/shoot

$mc/curl /report/mirrors | tidy --drop-empty-elements no | \
grep -A5 -F '<div class="repo">' | \
grep -A4 -F '<a class="repouncertain"' | \
grep -A3 -F '"diff in: /project2/folder2"' | \
grep -A2 -F '"http://127.0.0.1:1304/project2/folder2">' | \
grep -C3 '\b2\b' | \
grep -C3 -F '</a>'


rc=0
# no disabled mirror in the report
$mc/curl /report/mirrors | grep $($ap3/print_address) || rc=$?
test $rc -gt 0

echo success