1LWP(3)                User Contributed Perl Documentation               LWP(3)
2
3
4

NAME

6       LWP - The World-Wide Web library for Perl
7

SYNOPSIS

9         use LWP;
10         print "This is libwww-perl-$LWP::VERSION\n";
11

DESCRIPTION

13       The libwww-perl collection is a set of Perl modules which provides a
14       simple and consistent application programming interface (API) to the
15       World-Wide Web.  The main focus of the library is to provide classes
16       and functions that allow you to write WWW clients. The library also
17       contain modules that are of more general use and even classes that help
18       you implement simple HTTP servers.
19
20       Most modules in this library provide an object oriented API.  The user
21       agent, requests sent and responses received from the WWW server are all
22       represented by objects.  This makes a simple and powerful interface to
23       these services.  The interface is easy to extend and customize for your
24       own needs.
25
26       The main features of the library are:
27
28       ·  Contains various reusable components (modules) that can be used
29          separately or together.
30
31       ·  Provides an object oriented model of HTTP-style communication.
32          Within this framework we currently support access to http, https,
33          gopher, ftp, news, file, and mailto resources.
34
35       ·  Provides a full object oriented interface or a very simple
36          procedural interface.
37
38       ·  Supports the basic and digest authorization schemes.
39
40       ·  Supports transparent redirect handling.
41
42       ·  Supports access through proxy servers.
43
44       ·  Provides parser for robots.txt files and a framework for
45          constructing robots.
46
47       ·  Supports parsing of HTML forms.
48
49       ·  Implements HTTP content negotiation algorithm that can be used both
50          in protocol modules and in server scripts (like CGI scripts).
51
52       ·  Supports HTTP cookies.
53
54       ·  Some simple command line clients, for instance "lwp-request" and
55          "lwp-download".
56

HTTP STYLE COMMUNICATION

58       The libwww-perl library is based on HTTP style communication. This
59       section tries to describe what that means.
60
61       Let us start with this quote from the HTTP specification document
62       <URL:http://www.w3.org/pub/WWW/Protocols/>:
63
64       ·  The HTTP protocol is based on a request/response paradigm. A client
65          establishes a connection with a server and sends a request to the
66          server in the form of a request method, URI, and protocol version,
67          followed by a MIME-like message containing request modifiers, client
68          information, and possible body content. The server responds with a
69          status line, including the message's protocol version and a success
70          or error code, followed by a MIME-like message containing server
71          information, entity meta-information, and possible body content.
72
73       What this means to libwww-perl is that communication always take place
74       through these steps: First a request object is created and configured.
75       This object is then passed to a server and we get a response object in
76       return that we can examine. A request is always independent of any
77       previous requests, i.e. the service is stateless.  The same simple
78       model is used for any kind of service we want to access.
79
80       For example, if we want to fetch a document from a remote file server,
81       then we send it a request that contains a name for that document and
82       the response will contain the document itself.  If we access a search
83       engine, then the content of the request will contain the query
84       parameters and the response will contain the query result.  If we want
85       to send a mail message to somebody then we send a request object which
86       contains our message to the mail server and the response object will
87       contain an acknowledgment that tells us that the message has been
88       accepted and will be forwarded to the recipient(s).
89
90       It is as simple as that!
91
92   The Request Object
93       The libwww-perl request object has the class name "HTTP::Request".  The
94       fact that the class name uses "HTTP::" as a prefix only implies that we
95       use the HTTP model of communication.  It does not limit the kind of
96       services we can try to pass this request to.  For instance, we will
97       send "HTTP::Request"s both to ftp and gopher servers, as well as to the
98       local file system.
99
100       The main attributes of the request objects are:
101
102       ·  The method is a short string that tells what kind of request this
103          is.  The most common methods are GET, PUT, POST and HEAD.
104
105       ·  The uri is a string denoting the protocol, server and the name of
106          the "document" we want to access.  The uri might also encode various
107          other parameters.
108
109       ·  The headers contain additional information about the request and can
110          also used to describe the content.  The headers are a set of
111          keyword/value pairs.
112
113       ·  The content is an arbitrary amount of data.
114
115   The Response Object
116       The libwww-perl response object has the class name "HTTP::Response".
117       The main attributes of objects of this class are:
118
119       ·  The code is a numerical value that indicates the overall outcome of
120          the request.
121
122       ·  The message is a short, human readable string that corresponds to
123          the code.
124
125       ·  The headers contain additional information about the response and
126          describe the content.
127
128       ·  The content is an arbitrary amount of data.
129
130       Since we don't want to handle all possible code values directly in our
131       programs, a libwww-perl response object has methods that can be used to
132       query what kind of response this is.  The most commonly used response
133       classification methods are:
134
135       is_success()
136          The request was was successfully received, understood or accepted.
137
138       is_error()
139          The request failed.  The server or the resource might not be
140          available, access to the resource might be denied or other things
141          might have failed for some reason.
142
143   The User Agent
144       Let us assume that we have created a request object. What do we
145       actually do with it in order to receive a response?
146
147       The answer is that you pass it to a user agent object and this object
148       takes care of all the things that need to be done (like low-level
149       communication and error handling) and returns a response object. The
150       user agent represents your application on the network and provides you
151       with an interface that can accept requests and return responses.
152
153       The user agent is an interface layer between your application code and
154       the network.  Through this interface you are able to access the various
155       servers on the network.
156
157       The class name for the user agent is "LWP::UserAgent".  Every libwww-
158       perl application that wants to communicate should create at least one
159       object of this class. The main method provided by this object is
160       request(). This method takes an "HTTP::Request" object as argument and
161       (eventually) returns a "HTTP::Response" object.
162
163       The user agent has many other attributes that let you configure how it
164       will interact with the network and with your application.
165
166       ·  The timeout specifies how much time we give remote servers to
167          respond before the library disconnects and creates an internal
168          timeout response.
169
170       ·  The agent specifies the name that your application should use when
171          it presents itself on the network.
172
173       ·  The from attribute can be set to the e-mail address of the person
174          responsible for running the application.  If this is set, then the
175          address will be sent to the servers with every request.
176
177       ·  The parse_head specifies whether we should initialize response
178          headers from the <head> section of HTML documents.
179
180       ·  The proxy and no_proxy attributes specify if and when to go through
181          a proxy server. <URL:http://www.w3.org/pub/WWW/Proxies/>
182
183       ·  The credentials provide a way to set up user names and passwords
184          needed to access certain services.
185
186       Many applications want even more control over how they interact with
187       the network and they get this by sub-classing "LWP::UserAgent".  The
188       library includes a sub-class, "LWP::RobotUA", for robot applications.
189
190   An Example
191       This example shows how the user agent, a request and a response are
192       represented in actual perl code:
193
194         # Create a user agent object
195         use LWP::UserAgent;
196         my $ua = LWP::UserAgent->new;
197         $ua->agent("MyApp/0.1 ");
198
199         # Create a request
200         my $req = HTTP::Request->new(POST => 'http://search.cpan.org/search');
201         $req->content_type('application/x-www-form-urlencoded');
202         $req->content('query=libwww-perl&mode=dist');
203
204         # Pass request to the user agent and get a response back
205         my $res = $ua->request($req);
206
207         # Check the outcome of the response
208         if ($res->is_success) {
209             print $res->content;
210         }
211         else {
212             print $res->status_line, "\n";
213         }
214
215       The $ua is created once when the application starts up.  New request
216       objects should normally created for each request sent.
217

NETWORK SUPPORT

219       This section discusses the various protocol schemes and the HTTP style
220       methods that headers may be used for each.
221
222       For all requests, a "User-Agent" header is added and initialized from
223       the $ua->agent attribute before the request is handed to the network
224       layer.  In the same way, a "From" header is initialized from the
225       $ua->from attribute.
226
227       For all responses, the library adds a header called "Client-Date".
228       This header holds the time when the response was received by your
229       application.  The format and semantics of the header are the same as
230       the server created "Date" header.  You may also encounter other
231       "Client-XXX" headers.  They are all generated by the library internally
232       and are not received from the servers.
233
234   HTTP Requests
235       HTTP requests are just handed off to an HTTP server and it decides what
236       happens.  Few servers implement methods beside the usual "GET", "HEAD",
237       "POST" and "PUT", but CGI-scripts may implement any method they like.
238
239       If the server is not available then the library will generate an
240       internal error response.
241
242       The library automatically adds a "Host" and a "Content-Length" header
243       to the HTTP request before it is sent over the network.
244
245       For a GET request you might want to add a "If-Modified-Since" or "If-
246       None-Match" header to make the request conditional.
247
248       For a POST request you should add the "Content-Type" header.  When you
249       try to emulate HTML <FORM> handling you should usually let the value of
250       the "Content-Type" header be "application/x-www-form-urlencoded".  See
251       lwpcook for examples of this.
252
253       The libwww-perl HTTP implementation currently support the HTTP/1.1 and
254       HTTP/1.0 protocol.
255
256       The library allows you to access proxy server through HTTP.  This means
257       that you can set up the library to forward all types of request through
258       the HTTP protocol module.  See LWP::UserAgent for documentation of
259       this.
260
261   HTTPS Requests
262       HTTPS requests are HTTP requests over an encrypted network connection
263       using the SSL protocol developed by Netscape.  Everything about HTTP
264       requests above also apply to HTTPS requests.  In addition the library
265       will add the headers "Client-SSL-Cipher", "Client-SSL-Cert-Subject" and
266       "Client-SSL-Cert-Issuer" to the response.  These headers denote the
267       encryption method used and the name of the server owner.
268
269       The request can contain the header "If-SSL-Cert-Subject" in order to
270       make the request conditional on the content of the server certificate.
271       If the certificate subject does not match, no request is sent to the
272       server and an internally generated error response is returned.  The
273       value of the "If-SSL-Cert-Subject" header is interpreted as a Perl
274       regular expression.
275
276   FTP Requests
277       The library currently supports GET, HEAD and PUT requests.  GET
278       retrieves a file or a directory listing from an FTP server.  PUT stores
279       a file on a ftp server.
280
281       You can specify a ftp account for servers that want this in addition to
282       user name and password.  This is specified by including an "Account"
283       header in the request.
284
285       User name/password can be specified using basic authorization or be
286       encoded in the URL.  Failed logins return an UNAUTHORIZED response with
287       "WWW-Authenticate: Basic" and can be treated like basic authorization
288       for HTTP.
289
290       The library supports ftp ASCII transfer mode by specifying the "type=a"
291       parameter in the URL. It also supports transfer of ranges for FTP
292       transfers using the "Range" header.
293
294       Directory listings are by default returned unprocessed (as returned
295       from the ftp server) with the content media type reported to be
296       "text/ftp-dir-listing". The "File::Listing" module provides methods for
297       parsing of these directory listing.
298
299       The ftp module is also able to convert directory listings to HTML and
300       this can be requested via the standard HTTP content negotiation
301       mechanisms (add an "Accept: text/html" header in the request if you
302       want this).
303
304       For normal file retrievals, the "Content-Type" is guessed based on the
305       file name suffix. See LWP::MediaTypes.
306
307       The "If-Modified-Since" request header works for servers that implement
308       the MDTM command.  It will probably not work for directory listings
309       though.
310
311       Example:
312
313         $req = HTTP::Request->new(GET => 'ftp://me:passwd@ftp.some.where.com/');
314         $req->header(Accept => "text/html, */*;q=0.1");
315
316   News Requests
317       Access to the USENET News system is implemented through the NNTP
318       protocol.  The name of the news server is obtained from the NNTP_SERVER
319       environment variable and defaults to "news".  It is not possible to
320       specify the hostname of the NNTP server in news: URLs.
321
322       The library supports GET and HEAD to retrieve news articles through the
323       NNTP protocol.  You can also post articles to newsgroups by using
324       (surprise!) the POST method.
325
326       GET on newsgroups is not implemented yet.
327
328       Examples:
329
330         $req = HTTP::Request->new(GET => 'news:abc1234@a.sn.no');
331
332         $req = HTTP::Request->new(POST => 'news:comp.lang.perl.test');
333         $req->header(Subject => 'This is a test',
334                      From    => 'me@some.where.org');
335         $req->content(<<EOT);
336         This is the content of the message that we are sending to
337         the world.
338         EOT
339
340   Gopher Request
341       The library supports the GET and HEAD methods for gopher requests.  All
342       request header values are ignored.  HEAD cheats and returns a response
343       without even talking to server.
344
345       Gopher menus are always converted to HTML.
346
347       The response "Content-Type" is generated from the document type encoded
348       (as the first letter) in the request URL path itself.
349
350       Example:
351
352         $req = HTTP::Request->new(GET => 'gopher://gopher.sn.no/');
353
354   File Request
355       The library supports GET and HEAD methods for file requests.  The "If-
356       Modified-Since" header is supported.  All other headers are ignored.
357       The host component of the file URL must be empty or set to "localhost".
358       Any other host value will be treated as an error.
359
360       Directories are always converted to an HTML document.  For normal
361       files, the "Content-Type" and "Content-Encoding" in the response are
362       guessed based on the file suffix.
363
364       Example:
365
366         $req = HTTP::Request->new(GET => 'file:/etc/passwd');
367
368   Mailto Request
369       You can send (aka "POST") mail messages using the library.  All headers
370       specified for the request are passed on to the mail system.  The "To"
371       header is initialized from the mail address in the URL.
372
373       Example:
374
375         $req = HTTP::Request->new(POST => 'mailto:libwww@perl.org');
376         $req->header(Subject => "subscribe");
377         $req->content("Please subscribe me to the libwww-perl mailing list!\n");
378
379   CPAN Requests
380       URLs with scheme "cpan:" are redirected to the a suitable CPAN mirror.
381       If you have your own local mirror of CPAN you might tell LWP to use it
382       for "cpan:" URLs by an assignment like this:
383
384         $LWP::Protocol::cpan::CPAN = "file:/local/CPAN/";
385
386       Suitable CPAN mirrors are also picked up from the configuration for the
387       CPAN.pm, so if you have used that module a suitable mirror should be
388       picked automatically.  If neither of these apply, then a redirect to
389       the generic CPAN http location is issued.
390
391       Example request to download the newest perl:
392
393         $req = HTTP::Request->new(GET => "cpan:src/latest.tar.gz");
394

OVERVIEW OF CLASSES AND PACKAGES

396       This table should give you a quick overview of the classes provided by
397       the library. Indentation shows class inheritance.
398
399        LWP::MemberMixin   -- Access to member variables of Perl5 classes
400          LWP::UserAgent   -- WWW user agent class
401            LWP::RobotUA   -- When developing a robot applications
402          LWP::Protocol          -- Interface to various protocol schemes
403            LWP::Protocol::http  -- http:// access
404            LWP::Protocol::file  -- file:// access
405            LWP::Protocol::ftp   -- ftp:// access
406            ...
407
408        LWP::Authen::Basic -- Handle 401 and 407 responses
409        LWP::Authen::Digest
410
411        HTTP::Headers      -- MIME/RFC822 style header (used by HTTP::Message)
412        HTTP::Message      -- HTTP style message
413          HTTP::Request    -- HTTP request
414          HTTP::Response   -- HTTP response
415        HTTP::Daemon       -- A HTTP server class
416
417        WWW::RobotRules    -- Parse robots.txt files
418          WWW::RobotRules::AnyDBM_File -- Persistent RobotRules
419
420        Net::HTTP          -- Low level HTTP client
421
422       The following modules provide various functions and definitions.
423
424        LWP                -- This file.  Library version number and documentation.
425        LWP::MediaTypes    -- MIME types configuration (text/html etc.)
426        LWP::Simple        -- Simplified procedural interface for common functions
427        HTTP::Status       -- HTTP status code (200 OK etc)
428        HTTP::Date         -- Date parsing module for HTTP date formats
429        HTTP::Negotiate    -- HTTP content negotiation calculation
430        File::Listing      -- Parse directory listings
431        HTML::Form         -- Processing for <form>s in HTML documents
432

MORE DOCUMENTATION

434       All modules contain detailed information on the interfaces they
435       provide.  The lwpcook manpage is the libwww-perl cookbook that contain
436       examples of typical usage of the library.  You might want to take a
437       look at how the scripts lwp-request, lwp-rget and lwp-mirror are
438       implemented.
439

ENVIRONMENT

441       The following environment variables are used by LWP:
442
443       HOME
444           The "LWP::MediaTypes" functions will look for the .media.types and
445           .mime.types files relative to you home directory.
446
447       http_proxy
448       ftp_proxy
449       xxx_proxy
450       no_proxy
451           These environment variables can be set to enable communication
452           through a proxy server.  See the description of the "env_proxy"
453           method in LWP::UserAgent.
454
455       PERL_LWP_USE_HTTP_10
456           Enable the old HTTP/1.0 protocol driver instead of the new HTTP/1.1
457           driver.  You might want to set this to a TRUE value if you discover
458           that your old LWP applications fails after you installed LWP-5.60
459           or better.
460
461       PERL_HTTP_URI_CLASS
462           Used to decide what URI objects to instantiate.  The default is
463           "URI".  You might want to set it to "URI::URL" for compatibility
464           with old times.
465

AUTHORS

467       LWP was made possible by contributions from Adam Newby, Albert Dvornik,
468       Alexandre Duret-Lutz, Andreas Gustafsson, Andreas KA~Xnig, Andrew
469       Pimlott, Andy Lester, Ben Coleman, Benjamin Low, Ben Low, Ben Tilly,
470       Blair Zajac, Bob Dalgleish, BooK, Brad Hughes, Brian J. Murrell, Brian
471       McCauley, Charles C. Fu, Charles Lane, Chris Nandor, Christian Gilmore,
472       Chris W. Unger, Craig Macdonald, Dale Couch, Dan Kubb, Dave Dunkin,
473       Dave W. Smith, David Coppit, David Dick, David D. Kilzer, Doug
474       MacEachern, Edward Avis, erik, Gary Shea, Gisle Aas, Graham Barr,
475       Gurusamy Sarathy, Hans de Graaff, Harald Joerg, Harry Bochner, Hugo,
476       Ilya Zakharevich, INOUE Yoshinari, Ivan Panchenko, Jack Shirazi, James
477       Tillman, Jan Dubois, Jared Rhine, Jim Stern, Joao Lopes, John Klar,
478       Johnny Lee, Josh Kronengold, Josh Rai, Joshua Chamas, Joshua Hoblitt,
479       Kartik Subbarao, Keiichiro Nagano, Ken Williams, KONISHI Katsuhiro, Lee
480       T Lindley, Liam Quinn, Marc Hedlund, Marc Langheinrich, Mark D.
481       Anderson, Marko Asplund, Mark Stosberg, Markus B KrA~Xger, Markus
482       Laker, Martijn Koster, Martin Thurn, Matthew Eldridge,
483       Matthew.van.Eerde, Matt Sergeant, Michael A. Chase, Michael Quaranta,
484       Michael Thompson, Mike Schilli, Moshe Kaminsky, Nathan Torkington,
485       Nicolai Langfeldt, Norton Allen, Olly Betts, Paul J. Schinder, peterm,
486       Philip GuentherDaniel Buenzli, Pon Hwa Lin, Radoslaw Zielinski, Radu
487       Greab, Randal L. Schwartz, Richard Chen, Robin Barker, Roy Fielding,
488       Sander van Zoest, Sean M. Burke, shildreth, Slaven Rezic, Steve A Fink,
489       Steve Hay, Steven Butler, Steve_Kilbane, Takanori Ugai, Thomas
490       Lotterer, Tim Bunce, Tom Hughes, Tony Finch, Ville SkyttA~X, Ward
491       Vandewege, William York, Yale Huang, and Yitzchak Scott-Thoennes.
492
493       LWP owes a lot in motivation, design, and code, to the libwww-perl
494       library for Perl4 by Roy Fielding, which included work from Alberto
495       Accomazzi, James Casey, Brooks Cutter, Martijn Koster, Oscar
496       Nierstrasz, Mel Melchner, Gertjan van Oosten, Jared Rhine, Jack
497       Shirazi, Gene Spafford, Marc VanHeyningen, Steven E. Brenner, Marion
498       Hakanson, Waldemar Kebsch, Tony Sanders, and Larry Wall; see the
499       libwww-perl-0.40 library for details.
500
502         Copyright 1995-2009, Gisle Aas
503         Copyright 1995, Martijn Koster
504
505       This library is free software; you can redistribute it and/or modify it
506       under the same terms as Perl itself.
507

AVAILABILITY

509       The latest version of this library is likely to be available from CPAN
510       as well as:
511
512         http://github.com/gisle/libwww-perl
513
514       The best place to discuss this code is on the <libwww@perl.org> mailing
515       list.
516
517
518
519perl v5.12.4                      2010-09-20                            LWP(3)
Impressum