use Exporter;
-our $VERSION = '1.0.22';
+our $VERSION = '1.0.23';
our @ISA = qw(Exporter);
our @EXPORT = ();
our @EXPORT_OK = (
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,
}
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'} = '';
}
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) = @_;