| 1 | 7 | ▶ | use 5.008001; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 2 | 7 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 3 | 7 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||
| 5 | package HTTP::CookieJar; | ||||||||||||||||||||||||||||||||||||
| 6 | # ABSTRACT: A minimalist HTTP user agent cookie jar | ||||||||||||||||||||||||||||||||||||
| 7 | 7 | our $VERSION = '0.012'; | |||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | 7 | ▶ | use Carp (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 10 | 7 | ▶ | use HTTP::Date (); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||
| 12 | 7 | my $HAS_MPS = eval { require Mozilla::PublicSuffix; 1 }; | |||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||
| 14 | #pod =construct new | ||||||||||||||||||||||||||||||||||||
| 15 | #pod | ||||||||||||||||||||||||||||||||||||
| 16 | #pod my $jar = HTTP::CookieJar->new; | ||||||||||||||||||||||||||||||||||||
| 17 | #pod | ||||||||||||||||||||||||||||||||||||
| 18 | #pod Return a new, empty cookie jar | ||||||||||||||||||||||||||||||||||||
| 19 | #pod | ||||||||||||||||||||||||||||||||||||
| 20 | #pod =cut | ||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||
| 22 | sub new { | ||||||||||||||||||||||||||||||||||||
| 23 | 46 | ▶ | my ($class) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 24 | 46 | bless { store => {} }, $class; | |||||||||||||||||||||||||||||||||||
| 25 | } | ||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||
| 27 | #pod =method add | ||||||||||||||||||||||||||||||||||||
| 28 | #pod | ||||||||||||||||||||||||||||||||||||
| 29 | #pod $jar->add( | ||||||||||||||||||||||||||||||||||||
| 30 | #pod "http://www.example.com/", "lang=en-US; Path=/; Domain=example.com" | ||||||||||||||||||||||||||||||||||||
| 31 | #pod ); | ||||||||||||||||||||||||||||||||||||
| 32 | #pod | ||||||||||||||||||||||||||||||||||||
| 33 | #pod Given a request URL and a C<Set-Cookie> header string, attempts to adds the | ||||||||||||||||||||||||||||||||||||
| 34 | #pod cookie to the jar. If the cookie is expired, instead it deletes any matching | ||||||||||||||||||||||||||||||||||||
| 35 | #pod cookie from the jar. A C<Max-Age> attribute will be converted to an absolute | ||||||||||||||||||||||||||||||||||||
| 36 | #pod C<Expires> attribute. | ||||||||||||||||||||||||||||||||||||
| 37 | #pod | ||||||||||||||||||||||||||||||||||||
| 38 | #pod It will throw an exception if the request URL is missing or invalid. Returns true if | ||||||||||||||||||||||||||||||||||||
| 39 | #pod successful cookie processing or undef/empty-list on failure. | ||||||||||||||||||||||||||||||||||||
| 40 | #pod | ||||||||||||||||||||||||||||||||||||
| 41 | #pod =cut | ||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||
| 43 | sub add { | ||||||||||||||||||||||||||||||||||||
| 44 | 78 | ▶ | my ( $self, $request, $cookie ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
add: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 45 | 78 | ▶ | return unless defined $cookie and length $cookie; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cookie and length $cookie
MC/DC50%1/2
defined $cookie and length $cookie
defined $cookie
length $cookie
Truth table66%2/3
A and B
A= defined $cookieB= length $cookie
| |||||||||||||||||||||||||||||||||||||
| 46 | 77 | my ( $scheme, $host, $port, $request_path ) = eval { _split_url($request) }; | |||||||||||||||||||||||||||||||||||
| 47 | 77 | ▶ | Carp::croak($@) if $@; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||||||||||
| 49 | 77 | ▶ | return unless my $parse = _parse_cookie($cookie); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 50 | 76 | my $name = $parse->{name}; | |||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||
| 52 | # check and normalize domain | ||||||||||||||||||||||||||||||||||||
| 53 | 76 | ▶ | if ( exists $parse->{domain} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 54 | 31 | ▶ | _normalize_domain( $host, $parse ) or return; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||||||||||||||||||
| 57 | 45 | $parse->{domain} = $host; | |||||||||||||||||||||||||||||||||||
| 58 | 45 | $parse->{hostonly} = 1; | |||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||
| 60 | 71 | my $domain = $parse->{domain}; | |||||||||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||||||||||
| 62 | # normalize path | ||||||||||||||||||||||||||||||||||||
| 63 | 71 | ▶ | if ( !exists $parse->{path} || substr( $parse->{path}, 0, 1 ) ne "/" ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not exists $parse->{"path"} or substr($parse->{"path"}, 0, 1) ne "/"
MC/DC50%1/2
not exists $parse->{"path"} or substr($parse->{"path"}, 0, 1) ne "/"
not exists $parse->{"path"}
substr($parse->{"path"}, 0, 1) ne "/"
Truth table66%2/3
not(A) or B
A= not exists $parse->{"path"}B= substr($parse->{"path"}, 0, 1) ne "/"
| |||||||||||||||||||||||||||||||||||||
| 64 | 34 | $parse->{path} = _default_path($request_path); | |||||||||||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||||||||||
| 66 | 71 | my $path = $parse->{path}; | |||||||||||||||||||||||||||||||||||
| 67 | # set timestamps and normalize expires | ||||||||||||||||||||||||||||||||||||
| 68 | 71 | my $now = $parse->{creation_time} = $parse->{last_access_time} = time; | |||||||||||||||||||||||||||||||||||
| 69 | 71 | ▶ | if ( exists $parse->{'max-age'} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 70 | # "If delta-seconds is less than or equal to zero (0), let expiry-time | ||||||||||||||||||||||||||||||||||||
| 71 | # be the earliest representable date and time." | ||||||||||||||||||||||||||||||||||||
| 72 | $parse->{expires} = $parse->{'max-age'} <= 0 | ||||||||||||||||||||||||||||||||||||
| 73 | ? 0 | ||||||||||||||||||||||||||||||||||||
| 74 | 8 | ▶ | : $now + $parse->{'max-age'}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 75 | 8 | delete $parse->{'max-age'}; | |||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||
| 77 | # update creation time from old cookie, if exists | ||||||||||||||||||||||||||||||||||||
| 78 | 71 | ▶ | if ( my $old = $self->{store}{$domain}{$path}{$name} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 79 | 5 | $parse->{creation_time} = $old->{creation_time}; | |||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||
| 81 | # if cookie has expired, purge any old matching cookie, too | ||||||||||||||||||||||||||||||||||||
| 82 | 71 | ▶ | if ( defined $parse->{expires} && $parse->{expires} < $now ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $parse->{"expires"} and $parse->{"expires"} < $now
MC/DC100%2/2
defined $parse->{"expires"} and $parse->{"expires"} < $now
defined $parse->{"expires"}
$parse->{"expires"} < $now
Truth table100%3/3
A and B
A= defined $parse->{"expires"}B= $parse->{"expires"} < $now
| |||||||||||||||||||||||||||||||||||||
| 83 | 9 | delete $self->{store}{$domain}{$path}{$name}; | |||||||||||||||||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||||||||||||||||
| 85 | else { | ||||||||||||||||||||||||||||||||||||
| 86 | 62 | $self->{store}{$domain}{$path}{$name} = $parse; | |||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||
| 88 | 71 | return 1; | |||||||||||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||
| 91 | #pod =method clear | ||||||||||||||||||||||||||||||||||||
| 92 | #pod | ||||||||||||||||||||||||||||||||||||
| 93 | #pod $jar->clear | ||||||||||||||||||||||||||||||||||||
| 94 | #pod | ||||||||||||||||||||||||||||||||||||
| 95 | #pod Empties the cookie jar. | ||||||||||||||||||||||||||||||||||||
| 96 | #pod | ||||||||||||||||||||||||||||||||||||
| 97 | #pod =cut | ||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||
| 99 | sub clear { | ||||||||||||||||||||||||||||||||||||
| 100 | 5 | ▶ | my ($self) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
clear: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 101 | 5 | $self->{store} = {}; | |||||||||||||||||||||||||||||||||||
| 102 | 5 | return 1; | |||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||
| 105 | #pod =method cookies_for | ||||||||||||||||||||||||||||||||||||
| 106 | #pod | ||||||||||||||||||||||||||||||||||||
| 107 | #pod my @cookies = $jar->cookies_for("http://www.example.com/foo/bar"); | ||||||||||||||||||||||||||||||||||||
| 108 | #pod | ||||||||||||||||||||||||||||||||||||
| 109 | #pod Given a request URL, returns a list of hash references representing cookies | ||||||||||||||||||||||||||||||||||||
| 110 | #pod that should be sent. The hash references are copies -- changing values | ||||||||||||||||||||||||||||||||||||
| 111 | #pod will not change the cookies in the jar. | ||||||||||||||||||||||||||||||||||||
| 112 | #pod | ||||||||||||||||||||||||||||||||||||
| 113 | #pod Cookies set C<secure> will only be returned if the request scheme is C<https>. | ||||||||||||||||||||||||||||||||||||
| 114 | #pod Expired cookies will not be returned. | ||||||||||||||||||||||||||||||||||||
| 115 | #pod | ||||||||||||||||||||||||||||||||||||
| 116 | #pod Keys of a cookie hash reference might include: | ||||||||||||||||||||||||||||||||||||
| 117 | #pod | ||||||||||||||||||||||||||||||||||||
| 118 | #pod =for :list | ||||||||||||||||||||||||||||||||||||
| 119 | #pod * name -- the name of the cookie | ||||||||||||||||||||||||||||||||||||
| 120 | #pod * value -- the value of the cookie | ||||||||||||||||||||||||||||||||||||
| 121 | #pod * domain -- the domain name to which the cookie applies | ||||||||||||||||||||||||||||||||||||
| 122 | #pod * path -- the path to which the cookie applies | ||||||||||||||||||||||||||||||||||||
| 123 | #pod * expires -- if present, when the cookie expires in epoch seconds | ||||||||||||||||||||||||||||||||||||
| 124 | #pod * secure -- if present, the cookie was set C<Secure> | ||||||||||||||||||||||||||||||||||||
| 125 | #pod * httponly -- if present, the cookie was set C<HttpOnly> | ||||||||||||||||||||||||||||||||||||
| 126 | #pod * hostonly -- if present, the cookie may only be used with the domain as a host | ||||||||||||||||||||||||||||||||||||
| 127 | #pod * creation_time -- epoch seconds since the cookie was first stored | ||||||||||||||||||||||||||||||||||||
| 128 | #pod * last_access_time -- epoch seconds since the cookie was last stored | ||||||||||||||||||||||||||||||||||||
| 129 | #pod | ||||||||||||||||||||||||||||||||||||
| 130 | #pod Keep in mind that C<httponly> means it should only be used in requests and not | ||||||||||||||||||||||||||||||||||||
| 131 | #pod made available via Javascript, etc. This is pretty meaningless for Perl user | ||||||||||||||||||||||||||||||||||||
| 132 | #pod agents. | ||||||||||||||||||||||||||||||||||||
| 133 | #pod | ||||||||||||||||||||||||||||||||||||
| 134 | #pod Generally, user agents should use the C<cookie_header> method instead. | ||||||||||||||||||||||||||||||||||||
| 135 | #pod | ||||||||||||||||||||||||||||||||||||
| 136 | #pod It will throw an exception if the request URL is missing or invalid. | ||||||||||||||||||||||||||||||||||||
| 137 | #pod | ||||||||||||||||||||||||||||||||||||
| 138 | #pod =cut | ||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||
| 140 | sub cookies_for { | ||||||||||||||||||||||||||||||||||||
| 141 | 50 | ▶ | my ( $self, $request ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
cookies_for: called CC=12 SCAR=24.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 142 | 50 | my ( $scheme, $host, $port, $request_path ) = eval { _split_url($request) }; | |||||||||||||||||||||||||||||||||||
| 143 | 50 | ▶ | Carp::croak($@) if $@; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||
| 145 | 50 | my @found; | |||||||||||||||||||||||||||||||||||
| 146 | 50 | my $now = time; | |||||||||||||||||||||||||||||||||||
| 147 | 50 | for my $cookie ( $self->_all_cookies ) { | |||||||||||||||||||||||||||||||||||
| 148 | 70 | ▶ | next if $cookie->{hostonly} && $host ne $cookie->{domain}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cookie->{"hostonly"} and $host ne $cookie->{"domain"}
MC/DC100%2/2
$cookie->{"hostonly"} and $host ne $cookie->{"domain"}
$cookie->{"hostonly"}
$host ne $cookie->{"domain"}
Truth table100%3/3
A and B
A= $cookie->{"hostonly"}B= $host ne $cookie->{"domain"}
| |||||||||||||||||||||||||||||||||||||
| 149 | 69 | ▶ | next if $cookie->{secure} && $scheme ne 'https'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$cookie->{"secure"} and $scheme ne "https"
MC/DC100%2/2
$cookie->{"secure"} and $scheme ne "https"
$cookie->{"secure"}
$scheme ne "https"
Truth table100%3/3
A and B
A= $cookie->{"secure"}B= $scheme ne "https"
| |||||||||||||||||||||||||||||||||||||
| 150 | 67 | ▶ | next if defined( $cookie->{expires} ) && $cookie->{expires} < $now; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $cookie->{"expires"} and $cookie->{"expires"} < $now
MC/DC0%0/2
defined $cookie->{"expires"} and $cookie->{"expires"} < $now
defined $cookie->{"expires"}
$cookie->{"expires"} < $now
Truth table66%2/3
A and B
A= defined $cookie->{"expires"}B= $cookie->{"expires"} < $now
| |||||||||||||||||||||||||||||||||||||
| 151 | 67 | ▶ | next unless _domain_match( $host, $cookie->{domain} ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 152 | 57 | ▶ | next unless _path_match( $request_path, $cookie->{path} ); | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 153 | 52 | push @found, $cookie; | |||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||
| 155 | @found = sort { | ||||||||||||||||||||||||||||||||||||
| 156 | 50 | length( $b->{path} ) <=> length( $a->{path} ) | |||||||||||||||||||||||||||||||||||
| 157 | || $a->{creation_time} <=> $b->{creation_time} | ||||||||||||||||||||||||||||||||||||
| 158 | 22 | ▶ | } @found; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 159 | 50 | return @found; | |||||||||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||
| 162 | #pod =method cookie_header | ||||||||||||||||||||||||||||||||||||
| 163 | #pod | ||||||||||||||||||||||||||||||||||||
| 164 | #pod my $header = $jar->cookie_header("http://www.example.com/foo/bar"); | ||||||||||||||||||||||||||||||||||||
| 165 | #pod | ||||||||||||||||||||||||||||||||||||
| 166 | #pod Given a request URL, returns a correctly-formatted string with all relevant | ||||||||||||||||||||||||||||||||||||
| 167 | #pod cookies for the request. This string is ready to be used in a C<Cookie> header | ||||||||||||||||||||||||||||||||||||
| 168 | #pod in an HTTP request. E.g.: | ||||||||||||||||||||||||||||||||||||
| 169 | #pod | ||||||||||||||||||||||||||||||||||||
| 170 | #pod SID=31d4d96e407aad42; lang=en-US | ||||||||||||||||||||||||||||||||||||
| 171 | #pod | ||||||||||||||||||||||||||||||||||||
| 172 | #pod It follows the same exclusion rules as C<cookies_for>. | ||||||||||||||||||||||||||||||||||||
| 173 | #pod | ||||||||||||||||||||||||||||||||||||
| 174 | #pod If the request is invalid or no cookies apply, it will return an empty string. | ||||||||||||||||||||||||||||||||||||
| 175 | #pod | ||||||||||||||||||||||||||||||||||||
| 176 | #pod =cut | ||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||
| 178 | sub cookie_header { | ||||||||||||||||||||||||||||||||||||
| 179 | 48 | ▶ | my ( $self, $req ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
cookie_header: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 180 | 48 | return join( "; ", map { "$_->{name}=$_->{value}" } $self->cookies_for($req) ); | |||||||||||||||||||||||||||||||||||
| 181 | } | ||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||
| 183 | #pod =method dump_cookies | ||||||||||||||||||||||||||||||||||||
| 184 | #pod | ||||||||||||||||||||||||||||||||||||
| 185 | #pod my @list = $jar->dump_cookies; | ||||||||||||||||||||||||||||||||||||
| 186 | #pod my @list = $jar->dump_cookies( { persistent => 1 } ); | ||||||||||||||||||||||||||||||||||||
| 187 | #pod | ||||||||||||||||||||||||||||||||||||
| 188 | #pod Returns a list of raw cookies in string form. The strings resemble what | ||||||||||||||||||||||||||||||||||||
| 189 | #pod would be received from C<Set-Cookie> headers, but with additional internal | ||||||||||||||||||||||||||||||||||||
| 190 | #pod fields. The list is only intended for use with C<load_cookies> to allow | ||||||||||||||||||||||||||||||||||||
| 191 | #pod cookie jar persistence. | ||||||||||||||||||||||||||||||||||||
| 192 | #pod | ||||||||||||||||||||||||||||||||||||
| 193 | #pod If a hash reference with a true C<persistent> key is given as an argument, | ||||||||||||||||||||||||||||||||||||
| 194 | #pod cookies without an C<Expires> time (i.e. "session cookies") will be omitted. | ||||||||||||||||||||||||||||||||||||
| 195 | #pod | ||||||||||||||||||||||||||||||||||||
| 196 | #pod Here is a trivial example of saving a cookie jar file with L<Path::Tiny>: | ||||||||||||||||||||||||||||||||||||
| 197 | #pod | ||||||||||||||||||||||||||||||||||||
| 198 | #pod path("jar.txt")->spew( join "\n", $jar->dump_cookies ); | ||||||||||||||||||||||||||||||||||||
| 199 | #pod | ||||||||||||||||||||||||||||||||||||
| 200 | #pod =cut | ||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||
| 202 | sub dump_cookies { | ||||||||||||||||||||||||||||||||||||
| 203 | 7 | ▶ | my ( $self, $args ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
dump_cookies: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 204 | 7 | my @list; | |||||||||||||||||||||||||||||||||||
| 205 | 7 | for my $c ( $self->_all_cookies ) { | |||||||||||||||||||||||||||||||||||
| 206 | 8 | my @parts = "$c->{name}=$c->{value}"; | |||||||||||||||||||||||||||||||||||
| 207 | 8 | ▶ | if ( defined $c->{expires} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 208 | 4 | push @parts, 'Expires=' . HTTP::Date::time2str( $c->{expires} ); | |||||||||||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||||||||||
| 210 | else { | ||||||||||||||||||||||||||||||||||||
| 211 | 4 | ▶ | next if $args->{persistent}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 212 | } | ||||||||||||||||||||||||||||||||||||
| 213 | 7 | for my $attr (qw/Domain Path Creation_Time Last_Access_Time/) { | |||||||||||||||||||||||||||||||||||
| 214 | 28 | ▶ | push @parts, "$attr=$c->{lc $attr}" if defined $c->{ lc $attr }; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 215 | } | ||||||||||||||||||||||||||||||||||||
| 216 | 7 | for my $attr (qw/Secure HttpOnly HostOnly/) { | |||||||||||||||||||||||||||||||||||
| 217 | 21 | ▶ | push @parts, $attr if $c->{ lc $attr }; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 218 | } | ||||||||||||||||||||||||||||||||||||
| 219 | 7 | push @list, join( "; ", @parts ); | |||||||||||||||||||||||||||||||||||
| 220 | } | ||||||||||||||||||||||||||||||||||||
| 221 | 7 | return @list; | |||||||||||||||||||||||||||||||||||
| 222 | } | ||||||||||||||||||||||||||||||||||||
| 223 | |||||||||||||||||||||||||||||||||||||
| 224 | #pod =method load_cookies | ||||||||||||||||||||||||||||||||||||
| 225 | #pod | ||||||||||||||||||||||||||||||||||||
| 226 | #pod $jar->load_cookies( @cookies ); | ||||||||||||||||||||||||||||||||||||
| 227 | #pod | ||||||||||||||||||||||||||||||||||||
| 228 | #pod Given a list of cookie strings from C<dump_cookies>, it adds them to | ||||||||||||||||||||||||||||||||||||
| 229 | #pod the cookie jar. Cookies added in this way will supersede any existing | ||||||||||||||||||||||||||||||||||||
| 230 | #pod cookies with similar domain, path and name. | ||||||||||||||||||||||||||||||||||||
| 231 | #pod | ||||||||||||||||||||||||||||||||||||
| 232 | #pod It returns the jar object for convenience when loading a new object: | ||||||||||||||||||||||||||||||||||||
| 233 | #pod | ||||||||||||||||||||||||||||||||||||
| 234 | #pod my $jar = HTTP::CookieJar->new->load_cookies( @cookies ); | ||||||||||||||||||||||||||||||||||||
| 235 | #pod | ||||||||||||||||||||||||||||||||||||
| 236 | #pod Here is a trivial example of loading a cookie jar file with L<Path::Tiny>: | ||||||||||||||||||||||||||||||||||||
| 237 | #pod | ||||||||||||||||||||||||||||||||||||
| 238 | #pod my $jar = HTTP::CookieJar->new->load_cookies( | ||||||||||||||||||||||||||||||||||||
| 239 | #pod path("jar.txt")->lines | ||||||||||||||||||||||||||||||||||||
| 240 | #pod ); | ||||||||||||||||||||||||||||||||||||
| 241 | #pod | ||||||||||||||||||||||||||||||||||||
| 242 | #pod =cut | ||||||||||||||||||||||||||||||||||||
| 243 | |||||||||||||||||||||||||||||||||||||
| 244 | sub load_cookies { | ||||||||||||||||||||||||||||||||||||
| 245 | 4 | ▶ | my ( $self, @cookies ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
load_cookies: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||
| 246 | 4 | for my $cookie (@cookies) { | |||||||||||||||||||||||||||||||||||
| 247 | 5 | my $p = _parse_cookie( $cookie, 1 ); | |||||||||||||||||||||||||||||||||||
| 248 | 5 | ▶ | next unless exists $p->{domain} && exists $p->{path}; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $p->{"domain"} and exists $p->{"path"}
MC/DC50%1/2
exists $p->{"domain"} and exists $p->{"path"}
exists $p->{"domain"}
exists $p->{"path"}
Truth table66%2/3
A and B
A= exists $p->{"domain"}B= exists $p->{"path"}
| |||||||||||||||||||||||||||||||||||||
| 249 | 4 | $p->{$_} = time for grep { !defined $p->{$_} } qw/creation_time last_access_time/; | |||||||||||||||||||||||||||||||||||
| 250 | 4 | $self->{store}{ $p->{domain} }{ $p->{path} }{ $p->{name} } = $p; | |||||||||||||||||||||||||||||||||||
| 251 | } | ||||||||||||||||||||||||||||||||||||
| 252 | 4 | return $self; | |||||||||||||||||||||||||||||||||||
| 253 | } | ||||||||||||||||||||||||||||||||||||
| 254 | |||||||||||||||||||||||||||||||||||||
| 255 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||
| 256 | # private methods | ||||||||||||||||||||||||||||||||||||
| 257 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||
| 259 | # return a copy of all cookies | ||||||||||||||||||||||||||||||||||||
| 260 | sub _all_cookies { | ||||||||||||||||||||||||||||||||||||
| 261 | return map { | ||||||||||||||||||||||||||||||||||||
| 262 | 103 | { %$_ } | |||||||||||||||||||||||||||||||||||
| 263 | 67 | ▶ | } map { values %$_ } map { values %$_ } values %{ $_[0]->{store} }; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_all_cookies: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 264 | } | ||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||
| 266 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||
| 267 | # Helper subroutines | ||||||||||||||||||||||||||||||||||||
| 268 | #--------------------------------------------------------------------------# | ||||||||||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||||||||||
| 270 | 7 | my $pub_re = qr/(?:domain|path|expires|max-age|httponly|secure)/; | |||||||||||||||||||||||||||||||||||
| 271 | 7 | my $pvt_re = qr/(?:$pub_re|creation_time|last_access_time|hostonly)/; | |||||||||||||||||||||||||||||||||||
| 272 | |||||||||||||||||||||||||||||||||||||
| 273 | sub _parse_cookie { | ||||||||||||||||||||||||||||||||||||
| 274 | 95 | ▶ | my ( $cookie, $private ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_cookie: called CC=17 SCAR=28.4
| |||||||||||||||||||||||||||||||||||||
| 275 | 95 | ▶ | $cookie = '' unless defined $cookie; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 276 | 95 | my ( $kvp, @attrs ) = split /;/, $cookie; | |||||||||||||||||||||||||||||||||||
| 277 | 95 | ▶ | $kvp = '' unless defined $kvp; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 278 | my ( $name, $value ) = | ||||||||||||||||||||||||||||||||||||
| 279 | 95 | map { s/^\s*//; s/\s*$//; $_ } split( /=/, $kvp, 2 ); ## no critic | |||||||||||||||||||||||||||||||||||
| 280 | |||||||||||||||||||||||||||||||||||||
| 281 | 95 | ▶ | return unless defined $name and length $name; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $name and length $name
MC/DC100%2/2
defined $name and length $name
defined $name
length $name
Truth table100%3/3
A and B
A= defined $nameB= length $name
| |||||||||||||||||||||||||||||||||||||
| 282 | 91 | ▶ | $value = '' unless defined $value; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 283 | 91 | my $parse = { name => $name, value => $value }; | |||||||||||||||||||||||||||||||||||
| 284 | 91 | for my $s (@attrs) { | |||||||||||||||||||||||||||||||||||
| 285 | 148 | ▶ | next unless defined $s && $s =~ /\S/; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $s and $s =~ /\S/
MC/DC50%1/2
defined $s and $s =~ /\S/
defined $s
$s =~ /\S/
Truth table66%2/3
A and B
A= defined $sB= $s =~ /\S/
| |||||||||||||||||||||||||||||||||||||
| 286 | 144 | my ( $k, $v ) = map { s/^\s*//; s/\s*$//; $_ } split( /=/, $s, 2 ); ## no critic | |||||||||||||||||||||||||||||||||||
| 287 | 144 | $k = lc $k; | |||||||||||||||||||||||||||||||||||
| 288 | 144 | ▶ | next unless $private ? ( $k =~ m/^$pvt_re$/ ) : ( $k =~ m/^$pub_re$/ ); | ||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||
| 289 | 140 | ▶ | $v = 1 if $k =~ m/^(?:httponly|secure|hostonly)$/; # boolean flag if present | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 290 | 140 | ▶ | $v = HTTP::Date::str2time($v) || 0 if $k eq 'expires'; # convert to epoch | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
&HTTP::Date::str2time($v) || 0
MC/DC100%1/1
&HTTP::Date::str2time($v) || 0
&HTTP::Date::str2time($v)
Truth table100%2/2
A
A= &HTTP::Date::str2time($v)
| |||||||||||||||||||||||||||||||||||||
| 291 | 140 | ▶ | next unless length $v; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 292 | 138 | ▶ | $v =~ s{^\.}{} if $k eq 'domain'; # strip leading dot | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 293 | 138 | ▶ | $v =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg if $k eq 'path'; # unescape | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 294 | 138 | $parse->{$k} = $v; | |||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||
| 296 | 91 | return $parse; | |||||||||||||||||||||||||||||||||||
| 297 | } | ||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||
| 299 | sub _domain_match { | ||||||||||||||||||||||||||||||||||||
| 300 | 122 | ▶ | my ( $string, $dom_string ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_domain_match: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||||||||||
| 301 | 122 | ▶ | return 1 if $dom_string eq $string; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 302 | 69 | ▶ | return unless $string =~ /[a-z]/i; # non-numeric | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 303 | 62 | ▶ | if ( $string =~ s{\Q$dom_string\E$}{} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 304 | 29 | return substr( $string, -1, 1 ) eq '.'; # "foo." | |||||||||||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||||||||||
| 306 | 33 | return; | |||||||||||||||||||||||||||||||||||
| 307 | } | ||||||||||||||||||||||||||||||||||||
| 308 | |||||||||||||||||||||||||||||||||||||
| 309 | sub _normalize_domain { | ||||||||||||||||||||||||||||||||||||
| 310 | 31 | ▶ | my ( $host, $parse ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_normalize_domain: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||
| 311 | |||||||||||||||||||||||||||||||||||||
| 312 | 31 | ▶ | if ($HAS_MPS) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 313 | 31 | my $host_pub_suff = eval { Mozilla::PublicSuffix::public_suffix($host) }; | |||||||||||||||||||||||||||||||||||
| 314 | 31 | ▶ | $host_pub_suff = '' unless defined $host_pub_suff; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 315 | 31 | ▶ | if ( _domain_match( $host_pub_suff, $parse->{domain} ) ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 316 | 4 | ▶ | if ( $parse->{domain} eq $host ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 317 | 2 | return $parse->{hostonly} = 1; | |||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||
| 319 | else { | ||||||||||||||||||||||||||||||||||||
| 320 | 2 | return; | |||||||||||||||||||||||||||||||||||
| 321 | } | ||||||||||||||||||||||||||||||||||||
| 322 | } | ||||||||||||||||||||||||||||||||||||
| 323 | } | ||||||||||||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||||||||||
| 325 | 27 | ▶ | if ( $parse->{domain} !~ m{\.} && $parse->{domain} eq $host ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $parse->{"domain"} =~ /\./ and $parse->{"domain"} eq $host
MC/DC50%1/2
not $parse->{"domain"} =~ /\./ and $parse->{"domain"} eq $host
not $parse->{"domain"} =~ /\./
$parse->{"domain"} eq $host
Truth table66%2/3
not(A) and B
A= not $parse->{"domain"} =~ /\./B= $parse->{"domain"} eq $host
| |||||||||||||||||||||||||||||||||||||
| 326 | 3 | return $parse->{hostonly} = 1; | |||||||||||||||||||||||||||||||||||
| 327 | } | ||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||
| 329 | 24 | return _domain_match( $host, $parse->{domain} ); | |||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||
| 332 | sub _default_path { | ||||||||||||||||||||||||||||||||||||
| 333 | 34 | ▶ | my ($path) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_default_path: called CC=4 SCAR=14.6
| |||||||||||||||||||||||||||||||||||||
| 334 | 34 | ▶ | return "/" if !length $path || substr( $path, 0, 1 ) ne "/"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
not length $path or substr($path, 0, 1) ne "/"
MC/DC0%0/2
not length $path or substr($path, 0, 1) ne "/"
not length $path
substr($path, 0, 1) ne "/"
Truth table33%1/3
not(A) or B
A= not length $pathB= substr($path, 0, 1) ne "/"
| |||||||||||||||||||||||||||||||||||||
| 335 | 34 | my ($default) = $path =~ m{^(.*)/}; # greedy to last / | |||||||||||||||||||||||||||||||||||
| 336 | 34 | ▶ | return length($default) ? $default : "/"; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 337 | } | ||||||||||||||||||||||||||||||||||||
| 338 | |||||||||||||||||||||||||||||||||||||
| 339 | sub _path_match { | ||||||||||||||||||||||||||||||||||||
| 340 | 57 | ▶ | my ( $req_path, $cookie_path ) = @_; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_path_match: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||
| 341 | 57 | ▶ | return 1 if $req_path eq $cookie_path; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 342 | 39 | ▶ | if ( $req_path =~ m{^\Q$cookie_path\E(.*)} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 343 | 34 | my $rest = $1; | |||||||||||||||||||||||||||||||||||
| 344 | 34 | ▶ | return 1 if substr( $cookie_path, -1, 1 ) eq '/'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 345 | 19 | ▶ | return 1 if substr( $rest, 0, 1 ) eq '/'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||||||||||
| 347 | 5 | return; | |||||||||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||||||||||
| 350 | sub _split_url { | ||||||||||||||||||||||||||||||||||||
| 351 | 127 | ▶ | my $url = shift; | ||||||||||||||||||||||||||||||||||
|
Subroutine
_split_url: called CC=10 SCAR=24.0
| |||||||||||||||||||||||||||||||||||||
| 352 | 127 | ▶ | die(qq/No URL provided\n/) unless defined $url and length $url; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $url and length $url
MC/DC0%0/2
defined $url and length $url
defined $url
length $url
Truth table33%1/3
A and B
A= defined $urlB= length $url
| |||||||||||||||||||||||||||||||||||||
| 353 | |||||||||||||||||||||||||||||||||||||
| 354 | # URI regex adapted from the URI module | ||||||||||||||||||||||||||||||||||||
| 355 | # XXX path_query here really chops at ? or # to get just the path and not the query | ||||||||||||||||||||||||||||||||||||
| 356 | 127 | ▶ | my ( $scheme, $authority, $path_query ) = $url =~ m<\A([^:/?#]+)://([^/?#]*)([^#?]*)> | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 357 | or die(qq/Cannot parse URL: '$url'\n/); | ||||||||||||||||||||||||||||||||||||
| 358 | |||||||||||||||||||||||||||||||||||||
| 359 | 127 | $scheme = lc $scheme; | |||||||||||||||||||||||||||||||||||
| 360 | 127 | ▶ | $path_query = "/$path_query" unless $path_query =~ m<\A/>; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 361 | 127 | $path_query =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg; | |||||||||||||||||||||||||||||||||||
| 362 | |||||||||||||||||||||||||||||||||||||
| 363 | 127 | ▶ | my $host = ( length($authority) ) ? lc $authority : 'localhost'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 364 | 127 | $host =~ s/\A[^@]*@//; # userinfo | |||||||||||||||||||||||||||||||||||
| 365 | 127 | my $port = do { | |||||||||||||||||||||||||||||||||||
| 366 | 127 | ▶ | $host =~ s/:([0-9]*)\z// && length $1 | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
$host =~ s/:([0-9]*)\z// and length $1
MC/DC50%1/2
$host =~ s/:([0-9]*)\z// and length $1
$host =~ s/:([0-9]*)\z//
length $1
Truth table66%2/3
A and B
A= $host =~ s/:([0-9]*)\z//B= length $1
| |||||||||||||||||||||||||||||||||||||
| 367 | ? $1 | ||||||||||||||||||||||||||||||||||||
| 368 | : ( $scheme eq 'http' ? 80 : $scheme eq 'https' ? 443 : undef ); | ||||||||||||||||||||||||||||||||||||
| 369 | }; | ||||||||||||||||||||||||||||||||||||
| 370 | |||||||||||||||||||||||||||||||||||||
| 371 | 127 | return ( $scheme, $host, $port, $path_query ); | |||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||
| 374 | 7 | 1; | |||||||||||||||||||||||||||||||||||
| 375 | |||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||
| 377 | # vim: ts=4 sts=4 sw=4 et: | ||||||||||||||||||||||||||||||||||||
| 378 | |||||||||||||||||||||||||||||||||||||
| 379 | __END__ | ||||||||||||||||||||||||||||||||||||