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
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",
33 "https", "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
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/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::Requests 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 · method is a short string that tells what kind of request this is.
103 The most common methods are GET, PUT, POST and HEAD.
104
105 · uri is a string denoting the protocol, server and the name of the
106 "document" we want to access. The uri might also encode various
107 other parameters.
108
109 · headers contains 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 · content is an arbitrary amount of data.
114
115 The Response Object
116 The libwww-perl response object has the class name HTTP::Response. The
117 main attributes of objects of this class are:
118
119 · code is a numerical value that indicates the overall outcome of the
120 request.
121
122 · message is a short, human readable string that corresponds to the
123 code.
124
125 · headers contains additional information about the response and
126 describe the content.
127
128 · 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 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-perl
158 application that wants to communicate should create at least one object
159 of this class. The main method provided by this object is request().
160 This method takes an HTTP::Request object as argument and (eventually)
161 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 · timeout specifies how much time we give remote servers to respond
167 before the library disconnects and creates an internal timeout
168 response.
169
170 · agent specifies the name that your application uses when it presents
171 itself on the network.
172
173 · from can be set to the e-mail address of the person responsible for
174 running the application. If this is set, then the address will be
175 sent to the servers with every request.
176
177 · parse_head specifies whether we should initialize response headers
178 from the <head> section of HTML documents.
179
180 · proxy and no_proxy specify if and when to go through a proxy server.
181 <URL:http://www.w3.org/History/1994/WWW/Proxies/>
182
183 · credentials provides a way to set up user names and passwords needed
184 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
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 a suitable CPAN mirror. If
381 you have your own local mirror of CPAN you might tell LWP to use it for
382 "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
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
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-download, lwp-dump and lwp-
438 mirror are implemented.
439
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_ENV_PROXY
456 If set to a TRUE value, then the LWP::UserAgent will by default
457 call "env_proxy" during initialization. This makes LWP honor the
458 proxy variables described above.
459
460 PERL_LWP_SSL_VERIFY_HOSTNAME
461 The default "verify_hostname" setting for LWP::UserAgent. If not
462 set the default will be 1. Set it as 0 to disable hostname
463 verification (the default prior to libwww-perl 5.840.
464
465 PERL_LWP_SSL_CA_FILE
466 PERL_LWP_SSL_CA_PATH
467 The file and/or directory where the trusted Certificate Authority
468 certificates is located. See LWP::UserAgent for details.
469
470 PERL_HTTP_URI_CLASS
471 Used to decide what URI objects to instantiate. The default is
472 URI. You might want to set it to URI::URL for compatibility with
473 old times.
474
476 LWP was made possible by contributions from Adam Newby, Albert Dvornik,
477 Alexandre Duret-Lutz, Andreas Gustafsson, Andreas König, Andrew
478 Pimlott, Andy Lester, Ben Coleman, Benjamin Low, Ben Low, Ben Tilly,
479 Blair Zajac, Bob Dalgleish, BooK, Brad Hughes, Brian J. Murrell, Brian
480 McCauley, Charles C. Fu, Charles Lane, Chris Nandor, Christian Gilmore,
481 Chris W. Unger, Craig Macdonald, Dale Couch, Dan Kubb, Dave Dunkin,
482 Dave W. Smith, David Coppit, David Dick, David D. Kilzer, Doug
483 MacEachern, Edward Avis, erik, Gary Shea, Gisle Aas, Graham Barr,
484 Gurusamy Sarathy, Hans de Graaff, Harald Joerg, Harry Bochner, Hugo,
485 Ilya Zakharevich, INOUE Yoshinari, Ivan Panchenko, Jack Shirazi, James
486 Tillman, Jan Dubois, Jared Rhine, Jim Stern, Joao Lopes, John Klar,
487 Johnny Lee, Josh Kronengold, Josh Rai, Joshua Chamas, Joshua Hoblitt,
488 Kartik Subbarao, Keiichiro Nagano, Ken Williams, KONISHI Katsuhiro, Lee
489 T Lindley, Liam Quinn, Marc Hedlund, Marc Langheinrich, Mark D.
490 Anderson, Marko Asplund, Mark Stosberg, Markus B Krüger, Markus Laker,
491 Martijn Koster, Martin Thurn, Matthew Eldridge, Matthew.van.Eerde, Matt
492 Sergeant, Michael A. Chase, Michael Quaranta, Michael Thompson, Mike
493 Schilli, Moshe Kaminsky, Nathan Torkington, Nicolai Langfeldt, Norton
494 Allen, Olly Betts, Paul J. Schinder, peterm, Philip Guenther, Daniel
495 Buenzli, Pon Hwa Lin, Radoslaw Zielinski, Radu Greab, Randal L.
496 Schwartz, Richard Chen, Robin Barker, Roy Fielding, Sander van Zoest,
497 Sean M. Burke, shildreth, Slaven Rezic, Steve A Fink, Steve Hay, Steven
498 Butler, Steve_Kilbane, Takanori Ugai, Thomas Lotterer, Tim Bunce, Tom
499 Hughes, Tony Finch, Ville Skyttä, Ward Vandewege, William York, Yale
500 Huang, and Yitzchak Scott-Thoennes.
501
502 LWP owes a lot in motivation, design, and code, to the libwww-perl
503 library for Perl4 by Roy Fielding, which included work from Alberto
504 Accomazzi, James Casey, Brooks Cutter, Martijn Koster, Oscar
505 Nierstrasz, Mel Melchner, Gertjan van Oosten, Jared Rhine, Jack
506 Shirazi, Gene Spafford, Marc VanHeyningen, Steven E. Brenner, Marion
507 Hakanson, Waldemar Kebsch, Tony Sanders, and Larry Wall; see the
508 libwww-perl-0.40 library for details.
509
511 Copyright 1995-2009, Gisle Aas
512 Copyright 1995, Martijn Koster
513
514 This library is free software; you can redistribute it and/or modify it
515 under the same terms as Perl itself.
516
518 The latest version of this library is likely to be available from CPAN
519 as well as:
520
521 http://github.com/libwww-perl/libwww-perl
522
523 The best place to discuss this code is on the <libwww@perl.org> mailing
524 list.
525
526
527
528perl v5.28.0 2018-07-16 LWP(3)