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

NAME

6       HTTP::Headers - Class encapsulating HTTP Message headers
7

SYNOPSIS

9        require HTTP::Headers;
10        $h = HTTP::Headers->new;
11
12        $h->header('Content-Type' => 'text/plain');  # set
13        $ct = $h->header('Content-Type');            # get
14        $h->remove_header('Content-Type');           # delete
15

DESCRIPTION

17       The "HTTP::Headers" class encapsulates HTTP-style message headers.  The
18       headers consist of attribute-value pairs also called fields, which may
19       be repeated, and which are printed in a particular order.  The field
20       names are cases insensitive.
21
22       Instances of this class are usually created as member variables of the
23       "HTTP::Request" and "HTTP::Response" classes, internal to the library.
24
25       The following methods are available:
26
27       $h = HTTP::Headers->new
28           Constructs a new "HTTP::Headers" object.  You might pass some
29           initial attribute-value pairs as parameters to the constructor.
30           E.g.:
31
32            $h = HTTP::Headers->new(
33                  Date         => 'Thu, 03 Feb 1994 00:00:00 GMT',
34                  Content_Type => 'text/html; version=3.2',
35                  Content_Base => 'http://www.perl.org/');
36
37           The constructor arguments are passed to the "header" method which
38           is described below.
39
40       $h->clone
41           Returns a copy of this "HTTP::Headers" object.
42
43       $h->header( $field )
44       $h->header( $field => $value )
45       $h->header( $f1 => $v1, $f2 => $v2, ... )
46           Get or set the value of one or more header fields.  The header
47           field name ($field) is not case sensitive.  To make the life easier
48           for perl users who wants to avoid quoting before the => operator,
49           you can use '_' as a replacement for '-' in header names.
50
51           The header() method accepts multiple ($field => $value) pairs,
52           which means that you can update several fields with a single
53           invocation.
54
55           The $value argument may be a plain string or a reference to an
56           array of strings for a multi-valued field. If the $value is
57           provided as "undef" then the field is removed.  If the $value is
58           not given, then that header field will remain unchanged.
59
60           The old value (or values) of the last of the header fields is
61           returned.  If no such field exists "undef" will be returned.
62
63           A multi-valued field will be returned as separate values in list
64           context and will be concatenated with ", " as separator in scalar
65           context.  The HTTP spec (RFC 2616) promise that joining multiple
66           values in this way will not change the semantic of a header field,
67           but in practice there are cases like old-style Netscape cookies
68           (see HTTP::Cookies) where "," is used as part of the syntax of a
69           single field value.
70
71           Examples:
72
73            $header->header(MIME_Version => '1.0',
74                            User_Agent   => 'My-Web-Client/0.01');
75            $header->header(Accept => "text/html, text/plain, image/*");
76            $header->header(Accept => [qw(text/html text/plain image/*)]);
77            @accepts = $header->header('Accept');  # get multiple values
78            $accepts = $header->header('Accept');  # get values as a single string
79
80       $h->push_header( $field => $value )
81       $h->push_header( $f1 => $v1, $f2 => $v2, ... )
82           Add a new field value for the specified header field.  Previous
83           values for the same field are retained.
84
85           As for the header() method, the field name ($field) is not case
86           sensitive and '_' can be used as a replacement for '-'.
87
88           The $value argument may be a scalar or a reference to a list of
89           scalars.
90
91            $header->push_header(Accept => 'image/jpeg');
92            $header->push_header(Accept => [map "image/$_", qw(gif png tiff)]);
93
94       $h->init_header( $field => $value )
95           Set the specified header to the given value, but only if no
96           previous value for that field is set.
97
98           The header field name ($field) is not case sensitive and '_' can be
99           used as a replacement for '-'.
100
101           The $value argument may be a scalar or a reference to a list of
102           scalars.
103
104       $h->remove_header( $field, ... )
105           This function removes the header fields with the specified names.
106
107           The header field names ($field) are not case sensitive and '_' can
108           be used as a replacement for '-'.
109
110           The return value is the values of the fields removed.  In scalar
111           context the number of fields removed is returned.
112
113           Note that if you pass in multiple field names then it is generally
114           not possible to tell which of the returned values belonged to which
115           field.
116
117       $h->remove_content_headers
118           This will remove all the header fields used to describe the content
119           of a message.  All header field names prefixed with "Content-" fall
120           into this category, as well as "Allow", "Expires" and
121           "Last-Modified".  RFC 2616 denotes these fields as Entity Header
122           Fields.
123
124           The return value is a new "HTTP::Headers" object that contains the
125           removed headers only.
126
127       $h->clear
128           This will remove all header fields.
129
130       $h->header_field_names
131           Returns the list of distinct names for the fields present in the
132           header.  The field names have case as suggested by HTTP spec, and
133           the names are returned in the recommended "Good Practice" order.
134
135           In scalar context return the number of distinct field names.
136
137       $h->scan( \&process_header_field )
138           Apply a subroutine to each header field in turn.  The callback
139           routine is called with two parameters; the name of the field and a
140           single value (a string).  If a header field is multi-valued, then
141           the routine is called once for each value.  The field name passed
142           to the callback routine has case as suggested by HTTP spec, and the
143           headers will be visited in the recommended "Good Practice" order.
144
145           Any return values of the callback routine are ignored.  The loop
146           can be broken by raising an exception ("die"), but the caller of
147           scan() would have to trap the exception itself.
148
149       $h->as_string
150       $h->as_string( $eol )
151           Return the header fields as a formatted MIME header.  Since it
152           internally uses the "scan" method to build the string, the result
153           will use case as suggested by HTTP spec, and it will follow
154           recommended "Good Practice" of ordering the header fields.  Long
155           header values are not folded.
156
157           The optional $eol parameter specifies the line ending sequence to
158           use.  The default is "\n".  Embedded "\n" characters in header
159           field values will be substituted with this line ending sequence.
160

CONVENIENCE METHODS

162       The most frequently used headers can also be accessed through the
163       following convenience methods.  Most of these methods can both be used
164       to read and to set the value of a header.  The header value is set if
165       you pass an argument to the method.  The old header value is always
166       returned.  If the given header did not exist then "undef" is returned.
167
168       Methods that deal with dates/times always convert their value to system
169       time (seconds since Jan 1, 1970) and they also expect this kind of
170       value when the header value is set.
171
172       $h->date
173           This header represents the date and time at which the message was
174           originated. E.g.:
175
176             $h->date(time);  # set current date
177
178       $h->expires
179           This header gives the date and time after which the entity should
180           be considered stale.
181
182       $h->if_modified_since
183       $h->if_unmodified_since
184           These header fields are used to make a request conditional.  If the
185           requested resource has (or has not) been modified since the time
186           specified in this field, then the server will return a "304 Not
187           Modified" response instead of the document itself.
188
189       $h->last_modified
190           This header indicates the date and time at which the resource was
191           last modified. E.g.:
192
193             # check if document is more than 1 hour old
194             if (my $last_mod = $h->last_modified) {
195                 if ($last_mod < time - 60*60) {
196                     ...
197                 }
198             }
199
200       $h->content_type
201           The Content-Type header field indicates the media type of the
202           message content. E.g.:
203
204             $h->content_type('text/html');
205
206           The value returned will be converted to lower case, and potential
207           parameters will be chopped off and returned as a separate value if
208           in an array context.  If there is no such header field, then the
209           empty string is returned.  This makes it safe to do the following:
210
211             if ($h->content_type eq 'text/html') {
212                # we enter this place even if the real header value happens to
213                # be 'TEXT/HTML; version=3.0'
214                ...
215             }
216
217       $h->content_type_charset
218           Returns the upper-cased charset specified in the Content-Type
219           header.  In list context return the lower-cased bare content type
220           followed by the upper-cased charset.  Both values will be "undef"
221           if not specified in the header.
222
223       $h->content_is_text
224           Returns TRUE if the Content-Type header field indicate that the
225           content is textual.
226
227       $h->content_is_html
228           Returns TRUE if the Content-Type header field indicate that the
229           content is some kind of HTML (including XHTML).  This method can't
230           be used to set Content-Type.
231
232       $h->content_is_xhtml
233           Returns TRUE if the Content-Type header field indicate that the
234           content is XHTML.  This method can't be used to set Content-Type.
235
236       $h->content_is_xml
237           Returns TRUE if the Content-Type header field indicate that the
238           content is XML.  This method can't be used to set Content-Type.
239
240       $h->content_encoding
241           The Content-Encoding header field is used as a modifier to the
242           media type.  When present, its value indicates what additional
243           encoding mechanism has been applied to the resource.
244
245       $h->content_length
246           A decimal number indicating the size in bytes of the message
247           content.
248
249       $h->content_language
250           The natural language(s) of the intended audience for the message
251           content.  The value is one or more language tags as defined by RFC
252           1766.  Eg. "no" for some kind of Norwegian and "en-US" for English
253           the way it is written in the US.
254
255       $h->title
256           The title of the document.  In libwww-perl this header will be
257           initialized automatically from the <TITLE>...</TITLE> element of
258           HTML documents.  This header is no longer part of the HTTP
259           standard.
260
261       $h->user_agent
262           This header field is used in request messages and contains
263           information about the user agent originating the request.  E.g.:
264
265             $h->user_agent('Mozilla/5.0 (compatible; MSIE 7.0; Windows NT 6.0)');
266
267       $h->server
268           The server header field contains information about the software
269           being used by the originating server program handling the request.
270
271       $h->from
272           This header should contain an Internet e-mail address for the human
273           user who controls the requesting user agent.  The address should be
274           machine-usable, as defined by RFC822.  E.g.:
275
276             $h->from('King Kong <king@kong.com>');
277
278           This header is no longer part of the HTTP standard.
279
280       $h->referer
281           Used to specify the address (URI) of the document from which the
282           requested resource address was obtained.
283
284           The "Free On-line Dictionary of Computing" as this to say about the
285           word referer:
286
287                <World-Wide Web> A misspelling of "referrer" which
288                somehow made it into the {HTTP} standard.  A given {web
289                page}'s referer (sic) is the {URL} of whatever web page
290                contains the link that the user followed to the current
291                page.  Most browsers pass this information as part of a
292                request.
293
294                (1998-10-19)
295
296           By popular demand "referrer" exists as an alias for this method so
297           you can avoid this misspelling in your programs and still send the
298           right thing on the wire.
299
300           When setting the referrer, this method removes the fragment from
301           the given URI if it is present, as mandated by RFC2616.  Note that
302           the removal does not happen automatically if using the header(),
303           push_header() or init_header() methods to set the referrer.
304
305       $h->www_authenticate
306           This header must be included as part of a "401 Unauthorized"
307           response.  The field value consist of a challenge that indicates
308           the authentication scheme and parameters applicable to the
309           requested URI.
310
311       $h->proxy_authenticate
312           This header must be included in a "407 Proxy Authentication
313           Required" response.
314
315       $h->authorization
316       $h->proxy_authorization
317           A user agent that wishes to authenticate itself with a server or a
318           proxy, may do so by including these headers.
319
320       $h->authorization_basic
321           This method is used to get or set an authorization header that use
322           the "Basic Authentication Scheme".  In array context it will return
323           two values; the user name and the password.  In scalar context it
324           will return "uname:password" as a single string value.
325
326           When used to set the header value, it expects two arguments.  E.g.:
327
328             $h->authorization_basic($uname, $password);
329
330           The method will croak if the $uname contains a colon ':'.
331
332       $h->proxy_authorization_basic
333           Same as authorization_basic() but will set the "Proxy-
334           Authorization" header instead.
335

NON-CANONICALIZED FIELD NAMES

337       The header field name spelling is normally canonicalized including the
338       '_' to '-' translation.  There are some application where this is not
339       appropriate.  Prefixing field names with ':' allow you to force a
340       specific spelling.  For example if you really want a header field name
341       to show up as "foo_bar" instead of "Foo-Bar", you might set it like
342       this:
343
344         $h->header(":foo_bar" => 1);
345
346       These field names are returned with the ':' intact for
347       $h->header_field_names and the $h->scan callback, but the colons do not
348       show in $h->as_string.
349
351       Copyright 1995-2005 Gisle Aas.
352
353       This library is free software; you can redistribute it and/or modify it
354       under the same terms as Perl itself.
355
356
357
358perl v5.16.3                      2012-10-20                  HTTP::Headers(3)
Impressum