Skip to content

Commit

Permalink
added to_route method to Mojo::Path
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 7, 2013
1 parent ce256d3 commit 9c6510e
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 21 deletions.
4 changes: 4 additions & 0 deletions Changes
@@ -1,4 +1,8 @@

3.75 2013-01-08
- Added to_route method to Mojo::Path.
- Improved router performance slightly.

3.74 2013-01-07
- Improved documentation.
- Improved tests.
Expand Down
17 changes: 15 additions & 2 deletions lib/Mojo/Path.pm
Expand Up @@ -83,9 +83,11 @@ sub parse {
return $self->parts([split '/', $path, -1]);
}

sub to_abs_string {
sub to_abs_string { $_[0]->leading_slash ? "$_[0]" : "/$_[0]" }

sub to_route {
my $self = shift;
return $self->leading_slash ? "$self" : "/$self";
return '/' . join('/', @{$self->parts}) . ($self->trailing_slash ? '/' : '');
}

sub to_string {
Expand All @@ -104,6 +106,8 @@ sub to_string {

1;

=encoding utf8
=head1 NAME
Mojo::Path - Path
Expand Down Expand Up @@ -230,6 +234,15 @@ Parse path. Note that C<%2F> will be treated as C</> for security reasons.
Turn path into an absolute string.
=head2 to_route
my $route = $path->to_route;
Turn path into a route.
# "/i/♥/mojolicious"
Mojo::Path->new('/i/%E2%99%A5/mojolicious')->to_route;
=head2 to_string
my $string = $path->to_string;
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious.pm
Expand Up @@ -40,7 +40,7 @@ has static => sub { Mojolicious::Static->new };
has types => sub { Mojolicious::Types->new };

our $CODENAME = 'Rainbow';
our $VERSION = '3.74';
our $VERSION = '3.75';

sub AUTOLOAD {
my $self = shift;
Expand Down
3 changes: 1 addition & 2 deletions lib/Mojolicious/Controller.pm
Expand Up @@ -411,8 +411,7 @@ sub url_for {
my $path = $url->path;
if ($target =~ m!^/!) {
if (my $prefix = $self->stash->{path}) {
my $real = Mojo::Util::url_unescape($req->url->path->to_abs_string);
$real = Mojo::Util::decode('UTF-8', $real) // $real;
my $real = $req->url->path->to_route;
$real =~ s!/?$prefix$!$target!;
$target = $real;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/Mojolicious/Routes.pm
Expand Up @@ -40,7 +40,7 @@ sub dispatch {
my $req = $c->req;
my $path = $c->stash->{path};
if (defined $path) { $path = "/$path" if $path !~ m!^/! }
else { $path = $req->url->path->to_abs_string }
else { $path = $req->url->path->to_route }

# Prepare match
my $method = $req->method;
Expand Down
9 changes: 2 additions & 7 deletions lib/Mojolicious/Routes/Match.pm
@@ -1,20 +1,15 @@
package Mojolicious::Routes::Match;
use Mojo::Base -base;

use Mojo::Util qw(decode url_unescape);

has captures => sub { {} };
has [qw(endpoint root)];
has stack => sub { [] };

sub new {
my $self = shift->SUPER::new;

$self->{method} = uc shift;
my $path = url_unescape shift;
$self->{path} = decode('UTF-8', $path) // $path;
$self->{method} = uc shift;
$self->{path} = shift;
$self->{websocket} = shift;

return $self;
}

Expand Down
2 changes: 2 additions & 0 deletions t/mojo/path.t
Expand Up @@ -35,6 +35,7 @@ is $path->parts->[2], 'bar', 'right part';
is $path->parts->[3], undef, 'no part';
ok $path->leading_slash, 'has leading slash';
ok !$path->trailing_slash, 'no trailing slash';
is $path->to_route, '/foo/♥/bar', 'right route';
is $path->parse('/foo/%E2%99%A5/~b@a:r+')->to_string,
'/foo/%E2%99%A5/~b@a:r+', 'right path';
is $path->parts->[0], 'foo', 'right part';
Expand All @@ -43,6 +44,7 @@ is $path->parts->[2], '~b@a:r+', 'right part';
is $path->parts->[3], undef, 'no part';
ok $path->leading_slash, 'has leading slash';
ok !$path->trailing_slash, 'no trailing slash';
is $path->to_route, '/foo/♥/~b@a:r+', 'right route';

# Zero in path
is $path->parse('/path/0')->to_string, '/path/0', 'right path';
Expand Down
15 changes: 7 additions & 8 deletions t/mojolicious/routes.t
Expand Up @@ -468,17 +468,16 @@ is $m->stack->[0]{wildcard}, 'hello/there', 'right value';
is $m->path_for, '/wildcards/4/hello/there/foo', 'right path';
is @{$m->stack}, 1, 'right number of elements';

# Escaped
$m = Mojolicious::Routes::Match->new(
GET => '/wildcards/1/http://www.google.com');
# Special characters
$m = Mojolicious::Routes::Match->new(GET => '/wildcards/1/♥');
$m->match($r, $c);
is $m->stack->[0]{controller}, 'wild', 'right value';
is $m->stack->[0]{action}, 'card', 'right value';
is $m->stack->[0]{wildcard}, 'http://www.google.com', 'right value';
is $m->path_for, '/wildcards/1/http://www.google.com', 'right path';
is $m->stack->[0]{controller}, 'wild', 'right value';
is $m->stack->[0]{action}, 'card', 'right value';
is $m->stack->[0]{wildcard}, '', 'right value';
is $m->path_for, '/wildcards/1/', 'right path';
is @{$m->stack}, 1, 'right number of elements';
$m = Mojolicious::Routes::Match->new(
GET => '/wildcards/1/http%3A%2F%2Fwww.google.com');
GET => '/wildcards/1/http://www.google.com');
$m->match($r, $c);
is $m->stack->[0]{controller}, 'wild', 'right value';
is $m->stack->[0]{action}, 'card', 'right value';
Expand Down

0 comments on commit 9c6510e

Please sign in to comment.