Skip to content

Commit

Permalink
normalize cookie path
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Jan 12, 2013
1 parent 28e228e commit a216a1a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
9 changes: 5 additions & 4 deletions lib/Mojo/UserAgent/CookieJar.pm
Expand Up @@ -51,8 +51,8 @@ sub extract {

# Validate path
my $path = $cookie->path // $url->path->to_dir->to_abs_string;
$path =~ s!/$!!;
next unless $url->path->contains(Mojo::Path->new($path)->to_route);
$path = Mojo::Path->new($path)->trailing_slash(0)->to_abs_string;
next unless _path($path, $url->path->to_abs_string);
$self->add($cookie->path($path));
}
}
Expand All @@ -78,8 +78,7 @@ sub find {

# Taste cookie
next if $cookie->secure && $url->protocol ne 'https';
my $cpath = $cookie->path;
next unless $cpath eq '/' || $path eq $cpath || $path =~ m!^\Q$cpath/!;
next unless _path($cookie->path, $path);
my $name = $cookie->name;
my $value = $cookie->value;
push @found, Mojo::Cookie::Request->new(name => $name, value => $value);
Expand All @@ -99,6 +98,8 @@ sub inject {
$req->cookies($self->find($req->url));
}

sub _path { $_[0] eq '/' || $_[0] eq $_[1] || $_[1] =~ m!^\Q$_[0]/! }

1;

=head1 NAME
Expand Down
12 changes: 10 additions & 2 deletions t/mojo/cookiejar.t
Expand Up @@ -352,16 +352,24 @@ $tx->res->cookies(
value => 'with',
domain => 'KRAIH.com',
path => '/'
)
),
Mojo::Cookie::Response->new(
name => 'baz',
value => 'with',
domain => 'labs.KRAIH.com',
path => '/%70erldoc/Mojolicious/'
),
);
$jar->extract($tx);
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://labs.kraih.COM/perldoc');
$tx->req->url->parse('http://labs.kraih.COM/perldoc/Mojolicious/Lite');
$jar->inject($tx);
is $tx->req->cookie('foo')->name, 'foo', 'right name';
is $tx->req->cookie('foo')->value, 'with', 'right value';
is $tx->req->cookie('bar')->name, 'bar', 'right name';
is $tx->req->cookie('bar')->value, 'with', 'right value';
is $tx->req->cookie('baz')->name, 'baz', 'right name';
is $tx->req->cookie('baz')->value, 'with', 'right value';
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://labs.kraih.COM/Perldoc');
$jar->inject($tx);
Expand Down

0 comments on commit a216a1a

Please sign in to comment.