1HTTP::Headers::Util(3)User Contributed Perl DocumentationHTTP::Headers::Util(3)
2
3
4

NAME

6       HTTP::Headers::Util - Header value parsing utility functions
7

SYNOPSIS

9         use HTTP::Headers::Util qw(split_header_words);
10         @values = split_header_words($h->header("Content-Type"));
11

DESCRIPTION

13       This module provides a few functions that helps parsing and construc‐
14       tion of valid HTTP header values.  None of the functions are exported
15       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 value for a simple token (not part of a parameter) is
53           "undef".  Syntactically incorrect headers will not necessary be
54           parsed as you would want.
55
56           This is easier to describe with some examples:
57
58              split_header_words('foo="bar"; port="80,81"; discard, bar=baz');
59              split_header_words('text/html; charset="iso-8859-1"');
60              split_header_words('Basic realm="\\"foo\\\\bar\\""');
61
62           will return
63
64              [foo=>'bar', port=>'80,81', discard=> undef], [bar=>'baz' ]
65              ['text/html' => undef, charset => 'iso-8859-1']
66              [Basic => undef, realm => "\"foo\\bar\""]
67
68       join_header_words( @arrays )
69           This will do the opposite of the conversion done by
70           split_header_words().  It takes a list of anonymous arrays as argu‐
71           ments (or a list of key/value pairs) and produces a single header
72           value.  Attribute values are quoted if needed.
73
74           Example:
75
76              join_header_words(["text/plain" => undef, charset => "iso-8859/1"]);
77              join_header_words("text/plain" => undef, charset => "iso-8859/1");
78
79           will both return the string:
80
81              text/plain; charset="iso-8859/1"
82
84       Copyright 1997-1998, Gisle Aas
85
86       This library is free software; you can redistribute it and/or modify it
87       under the same terms as Perl itself.
88
89
90
91perl v5.8.8                       2004-04-06            HTTP::Headers::Util(3)
Impressum