| 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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 30 | 237 | ▶ | if (s/^\s*(=*[^\s=;,]+)//) { # 'token' or parameter 'attribute' | ||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||
| 31 | 185 | push(@cur, $1); | |||||||||||||||||||||||||||||||||
| 32 | # a quoted value | ||||||||||||||||||||||||||||||||||
| 33 | 185 | ▶ | if (s/^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"//) { | ||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 51 | 9 | @cur = (); | |||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||
| 53 | ▶ | elsif (s/^\s*;// || s/^\s+// || s/^=//) { | |||||||||||||||||||||||||||||||||
|
Condition83%5/6
s/^\s*;// or s/^\s+// or s/^=//
s/^\s*;// or s/^\s+//
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/^=//
| |||||||||||||||||||||||||||||||||||
| 54 | # continue | ||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||
| 56 | else { | ||||||||||||||||||||||||||||||||||
| 57 | 0 | die "This should not happen: '$_'"; | |||||||||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||
| 60 | 139 | ▶ | push(@res, \@cur) if @cur; | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
@_ and not ref $_[0]
MC/DC100%2/2
@_ and not ref $_[0]
@_
not ref $_[0]
Truth table100%3/3
A and not(B)
A= @_B= not ref $_[0]
| |||||||||||||||||||||||||||||||||||
| 69 | 31 | my @res; | |||||||||||||||||||||||||||||||||
| 70 | 31 | for (@_) { | |||||||||||||||||||||||||||||||||
| 71 | 33 | my @cur = @$_; | |||||||||||||||||||||||||||||||||
| 72 | 33 | my @attr; | |||||||||||||||||||||||||||||||||
| 73 | 33 | ▶ | while (@cur) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 74 | 57 | my $k = shift @cur; | |||||||||||||||||||||||||||||||||
| 75 | 57 | my $v = shift @cur; | |||||||||||||||||||||||||||||||||
| 76 | 57 | ▶ | if (defined $v) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||
| 77 | 31 | ▶ | if ($v =~ /[\x00-\x20()<>@,;:\\\"\/\[\]?={}\x7F-\xFF]/ || !length($v)) { | ||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$v =~ m([\x00-\x20()<>\@,;:\\\"/\[\]?={}\x7F-\xFF]) or not length $v
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
| |||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||