Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
added experimental canonicalize method to Mojo::URL
  • Loading branch information
kraih committed Nov 5, 2011
1 parent 06dd6f4 commit 5b454d2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Changes
@@ -1,6 +1,8 @@
This file documents the revision history for Perl extension Mojolicious.

2.24 2011-11-05 00:00:00
- Added EXPERIMENTAL canonicalize method to Mojo::URL.
- Fixed small path canonicalization bug in Mojo::URL.
- Fixed small trailing slash bug in Mojo::Path.

2.23 2011-11-04 00:00:00
Expand Down
27 changes: 24 additions & 3 deletions lib/Mojo/URL.pm
Expand Up @@ -59,6 +59,18 @@ sub authority {
return $authority;
}

sub canonicalize {
my $self = shift;

$self->path->canonicalize;
return $self unless my $port = $self->port;
return $self unless my $scheme = $self->scheme;
$self->port(undef) if $port eq '80' && $scheme ~~ [qw/http ws/];
$self->port(undef) if $port eq '443' && $scheme ~~ [qw/https wss/];

return $self;
}

sub clone {
my $self = shift;

Expand Down Expand Up @@ -214,7 +226,6 @@ sub to_abs {
}

# Absolute path
$new->canonicalize;
$new->leading_slash(1);
$new->trailing_slash($old->trailing_slash);
$abs->path($new);
Expand Down Expand Up @@ -388,6 +399,16 @@ following new ones.
Construct a new L<Mojo::URL> object.
=head2 C<canonicalize>
$url = $url->canonicalize;
Canonicalize URL.
Note that this method is EXPERIMENTAL and might change without warning!
# "http://mojolicio.us/foo/baz"
say Mojo::URL->new('http://mojolicio.us:80/foo/bar/../baz')->canonicalize;
=head2 C<clone>
my $url2 = $url->clone;
Expand Down Expand Up @@ -453,14 +474,14 @@ Query part of this URL, defaults to a L<Mojo::Parameters> object.
my $abs = $url->to_abs;
my $abs = $url->to_abs(Mojo::URL->new('http://kraih.com/foo'));
Turn relative URL into an absolute one.
Clone relative URL and turn it into an absolute one.
=head2 C<to_rel>
my $rel = $url->to_rel;
my $rel = $url->to_rel(Mojo::URL->new('http://kraih.com/foo'));
Turn absolute URL into a relative one.
Clone absolute URL and turn it into a relative one.
=head2 C<to_string>
Expand Down
59 changes: 50 additions & 9 deletions t/mojo/url.t
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 346;
use Test::More tests => 366;

# "I don't want you driving around in a car you built yourself.
# You can sit there complaining, or you can knit me some seat belts."
Expand Down Expand Up @@ -130,11 +130,15 @@ is $rel->to_abs, 'http://kraih.com/', 'right absolute version';
is $rel->to_abs->to_rel, '', 'right relative version';
$rel = $url->to_rel(Mojo::URL->new('http://kraih.com/a/'));
is $rel, '..', 'right relative version';
is $rel->to_abs, 'http://kraih.com/', 'right absolute version';
is $rel->to_abs, 'http://kraih.com/a/..', 'right absolute version';
is $rel->to_abs->canonicalize, 'http://kraih.com/',
'right canonicalized version';
is $rel->to_abs->to_rel, '..', 'right relative version';
$rel = $url->to_rel(Mojo::URL->new('http://kraih.com/a/b/'));
is $rel, '../..', 'right relative version';
is $rel->to_abs, 'http://kraih.com/', 'right absolute version';
is $rel->to_abs, 'http://kraih.com/a/b/../..', 'right absolute version';
is $rel->to_abs->canonicalize, 'http://kraih.com/',
'right canonicalized version';
is $rel->to_abs->to_rel, '../..', 'right relative version';
$url = Mojo::URL->new('http://kraih.com/index.html');
$rel = $url->to_rel(Mojo::URL->new('http://kraih.com/'));
Expand All @@ -144,12 +148,17 @@ is $rel->to_abs->to_rel, 'index.html', 'right relative version';
$url = Mojo::URL->new('http://kraih.com/index.html');
$rel = $url->to_rel(Mojo::URL->new('http://kraih.com/a/'));
is $rel, '../index.html', 'right relative version';
is $rel->to_abs, 'http://kraih.com/index.html', 'right absolute version';
is $rel->to_abs, 'http://kraih.com/a/../index.html', 'right absolute version';
is $rel->to_abs->canonicalize, 'http://kraih.com/index.html',
'right canonicalized version';
is $rel->to_abs->to_rel, '../index.html', 'right relative version';
$url = Mojo::URL->new('http://kraih.com/index.html');
$rel = $url->to_rel(Mojo::URL->new('http://kraih.com/a/b/'));
is $rel, '../../index.html', 'right relative version';
is $rel->to_abs, 'http://kraih.com/index.html', 'right absolute version';
is $rel->to_abs, 'http://kraih.com/a/b/../../index.html',
'right absolute version';
is $rel->to_abs->canonicalize, 'http://kraih.com/index.html',
'right canonicalized version';
is $rel->to_abs->to_rel, '../../index.html', 'right relative version';
$url = Mojo::URL->new('http://kraih.com/a/b/c/index.html');
$rel = $url->to_rel(Mojo::URL->new('http://kraih.com/a/b/'));
Expand Down Expand Up @@ -191,23 +200,31 @@ is $url->to_abs, 'http://kraih.com/foo?foo=bar#23', 'right absolute version';
$url = Mojo::URL->new('../cages/birds.gif');
$url->base->parse('http://www.aviary.com/products/intro.html');
ok !$url->is_abs, 'not absolute';
is $url->to_abs, 'http://www.aviary.com/cages/birds.gif',
is $url->to_abs, 'http://www.aviary.com/products/../cages/birds.gif',
'right absolute version';
is $url->to_abs->canonicalize, 'http://www.aviary.com/cages/birds.gif',
'right canonicalized version';
$url = Mojo::URL->new('.././cages/./birds.gif');
$url->base->parse('http://www.aviary.com/./products/./intro.html');
ok !$url->is_abs, 'not absolute';
is $url->to_abs, 'http://www.aviary.com/cages/birds.gif',
is $url->to_abs, 'http://www.aviary.com/./products/./.././cages/./birds.gif',
'right absolute version';
is $url->to_abs->canonicalize, 'http://www.aviary.com/cages/birds.gif',
'right canonicalized version';

# Absolute with path
$url = Mojo::URL->new('../foo?foo=bar#23');
$url->base->parse('http://kraih.com/bar/baz/');
ok !$url->is_abs, 'not absolute';
is $url->to_abs, 'http://kraih.com/bar/foo?foo=bar#23',
is $url->to_abs, 'http://kraih.com/bar/baz/../foo?foo=bar#23',
'right absolute version';
is $url->to_abs->canonicalize, 'http://kraih.com/bar/foo?foo=bar#23',
'right canonicalized version';
is $url->to_abs->to_rel, '../foo?foo=bar#23', 'right relative version';
is $url->to_abs->to_rel->to_abs, 'http://kraih.com/bar/foo?foo=bar#23',
is $url->to_abs->to_rel->to_abs, 'http://kraih.com/bar/baz/../foo?foo=bar#23',
'right absolute version';
is $url->to_abs->canonicalize, 'http://kraih.com/bar/foo?foo=bar#23',
'right canonicalized version';
is $url->to_abs->base, 'http://kraih.com/bar/baz/', 'right base';

# Real world tests
Expand Down Expand Up @@ -581,3 +598,27 @@ $url = Mojo::URL->new('http://mojolicio.us/100%25_fun');
is $url->path->parts->[0], '100%_fun', 'right part';
is $url->path, '/100%25_fun', 'right path';
is "$url", 'http://mojolicio.us/100%25_fun', 'right format';

# Canonicalize
$url = Mojo::URL->new('http://mojolicio.us:80/foo/../bar');
is "$url", 'http://mojolicio.us:80/foo/../bar', 'right format';
is $url->canonicalize, 'http://mojolicio.us/bar',
'right canonicalized version';
$url = Mojo::URL->new('https://mojolicio.us:443');
is "$url", 'https://mojolicio.us:443', 'right format';
is $url->canonicalize, 'https://mojolicio.us', 'right canonicalized version';
$url = Mojo::URL->new('http://mojolicio.us:8080/foo/../bar/');
is "$url", 'http://mojolicio.us:8080/foo/../bar/', 'right format';
is $url->canonicalize, 'http://mojolicio.us:8080/bar/',
'right canonicalized version';
$url = Mojo::URL->new('ws://mojolicio.us:80/foo/bar/../');
is "$url", 'ws://mojolicio.us:80/foo/bar/../', 'right format';
is $url->canonicalize, 'ws://mojolicio.us/foo/',
'right canonicalized version';
$url = Mojo::URL->new('wss://mojolicio.us:443/foo/bar/./');
is "$url", 'wss://mojolicio.us:443/foo/bar/./', 'right format';
is $url->canonicalize, 'wss://mojolicio.us/foo/bar/',
'right canonicalized version';
$url = Mojo::URL->new('/foo/bar/..');
is "$url", '/foo/bar/..', 'right format';
is $url->canonicalize, '/foo', 'right canonicalized version';

0 comments on commit 5b454d2

Please sign in to comment.