@node LWP, LWP/Debug, IniConf, Module List
@unnumbered Library for WWW access in Perl


@unnumberedsec NAME

LWP - Library for WWW access in Perl

@unnumberedsec DESCRIPTION

Libwww-perl is a collection of Perl modules which provides a simple
and consistent 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, thus libwww-perl said to be a WWW
client library. The library also contain modules that are of more
general use.

The main architecture of the library is object oriented.  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 should be easy to extend
and customize for your needs.

The main features of the library are:

@itemize @bullet
@item 
Contains various reusable components (modules) that can be
used separately or together.

@item 
Provides an object oriented model of HTTP-style communication.  Within
this framework we currently support access to http, gopher, ftp, news,
file, and mailto resources.

@item 
The library be used through the full object oriented interface or
through a very simple procedural interface.

@item 
Support the basic and digest authorization schemes.

@item 
Transparent redirect handling.

@item 
Supports access through proxy servers.

@item 
URL handling (both absolute and relative URLs are supported).

@item 
A parser for @file{robots.txt} files and a framework for constructing robots.

@item 
An experimental HTML parser and formatters (for PostScript and plain
text).

@item 
The library can cooperate with Tk.  A simple Tk-based GUI browser
called @'tkweb@' is distributed with the Tk extension for perl.

@item 
An implementation of the HTTP content negotiation algorithm that can
be used both in protocol modules and in server scripts (like CGI
scripts).

@item 
A simple command line client application called @code{lwp-request}.

@end itemize
@unnumberedsec HTTP STYLE COMMUNICATION

The libwww-perl library is based on HTTP style communication. This
section try to describe what that means.

Let us start with this quote from the HTTP specification document
<URL:http://www.w3.org/pub/WWW/Protocols/>:

@table @asis
@item 
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.

@end table
What this means to libwww-perl is that communication always take place
through these steps: First a @emph{request} object is created and
configured. This object is then passed to a server and we get a
@emph{response} 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.

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).

It is as simple as that!

@unnumberedsubsec The Request Object

The request object has the class name @code{HTTP::Request} in
libwww-perl. The fact that the class name use @code{HTTP::} as a name
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 @emph{request}
to.  For instance, we will send @code{HTTP::Request}s both to ftp and
gopher servers, as well as to the local file system.

The main attributes of the request objects are:

@itemize @bullet
@item 
The @strong{method} is a short string that tells what kind of
request this is.  The most used methods are GET, @strong{PUT},
POST and HEAD.

@item 
The url is a string denoting the protocol, server and
the name of the "document" we want to access.  The url might
also encode various other parameters.

@item 
The @strong{headers} contain additional information about the
request and can also used to describe the content.  The headers
is a set of keyword/value pairs.

@item 
The @strong{content} is an arbitrary amount of data.

@end itemize
@unnumberedsubsec The Response Object

The request object has the class name @code{HTTP::Response} in
libwww-perl.  The main attributes of objects of this class are:

@itemize @bullet
@item 
The code is a numerical value that encode the overall
outcome of the request.

@item 
The message is a short (human readable) string that
corresponds to the code.

@item 
The @strong{headers} contain additional information about the
response and they also describe the content.

@item 
The @strong{content} is an arbitrary amount of data.

@end itemize
Since we don@'t want to handle all possible code values directly in
our programs, the libwww-perl response object have methods that can be
used to query what kind of response this is.  The most commonly used
response classification methods are:

@table @asis
@item is_success()
The request was was successfully received, understood or accepted.

@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.

@end table
@unnumberedsubsec The User Agent

Let us assume that we have created a @emph{request} object. What do we
actually do with it in order to receive a @emph{response}?

The answer is that you pass it on to a @emph{user agent} object and this
object will take care of all the things that need to be done
(low-level communication and error handling). The user agent will give
you back a @emph{response} object. The user agent represents your
application on the network and it provides you with an interface that
can accept @emph{requests} and will return @emph{responses}.

You should think about the user agent as an interface layer between
your application code and the network.  Through this interface you are
able to access the various servers on the network.

The libwww-perl class name for the user agent is
@code{LWP::UserAgent}. Every libwww-perl application that wants to
communicate should create at least one object of this kind. The main
method provided by this object is request(). This method takes an
@code{HTTP::Request} object as argument and will (eventually) return a
@code{HTTP::Response} object.

The user agent has many other attributes that lets you
configure how it will interact with the network and with your
application code.

@itemize @bullet
@item 
The timeout specify how much time we give remote servers in
creating responses before the library disconnect and creates an
internal timeout response.

@item 
The @strong{agent} specify the name that your application should use when it
presents itself on the network.

@item 
The @strong{from} attribute 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.

@item 
The @strong{use_alarm} specify if it is OK for the user agent to use the
alarm(2) system to implement timeouts.

@item 
The @strong{use_eval} specify if the agent should raise an
exception (die in perl) if an error condition occur.

@item 
The @strong{parse_head} specify whether we should initialize response
headers from the <head> section of HTML documents.

@item 
The @strong{proxy} and @strong{no_proxy} specify if and when communication should
go through a proxy server. <URL:http://www.w3.org/pub/WWW/Proxies/>

@item 
The @strong{credentials} provide a way to set up user names and
passwords that is needed to access certain services.

@end itemize
Many applications would want even more control over how they interact
with the network and they get this by specializing the
@code{LWP::UserAgent} by sub-classing.  The library provide a
specialization called @code{LWP::RobotUA} that is used by robot
applications.

@unnumberedsubsec An Example

This example shows how the user agent, a request and a response are
represented in actual perl code:

@example
# Create a user agent object
use LWP::UserAgent;
$ua = new LWP::UserAgent;
$ua->agent("AgentName/0.1 " . $ua->agent);
@end example

@example
# Create a request
my $req = new HTTP::Request POST => @'http://www.perl.com/cgi-bin/BugGlimpse@';
$req->content_type(@'application/x-www-form-urlencoded@');
$req->content(@'match=www&errors=0@');
@end example

@example
# Pass request to the user agent and get a response back
my $res = $ua->request($req);
@end example

@example
# Check the outcome of the response
if ($res->is_success) @{
    print $res->content;
@} else @{
    print "Bad luck this time\n";
@}
@end example

The $ua is created once when the application starts up.  New request
objects are normally created for each request sent.

@unnumberedsec NETWORK SUPPORT

This section goes through the various protocol schemes and describe
the HTTP style methods that are supported and the headers that might
have any effect.

For all requests, a "User-Agent" header is added and initialized from
the $ua->agent value before the request is handed to the network
layer.  In the same way, a "From" header is initialized from the
$ua->from value.

For all responses, the library will add a header called "Client-Date".
This header will recode the time when the response was received by
your application.  This format and semantics of the header is just
like the server created "Date" header.

@unnumberedsubsec HTTP Requests

HTTP request are really just handed off to an HTTP server and it will
decide what happens.  Few servers implement methods beside the usual
"GET", "HEAD", "POST" and "PUT" but CGI-scripts can really implement
any method they like.

If the server is not available then the library will generate an
internal error response.

The library automatically adds a "Host" and a "Content-Length" header
to the HTTP request before it is sent over the network.

For GET request you might want to add the "If-Modified-Since" header
to make the request conditional.

For POST request you should add the "Content-Type" header.  When you
try to emulate HTML <FORM> handling you should usually let the value
of the "Content-Type" header be "application/x-www-form-urlencoded".
See @xref{lwpcook,Lwpcook}, for examples of this.

The libwww-perl HTTP implementation currently support the HTTP/1.0
protocol.  HTTP/0.9 servers are also handled correctly.

The library allows you to access proxy server through HTTP.  This
means that you can set up the library to forward all types of request
through the HTTP protocol module.  See @xref{LWP/UserAgent,LWP/UserAgent}, for
documentation of this.

@unnumberedsubsec FTP Requests

The library currently support GET, HEAD and PUT requests.  GET will
retrieve a file or a directory listing from an FTP server.  PUT will
store a file on a ftp server.

You can specify a ftp account for servers that want this in addition
user name and password.  This is specified by passing an "Account"
header in the request.

User name/password can be specified using basic authorization or be
encoded in the URL.  Bad logins return an UNAUTHORIZED response with
"WWW-Authenticate: Basic" and can be treated as basic authorization
for HTTP.

The library support ftp ASCII transfer mode by specifying the "type=a"
parameter in the URL.

Directory listings are by default returned unprocessed (as returned
from the ftp server) with the content media type reported to be
"text/ftp-dir-listing". The @code{File::Listing} module provide functionality
for parsing of these directory listing.

The ftp module is also able to convert directory listings to HTML and
this can be requested via the standard HTTP content negotiation
mechanisms (add an "Accept: text/html" header in the request if you
want this).

The normal file retrievals, the "Content-Type" is guessed based on the
file name suffix. See @xref{LWP/MediaTypes,LWP/MediaTypes},.

The "If-Modified-Since" header is not honored yet.

Example:

@example
$req = HTTP::Request->new(GET => @'ftp://me:passwd@@ftp.some.where.com/@');
$req->header(Accept => "text/html, */*;q=0.1");
@end example

@unnumberedsubsec News Requests

Access to the USENET News system is implemented through the NNTP
protocol.  The name of the news server is obtained from the
NNTP_SERVER environment variable and defaults to "news".  It is not
possible to specify the hostname of the NNTP server in the news:-URLs.

The library support GET and HEAD to retrieve news articles through the
NNTP protocol.  You can also post articles to newsgroups by using
(surprise!) the POST method.

GET on newsgroups is not implemented yet.

Examples:

@example
$req = HTTP::Request->new(GET => @'news:abc1234@@a.sn.no@');
@end example

@example
$req = HTTP::Request->new(POST => @'news:comp.lang.perl.test@');
$req->header(Subject => @'This is a test@',
             From    => @'me@@some.where.org@');
$req->content(<<EOT);
This is the content of the message that we are sending to
the world.
EOT
@end example

@unnumberedsubsec Gopher Request

The library supports the GET and HEAD method for gopher request.  All
request header values are ignored.  HEAD cheats and will return a
response without even talking to server.

Gopher menus are always converted to HTML.

The response "Content-Type" is generated from the document type
encoded (as the first letter) in the request URL path itself.

Example:

@example
$req = HTTP::Request->new(@'GET@', @'gopher://gopher.sn.no/@');
@end example

@unnumberedsubsec File Request

The library supports GET and HEAD methods for file requests.  The
"If-Modified-Since" header is supported.  All other headers are
ignored.  The host component of the file URL must be empty or set
to "localhost".  Any other host value will be treated as an error.

Directories are always converted to an HTML document.  For normal
files, the "Content-Type" and "Content-Encoding" in the response are
guessed based on the file suffix.

Example:

@example
$req = HTTP::Request->new(GET => @'file:/etc/passwd@');
@end example

@unnumberedsubsec Mailto Request

You can send (aka "POST") mail messages using the library.  All
headers specified for the request are passed on to the mail system.
The "To" header is initialized from the mail address in the URL.

Example:

@example
$req = HTTP::Request->new(POST => @'mailto:libwww-perl-request@@ics.uci.edu@');
$req->header("Subject", "subscribe");
$req->content("Please subscribe me to the libwww-perl mailing list!\n");
@end example

@unnumberedsec OVERVIEW OF CLASSES AND PACKAGES

This table should give you a quick overview of the classes provided by the
library. Indentation shows class inheritance.

@example
LWP::MemberMixin   -- Access to member variables of Perl5 classes
  LWP::UserAgent   -- WWW user agent class
    LWP::RobotUA   -- When developing a robot applications
  LWP::Protocol          -- Interface to various protocol schemes
    LWP::Protocol::http  -- http:// access
    LWP::Protocol::file  -- file:// access
    LWP::Protocol::ftp   -- ftp:// access
    ...
@end example

@example
LWP::Socket        -- Socket creation and IO
@end example

@example
HTTP::Headers      -- MIME/RFC822 style header (used by HTTP::Message)
HTTP::Message      -- HTTP style message
  HTTP::Request    -- HTTP request
  HTTP::Response   -- HTTP response
HTTP::Daemon       -- A HTTP server class
@end example

@example
URI::URL           -- Uniform Resource Locators
@end example

@example
WWW::RobotRules    -- Parse robots.txt files
  WWW::RobotRules::AnyDBM_File -- Persistent RobotRules
@end example

@example
HTML::Parser       -- Parse HTML documents
  HTML::TreeBuilder-- Build a HTML syntax tree
  HTML::HeadParser -- Parse the <HEAD> section of a HTML document
  HTML::LinkExtor  -- Extract links from a HTML document
HTML::Element      -- Building block for the HTML::TreeBuilder
HTML::Formatter    -- Convert HTML syntax trees to readable formats
  HTML::FormatText -- Output is plain text
  HTML::FormatPS   -- Output is PostScript
@end example

The following modules provide various functions and definitions.

@example
LWP                -- This file.  Library version number and documentation.
LWP::MediaTypes    -- MIME types configuration (text/html etc.)
LWP::Debug         -- Debug logging module
LWP::Simple        -- Simplified procedural interface for common functions
HTTP::Status       -- HTTP status code (200 OK etc)
HTTP::Date         -- Date parsing module for HTTP date formats
HTTP::Negotiate    -- HTTP content negotiation calculation
HTML::Entities     -- Expand or unexpand entities in HTML text
File::Listing      -- Parse directory listings
@end example

HTTP use the Base64 encoding at some places.  The QuotedPrint module
is just included to make the MIME:: collection more complete.

@example
MIME::Base64       -- Base64 encoding/decoding routines
MIME::QuotedPrint  -- Quoted Printable encoding/decoding routines
@end example

The following modules does not have much to do with the World Wide
Web, but are included just because I am lazy and did not bother to
make separate distributions for them.  Regard them as bonus, provided
free for your pleasure.

@example
Font::AFM          -- Parse Adobe Font Metric files
File::CounterFile  -- Persistent counter class
@end example

@unnumberedsec MORE DOCUMENTATION

All modules contain detailed information on the interfaces they
provide.  The @xref{lwpcook,Lwpcook}, is the libwww-perl cookbook that contain
examples of typical usage of the library.  You might want to take a
look at how the scripts @code{lwp-request}, @code{lwp-rget} and @code{lwp-mirror}
are implemented.

@unnumberedsec BUGS

The library can not handle multiple simultaneous requests yet.  The
HTML:: modules are still experimental.  Also, check out what@'s left in
the TODO file.

@unnumberedsec ACKNOWLEDGEMENTS

This package owes a lot in motivation, design, and code, to the
libwww-perl library for Perl 4, maintained by Roy Fielding
<fielding@@ics.uci.edu>.

That package used work from Alberto Accomazzi, James Casey, Brooks
Cutter, Martijn Koster, Oscar Nierstrasz, Mel Melchner, Gertjan van
Oosten, Jared Rhine, Jack Shirazi, Gene Spafford, Marc VanHeyningen,
Steven E. Brenner, Marion Hakanson, Waldemar Kebsch, Tony Sanders, and
Larry Wall; see the libwww-perl-0.40 library for details.

The primary architect for this Perl 5 library is Martijn Koster and
Gisle Aas, with lots of help from Graham Barr, Tim Bunce, Andreas
Koenig, Jared Rhine, and Jack Shirazi.

@unnumberedsec COPYRIGHT

@example
Copyright 1995-1996, Gisle Aas
Copyright 1995, Martijn Koster
@end example

This library is free software; you can redistribute it and/or
modify it under the same terms as Perl itself.

@unnumberedsec AVAILABILITY

The latest version of this library is likely to be available from:

@example
http://www.sn.no/libwww-perl/
@end example

The best place to discuss this code is on the
<libwww-perl@@ics.uci.edu> mailing list.

