]> bicyclesonthemoon.info Git - botm/common-perl/commitdiff
improve merge_url() v1.0.21
authorb <rowerynaksiezycu@gmail.com>
Fri, 29 Sep 2023 21:18:42 +0000 (21:18 +0000)
committerb <rowerynaksiezycu@gmail.com>
Fri, 29 Sep 2023 21:18:42 +0000 (21:18 +0000)
botm_common.pm

index b818872b63f5c104464105988c306a1916f8b83d..82c62a626aa70c1f2aef798ee930c272916621b0 100644 (file)
@@ -25,7 +25,7 @@ use Encode ('encode', 'decode');
 
 use Exporter;
 
-our $VERSION     = '1.0.20';
+our $VERSION     = '1.0.21';
 our @ISA         = qw(Exporter);
 our @EXPORT      = ();
 our @EXPORT_OK   = (
@@ -781,57 +781,61 @@ sub join_url {
 # ".." in the path is not resolved so "/aaa/bbb" + "../ccc" will be
 # "/aaa/bbb/../ccc" and not "/aaa/ccc". TODO: improve?
 sub merge_url {
-       (my $base_url, my $target_url) = @_;
-       my %base_data;
-       my %target_data;
-       my %final_data;
-       my $final_url;
-       if (ref($base_url)) {
-               %base_data = %$base_url;
-       }
-       else {
-               %base_data = split_url($base_url);
-       }
-       if (ref($target_url)) {
-               %target_data = %$target_url;
-       }
-       else {
-               %target_data = split_url($target_url, 1);
-       }
-       
-       if (
-               ($target_data{'scheme'} ne '') or
-               ($target_data{'authority'} ne '') or
-               ($target_data{'host'} ne '')
-       ) {
-               return join_url(\%target_data);
-       }
-       
-       $final_data{'scheme'} = $base_data{'scheme'};
-       $final_data{'authority'} = $base_data{'authority'};
-       $final_data{'host'} = $base_data{'host'};
-       $final_data{'port'} = $base_data{'port'};
-       $final_data{'userinfo'} = $base_data{'userinfo'};
-       $final_data{'path'} = $base_data{'path'};
-       $final_data{'query'} = $base_data{'query'};
-       $final_data{'fragment'} = $base_data{'fragment'};
-       
-       if ($target_data{'path'} ne '') {
-               $final_data{'path'} = join_path('/', $base_data{'path'}, $target_data{'path'});
-               $final_data{'query'} = '';
-               $final_data{'fragment'} = '';
-       }
-       
-       if ($target_data{'query'} ne '') {
-               $final_data{'query'} = $target_data{'query'};
-               $final_data{'fragment'} = '';
-       }
-       
-       if ($target_data{'fragment'} ne '') {
-               $final_data{'fragment'} = $target_data{'fragment'};
+       (my $base_url, my @target_urls) = @_;
+       
+       foreach my $target_url (@target_urls) {
+               my %base_data;
+               my %target_data;
+               my %final_data;
+               if (ref($base_url)) {
+                       %base_data = %$base_url;
+               }
+               else {
+                       %base_data = split_url($base_url);
+               }
+               if (ref($target_url)) {
+                       %target_data = %$target_url;
+               }
+               else {
+                       %target_data = split_url($target_url, 1);
+               }
+               
+               if (
+                       ($target_data{'scheme'} ne '') or
+                       ($target_data{'authority'} ne '') or
+                       ($target_data{'host'} ne '')
+               ) {
+                       $base_url = join_url(\%target_data);
+                       next;
+               }
+               
+               $final_data{'scheme'} = $base_data{'scheme'};
+               $final_data{'authority'} = $base_data{'authority'};
+               $final_data{'host'} = $base_data{'host'};
+               $final_data{'port'} = $base_data{'port'};
+               $final_data{'userinfo'} = $base_data{'userinfo'};
+               $final_data{'path'} = $base_data{'path'};
+               $final_data{'query'} = $base_data{'query'};
+               $final_data{'fragment'} = $base_data{'fragment'};
+               
+               if ($target_data{'path'} ne '') {
+                       $final_data{'path'} = join_path('/', $base_data{'path'}, $target_data{'path'});
+                       $final_data{'query'} = '';
+                       $final_data{'fragment'} = '';
+               }
+               
+               if ($target_data{'query'} ne '') {
+                       $final_data{'query'} = $target_data{'query'};
+                       $final_data{'fragment'} = '';
+               }
+               
+               if ($target_data{'fragment'} ne '') {
+                       $final_data{'fragment'} = $target_data{'fragment'};
+               }
+               
+               $base_url = join_url(\%final_data);
        }
-       
-       return join_url(\%final_data);
+       return $base_url;
 }
 
 # URL query string