Http style response message
Response objects are returned by the request() method of the \*(C`LWP::UserAgent\*(C':
# ... $response = $ua->request($request) if ($response->is_success) { print $response->decoded_content; } else { print STDERR $response->status_line, "\n"; }
The \*(C`HTTP::Response\*(C' class encapsulates \s-1HTTP\s0 style responses. A response consists of a response line, some headers, and a content body. Note that the \s-1LWP\s0 library uses \s-1HTTP\s0 style responses even for non-HTTP protocol schemes. Instances of this class are usually created and returned by the request() method of an \*(C`LWP::UserAgent\*(C' object.
\*(C`HTTP::Response\*(C' is a subclass of \*(C`HTTP::Message\*(C' and therefore inherits its methods. The following additional methods are available: Constructs a new \*(C`HTTP::Response\*(C' object describing a response with response code $code and optional message $msg. The optional $header argument should be a reference to an \*(C`HTTP::Headers\*(C' object or a plain array reference of key/value pairs. The optional $content argument should be a string of bytes. The meanings of these arguments are described below. This constructs a new response object by parsing the given string. This is used to get/set the code attribute. The code is a 3 digit number that encode the overall outcome of an \s-1HTTP\s0 response. The \*(C`HTTP::Status\*(C' module provide constants that provide mnemonic names for the code attribute. This is used to get/set the message attribute. The message is a short human readable single line string that explains the response code. This is used to get/set header values and it is inherited from \*(C`HTTP::Headers\*(C' via \*(C`HTTP::Message\*(C'. See HTTP::Headers for details and other similar methods that can be used to access the headers. This is used to get/set the raw content and it is inherited from the \*(C`HTTP::Message\*(C' base class. See HTTP::Message for details and other methods that can be used to access the content. This will return the content after any \*(C`Content-Encoding\*(C' and charsets have been decoded. See HTTP::Message for details. This is used to get/set the request attribute. The request attribute is a reference to the the request that caused this response. It does not have to be the same request passed to the $ua->request() method, because there might have been redirects and authorization retries in between. This is used to get/set the previous attribute. The previous attribute is used to link together chains of responses. You get chains of responses if the first response is redirect or unauthorized. The value is \*(C`undef\*(C' if this is the first response in a chain. Note that the method $r->redirects is provided as a more convenient way to access the response chain. Returns the string \*(L"<code> <message>\*(R". If the message attribute is not set then the official name of <code> (see HTTP::Status) is substituted. Returns the base \s-1URI\s0 for this response. The return value will be a reference to a \s-1URI\s0 object. The base \s-1URI\s0 is obtained from one the following sources (in priority order):
Embedded in the document content, for instance <\s-1BASE\s0 HREF=\*(L"...\*(R"> in \s-1HTML\s0 documents.
A \*(L"Content-Base:\*(R" or a \*(L"Content-Location:\*(R" header in the response. For backwards compatibility with older \s-1HTTP\s0 implementations we will also look for the \*(L"Base:\*(R" header.
The \s-1URI\s0 used to request this response. This might not be the original \s-1URI\s0 that was passed to $ua->request() method, because we might have received some redirect responses first.
If none of these sources provide an absolute \s-1URI\s0, undef is returned. When the \s-1LWP\s0 protocol modules produce the HTTP::Response object, then any base \s-1URI\s0 embedded in the document (step 1) will already have initialized the \*(L"Content-Base:\*(R" header. This means that this method only performs the last 2 steps (the content is not always available either).
Returns a filename for this response. Note that doing sanity checks on the returned filename (eg. removing characters that cannot be used on the target filesystem where the filename would be used, and laundering it for security purposes) are the caller's responsibility; the only related thing done by this method is that it makes a simple attempt to return a plain filename with no preceding path segments. The filename is obtained from one the following sources (in priority order):
A \*(L"Content-Disposition:\*(R" header in the response. Proper decoding of \s-1RFC\s0 2047 encoded filenames requires the \*(C`MIME::QuotedPrint\*(C' (for \*(L"Q\*(R" encoding), \*(C`MIME::Base64\*(C' (for \*(L"B\*(R" encoding), and \*(C`Encode\*(C' modules.
A \*(L"Content-Location:\*(R" header in the response.
The \s-1URI\s0 used to request this response. This might not be the original \s-1URI\s0 that was passed to $ua->request() method, because we might have received some redirect responses first.
If a filename cannot be derived from any of these sources, undef is returned.
Returns a textual representation of the response. These methods indicate if the response was informational, successful, a redirection, or an error. See HTTP::Status for the meaning of these. Returns a string containing a complete \s-1HTML\s0 document indicating what error occurred. This method should only be called when $r->is_error is \s-1TRUE\s0. Returns the list of redirect responses that lead up to this response by following the $r->previous chain. The list order is oldest first. In scalar context return the number of redirect responses leading up to this one. Calculates the \*(L"current age\*(R" of the response as specified by \s-1RFC\s0 2616 section 13.2.3. The age of a response is the time since it was sent by the origin server. The returned value is a number representing the age in seconds. Calculates the \*(L"freshness lifetime\*(R" of the response as specified by \s-1RFC\s0 2616 section 13.2.4. The \*(L"freshness lifetime\*(R" is the length of time between the generation of a response and its expiration time. The returned value is the number of seconds until expiry. If the response does not contain an \*(L"Expires\*(R" or a \*(L"Cache-Control\*(R" header, then this function will apply some simple heuristic based on the \*(L"Last-Modified\*(R" header to determine a suitable lifetime. The following options might be passed to control the heuristics:
If passed as a \s-1FALSE\s0 value, don't apply heuristics and just return \*(C`undef\*(C' when \*(L"Expires\*(R" or \*(L"Cache-Control\*(R" is lacking. This number represent the fraction of the difference since the \*(L"Last-Modified\*(R" timestamp to make the expiry time. The default is 0.10, the suggested typical setting of 10% in \s-1RFC\s0 2616. This is the lower limit of the heuristic expiry age to use. The default is 60 (1 minute). This is the upper limit of the heuristic expiry age to use. The default is 86400 (24 hours). This is the expiry age to use when nothing else applies. The default is 3600 (1 hour) or \*(L"h_min\*(R" if greater.
Returns \s-1TRUE\s0 if the response is fresh, based on the values of freshness_lifetime() and current_age(). If the response is no longer fresh, then it has to be re-fetched or re-validated by the origin server. Options might be passed to control expiry heuristics, see the description of freshness_lifetime(). Returns the time (seconds since epoch) when this entity is no longer fresh. Options might be passed to control expiry heuristics, see the description of freshness_lifetime().
HTTP::Headers, HTTP::Message, HTTP::Status, HTTP::Request
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.