Skip to content

Commit

Permalink
use a callback instead of a list
Browse files Browse the repository at this point in the history
  • Loading branch information
kraih committed Sep 9, 2015
1 parent 060bb72 commit fd5137b
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 17 deletions.
2 changes: 1 addition & 1 deletion Changes
@@ -1,7 +1,7 @@

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

6.18 2015-09-02
- Improved portability of slurp function in Mojo::Util.
Expand Down
8 changes: 6 additions & 2 deletions lib/Mojo/UserAgent.pm
Expand Up @@ -513,8 +513,12 @@ L<Mojo::UserAgent::CookieJar> object.
# Disable collecting cookies from responses
$ua->cookie_jar->collecting(0);
# Ignore cookies for certain public suffixes
$ua->cookie_jar->public_suffixes(['com', 'net', 'org']);
# Ignore cookies for public suffixes
my $ps = IO::Socket::SSL::PublicSuffix->default;
$ua->cookie_jar(sub {
my $cookie = shift;
return $ps->public_suffix($cookie->domain) eq $cookie->domain;
});
# Add custom cookie to the jar
$ua->cookie_jar->add(
Expand Down
28 changes: 15 additions & 13 deletions lib/Mojo/UserAgent/CookieJar.pm
Expand Up @@ -4,9 +4,9 @@ use Mojo::Base -base;
use Mojo::Cookie::Request;
use Mojo::Path;

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

sub add {
my ($self, @cookies) = @_;
Expand Down Expand Up @@ -42,7 +42,6 @@ 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 @@ -51,7 +50,7 @@ sub collect {
my $host = $url->ihost;
my $domain = lc($cookie->domain // $cookie->origin($host)->origin);
$domain =~ s/^\.//;
next if $ignore->{$domain};
if (my $cb = $self->ignore) { next if $cb->($cookie) }
next if $host ne $domain && ($host !~ /\Q.$domain\E$/ || $host =~ /\.\d+$/);

# Validate path
Expand Down Expand Up @@ -158,22 +157,25 @@ L<Mojo::UserAgent::CookieJar> implements the following attributes.
Allow L</"collect"> to L</"add"> new cookies to the jar, defaults to a true
value.
=head2 ignore
my $ignore = $jar->ignore;
$jar = $jar->ignore(sub {...});
A callback used to decide if a cookie should be ignored.
$jar->ignore(sub {
my $cookie = shift;
return $cookie->domain =~ /^\.?com$/;
});
=head2 max_cookie_size
my $size = $jar->max_cookie_size;
$jar = $jar->max_cookie_size(4096);
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. 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
2 changes: 1 addition & 1 deletion t/mojo/cookiejar.t
Expand Up @@ -392,7 +392,7 @@ $tx->res->cookies(
value => 'baz'
)
);
$jar->public_suffixes(['com'])->collect($tx);
$jar->ignore(sub { shift->domain eq 'com' })->collect($tx);
$tx = Mojo::Transaction::HTTP->new;
$tx->req->url->parse('http://bücher.com/foo');
$jar->prepare($tx);
Expand Down

0 comments on commit fd5137b

Please sign in to comment.