Headers/Util.pm

filter: Statementstmt 98.1% 52 / 53 Branchbran 96.4% 27 / 28 Conditioncond 91.6% 11 / 12 MC/DCmcdc 57.1% 4 / 7 Subroutinesub 100% 6 / 6 Podpod 100% 2 / 2 totaltotal 94.4% 102 / 108 CC 22 SCAR 21.7
CC
22
Cov
97%
_split_header_words
12.1
join_header_words
9.0
split_header_words
3.0
CRAP 8.8
« Headers.pm ↑ Summary Headers/Auth.pm »
1 package HTTP::Headers::Util;
2  
3 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 13 our $VERSION = '7.04';
7  
8 13 ▶use Exporter 5.57 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 13 our @EXPORT_OK=qw(split_header_words _split_header_words join_header_words);
11  
12  
13 138 ▶sub split_header_words {
Subroutine split_header_words: called CC=3 SCAR=11.0
Pod documented
14 138 my @res = &_split_header_words;
15 138 for my $arr (@res) {
16 144 ▶ for (my $i = @$arr - 2; $i >= 0; $i -= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i >= 0) 185 144 329
17 185 $arr->[$i] = lc($arr->[$i]);
18 }
19 }
20 138 return @res;
21 }
22  
23 sub _split_header_words
24 138 ▶{
Subroutine _split_header_words: called CC=12 SCAR=24.9
25 138 my(@val) = @_;
26 138 my @res;
27 138 for (@val) {
28 139 my @cur;
29 139 ▶ while (length) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $_) 237 139 376
30 237 ▶ if (s/^\s*(=*[^\s=;,]+)//) { # 'token' or parameter 'attribute'
Branch: 3 branches
BranchTrueFalseTotal
if (s/^\s*(=*[^\s=;,]+)//) { } 185 52 237
elsif (s/^\s*,//) { } 9 43 52
elsif (s/^\s*;// or s/^\s+// or s/^=//) { } 43 0 43
31 185 push(@cur, $1);
32 # a quoted value
33 185 ▶ if (s/^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"//) {
Branch: 2 branches
BranchTrueFalseTotal
if (s/^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"//) { } 10 175 185
elsif (s/^\s*=\s*([^;,\s]*)//) { } 40 135 175
34 10 my $val = $1;
35 10 $val =~ s/\\(.)/$1/g;
36 10 push(@cur, $val);
37 # some unquoted value
38 }
39 elsif (s/^\s*=\s*([^;,\s]*)//) {
40 40 my $val = $1;
41 40 $val =~ s/\s+$//;
42 40 push(@cur, $val);
43 # no value, a lone token
44 }
45 else {
46 135 push(@cur, undef);
47 }
48 }
49 elsif (s/^\s*,//) {
50 9 ▶ push(@res, [@cur]) if @cur;
Branch: 1 branch
BranchTrueFalseTotal
if @cur 7 2 9
51 9 @cur = ();
52 }
53 ▶ elsif (s/^\s*;// || s/^\s+// || s/^=//) {
Condition83%5/6
s/^\s*;// or s/^\s+// or s/^=//
l!l&&r!l&&!r
4210
s/^\s*;// or s/^\s+//
l!l&&r!l&&!r
3481
MC/DC0%0/3
s/^\s*;// or s/^\s+// or s/^=//
s/^\s*;// s/^\s+// s/^=//
Truth table75%3/4
A or B or C
A= s/^\s*;//B= s/^\s+//C= s/^=//
ABCresult
1XX1
01X1
0011
0000
54 # continue
55 }
56 else {
57 0 die "This should not happen: '$_'";
58 }
59 }
60 139 ▶ push(@res, \@cur) if @cur;
Branch: 1 branch
BranchTrueFalseTotal
if @cur 137 2 139
61 }
62 138 @res;
63 }
64  
65  
66 sub join_header_words
67 31 ▶{
Subroutine join_header_words: called CC=9 SCAR=22.0
Pod documented
68 31 ▶ @_ = ([@_]) if @_ && !ref($_[0]);
Branch: 1 branch
BranchTrueFalseTotal
if @_ and not ref $_[0] 16 15 31
Condition100%3/3
@_ and not ref $_[0]
!ll&&!rl&&r
11416
MC/DC100%2/2
@_ and not ref $_[0]
@_ not ref $_[0]
Truth table100%3/3
A and not(B)
A= @_B= not ref $_[0]
ABresult
0X0
100
111
69 31 my @res;
70 31 for (@_) {
71 33 my @cur = @$_;
72 33 my @attr;
73 33 ▶ while (@cur) {
Branch: 1 branch
BranchTrueFalseTotal
if (@cur) 57 33 90
74 57 my $k = shift @cur;
75 57 my $v = shift @cur;
76 57 ▶ if (defined $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v) 31 26 57
77 31 ▶ if ($v =~ /[\x00-\x20()<>@,;:\\\"\/\[\]?={}\x7F-\xFF]/ || !length($v)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($v =~ m([\x00-\x20()<>\@,;:\\\"/\[\]?={}\x7F-\xFF]) or not length $v) { } 5 26 31
Condition100%3/3
$v =~ m([\x00-\x20()<>\@,;:\\\"/\[\]?={}\x7F-\xFF]) or not length $v
l!l&&r!l&&!r
4126
MC/DC100%2/2
$v =~ m([\x00-\x20()<>\@,;:\\\"/\[\]?={}\x7F-\xFF]) or not length $v
$v =~ m([\x00-\x20()<>\@,;:\\\"/\[\]?={}\x7F-\xFF]) not length $v
Truth table100%3/3
A or not(B)
A= $v =~ m([\x00-\x20()<>\@,;:\\\"/\[\]?={}\x7F-\xFF])B= not length $v
ABresult
1X1
011
000
78 5 $v =~ s/([\"\\])/\\$1/g; # escape " and \
79 5 $k .= qq(="$v");
80 }
81 else {
82 # token
83 26 $k .= "=$v";
84 }
85 }
86 57 push(@attr, $k);
87 }
88 33 ▶ push(@res, join("; ", @attr)) if @attr;
Branch: 1 branch
BranchTrueFalseTotal
if @attr 32 1 33
89 }
90 31 join(", ", @res);
91 }
92  
93  
94 13 1;
95  
96 =pod
97  
98 =encoding UTF-8
99  
100 =head1 NAME
101  
102 HTTP::Headers::Util - Header value parsing utility functions
103  
104 =head1 VERSION
105  
106 version 7.04
107  
108 =head1 SYNOPSIS
109  
110 use HTTP::Headers::Util qw(split_header_words);
111 @values = split_header_words($h->header("Content-Type"));
112  
113 =head1 DESCRIPTION
114  
115 This module provides a few functions that helps parsing and
116 construction of valid HTTP header values. None of the functions are
117 exported by default.
118  
119 The following functions are available:
120  
121 =over 4
122  
123 =item split_header_words( @header_values )
124  
125 This function will parse the header values given as argument into a
126 list of anonymous arrays containing key/value pairs. The function
127 knows how to deal with ",", ";" and "=" as well as quoted values after
128 "=". A list of space separated tokens are parsed as if they were
129 separated by ";".
130  
131 If the @header_values passed as argument contains multiple values,
132 then they are treated as if they were a single value separated by
133 comma ",".
134  
135 This means that this function is useful for parsing header fields that
136 follow this syntax (BNF as from the HTTP/1.1 specification, but we relax
137 the requirement for tokens).
138  
139 headers = #header
140 header = (token | parameter) *( [";"] (token | parameter))
141  
142 token = 1*<any CHAR except CTLs or separators>
143 separators = "(" | ")" | "<" | ">" | "@"
144 | "," | ";" | ":" | "\" | <">
145 | "/" | "[" | "]" | "?" | "="
146 | "{" | "}" | SP | HT
147  
148 quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
149 qdtext = <any TEXT except <">>
150 quoted-pair = "\" CHAR
151  
152 parameter = attribute "=" value
153 attribute = token
154 value = token | quoted-string
155  
156 Each I<header> is represented by an anonymous array of key/value
157 pairs. The keys will be all be forced to lower case.
158 The value for a simple token (not part of a parameter) is C<undef>.
159 Syntactically incorrect headers will not necessarily be parsed as you
160 would want.
161  
162 This is easier to describe with some examples:
163  
164 split_header_words('foo="bar"; port="80,81"; DISCARD, BAR=baz');
165 split_header_words('text/html; charset="iso-8859-1"');
166 split_header_words('Basic realm="\\"foo\\\\bar\\""');
167  
168 will return
169  
170 [foo=>'bar', port=>'80,81', discard=> undef], [bar=>'baz' ]
171 ['text/html' => undef, charset => 'iso-8859-1']
172 [basic => undef, realm => "\"foo\\bar\""]
173  
174 If you don't want the function to convert tokens and attribute keys to
175 lower case you can call it as C<_split_header_words> instead (with a
176 leading underscore).
177  
178 =item join_header_words( @arrays )
179  
180 This will do the opposite of the conversion done by split_header_words().
181 It takes a list of anonymous arrays as arguments (or a list of
182 key/value pairs) and produces a single header value. Attribute values
183 are quoted if needed.
184  
185 Example:
186  
187 join_header_words(["text/plain" => undef, charset => "iso-8859/1"]);
188 join_header_words("text/plain" => undef, charset => "iso-8859/1");
189  
190 will both return the string:
191  
192 text/plain; charset="iso-8859/1"
193  
194 =back
195  
196 =head1 AUTHOR
197  
198 Gisle Aas <gisle@activestate.com>
199  
200 =head1 COPYRIGHT AND LICENSE
201  
202 This software is copyright (c) 1994 by Gisle Aas.
203  
204 This is free software; you can redistribute it and/or modify it under
205 the same terms as the Perl 5 programming language system itself.
206  
207 =cut
208  
209 __END__
« Headers.pm ↑ Summary Headers/Auth.pm »