.\" Automatically generated by Pod::Man 4.14 (Pod::Simple 3.43) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" ======================================================================== .\" .IX Title "HTTP::Headers 3pm" .TH HTTP::Headers 3pm "2023-10-29" "perl v5.36.0" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" HTTP::Headers \- Class encapsulating HTTP Message headers .SH "VERSION" .IX Header "VERSION" version 6.45 .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 2 \& require HTTP::Headers; \& $h = HTTP::Headers\->new; \& \& $h\->header(\*(AqContent\-Type\*(Aq => \*(Aqtext/plain\*(Aq); # set \& $ct = $h\->header(\*(AqContent\-Type\*(Aq); # get \& $h\->remove_header(\*(AqContent\-Type\*(Aq); # delete .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" The \f(CW\*(C`HTTP::Headers\*(C'\fR 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. .PP Instances of this class are usually created as member variables of the \&\f(CW\*(C`HTTP::Request\*(C'\fR and \f(CW\*(C`HTTP::Response\*(C'\fR classes, internal to the library. .PP The following methods are available: .ie n .IP "$h = HTTP::Headers\->new" 4 .el .IP "\f(CW$h\fR = HTTP::Headers\->new" 4 .IX Item "$h = HTTP::Headers->new" Constructs a new \f(CW\*(C`HTTP::Headers\*(C'\fR object. You might pass some initial attribute-value pairs as parameters to the constructor. \fIE.g.\fR: .Sp .Vb 4 \& $h = HTTP::Headers\->new( \& Date => \*(AqThu, 03 Feb 1994 00:00:00 GMT\*(Aq, \& Content_Type => \*(Aqtext/html; version=3.2\*(Aq, \& Content_Base => \*(Aqhttp://www.perl.org/\*(Aq); .Ve .Sp The constructor arguments are passed to the \f(CW\*(C`header\*(C'\fR method which is described below. .ie n .IP "$h\->clone" 4 .el .IP "\f(CW$h\fR\->clone" 4 .IX Item "$h->clone" Returns a copy of this \f(CW\*(C`HTTP::Headers\*(C'\fR object. .ie n .IP "$h\->header( $field )" 4 .el .IP "\f(CW$h\fR\->header( \f(CW$field\fR )" 4 .IX Item "$h->header( $field )" .PD 0 .ie n .IP "$h\->header( $field => $value )" 4 .el .IP "\f(CW$h\fR\->header( \f(CW$field\fR => \f(CW$value\fR )" 4 .IX Item "$h->header( $field => $value )" .ie n .IP "$h\->header( $f1 => $v1, $f2 => $v2, ... )" 4 .el .IP "\f(CW$h\fR\->header( \f(CW$f1\fR => \f(CW$v1\fR, \f(CW$f2\fR => \f(CW$v2\fR, ... )" 4 .IX Item "$h->header( $f1 => $v1, $f2 => $v2, ... )" .PD Get or set the value of one or more header fields. The header field name ($field) is not case sensitive. To make the life easier for perl users who wants to avoid quoting before the => operator, you can use \&'_' as a replacement for '\-' in header names. .Sp The \fBheader()\fR method accepts multiple ($field => \f(CW$value\fR) pairs, which means that you can update several fields with a single invocation. .Sp The \f(CW$value\fR argument may be a plain string or a reference to an array of strings for a multi-valued field. If the \f(CW$value\fR is provided as \&\f(CW\*(C`undef\*(C'\fR then the field is removed. If the \f(CW$value\fR is not given, then that header field will remain unchanged. In addition to being a string, \&\f(CW$value\fR may be something that stringifies. .Sp The old value (or values) of the last of the header fields is returned. If no such field exists \f(CW\*(C`undef\*(C'\fR will be returned. .Sp A multi-valued field will be returned as separate values in list context and will be concatenated with \*(L", \*(R" as separator in scalar context. The \s-1HTTP\s0 spec (\s-1RFC 2616\s0) promises 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 HTTP::Cookies) where \*(L",\*(R" is used as part of the syntax of a single field value. .Sp Examples: .Sp .Vb 6 \& $header\->header(MIME_Version => \*(Aq1.0\*(Aq, \& User_Agent => \*(AqMy\-Web\-Client/0.01\*(Aq); \& $header\->header(Accept => "text/html, text/plain, image/*"); \& $header\->header(Accept => [qw(text/html text/plain image/*)]); \& @accepts = $header\->header(\*(AqAccept\*(Aq); # get multiple values \& $accepts = $header\->header(\*(AqAccept\*(Aq); # get values as a single string .Ve .ie n .IP "$h\->push_header( $field => $value )" 4 .el .IP "\f(CW$h\fR\->push_header( \f(CW$field\fR => \f(CW$value\fR )" 4 .IX Item "$h->push_header( $field => $value )" .PD 0 .ie n .IP "$h\->push_header( $f1 => $v1, $f2 => $v2, ... )" 4 .el .IP "\f(CW$h\fR\->push_header( \f(CW$f1\fR => \f(CW$v1\fR, \f(CW$f2\fR => \f(CW$v2\fR, ... )" 4 .IX Item "$h->push_header( $f1 => $v1, $f2 => $v2, ... )" .PD Add a new field value for the specified header field. Previous values for the same field are retained. .Sp As for the \fBheader()\fR method, the field name ($field) is not case sensitive and '_' can be used as a replacement for '\-'. .Sp The \f(CW$value\fR argument may be a scalar or a reference to a list of scalars. .Sp .Vb 2 \& $header\->push_header(Accept => \*(Aqimage/jpeg\*(Aq); \& $header\->push_header(Accept => [map "image/$_", qw(gif png tiff)]); .Ve .ie n .IP "$h\->init_header( $field => $value )" 4 .el .IP "\f(CW$h\fR\->init_header( \f(CW$field\fR => \f(CW$value\fR )" 4 .IX Item "$h->init_header( $field => $value )" Set the specified header to the given value, but only if no previous value for that field is set. .Sp The header field name ($field) is not case sensitive and '_' can be used as a replacement for '\-'. .Sp The \f(CW$value\fR argument may be a scalar or a reference to a list of scalars. .ie n .IP "$h\->remove_header( $field, ... )" 4 .el .IP "\f(CW$h\fR\->remove_header( \f(CW$field\fR, ... )" 4 .IX Item "$h->remove_header( $field, ... )" This function removes the header fields with the specified names. .Sp The header field names ($field) are not case sensitive and '_' can be used as a replacement for '\-'. .Sp The return value is the values of the fields removed. In scalar context the number of fields removed is returned. .Sp 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. .ie n .IP "$h\->remove_content_headers" 4 .el .IP "\f(CW$h\fR\->remove_content_headers" 4 .IX Item "$h->remove_content_headers" This will remove all the header fields used to describe the content of a message. All header field names prefixed with \f(CW\*(C`Content\-\*(C'\fR fall into this category, as well as \f(CW\*(C`Allow\*(C'\fR, \f(CW\*(C`Expires\*(C'\fR and \&\f(CW\*(C`Last\-Modified\*(C'\fR. \s-1RFC 2616\s0 denotes these fields as \fIEntity Header Fields\fR. .Sp The return value is a new \f(CW\*(C`HTTP::Headers\*(C'\fR object that contains the removed headers only. .ie n .IP "$h\->clear" 4 .el .IP "\f(CW$h\fR\->clear" 4 .IX Item "$h->clear" This will remove all header fields. .ie n .IP "$h\->header_field_names" 4 .el .IP "\f(CW$h\fR\->header_field_names" 4 .IX Item "$h->header_field_names" Returns the list of distinct names for the fields present in the header. The field names have case as suggested by \s-1HTTP\s0 spec, and the names are returned in the recommended \*(L"Good Practice\*(R" order. .Sp In scalar context return the number of distinct field names. .ie n .IP "$h\->scan( \e&process_header_field )" 4 .el .IP "\f(CW$h\fR\->scan( \e&process_header_field )" 4 .IX Item "$h->scan( &process_header_field )" Apply a subroutine to each header field in turn. The callback routine is called with two parameters; the name of the field and a single value (a string). If a header field is multi-valued, then the routine is called once for each value. The field name passed to the callback routine has case as suggested by \s-1HTTP\s0 spec, and the headers will be visited in the recommended \*(L"Good Practice\*(R" order. .Sp Any return values of the callback routine are ignored. The loop can be broken by raising an exception (\f(CW\*(C`die\*(C'\fR), but the caller of \fBscan()\fR would have to trap the exception itself. .ie n .IP "$h\->\fBflatten()\fR" 4 .el .IP "\f(CW$h\fR\->\fBflatten()\fR" 4 .IX Item "$h->flatten()" Returns the list of pairs of keys and values. .ie n .IP "$h\->as_string" 4 .el .IP "\f(CW$h\fR\->as_string" 4 .IX Item "$h->as_string" .PD 0 .ie n .IP "$h\->as_string( $eol )" 4 .el .IP "\f(CW$h\fR\->as_string( \f(CW$eol\fR )" 4 .IX Item "$h->as_string( $eol )" .PD Return the header fields as a formatted \s-1MIME\s0 header. Since it internally uses the \f(CW\*(C`scan\*(C'\fR method to build the string, the result will use case as suggested by \s-1HTTP\s0 spec, and it will follow recommended \*(L"Good Practice\*(R" of ordering the header fields. Long header values are not folded. .Sp The optional \f(CW$eol\fR parameter specifies the line ending sequence to use. The default is \*(L"\en\*(R". Embedded \*(L"\en\*(R" characters in header field values will be substituted with this line ending sequence. .SH "CONVENIENCE METHODS" .IX Header "CONVENIENCE METHODS" The most frequently used headers can also be accessed through the following convenience methods. Most of 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 \f(CW\*(C`undef\*(C'\fR is returned. .PP 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. .ie n .IP "$h\->date" 4 .el .IP "\f(CW$h\fR\->date" 4 .IX Item "$h->date" This header represents the date and time at which the message was originated. \fIE.g.\fR: .Sp .Vb 1 \& $h\->date(time); # set current date .Ve .ie n .IP "$h\->expires" 4 .el .IP "\f(CW$h\fR\->expires" 4 .IX Item "$h->expires" This header gives the date and time after which the entity should be considered stale. .ie n .IP "$h\->if_modified_since" 4 .el .IP "\f(CW$h\fR\->if_modified_since" 4 .IX Item "$h->if_modified_since" .PD 0 .ie n .IP "$h\->if_unmodified_since" 4 .el .IP "\f(CW$h\fR\->if_unmodified_since" 4 .IX Item "$h->if_unmodified_since" .PD These header fields are used to make a request conditional. If the requested resource has (or has not) been modified since the time specified in this field, then the server will return a \f(CW\*(C`304 Not Modified\*(C'\fR response instead of the document itself. .ie n .IP "$h\->last_modified" 4 .el .IP "\f(CW$h\fR\->last_modified" 4 .IX Item "$h->last_modified" This header indicates the date and time at which the resource was last modified. \fIE.g.\fR: .Sp .Vb 6 \& # check if document is more than 1 hour old \& if (my $last_mod = $h\->last_modified) { \& if ($last_mod < time \- 60*60) { \& ... \& } \& } .Ve .ie n .IP "$h\->content_type" 4 .el .IP "\f(CW$h\fR\->content_type" 4 .IX Item "$h->content_type" The Content-Type header field indicates the media type of the message content. \fIE.g.\fR: .Sp .Vb 1 \& $h\->content_type(\*(Aqtext/html\*(Aq); .Ve .Sp 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: .Sp .Vb 5 \& if ($h\->content_type eq \*(Aqtext/html\*(Aq) { \& # we enter this place even if the real header value happens to \& # be \*(AqTEXT/HTML; version=3.0\*(Aq \& ... \& } .Ve .ie n .IP "$h\->content_type_charset" 4 .el .IP "\f(CW$h\fR\->content_type_charset" 4 .IX Item "$h->content_type_charset" Returns the upper-cased charset specified in the Content-Type header. In list context return the lower-cased bare content type followed by the upper-cased charset. Both values will be \f(CW\*(C`undef\*(C'\fR if not specified in the header. .ie n .IP "$h\->content_is_text" 4 .el .IP "\f(CW$h\fR\->content_is_text" 4 .IX Item "$h->content_is_text" Returns \s-1TRUE\s0 if the Content-Type header field indicate that the content is textual. .ie n .IP "$h\->content_is_html" 4 .el .IP "\f(CW$h\fR\->content_is_html" 4 .IX Item "$h->content_is_html" Returns \s-1TRUE\s0 if the Content-Type header field indicate that the content is some kind of \s-1HTML\s0 (including \s-1XHTML\s0). This method can't be used to set Content-Type. .ie n .IP "$h\->content_is_xhtml" 4 .el .IP "\f(CW$h\fR\->content_is_xhtml" 4 .IX Item "$h->content_is_xhtml" Returns \s-1TRUE\s0 if the Content-Type header field indicate that the content is \s-1XHTML.\s0 This method can't be used to set Content-Type. .ie n .IP "$h\->content_is_xml" 4 .el .IP "\f(CW$h\fR\->content_is_xml" 4 .IX Item "$h->content_is_xml" Returns \s-1TRUE\s0 if the Content-Type header field indicate that the content is \s-1XML.\s0 This method can't be used to set Content-Type. .ie n .IP "$h\->content_encoding" 4 .el .IP "\f(CW$h\fR\->content_encoding" 4 .IX Item "$h->content_encoding" The Content-Encoding header field is used as a modifier to the media type. When present, its value indicates what additional encoding mechanism has been applied to the resource. .ie n .IP "$h\->content_length" 4 .el .IP "\f(CW$h\fR\->content_length" 4 .IX Item "$h->content_length" A decimal number indicating the size in bytes of the message content. .ie n .IP "$h\->content_language" 4 .el .IP "\f(CW$h\fR\->content_language" 4 .IX Item "$h->content_language" The natural language(s) of the intended audience for the message content. The value is one or more language tags as defined by \s-1RFC 1766.\s0 Eg. \*(L"no\*(R" for some kind of Norwegian and \*(L"en-US\*(R" for English the way it is written in the \s-1US.\s0 .ie n .IP "$h\->title" 4 .el .IP "\f(CW$h\fR\->title" 4 .IX Item "$h->title" The title of the document. In libwww-perl this header will be initialized automatically from the <\s-1TITLE\s0>... element of \s-1HTML\s0 documents. \fIThis header is no longer part of the \s-1HTTP\s0 standard.\fR .ie n .IP "$h\->user_agent" 4 .el .IP "\f(CW$h\fR\->user_agent" 4 .IX Item "$h->user_agent" This header field is used in request messages and contains information about the user agent originating the request. \fIE.g.\fR: .Sp .Vb 1 \& $h\->user_agent(\*(AqMozilla/5.0 (compatible; MSIE 7.0; Windows NT 6.0)\*(Aq); .Ve .ie n .IP "$h\->server" 4 .el .IP "\f(CW$h\fR\->server" 4 .IX Item "$h->server" The server header field contains information about the software being used by the originating server program handling the request. .ie n .IP "$h\->from" 4 .el .IP "\f(CW$h\fR\->from" 4 .IX Item "$h->from" This header should contain an Internet e\-mail address for the human user who controls the requesting user agent. The address should be machine-usable, as defined by \s-1RFC822.\s0 E.g.: .Sp .Vb 1 \& $h\->from(\*(AqKing Kong \*(Aq); .Ve .Sp \&\fIThis header is no longer part of the \s-1HTTP\s0 standard.\fR .ie n .IP "$h\->referer" 4 .el .IP "\f(CW$h\fR\->referer" 4 .IX Item "$h->referer" Used to specify the address (\s-1URI\s0) of the document from which the requested resource address was obtained. .Sp The \*(L"Free On-line Dictionary of Computing\*(R" as this to say about the word \fIreferer\fR: .Sp .Vb 6 \& A misspelling of "referrer" which \& somehow made it into the {HTTP} standard. A given {web \& page}\*(Aqs 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) .Ve .Sp By popular demand \f(CW\*(C`referrer\*(C'\fR 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. .Sp When setting the referrer, this method removes the fragment from the given \s-1URI\s0 if it is present, as mandated by \s-1RFC2616.\s0 Note that the removal does \fInot\fR happen automatically if using the \fBheader()\fR, \&\fBpush_header()\fR or \fBinit_header()\fR methods to set the referrer. .ie n .IP "$h\->www_authenticate" 4 .el .IP "\f(CW$h\fR\->www_authenticate" 4 .IX Item "$h->www_authenticate" This header must be included as part of a \f(CW\*(C`401 Unauthorized\*(C'\fR response. The field value consist of a challenge that indicates the authentication scheme and parameters applicable to the requested \s-1URI.\s0 .ie n .IP "$h\->proxy_authenticate" 4 .el .IP "\f(CW$h\fR\->proxy_authenticate" 4 .IX Item "$h->proxy_authenticate" This header must be included in a \f(CW\*(C`407 Proxy Authentication Required\*(C'\fR response. .ie n .IP "$h\->authorization" 4 .el .IP "\f(CW$h\fR\->authorization" 4 .IX Item "$h->authorization" .PD 0 .ie n .IP "$h\->proxy_authorization" 4 .el .IP "\f(CW$h\fR\->proxy_authorization" 4 .IX Item "$h->proxy_authorization" .PD A user agent that wishes to authenticate itself with a server or a proxy, may do so by including these headers. .ie n .IP "$h\->authorization_basic" 4 .el .IP "\f(CW$h\fR\->authorization_basic" 4 .IX Item "$h->authorization_basic" This method is used to get or set an authorization header that use the \&\*(L"Basic Authentication Scheme\*(R". In array context it will return two values; the user name and the password. In scalar context it will return \fI\*(L"uname:password\*(R"\fR as a single string value. .Sp When used to set the header value, it expects two arguments. \fIE.g.\fR: .Sp .Vb 1 \& $h\->authorization_basic($uname, $password); .Ve .Sp The method will croak if the \f(CW$uname\fR contains a colon ':'. .ie n .IP "$h\->proxy_authorization_basic" 4 .el .IP "\f(CW$h\fR\->proxy_authorization_basic" 4 .IX Item "$h->proxy_authorization_basic" Same as \fBauthorization_basic()\fR but will set the \*(L"Proxy-Authorization\*(R" header instead. .SH "NON-CANONICALIZED FIELD NAMES" .IX Header "NON-CANONICALIZED FIELD NAMES" 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 \f(CW\*(C`foo_bar\*(C'\fR instead of \*(L"Foo-Bar\*(R", you might set it like this: .PP .Vb 1 \& $h\->header(":foo_bar" => 1); .Ve .PP These field names are returned with the ':' intact for \&\f(CW$h\fR\->header_field_names and the \f(CW$h\fR\->scan callback, but the colons do not show in \f(CW$h\fR\->as_string. .SH "AUTHOR" .IX Header "AUTHOR" Gisle Aas .SH "COPYRIGHT AND LICENSE" .IX Header "COPYRIGHT AND LICENSE" This software is copyright (c) 1994 by Gisle Aas. .PP This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.