1 package HTTP::Headers::Util;
4 use vars qw($VERSION @ISA @EXPORT_OK);
11 @EXPORT_OK=qw(split_header_words join_header_words);
15 sub split_header_words
22 if (s/^\s*(=*[^\s=;,]+)//) { # 'token' or parameter 'attribute'
25 if (s/^\s*=\s*\"([^\"\\]*(?:\\.[^\"\\]*)*)\"//) {
31 elsif (s/^\s*=\s*([^;,\s]*)//) {
35 # no value, a lone token
42 push(@res, [@cur]) if @cur;
45 elsif (s/^\s*;// || s/^\s+//) {
49 die "This should not happen: '$_'";
52 push(@res, \@cur) if @cur;
60 @_ = ([@_]) if @_ && !ref($_[0]);
69 if ($v =~ /[\x00-\x20()<>@,;:\\\"\/\[\]?={}\x7F-\xFF]/ || !length($v)) {
70 $v =~ s/([\"\\])/\\$1/g; # escape " and \
80 push(@res, join("; ", @attr)) if @attr;
92 HTTP::Headers::Util - Header value parsing utility functions
96 use HTTP::Headers::Util qw(split_header_words);
97 @values = split_header_words($h->header("Content-Type"));
101 This module provides a few functions that helps parsing and
102 construction of valid HTTP header values. None of the functions are
105 The following functions are available:
110 =item split_header_words( @header_values )
112 This function will parse the header values given as argument into a
113 list of anonymous arrays containing key/value pairs. The function
114 knows how to deal with ",", ";" and "=" as well as quoted values after
115 "=". A list of space separated tokens are parsed as if they were
118 If the @header_values passed as argument contains multiple values,
119 then they are treated as if they were a single value separated by
122 This means that this function is useful for parsing header fields that
123 follow this syntax (BNF as from the HTTP/1.1 specification, but we relax
124 the requirement for tokens).
127 header = (token | parameter) *( [";"] (token | parameter))
129 token = 1*<any CHAR except CTLs or separators>
130 separators = "(" | ")" | "<" | ">" | "@"
131 | "," | ";" | ":" | "\" | <">
132 | "/" | "[" | "]" | "?" | "="
133 | "{" | "}" | SP | HT
135 quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
136 qdtext = <any TEXT except <">>
137 quoted-pair = "\" CHAR
139 parameter = attribute "=" value
141 value = token | quoted-string
143 Each I<header> is represented by an anonymous array of key/value
144 pairs. The value for a simple token (not part of a parameter) is C<undef>.
145 Syntactically incorrect headers will not necessary be parsed as you
148 This is easier to describe with some examples:
150 split_header_words('foo="bar"; port="80,81"; discard, bar=baz');
151 split_header_words('text/html; charset="iso-8859-1"');
152 split_header_words('Basic realm="\\"foo\\\\bar\\""');
156 [foo=>'bar', port=>'80,81', discard=> undef], [bar=>'baz' ]
157 ['text/html' => undef, charset => 'iso-8859-1']
158 [Basic => undef, realm => "\"foo\\bar\""]
160 =item join_header_words( @arrays )
162 This will do the opposite of the conversion done by split_header_words().
163 It takes a list of anonymous arrays as arguments (or a list of
164 key/value pairs) and produces a single header value. Attribute values
165 are quoted if needed.
169 join_header_words(["text/plain" => undef, charset => "iso-8859/1"]);
170 join_header_words("text/plain" => undef, charset => "iso-8859/1");
172 will both return the string:
174 text/plain; charset="iso-8859/1"
180 Copyright 1997-1998, Gisle Aas
182 This library is free software; you can redistribute it and/or
183 modify it under the same terms as Perl itself.