blib/lib/HTTP/Cookies/PhantomJS.pm

filter: Statementstmt 97.6% 124 / 127 Branchbran 81.2% 39 / 48 Conditioncond 68.0% 17 / 25 MC/DCmcdc 38.4% 5 / 13 Subroutinesub 100% 13 / 13 Podpod 100% 2 / 2 totaltotal 87.7% 200 / 228 CC 37 SCAR 22.0
CC
37
Cov
90%
load
16.7
_read_length_block
9.0
__ANON__
6.2
CRAP 9.0
↑ Summary
1 package HTTP::Cookies::PhantomJS;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use HTTP::Cookies;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use HTTP::Response;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use HTTP::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use HTTP::Headers::Util qw/split_header_words join_header_words/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use HTTP::Date qw/time2str/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our @ISA = 'HTTP::Cookies';
11 1 our $VERSION = '0.01';
12  
13 1 ▶use constant MAGIC => 'cookies="@Variant(\0\0\0\x7f\0\0\0\x16QList<QNetworkCookie>\0\0\0\0\x1';
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 my %ESCAPES = (
15 'b' => "\b",
16 'f' => "\f",
17 'n' => "\n",
18 'r' => "\r",
19 't' => "\t",
20 '\\' => '\\',
21 );
22  
23 sub _read_length_block {
24 298 ▶ my $str_ref = shift;
Subroutine _read_length_block: called CC=9 SCAR=22.0
25
26 298 my $bytes;
27 298 for (1..4) {
28 1192 my $c = substr($$str_ref, 0, 1, '');
29 1192 ▶ if ($c ne '\\') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne "\\") 107 1085 1192
30 107 $bytes .= sprintf '%x', ord($c);
31 107 next;
32 }
33
34 1085 $c = substr($$str_ref, 0, 1, '');
35 1085 ▶ if ($c ne 'x') {
Branch: 1 branch
BranchTrueFalseTotal
if ($c ne "x") 892 193 1085
36 892 ▶ if (exists $ESCAPES{$c}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $ESCAPES{$c}) { } 4 888 892
37 4 $bytes .= sprintf '%x', ord($ESCAPES{$c});
38 }
39 else {
40 888 $bytes .= sprintf '%x', int $c;
41 }
42 892 next;
43 }
44
45 193 $c = substr($$str_ref, 0, 1, '');
46 193 ▶ if (substr($$str_ref, 0, 1) =~ /[a-f0-9]/) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($$str_ref, 0, 1) =~ /[a-f0-9]/) 185 8 193
47 185 $c .= substr($$str_ref, 0, 1, '');
48 }
49 193 ▶ if (length($c) == 1 && $bytes && substr($bytes, -2) ne '\0') {
Branch: 1 branch
BranchTrueFalseTotal
if (length $c == 1 and $bytes and substr($bytes, -2) ne "\\0") 8 185 193
Condition66%4/6
length $c == 1 and $bytes and substr($bytes, -2) ne "\\0"
!ll&&!rl&&r
18508
length $c == 1 and $bytes
!ll&&!rl&&r
18508
MC/DC33%1/3
length $c == 1 and $bytes and substr($bytes, -2) ne "\\0"
length $c == 1 $bytes substr($bytes, -2) ne "\\0"
Truth table50%2/4
A and B and C
A= length $c == 1B= $bytesC= substr($bytes, -2) ne "\\0"
ABCresult
0XX0
10X0
1100
1111
50 # \0\0\x1\x4 -> 00104
51 8 $c = '0'.$c;
52 }
53 193 $bytes .= $c;
54 }
55
56 298 hex($bytes);
57 }
58  
59 sub load {
60 3 ▶ my $self = shift;
Subroutine load: called CC=16 SCAR=28.1
Pod documented
61 3 ▶ my $file = shift || $self->{'file'} || return;
Condition100%5/5
shift() || $self->{'file'} || (return)
l!l
21
shift() or $self->{"file"}
l!l&&r!l&&!r
111
MC/DC100%2/2
shift() || $self->{'file'} || (return)
shift() $self->{"file"}
Truth table100%3/3
A or B
A= shift()B= $self->{"file"}
ABresult
1X1
011
000
62
63 2 ▶ open my $fh, '<', $file or return;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or return 0 2 2
64 2 <$fh>; # omit header
65 2 my $data = <$fh>;
66 2 $data =~ s/\\"/"/g;
67 2 close $fh;
68 2 ▶ unless (substr($data, 0, length(MAGIC), '') eq MAGIC) {
Branch: 1 branch
BranchTrueFalseTotal
unless (substr($data, 0, 70, '') eq "cookies=\"\@Variant(\\0\\0\\0\\x7f\\0\\0\\0\\x16QList<QNetworkCookie>\\0\\0\\0\\0\\x1") 0 2 2
69 0 warn "$file does not seem to contain cookies";
70 0 return;
71 }
72
73 2 my $cnt = _read_length_block(\$data);
74 2 my ($len, $cookie, $cookie_str);
75 2 ▶ for (my $i=0; $i<$cnt; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $cnt) 296 2 298
76 296 $len = _read_length_block(\$data);
77 296 $cookie_str = substr($data, 0, $len, '');
78
79 # beginning may be in hex notation
80 296 my $additional = 0;
81 296 ▶ while ((my $c = substr($cookie_str, $additional, 4)) =~ /\\x[a-f0-9]{2}/) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $c = substr($cookie_str, $additional, 4)) =~ /\\x[a-f0-9]{2}/) 17 296 313
82 17 substr($cookie_str, $additional, 4) = chr hex substr $c, 2;
83 17 $additional++;
84 }
85 296 $cookie_str .= substr($data, 0, $additional*3, '');
86
87 296 ▶ if ($additional = $cookie_str =~ s/\\\\/\\/g) {
Branch: 1 branch
BranchTrueFalseTotal
if ($additional = $cookie_str =~ s/\\\\/\\/g) 0 296 296
88 0 $cookie_str .= substr($data, 0, $additional, '');
89 }
90 #print $cookie_str, "\n";
91
92 # properly process quoted values
93 # however anyway it is broken in HTTP::Cookies 6.01 - rt70721
94 296 my ($key_val) = split_header_words($cookie_str);
95 296 $key_val = join_header_words($key_val->[0], $key_val->[1]);
96 296 my $tmp = $cookie_str;
97 # value inside key_val may be quoted, but original may be not, so check it
98 296 ▶ substr($tmp, 0, substr($tmp, length($key_val), 1) eq ';' ? length($key_val)+1 : length($key_val)-1) = '';
Branch: 1 branch
BranchTrueFalseTotal
substr($tmp, length $key_val, 1) eq ';' ? : 269 27 296
99 296 my @cookie_parts = split ';', $tmp;
100
101 296 my ($domain, $path);
102 296 ▶ for (my $i=0; $i<@cookie_parts; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @cookie_parts) 861 296 1157
103 861 ▶ last if $path && $domain;
Branch: 1 branch
BranchTrueFalseTotal
if $path and $domain 0 861 861
Condition33%1/3
$path and $domain
!ll&&!rl&&r
86100
MC/DC0%0/2
$path and $domain
$path $domain
Truth table33%1/3
A and B
A= $pathB= $domain
ABresult
0X0
100
111
104 861 ▶ if (!$domain and ($domain) = $cookie_parts[$i] =~ /domain=(.+)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $domain and ($domain) = $cookie_parts[$i] =~ /domain=(.+)/) 296 565 861
Condition100%3/3
not $domain and ($domain) = $cookie_parts[$i] =~ /domain=(.+)/
!ll&&!rl&&r
296269296
MC/DC100%2/2
not $domain and ($domain) = $cookie_parts[$i] =~ /domain=(.+)/
not $domain ($domain) = $cookie_parts[$i] =~ /domain=(.+)/
Truth table100%3/3
not(A) and B
A= not $domainB= ($domain) = $cookie_parts[$i] =~ /domain=(.+)/
ABresult
0X0
100
111
105 296 next;
106 }
107 565 ▶ if (!$path) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($path) 565 0 565
108 565 ($path) = $cookie_parts[$i] =~ /path=(.+)/
109 }
110 }
111
112 # generate fake request, so we can reuse extract_cookies() method
113 296 ▶ my $req = HTTP::Request->new(GET => "http://".(substr($domain, 0, 1) eq '.' ? 'www' : '')."$domain$path");
Branch: 1 branch
BranchTrueFalseTotal
substr($domain, 0, 1) eq '.' ? : 252 44 296
114 296 my $resp = HTTP::Response->new(200, 'OK', ['Set-Cookie', $cookie_str]);
115 296 $resp->request($req);
116
117 296 $self->extract_cookies($resp);
118 }
119
120 2 1;
121 }
122  
123 sub _generate_length_block {
124 149 ▶ my $length = shift;
Subroutine _generate_length_block: called CC=3 SCAR=11.0
125
126 my $normalize = sub {
127 152 ▶ my $str = shift;
Subroutine __ANON__: called CC=2 SCAR=6.9
128 152 ▶ return $str if length($str) != 2;
Branch: 1 branch
BranchTrueFalseTotal
if length $str != 2 3 149 152
129 149 $str =~ s/^0//;
130 149 $str;
131 149 };
132
133 149 my $bytes;
134 149 my $hex = sprintf '%x', $length;
135 149 my $part;
136 149 for (1..4) {
137 596 ▶ $bytes = (length($hex) ? '\x'.$normalize->(substr($hex, -2, 2, '')) : '\0'). $bytes;
Branch: 1 branch
BranchTrueFalseTotal
length $hex ? : 152 444 596
138 }
139
140 149 $bytes;
141 }
142  
143 sub save {
144 1 ▶ my $self = shift;
Subroutine save: called CC=6 SCAR=18.1
Pod documented
145 1 ▶ my $file = shift || $self->{'file'} || return;
Condition40%2/5
shift() || $self->{'file'} || (return)
l!l
10
shift() or $self->{"file"}
l!l&&r!l&&!r
100
MC/DC0%0/2
shift() || $self->{'file'} || (return)
shift() $self->{"file"}
Truth table33%1/3
A or B
A= shift()B= $self->{"file"}
ABresult
1X1
011
000
146 1 ▶ open my $fh, '>', $file or die "Can't open $file: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $file or die "Can't open ${file}: $!" 0 1 1
147
148 1 my $res = MAGIC;
149 1 my @cookies;
150
151 $self->scan(sub {
152 148 ▶ my ($version,$key,$val,$path,$domain,$port,
Subroutine __ANON__: called CC=6 SCAR=18.3
153 $path_spec,$secure,$expires,$discard,$rest) = @_;
154
155 148 ▶ return if $discard && !$self->{ignore_discard};
Branch: 1 branch
BranchTrueFalseTotal
if $discard and not $self->{"ignore_discard"} 0 148 148
Condition66%2/3
$discard and not $self->{"ignore_discard"}
!ll&&!rl&&r
128200
MC/DC0%0/2
$discard and not $self->{"ignore_discard"}
$discard not $self->{"ignore_discard"}
Truth table66%2/3
A and not(B)
A= $discardB= not $self->{"ignore_discard"}
ABresult
0X0
100
111
156 148 my @cookie_parts;
157
158 148 ▶ push @cookie_parts, $val =~ /^"/ ? "$key=$val" : join_header_words($key, $val);
Branch: 1 branch
BranchTrueFalseTotal
$val =~ /^"/ ? : 0 148 148
159 148 ▶ push @cookie_parts, 'secure' if $secure;
Branch: 1 branch
BranchTrueFalseTotal
if $secure 0 148 148
160 148 push @cookie_parts, keys %$rest;
161 148 ▶ push @cookie_parts, 'expires='.time2str($expires) if $expires;
Branch: 1 branch
BranchTrueFalseTotal
if $expires 128 20 148
162 148 push @cookie_parts, 'domain='.$domain;
163 148 push @cookie_parts, 'path='.$path;
164
165 148 push @cookies, join '; ', @cookie_parts;
166 1 });
167
168 1 $res .= _generate_length_block(scalar @cookies);
169 1 for my $cookie (@cookies) {
170 148 $res .= _generate_length_block(length $cookie);
171 148 $cookie =~ s/\\/\\\\/g;
172 148 $cookie =~ s/"/\\"/g;
173 # any valid hex symbol at the beginning should be replaced with \x notation
174 148 my $i = 0;
175 148 ▶ while ((my $c = substr($cookie, $i, 1)) =~ /[a-f0-9]/) {
Branch: 1 branch
BranchTrueFalseTotal
if ((my $c = substr($cookie, $i, 1)) =~ /[a-f0-9]/) 15 148 163
176 15 substr($cookie, $i, 1) = sprintf '\x%x', ord($c);
177 15 $i += 4;
178 }
179 148 $res .= $cookie;
180 }
181 1 $res .= ')"';
182
183 1 print $fh "[General]\n";
184 1 print $fh $res, "\n";
185 1 close $fh;
186
187 1 1;
188 }
189  
190 1 1;
191  
192 __END__
↑ Summary