Skip to content

Commit

Permalink
add public_suffixes attribute to Mojo::UserAgent::CookieJar
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 9, 2015
1 parent 3336f6e commit 8aab5b3
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 36 deletions.
1 change: 1 addition & 0 deletions Changes
@@ -1,6 +1,7 @@

6.19 2015-09-09
- Added code of conduct to Mojolicious::Guides::Contributing.
- Added public_suffixes attribute to Mojo::UserAgent::CookieJar.

6.18 2015-09-02
- Improved portability of slurp function in Mojo::Util.
Expand Down
14 changes: 13 additions & 1 deletion lib/Mojo/UserAgent/CookieJar.pm
Expand Up @@ -6,6 +6,7 @@ use Mojo::Path;

has collecting => 1;
has max_cookie_size => 4096;
has public_suffixes => sub { ['com'] };

sub add {
my ($self, @cookies) = @_;
Expand Down Expand Up @@ -41,6 +42,7 @@ sub collect {
my ($self, $tx) = @_;

return unless $self->collecting;
my $ignore = $self->{ignore} ||= {map { $_ => 1 } @{$self->public_suffixes}};

my $url = $tx->req->url;
for my $cookie (@{$tx->res->cookies}) {
Expand All @@ -49,6 +51,7 @@ sub collect {
my $host = $url->ihost;
my $domain = lc($cookie->domain // $cookie->origin($host)->origin);
$domain =~ s/^\.//;
next if $ignore->{$domain};
next if $host ne $domain && ($host !~ /\Q.$domain\E$/ || $host =~ /\.\d+$/);

# Validate path
Expand All @@ -67,7 +70,7 @@ sub find {
my @found;
return \@found unless my $domain = my $host = $url->ihost;
my $path = $url->path->to_abs_string;
while ($domain =~ /[^.]+\.[^.]+|localhost$/) {
while ($domain) {
next unless my $old = $self->{jar}{$domain};

# Grab cookies
Expand Down Expand Up @@ -162,6 +165,15 @@ value.
Maximum cookie size in bytes, defaults to C<4096> (4KB).
=head2 public_suffixes
my $suffixes = $jar->public_suffixes;
$jar = $jar->public_suffixes(['com', 'net', 'org']);
Public suffixes for which cookies should always be ignored, defaults to C<com>.
A comprehensive list of public suffixes currently being used across the internet
can be found at L<https://publicsuffix.org>.
=head1 METHODS
L<Mojo::UserAgent::CookieJar> inherits all methods from L<Mojo::Base> and
Expand Down
61 changes: 26 additions & 35 deletions t/mojo/cookiejar.t
Expand Up @@ -100,41 +100,6 @@ is $cookies->[1]->name, 'foo', 'right name';
is $cookies->[1]->value, 'bar', 'right value';
is $cookies->[2], undef, 'no third cookie';

# Random top-level domain and IDNA
$jar = Mojo::UserAgent::CookieJar->new;
$jar->add(
Mojo::Cookie::Response->new(
domain => 'com',
path => '/foo',
name => 'foo',
value => 'bar'
),
Mojo::Cookie::Response->new(
domain => 'xn--bcher-kva.com',
path => '/foo',
name => 'bar',
value => 'baz'
)
);
$cookies = $jar->find(Mojo::URL->new('http://bücher.com/foo'));
is $cookies->[0]->name, 'bar', 'right name';
is $cookies->[0]->value, 'baz', 'right value';
is $cookies->[1], undef, 'no second cookie';
$cookies = $jar->find(Mojo::URL->new('http://bücher.com/foo'));
is $cookies->[0]->name, 'bar', 'right name';
is $cookies->[0]->value, 'baz', 'right value';
is $cookies->[1], undef, 'no second cookie';
$cookies = $jar->all;
is $cookies->[0]->domain, 'com', 'right domain';
is $cookies->[0]->path, '/foo', 'right path';
is $cookies->[0]->name, 'foo', 'right name';
is $cookies->[0]->value, 'bar', 'right value';
is $cookies->[1]->domain, 'xn--bcher-kva.com', 'right domain';
is $cookies->[1]->path, '/foo', 'right path';
is $cookies->[1]->name, 'bar', 'right name';
is $cookies->[1]->value, 'baz', 'right value';
is $cookies->[2], undef, 'no third cookie';

# Huge cookie
$jar = Mojo::UserAgent::CookieJar->new->max_cookie_size(1024);
$jar->add(
Expand Down Expand Up @@ -382,6 +347,32 @@ is $tx->req->cookie('foo')->name, 'foo', 'right name';
is $tx->req->cookie('foo')->value, 'local', 'right value';
is $tx->req->cookie('bar'), undef, 'no cookie';

# Gather and prepare cookies for public suffix (with IDNA)
$jar = Mojo::UserAgent::CookieJar->new;
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://bücher.com/foo');
$tx->res->cookies(
Mojo::Cookie::Response->new(
domain => 'com',
path => '/foo',
name => 'foo',
value => 'bar'
),
Mojo::Cookie::Response->new(
domain => 'xn--bcher-kva.com',
path => '/foo',
name => 'bar',
value => 'baz'
)
);
$jar->collect($tx);
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://bücher.com/foo');
$jar->prepare($tx);
is $tx->req->cookie('foo'), undef, 'no cookie';
is $tx->req->cookie('bar')->name, 'bar', 'right name';
is $tx->req->cookie('bar')->value, 'baz', 'right value';

# Gather and prepare cookies with domain and path
$jar = Mojo::UserAgent::CookieJar->new;
$tx = Mojo::Transaction::HTTP->new;
Expand Down

0 comments on commit 8aab5b3

Please sign in to comment.