From bd6107e790c6666a87a0e9f2bec9370868bf531b Mon Sep 17 00:00:00 2001 From: b Date: Fri, 29 Sep 2023 21:18:42 +0000 Subject: [PATCH] improve merge_url() --- botm_common.pm | 106 +++++++++++++++++++++++++------------------------ 1 file changed, 55 insertions(+), 51 deletions(-) diff --git a/botm_common.pm b/botm_common.pm index b818872..82c62a6 100644 --- a/botm_common.pm +++ b/botm_common.pm @@ -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 -- 2.30.2