1 package HTTP::Headers::Util;
4 use vars qw($VERSION @ISA @EXPORT_OK);
11 @EXPORT_OK=qw(split_header_words _split_header_words join_header_words);
15 sub split_header_words {
16 my @res = &_split_header_words;
18 for (my $i = @$arr - 2; $i >= 0; $i -= 2) {
19 $arr->[$i] = lc($arr->[$i]);
25 sub _split_header_words
32 if (s/^\s*(=*[^\s=;,]+)//) { # 'token' or parameter 'attribute'
35 if (s/^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"//) {
41 elsif (s/^\s*=\s*([^;,\s]*)//) {
45 # no value, a lone token
52 push(@res, [@cur]) if @cur;
55 elsif (s/^\s*;// || s/^\s+//) {
59 die "This should not happen: '$_'";
62 push(@res, \@cur) if @cur;
70 @_ = ([@_]) if @_ && !ref($_[0]);
79 if ($v =~ /[\x00-\x20()<>@,;:\\\"\/\[\]?={}\x7F-\xFF]/ || !length($v)) {
80 $v =~ s/([\"\\])/\\$1/g; # escape " and \
90 push(@res, join("; ", @attr)) if @attr;
102 HTTP::Headers::Util - Header value parsing utility functions
106 use HTTP::Headers::Util qw(split_header_words);
107 @values = split_header_words($h->header("Content-Type"));
111 This module provides a few functions that helps parsing and
112 construction of valid HTTP header values. None of the functions are
115 The following functions are available:
120 =item split_header_words( @header_values )
122 This function will parse the header values given as argument into a
123 list of anonymous arrays containing key/value pairs. The function
124 knows how to deal with ",", ";" and "=" as well as quoted values after
125 "=". A list of space separated tokens are parsed as if they were
128 If the @header_values passed as argument contains multiple values,
129 then they are treated as if they were a single value separated by
132 This means that this function is useful for parsing header fields that
133 follow this syntax (BNF as from the HTTP/1.1 specification, but we relax
134 the requirement for tokens).
137 header = (token | parameter) *( [";"] (token | parameter))
139 token = 1*<any CHAR except CTLs or separators>
140 separators = "(" | ")" | "<" | ">" | "@"
141 | "," | ";" | ":" | "\" | <">
142 | "/" | "[" | "]" | "?" | "="
143 | "{" | "}" | SP | HT
145 quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
146 qdtext = <any TEXT except <">>
147 quoted-pair = "\" CHAR
149 parameter = attribute "=" value
151 value = token | quoted-string
153 Each I<header> is represented by an anonymous array of key/value
154 pairs. The keys will be all be forced to lower case.
155 The value for a simple token (not part of a parameter) is C<undef>.
156 Syntactically incorrect headers will not necessary be parsed as you
159 This is easier to describe with some examples:
161 split_header_words('foo="bar"; port="80,81"; DISCARD, BAR=baz');
162 split_header_words('text/html; charset="iso-8859-1"');
163 split_header_words('Basic realm="\\"foo\\\\bar\\""');
167 [foo=>'bar', port=>'80,81', discard=> undef], [bar=>'baz' ]
168 ['text/html' => undef, charset => 'iso-8859-1']
169 [basic => undef, realm => "\"foo\\bar\""]
171 If you don't want the function to convert tokens and attribute keys to
172 lower case you can call it as C<_split_header_words> instead (with a
175 =item join_header_words( @arrays )
177 This will do the opposite of the conversion done by split_header_words().
178 It takes a list of anonymous arrays as arguments (or a list of
179 key/value pairs) and produces a single header value. Attribute values
180 are quoted if needed.
184 join_header_words(["text/plain" => undef, charset => "iso-8859/1"]);
185 join_header_words("text/plain" => undef, charset => "iso-8859/1");
187 will both return the string:
189 text/plain; charset="iso-8859/1"
195 Copyright 1997-1998, Gisle Aas
197 This library is free software; you can redistribute it and/or
198 modify it under the same terms as Perl itself.