Chapter 5: The LWP Library- P2
HTTP::Response
Responses from a web server are described by HTTP::Response objects. If
LWP has problems fulfilling your request, it internally generates an
HTTP::Response object and fills in an appropriate response code. In the
context of web client programming, you'll usually get an HTTP::Response
object from LWP::UserAgent and LWP::RobotUA. If you plan to write
extensions to LWP or a web server or proxy server, you might use
HTTP::Response to generate your own responses.
$r = new HTTP::Response ($rc, [$msg, [$header, [$content]]])
In its simplest form, an HTTP::Response object can contain just a
response code. If you would like to specify a more detailed message
than "OK" or "Not found," you can specify a human-readable
description of the response code as the second parameter. As a third
parameter, you can pass a reference to an HTTP::Headers object to
specify the response headers. Finally, you can also include an entity-
body in the fourth parameter as a scalar.
$r->code([$code])
When invoked without any parameters, the code( ) method returns the
object's response code. When invoked with a status code as the first
parameter, code( ) defines the object's response to that value.
$r->is_info( )
Returns true when the response code is 100 through 199.
$r->is_success( )
Returns true when the response code is 200 through 299.
$r->is_redirect( )
Returns true when the response code is 300 through 399.
$r->is_error( )
Returns true when the response code is 400 through 599. When an
error occurs, you might want to use error_as_HTML( ) to generate an
Appends $data to the end of the object's current entity-body.
$r->error_as_HTML( )
When is_error( ) is true, this method returns an HTML explanation of
what happened. LWP usually returns a plain text explanation.
$r->base( )
Returns the base of the request. If the response was hypertext, any
links from the hypertext should be relative to the location specified by
this method. LWP looks for the BASE tag in HTML and Content-
base/Content-location HTTP headers for a base
specification. If a base was not explicitly defined by the server, LWP
uses the requesting URL as the base.
$r->as_string( )
This returns a text version of the response. Useful for debugging
purposes. For example,
use HTTP::Response;
use HTTP::Status;
$response = new HTTP::Response(RC_OK, 'all is
fine');
$response->header('content-length' => 2);
$response->header('content-type' =>
'text/plain');
$response->content('hi');
print $response->as_string( );
would look like this:
--- HTTP::Response=HASH(0xc8548) ---
RC: 200 (OK)
Message: all is fine
header. To define a new header value, invoke header( ) with an
associative array of header => value pairs, where the value is a scalar
or reference to an array. For example, to define the Content-type
header, one would do this:
$h->header('content-type' => 'text/plain')
$h->push_header($field, $val)
Appends the second parameter to the header specified by the first
parameter. A subsequent call to header( ) would return an array. For
example:
$h->push_header(Accept => 'image/jpeg');
$h->remove_header($field,...)
Removes the header specified in the parameter(s) and the header's
associated value.
HTTP::Status
This module provides functions to determine the type of a response code. It
also exports a list of mnemonics that can be used by the programmer to refer
to a status code.
is_info( )
Returns true when the response code is 100 through 199.
is_success( )
Returns true when the response code is 200 through 299.
is_redirect( )
Returns true when the response code is 300 through 399.
is_client_error( )
Returns true when the response code is 400 through 499.
is_server_error( )
Returns true when the response code is 500 through 599.
is_error( )
RC_REQUEST_URI_TOO_LARGE
(414)
RC_MOVED_TEMPORARILY
(302)
RC_UNSUPPORTED_MEDIA_TYP
E (415)
RC_SEE_OTHER (303)
RC_INTERNAL_SERVER_ERROR
(500)
RC_NOT_MODIFIED (304) RC_NOT_IMPLEMENTED (501)
RC_USE_PROXY (305) RC_BAD_GATEWAY (502)
RC_BAD_REQUEST (400)
RC_SERVICE_UNAVAILABLE
(503)
RC_UNAUTHORIZED (401) RC_GATEWAY_TIMEOUT (504)
RC_PAYMENT_REQUIRED (402)
RC_HTTP_VERSION_NOT_SUPP
ORTED (505)
RC_FORBIDDEN (403)
See the section "Server Response Codes" in Chapter 3 for more information.
HTTP::Date
The HTTP::Date module is useful when you want to process a date string.
time2str([$time])
Given the number of seconds since machine epoch,[3] this function
generates the equivalent time as specified in RFC 1123, which is the
recommended time format used in HTTP. When invoked with no
parameter, the current time is used.
str2time($str [, $zone])
Converts the time specified as a string in the first parameter into the
number of seconds since epoch. This function recognizes a wide