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

CONVENIENCE METHODS

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

NON-CANONICALIZED FIELD NAMES

310       The header field name spelling is normally canonicalized including the
311       '_' to '-' translation.  There are some application where this is not
312       appropriate.  Prefixing field names with ':' allow you to force a spe‐
313       cific spelling.  For example if you really want a header field name to
314       show up as "foo_bar" instead of "Foo-Bar", you might set it like this:
315
316         $h->header(":foo_bar" => 1);
317
318       These field names are returned with the ':' intact for
319       $h->header_field_names and the $h->scan callback, but the colons do not
320       show in $h->as_string.
321
323       Copyright 1995-2005 Gisle Aas.
324
325       This library is free software; you can redistribute it and/or modify it
326       under the same terms as Perl itself.
327
328
329
330perl v5.8.8                       2004-04-06                  HTTP::Headers(3)
Impressum