HTTP::Headers - Class encapsulating HTTP Message headers |
HTTP::Headers - Class encapsulating HTTP Message headers
require HTTP::Headers; $h = HTTP::Headers->new;
$h->header('Content-Type' => 'text/plain'); # set $ct = $h->header('Content-Type'); # get $h->remove_header('Content-Type'); # delete
The HTTP::Headers
class encapsulates HTTP-style message headers.
The headers consist of attribute-value pairs also called fields, which
may be repeated, and which are printed in a particular order. The
field names are cases insensitive.
Instances of this class are usually created as member variables of the
HTTP::Request
and HTTP::Response
classes, internal to the
library.
The following methods are available:
HTTP::Headers
object. You might pass some initial
attribute-value pairs as parameters to the constructor. E.g.:
$h = HTTP::Headers->new( Date => 'Thu, 03 Feb 1994 00:00:00 GMT', Content_Type => 'text/html; version=3.2', Content_Base => 'http://www.perl.org/');
The constructor arguments are passed to the header
method which is
described below.
HTTP::Headers
object.
The header()
method accepts multiple ($field => $value) pairs, which
means that you can update several fields with a single invocation.
The $value argument may be a plain string or a reference to an array of strings for a multi-valued field. If the $value is undefined or not given, then that header field will remain unchanged.
The old value (or values) of the last of the header fields is returned.
If no such field exists undef
will be returned.
A multi-valued field will be returned as separate values in list context and will be concatenated with ``, '' as separator in scalar context. The HTTP spec (RFC 2616) promise that joining multiple values in this way will not change the semantic of a header field, but in practice there are cases like old-style Netscape cookies (see the HTTP::Cookies manpage) where ``,'' is used as part of the syntax of a single field value.
Examples:
$header->header(MIME_Version => '1.0', User_Agent => 'My-Web-Client/0.01'); $header->header(Accept => "text/html, text/plain, image/*"); $header->header(Accept => [qw(text/html text/plain image/*)]); @accepts = $header->header('Accept'); # get multiple values $accepts = $header->header('Accept'); # get values as a single string
As for the header()
method, the field name ($field) is not case
sensitive and '_' can be used as a replacement for '-'.
The $value argument may be a scalar or a reference to a list of scalars.
$header->push_header(Accept => 'image/jpeg'); $header->push_header(Accept => [map "image/$_", qw(gif png tiff)]);
The header field name ($field) is not case sensitive and '_' can be used as a replacement for '-'.
The $value argument may be a scalar or a reference to a list of scalars.
The header field names ($field) are not case sensitive and '_' can be used as a replacement for '-'.
The return value is the values of the fields removed. In scalar context the number of fields removed is returned.
Note that if you pass in multiple field names then it is generally not possible to tell which of the returned values belonged to which field.
Content-
falls
into this category, as well as Allow
, Expires
and
Last-Modified
. RFC 2616 denote these fields as Entity Header
Fields.
The return value is a new HTTP::Headers
object that contains the
removed headers only.
In scalar context return the number of distinct field names.
Any return values of the callback routine are ignored. The loop can
be broken by raising an exception (die
), but the caller of scan()
would have to trap the exception itself.
scan
method to build the string, the result
will use case as suggested by HTTP spec, and it will follow
recommended ``Good Practice'' of ordering the header fields. Long header
values are not folded.
The optional $eol parameter specifies the line ending sequence to use. The default is ``\n''. Embedded ``\n'' characters in header field values will be substituted with this line ending sequence.
The most frequently used headers can also be accessed through the
following convenience methods. These methods can both be used to read
and to set the value of a header. The header value is set if you pass
an argument to the method. The old header value is always returned.
If the given header did not exist then undef
is returned.
Methods that deal with dates/times always convert their value to system time (seconds since Jan 1, 1970) and they also expect this kind of value when the header value is set.
$h->date(time); # set current date
304 Not Modified
response instead of
the document itself.
# check if document is more than 1 hour old if (my $last_mod = $h->last_modified) { if ($last_mod < time - 60*60) { ... } }
$h->content_type('text/html');
The value returned will be converted to lower case, and potential parameters will be chopped off and returned as a separate value if in an array context. If there is no such header field, then the empty string is returned. This makes it safe to do the following:
if ($h->content_type eq 'text/html') { # we enter this place even if the real header value happens to # be 'TEXT/HTML; version=3.0' ... }
language(s)
of the intended audience for the message
content. The value is one or more language tags as defined by RFC
1766. Eg. ``no'' for some kind of Norwegian and ``en-US'' for English the
way it is written in the US.
$h->user_agent('Mozilla/1.2');
$h->from('King Kong <king@kong.com>');
This header is no longer part of the HTTP standard.
The ``Free On-line Dictionary of Computing'' as this to say about the word referer:
<World-Wide Web> A misspelling of "referrer" which somehow made it into the {HTTP} standard. A given {web page}'s referer (sic) is the {URL} of whatever web page contains the link that the user followed to the current page. Most browsers pass this information as part of a request.
(1998-10-19)
By popular demand referrer
exists as an alias for this method so you
can avoid this misspelling in your programs and still send the right
thing on the wire.
When setting the referrer, this method removes the fragment from the
given URI if it is present, as mandated by RFC2616. Note that
the removal does not happen automatically if using the header(),
push_header()
or init_header()
methods to set the referrer.
401 Unauthorized
response.
The field value consist of a challenge that indicates the
authentication scheme and parameters applicable to the requested URI.
407 Proxy Authentication Required
response.
When used to set the header value, it expects two arguments. E.g.:
$h->authorization_basic($uname, $password);
The method will croak if the $uname contains a colon ':'.
authorization_basic()
but will set the ``Proxy-Authorization''
header instead.
The header field name spelling is normally canonicalized including the
'_' to '-' translation. There are some application where this is not
appropriate. Prefixing field names with ':' allow you to force a
specific spelling. For example if you really want a header field name
to show up as foo_bar
instead of ``Foo-Bar'', you might set it like
this:
$h->header(":foo_bar" => 1);
These field names are returned with the ':' intact for $h->header_field_names and the $h->scan callback, but the colons do not show in $h->as_string.
In the argument list to the constructor or header()
method, the same
field name should not occur multiple times. The result of doing so,
it that only the last of these fields will be present in the header
after the call. All values ought to be kept.
Passing a value of undef
to header()
or any of the convenience
methods, does not delete that field. It ought to do that.
Copyright 1995-2004 Gisle Aas.
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
HTTP::Headers - Class encapsulating HTTP Message headers |