From 373116e970d4474d1154fdde9e9d0a033acd27e9 Mon Sep 17 00:00:00 2001 From: b Date: Mon, 9 Oct 2023 21:10:56 +0000 Subject: [PATCH] url query merge --- botm_common.pm | 91 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 59 insertions(+), 32 deletions(-) diff --git a/botm_common.pm b/botm_common.pm index 6d76275..69d6cd9 100644 --- a/botm_common.pm +++ b/botm_common.pm @@ -25,7 +25,7 @@ use Encode ('encode', 'decode'); use Exporter; -our $VERSION = '1.0.22'; +our $VERSION = '1.0.23'; our @ISA = qw(Exporter); our @EXPORT = (); our @EXPORT_OK = ( @@ -773,6 +773,57 @@ sub join_url { return $url; } +# URL query string + +sub url_query_encode { + (my $data, my $encoding) = @_; + my $query = ''; + + foreach my $ind (keys %$data) { + if ($query ne '') { + $query .= '&'; + } + $query .= url_encode($ind, $encoding) . '=' . url_encode($data->{$ind}, $encoding); + } + return $query; +} + +sub url_query_decode { + (my $query, my $encoding) = @_; + my %data; + + $query =~ s/\n$//s; + $query =~ s/\r$//s; + my @list = split('&', $query); + foreach my $entry (@list) { + (my $name, my $value) = split('=', $entry, 2); + $name = url_decode($name, $encoding); + $value = url_decode($value, $encoding); + $data{$name} = $value; + } + return %data; +} + +# merge + +sub merge_url_query { + (my $encoding, my @querys) = @_; + my %final_data; + foreach my $query (@querys) { + my %data; + if (ref($query) { + %data = %$query; + } + else { + %data = url_query_decode($query, $encoding); + } + foreach my $ind (keys %data) { + $final_data{$ind} = $data{$ind}; + } + } + return url_query_encode(\%data, $encoding); +} + # merge_url() will create a new URL based on # $base_url and $target_url. # both can be a string or hash reference, @@ -826,7 +877,12 @@ sub merge_url { } if ($target_data{'query'} ne '') { - $final_data{'query'} = $target_data{'query'}; + if ($target_data{'append_query'}) { + $final_data{'query'} = merge_url_query($target_data{'encoding'}, $final_data{'query'}, $target_data{'query'}); + } + else { + $final_data{'query'} = $target_data{'query'}; + } $final_data{'fragment'} = ''; } @@ -839,36 +895,7 @@ sub merge_url { return $base_url; } -# URL query string - -sub url_query_encode { - (my $data, my $encoding) = @_; - my $query = ''; - - foreach my $ind (keys %$data) { - if ($query ne '') { - $query .= '&'; - } - $query .= url_encode($ind, $encoding) . '=' . url_encode($data->{$ind}, $encoding); - } - return $query; -} - -sub url_query_decode { - (my $query, my $encoding) = @_; - my %data; - - $query =~ s/\n$//s; - $query =~ s/\r$//s; - my @list = split('&', $query); - foreach my $entry (@list) { - (my $name, my $value) = split('=', $entry, 2); - $name = url_decode($name, $encoding); - $value = url_decode($value, $encoding); - $data{$name} = $value; - } - return %data; -} +# why did I want this? # sub join_url_host { # (my $encoding, my @segments) = @_; -- 2.30.2