.\" -*- mode: troff; coding: utf-8 -*-
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45)
.\"
.\" 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
..
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>.
.ie n \{\
. ds C` ""
. ds C' ""
'br\}
.el\{\
. 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 "LWP 3"
.TH LWP 3 2024-09-01 "perl v5.40.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
LWP \- The World\-Wide Web library for Perl
.SH SYNOPSIS
.IX Header "SYNOPSIS"
.Vb 2
\& use LWP;
\& print "This is libwww\-perl\-$LWP::VERSION\en";
.Ve
.SH DESCRIPTION
.IX Header "DESCRIPTION"
The libwww-perl collection is a set of Perl modules which provides a
simple and consistent application programming interface (API) to the
World-Wide Web. The main focus of the library is to provide classes
and functions that allow you to write WWW clients. The library also
contain modules that are of more general use and even classes that
help you implement simple HTTP servers.
.PP
Most modules in this library provide an object oriented API. The user
agent, requests sent and responses received from the WWW server are
all represented by objects. This makes a simple and powerful
interface to these services. The interface is easy to extend
and customize for your own needs.
.PP
The main features of the library are:
.IP \(bu 3
Contains various reusable components (modules) that can be
used separately or together.
.IP \(bu 3
Provides an object oriented model of HTTP-style communication. Within
this framework we currently support access to \f(CW\*(C`http\*(C'\fR, \f(CW\*(C`https\*(C'\fR, \f(CW\*(C`gopher\*(C'\fR,
\&\f(CW\*(C`ftp\*(C'\fR, \f(CW\*(C`news\*(C'\fR, \f(CW\*(C`file\*(C'\fR, and \f(CW\*(C`mailto\*(C'\fR resources.
.IP \(bu 3
Provides a full object oriented interface or
a very simple procedural interface.
.IP \(bu 3
Supports the basic and digest authorization schemes.
.IP \(bu 3
Supports transparent redirect handling.
.IP \(bu 3
Supports access through proxy servers.
.IP \(bu 3
Provides parser for \fIrobots.txt\fR files and a framework for constructing robots.
.IP \(bu 3
Supports parsing of HTML forms.
.IP \(bu 3
Implements HTTP content negotiation algorithm that can
be used both in protocol modules and in server scripts (like CGI
scripts).
.IP \(bu 3
Supports HTTP cookies.
.IP \(bu 3
Some simple command line clients, for instance \f(CW\*(C`lwp\-request\*(C'\fR and \f(CW\*(C`lwp\-download\*(C'\fR.
.SH "HTTP STYLE COMMUNICATION"
.IX Header "HTTP STYLE COMMUNICATION"
The libwww-perl library is based on HTTP style communication. This
section tries to describe what that means.
.PP
Let us start with this quote from the HTTP specification document
:
.IP \(bu 3
The HTTP protocol is based on a request/response paradigm. A client
establishes a connection with a server and sends a request to the
server in the form of a request method, URI, and protocol version,
followed by a MIME-like message containing request modifiers, client
information, and possible body content. The server responds with a
status line, including the message's protocol version and a success or
error code, followed by a MIME-like message containing server
information, entity meta-information, and possible body content.
.PP
What this means to libwww-perl is that communication always take place
through these steps: First a \fIrequest\fR object is created and
configured. This object is then passed to a server and we get a
\&\fIresponse\fR object in return that we can examine. A request is always
independent of any previous requests, i.e. the service is stateless.
The same simple model is used for any kind of service we want to
access.
.PP
For example, if we want to fetch a document from a remote file server,
then we send it a request that contains a name for that document and
the response will contain the document itself. If we access a search
engine, then the content of the request will contain the query
parameters and the response will contain the query result. If we want
to send a mail message to somebody then we send a request object which
contains our message to the mail server and the response object will
contain an acknowledgment that tells us that the message has been
accepted and will be forwarded to the recipient(s).
.PP
It is as simple as that!
.SS "The Request Object"
.IX Subsection "The Request Object"
The libwww-perl request object has the class name HTTP::Request.
The fact that the class name uses \f(CW\*(C`HTTP::\*(C'\fR as a
prefix only implies that we use the HTTP model of communication. It
does not limit the kind of services we can try to pass this \fIrequest\fR
to. For instance, we will send HTTP::Requests both to ftp and
gopher servers, as well as to the local file system.
.PP
The main attributes of the request objects are:
.IP \(bu 3
\&\fBmethod\fR is a short string that tells what kind of
request this is. The most common methods are \fBGET\fR, \fBPUT\fR,
\&\fBPOST\fR and \fBHEAD\fR.
.IP \(bu 3
\&\fBuri\fR is a string denoting the protocol, server and
the name of the "document" we want to access. The \fBuri\fR might
also encode various other parameters.
.IP \(bu 3
\&\fBheaders\fR contains additional information about the
request and can also used to describe the content. The headers
are a set of keyword/value pairs.
.IP \(bu 3
\&\fBcontent\fR is an arbitrary amount of data.
.SS "The Response Object"
.IX Subsection "The Response Object"
The libwww-perl response object has the class name HTTP::Response.
The main attributes of objects of this class are:
.IP \(bu 3
\&\fBcode\fR is a numerical value that indicates the overall
outcome of the request.
.IP \(bu 3
\&\fBmessage\fR is a short, human readable string that
corresponds to the \fIcode\fR.
.IP \(bu 3
\&\fBheaders\fR contains additional information about the
response and describe the content.
.IP \(bu 3
\&\fBcontent\fR is an arbitrary amount of data.
.PP
Since we don't want to handle all possible \fIcode\fR values directly in
our programs, a libwww-perl response object has methods that can be
used to query what kind of response this is. The most commonly used
response classification methods are:
.IP \fBis_success()\fR 3
.IX Item "is_success()"
The request was successfully received, understood or accepted.
.IP \fBis_error()\fR 3
.IX Item "is_error()"
The request failed. The server or the resource might not be
available, access to the resource might be denied or other things might
have failed for some reason.
.SS "The User Agent"
.IX Subsection "The User Agent"
Let us assume that we have created a \fIrequest\fR object. What do we
actually do with it in order to receive a \fIresponse\fR?
.PP
The answer is that you pass it to a \fIuser agent\fR object and this
object takes care of all the things that need to be done
(like low-level communication and error handling) and returns
a \fIresponse\fR object. The user agent represents your
application on the network and provides you with an interface that
can accept \fIrequests\fR and return \fIresponses\fR.
.PP
The user agent is an interface layer between
your application code and the network. Through this interface you are
able to access the various servers on the network.
.PP
The class name for the user agent is LWP::UserAgent. Every
libwww-perl application that wants to communicate should create at
least one object of this class. The main method provided by this
object is \fBrequest()\fR. This method takes an HTTP::Request object as
argument and (eventually) returns a HTTP::Response object.
.PP
The user agent has many other attributes that let you
configure how it will interact with the network and with your
application.
.IP \(bu 3
\&\fBtimeout\fR specifies how much time we give remote servers to
respond before the library disconnects and creates an
internal \fItimeout\fR response.
.IP \(bu 3
\&\fBagent\fR specifies the name that your application uses when it
presents itself on the network.
.IP \(bu 3
\&\fBfrom\fR can be set to the e\-mail address of the person
responsible for running the application. If this is set, then the
address will be sent to the servers with every request.
.IP \(bu 3
\&\fBparse_head\fR specifies whether we should initialize response
headers from the \f(CW\*(C`
\*(C'\fR section of HTML documents.
.IP \(bu 3
\&\fBproxy\fR and \fBno_proxy\fR specify if and when to go through
a proxy server.
.IP \(bu 3
\&\fBcredentials\fR provides a way to set up user names and
passwords needed to access certain services.
.PP
Many applications want even more control over how they interact
with the network and they get this by sub-classing
LWP::UserAgent. The library includes a
sub-class, LWP::RobotUA, for robot applications.
.SS "An Example"
.IX Subsection "An Example"
This example shows how the user agent, a request and a response are
represented in actual perl code:
.PP
.Vb 4
\& # Create a user agent object
\& use LWP::UserAgent;
\& my $ua = LWP::UserAgent\->new;
\& $ua\->agent("MyApp/0.1 ");
\&
\& # Create a request
\& my $req = HTTP::Request\->new(POST => \*(Aqhttp://search.cpan.org/search\*(Aq);
\& $req\->content_type(\*(Aqapplication/x\-www\-form\-urlencoded\*(Aq);
\& $req\->content(\*(Aqquery=libwww\-perl&mode=dist\*(Aq);
\&
\& # Pass request to the user agent and get a response back
\& my $res = $ua\->request($req);
\&
\& # Check the outcome of the response
\& if ($res\->is_success) {
\& print $res\->content;
\& }
\& else {
\& print $res\->status_line, "\en";
\& }
.Ve
.PP
The \f(CW$ua\fR is created once when the application starts up. New request
objects should normally created for each request sent.
.SH "NETWORK SUPPORT"
.IX Header "NETWORK SUPPORT"
This section discusses the various protocol schemes and
the HTTP style methods that headers may be used for each.
.PP
For all requests, a "User-Agent" header is added and initialized from
the \f(CW\*(C`$ua\->agent\*(C'\fR attribute before the request is handed to the network
layer. In the same way, a "From" header is initialized from the
\&\f(CW$ua\fR\->from attribute.
.PP
For all responses, the library adds a header called "Client-Date".
This header holds the time when the response was received by
your application. The format and semantics of the header are the
same as the server created "Date" header. You may also encounter other
"Client-XXX" headers. They are all generated by the library
internally and are not received from the servers.
.SS "HTTP Requests"
.IX Subsection "HTTP Requests"
HTTP requests are just handed off to an HTTP server and it
decides what happens. Few servers implement methods beside the usual
"GET", "HEAD", "POST" and "PUT", but CGI-scripts may implement
any method they like.
.PP
If the server is not available then the library will generate an
internal error response.
.PP
The library automatically adds a "Host" and a "Content-Length" header
to the HTTP request before it is sent over the network.
.PP
For a GET request you might want to add an "If-Modified-Since" or
"If-None-Match" header to make the request conditional.
.PP
For a POST request you should add the "Content-Type" header. When you
try to emulate HTML