1LWP(3) User Contributed Perl Documentation LWP(3)
2
3
4
6 LWP - The World-Wide Web library for Perl
7
9 use LWP;
10 print "This is libwww-perl-$LWP::VERSION\n";
11
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 sep‐
29 arately 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 procedur‐
36 al 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 construct‐
45 ing 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
58 The libwww-perl library is based on HTTP style communication. This sec‐
59 tion 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 pre‐
77 vious requests, i.e. the service is stateless. The same simple model
78 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 parame‐
84 ters and the response will contain the query result. If we want to
85 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
94 The libwww-perl request object has the class name "HTTP::Request". The
95 fact that the class name uses "HTTP::" as a prefix only implies that we
96 use the HTTP model of communication. It does not limit the kind of
97 services we can try to pass this request to. For instance, we will
98 send "HTTP::Request"s both to ftp and gopher servers, as well as to the
99 local file system.
100
101 The main attributes of the request objects are:
102
103 · The method is a short string that tells what kind of request this
104 is. The most common methods are GET, PUT, POST and HEAD.
105
106 · The uri is a string denoting the protocol, server and the name of
107 the "document" we want to access. The uri might also encode various
108 other parameters.
109
110 · The headers contain additional information about the request and can
111 also used to describe the content. The headers are a set of key‐
112 word/value pairs.
113
114 · The content is an arbitrary amount of data.
115
116 The Response Object
117
118 The libwww-perl response object has the class name "HTTP::Response".
119 The main attributes of objects of this class are:
120
121 · The code is a numerical value that indicates the overall outcome of
122 the request.
123
124 · The message is a short, human readable string that corresponds to
125 the code.
126
127 · The headers contain additional information about the response and
128 describe the content.
129
130 · The content is an arbitrary amount of data.
131
132 Since we don't want to handle all possible code values directly in our
133 programs, a libwww-perl response object has methods that can be used to
134 query what kind of response this is. The most commonly used response
135 classification methods are:
136
137 is_success()
138 The request was was successfully received, understood or accepted.
139
140 is_error()
141 The request failed. The server or the resource might not be avail‐
142 able, access to the resource might be denied or other things might
143 have failed for some reason.
144
145 The User Agent
146
147 Let us assume that we have created a request object. What do we actu‐
148 ally do with it in order to receive a response?
149
150 The answer is that you pass it to a user agent object and this object
151 takes care of all the things that need to be done (like low-level com‐
152 munication and error handling) and returns a response object. The user
153 agent represents your application on the network and provides you with
154 an interface that can accept requests and return responses.
155
156 The user agent is an interface layer between your application code and
157 the network. Through this interface you are able to access the various
158 servers on the network.
159
160 The class name for the user agent is "LWP::UserAgent". Every libwww-
161 perl application that wants to communicate should create at least one
162 object of this class. The main method provided by this object is
163 request(). This method takes an "HTTP::Request" object as argument and
164 (eventually) returns a "HTTP::Response" object.
165
166 The user agent has many other attributes that let you configure how it
167 will interact with the network and with your application.
168
169 · The timeout specifies how much time we give remote servers to
170 respond before the library disconnects and creates an internal time‐
171 out response.
172
173 · The agent specifies the name that your application should use when
174 it presents itself on the network.
175
176 · The from attribute can be set to the e-mail address of the person
177 responsible for running the application. If this is set, then the
178 address will be sent to the servers with every request.
179
180 · The parse_head specifies whether we should initialize response head‐
181 ers from the <head> section of HTML documents.
182
183 · The proxy and no_proxy attributes specify if and when to go through
184 a proxy server. <URL:http://www.w3.org/pub/WWW/Proxies/>
185
186 · The credentials provide a way to set up user names and passwords
187 needed to access certain services.
188
189 Many applications want even more control over how they interact with
190 the network and they get this by sub-classing "LWP::UserAgent". The
191 library includes a sub-class, "LWP::RobotUA", for robot applications.
192
193 An Example
194
195 This example shows how the user agent, a request and a response are
196 represented in actual perl code:
197
198 # Create a user agent object
199 use LWP::UserAgent;
200 $ua = LWP::UserAgent->new;
201 $ua->agent("MyApp/0.1 ");
202
203 # Create a request
204 my $req = HTTP::Request->new(POST => 'http://search.cpan.org/search');
205 $req->content_type('application/x-www-form-urlencoded');
206 $req->content('query=libwww-perl&mode=dist');
207
208 # Pass request to the user agent and get a response back
209 my $res = $ua->request($req);
210
211 # Check the outcome of the response
212 if ($res->is_success) {
213 print $res->content;
214 }
215 else {
216 print $res->status_line, "\n";
217 }
218
219 The $ua is created once when the application starts up. New request
220 objects should normally created for each request sent.
221
223 This section discusses the various protocol schemes and the HTTP style
224 methods that headers may be used for each.
225
226 For all requests, a "User-Agent" header is added and initialized from
227 the $ua->agent attribute before the request is handed to the network
228 layer. In the same way, a "From" header is initialized from the
229 $ua->from attribute.
230
231 For all responses, the library adds a header called "Client-Date".
232 This header holds the time when the response was received by your
233 application. The format and semantics of the header are the same as
234 the server created "Date" header. You may also encounter other
235 "Client-XXX" headers. They are all generated by the library internally
236 and are not received from the servers.
237
238 HTTP Requests
239
240 HTTP requests are just handed off to an HTTP server and it decides what
241 happens. Few servers implement methods beside the usual "GET", "HEAD",
242 "POST" and "PUT", but CGI-scripts may implement any method they like.
243
244 If the server is not available then the library will generate an inter‐
245 nal error response.
246
247 The library automatically adds a "Host" and a "Content-Length" header
248 to the HTTP request before it is sent over the network.
249
250 For a GET request you might want to add a "If-Modified-Since" or
251 "If-None-Match" header to make the request conditional.
252
253 For a POST request you should add the "Content-Type" header. When you
254 try to emulate HTML <FORM> handling you should usually let the value of
255 the "Content-Type" header be "application/x-www-form-urlencoded". See
256 lwpcook for examples of this.
257
258 The libwww-perl HTTP implementation currently support the HTTP/1.1 and
259 HTTP/1.0 protocol.
260
261 The library allows you to access proxy server through HTTP. This means
262 that you can set up the library to forward all types of request through
263 the HTTP protocol module. See LWP::UserAgent for documentation of
264 this.
265
266 HTTPS Requests
267
268 HTTPS requests are HTTP requests over an encrypted network connection
269 using the SSL protocol developed by Netscape. Everything about HTTP
270 requests above also apply to HTTPS requests. In addition the library
271 will add the headers "Client-SSL-Cipher", "Client-SSL-Cert-Subject" and
272 "Client-SSL-Cert-Issuer" to the response. These headers denote the
273 encryption method used and the name of the server owner.
274
275 The request can contain the header "If-SSL-Cert-Subject" in order to
276 make the request conditional on the content of the server certificate.
277 If the certificate subject does not match, no request is sent to the
278 server and an internally generated error response is returned. The
279 value of the "If-SSL-Cert-Subject" header is interpreted as a Perl reg‐
280 ular expression.
281
282 FTP Requests
283
284 The library currently supports GET, HEAD and PUT requests. GET
285 retrieves a file or a directory listing from an FTP server. PUT stores
286 a file on a ftp server.
287
288 You can specify a ftp account for servers that want this in addition to
289 user name and password. This is specified by including an "Account"
290 header in the request.
291
292 User name/password can be specified using basic authorization or be
293 encoded in the URL. Failed logins return an UNAUTHORIZED response with
294 "WWW-Authenticate: Basic" and can be treated like basic authorization
295 for HTTP.
296
297 The library supports ftp ASCII transfer mode by specifying the "type=a"
298 parameter in the URL. It also supports transfer of ranges for FTP
299 transfers using the "Range" header.
300
301 Directory listings are by default returned unprocessed (as returned
302 from the ftp server) with the content media type reported to be
303 "text/ftp-dir-listing". The "File::Listing" module provides methods for
304 parsing of these directory listing.
305
306 The ftp module is also able to convert directory listings to HTML and
307 this can be requested via the standard HTTP content negotiation mecha‐
308 nisms (add an "Accept: text/html" header in the request if you want
309 this).
310
311 For normal file retrievals, the "Content-Type" is guessed based on the
312 file name suffix. See LWP::MediaTypes.
313
314 The "If-Modified-Since" request header works for servers that implement
315 the MDTM command. It will probably not work for directory listings
316 though.
317
318 Example:
319
320 $req = HTTP::Request->new(GET => 'ftp://me:passwd@ftp.some.where.com/');
321 $req->header(Accept => "text/html, */*;q=0.1");
322
323 News Requests
324
325 Access to the USENET News system is implemented through the NNTP proto‐
326 col. The name of the news server is obtained from the NNTP_SERVER
327 environment variable and defaults to "news". It is not possible to
328 specify the hostname of the NNTP server in news: URLs.
329
330 The library supports GET and HEAD to retrieve news articles through the
331 NNTP protocol. You can also post articles to newsgroups by using (sur‐
332 prise!) the POST method.
333
334 GET on newsgroups is not implemented yet.
335
336 Examples:
337
338 $req = HTTP::Request->new(GET => 'news:abc1234@a.sn.no');
339
340 $req = HTTP::Request->new(POST => 'news:comp.lang.perl.test');
341 $req->header(Subject => 'This is a test',
342 From => 'me@some.where.org');
343 $req->content(<<EOT);
344 This is the content of the message that we are sending to
345 the world.
346 EOT
347
348 Gopher Request
349
350 The library supports the GET and HEAD methods for gopher requests. All
351 request header values are ignored. HEAD cheats and returns a response
352 without even talking to server.
353
354 Gopher menus are always converted to HTML.
355
356 The response "Content-Type" is generated from the document type encoded
357 (as the first letter) in the request URL path itself.
358
359 Example:
360
361 $req = HTTP::Request->new(GET => 'gopher://gopher.sn.no/');
362
363 File Request
364
365 The library supports GET and HEAD methods for file requests. The
366 "If-Modified-Since" header is supported. All other headers are
367 ignored. The host component of the file URL must be empty or set to
368 "localhost". Any other host value will be treated as an error.
369
370 Directories are always converted to an HTML document. For normal
371 files, the "Content-Type" and "Content-Encoding" in the response are
372 guessed based on the file suffix.
373
374 Example:
375
376 $req = HTTP::Request->new(GET => 'file:/etc/passwd');
377
378 Mailto Request
379
380 You can send (aka "POST") mail messages using the library. All headers
381 specified for the request are passed on to the mail system. The "To"
382 header is initialized from the mail address in the URL.
383
384 Example:
385
386 $req = HTTP::Request->new(POST => 'mailto:libwww@perl.org');
387 $req->header(Subject => "subscribe");
388 $req->content("Please subscribe me to the libwww-perl mailing list!\n");
389
390 CPAN Requests
391
392 URLs with scheme "cpan:" are redirected to the a suitable CPAN mirror.
393 If you have your own local mirror of CPAN you might tell LWP to use it
394 for "cpan:" URLs by an assignment like this:
395
396 $LWP::Protocol::cpan::CPAN = "file:/local/CPAN/";
397
398 Suitable CPAN mirrors are also picked up from the configuration for the
399 CPAN.pm, so if you have used that module a suitable mirror should be
400 picked automatically. If neither of these apply, then a redirect to
401 the generic CPAN http location is issued.
402
403 Example request to download the newest perl:
404
405 $req = HTTP::Request->new(GET => "cpan:src/latest.tar.gz");
406
408 This table should give you a quick overview of the classes provided by
409 the library. Indentation shows class inheritance.
410
411 LWP::MemberMixin -- Access to member variables of Perl5 classes
412 LWP::UserAgent -- WWW user agent class
413 LWP::RobotUA -- When developing a robot applications
414 LWP::Protocol -- Interface to various protocol schemes
415 LWP::Protocol::http -- http:// access
416 LWP::Protocol::file -- file:// access
417 LWP::Protocol::ftp -- ftp:// access
418 ...
419
420 LWP::Authen::Basic -- Handle 401 and 407 responses
421 LWP::Authen::Digest
422
423 HTTP::Headers -- MIME/RFC822 style header (used by HTTP::Message)
424 HTTP::Message -- HTTP style message
425 HTTP::Request -- HTTP request
426 HTTP::Response -- HTTP response
427 HTTP::Daemon -- A HTTP server class
428
429 WWW::RobotRules -- Parse robots.txt files
430 WWW::RobotRules::AnyDBM_File -- Persistent RobotRules
431
432 Net::HTTP -- Low level HTTP client
433
434 The following modules provide various functions and definitions.
435
436 LWP -- This file. Library version number and documentation.
437 LWP::MediaTypes -- MIME types configuration (text/html etc.)
438 LWP::Debug -- Debug logging module
439 LWP::Simple -- Simplified procedural interface for common functions
440 HTTP::Status -- HTTP status code (200 OK etc)
441 HTTP::Date -- Date parsing module for HTTP date formats
442 HTTP::Negotiate -- HTTP content negotiation calculation
443 File::Listing -- Parse directory listings
444 HTML::Form -- Processing for <form>s in HTML documents
445
447 All modules contain detailed information on the interfaces they pro‐
448 vide. The lwpcook manpage is the libwww-perl cookbook that contain
449 examples of typical usage of the library. You might want to take a
450 look at how the scripts "lwp-request", "lwp-rget" and "lwp-mirror" are
451 implemented.
452
454 The following environment variables are used by LWP:
455
456 HOME
457 The "LWP::MediaTypes" functions will look for the .media.types and
458 .mime.types files relative to you home directory.
459
460 http_proxy
461 ftp_proxy
462 xxx_proxy
463 no_proxy
464 These environment variables can be set to enable communication
465 through a proxy server. See the description of the "env_proxy"
466 method in LWP::UserAgent.
467
468 PERL_LWP_USE_HTTP_10
469 Enable the old HTTP/1.0 protocol driver instead of the new HTTP/1.1
470 driver. You might want to set this to a TRUE value if you discover
471 that your old LWP applications fails after you installed LWP-5.60
472 or better.
473
474 PERL_HTTP_URI_CLASS
475 Used to decide what URI objects to instantiate. The default is
476 "URI". You might want to set it to "URI::URL" for compatibility
477 with old times.
478
480 LWP was made possible by contributions from Adam Newby, Albert Dvornik,
481 Alexandre Duret-Lutz, Andreas Gustafsson, Andreas König, Andrew Pim‐
482 lott, Andy Lester, Ben Coleman, Benjamin Low, Ben Low, Ben Tilly, Blair
483 Zajac, Bob Dalgleish, BooK, Brad Hughes, Brian J. Murrell, Brian
484 McCauley, Charles C. Fu, Charles Lane, Chris Nandor, Christian Gilmore,
485 Chris W. Unger, Craig Macdonald, Dale Couch, Dan Kubb, Dave Dunkin,
486 Dave W. Smith, David Coppit, David Dick, David D. Kilzer, Doug MacEach‐
487 ern, Edward Avis, erik, Gary Shea, Gisle Aas, Graham Barr, Gurusamy
488 Sarathy, Hans de Graaff, Harald Joerg, Harry Bochner, Hugo, Ilya
489 Zakharevich, INOUE Yoshinari, Ivan Panchenko, Jack Shirazi, James Till‐
490 man, Jan Dubois, Jared Rhine, Jim Stern, Joao Lopes, John Klar, Johnny
491 Lee, Josh Kronengold, Josh Rai, Joshua Chamas, Joshua Hoblitt, Kartik
492 Subbarao, Keiichiro Nagano, Ken Williams, KONISHI Katsuhiro, Lee T
493 Lindley, Liam Quinn, Marc Hedlund, Marc Langheinrich, Mark D. Anderson,
494 Marko Asplund, Mark Stosberg, Markus B Krüger, Markus Laker, Martijn
495 Koster, Martin Thurn, Matthew Eldridge, Matthew.van.Eerde, Matt
496 Sergeant, Michael A. Chase, Michael Quaranta, Michael Thompson, Mike
497 Schilli, Moshe Kaminsky, Nathan Torkington, Nicolai Langfeldt, Norton
498 Allen, Olly Betts, Paul J. Schinder, peterm, Philip GuentherDaniel
499 Buenzli, Pon Hwa Lin, Radoslaw Zielinski, Radu Greab, Randal L.
500 Schwartz, Richard Chen, Robin Barker, Roy Fielding, Sander van Zoest,
501 Sean M. Burke, shildreth, Slaven Rezic, Steve A Fink, Steve Hay, Steven
502 Butler, Steve_Kilbane, Takanori Ugai, Thomas Lotterer, Tim Bunce, Tom
503 Hughes, Tony Finch, Ville Skyttä, Ward Vandewege, William York, Yale
504 Huang, and Yitzchak Scott-Thoennes.
505
506 LWP owes a lot in motivation, design, and code, to the libwww-perl
507 library for Perl4 by Roy Fielding, which included work from Alberto
508 Accomazzi, James Casey, Brooks Cutter, Martijn Koster, Oscar Nier‐
509 strasz, Mel Melchner, Gertjan van Oosten, Jared Rhine, Jack Shirazi,
510 Gene Spafford, Marc VanHeyningen, Steven E. Brenner, Marion Hakanson,
511 Waldemar Kebsch, Tony Sanders, and Larry Wall; see the libwww-perl-0.40
512 library for details.
513
515 Copyright 1995-2005, Gisle Aas
516 Copyright 1995, Martijn Koster
517
518 This library is free software; you can redistribute it and/or modify it
519 under the same terms as Perl itself.
520
522 The latest version of this library is likely to be available from CPAN
523 as well as:
524
525 http://www.linpro.no/lwp/
526
527 The best place to discuss this code is on the <libwww@perl.org> mailing
528 list.
529
530
531
532perl v5.8.8 2004-04-06 LWP(3)