CookieJar.pm

filter: Statementstmt 100% 168 / 168 Branchbran 87.9% 95 / 108 Conditioncond 75.0% 33 / 44 MC/DCmcdc 58.6% 17 / 29 Subroutinesub 100% 20 / 20 Podpod 100% 7 / 7 totaltotal 90.4% 340 / 376 CC 77 SCAR 22.9
CC
77
Cov
92%
_parse_cookie
17.0
add
14.0
_cookies_for
12.1
CRAP 9.9
↑ Summary CookieJar/LWP.pm »
1 8 ▶use 5.008001;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 8 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 8 ▶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 8 our $VERSION = '0.014';
8  
9 8 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 8 ▶use HTTP::Date ();
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 8 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 47 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
23 47 my ($class) = @_;
24 47 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 81 ▶sub add {
Subroutine add: called CC=14 SCAR=26.4
Pod documented
44 81 my ( $self, $request, $cookie ) = @_;
45 81 ▶ return unless defined $cookie and length $cookie;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cookie and length $cookie 1 80 81
Condition66%2/3
defined $cookie and length $cookie
!ll&&!rl&&r
0180
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
ABresult
0X0
100
111
46 80 my ( $scheme, $host, $port, $request_path ) = eval { _split_url($request) };
47 80 ▶ Carp::croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 80 80
48  
49 80 ▶ return unless my $parse = _parse_cookie($cookie);
Branch: 1 branch
BranchTrueFalseTotal
unless my $parse = _parse_cookie($cookie) 1 79 80
50 79 my $name = $parse->{name};
51  
52 # check and normalize domain
53 79 ▶ if ( exists $parse->{domain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parse->{'domain'}) { } 31 48 79
54 31 ▶ _normalize_domain( $host, $parse ) or return;
Branch: 1 branch
BranchTrueFalseTotal
_normalize_domain($host, $parse) or return 26 5 31
55 }
56 else {
57 48 $parse->{domain} = $host;
58 48 $parse->{hostonly} = 1;
59 }
60 74 my $domain = $parse->{domain};
61  
62 # normalize path
63 74 ▶ if ( !exists $parse->{path} || substr( $parse->{path}, 0, 1 ) ne "/" ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not exists $parse->{"path"} or substr($parse->{"path"}, 0, 1) ne "/") 34 40 74
Condition66%2/3
not exists $parse->{"path"} or substr($parse->{"path"}, 0, 1) ne "/"
l!l&&r!l&&!r
34040
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 "/"
ABresult
1X1
011
000
64 34 $parse->{path} = _default_path($request_path);
65 }
66 74 my $path = $parse->{path};
67 # set timestamps and normalize expires
68 74 my $now = $parse->{creation_time} = $parse->{last_access_time} = time;
69 74 ▶ if ( exists $parse->{'max-age'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parse->{"max-age"}) 8 66 74
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
BranchTrueFalseTotal
$parse->{'max-age'} <= 0 ? : 3 5 8
75 8 delete $parse->{'max-age'};
76 }
77 # update creation time from old cookie, if exists
78 74 ▶ if ( my $old = $self->{store}{$domain}{$path}{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $old = $self->{"store"}{$domain}{$path}{$name}) 5 69 74
79 5 $parse->{creation_time} = $old->{creation_time};
80 }
81 # if cookie has expired, purge any old matching cookie, too
82 74 ▶ if ( defined $parse->{expires} && $parse->{expires} < $now ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $parse->{'expires'} and $parse->{'expires'} < $now) { } 9 65 74
Condition100%3/3
defined $parse->{"expires"} and $parse->{"expires"} < $now
!ll&&!rl&&r
5699
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
ABresult
0X0
100
111
83 9 delete $self->{store}{$domain}{$path}{$name};
84 }
85 else {
86 65 $self->{store}{$domain}{$path}{$name} = $parse;
87 }
88 74 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 5 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
100 5 my ($self) = @_;
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 time when the cookie was first stored
128 #pod * last_access_time -- epoch time when the cookie was last accessed (i.e. "now")
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 52 ▶sub cookies_for {
Subroutine cookies_for: called CC=1 SCAR=0.0
Pod documented
141 52 my ( $self, $request ) = @_;
142 52 my @found = $self->_cookies_for($request);
143 52 return map { {%$_} } @found;
144 }
145  
146 # _cookies_for returns originals, not copies, which helps in testing
147 53 ▶sub _cookies_for {
Subroutine _cookies_for: called CC=12 SCAR=24.9
148 53 my ( $self, $request ) = @_;
149 53 my ( $scheme, $host, $port, $request_path ) = eval { _split_url($request) };
150 53 ▶ Carp::croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 53 53
151  
152 53 my @found;
153 53 my $now = time;
154 53 for my $cookie ( $self->_all_cookies ) {
155 79 ▶ next if $cookie->{hostonly} && $host ne $cookie->{domain};
Branch: 1 branch
BranchTrueFalseTotal
if $cookie->{"hostonly"} and $host ne $cookie->{"domain"} 1 78 79
Condition100%3/3
$cookie->{"hostonly"} and $host ne $cookie->{"domain"}
!ll&&!rl&&r
26521
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"}
ABresult
0X0
100
111
156 78 ▶ next if $cookie->{secure} && $scheme ne 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $cookie->{"secure"} and $scheme ne "https" 2 76 78
Condition100%3/3
$cookie->{"secure"} and $scheme ne "https"
!ll&&!rl&&r
7422
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"
ABresult
0X0
100
111
157 76 ▶ next if defined( $cookie->{expires} ) && $cookie->{expires} < $now;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cookie->{"expires"} and $cookie->{"expires"} < $now 0 76 76
Condition66%2/3
defined $cookie->{"expires"} and $cookie->{"expires"} < $now
!ll&&!rl&&r
6790
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
ABresult
0X0
100
111
158 76 ▶ next unless _domain_match( $host, $cookie->{domain} );
Branch: 1 branch
BranchTrueFalseTotal
unless _domain_match($host, $cookie->{"domain"}) 10 66 76
159 66 ▶ next unless _path_match( $request_path, $cookie->{path} );
Branch: 1 branch
BranchTrueFalseTotal
unless _path_match($request_path, $cookie->{"path"}) 5 61 66
160 61 $cookie->{last_access_time} = time;
161 61 push @found, $cookie;
162 }
163 @found = sort {
164 53 length( $b->{path} ) <=> length( $a->{path} )
165 || $a->{creation_time} <=> $b->{creation_time}
166 30 ▶ } @found;
Condition100%3/3
length $HTTP::CookieJar::b->{'path'} <=> length $HTTP::CookieJar::a->{'path'} || $HTTP::CookieJar::a->{'creation_time'} <=> $HTTP::CookieJar::b->{'creation_time'}
l!l&&r!l&&!r
1929
MC/DC100%2/2
length $HTTP::CookieJar::b->{'path'} <=> length $HTTP::CookieJar::a->{'path'} || $HTTP::CookieJar::a->{'creation_time'} <=> $HTTP::CookieJar::b->{'creation_time'}
length $HTTP::CookieJar::b->{'path'} <=> length $HTTP::CookieJar::a->{'path'} $HTTP::CookieJar::a->{'creation_time'} <=> $HTTP::CookieJar::b->{'creation_time'}
Truth table100%3/3
A || B
A= length $HTTP::CookieJar::b->{'path'} <=> length $HTTP::CookieJar::a->{'path'}B= $HTTP::CookieJar::a->{'creation_time'} <=> $HTTP::CookieJar::b->{'creation_time'}
ABresult
1X1
011
000
167 53 return @found;
168 }
169  
170 #pod =method cookie_header
171 #pod
172 #pod my $header = $jar->cookie_header("http://www.example.com/foo/bar");
173 #pod
174 #pod Given a request URL, returns a correctly-formatted string with all relevant
175 #pod cookies for the request. This string is ready to be used in a C<Cookie> header
176 #pod in an HTTP request. E.g.:
177 #pod
178 #pod SID=31d4d96e407aad42; lang=en-US
179 #pod
180 #pod It follows the same exclusion rules as C<cookies_for>.
181 #pod
182 #pod If the request is invalid or no cookies apply, it will return an empty string.
183 #pod
184 #pod =cut
185  
186 48 ▶sub cookie_header {
Subroutine cookie_header: called CC=1 SCAR=0.0
Pod documented
187 48 my ( $self, $req ) = @_;
188 48 return join( "; ", map { "$_->{name}=$_->{value}" } $self->cookies_for($req) );
189 }
190  
191 #pod =method dump_cookies
192 #pod
193 #pod my @list = $jar->dump_cookies;
194 #pod my @list = $jar->dump_cookies( { persistent => 1 } );
195 #pod
196 #pod Returns a list of raw cookies in string form. The strings resemble what
197 #pod would be received from C<Set-Cookie> headers, but with additional internal
198 #pod fields. The list is only intended for use with C<load_cookies> to allow
199 #pod cookie jar persistence.
200 #pod
201 #pod If a hash reference with a true C<persistent> key is given as an argument,
202 #pod cookies without an C<Expires> time (i.e. "session cookies") will be omitted.
203 #pod
204 #pod Here is a trivial example of saving a cookie jar file with L<Path::Tiny>:
205 #pod
206 #pod path("jar.txt")->spew( join "\n", $jar->dump_cookies );
207 #pod
208 #pod =cut
209  
210 7 ▶sub dump_cookies {
Subroutine dump_cookies: called CC=8 SCAR=20.8
Pod documented
211 7 my ( $self, $args ) = @_;
212 7 my @list;
213 7 for my $c ( $self->_all_cookies ) {
214 8 my @parts = "$c->{name}=$c->{value}";
215 8 ▶ if ( defined $c->{expires} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $c->{'expires'}) { } 4 4 8
216 4 push @parts, 'Expires=' . HTTP::Date::time2str( $c->{expires} );
217 }
218 else {
219 4 ▶ next if $args->{persistent};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"persistent"} 1 3 4
220 }
221 7 for my $attr (qw/Domain Path Creation_Time Last_Access_Time/) {
222 28 ▶ push @parts, "$attr=$c->{lc $attr}" if defined $c->{ lc $attr };
Branch: 1 branch
BranchTrueFalseTotal
if defined $$c{lc $attr} 28 0 28
223 }
224 7 for my $attr (qw/Secure HttpOnly HostOnly/) {
225 21 ▶ push @parts, $attr if $c->{ lc $attr };
Branch: 1 branch
BranchTrueFalseTotal
if $$c{lc $attr} 16 5 21
226 }
227 7 push @list, join( "; ", @parts );
228 }
229 7 return @list;
230 }
231  
232 #pod =method load_cookies
233 #pod
234 #pod $jar->load_cookies( @cookies );
235 #pod
236 #pod Given a list of cookie strings from C<dump_cookies>, it adds them to
237 #pod the cookie jar. Cookies added in this way will supersede any existing
238 #pod cookies with similar domain, path and name.
239 #pod
240 #pod It returns the jar object for convenience when loading a new object:
241 #pod
242 #pod my $jar = HTTP::CookieJar->new->load_cookies( @cookies );
243 #pod
244 #pod Here is a trivial example of loading a cookie jar file with L<Path::Tiny>:
245 #pod
246 #pod my $jar = HTTP::CookieJar->new->load_cookies(
247 #pod path("jar.txt")->lines
248 #pod );
249 #pod
250 #pod =cut
251  
252 4 ▶sub load_cookies {
Subroutine load_cookies: called CC=5 SCAR=16.1
Pod documented
253 4 my ( $self, @cookies ) = @_;
254 4 for my $cookie (@cookies) {
255 5 my $p = _parse_cookie( $cookie, 1 );
256 5 ▶ next unless exists $p->{domain} && exists $p->{path};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $p->{"domain"} and exists $p->{"path"} 1 4 5
Condition66%2/3
exists $p->{"domain"} and exists $p->{"path"}
!ll&&!rl&&r
104
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"}
ABresult
0X0
100
111
257 4 $p->{$_} = time for grep { !defined $p->{$_} } qw/creation_time last_access_time/;
258 4 $self->{store}{ $p->{domain} }{ $p->{path} }{ $p->{name} } = $p;
259 }
260 4 return $self;
261 }
262  
263 #--------------------------------------------------------------------------#
264 # private methods
265 #--------------------------------------------------------------------------#
266  
267 # return flattened list of all cookies
268 70 ▶sub _all_cookies {
Subroutine _all_cookies: called CC=1 SCAR=0.0
269 70 return map { values %$_ } map { values %$_ } values %{ $_[0]->{store} };
270 }
271  
272 #--------------------------------------------------------------------------#
273 # Helper subroutines
274 #--------------------------------------------------------------------------#
275  
276 8 my $pub_re = qr/(?:domain|path|expires|max-age|httponly|secure)/;
277 8 my $pvt_re = qr/(?:$pub_re|creation_time|last_access_time|hostonly)/;
278  
279 98 ▶sub _parse_cookie {
Subroutine _parse_cookie: called CC=17 SCAR=28.4
280 98 my ( $cookie, $private ) = @_;
281 98 ▶ $cookie = '' unless defined $cookie;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cookie 0 98 98
282 98 my ( $kvp, @attrs ) = split /;/, $cookie;
283 98 ▶ $kvp = '' unless defined $kvp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kvp 2 96 98
284 my ( $name, $value ) =
285 98 map { s/^\s*//; s/\s*$//; $_ } split( /=/, $kvp, 2 ); ## no critic
286  
287 98 ▶ return unless defined $name and length $name;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $name and length $name 4 94 98
Condition100%3/3
defined $name and length $name
!ll&&!rl&&r
3194
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
ABresult
0X0
100
111
288 94 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 94 94
289 94 my $parse = { name => $name, value => $value };
290 94 for my $s (@attrs) {
291 151 ▶ next unless defined $s && $s =~ /\S/;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and $s =~ /\S/ 4 147 151
Condition66%2/3
defined $s and $s =~ /\S/
!ll&&!rl&&r
04147
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/
ABresult
0X0
100
111
292 147 my ( $k, $v ) = map { s/^\s*//; s/\s*$//; $_ } split( /=/, $s, 2 ); ## no critic
293 147 $k = lc $k;
294 147 ▶ next unless $private ? ( $k =~ m/^$pvt_re$/ ) : ( $k =~ m/^$pub_re$/ );
Branch: 2 branches
BranchTrueFalseTotal
unless $private ? $k =~ /^$pvt_re$/ : $k =~ /^$pub_re$/ 4 143 147
$private ? : 29 118 147
295 143 ▶ $v = 1 if $k =~ m/^(?:httponly|secure|hostonly)$/; # boolean flag if present
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^(?:httponly|secure|hostonly)$/ 23 120 143
296 143 ▶ $v = HTTP::Date::str2time($v) || 0 if $k eq 'expires'; # convert to epoch
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "expires" 17 126 143
Condition100%2/2
&HTTP::Date::str2time($v) || 0
l!l
134
MC/DC100%1/1
&HTTP::Date::str2time($v) || 0
&HTTP::Date::str2time($v)
Truth table100%2/2
A
A= &HTTP::Date::str2time($v)
Aresult
11
00
297 143 ▶ next unless length $v;
Branch: 1 branch
BranchTrueFalseTotal
unless length $v 2 141 143
298 141 ▶ $v =~ s{^\.}{} if $k eq 'domain'; # strip leading dot
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "domain" 37 104 141
299 141 ▶ $v =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg if $k eq 'path'; # unescape
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "path" 48 93 141
300 141 $parse->{$k} = $v;
301 }
302 94 return $parse;
303 }
304  
305 131 ▶sub _domain_match {
Subroutine _domain_match: called CC=4 SCAR=13.9
306 131 my ( $string, $dom_string ) = @_;
307 131 ▶ return 1 if $dom_string eq $string;
Branch: 1 branch
BranchTrueFalseTotal
if $dom_string eq $string 62 69 131
308 69 ▶ return unless $string =~ /[a-z]/i; # non-numeric
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /[a-z]/i 7 62 69
309 62 ▶ if ( $string =~ s{\Q$dom_string\E$}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/\Q$dom_string\E$//) 29 33 62
310 29 return substr( $string, -1, 1 ) eq '.'; # "foo."
311 }
312 33 return;
313 }
314  
315 31 ▶sub _normalize_domain {
Subroutine _normalize_domain: called CC=7 SCAR=19.5
316 31 my ( $host, $parse ) = @_;
317  
318 31 ▶ if ($HAS_MPS) {
Branch: 1 branch
BranchTrueFalseTotal
if ($HAS_MPS) 31 0 31
319 31 my $host_pub_suff = eval { Mozilla::PublicSuffix::public_suffix($host) };
320 31 ▶ $host_pub_suff = '' unless defined $host_pub_suff;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host_pub_suff 4 27 31
321 31 ▶ if ( _domain_match( $host_pub_suff, $parse->{domain} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_domain_match($host_pub_suff, $parse->{"domain"})) 4 27 31
322 4 ▶ if ( $parse->{domain} eq $host ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse->{'domain'} eq $host) { } 2 2 4
323 2 return $parse->{hostonly} = 1;
324 }
325 else {
326 2 return;
327 }
328 }
329 }
330  
331 27 ▶ if ( $parse->{domain} !~ m{\.} && $parse->{domain} eq $host ) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $parse->{"domain"} =~ /\./ and $parse->{"domain"} eq $host) 3 24 27
Condition66%2/3
not $parse->{"domain"} =~ /\./ and $parse->{"domain"} eq $host
!ll&&!rl&&r
2403
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
ABresult
0X0
100
111
332 3 return $parse->{hostonly} = 1;
333 }
334  
335 24 return _domain_match( $host, $parse->{domain} );
336 }
337  
338 34 ▶sub _default_path {
Subroutine _default_path: called CC=4 SCAR=14.6
339 34 my ($path) = @_;
340 34 ▶ return "/" if !length $path || substr( $path, 0, 1 ) ne "/";
Branch: 1 branch
BranchTrueFalseTotal
if not length $path or substr($path, 0, 1) ne "/" 0 34 34
Condition33%1/3
not length $path or substr($path, 0, 1) ne "/"
l!l&&r!l&&!r
0034
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 "/"
ABresult
1X1
011
000
341 34 my ($default) = $path =~ m{^(.*)/}; # greedy to last /
342 34 ▶ return length($default) ? $default : "/";
Branch: 1 branch
BranchTrueFalseTotal
length $default ? : 9 25 34
343 }
344  
345 66 ▶sub _path_match {
Subroutine _path_match: called CC=5 SCAR=16.1
346 66 my ( $req_path, $cookie_path ) = @_;
347 66 ▶ return 1 if $req_path eq $cookie_path;
Branch: 1 branch
BranchTrueFalseTotal
if $req_path eq $cookie_path 18 48 66
348 48 ▶ if ( $req_path =~ m{^\Q$cookie_path\E(.*)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_path =~ /^\Q$cookie_path\E(.*)/) 43 5 48
349 43 my $rest = $1;
350 43 ▶ return 1 if substr( $cookie_path, -1, 1 ) eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if substr($cookie_path, -1, 1) eq "/" 18 25 43
351 25 ▶ return 1 if substr( $rest, 0, 1 ) eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if substr($rest, 0, 1) eq "/" 25 0 25
352 }
353 5 return;
354 }
355  
356 133 ▶sub _split_url {
Subroutine _split_url: called CC=10 SCAR=24.0
357 133 my $url = shift;
358 133 ▶ die(qq/No URL provided\n/) unless defined $url and length $url;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $url and length $url 0 133 133
Condition33%1/3
defined $url and length $url
!ll&&!rl&&r
00133
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
ABresult
0X0
100
111
359  
360 # URI regex adapted from the URI module
361 # XXX path_query here really chops at ? or # to get just the path and not the query
362 133 ▶ my ( $scheme, $authority, $path_query ) = $url =~ m<\A([^:/?#]+)://([^/?#]*)([^#?]*)>
Branch: 1 branch
BranchTrueFalseTotal
() = $url =~ m[\A([^:/?#]+)://([^/?#]*)([^#?]*)] or die "Cannot parse URL: '${url}'\n" 133 0 133
363 or die(qq/Cannot parse URL: '$url'\n/);
364  
365 133 $scheme = lc $scheme;
366 133 ▶ $path_query = "/$path_query" unless $path_query =~ m<\A/>;
Branch: 1 branch
BranchTrueFalseTotal
unless $path_query =~ m[\A/] 13 120 133
367 133 $path_query =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
368  
369 133 ▶ my $host = ( length($authority) ) ? lc $authority : 'localhost';
Branch: 1 branch
BranchTrueFalseTotal
length $authority ? : 133 0 133
370 133 $host =~ s/\A[^@]*@//; # userinfo
371 133 my $port = do {
372 133 ▶ $host =~ s/:([0-9]*)\z// && length $1
Branch: 3 branches
BranchTrueFalseTotal
$host =~ s/:([0-9]*)\z// && length $1 ? : 1 132 133
$scheme eq 'http' ? : 129 3 132
$scheme eq 'https' ? : 3 0 3
Condition66%2/3
$host =~ s/:([0-9]*)\z// and length $1
!ll&&!rl&&r
13201
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
ABresult
0X0
100
111
373 ? $1
374 : ( $scheme eq 'http' ? 80 : $scheme eq 'https' ? 443 : undef );
375 };
376  
377 133 return ( $scheme, $host, $port, $path_query );
378 }
379  
380 8 1;
381  
382  
383 # vim: ts=4 sts=4 sw=4 et:
384  
385 __END__
↑ Summary CookieJar/LWP.pm »