| 1 | package HTTP::CookieMonster; | ||||||||||
| 2 | 2 | our $VERSION = '0.10'; | |||||||||
| 3 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | |||||||||||
| 6 | 2 | ▶ | use 5.006; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | |||||||||||
| 8 | 2 | ▶ | use Moo 1.000003; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 2 | ▶ | use Carp qw( croak ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 2 | ▶ | use HTTP::Cookies; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use HTTP::CookieMonster::Cookie; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | ▶ | use Safe::Isa; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 2 | ▶ | use Scalar::Util qw( reftype ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 2 | ▶ | use Sub::Exporter -setup => { exports => ['cookies'] }; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 2 | ▶ | use URI::Escape qw( uri_escape uri_unescape ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | |||||||||||
| 17 | 2 | my @_cookies = (); | |||||||||
| 18 | has 'cookie_jar' => ( | ||||||||||
| 19 | required => 1, | ||||||||||
| 20 | is => 'ro', | ||||||||||
| 21 | isa => sub { | ||||||||||
| 22 | 8 | ▶ | croak 'HTTP::Cookies object expected' | ||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.6
Branch: 1 branch
| |||||||||||
| 23 | if !$_[0]->$_isa('HTTP::Cookies'); | ||||||||||
| 24 | }, | ||||||||||
| 25 | 2 | ); | |||||||||
| 26 | |||||||||||
| 27 | sub BUILDARGS { | ||||||||||
| 28 | 8 | ▶ | my ( $class, @args ) = @_; | ||||||||
|
Subroutine
BUILDARGS: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||
| 29 | |||||||||||
| 30 | 8 | ▶ | return { cookie_jar => shift @args } if @args == 1; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 1 | return {@args}; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | # all_cookies() is now a straight method rather than a Moo accessor in order to | ||||||||||
| 35 | # prevent the all_cookies list from getting out of sync with changes to the | ||||||||||
| 36 | # cookie_jar which happen outside of this module. Rather than trying to detect | ||||||||||
| 37 | # changes, we'll just create a fresh list each time. Performance penalties | ||||||||||
| 38 | # should be minimal and this keeps things simple. | ||||||||||
| 39 | |||||||||||
| 40 | sub all_cookies { | ||||||||||
| 41 | 13 | ▶ | my $self = shift; | ||||||||
|
Subroutine
all_cookies: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 42 | 13 | @_cookies = (); | |||||||||
| 43 | 13 | $self->cookie_jar->scan( \&_check_cookies ); | |||||||||
| 44 | |||||||||||
| 45 | 13 | ▶ | wantarray ? return @_cookies : return \@_cookies; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 46 | } | ||||||||||
| 47 | |||||||||||
| 48 | # my $cookie = cookies( $jar ); -- first cookie (makes no sense) | ||||||||||
| 49 | # my $session = cookies( $jar, 'session' ); | ||||||||||
| 50 | # my @cookies = cookies( $jar ); | ||||||||||
| 51 | # my @sessions = cookies( $jar, 'session' ); | ||||||||||
| 52 | |||||||||||
| 53 | sub cookies { | ||||||||||
| 54 | 5 | ▶ | my ( $cookie_jar, $name ) = @_; | ||||||||
|
Subroutine
cookies: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 55 | 5 | ▶ | croak 'This function is not part of the OO interface' | ||||||||
|
Branch: 1 branch
| |||||||||||
| 56 | if $cookie_jar->$_isa('HTTP::CookieMonster'); | ||||||||||
| 57 | |||||||||||
| 58 | 5 | my $monster = HTTP::CookieMonster->new($cookie_jar); | |||||||||
| 59 | |||||||||||
| 60 | 5 | ▶ | if ( !$name ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 61 | 2 | ▶ | if ( !wantarray ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 62 | 1 | croak | |||||||||
| 63 | 'Please specify a cookie name when asking for a single cookie'; | ||||||||||
| 64 | } | ||||||||||
| 65 | 1 | return @{ $monster->all_cookies }; | |||||||||
| 66 | } | ||||||||||
| 67 | |||||||||||
| 68 | 3 | return $monster->get_cookie($name); | |||||||||
| 69 | } | ||||||||||
| 70 | |||||||||||
| 71 | sub get_cookie { | ||||||||||
| 72 | 8 | ▶ | my $self = shift; | ||||||||
|
Subroutine
get_cookie: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 73 | 8 | my $name = shift; | |||||||||
| 74 | |||||||||||
| 75 | 8 | my @cookies = (); | |||||||||
| 76 | 8 | foreach my $cookie ( $self->all_cookies ) { | |||||||||
| 77 | 16 | ▶ | if ( $cookie->key eq $name ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 10 | ▶ | return $cookie if !wantarray; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | 6 | push @cookies, $cookie; | |||||||||
| 80 | } | ||||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 4 | ▶ | return shift @cookies if !wantarray; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 4 | return @cookies; | |||||||||
| 85 | } | ||||||||||
| 86 | |||||||||||
| 87 | sub set_cookie { | ||||||||||
| 88 | 6 | ▶ | my $self = shift; | ||||||||
|
Subroutine
set_cookie: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||
| 89 | 6 | my $cookie = shift; | |||||||||
| 90 | |||||||||||
| 91 | 6 | ▶ | if ( !$cookie ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 1 | croak 'Missing cookie, an HTTP::CookieMonster::Cookie object'; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 5 | ▶ | if ( !$cookie->$_isa('HTTP::CookieMonster::Cookie') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 0 | croak "$cookie is not a HTTP::CookieMonster::Cookie object"; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 5 | ▶ | return $self->cookie_jar->set_cookie( | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | $cookie->version, $cookie->key, | ||||||||||
| 101 | uri_escape( $cookie->val ), $cookie->path, | ||||||||||
| 102 | $cookie->domain, $cookie->port, | ||||||||||
| 103 | $cookie->path_spec, $cookie->secure, | ||||||||||
| 104 | $cookie->expires, $cookie->discard, | ||||||||||
| 105 | $cookie->hash | ||||||||||
| 106 | ) ? 1 : 0; | ||||||||||
| 107 | } | ||||||||||
| 108 | |||||||||||
| 109 | sub delete_cookie { | ||||||||||
| 110 | 1 | ▶ | my $self = shift; | ||||||||
|
Subroutine
delete_cookie: called CC=2 SCAR=7.2
Pod
documented
| |||||||||||
| 111 | 1 | my $cookie = shift; | |||||||||
| 112 | |||||||||||
| 113 | 1 | ▶ | if ( !$cookie->$_isa('HTTP::CookieMonster::Cookie') ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 114 | 0 | croak "$cookie is not a HTTP::CookieMonster::Cookie object"; | |||||||||
| 115 | } | ||||||||||
| 116 | |||||||||||
| 117 | 1 | $cookie->expires(-1); | |||||||||
| 118 | |||||||||||
| 119 | 1 | return $self->set_cookie($cookie); | |||||||||
| 120 | } | ||||||||||
| 121 | |||||||||||
| 122 | sub _check_cookies { | ||||||||||
| 123 | 32 | ▶ | my @args = @_; | ||||||||
|
Subroutine
_check_cookies: called CC=1 SCAR=0.0
| |||||||||||
| 124 | |||||||||||
| 125 | 32 | push @_cookies, | |||||||||
| 126 | HTTP::CookieMonster::Cookie->new( | ||||||||||
| 127 | version => $args[0], | ||||||||||
| 128 | key => $args[1], | ||||||||||
| 129 | val => uri_unescape( $args[2] ), | ||||||||||
| 130 | path => $args[3], | ||||||||||
| 131 | domain => $args[4], | ||||||||||
| 132 | port => $args[5], | ||||||||||
| 133 | path_spec => $args[6], | ||||||||||
| 134 | secure => $args[7], | ||||||||||
| 135 | expires => $args[8], | ||||||||||
| 136 | discard => $args[9], | ||||||||||
| 137 | hash => $args[10], | ||||||||||
| 138 | ); | ||||||||||
| 139 | |||||||||||
| 140 | 32 | return; | |||||||||
| 141 | } | ||||||||||
| 142 | |||||||||||
| 143 | 2 | 1; | |||||||||
| 144 | |||||||||||
| 145 | # ABSTRACT: Easy read/write access to your jar of HTTP::Cookies | ||||||||||
| 146 | # | ||||||||||
| 147 | |||||||||||
| 148 | __END__ | ||||||||||