Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
fixed a few small bugs in Mojo::URL
  • Loading branch information
kraih committed Nov 8, 2011
1 parent 729ebcb commit 640fdf2
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 47 deletions.
1 change: 1 addition & 0 deletions Changes
Expand Up @@ -3,6 +3,7 @@ This file documents the revision history for Perl extension Mojolicious.
2.25 2011-11-08 00:00:00
- Improved documentation.
- Fixed URL without scheme handling in Mojo::URL.
- Fixed a few small bugs in Mojo::URL.

2.24 2011-11-05 00:00:00
- Added EXPERIMENTAL canonicalize method to Mojo::URL.
Expand Down
1 change: 1 addition & 0 deletions lib/Mojo/Path.pm
Expand Up @@ -44,6 +44,7 @@ sub canonicalize {
# Part
push @parts, $part;
}
$self->trailing_slash(undef) unless @parts;
$self->parts(\@parts);

return $self;
Expand Down
49 changes: 29 additions & 20 deletions lib/Mojo/URL.pm
Expand Up @@ -201,45 +201,54 @@ sub to_abs {
my $self = shift;
my $base = shift || $self->base->clone;

# Absolute URL
# Scheme
my $abs = $self->clone;
return $abs if $abs->is_abs;
$abs->scheme($base->scheme);
$abs->authority($base->authority) unless $abs->authority;

# New base
$abs->base($base->clone);
my $new = $base->path->clone;
# Authority
return $abs if $abs->authority;
$abs->authority($base->authority);

# Replace path
my $old = $self->path;
if ($old->leading_slash) { $new->parts([@{$old->parts}]) }
# Absolute path
my $path = $abs->path;
return $abs if $path->leading_slash;

# Inherit path
my $base_path = $base->path;
if (!@{$path->parts}) {
$path = $abs->path($base_path->clone)->path->trailing_slash(0);

# Query
return $abs if length($abs->query->to_string);
$abs->query($base->query->clone);
}

# Merge paths
else {
my $new = $base_path->clone;
$new->leading_slash(1);

# Characters after the right-most '/' need to go
pop @{$new->parts} unless $new->trailing_slash;
push @{$new->parts}, @{$old->parts};
pop @{$new->parts} if @{$path->parts} && !$new->trailing_slash;
push @{$new->parts}, @{$path->parts};
$new->trailing_slash($path->trailing_slash) if @{$new->parts};
$abs->path($new);
}

# Absolute path
$new->leading_slash(1);
$new->trailing_slash($old->trailing_slash);
$abs->path($new);

return $abs;
}

sub to_rel {
my $self = shift;
my $base = shift || $self->base->clone;

# Relative URL
# Scheme and authority
my $rel = $self->clone->base($base);
$rel->scheme(undef)->userinfo(undef)->host(undef)->port(undef);
$rel->scheme(undef);
$rel->userinfo(undef)->host(undef)->port(undef) if $base->authority;

# Build relative path
# Path
my @rel_parts = @{$rel->path->parts};
my $base_path = $base->path;
my @base_parts = @{$base_path->parts};
Expand All @@ -248,8 +257,8 @@ sub to_rel {
shift @rel_parts;
shift @base_parts;
}
$rel->path(Mojo::Path->new);
my $rel_path = $rel->path;
my $rel_path = $rel->path(Mojo::Path->new)->path;
$rel_path->leading_slash(1) if $rel->authority;
$rel_path->parts([('..') x @base_parts, @rel_parts]);
$rel_path->trailing_slash(1) if $self->path->trailing_slash;

Expand Down
8 changes: 4 additions & 4 deletions t/mojo/request.t
Expand Up @@ -1364,10 +1364,10 @@ is $req->version, '1.1', 'right version';
ok $req->at_least_version('1.0'), 'at least version 1.0';
ok !$req->at_least_version('1.2'), 'not version 1.2';
is $req->url, '//127.0.0.1:3000', 'right URL';
is $req->url->host, '127.0.0.1', 'right host';
is $req->url->port, '3000', 'right port';
is $req->url->to_abs, 'http://127.0.0.1:3000/', 'right absolute URL';
is $req->proxy->userinfo, 'Aladdin:open sesame', 'right proxy userinfo';
is $req->url->host, '127.0.0.1', 'right host';
is $req->url->port, '3000', 'right port';
is $req->url->to_abs, 'http://127.0.0.1:3000', 'right absolute URL';
is $req->proxy->userinfo, 'Aladdin:open sesame', 'right proxy userinfo';
is $req->headers->authorization, 'Basic QWxhZGRpbjpvcGVuIHNlc2FtZQ==',
'right "Authorization" value';
is $req->headers->host, '127.0.0.1:3000', 'right "Host" value';
Expand Down
13 changes: 6 additions & 7 deletions t/mojo/request_cgi.t
Expand Up @@ -361,25 +361,24 @@ is $req->url->to_abs->to_string, 'http://localhost/test/index.cgi',
# Parse Apache 2.2.9 like CGI environment variables (root without PATH_INFO)
$req = Mojo::Message::Request->new;
$req->parse(
SCRIPT_NAME => '/cgi-bin/bootylicious/bootylicious.pl',
SCRIPT_NAME => '/cgi-bin/myapp/myapp.pl',
HTTP_CONNECTION => 'keep-alive',
HTTP_HOST => 'getbootylicious.org',
HTTP_HOST => 'getmyapp.org',
REQUEST_METHOD => 'GET',
QUERY_STRING => '',
REQUEST_URI => '/cgi-bin/bootylicious/bootylicious.pl',
REQUEST_URI => '/cgi-bin/myapp/myapp.pl',
SERVER_PROTOCOL => 'HTTP/1.1',
);
ok $req->is_finished, 'request is finished';
is $req->method, 'GET', 'right method';
is $req->url->base->host, 'getbootylicious.org', 'right base host';
is $req->url->base->host, 'getmyapp.org', 'right base host';
is $req->url->path, '', 'right path';
is $req->url->base->path, '/cgi-bin/bootylicious/bootylicious.pl/',
'right base path';
is $req->url->base->path, '/cgi-bin/myapp/myapp.pl/', 'right base path';
is $req->version, '1.1', 'right version';
ok $req->at_least_version('1.0'), 'at least version 1.0';
ok !$req->at_least_version('1.2'), 'not version 1.2';
is $req->url->to_abs->to_string,
'http://getbootylicious.org/cgi-bin/bootylicious/bootylicious.pl',
'http://getmyapp.org/cgi-bin/myapp/myapp.pl',
'right absolute URL';

# Parse Apache mod_fastcgi like CGI environment variables
Expand Down
94 changes: 78 additions & 16 deletions t/mojo/url.t
Expand Up @@ -3,7 +3,7 @@ use Mojo::Base -strict;

use utf8;

use Test::More tests => 376;
use Test::More tests => 397;

# "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 @@ -129,6 +129,15 @@ is $url->to_rel->to_abs,
'http://sri:foobar@kraih.com:8080/baz/foo?foo=bar#23',
'right absolute version';

# Relative (base without authority)
$url = Mojo::URL->new('http://sri:foobar@kraih.com:8080/baz/foo?foo=bar#23');
$url->base->parse('http://');
is $url->to_rel, '//sri:foobar@kraih.com:8080/baz/foo?foo=bar#23',
'right relative version';
is $url->to_rel->to_abs,
'http://sri:foobar@kraih.com:8080/baz/foo?foo=bar#23',
'right absolute version';

# Relative with path
$url = Mojo::URL->new('http://kraih.com/foo/index.html?foo=bar#23');
$url->base->parse('http://kraih.com/foo/');
Expand Down Expand Up @@ -403,7 +412,7 @@ is $url->path, '/baz', 'right path';
is $url->query, 'yada', 'right query';
is $url->fragment, undef, 'no fragment';
is "$url", 'http://foo.bar/baz?yada', 'right absolute URL';
$url = $url->clone->base($url)->parse('zzz?Zzz')->to_abs;
$url = Mojo::URL->new('zzz?Zzz')->base($url)->to_abs;
is $url->base, 'http://foo.bar/baz?yada', 'right base';
is $url->scheme, 'http', 'right scheme';
is $url->userinfo, undef, 'no userinfo';
Expand All @@ -425,8 +434,7 @@ is $url->path, '/baz/index.html', 'right path';
is $url->query, 'yada', 'right query';
is $url->fragment, undef, 'no fragment';
is "$url", 'http://foo.bar/baz/index.html?yada', 'right absolute URL';
$url->base(undef);
$url = $url->clone->base($url)->parse('zzz?Zzz')->to_abs;
$url = Mojo::URL->new('zzz?Zzz')->base($url)->to_abs;
is $url->base, 'http://foo.bar/baz/index.html?yada', 'right base';
is $url->scheme, 'http', 'right scheme';
is $url->userinfo, undef, 'no userinfo';
Expand All @@ -448,8 +456,7 @@ is $url->path, '/baz/index.html', 'right path';
is $url->query, 'yada', 'right query';
is $url->fragment, undef, 'no fragment';
is "$url", 'http://foo.bar/baz/index.html?yada', 'right absolute URL';
$url->base(undef);
$url = $url->clone->base($url)->parse('/zzz?Zzz')->to_abs;
$url = Mojo::URL->new('/zzz?Zzz')->base($url)->to_abs;
is $url->base, 'http://foo.bar/baz/index.html?yada', 'right base';
is $url->scheme, 'http', 'right scheme';
is $url->userinfo, undef, 'no userinfo';
Expand All @@ -471,8 +478,7 @@ is $url->path, '/baz/index.html', 'right path';
is $url->query, 'yada', 'right query';
is $url->fragment, undef, 'no fragment';
is "$url", 'http://foo.bar/baz/index.html?yada', 'right absolute URL';
$url->base(undef);
$url = $url->clone->base($url)->parse('/zzz')->to_abs;
$url = Mojo::URL->new('/zzz')->base($url)->to_abs;
is $url->base, 'http://foo.bar/baz/index.html?yada', 'right base';
is $url->scheme, 'http', 'right scheme';
is $url->userinfo, undef, 'no userinfo';
Expand All @@ -494,8 +500,7 @@ is $url->path, '/baz/index.html', 'right path';
is $url->query, 'yada', 'right query';
is $url->fragment, 'test1', 'right fragment';
is "$url", 'http://foo.bar/baz/index.html?yada#test1', 'right absolute URL';
$url->base(undef);
$url = $url->clone->base($url)->parse('/zzz#test2')->to_abs;
$url = Mojo::URL->new('/zzz#test2')->base($url)->to_abs;
is $url->base, 'http://foo.bar/baz/index.html?yada#test1', 'right base';
is $url->scheme, 'http', 'right scheme';
is $url->userinfo, undef, 'no userinfo';
Expand All @@ -517,8 +522,7 @@ is $url->path, '/baz/index.html', 'right path';
is $url->query, 'yada', 'right query';
is $url->fragment, 'test1', 'right fragment';
is "$url", 'http://foo.bar/baz/index.html?yada#test1', 'right absolute URL';
$url->base(undef);
$url = $url->clone->base($url)->parse('zzz#test2')->to_abs;
$url = Mojo::URL->new('zzz#test2')->base($url)->to_abs;
is $url->base, 'http://foo.bar/baz/index.html?yada#test1', 'right base';
is $url->scheme, 'http', 'right scheme';
is $url->userinfo, undef, 'no userinfo';
Expand All @@ -540,8 +544,7 @@ is $url->path, '/baz/index.html', 'right path';
is $url->query, 'yada', 'right query';
is $url->fragment, 'test1', 'right fragment';
is "$url", 'http://foo.bar/baz/index.html?yada#test1', 'right absolute URL';
$url->base(undef);
$url = $url->clone->base($url)->parse('/zzz')->to_abs;
$url = Mojo::URL->new('/zzz')->base($url)->to_abs;
is $url->base, 'http://foo.bar/baz/index.html?yada#test1', 'right base';
is $url->scheme, 'http', 'right scheme';
is $url->userinfo, undef, 'no userinfo';
Expand All @@ -563,8 +566,7 @@ is $url->path, '/baz/index.html', 'right path';
is $url->query, 'yada', 'right query';
is $url->fragment, 'test1', 'right fragment';
is "$url", 'http://foo.bar/baz/index.html?yada#test1', 'right absolute URL';
$url->base(undef);
$url = $url->clone->base($url)->parse('zzz')->to_abs;
$url = Mojo::URL->new('zzz')->base($url)->to_abs;
is $url->base, 'http://foo.bar/baz/index.html?yada#test1', 'right base';
is $url->scheme, 'http', 'right scheme';
is $url->userinfo, undef, 'no userinfo';
Expand Down Expand Up @@ -638,3 +640,63 @@ is $url->canonicalize, 'wss://mojolicio.us/foo/bar/',
$url = Mojo::URL->new('/foo/bar/..');
is "$url", '/foo/bar/..', 'right format';
is $url->canonicalize, '/foo', 'right canonicalized version';

# Resolve RFC 1808 examples
my $base = Mojo::URL->new('http://a/b/c/d?q#f');
$url = Mojo::URL->new('g');
is $url->to_abs($base)->canonicalize, 'http://a/b/c/g',
'right canonicalized version';
$url = Mojo::URL->new('./g');
is $url->to_abs($base)->canonicalize, 'http://a/b/c/g',
'right canonicalized version';
$url = Mojo::URL->new('g/');
is $url->to_abs($base)->canonicalize, 'http://a/b/c/g/',
'right canonicalized version';
$url = Mojo::URL->new('//g');
is $url->to_abs($base)->canonicalize, 'http://g',
'right canonicalized version';
$url = Mojo::URL->new('?y');
is $url->to_abs($base)->canonicalize, 'http://a/b/c/d?y',
'right canonicalized version';
$url = Mojo::URL->new('g?y');
is $url->to_abs($base)->canonicalize, 'http://a/b/c/g?y',
'right canonicalized version';
$url = Mojo::URL->new('g?y/./x');
is $url->to_abs($base)->canonicalize, 'http://a/b/c/g?y%2F.%2Fx',
'right canonicalized version';
$url = Mojo::URL->new('#s');
is $url->to_abs($base)->canonicalize, 'http://a/b/c/d?q#s',
'right canonicalized version';
$url = Mojo::URL->new('g#s');
is $url->to_abs($base)->canonicalize, 'http://a/b/c/g#s',
'right canonicalized version';
$url = Mojo::URL->new('g#s/./x');
is $url->to_abs($base)->canonicalize, 'http://a/b/c/g#s/./x',
'right canonicalized version';
$url = Mojo::URL->new('g?y#s');
is $url->to_abs($base)->canonicalize, 'http://a/b/c/g?y#s',
'right canonicalized version';
$url = Mojo::URL->new('.');
is $url->to_abs($base)->canonicalize, 'http://a/b/c',
'right canonicalized version';
$url = Mojo::URL->new('./');
is $url->to_abs($base)->canonicalize, 'http://a/b/c/',
'right canonicalized version';
$url = Mojo::URL->new('..');
is $url->to_abs($base)->canonicalize, 'http://a/b',
'right canonicalized version';
$url = Mojo::URL->new('../');
is $url->to_abs($base)->canonicalize, 'http://a/b/',
'right canonicalized version';
$url = Mojo::URL->new('../g');
is $url->to_abs($base)->canonicalize, 'http://a/b/g',
'right canonicalized version';
$url = Mojo::URL->new('../..');
is $url->to_abs($base)->canonicalize, 'http://a/',
'right canonicalized version';
$url = Mojo::URL->new('../../');
is $url->to_abs($base)->canonicalize, 'http://a/',
'right canonicalized version';
$url = Mojo::URL->new('../../g');
is $url->to_abs($base)->canonicalize, 'http://a/g',
'right canonicalized version';

0 comments on commit 640fdf2

Please sign in to comment.