GX/HTTP/Headers.pm

filter: Statementstmt 91.6% 109 / 119 Branchbran 69.4% 25 / 36 Conditioncond 54.5% 12 / 22 MC/DCmcdc 7.6% 1 / 13 Subroutinesub 90.4% 19 / 21 Podpod 90.0% 9 / 10 totaltotal 79.1% 175 / 221 CC 33 SCAR 15.6
CC
33
Cov
82%
as_hash
12.0
parse
9.4
sorted_field_names
6.0
CRAP 4.8
« GX/Meta/Attribute/Array.pm ↑ Summary GX/HTTP/Parser/Body.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/HTTP/Headers.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::HTTP::Headers;
7  
8 41 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 41 ▶use GX::HTTP::Constants qw( CRLF CRLFSP REGEX_CRLF );
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 41 ▶use Storable ();
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13  
14 # ----------------------------------------------------------------------------------------------------------------------
15 # Header fields (see RFC 2616)
16 # ----------------------------------------------------------------------------------------------------------------------
17  
18 41 ▶use constant GENERAL_HEADERS => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
19 Cache-Control
20 Connection
21 Date
22 Pragma
23 Trailer
24 Transfer-Encoding
25 Upgrade
26 Via
27 Warning
28 41 );
29  
30 41 ▶use constant REQUEST_HEADERS => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
31 Accept
32 Accept-Charset
33 Accept-Encoding
34 Accept-Language
35 Authorization
36 Expect
37 From
38 Host
39 If-Match
40 If-Modified-Since
41 If-None-Match
42 If-Range
43 If-Unmodified-Since
44 Max-Forwards
45 Proxy-Authorization
46 Range
47 Referer
48 TE
49 User-Agent
50 41 );
51  
52 41 ▶use constant RESPONSE_HEADERS => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
53 Accept-Ranges
54 Age
55 ETag
56 Location
57 Proxy-Authenticate
58 Retry-After
59 Server
60 Vary
61 WWW-Authenticate
62 41 );
63  
64 41 ▶use constant ENTITY_HEADERS => qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
65 Allow
66 Content-Encoding
67 Content-Language
68 Content-Length
69 Content-Location
70 Content-MD5
71 Content-Range
72 Content-Type
73 Expires
74 Last-Modified
75 41 );
76  
77 41 my %FIELD_NAMES;
78 my %FIELD_PRIORITIES;
79  
80 {
81  
82 41 my $priority = 0;
83  
84 41 for my $field (
85 GENERAL_HEADERS(),
86 sort(
87 REQUEST_HEADERS(),
88 'Cookie'
89 ),
90 sort(
91 RESPONSE_HEADERS(),
92 'Set-Cookie',
93 'Set-Cookie2'
94 ),
95 sort(
96 ENTITY_HEADERS(),
97 'Content-Disposition'
98 )
99 ) {
100  
101 2091 my $key = uc $field;
102  
103 2091 $FIELD_NAMES{$key} = $field;
104 2091 $FIELD_PRIORITIES{$key} = ++$priority;
105  
106 }
107  
108 }
109  
110  
111 # ----------------------------------------------------------------------------------------------------------------------
112 # Class setup
113 # ----------------------------------------------------------------------------------------------------------------------
114  
115 41 ▶use GX::Class::Object;
Subroutine BEGIN: called CC=1 SCAR=0.0
116  
117 41 has 'headers' => (
118 isa => 'Hash::Ordered',
119 accessors => {
120 '_headers' => { type => 'get_reference' }
121 }
122 );
123  
124 41 build;
125  
126  
127 # ----------------------------------------------------------------------------------------------------------------------
128 # Public methods
129 # ----------------------------------------------------------------------------------------------------------------------
130  
131 2352 ▶sub add {
Subroutine add: called CC=3 SCAR=12.0
Pod documented
132  
133 2352 my $self = shift;
134 2352 my $field = shift;
135  
136 2352 ▶ if ( ! defined $field || ! length $field ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field and length $field) 0 2352 2352
Condition33%1/3
defined $field and length $field
!ll&&!rl&&r
002352
MC/DC0%0/2
defined $field and length $field
defined $field length $field
Truth table33%1/3
A and B
A= defined $fieldB= length $field
ABresult
0X0
100
111
137 0 complain "Invalid header name";
138 }
139  
140 2352 push @{$self->_headers->{ uc $field }}, @_;
141  
142 2352 return;
143  
144 }
145  
146 0 ▶sub as_hash {
Subroutine as_hash: not called CC=3 SCAR=24.8
Pod undocumented
147  
148 # UNDOCUMENTED
149  
150 0 my $self = shift;
151  
152 0 my %headers;
153 0 my $headers = $self->_headers;
154  
155 0 for my $field ( keys %$headers ) {
156 0 $headers{$field} = Storable::dclone( $headers->{$field} );
157 }
158  
159 0 ▶ return wantarray ? %headers : \%headers;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 0 0
160  
161 }
162  
163 21 ▶sub as_string {
Subroutine as_string: called CC=5 SCAR=16.1
Pod documented
164  
165 21 my $self = shift;
166  
167 21 my $string = '';
168  
169 21 my $headers = $self->_headers;
170  
171 21 for my $key ( $self->_sorted_field_keys ) {
172  
173 29 ▶ my $field = $FIELD_NAMES{$key} || $key;
Condition66%2/3
$FIELD_NAMES{$key} || $key
l!l&&r!l&&!r
14150
MC/DC0%0/2
$FIELD_NAMES{$key} || $key
$FIELD_NAMES{$key} $key
Truth table66%2/3
A || B
A= $FIELD_NAMES{$key}B= $key
ABresult
1X1
011
000
174  
175 29 for my $value ( grep { defined } @{$headers->{$key}} ) {
176  
177 # Handle newlines in the field value
178 29 ▶ if ( $value =~ /\n/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /\n/) { } 2 27 29
179 # A leading SP indicates folding [RFC 2616]
180 2 $string .= "$field: " . join( CRLFSP, grep { length } split( /\n/, $value ) );
181 }
182 else {
183 27 $string .= "$field: $value";
184 }
185  
186 29 $string .= CRLF;
187  
188 }
189  
190 }
191  
192 21 return $string;
193  
194 }
195  
196 3 ▶sub count {
Subroutine count: called CC=1 SCAR=0.0
Pod documented
197  
198 3 return scalar keys %{$_[0]->_headers};
199  
200 }
201  
202 6 ▶sub field_names {
Subroutine field_names: called CC=2 SCAR=7.0
Pod documented
203  
204 6 ▶ return map { $FIELD_NAMES{$_} || $_ } keys %{$_[0]->_headers};
Condition66%2/3
$FIELD_NAMES{$_} || $_
l!l&&r!l&&!r
640
MC/DC0%0/2
$FIELD_NAMES{$_} || $_
$FIELD_NAMES{$_} $_
Truth table66%2/3
A || B
A= $FIELD_NAMES{$_}B= $_
ABresult
1X1
011
000
205  
206 }
207  
208 21 ▶sub get {
Subroutine get: called CC=4 SCAR=13.9
Pod documented
209  
210 21 my $self = shift;
211 21 my $field = shift;
212  
213 21 ▶ defined $field or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $field or return 21 0 21
214  
215 21 ▶ my $values = $self->_headers->{ uc $field } or return;
Branch: 1 branch
BranchTrueFalseTotal
my $values = $self->_headers->{uc $field} or return 18 3 21
216  
217 18 ▶ return wantarray ? @$values : $values->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 6 12 18
218  
219 }
220  
221 1339 ▶sub parse {
Subroutine parse: called CC=9 SCAR=22.4
Pod documented
222  
223 1339 my $invocant = shift;
224 1339 my $string = shift;
225  
226 1339 ▶ my $self = ref $invocant ? $invocant : $invocant->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $invocant ? : 0 1339 1339
227  
228 1339 ▶ return $self if ! defined $string;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $string 0 1339 1339
229  
230 1339 my @lines;
231  
232 1339 for my $line ( split( REGEX_CRLF, $string ) ) {
233  
234 # An empty line (i.e., a line with nothing preceding the CRLF)
235 # indicates the end of the header fields. [RFC 2616]
236 2344 ▶ last unless length $line;
Branch: 1 branch
BranchTrueFalseTotal
unless length $line 0 2344 2344
237  
238 # Header fields can be extended over multiple lines by preceding each
239 # extra line with at least one SP or HT. A recipient MAY replace any
240 # linear white space with a single SP before interpreting the field
241 # value or forwarding the message downstream. [RFC 2616]
242 2344 ▶ if ( $line =~ s/^[ \t]// ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($line =~ s/^[ \t]//) { } 5 2339 2344
243 5 ▶ next unless @lines;
Branch: 1 branch
BranchTrueFalseTotal
unless @lines 0 5 5
244 5 $lines[-1] .= "\n$line";
245 }
246 else {
247 2339 push @lines, $line;
248 }
249  
250 }
251  
252 1339 for my $line ( @lines ) {
253 2339 my ( $field, $value ) = split( /:\s*/, $line, 2 );
254 2339 ▶ next unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 2339 2339
255 2339 $value =~ s/\s+$//;
256 2339 $self->add( $field, $value );
257 }
258  
259 1339 return $self;
260  
261 }
262  
263 4 ▶sub remove {
Subroutine remove: called CC=3 SCAR=11.0
Pod documented
264  
265 4 my $self = shift;
266 4 my $field = shift;
267  
268 4 ▶ defined $field or return;
Branch: 1 branch
BranchTrueFalseTotal
defined $field or return 4 0 4
269  
270 4 ▶ return delete $self->_headers->{ uc $field } ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
delete $self->_headers->{uc $field} ? : 3 1 4
271  
272 }
273  
274 31 ▶sub set {
Subroutine set: called CC=3 SCAR=12.3
Pod documented
275  
276 31 my $self = shift;
277 31 my $field = shift;
278  
279 31 ▶ if ( ! defined $field || ! length $field ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $field and length $field) 0 31 31
Condition33%1/3
defined $field and length $field
!ll&&!rl&&r
0031
MC/DC0%0/2
defined $field and length $field
defined $field length $field
Truth table33%1/3
A and B
A= defined $fieldB= length $field
ABresult
0X0
100
111
280 0 complain "Invalid header name";
281 }
282  
283 31 $self->_headers->{ uc $field } = [ @_ ];
284  
285 31 return;
286  
287 }
288  
289 0 ▶sub sorted_field_names {
Subroutine sorted_field_names: not called CC=2 SCAR=17.9
Pod documented
290  
291 0 ▶ return map { $FIELD_NAMES{$_} || $_ } $_[0]->_sorted_field_keys;
Condition0%0/3
$FIELD_NAMES{$_} || $_
l!l&&r!l&&!r
000
MC/DC0%0/2
$FIELD_NAMES{$_} || $_
$FIELD_NAMES{$_} $_
Truth table0%0/3
A || B
A= $FIELD_NAMES{$_}B= $_
ABresult
1X1
011
000
292  
293 }
294  
295  
296 # ----------------------------------------------------------------------------------------------------------------------
297 # Private methods
298 # ----------------------------------------------------------------------------------------------------------------------
299  
300 69 ▶sub _install_field_accessors {
Subroutine _install_field_accessors: called CC=2 SCAR=6.9
301  
302 69 my $class = shift;
303  
304 69 for my $field ( @_ ) {
305  
306 448 ( my $accessor = lc $field ) =~ tr/-/_/;
307  
308 448 my $key = uc $field;
309  
310 1480 ▶ my $code = sub {
Subroutine __ANON__: called CC=4 SCAR=13.9
311  
312 1480 my $self = shift;
313  
314 1480 ▶ if ( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 36 1444 1480
315 36 $self->_headers->{$key} = [ @_ ];
316 36 return;
317 }
318  
319 1444 ▶ my $values = $self->_headers->{$key} or return;
Branch: 1 branch
BranchTrueFalseTotal
my $values = $self->_headers->{$key} or return 1079 365 1444
320  
321 1079 ▶ return wantarray ? @$values : $values->[0];
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 35 1044 1079
322  
323 448 };
324  
325 448 $class->meta->add_method( $accessor, $code );
326  
327 }
328  
329 69 return;
330  
331 }
332  
333 21 ▶sub _sorted_field_keys {
Subroutine _sorted_field_keys: called CC=4 SCAR=13.9
334  
335 21 my $self = shift;
336  
337 my @keys = sort {
338 39 ▶ ( $FIELD_PRIORITIES{$a} || 999 ) <=> ( $FIELD_PRIORITIES{$b} || 999 ) || $a cmp $b
Condition85%6/7
($FIELD_PRIORITIES{$a} || 999) <=> ($FIELD_PRIORITIES{$b} || 999) || $a cmp $b
l!l&&r!l&&!r
3270
$FIELD_PRIORITIES{$a} || 999
l!l
309
$FIELD_PRIORITIES{$b} || 999
l!l
1821
MC/DC0%0/2
($FIELD_PRIORITIES{$a} || 999) <=> ($FIELD_PRIORITIES{$b} || 999) || $a cmp $b
$FIELD_PRIORITIES{$a} $a cmp $b
MC/DC100%1/1
$FIELD_PRIORITIES{$b} || 999
$FIELD_PRIORITIES{$b}
Truth table66%2/3
A || B
A= $FIELD_PRIORITIES{$a}B= $a cmp $b
ABresult
1X1
011
000
Truth table100%2/2
A
A= $FIELD_PRIORITIES{$b}
Aresult
11
00
339 21 } keys %{$self->_headers};
340  
341 21 return @keys;
342  
343 }
344  
345  
346 # ----------------------------------------------------------------------------------------------------------------------
347 # Field accessors
348 # ----------------------------------------------------------------------------------------------------------------------
349  
350 41 __PACKAGE__->_install_field_accessors( qw(
351 Content-Disposition
352 Content-Encoding
353 Content-Language
354 Content-Length
355 Content-Type
356 Date
357 Expires
358 Last-Modified
359 ) );
360  
361  
362 41 1;
363  
364 __END__
« GX/Meta/Attribute/Array.pm ↑ Summary GX/HTTP/Parser/Body.pm »