1HTTP::Headers::Util(3)User Contributed Perl DocumentationHTTP::Headers::Util(3)
2
3
4
6 HTTP::Headers::Util - Header value parsing utility functions
7
9 use HTTP::Headers::Util qw(split_header_words);
10 @values = split_header_words($h->header("Content-Type"));
11
13 This module provides a few functions that helps parsing and
14 construction of valid HTTP header values. None of the functions are
15 exported by default.
16
17 The following functions are available:
18
19 split_header_words( @header_values )
20 This function will parse the header values given as argument into a
21 list of anonymous arrays containing key/value pairs. The function
22 knows how to deal with ",", ";" and "=" as well as quoted values
23 after "=". A list of space separated tokens are parsed as if they
24 were separated by ";".
25
26 If the @header_values passed as argument contains multiple values,
27 then they are treated as if they were a single value separated by
28 comma ",".
29
30 This means that this function is useful for parsing header fields
31 that follow this syntax (BNF as from the HTTP/1.1 specification,
32 but we relax the requirement for tokens).
33
34 headers = #header
35 header = (token | parameter) *( [";"] (token | parameter))
36
37 token = 1*<any CHAR except CTLs or separators>
38 separators = "(" | ")" | "<" | ">" | "@"
39 | "," | ";" | ":" | "\" | <">
40 | "/" | "[" | "]" | "?" | "="
41 | "{" | "}" | SP | HT
42
43 quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
44 qdtext = <any TEXT except <">>
45 quoted-pair = "\" CHAR
46
47 parameter = attribute "=" value
48 attribute = token
49 value = token | quoted-string
50
51 Each header is represented by an anonymous array of key/value
52 pairs. The keys will be all be forced to lower case. The value
53 for a simple token (not part of a parameter) is "undef".
54 Syntactically incorrect headers will not necessarily be parsed as
55 you would want.
56
57 This is easier to describe with some examples:
58
59 split_header_words('foo="bar"; port="80,81"; DISCARD, BAR=baz');
60 split_header_words('text/html; charset="iso-8859-1"');
61 split_header_words('Basic realm="\\"foo\\\\bar\\""');
62
63 will return
64
65 [foo=>'bar', port=>'80,81', discard=> undef], [bar=>'baz' ]
66 ['text/html' => undef, charset => 'iso-8859-1']
67 [basic => undef, realm => "\"foo\\bar\""]
68
69 If you don't want the function to convert tokens and attribute keys
70 to lower case you can call it as "_split_header_words" instead
71 (with a leading underscore).
72
73 join_header_words( @arrays )
74 This will do the opposite of the conversion done by
75 split_header_words(). It takes a list of anonymous arrays as
76 arguments (or a list of key/value pairs) and produces a single
77 header value. Attribute values are quoted if needed.
78
79 Example:
80
81 join_header_words(["text/plain" => undef, charset => "iso-8859/1"]);
82 join_header_words("text/plain" => undef, charset => "iso-8859/1");
83
84 will both return the string:
85
86 text/plain; charset="iso-8859/1"
87
89 Copyright 1997-1998, Gisle Aas
90
91 This library is free software; you can redistribute it and/or modify it
92 under the same terms as Perl itself.
93
94
95
96perl v5.16.3 2012-02-16 HTTP::Headers::Util(3)