blib/lib/HTTP/Headers/Fast.pm

filter: Statementstmt 93.4% 312 / 334 Branchbran 77.5% 152 / 196 Conditioncond 72.3% 55 / 76 MC/DCmcdc 45.8% 22 / 48 Subroutinesub 97.9% 47 / 48 Podpod 10.0% 3 / 30 totaltotal 80.7% 591 / 732 CC 136 SCAR 21.0
CC
136
Cov
86%
push_header
25.0
_split_header_words
17.4
_header
14.8
CRAP 8.2
↑ Summary
1 package HTTP::Headers::Fast;
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 8 ▶use 5.00800;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 8 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 8 our $VERSION = '0.22';
8  
9 8 our $TRANSLATE_UNDERSCORE = 1;
10  
11 # "Good Practice" order of HTTP message headers:
12 # - General-Headers
13 # - Request-Headers
14 # - Response-Headers
15 # - Entity-Headers
16  
17 # yappo says "Readonly sucks".
18 8 my $OP_GET = 0;
19 8 my $OP_SET = 1;
20 8 my $OP_INIT = 2;
21 8 my $OP_PUSH = 3;
22  
23 8 my @general_headers = qw(
24 Cache-Control Connection Date Pragma Trailer Transfer-Encoding Upgrade
25 Via Warning
26 );
27  
28 8 my @request_headers = qw(
29 Accept Accept-Charset Accept-Encoding Accept-Language
30 Authorization Expect From Host
31 If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since
32 Max-Forwards Proxy-Authorization Range Referer TE User-Agent
33 );
34  
35 8 my @response_headers = qw(
36 Accept-Ranges Age ETag Location Proxy-Authenticate Retry-After Server
37 Vary WWW-Authenticate
38 );
39  
40 8 my @entity_headers = qw(
41 Allow Content-Encoding Content-Language Content-Length Content-Location
42 Content-MD5 Content-Range Content-Type Expires Last-Modified
43 );
44  
45 8 my %entity_header = map { lc($_) => 1 } @entity_headers;
46  
47 8 my @header_order =
48 ( @general_headers, @request_headers, @response_headers, @entity_headers, );
49  
50 # Make alternative representations of @header_order. This is used
51 # for sorting and case matching.
52 8 my %header_order;
53 8 our %standard_case;
54  
55 {
56 8 my $i = 0;
57 8 for (@header_order) {
58 376 my $lc = lc $_;
59 376 $header_order{$lc} = ++$i;
60 376 $standard_case{$lc} = $_;
61 }
62 }
63  
64 29 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod undocumented
65 29 my ($class) = shift;
66 29 my $self = bless {}, $class;
67 29 ▶ $self->header(@_) if @_; # set up initial headers
Branch: 1 branch
BranchTrueFalseTotal
if @_ 17 12 29
68 29 $self;
69 }
70  
71 5 ▶sub isa {
Subroutine isa: called CC=4 SCAR=14.1
Pod undocumented
72 5 my ($self, $klass) = @_;
73 5 ▶ my $proto = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
500
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
74 5 ▶ return ($proto eq $klass || $klass eq 'HTTP::Headers') ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$proto eq $klass || $klass eq 'HTTP::Headers' ? : 4 1 5
Condition100%3/3
$proto eq $klass or $klass eq "HTTP::Headers"
l!l&&r!l&&!r
221
MC/DC100%2/2
$proto eq $klass or $klass eq "HTTP::Headers"
$proto eq $klass $klass eq "HTTP::Headers"
Truth table100%3/3
A or B
A= $proto eq $klassB= $klass eq "HTTP::Headers"
ABresult
1X1
011
000
75 }
76  
77 68 ▶sub header {
Subroutine header: called CC=8 SCAR=20.8
Pod undocumented
78 68 my $self = shift;
79 68 ▶ Carp::croak('Usage: $h->header($field, ...)') unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 67 68
80 67 my (@old);
81  
82 67 ▶ if (@_ == 1) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 30 37 67
elsif (@_ == 2) { } 21 16 37
83 30 @old = $self->_header_get(@_);
84 } elsif( @_ == 2 ) {
85 21 @old = $self->_header_set(@_);
86 } else {
87 16 my %seen;
88 16 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 56 16 72
89 56 my $field = shift;
90 56 ▶ if ( $seen{ lc $field }++ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($seen{lc $field}++) { } 7 49 56
91 7 @old = $self->_header_push($field, shift);
92 } else {
93 49 @old = $self->_header_set($field, shift);
94 }
95 }
96 }
97 67 ▶ return @old if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 8 59 67
98 59 ▶ return $old[0] if @old <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @old <= 1 51 8 59
99 8 join( ", ", @old );
100 }
101  
102 5 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod undocumented
103 5 my $self = shift;
104 5 %$self = ();
105 }
106  
107 6 ▶sub push_header {
Subroutine push_header: called CC=11 SCAR=32.2
Pod undocumented
108 6 my $self = shift;
109  
110 6 ▶ if (@_ == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 2) { } 6 0 6
111 6 my ($field, $val) = @_;
112 6 ▶ $field = _standardize_field_name($field) unless $field =~ /^:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /^:/ 5 1 6
113  
114 6 my $h = $self->{$field};
115 6 ▶ if (!defined $h) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $h) { } 1 5 6
elsif (ref $h ne 'ARRAY') { } 3 2 5
116 1 $h = [];
117 1 $self->{$field} = $h;
118 } elsif (ref $h ne 'ARRAY') {
119 3 $h = [ $h ];
120 3 $self->{$field} = $h;
121 }
122
123 6 ▶ push @$h, ref $val ne 'ARRAY' ? $val : @$val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val ne 'ARRAY' ? : 5 1 6
124 } else {
125 0 ▶ while ( my ($field, $val) = splice( @_, 0, 2 ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 0 0 0
126 0 ▶ $field = _standardize_field_name($field) unless $field =~ /^:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /^:/ 0 0 0
127  
128 0 my $h = $self->{$field};
129 0 ▶ if (!defined $h) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $h) { } 0 0 0
elsif (ref $h ne 'ARRAY') { } 0 0 0
130 0 $h = [];
131 0 $self->{$field} = $h;
132 } elsif (ref $h ne 'ARRAY') {
133 0 $h = [ $h ];
134 0 $self->{$field} = $h;
135 }
136
137 0 ▶ push @$h, ref $val ne 'ARRAY' ? $val : @$val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val ne 'ARRAY' ? : 0 0 0
138 }
139 }
140 6 return ();
141 }
142  
143 5 ▶sub init_header {
Subroutine init_header: called CC=2 SCAR=6.9
Pod undocumented
144 5 ▶ Carp::croak('Usage: $h->init_header($field, $val)') if @_ != 3;
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 1 4 5
145 4 shift->_header( @_, $OP_INIT );
146 }
147  
148 14 ▶sub remove_header {
Subroutine remove_header: called CC=6 SCAR=17.9
Pod undocumented
149 14 my ( $self, @fields ) = @_;
150 14 my $field;
151 my @values;
152 14 for my $field (@fields) {
153 21 ▶ $field =~ tr/_/-/ if $field !~ /^:/ && $TRANSLATE_UNDERSCORE;
Branch: 1 branch
BranchTrueFalseTotal
if not $field =~ /^:/ and $HTTP::Headers::Fast::TRANSLATE_UNDERSCORE 20 1 21
Condition66%2/3
not $field =~ /^:/ and $HTTP::Headers::Fast::TRANSLATE_UNDERSCORE
!ll&&!rl&&r
0120
MC/DC50%1/2
not $field =~ /^:/ and $HTTP::Headers::Fast::TRANSLATE_UNDERSCORE
not $field =~ /^:/ $HTTP::Headers::Fast::TRANSLATE_UNDERSCORE
Truth table66%2/3
not(A) and B
A= not $field =~ /^:/B= $HTTP::Headers::Fast::TRANSLATE_UNDERSCORE
ABresult
0X0
100
111
154 21 my $v = delete $self->{ lc $field };
155 21 ▶ push( @values, ref($v) eq 'ARRAY' ? @$v : $v ) if defined $v;
Branch: 2 branches
BranchTrueFalseTotal
if defined $v 17 4 21
ref $v eq 'ARRAY' ? : 4 13 17
156 }
157 14 return @values;
158 }
159  
160 3 ▶sub remove_content_headers {
Subroutine remove_content_headers: called CC=5 SCAR=16.1
Pod undocumented
161 3 my $self = shift;
162 3 ▶ unless ( defined(wantarray) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 1 2 3
163  
164 # fast branch that does not create return object
165 1 ▶ delete @$self{ grep $entity_header{$_} || /^content-/, keys %$self };
Condition100%3/3
$entity_header{$_} || /^content-/
l!l&&r!l&&!r
615
MC/DC100%2/2
$entity_header{$_} || /^content-/
$entity_header{$_} /^content-/
Truth table100%3/3
A || B
A= $entity_header{$_}B= /^content-/
ABresult
1X1
011
000
166 1 return;
167 }
168  
169 2 my $c = ref($self)->new;
170 2 ▶ for my $f ( grep $entity_header{$_} || /^content-/, keys %$self ) {
Condition100%3/3
$entity_header{$_} || /^content-/
l!l&&r!l&&!r
827
MC/DC100%2/2
$entity_header{$_} || /^content-/
$entity_header{$_} /^content-/
Truth table100%3/3
A || B
A= $entity_header{$_}B= /^content-/
ABresult
1X1
011
000
171 10 $c->{$f} = delete $self->{$f};
172 }
173 2 $c;
174 }
175  
176 8 my %field_name;
177 148 ▶sub _standardize_field_name {
Subroutine _standardize_field_name: called CC=4 SCAR=13.9
178 148 my $field = shift;
179  
180 148 ▶ $field =~ tr/_/-/ if $TRANSLATE_UNDERSCORE;
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Headers::Fast::TRANSLATE_UNDERSCORE 142 6 148
181 148 ▶ if (my $cache = $field_name{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $cache = $field_name{$field}) 74 74 148
182 74 return $cache;
183 }
184  
185 74 my $old = $field;
186 74 $field = lc $field;
187 74 ▶ unless ( defined $standard_case{$field} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $HTTP::Headers::Fast::standard_case{$field}) 34 40 74
188 # generate a %standard_case entry for this field
189 34 $old =~ s/\b(\w)/\u$1/g;
190 34 $standard_case{$field} = $old;
191 }
192 74 $field_name{$old} = $field;
193 74 return $field;
194 }
195  
196 41 ▶sub _header_get {
Subroutine _header_get: called CC=5 SCAR=16.1
197 41 my ($self, $field, $skip_standardize) = @_;
198  
199 41 ▶ $field = _standardize_field_name($field) unless $skip_standardize || $field =~ /^:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $skip_standardize or $field =~ /^:/ 28 13 41
Condition100%3/3
$skip_standardize or $field =~ /^:/
l!l&&r!l&&!r
11228
MC/DC100%2/2
$skip_standardize or $field =~ /^:/
$skip_standardize $field =~ /^:/
Truth table100%3/3
A or B
A= $skip_standardizeB= $field =~ /^:/
ABresult
1X1
011
000
200  
201 41 my $h = $self->{$field};
202 41 ▶ return (ref($h) eq 'ARRAY') ? @$h : ( defined($h) ? ($h) : () );
Branch: 2 branches
BranchTrueFalseTotal
ref $h eq 'ARRAY' ? : 9 32 41
defined $h ? : 23 9 32
203 }
204  
205 85 ▶sub _header_set {
Subroutine _header_set: called CC=7 SCAR=19.6
206 85 my ($self, $field, $val) = @_;
207  
208 85 ▶ $field = _standardize_field_name($field) unless $field =~ /^:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /^:/ 84 1 85
209  
210 85 my $h = $self->{$field};
211 85 ▶ my @old = ref($h) eq 'ARRAY' ? @$h : ( defined($h) ? ($h) : () );
Branch: 2 branches
BranchTrueFalseTotal
ref $h eq 'ARRAY' ? : 2 83 85
defined $h ? : 7 76 83
212 85 ▶ if ( defined($val) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val) { } 84 1 85
213 84 ▶ if (ref $val eq 'ARRAY' && scalar(@$val) == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq "ARRAY" and scalar @$val == 1) 0 84 84
Condition66%2/3
ref $val eq "ARRAY" and scalar @$val == 1
!ll&&!rl&&r
7950
MC/DC0%0/2
ref $val eq "ARRAY" and scalar @$val == 1
ref $val eq "ARRAY" scalar @$val == 1
Truth table66%2/3
A and B
A= ref $val eq "ARRAY"B= scalar @$val == 1
ABresult
0X0
100
111
214 0 $val = $val->[0];
215 }
216 84 $self->{$field} = $val;
217 } else {
218 1 delete $self->{$field};
219 }
220 85 return @old;
221 }
222  
223 7 ▶sub _header_push {
Subroutine _header_push: called CC=7 SCAR=22.0
224 7 my ($self, $field, $val) = @_;
225  
226 7 ▶ $field = _standardize_field_name($field) unless $field =~ /^:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /^:/ 7 0 7
227  
228 7 my $h = $self->{$field};
229 7 ▶ if (ref($h) eq 'ARRAY') {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $h eq 'ARRAY') { } 3 4 7
elsif (defined $h) { } 4 0 4
230 3 my @old = @$h;
231 3 ▶ push @$h, ref $val ne 'ARRAY' ? $val : @$val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val ne 'ARRAY' ? : 3 0 3
232 3 return @old;
233 } elsif (defined $h) {
234 4 ▶ $self->{$field} = [$h, ref $val ne 'ARRAY' ? $val : @$val ];
Branch: 1 branch
BranchTrueFalseTotal
ref $val ne 'ARRAY' ? : 4 0 4
235 4 return ($h);
236 } else {
237 0 ▶ $self->{$field} = ref $val ne 'ARRAY' ? $val : @$val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val ne 'ARRAY' ? : 0 0 0
238 0 return ();
239 }
240 }
241  
242 24 ▶sub _header {
Subroutine _header: called CC=14 SCAR=27.0
243 24 my ($self, $field, $val, $op) = @_;
244  
245 24 ▶ $field = _standardize_field_name($field) unless $field =~ /^:/;
Branch: 1 branch
BranchTrueFalseTotal
unless $field =~ /^:/ 24 0 24
246  
247 24 ▶ $op ||= defined($val) ? $OP_SET : $OP_GET;
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 6 14 20
Condition100%3/3
$op ||= defined $val ? $OP_SET : $OP_GET
l!l&&r!l&&!r
4614
MC/DC100%2/2
$op ||= defined $val ? $OP_SET : $OP_GET
$op defined $val ? $OP_SET : $OP_GET
Truth table100%3/3
A ||= B
A= $opB= defined $val ? $OP_SET : $OP_GET
ABresult
1X1
011
000
248  
249 24 my $h = $self->{$field};
250 24 ▶ my @old = ref($h) eq 'ARRAY' ? @$h : ( defined($h) ? ($h) : () );
Branch: 2 branches
BranchTrueFalseTotal
ref $h eq 'ARRAY' ? : 0 24 24
defined $h ? : 16 8 24
251  
252 24 ▶ unless ( $op == $OP_GET || ( $op == $OP_INIT && @old ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op == $OP_GET or $op == $OP_INIT and @old) 8 16 24
Condition100%6/6
$op == $OP_GET or $op == $OP_INIT and @old
l!l&&r!l&&!r
1428
$op == $OP_INIT and @old
!ll&&!rl&&r
622
MC/DC100%3/3
$op == $OP_GET or $op == $OP_INIT and @old
$op == $OP_GET $op == $OP_INIT @old
Truth table100%4/4
A or B and C
A= $op == $OP_GETB= $op == $OP_INITC= @old
ABCresult
1XX1
0111
00X0
0100
253 8 ▶ if ( defined($val) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $val) { } 8 0 8
elsif ($op != $OP_PUSH) { } 0 0 0
254 8 ▶ my @new = ( $op == $OP_PUSH ) ? @old : ();
Branch: 1 branch
BranchTrueFalseTotal
$op == $OP_PUSH ? : 0 8 8
255 8 ▶ if ( ref($val) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val ne 'ARRAY') { } 7 1 8
256 7 push( @new, $val );
257 }
258 else {
259 1 push( @new, @$val );
260 }
261 8 ▶ $self->{$field} = @new > 1 ? \@new : $new[0];
Branch: 1 branch
BranchTrueFalseTotal
@new > 1 ? : 1 7 8
262 }
263 elsif ( $op != $OP_PUSH ) {
264 0 delete $self->{$field};
265 }
266 }
267 24 @old;
268 }
269  
270 45 ▶sub _sorted_field_names {
Subroutine _sorted_field_names: called CC=4 SCAR=13.9
271 45 my $self = shift;
272 return [ sort {
273 45 ▶ ( $header_order{$a} || 999 ) <=> ( $header_order{$b} || 999 )
Condition85%6/7
($header_order{$a} || 999) <=> ($header_order{$b} || 999) || $a cmp $b
l!l&&r!l&&!r
178420
$header_order{$a} || 999
l!l
14773
$header_order{$b} || 999
l!l
13882
MC/DC0%0/2
($header_order{$a} || 999) <=> ($header_order{$b} || 999) || $a cmp $b
$header_order{$a} $a cmp $b
MC/DC100%1/1
$header_order{$b} || 999
$header_order{$b}
Truth table66%2/3
A || B
A= $header_order{$a}B= $a cmp $b
ABresult
1X1
011
000
Truth table100%2/2
A
A= $header_order{$b}
Aresult
11
00
274 || $a cmp $b
275 } keys %$self ];
276 }
277  
278 8 ▶sub header_field_names {
Subroutine header_field_names: called CC=3 SCAR=11.0
Pod undocumented
279 8 my $self = shift;
280 8 ▶ return map $standard_case{$_} || $_, @{ $self->_sorted_field_names }
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 6 2 8
Condition66%2/3
$HTTP::Headers::Fast::standard_case{$_} || $_
l!l&&r!l&&!r
1220
MC/DC0%0/2
$HTTP::Headers::Fast::standard_case{$_} || $_
$HTTP::Headers::Fast::standard_case{$_} $_
Truth table66%2/3
A || B
A= $HTTP::Headers::Fast::standard_case{$_}B= $_
ABresult
1X1
011
000
281 if wantarray;
282 2 return keys %$self;
283 }
284  
285 4 ▶sub scan {
Subroutine scan: called CC=7 SCAR=20.7
Pod undocumented
286 4 my ( $self, $sub ) = @_;
287 4 for my $key (@{ $self->_sorted_field_names }) {
288 12 ▶ next if substr($key, 0, 1) eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if substr($key, 0, 1) eq "_" 0 12 12
289 12 my $vals = $self->{$key};
290 12 ▶ if ( ref($vals) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $vals eq 'ARRAY') { } 3 9 12
291 3 for my $val (@$vals) {
292 7 ▶ $sub->( $standard_case{$key} || $key, $val );
Condition33%1/3
$HTTP::Headers::Fast::standard_case{$key} || $key
l!l&&r!l&&!r
700
MC/DC0%0/2
$HTTP::Headers::Fast::standard_case{$key} || $key
$HTTP::Headers::Fast::standard_case{$key} $key
Truth table33%1/3
A || B
A= $HTTP::Headers::Fast::standard_case{$key}B= $key
ABresult
1X1
011
000
293 }
294 }
295 else {
296 9 ▶ $sub->( $standard_case{$key} || $key, $vals );
Condition33%1/3
$HTTP::Headers::Fast::standard_case{$key} || $key
l!l&&r!l&&!r
900
MC/DC0%0/2
$HTTP::Headers::Fast::standard_case{$key} || $key
$HTTP::Headers::Fast::standard_case{$key} $key
Truth table33%1/3
A || B
A= $HTTP::Headers::Fast::standard_case{$key}B= $key
ABresult
1X1
011
000
297 }
298 }
299 }
300  
301 10 ▶sub _process_newline {
Subroutine _process_newline: called CC=1 SCAR=0.0
302 10 local $_ = shift;
303 10 my $endl = shift;
304 # must handle header values with embedded newlines with care
305 10 s/\s+$//; # trailing newlines and space must go
306 10 s/\n(\x0d?\n)+/\n/g; # no empty lines
307 10 s/\n([^\040\t])/\n $1/g; # intial space for continuation
308 10 s/\n/$endl/g; # substitute with requested line ending
309 10 $_;
310 }
311  
312 28 ▶sub _as_string {
Subroutine _as_string: called CC=9 SCAR=22.3
313 28 my ($self, $endl, $fieldnames) = @_;
314  
315 28 my @result;
316 28 for my $key ( @$fieldnames ) {
317 114 ▶ next if index($key, '_') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($key, "_") == 0 0 114 114
318 114 my $vals = $self->{$key};
319 114 ▶ if ( ref($vals) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $vals eq 'ARRAY') { } 7 107 114
320 7 for my $val (@$vals) {
321 16 ▶ my $field = $standard_case{$key} || $key;
Condition66%2/3
$HTTP::Headers::Fast::standard_case{$key} || $key
l!l&&r!l&&!r
1510
MC/DC0%0/2
$HTTP::Headers::Fast::standard_case{$key} || $key
$HTTP::Headers::Fast::standard_case{$key} $key
Truth table66%2/3
A || B
A= $HTTP::Headers::Fast::standard_case{$key}B= $key
ABresult
1X1
011
000
322 16 $field =~ s/^://;
323 16 ▶ if ( index($val, "\n") >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($val, "\n") != -1) 0 16 16
324 0 $val = _process_newline($val, $endl);
325 }
326 16 push @result, $field . ': ' . $val;
327 }
328 } else {
329 107 ▶ my $field = $standard_case{$key} || $key;
Condition66%2/3
$HTTP::Headers::Fast::standard_case{$key} || $key
l!l&&r!l&&!r
10610
MC/DC0%0/2
$HTTP::Headers::Fast::standard_case{$key} || $key
$HTTP::Headers::Fast::standard_case{$key} $key
Truth table66%2/3
A || B
A= $HTTP::Headers::Fast::standard_case{$key}B= $key
ABresult
1X1
011
000
330 107 $field =~ s/^://;
331 107 ▶ if ( index($vals, "\n") >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($vals, "\n") != -1) 10 97 107
332 10 $vals = _process_newline($vals, $endl);
333 }
334 107 push @result, $field . ': ' . $vals;
335 }
336 }
337  
338 28 join( $endl, @result, '' );
339 }
340  
341 27 ▶sub as_string {
Subroutine as_string: called CC=2 SCAR=6.9
Pod undocumented
342 27 my ( $self, $endl ) = @_;
343 27 ▶ $endl = "\n" unless defined $endl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $endl 25 2 27
344 27 $self->_as_string($endl, $self->_sorted_field_names);
345 }
346  
347 1 ▶sub as_string_without_sort {
Subroutine as_string_without_sort: called CC=2 SCAR=7.1
Pod documented
348 1 my ( $self, $endl ) = @_;
349 1 ▶ $endl = "\n" unless defined $endl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $endl 1 0 1
350 1 $self->_as_string($endl, [keys(%$self)]);
351 }
352  
353  
354 12 ▶sub _psgi_flatten {
Subroutine _psgi_flatten: called CC=7 SCAR=20.2
355 12 my ($self, $keys) = @_;
356 12 my @headers;
357 12 for my $key ( @{$keys} ) {
358 19 ▶ next if substr($key, 0, 1) eq '_';
Branch: 1 branch
BranchTrueFalseTotal
if substr($key, 0, 1) eq "_" 0 19 19
359 19 my $vals = $self->{$key};
360 19 ▶ if ( ref($vals) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $vals eq 'ARRAY') { } 3 16 19
361 3 for my $val (@$vals) {
362 0 $val =~ s/\015\012[\040|\011]+/chr(32)/ge; # replace LWS with a single SP
363 8 $val =~ s/\015|\012//g; # remove CR and LF since the char is invalid here
364 8 ▶ push @headers, $standard_case{$key} || $key, $val;
Condition33%1/3
$HTTP::Headers::Fast::standard_case{$key} || $key
l!l&&r!l&&!r
800
MC/DC0%0/2
$HTTP::Headers::Fast::standard_case{$key} || $key
$HTTP::Headers::Fast::standard_case{$key} $key
Truth table33%1/3
A || B
A= $HTTP::Headers::Fast::standard_case{$key}B= $key
ABresult
1X1
011
000
365 }
366 }
367 else {
368 16 $vals =~ s/\015\012[\040|\011]+/chr(32)/ge; # replace LWS with a single SP
369 16 $vals =~ s/\015|\012//g; # remove CR and LF since the char is invalid here
370 16 ▶ push @headers, $standard_case{$key} || $key, $vals;
Condition33%1/3
$HTTP::Headers::Fast::standard_case{$key} || $key
l!l&&r!l&&!r
1600
MC/DC0%0/2
$HTTP::Headers::Fast::standard_case{$key} || $key
$HTTP::Headers::Fast::standard_case{$key} $key
Truth table33%1/3
A || B
A= $HTTP::Headers::Fast::standard_case{$key}B= $key
ABresult
1X1
011
000
371 }
372 }
373 12 return \@headers;
374 }
375  
376 8 ▶sub psgi_flatten {
Subroutine psgi_flatten: called CC=1 SCAR=0.0
Pod documented
377 8 $_[0]->_psgi_flatten($_[0]->_sorted_field_names);
378 }
379  
380  
381 4 ▶sub psgi_flatten_without_sort {
Subroutine psgi_flatten_without_sort: called CC=1 SCAR=0.0
Pod documented
382 4 $_[0]->_psgi_flatten([keys %{$_[0]}]);
383 }
384  
385 {
386 8 my $storable_required;
387 12 ▶ sub clone {
Subroutine clone: called CC=2 SCAR=6.9
Pod undocumented
388 12 ▶ unless ($storable_required) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($storable_required) 1 11 12
389 1 require Storable;
390 1 $storable_required++;
391 }
392 12 goto &Storable::dclone;
393 }
394 }
395  
396 8 ▶sub _date_header {
Subroutine _date_header: called CC=3 SCAR=11.0
397 16 require HTTP::Date;
398 16 my ( $self, $header, $time ) = @_;
399 16 my $old;
400 16 ▶ if ( defined $time ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time) { } 5 11 16
401 5 ($old) = $self->_header_set( $header, HTTP::Date::time2str($time) );
402 } else {
403 11 ($old) = $self->_header_get($header, 1);
404 }
405 16 ▶ $old =~ s/;.*// if defined($old);
Branch: 1 branch
BranchTrueFalseTotal
if defined $old 6 10 16
406 16 HTTP::Date::str2time($old);
407 }
408  
409 3 ▶sub date { shift->_date_header( 'date', @_ ); }
Subroutine date: called CC=1 SCAR=0.0
Pod undocumented
410 3 ▶sub expires { shift->_date_header( 'expires', @_ ); }
Subroutine expires: called CC=1 SCAR=0.0
Pod undocumented
411 4 ▶sub if_modified_since { shift->_date_header( 'if-modified-since', @_ ); }
Subroutine if_modified_since: called CC=1 SCAR=0.0
Pod undocumented
412 3 ▶sub if_unmodified_since { shift->_date_header( 'if-unmodified-since', @_ ); }
Subroutine if_unmodified_since: called CC=1 SCAR=0.0
Pod undocumented
413 3 ▶sub last_modified { shift->_date_header( 'last-modified', @_ ); }
Subroutine last_modified: called CC=1 SCAR=0.0
Pod undocumented
414  
415 # This is used as a private LWP extension. The Client-Date header is
416 # added as a timestamp to a response when it has been received.
417 0 ▶sub client_date { shift->_date_header( 'client-date', @_ ); }
Subroutine client_date: not called CC=1 SCAR=6.9
Pod undocumented
418  
419 # The retry_after field is dual format (can also be a expressed as
420 # number of seconds from now), so we don't provide an easy way to
421 # access it until we have know how both these interfaces can be
422 # addressed. One possibility is to return a negative value for
423 # relative seconds and a positive value for epoch based time values.
424 #sub retry_after { shift->_date_header('Retry-After', @_); }
425  
426 29 ▶sub content_type {
Subroutine content_type: called CC=7 SCAR=19.5
Pod undocumented
427 29 my $self = shift;
428 29 my $ct = $self->{'content-type'};
429 29 ▶ $self->{'content-type'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 9 20 29
430 29 ▶ $ct = $ct->[0] if ref($ct) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ct eq "ARRAY" 0 29 29
431 29 ▶ return '' unless defined($ct) && length($ct);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ct and length $ct 5 24 29
Condition66%2/3
defined $ct and length $ct
!ll&&!rl&&r
5024
MC/DC50%1/2
defined $ct and length $ct
defined $ct length $ct
Truth table66%2/3
A and B
A= defined $ctB= length $ct
ABresult
0X0
100
111
432 24 my @ct = split( /;\s*/, $ct, 2 );
433 24 for ( $ct[0] ) {
434 24 s/\s+//g;
435 24 $_ = lc($_);
436 }
437 24 ▶ wantarray ? @ct : $ct[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 22 24
438 }
439  
440 2 ▶sub content_type_charset {
Subroutine content_type_charset: called CC=9 SCAR=22.3
Pod undocumented
441 2 my $self = shift;
442 2 my $h = $self->{'content-type'};
443 2 ▶ $h = $h->[0] if ref($h);
Branch: 1 branch
BranchTrueFalseTotal
if ref $h 0 2 2
444 2 ▶ $h = "" unless defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 1 1 2
445 2 my @v = _split_header_words($h);
446 2 ▶ if (@v) {
Branch: 1 branch
BranchTrueFalseTotal
if (@v) 1 1 2
447 1 my($ct, undef, %ct_param) = @{$v[0]};
448 1 my $charset = $ct_param{charset};
449 1 ▶ if ($ct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct) 1 0 1
450 1 $ct = lc($ct);
451 1 $ct =~ s/\s+//;
452 }
453 1 ▶ if ($charset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charset) 1 0 1
454 1 $charset = uc($charset);
455 1 $charset =~ s/^\s+//; $charset =~ s/\s+\z//;
456 1 ▶ undef($charset) if $charset eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $charset eq "" 0 1 1
457 }
458 1 ▶ return $ct, $charset if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 1 1
459 1 return $charset;
460 }
461 1 ▶ return undef, undef if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 1 1
462 1 return undef;
463 }
464  
465 sub _split_header_words
466 2 ▶{
Subroutine _split_header_words: called CC=13 SCAR=28.5
467 2 my(@val) = @_;
468 2 my @res;
469 2 for (@val) {
470 2 my @cur;
471 2 ▶ while (length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) 3 2 5
472 3 ▶ if (s/^\s*(=*[^\s=;,]+)//) { # 'token' or parameter 'attribute'
Branch: 3 branches
BranchTrueFalseTotal
if (s/^\s*(=*[^\s=;,]+)//) { } 2 1 3
elsif (s/^\s*,//) { } 0 1 1
elsif (s/^\s*;// or s/^\s+//) { } 1 0 1
473 2 push(@cur, $1);
474 # a quoted value
475 2 ▶ if (s/^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"//) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"//) { } 1 1 2
elsif (s/^\s*=\s*([^;,\s]*)//) { } 0 1 1
476 1 my $val = $1;
477 1 $val =~ s/\\(.)/$1/g;
478 1 push(@cur, $val);
479 # some unquoted value
480 }
481 elsif (s/^\s*=\s*([^;,\s]*)//) {
482 0 my $val = $1;
483 0 $val =~ s/\s+$//;
484 0 push(@cur, $val);
485 # no value, a lone token
486 }
487 else {
488 1 push(@cur, undef);
489 }
490 }
491 elsif (s/^\s*,//) {
492 0 ▶ push(@res, [@cur]) if @cur;
Branch: 1 branch
BranchTrueFalseTotal
if @cur 0 0 0
493 0 @cur = ();
494 }
495 ▶ elsif (s/^\s*;// || s/^\s+//) {
Condition33%1/3
s/^\s*;// or s/^\s+//
l!l&&r!l&&!r
100
MC/DC0%0/2
s/^\s*;// or s/^\s+//
s/^\s*;// s/^\s+//
Truth table33%1/3
A or B
A= s/^\s*;//B= s/^\s+//
ABresult
1X1
011
000
496 # continue
497 }
498 else {
499 0 die "This should not happen: '$_'";
500 }
501 }
502 2 ▶ push(@res, \@cur) if @cur;
Branch: 1 branch
BranchTrueFalseTotal
if @cur 1 1 2
503 }
504  
505 2 for my $arr (@res) {
506 1 ▶ for (my $i = @$arr - 2; $i >= 0; $i -= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 2 1 3
507 2 $arr->[$i] = lc($arr->[$i]);
508 }
509 }
510 2 return @res;
511 }
512  
513 3 ▶sub content_is_text {
Subroutine content_is_text: called CC=1 SCAR=0.0
Pod undocumented
514 3 my $self = shift;
515 3 return $self->content_type =~ m,^text/,;
516 }
517  
518 3 ▶sub content_is_html {
Subroutine content_is_html: called CC=2 SCAR=6.9
Pod undocumented
519 3 my $self = shift;
520 3 ▶ return $self->content_type eq 'text/html' || $self->content_is_xhtml;
Condition100%3/3
$self->content_type eq 'text/html' || $self->content_is_xhtml
l!l&&r!l&&!r
111
MC/DC100%2/2
$self->content_type eq 'text/html' || $self->content_is_xhtml
$self->content_type eq 'text/html' $self->content_is_xhtml
Truth table100%3/3
A || B
A= $self->content_type eq 'text/html'B= $self->content_is_xhtml
ABresult
1X1
011
000
521 }
522  
523 5 ▶sub content_is_xhtml {
Subroutine content_is_xhtml: called CC=2 SCAR=7.1
Pod undocumented
524 5 my $ct = shift->content_type;
525 5 ▶ return $ct eq "application/xhtml+xml"
Condition66%2/3
$ct eq 'application/xhtml+xml' || $ct eq 'application/vnd.wap.xhtml+xml'
l!l&&r!l&&!r
203
MC/DC50%1/2
$ct eq 'application/xhtml+xml' || $ct eq 'application/vnd.wap.xhtml+xml'
$ct eq 'application/xhtml+xml' $ct eq 'application/vnd.wap.xhtml+xml'
Truth table66%2/3
A || B
A= $ct eq 'application/xhtml+xml'B= $ct eq 'application/vnd.wap.xhtml+xml'
ABresult
1X1
011
000
526 || $ct eq "application/vnd.wap.xhtml+xml";
527 }
528  
529 3 ▶sub content_is_xml {
Subroutine content_is_xml: called CC=4 SCAR=14.1
Pod undocumented
530 3 my $ct = shift->content_type;
531 3 ▶ return 1 if $ct eq "text/xml";
Branch: 1 branch
BranchTrueFalseTotal
if $ct eq "text/xml" 0 3 3
532 3 ▶ return 1 if $ct eq "application/xml";
Branch: 1 branch
BranchTrueFalseTotal
if $ct eq "application/xml" 0 3 3
533 3 ▶ return 1 if $ct =~ /\+xml$/;
Branch: 1 branch
BranchTrueFalseTotal
if $ct =~ /\+xml$/ 1 2 3
534 2 return 0;
535 }
536  
537 8 ▶sub referer {
Subroutine referer: called CC=4 SCAR=13.9
Pod undocumented
538 8 my $self = shift;
539 8 ▶ if ( @_ && $_[0] =~ /#/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $_[0] =~ /#/) 2 6 8
Condition100%3/3
@_ and $_[0] =~ /#/
!ll&&!rl&&r
512
MC/DC100%2/2
@_ and $_[0] =~ /#/
@_ $_[0] =~ /#/
Truth table100%3/3
A and B
A= @_B= $_[0] =~ /#/
ABresult
0X0
100
111
540  
541 # Strip fragment per RFC 2616, section 14.36.
542 2 my $uri = shift;
543 2 ▶ if ( ref($uri) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uri) { } 1 1 2
544 1 $uri = $uri->clone;
545 1 $uri->fragment(undef);
546 }
547 else {
548 1 $uri =~ s/\#.*//;
549 }
550 2 unshift @_, $uri;
551 }
552 8 ( $self->_header( 'Referer', @_ ) )[0];
553 }
554 8 *referrer = \&referer; # on tchrist's request
555  
556 8 for my $key (qw/content-length content-language content-encoding title user-agent server from warnings www-authenticate authorization proxy-authenticate proxy-authorization/) {
557 8 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
558 96 (my $meth = $key) =~ s/-/_/g;
559 96 ▶ *{$meth} = sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
560 24 my $self = shift;
561 24 ▶ if (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 10 14 24
562 10 ( $self->_header_set( $key, @_ ) )[0]
563 } else {
564 14 my $h = $self->{$key};
565 14 ▶ (ref($h) eq 'ARRAY') ? $h->[0] : $h;
Branch: 1 branch
BranchTrueFalseTotal
ref $h eq 'ARRAY' ? : 0 14 14
566 }
567 96 };
568 }
569  
570 7 ▶sub authorization_basic { shift->_basic_auth( "Authorization", @_ ) }
Subroutine authorization_basic: called CC=1 SCAR=0.0
Pod undocumented
571 2 ▶sub proxy_authorization_basic {
Subroutine proxy_authorization_basic: called CC=1 SCAR=0.0
Pod undocumented
572 2 shift->_basic_auth( "Proxy-Authorization", @_ );
573 }
574  
575 9 ▶sub _basic_auth {
Subroutine _basic_auth: called CC=7 SCAR=19.5
576 9 require MIME::Base64;
577 9 my ( $self, $h, $user, $passwd ) = @_;
578 9 my ($old) = $self->_header($h);
579 9 ▶ if ( defined $user ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user) 4 5 9
580 4 ▶ Carp::croak("Basic authorization user name can't contain ':'")
Branch: 1 branch
BranchTrueFalseTotal
if $user =~ /:/ 1 3 4
581 if $user =~ /:/;
582 3 ▶ $passwd = '' unless defined $passwd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $passwd 1 2 3
583 3 $self->_header(
584 $h => 'Basic ' . MIME::Base64::encode( "$user:$passwd", '' ) );
585 }
586 8 ▶ if ( defined $old && $old =~ s/^\s*Basic\s+// ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old and $old =~ s/^\s*Basic\s+//) 5 3 8
Condition66%2/3
defined $old and $old =~ s/^\s*Basic\s+//
!ll&&!rl&&r
305
MC/DC50%1/2
defined $old and $old =~ s/^\s*Basic\s+//
defined $old $old =~ s/^\s*Basic\s+//
Truth table66%2/3
A and B
A= defined $oldB= $old =~ s/^\s*Basic\s+//
ABresult
0X0
100
111
587 5 my $val = MIME::Base64::decode($old);
588 5 ▶ return $val unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 2 3 5
589 3 return split( /:/, $val, 2 );
590 }
591 3 return;
592 }
593  
594 8 1;
595 __END__
↑ Summary