Headers.pm

filter: Statementstmt 99.5% 240 / 241 Branchbran 91.1% 124 / 136 Conditioncond 90.4% 66 / 73 MC/DCmcdc 54.7% 23 / 42 Subroutinesub 100% 47 / 47 Podpod 94.7% 36 / 38 totaltotal 92.8% 536 / 577 CC 100 SCAR 21.7
CC
100
Cov
96%
_header
24.0
as_string
14.6
content_type_charset
9.2
CRAP 8.7
« Response.pm ↑ Summary Headers/Util.pm »
1 package HTTP::Headers;
2  
3 19 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 19 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 19 our $VERSION = '7.01';
7  
8 19 ▶use Clone qw(clone);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 19 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 # The $TRANSLATE_UNDERSCORE variable controls whether '_' can be used
12 # as a replacement for '-' in header field names.
13 19 ▶our $TRANSLATE_UNDERSCORE = 1 unless defined $TRANSLATE_UNDERSCORE;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $HTTP::Headers::TRANSLATE_UNDERSCORE 19 0 19
14  
15 # "Good Practice" order of HTTP message headers:
16 # - General-Headers
17 # - Request-Headers
18 # - Response-Headers
19 # - Entity-Headers
20  
21 19 my @general_headers = qw(
22 Cache-Control Connection Date Pragma Trailer Transfer-Encoding Upgrade
23 Via Warning
24 );
25  
26 19 my @request_headers = qw(
27 Accept Accept-Charset Accept-Encoding Accept-Language
28 Authorization Expect From Host
29 If-Match If-Modified-Since If-None-Match If-Range If-Unmodified-Since
30 Max-Forwards Proxy-Authorization Range Referer TE User-Agent
31 );
32  
33 19 my @response_headers = qw(
34 Accept-Ranges Age ETag Location Proxy-Authenticate Retry-After Server
35 Vary WWW-Authenticate
36 );
37  
38 19 my @entity_headers = qw(
39 Allow Content-Encoding Content-Language Content-Length Content-Location
40 Content-MD5 Content-Range Content-Type Expires Last-Modified
41 );
42  
43 19 my %entity_header = map { lc($_) => 1 } @entity_headers;
44  
45 19 my @header_order = (
46 @general_headers,
47 @request_headers,
48 @response_headers,
49 @entity_headers,
50 );
51  
52 # Make alternative representations of @header_order. This is used
53 # for sorting and case matching.
54 19 my %header_order;
55 my %standard_case;
56  
57 {
58 19 my $i = 0;
59 19 for (@header_order) {
60 893 my $lc = lc $_;
61 893 $header_order{$lc} = ++$i;
62 893 $standard_case{$lc} = $_;
63 }
64 }
65  
66  
67  
68 sub new
69 {
70 199 ▶ my($class) = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
71 199 my $self = bless {}, $class;
72 199 ▶ $self->header(@_) if @_; # set up initial headers
Branch: 1 branch
BranchTrueFalseTotal
if @_ 67 132 199
73 199 $self;
74 }
75  
76  
77 sub header
78 {
79 512 ▶ my $self = shift;
Subroutine header: called CC=7 SCAR=19.5
Pod documented
80 512 ▶ Carp::croak('Usage: $h->header($field, ...)') unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 1 511 512
81 511 my(@old);
82 my %seen;
83 511 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 570 509 1079
84 570 my $field = shift;
85 570 ▶ my $op = @_ ? ($seen{lc($field)}++ ? 'PUSH' : 'SET') : 'GET';
Branch: 2 branches
BranchTrueFalseTotal
@_ ? : 235 335 570
$seen{lc $field}++ ? : 4 231 235
86 570 @old = $self->_header($field, shift, $op);
87 }
88 509 ▶ return @old if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 60 449 509
89 449 ▶ return $old[0] if @old <= 1;
Branch: 1 branch
BranchTrueFalseTotal
if @old <= 1 443 6 449
90 6 join(", ", @old);
91 }
92  
93 sub clear
94 {
95 9 ▶ my $self = shift;
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
96 9 %$self = ();
97 }
98  
99  
100 sub push_header
101 {
102 42 ▶ my $self = shift;
Subroutine push_header: called CC=3 SCAR=11.0
Pod documented
103 42 ▶ return $self->_header(@_, 'PUSH_H') if @_ == 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 2 41 1 42
104 1 ▶ while (@_) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 1 3
105 2 $self->_header(splice(@_, 0, 2), 'PUSH_H');
106 }
107 }
108  
109  
110 sub init_header
111 {
112 5 ▶ Carp::croak('Usage: $h->init_header($field, $val)') if @_ != 3;
Subroutine init_header: called CC=2 SCAR=6.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 1 4 5
113 4 shift->_header(@_, 'INIT');
114 }
115  
116  
117 sub remove_header
118 {
119 48 ▶ my($self, @fields) = @_;
Subroutine remove_header: called CC=6 SCAR=17.9
Pod documented
120 48 my $field;
121 my @values;
122 48 foreach $field (@fields) {
123 84 ▶ $field =~ tr/_/-/ if $field !~ /^:/ && $TRANSLATE_UNDERSCORE;
Branch: 1 branch
BranchTrueFalseTotal
if not $field =~ /^:/ and $HTTP::Headers::TRANSLATE_UNDERSCORE 82 2 84
Condition100%3/3
not $field =~ /^:/ and $HTTP::Headers::TRANSLATE_UNDERSCORE
!ll&&!rl&&r
1182
MC/DC100%2/2
not $field =~ /^:/ and $HTTP::Headers::TRANSLATE_UNDERSCORE
not $field =~ /^:/ $HTTP::Headers::TRANSLATE_UNDERSCORE
Truth table100%3/3
not(A) and B
A= not $field =~ /^:/B= $HTTP::Headers::TRANSLATE_UNDERSCORE
ABresult
0X0
100
111
124 84 my $v = delete $self->{lc $field};
125 84 ▶ push(@values, ref($v) eq 'ARRAY' ? @$v : $v) if defined $v;
Branch: 2 branches
BranchTrueFalseTotal
if defined $v 42 42 84
ref $v eq 'ARRAY' ? : 8 34 42
126 }
127 48 return @values;
128 }
129  
130 sub remove_content_headers
131 {
132 9 ▶ my $self = shift;
Subroutine remove_content_headers: called CC=6 SCAR=17.9
Pod documented
133 9 ▶ unless (defined(wantarray)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined wantarray) 5 4 9
134 # fast branch that does not create return object
135 5 ▶ delete @$self{grep $entity_header{$_} || /^content-/, keys %$self};
Condition100%3/3
$entity_header{$_} || /^content-/
l!l&&r!l&&!r
716
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
136 5 return;
137 }
138  
139 4 my $c = ref($self)->new;
140 4 ▶ for my $f (grep $entity_header{$_} || /^content-/, keys %$self) {
Condition100%3/3
$entity_header{$_} || /^content-/
l!l&&r!l&&!r
929
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
141 11 $c->{$f} = delete $self->{$f};
142 }
143 4 ▶ if (exists $self->{'::std_case'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"::std_case"}) 2 2 4
144 2 $c->{'::std_case'} = $self->{'::std_case'};
145 }
146 4 $c;
147 }
148  
149  
150 sub _header
151 {
152 869 ▶ my($self, $field, $val, $op) = @_;
Subroutine _header: called CC=24 SCAR=31.8
153  
154 869 ▶ Carp::croak("Illegal field name '$field'")
Branch: 1 branch
BranchTrueFalseTotal
if rindex($field, ":") > 1 or not length $field 2 867 869
Condition100%3/3
rindex($field, ":") > 1 or not length $field
l!l&&r!l&&!r
11867
MC/DC100%2/2
rindex($field, ":") > 1 or not length $field
rindex($field, ":") > 1 not length $field
Truth table100%3/3
A or not(B)
A= rindex($field, ":") > 1B= not length $field
ABresult
1X1
011
000
155 if rindex($field, ':') > 1 || !length($field);
156  
157 867 ▶ unless ($field =~ /^:/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($field =~ /^:/) 855 12 867
158 855 ▶ $field =~ tr/_/-/ if $TRANSLATE_UNDERSCORE;
Branch: 1 branch
BranchTrueFalseTotal
if $HTTP::Headers::TRANSLATE_UNDERSCORE 820 35 855
159 855 my $old = $field;
160 855 $field = lc $field;
161 855 ▶ unless($standard_case{$field} || $self->{'::std_case'}{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($standard_case{$field} or $self->{"::std_case"}{$field}) 84 771 855
Condition100%3/3
$standard_case{$field} or $self->{"::std_case"}{$field}
l!l&&r!l&&!r
66310884
MC/DC100%2/2
$standard_case{$field} or $self->{"::std_case"}{$field}
$standard_case{$field} $self->{"::std_case"}{$field}
Truth table100%3/3
A or B
A= $standard_case{$field}B= $self->{"::std_case"}{$field}
ABresult
1X1
011
000
162 # generate a %std_case entry for this field
163 84 $old =~ s/\b(\w)/\u$1/g;
164 84 $self->{'::std_case'}{$field} = $old;
165 }
166 }
167  
168 867 ▶ $op ||= defined($val) ? 'SET' : 'GET';
Branch: 1 branch
BranchTrueFalseTotal
defined $val ? : 45 205 250
Condition66%2/3
$op ||= defined $val ? "SET" : "GET"
l!l&&r!l&&!r
6172500
MC/DC0%0/2
$op ||= defined $val ? "SET" : "GET"
$op defined $val ? "SET" : "GET"
Truth table66%2/3
A ||= B
A= $opB= defined $val ? "SET" : "GET"
ABresult
1X1
011
000
169 867 ▶ if ($op eq 'PUSH_H') {
Branch: 1 branch
BranchTrueFalseTotal
if ($op eq "PUSH_H") 43 824 867
170 # Like PUSH but where we don't care about the return value
171 43 ▶ if (exists $self->{$field}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{$field}) 11 32 43
172 11 my $h = $self->{$field};
173 11 ▶ if (ref($h) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $h eq 'ARRAY') { } 2 9 11
174 2 ▶ push(@$h, ref($val) eq "ARRAY" ? @$val : $val);
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 1 1 2
175 }
176 else {
177 9 ▶ $self->{$field} = [$h, ref($val) eq "ARRAY" ? @$val : $val]
Branch: 1 branch
BranchTrueFalseTotal
ref $val eq 'ARRAY' ? : 1 8 9
178 }
179 11 return;
180 }
181 32 $self->{$field} = $val;
182 32 return;
183 }
184  
185 824 my $h = $self->{$field};
186 824 ▶ my @old = ref($h) eq 'ARRAY' ? @$h : (defined($h) ? ($h) : ());
Branch: 2 branches
BranchTrueFalseTotal
ref $h eq 'ARRAY' ? : 24 800 824
defined $h ? : 337 463 800
187  
188 824 ▶ unless ($op eq 'GET' || ($op eq 'INIT' && @old)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($op eq "GET" or $op eq "INIT" and @old) 281 543 824
Condition100%6/6
$op eq "GET" or $op eq "INIT" and @old
l!l&&r!l&&!r
54117266
$op eq "INIT" and @old
!ll&&!rl&&r
27832
MC/DC66%2/3
$op eq "GET" or $op eq "INIT" and @old
$op eq "GET" $op eq "INIT" @old
Truth table75%3/4
A or B and C
A= $op eq "GET"B= $op eq "INIT"C= @old
ABCresult
1XX1
0111
00X0
0100
189 281 ▶ if (defined($val)) {
Branch: 2 branches
BranchTrueFalseTotal
if (defined $val) { } 280 1 281
elsif ($op ne 'PUSH') { } 1 0 1
190 280 ▶ my @new = ($op eq 'PUSH') ? @old : ();
Branch: 1 branch
BranchTrueFalseTotal
$op eq 'PUSH' ? : 4 276 280
191 280 ▶ if (ref($val) ne 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val ne 'ARRAY') { } 272 8 280
192 272 push(@new, $val);
193 }
194 else {
195 8 push(@new, @$val);
196 }
197 280 ▶ $self->{$field} = @new > 1 ? \@new : $new[0];
Branch: 1 branch
BranchTrueFalseTotal
@new > 1 ? : 12 268 280
198 }
199 elsif ($op ne 'PUSH') {
200 1 delete $self->{$field};
201 }
202 }
203 824 @old;
204 }
205  
206  
207 sub _sorted_field_names
208 {
209 157 ▶ my $self = shift;
Subroutine _sorted_field_names: called CC=4 SCAR=13.9
210 return [ sort {
211 157 ▶ ($header_order{$a} || 999) <=> ($header_order{$b} || 999) ||
Branch: 1 branch
BranchTrueFalseTotal
($header_order{$a} || 999) <=> ($header_order{$b} || 999) or $a cmp $b 55 207 262
Condition100%4/4
$header_order{$a} || 999
l!l
17587
$header_order{$b} || 999
l!l
16597
MC/DC100%1/1
$header_order{$a} || 999
$header_order{$a}
MC/DC100%1/1
$header_order{$b} || 999
$header_order{$b}
Truth table100%2/2
A
A= $header_order{$a}
Aresult
11
00
Truth table100%2/2
A
A= $header_order{$b}
Aresult
11
00
212 $a cmp $b
213 } grep !/^::/, keys %$self ];
214 }
215  
216  
217 sub header_field_names {
218 15 ▶ my $self = shift;
Subroutine header_field_names: called CC=4 SCAR=13.9
Pod documented
219 15 ▶ return map $standard_case{$_} || $self->{'::std_case'}{$_} || $_, @{ $self->_sorted_field_names },
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 10 5 15
Condition83%5/6
$standard_case{$_} || $self->{'::std_case'}{$_} || $_
l!l&&r!l&&!r
1860
$standard_case{$_} or $self->{"::std_case"}{$_}
l!l&&r!l&&!r
1536
MC/DC0%0/3
$standard_case{$_} || $self->{'::std_case'}{$_} || $_
$standard_case{$_} $self->{"::std_case"}{$_} $_
Truth table75%3/4
A or B || C
A= $standard_case{$_}B= $self->{"::std_case"}{$_}C= $_
ABCresult
1XX1
01X1
0011
0000
220 if wantarray;
221 5 return grep !/^::/, keys %$self;
222 }
223  
224  
225 sub scan
226 {
227 5 ▶ my($self, $sub) = @_;
Subroutine scan: called CC=8 SCAR=20.8
Pod documented
228 5 my $key;
229 5 for $key (@{ $self->_sorted_field_names }) {
230 14 my $vals = $self->{$key};
231 14 ▶ if (ref($vals) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $vals eq 'ARRAY') { } 4 10 14
232 4 my $val;
233 4 for $val (@$vals) {
234 10 ▶ $sub->($standard_case{$key} || $self->{'::std_case'}{$key} || $key, $val);
Condition83%5/6
$standard_case{$key} || $self->{'::std_case'}{$key} || $key
l!l&&r!l&&!r
730
$standard_case{$key} or $self->{"::std_case"}{$key}
l!l&&r!l&&!r
343
MC/DC0%0/3
$standard_case{$key} || $self->{'::std_case'}{$key} || $key
$standard_case{$key} $self->{"::std_case"}{$key} $key
Truth table75%3/4
A or B || C
A= $standard_case{$key}B= $self->{"::std_case"}{$key}C= $key
ABCresult
1XX1
01X1
0011
0000
235 }
236 }
237 else {
238 10 ▶ $sub->($standard_case{$key} || $self->{'::std_case'}{$key} || $key, $vals);
Condition83%5/6
$standard_case{$key} || $self->{'::std_case'}{$key} || $key
l!l&&r!l&&!r
910
$standard_case{$key} or $self->{"::std_case"}{$key}
l!l&&r!l&&!r
811
MC/DC0%0/3
$standard_case{$key} || $self->{'::std_case'}{$key} || $key
$standard_case{$key} $self->{"::std_case"}{$key} $key
Truth table75%3/4
A or B || C
A= $standard_case{$key}B= $self->{"::std_case"}{$key}C= $key
ABCresult
1XX1
01X1
0011
0000
239 }
240 }
241 }
242  
243 sub flatten {
244 2 ▶ my($self)=@_;
Subroutine flatten: called CC=1 SCAR=0.0
Pod documented
245  
246 (
247 map {
248 2 my $k = $_;
249 map {
250 6 ( $k => $_ )
251 } $self->header($_);
252 } $self->header_field_names
253 );
254 }
255  
256 sub as_string
257 {
258 142 ▶ my($self, $endl) = @_;
Subroutine as_string: called CC=14 SCAR=26.8
Pod documented
259 142 ▶ $endl = "\n" unless defined $endl;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $endl 47 95 142
260  
261 142 my @result = ();
262 142 for my $key (@{ $self->_sorted_field_names }) {
263 219 ▶ next if index($key, '_') == 0;
Branch: 1 branch
BranchTrueFalseTotal
if index($key, "_") == 0 0 219 219
264 219 my $vals = $self->{$key};
265 219 ▶ if ( ref($vals) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $vals eq 'ARRAY') { } 14 205 219
266 14 for my $val (@$vals) {
267 35 ▶ $val = '' if not defined $val;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $val 0 35 35
268 35 ▶ my $field = $standard_case{$key} || $self->{'::std_case'}{$key} || $key;
Condition83%5/6
$standard_case{$key} || $self->{'::std_case'}{$key} || $key
l!l&&r!l&&!r
3230
$standard_case{$key} or $self->{"::std_case"}{$key}
l!l&&r!l&&!r
11213
MC/DC0%0/3
$standard_case{$key} || $self->{'::std_case'}{$key} || $key
$standard_case{$key} $self->{"::std_case"}{$key} $key
Truth table75%3/4
A or B || C
A= $standard_case{$key}B= $self->{"::std_case"}{$key}C= $key
ABCresult
1XX1
01X1
0011
0000
269 35 $field =~ s/^://;
270 35 ▶ if ( index($val, "\n") >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($val, "\n") != -1) 0 35 35
271 0 $val = _process_newline($val, $endl);
272 }
273 35 push @result, $field . ': ' . $val;
274 }
275 }
276 else {
277 205 ▶ $vals = '' if not defined $vals;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $vals 0 205 205
278 205 ▶ my $field = $standard_case{$key} || $self->{'::std_case'}{$key} || $key;
Condition83%5/6
$standard_case{$key} || $self->{'::std_case'}{$key} || $key
l!l&&r!l&&!r
20230
$standard_case{$key} or $self->{"::std_case"}{$key}
l!l&&r!l&&!r
115873
MC/DC0%0/3
$standard_case{$key} || $self->{'::std_case'}{$key} || $key
$standard_case{$key} $self->{"::std_case"}{$key} $key
Truth table75%3/4
A or B || C
A= $standard_case{$key}B= $self->{"::std_case"}{$key}C= $key
ABCresult
1XX1
01X1
0011
0000
279 205 $field =~ s/^://;
280 205 ▶ if ( index($vals, "\n") >= 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($vals, "\n") != -1) 13 192 205
281 13 $vals = _process_newline($vals, $endl);
282 }
283 205 push @result, $field . ': ' . $vals;
284 }
285 }
286  
287 142 join($endl, @result, '');
288 }
289  
290 sub _process_newline {
291 13 ▶ local $_ = shift;
Subroutine _process_newline: called CC=1 SCAR=0.0
292 13 my $endl = shift;
293 # must handle header values with embedded newlines with care
294 13 s/\s+$//; # trailing newlines and space must go
295 13 s/\n(\x0d?\n)+/\n/g; # no empty lines
296 13 s/\n([^\040\t])/\n $1/g; # initial space for continuation
297 13 s/\n/$endl/g; # substitute with requested line ending
298 13 $_;
299 }
300  
301  
302 sub _date_header
303 {
304 134 ▶ require HTTP::Date;
Subroutine _date_header: called CC=3 SCAR=11.0
305 134 my($self, $header, $time) = @_;
306 134 my($old) = $self->_header($header);
307 134 ▶ if (defined $time) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $time) 17 117 134
308 17 $self->_header($header, HTTP::Date::time2str($time));
309 }
310 134 ▶ $old =~ s/;.*// if defined($old);
Branch: 1 branch
BranchTrueFalseTotal
if defined $old 57 77 134
311 134 HTTP::Date::str2time($old);
312 }
313  
314  
315 50 ▶sub date { shift->_date_header('Date', @_); }
Subroutine date: called CC=1 SCAR=0.0
Pod documented
316 25 ▶sub expires { shift->_date_header('Expires', @_); }
Subroutine expires: called CC=1 SCAR=0.0
Pod documented
317 5 ▶sub if_modified_since { shift->_date_header('If-Modified-Since', @_); }
Subroutine if_modified_since: called CC=1 SCAR=0.0
Pod documented
318 3 ▶sub if_unmodified_since { shift->_date_header('If-Unmodified-Since', @_); }
Subroutine if_unmodified_since: called CC=1 SCAR=0.0
Pod documented
319 22 ▶sub last_modified { shift->_date_header('Last-Modified', @_); }
Subroutine last_modified: called CC=1 SCAR=0.0
Pod documented
320  
321 # This is used as a private LWP extension. The Client-Date header is
322 # added as a timestamp to a response when it has been received.
323 28 ▶sub client_date { shift->_date_header('Client-Date', @_); }
Subroutine client_date: called CC=1 SCAR=0.0
Pod undocumented
324  
325 # The retry_after field is dual format (can also be a expressed as
326 # number of seconds from now), so we don't provide an easy way to
327 # access it until we have know how both these interfaces can be
328 # addressed. One possibility is to return a negative value for
329 # relative seconds and a positive value for epoch based time values.
330 #sub retry_after { shift->_date_header('Retry-After', @_); }
331  
332 sub content_type {
333 422 ▶ my $self = shift;
Subroutine content_type: called CC=7 SCAR=19.5
Pod documented
334 422 my $ct = $self->{'content-type'};
335 422 ▶ $self->{'content-type'} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 37 385 422
336 422 ▶ $ct = $ct->[0] if ref($ct) eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $ct eq "ARRAY" 0 422 422
337 422 ▶ return '' unless defined($ct) && length($ct);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ct and length $ct 41 381 422
Condition100%3/3
defined $ct and length $ct
!ll&&!rl&&r
401381
MC/DC100%2/2
defined $ct and length $ct
defined $ct length $ct
Truth table100%3/3
A and B
A= defined $ctB= length $ct
ABresult
0X0
100
111
338 381 my @ct = split(/;\s*/, $ct, 2);
339 381 for ($ct[0]) {
340 381 s/\s+//g;
341 381 $_ = lc($_);
342 }
343 381 ▶ wantarray ? @ct : $ct[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 379 381
344 }
345  
346 sub content_type_charset {
347 92 ▶ my $self = shift;
Subroutine content_type_charset: called CC=9 SCAR=22.2
Pod documented
348 92 require HTTP::Headers::Util;
349 92 my $h = $self->{'content-type'};
350 92 ▶ $h = $h->[0] if ref($h);
Branch: 1 branch
BranchTrueFalseTotal
if ref $h 0 92 92
351 92 ▶ $h = "" unless defined $h;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $h 2 90 92
352 92 my @v = HTTP::Headers::Util::split_header_words($h);
353 92 ▶ if (@v) {
Branch: 1 branch
BranchTrueFalseTotal
if (@v) 90 2 92
354 90 my($ct, undef, %ct_param) = @{$v[0]};
355 90 my $charset = $ct_param{charset};
356 90 ▶ if ($ct) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ct) 90 0 90
357 90 $ct = lc($ct);
358 90 $ct =~ s/\s+//;
359 }
360 90 ▶ if ($charset) {
Branch: 1 branch
BranchTrueFalseTotal
if ($charset) 16 74 90
361 16 $charset = uc($charset);
362 16 $charset =~ s/^\s+//; $charset =~ s/\s+\z//;
363 16 ▶ undef($charset) if $charset eq "";
Branch: 1 branch
BranchTrueFalseTotal
if $charset eq "" 0 16 16
364 }
365 90 ▶ return $ct, $charset if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 90 90
366 90 return $charset;
367 }
368 2 ▶ return undef, undef if wantarray;
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 2 2
369 2 return undef;
370 }
371  
372 sub content_is_text {
373 107 ▶ my $self = shift;
Subroutine content_is_text: called CC=1 SCAR=0.0
Pod documented
374 107 return $self->content_type =~ m,^text/,;
375 }
376  
377 sub content_is_html {
378 35 ▶ my $self = shift;
Subroutine content_is_html: called CC=2 SCAR=6.9
Pod documented
379 35 ▶ 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
7325
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
380 }
381  
382 sub content_is_xhtml {
383 36 ▶ my $ct = shift->content_type;
Subroutine content_is_xhtml: called CC=2 SCAR=6.9
Pod documented
384 36 ▶ return $ct eq "application/xhtml+xml" ||
Condition100%3/3
$ct eq 'application/xhtml+xml' || $ct eq 'application/vnd.wap.xhtml+xml'
l!l&&r!l&&!r
4230
MC/DC100%2/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 table100%3/3
A || B
A= $ct eq 'application/xhtml+xml'B= $ct eq 'application/vnd.wap.xhtml+xml'
ABresult
1X1
011
000
385 $ct eq "application/vnd.wap.xhtml+xml";
386 }
387  
388 sub content_is_xml {
389 84 ▶ my $ct = shift->content_type;
Subroutine content_is_xml: called CC=4 SCAR=13.9
Pod documented
390 84 ▶ return 1 if $ct eq "text/xml";
Branch: 1 branch
BranchTrueFalseTotal
if $ct eq "text/xml" 1 83 84
391 83 ▶ return 1 if $ct eq "application/xml";
Branch: 1 branch
BranchTrueFalseTotal
if $ct eq "application/xml" 44 39 83
392 39 ▶ return 1 if $ct =~ /\+xml$/;
Branch: 1 branch
BranchTrueFalseTotal
if $ct =~ /\+xml$/ 2 37 39
393 37 return 0;
394 }
395  
396 sub referer {
397 9 ▶ my $self = shift;
Subroutine referer: called CC=4 SCAR=13.9
Pod documented
398 9 ▶ if (@_ && $_[0] =~ /#/) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ and $_[0] =~ /#/) 2 7 9
Condition100%3/3
@_ and $_[0] =~ /#/
!ll&&!rl&&r
612
MC/DC100%2/2
@_ and $_[0] =~ /#/
@_ $_[0] =~ /#/
Truth table100%3/3
A and B
A= @_B= $_[0] =~ /#/
ABresult
0X0
100
111
399 # Strip fragment per RFC 2616, section 14.36.
400 2 my $uri = shift;
401 2 ▶ if (ref($uri)) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $uri) { } 1 1 2
402 1 $uri = $uri->clone;
403 1 $uri->fragment(undef);
404 }
405 else {
406 1 $uri =~ s/\#.*//;
407 }
408 2 unshift @_, $uri;
409 }
410 9 ($self->_header('Referer', @_))[0];
411 }
412 19 *referrer = \&referer; # on tchrist's request
413  
414 3 ▶sub title { (shift->_header('Title', @_))[0] }
Subroutine title: called CC=1 SCAR=0.0
Pod documented
415 3 ▶sub content_encoding { (shift->_header('Content-Encoding', @_))[0] }
Subroutine content_encoding: called CC=1 SCAR=0.0
Pod documented
416 3 ▶sub content_language { (shift->_header('Content-Language', @_))[0] }
Subroutine content_language: called CC=1 SCAR=0.0
Pod documented
417 12 ▶sub content_length { (shift->_header('Content-Length', @_))[0] }
Subroutine content_length: called CC=1 SCAR=0.0
Pod documented
418  
419 4 ▶sub user_agent { (shift->_header('User-Agent', @_))[0] }
Subroutine user_agent: called CC=1 SCAR=0.0
Pod documented
420 3 ▶sub server { (shift->_header('Server', @_))[0] }
Subroutine server: called CC=1 SCAR=0.0
Pod documented
421  
422 1 ▶sub from { (shift->_header('From', @_))[0] }
Subroutine from: called CC=1 SCAR=0.0
Pod documented
423 3 ▶sub warning { (shift->_header('Warning', @_))[0] }
Subroutine warning: called CC=1 SCAR=0.0
Pod undocumented
424  
425 3 ▶sub www_authenticate { (shift->_header('WWW-Authenticate', @_))[0] }
Subroutine www_authenticate: called CC=1 SCAR=0.0
Pod documented
426 1 ▶sub authorization { (shift->_header('Authorization', @_))[0] }
Subroutine authorization: called CC=1 SCAR=0.0
Pod documented
427  
428 3 ▶sub proxy_authenticate { (shift->_header('Proxy-Authenticate', @_))[0] }
Subroutine proxy_authenticate: called CC=1 SCAR=0.0
Pod documented
429 1 ▶sub proxy_authorization { (shift->_header('Proxy-Authorization', @_))[0] }
Subroutine proxy_authorization: called CC=1 SCAR=0.0
Pod documented
430  
431 9 ▶sub authorization_basic { shift->_basic_auth("Authorization", @_) }
Subroutine authorization_basic: called CC=1 SCAR=0.0
Pod documented
432 2 ▶sub proxy_authorization_basic { shift->_basic_auth("Proxy-Authorization", @_) }
Subroutine proxy_authorization_basic: called CC=1 SCAR=0.0
Pod documented
433  
434 sub _basic_auth {
435 11 ▶ require MIME::Base64;
Subroutine _basic_auth: called CC=7 SCAR=19.5
436 11 my($self, $h, $user, $passwd) = @_;
437 11 my($old) = $self->_header($h);
438 11 ▶ if (defined $user) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $user) 5 6 11
439 5 ▶ Carp::croak("Basic authorization user name can't contain ':'")
Branch: 1 branch
BranchTrueFalseTotal
if $user =~ /:/ 1 4 5
440 if $user =~ /:/;
441 4 ▶ $passwd = '' unless defined $passwd;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $passwd 1 3 4
442 4 $self->_header($h => 'Basic ' .
443 MIME::Base64::encode("$user:$passwd", ''));
444 }
445 10 ▶ if (defined $old && $old =~ s/^\s*Basic\s+//) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $old and $old =~ s/^\s*Basic\s+//) 6 4 10
Condition66%2/3
defined $old and $old =~ s/^\s*Basic\s+//
!ll&&!rl&&r
406
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
446 6 my $val = MIME::Base64::decode($old);
447 6 ▶ return $val unless wantarray;
Branch: 1 branch
BranchTrueFalseTotal
unless wantarray 2 4 6
448 4 return split(/:/, $val, 2);
449 }
450 4 return;
451 }
452  
453  
454 19 1;
455  
456 =pod
457  
458 =encoding UTF-8
459  
460 =head1 NAME
461  
462 HTTP::Headers - Class encapsulating HTTP Message headers
463  
464 =head1 VERSION
465  
466 version 7.01
467  
468 =head1 SYNOPSIS
469  
470 require HTTP::Headers;
471 $h = HTTP::Headers->new;
472  
473 $h->header('Content-Type' => 'text/plain'); # set
474 $ct = $h->header('Content-Type'); # get
475 $h->remove_header('Content-Type'); # delete
476  
477 =head1 DESCRIPTION
478  
479 The C<HTTP::Headers> class encapsulates HTTP-style message headers.
480 The headers consist of attribute-value pairs also called fields, which
481 may be repeated, and which are printed in a particular order. The
482 field names are cases insensitive.
483  
484 Instances of this class are usually created as member variables of the
485 C<HTTP::Request> and C<HTTP::Response> classes, internal to the
486 library.
487  
488 The following methods are available:
489  
490 =over 4
491  
492 =item $h = HTTP::Headers->new
493  
494 Constructs a new C<HTTP::Headers> object. You might pass some initial
495 attribute-value pairs as parameters to the constructor. I<E.g.>:
496  
497 $h = HTTP::Headers->new(
498 Date => 'Thu, 03 Feb 1994 00:00:00 GMT',
499 Content_Type => 'text/html; version=3.2',
500 Content_Base => 'http://www.perl.org/');
501  
502 The constructor arguments are passed to the C<header> method which is
503 described below.
504  
505 =item $h->clone
506  
507 Returns a copy of this C<HTTP::Headers> object.
508  
509 =item $h->header( $field )
510  
511 =item $h->header( $field => $value )
512  
513 =item $h->header( $f1 => $v1, $f2 => $v2, ... )
514  
515 Get or set the value of one or more header fields. The header field
516 name ($field) is not case sensitive. To make the life easier for perl
517 users who wants to avoid quoting before the => operator, you can use
518 '_' as a replacement for '-' in header names.
519  
520 The header() method accepts multiple ($field => $value) pairs, which
521 means that you can update several fields with a single invocation.
522  
523 The $value argument may be a plain string or a reference to an array
524 of strings for a multi-valued field. If the $value is provided as
525 C<undef> then the field is removed. If the $value is not given, then
526 that header field will remain unchanged. In addition to being a string,
527 $value may be something that stringifies.
528  
529 The old value (or values) of the last of the header fields is returned.
530 If no such field exists C<undef> will be returned.
531  
532 A multi-valued field will be returned as separate values in list
533 context and will be concatenated with ", " as separator in scalar
534 context. The HTTP spec (RFC 2616) promises that joining multiple
535 values in this way will not change the semantic of a header field, but
536 in practice there are cases like old-style Netscape cookies (see
537 L<HTTP::Cookies>) where "," is used as part of the syntax of a single
538 field value.
539  
540 Examples:
541  
542 $header->header(MIME_Version => '1.0',
543 User_Agent => 'My-Web-Client/0.01');
544 $header->header(Accept => "text/html, text/plain, image/*");
545 $header->header(Accept => [qw(text/html text/plain image/*)]);
546 @accepts = $header->header('Accept'); # get multiple values
547 $accepts = $header->header('Accept'); # get values as a single string
548  
549 =item $h->push_header( $field => $value )
550  
551 =item $h->push_header( $f1 => $v1, $f2 => $v2, ... )
552  
553 Add a new field value for the specified header field. Previous values
554 for the same field are retained.
555  
556 As for the header() method, the field name ($field) is not case
557 sensitive and '_' can be used as a replacement for '-'.
558  
559 The $value argument may be a scalar or a reference to a list of
560 scalars.
561  
562 $header->push_header(Accept => 'image/jpeg');
563 $header->push_header(Accept => [map "image/$_", qw(gif png tiff)]);
564  
565 =item $h->init_header( $field => $value )
566  
567 Set the specified header to the given value, but only if no previous
568 value for that field is set.
569  
570 The header field name ($field) is not case sensitive and '_'
571 can be used as a replacement for '-'.
572  
573 The $value argument may be a scalar or a reference to a list of
574 scalars.
575  
576 =item $h->remove_header( $field, ... )
577  
578 This function removes the header fields with the specified names.
579  
580 The header field names ($field) are not case sensitive and '_'
581 can be used as a replacement for '-'.
582  
583 The return value is the values of the fields removed. In scalar
584 context the number of fields removed is returned.
585  
586 Note that if you pass in multiple field names then it is generally not
587 possible to tell which of the returned values belonged to which field.
588  
589 =item $h->remove_content_headers
590  
591 This will remove all the header fields used to describe the content of
592 a message. All header field names prefixed with C<Content-> fall
593 into this category, as well as C<Allow>, C<Expires> and
594 C<Last-Modified>. RFC 2616 denotes these fields as I<Entity Header
595 Fields>.
596  
597 The return value is a new C<HTTP::Headers> object that contains the
598 removed headers only.
599  
600 =item $h->clear
601  
602 This will remove all header fields.
603  
604 =item $h->header_field_names
605  
606 Returns the list of distinct names for the fields present in the
607 header. The field names have case as suggested by HTTP spec, and the
608 names are returned in the recommended "Good Practice" order.
609  
610 In scalar context return the number of distinct field names.
611  
612 =item $h->scan( \&process_header_field )
613  
614 Apply a subroutine to each header field in turn. The callback routine
615 is called with two parameters; the name of the field and a single
616 value (a string). If a header field is multi-valued, then the
617 routine is called once for each value. The field name passed to the
618 callback routine has case as suggested by HTTP spec, and the headers
619 will be visited in the recommended "Good Practice" order.
620  
621 Any return values of the callback routine are ignored. The loop can
622 be broken by raising an exception (C<die>), but the caller of scan()
623 would have to trap the exception itself.
624  
625 =item $h->flatten()
626  
627 Returns the list of pairs of keys and values.
628  
629 =item $h->as_string
630  
631 =item $h->as_string( $eol )
632  
633 Return the header fields as a formatted MIME header. Since it
634 internally uses the C<scan> method to build the string, the result
635 will use case as suggested by HTTP spec, and it will follow
636 recommended "Good Practice" of ordering the header fields. Long header
637 values are not folded.
638  
639 The optional $eol parameter specifies the line ending sequence to
640 use. The default is "\n". Embedded "\n" characters in header field
641 values will be substituted with this line ending sequence.
642  
643 =back
644  
645 =head1 CONVENIENCE METHODS
646  
647 The most frequently used headers can also be accessed through the
648 following convenience methods. Most of these methods can both be used to read
649 and to set the value of a header. The header value is set if you pass
650 an argument to the method. The old header value is always returned.
651 If the given header did not exist then C<undef> is returned.
652  
653 Methods that deal with dates/times always convert their value to system
654 time (seconds since Jan 1, 1970) and they also expect this kind of
655 value when the header value is set.
656  
657 =over 4
658  
659 =item $h->date
660  
661 This header represents the date and time at which the message was
662 originated. I<E.g.>:
663  
664 $h->date(time); # set current date
665  
666 =item $h->expires
667  
668 This header gives the date and time after which the entity should be
669 considered stale.
670  
671 =item $h->if_modified_since
672  
673 =item $h->if_unmodified_since
674  
675 These header fields are used to make a request conditional. If the requested
676 resource has (or has not) been modified since the time specified in this field,
677 then the server will return a C<304 Not Modified> response instead of
678 the document itself.
679  
680 =item $h->last_modified
681  
682 This header indicates the date and time at which the resource was last
683 modified. I<E.g.>:
684  
685 # check if document is more than 1 hour old
686 if (my $last_mod = $h->last_modified) {
687 if ($last_mod < time - 60*60) {
688 ...
689 }
690 }
691  
692 =item $h->content_type
693  
694 The Content-Type header field indicates the media type of the message
695 content. I<E.g.>:
696  
697 $h->content_type('text/html');
698  
699 The value returned will be converted to lower case, and potential
700 parameters will be chopped off and returned as a separate value if in
701 an array context. If there is no such header field, then the empty
702 string is returned. This makes it safe to do the following:
703  
704 if ($h->content_type eq 'text/html') {
705 # we enter this place even if the real header value happens to
706 # be 'TEXT/HTML; version=3.0'
707 ...
708 }
709  
710 =item $h->content_type_charset
711  
712 Returns the upper-cased charset specified in the Content-Type header. In list
713 context return the lower-cased bare content type followed by the upper-cased
714 charset. Both values will be C<undef> if not specified in the header.
715  
716 =item $h->content_is_text
717  
718 Returns TRUE if the Content-Type header field indicate that the
719 content is textual.
720  
721 =item $h->content_is_html
722  
723 Returns TRUE if the Content-Type header field indicate that the
724 content is some kind of HTML (including XHTML). This method can't be
725 used to set Content-Type.
726  
727 =item $h->content_is_xhtml
728  
729 Returns TRUE if the Content-Type header field indicate that the
730 content is XHTML. This method can't be used to set Content-Type.
731  
732 =item $h->content_is_xml
733  
734 Returns TRUE if the Content-Type header field indicate that the
735 content is XML. This method can't be used to set Content-Type.
736  
737 =item $h->content_encoding
738  
739 The Content-Encoding header field is used as a modifier to the
740 media type. When present, its value indicates what additional
741 encoding mechanism has been applied to the resource.
742  
743 =item $h->content_length
744  
745 A decimal number indicating the size in bytes of the message content.
746  
747 =item $h->content_language
748  
749 The natural language(s) of the intended audience for the message
750 content. The value is one or more language tags as defined by RFC
751 1766. Eg. "no" for some kind of Norwegian and "en-US" for English the
752 way it is written in the US.
753  
754 =item $h->title
755  
756 The title of the document. In libwww-perl this header will be
757 initialized automatically from the E<lt>TITLE>...E<lt>/TITLE> element
758 of HTML documents. I<This header is no longer part of the HTTP
759 standard.>
760  
761 =item $h->user_agent
762  
763 This header field is used in request messages and contains information
764 about the user agent originating the request. I<E.g.>:
765  
766 $h->user_agent('Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 6.0)');
767  
768 =item $h->server
769  
770 The server header field contains information about the software being
771 used by the originating server program handling the request.
772  
773 =item $h->from
774  
775 This header should contain an Internet e-mail address for the human
776 user who controls the requesting user agent. The address should be
777 machine-usable, as defined by RFC822. E.g.:
778  
779 $h->from('King Kong <king@kong.com>');
780  
781 I<This header is no longer part of the HTTP standard.>
782  
783 =item $h->referer
784  
785 Used to specify the address (URI) of the document from which the
786 requested resource address was obtained.
787  
788 The "Free On-line Dictionary of Computing" as this to say about the
789 word I<referer>:
790  
791 <World-Wide Web> A misspelling of "referrer" which
792 somehow made it into the {HTTP} standard. A given {web
793 page}'s referer (sic) is the {URL} of whatever web page
794 contains the link that the user followed to the current
795 page. Most browsers pass this information as part of a
796 request.
797  
798 (1998-10-19)
799  
800 By popular demand C<referrer> exists as an alias for this method so you
801 can avoid this misspelling in your programs and still send the right
802 thing on the wire.
803  
804 When setting the referrer, this method removes the fragment from the
805 given URI if it is present, as mandated by RFC2616. Note that
806 the removal does I<not> happen automatically if using the header(),
807 push_header() or init_header() methods to set the referrer.
808  
809 =item $h->www_authenticate
810  
811 This header must be included as part of a C<401 Unauthorized> response.
812 The field value consist of a challenge that indicates the
813 authentication scheme and parameters applicable to the requested URI.
814  
815 =item $h->proxy_authenticate
816  
817 This header must be included in a C<407 Proxy Authentication Required>
818 response.
819  
820 =item $h->authorization
821  
822 =item $h->proxy_authorization
823  
824 A user agent that wishes to authenticate itself with a server or a
825 proxy, may do so by including these headers.
826  
827 =item $h->authorization_basic
828  
829 This method is used to get or set an authorization header that use the
830 "Basic Authentication Scheme". In array context it will return two
831 values; the user name and the password. In scalar context it will
832 return I<"uname:password"> as a single string value.
833  
834 When used to set the header value, it expects two arguments. I<E.g.>:
835  
836 $h->authorization_basic($uname, $password);
837  
838 The method will croak if the $uname contains a colon ':'.
839  
840 =item $h->proxy_authorization_basic
841  
842 Same as authorization_basic() but will set the "Proxy-Authorization"
843 header instead.
844  
845 =back
846  
847 =head1 NON-CANONICALIZED FIELD NAMES
848  
849 The header field name spelling is normally canonicalized including the
850 '_' to '-' translation. There are some application where this is not
851 appropriate. Prefixing field names with ':' allow you to force a
852 specific spelling. For example if you really want a header field name
853 to show up as C<foo_bar> instead of "Foo-Bar", you might set it like
854 this:
855  
856 $h->header(":foo_bar" => 1);
857  
858 These field names are returned with the ':' intact for
859 $h->header_field_names and the $h->scan callback, but the colons do
860 not show in $h->as_string.
861  
862 =head1 AUTHOR
863  
864 Gisle Aas <gisle@activestate.com>
865  
866 =head1 COPYRIGHT AND LICENSE
867  
868 This software is copyright (c) 1994 by Gisle Aas.
869  
870 This is free software; you can redistribute it and/or modify it under
871 the same terms as the Perl 5 programming language system itself.
872  
873 =cut
874  
875 __END__
« Response.pm ↑ Summary Headers/Util.pm »