Skip to content

Commit

Permalink
improved Mojo::URL performance slightly
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed May 14, 2013
1 parent 9f75c27 commit 7e9b45a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 14 deletions.
10 changes: 3 additions & 7 deletions lib/Mojo/URL.pm
Expand Up @@ -201,13 +201,9 @@ sub to_string {
my $authority = $self->authority;
$url .= "//$authority" if defined $authority;

# Relative path
my $path = $self->path;
if (!$authority) { $url .= "$path" }

# Absolute path
elsif ($path->leading_slash) { $url .= "$path" }
else { $url .= @{$path->parts} ? "/$path" : '' }
# Relative or no path
my $path = $self->path->to_string;
$url .= !$authority || $path eq '' || $path =~ m!^/! ? $path : "/$path";

# Query
if (length(my $query = $self->query)) { $url .= "?$query" }
Expand Down
17 changes: 10 additions & 7 deletions t/mojo/url.t
Expand Up @@ -327,20 +327,23 @@ is $url->to_abs, 'http://example.com/bar/foo?foo=bar#23',
'right absolute version';
is $url->to_abs->base, 'http://example.com/bar/baz/', 'right base';

# Real world tests
$url = Mojo::URL->new('http://acme.s3.amazonaws.com'
. '/mojo%2Fg%2B%2B-4%2E2_4%2E2%2E3-2ubuntu7_i386%2Edeb');
# Canonicalize (escaped path)
$url = Mojo::URL->new(
'http://example.com/mojo%2Fg%2B%2B-4%2E2_4%2E2%2E3-2ubuntu7_i386%2Edeb');
ok $url->is_abs, 'is absolute';
is $url->scheme, 'http', 'right scheme';
is $url->userinfo, undef, 'no userinfo';
is $url->host, 'acme.s3.amazonaws.com', 'right host';
is $url->host, 'example.com', 'right host';
is $url->port, undef, 'no port';
is $url->path, '/mojo%2Fg%2B%2B-4%2E2_4%2E2%2E3-2ubuntu7_i386%2Edeb',
'right path';
ok !$url->query->to_string, 'no query';
is_deeply $url->query->to_hash, {}, 'right structure';
is $url->query, '', 'no query';
is $url->fragment, undef, 'no fragment';
is "$url", 'http://acme.s3.amazonaws.com/mojo/g++-4.2_4.2.3-2ubuntu7_i386.deb',
is "$url",
'http://example.com/mojo%2Fg%2B%2B-4%2E2_4%2E2%2E3-2ubuntu7_i386%2Edeb',
'right format';
$url->path->canonicalize;
is "$url", 'http://example.com/mojo/g++-4.2_4.2.3-2ubuntu7_i386.deb',
'right format';

# Clone (advanced)
Expand Down

0 comments on commit 7e9b45a

Please sign in to comment.