CookieJar.pm

filter: Statementstmt 100% 164 / 164 Branchbran 88.1% 97 / 110 Conditioncond 73.1% 30 / 41 MC/DCmcdc 55.5% 15 / 27 Subroutinesub 100% 19 / 19 Podpod 100% 7 / 7 totaltotal 90.2% 332 / 368 CC 77 SCAR 23.0
CC
77
Cov
92%
_parse_cookie
17.0
add
14.0
cookies_for
12.1
CRAP 10.0
↑ Summary CookieJar/LWP.pm »
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
BranchTrueFalseTotal
unless defined $cookie and length $cookie 1 77 78
Condition66%2/3
defined $cookie and length $cookie
!ll&&!rl&&r
0177
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 77 my ( $scheme, $host, $port, $request_path ) = eval { _split_url($request) };
47 77 ▶ Carp::croak($@) if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 77 77
48  
49 77 ▶ return unless my $parse = _parse_cookie($cookie);
Branch: 1 branch
BranchTrueFalseTotal
unless my $parse = _parse_cookie($cookie) 1 76 77
50 76 my $name = $parse->{name};
51  
52 # check and normalize domain
53 76 ▶ if ( exists $parse->{domain} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $parse->{'domain'}) { } 31 45 76
54 31 ▶ _normalize_domain( $host, $parse ) or return;
Branch: 1 branch
BranchTrueFalseTotal
_normalize_domain($host, $parse) or return 5 26 31
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
BranchTrueFalseTotal
if (not exists $parse->{"path"} or substr($parse->{"path"}, 0, 1) ne "/") 34 37 71
Condition66%2/3
not exists $parse->{"path"} or substr($parse->{"path"}, 0, 1) ne "/"
l!l&&r!l&&!r
34037
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 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
BranchTrueFalseTotal
if (exists $parse->{"max-age"}) 8 63 71
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 71 ▶ if ( my $old = $self->{store}{$domain}{$path}{$name} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $old = $self->{"store"}{$domain}{$path}{$name}) 5 66 71
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
BranchTrueFalseTotal
if (defined $parse->{'expires'} and $parse->{'expires'} < $now) { } 9 62 71
Condition100%3/3
defined $parse->{"expires"} and $parse->{"expires"} < $now
!ll&&!rl&&r
5399
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 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
BranchTrueFalseTotal
if $@ 0 50 50
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
BranchTrueFalseTotal
if $cookie->{"hostonly"} and $host ne $cookie->{"domain"} 1 69 70
Condition100%3/3
$cookie->{"hostonly"} and $host ne $cookie->{"domain"}
!ll&&!rl&&r
26431
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
149 69 ▶ next if $cookie->{secure} && $scheme ne 'https';
Branch: 1 branch
BranchTrueFalseTotal
if $cookie->{"secure"} and $scheme ne "https" 2 67 69
Condition100%3/3
$cookie->{"secure"} and $scheme ne "https"
!ll&&!rl&&r
6522
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
150 67 ▶ next if defined( $cookie->{expires} ) && $cookie->{expires} < $now;
Branch: 1 branch
BranchTrueFalseTotal
if defined $cookie->{"expires"} and $cookie->{"expires"} < $now 0 67 67
Condition66%2/3
defined $cookie->{"expires"} and $cookie->{"expires"} < $now
!ll&&!rl&&r
5890
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
151 67 ▶ next unless _domain_match( $host, $cookie->{domain} );
Branch: 1 branch
BranchTrueFalseTotal
unless _domain_match($host, $cookie->{"domain"}) 10 57 67
152 57 ▶ next unless _path_match( $request_path, $cookie->{path} );
Branch: 1 branch
BranchTrueFalseTotal
unless _path_match($request_path, $cookie->{"path"}) 5 52 57
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
BranchTrueFalseTotal
length $HTTP::CookieJar::b->{"path"} <=> length $HTTP::CookieJar::a->{"path"} or $HTTP::CookieJar::a->{"creation_time"} <=> $HTTP::CookieJar::b->{"creation_time"} 12 10 22
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
BranchTrueFalseTotal
if (defined $c->{'expires'}) { } 4 4 8
208 4 push @parts, 'Expires=' . HTTP::Date::time2str( $c->{expires} );
209 }
210 else {
211 4 ▶ next if $args->{persistent};
Branch: 1 branch
BranchTrueFalseTotal
if $args->{"persistent"} 1 3 4
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
BranchTrueFalseTotal
if defined $$c{lc $attr} 28 0 28
215 }
216 7 for my $attr (qw/Secure HttpOnly HostOnly/) {
217 21 ▶ push @parts, $attr if $c->{ lc $attr };
Branch: 1 branch
BranchTrueFalseTotal
if $$c{lc $attr} 16 5 21
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
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
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
BranchTrueFalseTotal
unless defined $cookie 0 95 95
276 95 my ( $kvp, @attrs ) = split /;/, $cookie;
277 95 ▶ $kvp = '' unless defined $kvp;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $kvp 2 93 95
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
BranchTrueFalseTotal
unless defined $name and length $name 4 91 95
Condition100%3/3
defined $name and length $name
!ll&&!rl&&r
3191
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
282 91 ▶ $value = '' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 91 91
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
BranchTrueFalseTotal
unless defined $s and $s =~ /\S/ 4 144 148
Condition66%2/3
defined $s and $s =~ /\S/
!ll&&!rl&&r
04144
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
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
BranchTrueFalseTotal
unless $private ? $k =~ /^$pvt_re$/ : $k =~ /^$pub_re$/ 4 140 144
$private ? : 29 115 144
289 140 ▶ $v = 1 if $k =~ m/^(?:httponly|secure|hostonly)$/; # boolean flag if present
Branch: 1 branch
BranchTrueFalseTotal
if $k =~ /^(?:httponly|secure|hostonly)$/ 23 117 140
290 140 ▶ $v = HTTP::Date::str2time($v) || 0 if $k eq 'expires'; # convert to epoch
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "expires" 17 123 140
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
291 140 ▶ next unless length $v;
Branch: 1 branch
BranchTrueFalseTotal
unless length $v 2 138 140
292 138 ▶ $v =~ s{^\.}{} if $k eq 'domain'; # strip leading dot
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "domain" 37 101 138
293 138 ▶ $v =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg if $k eq 'path'; # unescape
Branch: 1 branch
BranchTrueFalseTotal
if $k eq "path" 45 93 138
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
BranchTrueFalseTotal
if $dom_string eq $string 53 69 122
302 69 ▶ return unless $string =~ /[a-z]/i; # non-numeric
Branch: 1 branch
BranchTrueFalseTotal
unless $string =~ /[a-z]/i 7 62 69
303 62 ▶ if ( $string =~ s{\Q$dom_string\E$}{} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($string =~ s/\Q$dom_string\E$//) 29 33 62
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
BranchTrueFalseTotal
if ($HAS_MPS) 31 0 31
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
BranchTrueFalseTotal
unless defined $host_pub_suff 4 27 31
315 31 ▶ if ( _domain_match( $host_pub_suff, $parse->{domain} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (_domain_match($host_pub_suff, $parse->{"domain"})) 4 27 31
316 4 ▶ if ( $parse->{domain} eq $host ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse->{'domain'} eq $host) { } 2 2 4
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
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
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
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
335 34 my ($default) = $path =~ m{^(.*)/}; # greedy to last /
336 34 ▶ return length($default) ? $default : "/";
Branch: 1 branch
BranchTrueFalseTotal
length $default ? : 9 25 34
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
BranchTrueFalseTotal
if $req_path eq $cookie_path 18 39 57
342 39 ▶ if ( $req_path =~ m{^\Q$cookie_path\E(.*)} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($req_path =~ /^\Q$cookie_path\E(.*)/) 34 5 39
343 34 my $rest = $1;
344 34 ▶ return 1 if substr( $cookie_path, -1, 1 ) eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if substr($cookie_path, -1, 1) eq "/" 15 19 34
345 19 ▶ return 1 if substr( $rest, 0, 1 ) eq '/';
Branch: 1 branch
BranchTrueFalseTotal
if substr($rest, 0, 1) eq "/" 19 0 19
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
BranchTrueFalseTotal
unless defined $url and length $url 0 127 127
Condition33%1/3
defined $url and length $url
!ll&&!rl&&r
00127
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
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
BranchTrueFalseTotal
() = $url =~ m[\A([^:/?#]+)://([^/?#]*)([^#?]*)] or die "Cannot parse URL: '${url}'\n" 0 127 127
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
BranchTrueFalseTotal
unless $path_query =~ m[\A/] 13 114 127
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
BranchTrueFalseTotal
length $authority ? : 127 0 127
364 127 $host =~ s/\A[^@]*@//; # userinfo
365 127 my $port = do {
366 127 ▶ $host =~ s/:([0-9]*)\z// && length $1
Branch: 3 branches
BranchTrueFalseTotal
$host =~ s/:([0-9]*)\z// && length $1 ? : 1 126 127
$scheme eq 'http' ? : 123 3 126
$scheme eq 'https' ? : 3 0 3
Condition66%2/3
$host =~ s/:([0-9]*)\z// and length $1
!ll&&!rl&&r
12601
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
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__
↑ Summary CookieJar/LWP.pm »