|
HTTP::Headers - Class encapsulating HTTP Message headers |
HTTP::Headers - Class encapsulating HTTP Message headers
require HTTP::Headers; $h = new HTTP::Headers;
The HTTP::Headers class encapsulates HTTP-style message headers.
The headers consist of attribute-value pairs, which may be repeated,
and which are printed in a particular order.
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 = new HTTP::Headers
Date => 'Thu, 03 Feb 1994 00:00:00 GMT',
Content_Type => 'text/html; version=3.2',
Content_Base => 'http://www.sn.no/';
The header() method accepts multiple ($field => $value) pairs, so you
can update several fields with a single invocation.
The optional $value argument may be a scalar or a reference to a list of scalars. If the $value argument is undefined or not given, then the header is not modified.
The old value of the last of the $field values is returned. Multi-valued fields will be concatenated with ``,'' as separator in scalar context.
$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');
scan(\&doit)as_string([$endl])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 fieds. Long header
values are not folded.
The optional parameter specifies the line ending sequence to use. The
default is "\n". Embedded ``\n'' characters in the header will be
substitued with this line ending sequence.
$header->push_header(Accept => 'image/jpeg');
remove_header($field,...)
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.
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 ($h->last_modified < 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. 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 Norwegian and ``en-US'' for US-English.
$h->user_agent('Mozilla/1.2');
$h->from('Gisle Aas <aas@sn.no>');
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.
Copyright 1995-1998 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 |