LWP (3)
Leading comments
Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) Standard preamble: ========================================================================
NAME
LWP - The World-Wide Web library for PerlSYNOPSIS
use LWP; print "This is libwww-perl-$LWP::VERSION\n";
DESCRIPTION
The libwww-perl collection is a set of Perl modules which provides a simple and consistent application programming interface (Most modules in this library provide an object oriented
The main features of the library are:
- *
- Contains various reusable components (modules) that can be used separately or together.
- *
- Provides an object oriented model of HTTP-style communication. Within this framework we currently support access to http, https, gopher, ftp, news, file, and mailto resources.
- *
- Provides a full object oriented interface or a very simple procedural interface.
- *
- Supports the basic and digest authorization schemes.
- *
- Supports transparent redirect handling.
- *
- Supports access through proxy servers.
- *
- Provides parser for robots.txt files and a framework for constructing robots.
- *
-
Supports parsing of HTMLforms.
- *
-
Implements HTTPcontent negotiation algorithm that can be used both in protocol modules and in server scripts (likeCGIscripts).
- *
-
Supports HTTPcookies.
- *
- Some simple command line clients, for instance "lwp-request" and "lwp-download".
HTTP STYLE COMMUNICATION
The libwww-perl library is based onLet us start with this quote from the
- *
-
The HTTPprotocol 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.
What this means to libwww-perl is that communication always take place through these steps: First a request object is created and configured. This object is then passed to a server and we get a 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!
The Request Object
The libwww-perl request object has the class name "HTTP::Request". The fact that the class name uses "HTTP::" as a prefix only implies that we use theThe main attributes of the request objects are:
- *
-
method is a short string that tells what kind of
request this is. The most common methods are GET,PUT,POSTandHEAD.
- *
- uri is a string denoting the protocol, server and the name of the ``document'' we want to access. The uri might also encode various other parameters.
- *
- headers contains additional information about the request and can also used to describe the content. The headers are a set of keyword/value pairs.
- *
- content is an arbitrary amount of data.
The Response Object
The libwww-perl response object has the class name "HTTP::Response". The main attributes of objects of this class are:- *
- code is a numerical value that indicates the overall outcome of the request.
- *
- message is a short, human readable string that corresponds to the code.
- *
- headers contains additional information about the response and describe the content.
- *
- content is an arbitrary amount of data.
Since we don't want to handle all possible code 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:
- is_success()
- The request was successfully received, understood or accepted.
- 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.
The User Agent
Let us assume that we have created a request object. What do we actually do with it in order to receive a response?The answer is that you pass it to a user agent 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 response object. The user agent represents your application on the network and provides you with an interface that can accept requests and return responses.
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.
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 request(). This method takes an "HTTP::Request" object as argument and (eventually) returns a "HTTP::Response" object.
The user agent has many other attributes that let you configure how it will interact with the network and with your application.
- *
- timeout specifies how much time we give remote servers to respond before the library disconnects and creates an internal timeout response.
- *
- agent specifies the name that your application uses when it presents itself on the network.
- *
- from 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.
- *
-
parse_head specifies whether we should initialize response
headers from the <head> section of HTMLdocuments.
- *
- proxy and no_proxy specify if and when to go through a proxy server. <URL:www.w3.org/History/1994/WWW/Proxies>
- *
- credentials provides a way to set up user names and passwords needed to access certain services.
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.
An Example
This example shows how the user agent, a request and a response are represented in actual perl code:
# 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 => 'search.cpan.org/search'); $req->content_type('application/x-www-form-urlencoded'); $req->content('query=libwww-perl&mode=dist'); # 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, "\n"; }
The $ua is created once when the application starts up. New request objects should normally created for each request sent.
NETWORK SUPPORT
This section discusses the various protocol schemes and theFor all requests, a ``User-Agent'' header is added and initialized from the $ua->agent attribute before the request is handed to the network layer. In the same way, a ``From'' header is initialized from the $ua->from attribute.
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.
HTTP Requests
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
For a
For a
The libwww-perl
The library allows you to access proxy server through
HTTPS Requests
The request can contain the header ``If-SSL-Cert-Subject'' in order to make the request conditional on the content of the server certificate. If the certificate subject does not match, no request is sent to the server and an internally generated error response is returned. The value of the ``If-SSL-Cert-Subject'' header is interpreted as a Perl regular expression.
FTP Requests
The library currently supports You can specify a ftp account for servers that want this in addition to user name and password. This is specified by including an ``Account'' header in the request.
User name/password can be specified using basic authorization or be encoded in the
The library supports ftp
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 "File::Listing" module provides methods for parsing of these directory listing.
The ftp module is also able to convert directory listings to
For normal file retrievals, the ``Content-Type'' is guessed based on the file name suffix. See LWP::MediaTypes.
The ``If-Modified-Since'' request header works for servers that implement the
Example:
$req = HTTP::Request->new(GET => 'ftp://me:passwd@ftp.some.where.com/'); $req->header(Accept => "text/html, */*;q=0.1");
News Requests
Access to theThe library supports
Examples:
$req = HTTP::Request->new(GET => 'news:abc1234@a.sn.no'); $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
Gopher Request
The library supports theGopher menus are always converted to
The response ``Content-Type'' is generated from the document type encoded (as the first letter) in the request
Example:
$req = HTTP::Request->new(GET => 'gopher://gopher.sn.no/');
File Request
The library supportsDirectories are always converted to an
Example:
$req = HTTP::Request->new(GET => 'file:/etc/passwd');
Mailto Request
You can send (aka ``Example:
$req = HTTP::Request->new(POST => 'mailto:libwww@perl.org'); $req->header(Subject => "subscribe"); $req->content("Please subscribe me to the libwww-perl mailing list!\n");
CPAN Requests
URLs with scheme "cpan:" are redirected to the a suitable
$LWP::Protocol::cpan::CPAN = "file:/local/CPAN/";
Suitable
Example request to download the newest perl:
$req = HTTP::Request->new(GET => "cpan:src/latest.tar.gz");
OVERVIEW OF CLASSES AND PACKAGES
This table should give you a quick overview of the classes provided by the library. Indentation shows class inheritance.
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 ... LWP::Authen::Basic -- Handle 401 and 407 responses LWP::Authen::Digest 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 WWW::RobotRules -- Parse robots.txt files WWW::RobotRules::AnyDBM_File -- Persistent RobotRules Net::HTTP -- Low level HTTP client
The following modules provide various functions and definitions.
LWP -- This file. Library version number and documentation. LWP::MediaTypes -- MIME types configuration (text/html etc.) 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 File::Listing -- Parse directory listings HTML::Form -- Processing for <form>s in HTML documents
MORE DOCUMENTATION
All modules contain detailed information on the interfaces they provide. The lwpcook manpage 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 lwp-request, lwp-download, lwp-dump and lwp-mirror are implemented.ENVIRONMENT
The following environment variables are used by- HOME
- The "LWP::MediaTypes" functions will look for the .media.types and .mime.types files relative to you home directory.
- http_proxy
- ftp_proxy
- xxx_proxy
- no_proxy
- These environment variables can be set to enable communication through a proxy server. See the description of the "env_proxy" method in LWP::UserAgent.
- PERL_LWP_ENV_PROXY
-
If set to a TRUEvalue, then the "LWP::UserAgent" will by default call "env_proxy" during initialization. This makesLWPhonor the proxy variables described above.
- PERL_LWP_SSL_VERIFY_HOSTNAME
- The default "verify_hostname" setting for "LWP::UserAgent". If not set the default will be 1. Set it as 0 to disable hostname verification (the default prior to libwww-perl 5.840.
- PERL_LWP_SSL_CA_FILE
- PERL_LWP_SSL_CA_PATH
- The file and/or directory where the trusted Certificate Authority certificates is located. See LWP::UserAgent for details.
- PERL_HTTP_URI_CLASS
-
Used to decide what URIobjects to instantiate. The default is "URI". You might want to set it to "URI::URL" for compatibility with old times.
AUTHORS
COPYRIGHT
Copyright 1995-2009, Gisle Aas Copyright 1995, Martijn Koster
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
AVAILABILITY
The latest version of this library is likely to be available from
github.com/libwww-perl/libwww-perl
The best place to discuss this code is on the <libwww@perl.org> mailing list.