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

patching #57, $uri->canonical unconditionally returns a clone #58

Merged
merged 1 commit into from
Jan 8, 2019
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
31 changes: 18 additions & 13 deletions lib/URI.pm
Original file line number Diff line number Diff line change
Expand Up @@ -302,21 +302,22 @@ sub canonical
# Make sure scheme is lowercased, that we don't escape unreserved chars,
# and that we use upcase escape sequences.

my $self = shift;
my $scheme = $self->_scheme || "";
# We now clone unconditionally; see
# https://github.com/libwww-perl/URI/issues/57

my $other = $_[0]->clone;
my $scheme = $other->_scheme || "";
my $uc_scheme = $scheme =~ /[A-Z]/;
my $esc = $$self =~ /%[a-fA-F0-9]{2}/;
return $self unless $uc_scheme || $esc;
my $esc = $$other =~ /%[a-fA-F0-9]{2}/;
return $other unless $uc_scheme || $esc;

$other->_scheme(lc $scheme) if $uc_scheme;

my $other = $self->clone;
if ($uc_scheme) {
$other->_scheme(lc $scheme);
}
if ($esc) {
$$other =~ s{%([0-9a-fA-F]{2})}
{ my $a = chr(hex($1));
$$other =~ s{%([0-9a-fA-F]{2})}
{ my $a = chr(hex($1));
$a =~ /^[$unreserved]\z/o ? $a : "%\U$1"
}ge;
}ge;
}
return $other;
}
Expand Down Expand Up @@ -571,8 +572,12 @@ removing the explicit port specification if it matches the default port,
uppercasing all escape sequences, and unescaping octets that can be
better represented as plain characters.

For efficiency reasons, if the $uri is already in normalized form,
then a reference to it is returned instead of a copy.
Before version 1.75, this method would return the original unchanged
C<$uri> object if it detected nothing to change. To make the return
value consistent (and since the efficiency gains from this behaviour
were marginal), this method now unconditionally returns a clone. This
means idioms like C<< $uri->clone->canonical >> are no longer
necessary.

=item $uri->eq( $other_uri )

Expand Down
8 changes: 7 additions & 1 deletion t/generic.t
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use strict;
use warnings;

print "1..48\n";
print "1..49\n";

use URI;
use Scalar::Util qw(refaddr);

my $foo = URI->new("Foo:opaque#frag");

Expand Down Expand Up @@ -217,3 +218,8 @@ $old = $foo->query("q");
print "not " unless !defined($old) && $foo eq "?q";
print "ok 48\n";

# canonical must always be a clone
my $c1 = $foo->canonical; # canonicalize first
my $c2 = $c1->canonical; # canonicalize again
print 'not ' if refaddr($c1) == refaddr($c2) or $$c1 ne $$c2;
print "ok 49\n";