home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_ste.zip / LWP.pm < prev    next >
Text File  |  1997-11-20  |  19KB  |  611 lines

  1. #
  2. # $Id: LWP.pm,v 1.57 1997/11/21 00:35:42 aas Exp $
  3.  
  4. package LWP;
  5.  
  6. $VERSION = "5.16";
  7. sub Version { $VERSION; }
  8.  
  9. require 5.004;
  10. require LWP::UserAgent;  # this should load everything you need
  11.  
  12. 1;
  13.  
  14. __END__
  15.  
  16. =head1 NAME
  17.  
  18. LWP - Library for WWW access in Perl
  19.  
  20. =head1 SYNOPSIS
  21.  
  22.   use LWP;
  23.   print "This is libwww-perl-$LWP::VERSION\n";
  24.  
  25.  
  26. =head1 DESCRIPTION
  27.  
  28. Libwww-perl is a collection of Perl modules which provides a simple
  29. and consistent programming interface (API) to the World-Wide Web.  The
  30. main focus of the library is to provide classes and functions that
  31. allow you to write WWW clients, thus libwww-perl said to be a WWW
  32. client library. The library also contain modules that are of more
  33. general use.
  34.  
  35. The main architecture of the library is object oriented.  The user
  36. agent, requests sent and responses received from the WWW server are
  37. all represented by objects.  This makes a simple and powerful
  38. interface to these services.  The interface should be easy to extend
  39. and customize for your needs.
  40.  
  41. The main features of the library are:
  42.  
  43. =over 3
  44.  
  45. =item *
  46.  
  47. Contains various reusable components (modules) that can be
  48. used separately or together.
  49.  
  50. =item *
  51.  
  52. Provides an object oriented model of HTTP-style communication.  Within
  53. this framework we currently support access to http, gopher, ftp, news,
  54. file, and mailto resources.
  55.  
  56. =item *
  57.  
  58. The library be used through the full object oriented interface or
  59. through a very simple procedural interface.
  60.  
  61. =item *
  62.  
  63. Support the basic and digest authorization schemes.
  64.  
  65. =item *
  66.  
  67. Transparent redirect handling.
  68.  
  69. =item *
  70.  
  71. Supports access through proxy servers.
  72.  
  73. =item *
  74.  
  75. URL handling (both absolute and relative URLs are supported).
  76.  
  77. =item *
  78.  
  79. A parser for F<robots.txt> files and a framework for constructing robots.
  80.  
  81. =item *
  82.  
  83. An experimental HTML parser and formatters (for PostScript and plain
  84. text).
  85.  
  86. =item *
  87.  
  88. The library can cooperate with Tk.  A simple Tk-based GUI browser
  89. called 'tkweb' is distributed with the Tk extension for perl.
  90.  
  91. =item *
  92.  
  93. An implementation of the HTTP content negotiation algorithm that can
  94. be used both in protocol modules and in server scripts (like CGI
  95. scripts).
  96.  
  97. =item *
  98.  
  99. A simple command line client application called C<lwp-request>.
  100.  
  101. =back
  102.  
  103.  
  104. =head1 HTTP STYLE COMMUNICATION
  105.  
  106.  
  107. The libwww-perl library is based on HTTP style communication. This
  108. section try to describe what that means.
  109.  
  110. Let us start with this quote from the HTTP specification document
  111. <URL:http://www.w3.org/pub/WWW/Protocols/>:
  112.  
  113. =over 3
  114.  
  115. =item
  116.  
  117. The HTTP protocol is based on a request/response paradigm. A client
  118. establishes a connection with a server and sends a request to the
  119. server in the form of a request method, URI, and protocol version,
  120. followed by a MIME-like message containing request modifiers, client
  121. information, and possible body content. The server responds with a
  122. status line, including the message's protocol version and a success or
  123. error code, followed by a MIME-like message containing server
  124. information, entity meta-information, and possible body content.
  125.  
  126. =back
  127.  
  128. What this means to libwww-perl is that communication always take place
  129. through these steps: First a I<request> object is created and
  130. configured. This object is then passed to a server and we get a
  131. I<response> object in return that we can examine. A request is always
  132. independent of any previous requests, i.e. the service is stateless.
  133. The same simple model is used for any kind of service we want to
  134. access.
  135.  
  136. For example, if we want to fetch a document from a remote file server,
  137. then we send it a request that contains a name for that document and
  138. the response will contain the document itself.  If we access a search
  139. engine, then the content of the request will contain the query
  140. parameters and the response will contain the query result.  If we want
  141. to send a mail message to somebody then we send a request object which
  142. contains our message to the mail server and the response object will
  143. contain an acknowledgment that tells us that the message has been
  144. accepted and will be forwarded to the recipient(s).
  145.  
  146. It is as simple as that!
  147.  
  148.  
  149. =head2 The Request Object
  150.  
  151. The request object has the class name C<HTTP::Request> in
  152. libwww-perl. The fact that the class name use C<HTTP::> as a name
  153. prefix only implies that we use the HTTP model of communication. It
  154. does not limit the kind of services we can try to pass this I<request>
  155. to.  For instance, we will send C<HTTP::Request>s both to ftp and
  156. gopher servers, as well as to the local file system.
  157.  
  158. The main attributes of the request objects are:
  159.  
  160. =over 3
  161.  
  162. =item *
  163.  
  164. The B<method> is a short string that tells what kind of
  165. request this is.  The most used methods are B<GET>, B<PUT>,
  166. B<POST> and B<HEAD>.
  167.  
  168. =item *
  169.  
  170. The B<url> is a string denoting the protocol, server and
  171. the name of the "document" we want to access.  The B<url> might
  172. also encode various other parameters.
  173.  
  174. =item *
  175.  
  176. The B<headers> contain additional information about the
  177. request and can also used to describe the content.  The headers
  178. is a set of keyword/value pairs.
  179.  
  180. =item *
  181.  
  182. The B<content> is an arbitrary amount of data.
  183.  
  184. =back
  185.  
  186. =head2 The Response Object
  187.  
  188. The request object has the class name C<HTTP::Response> in
  189. libwww-perl.  The main attributes of objects of this class are:
  190.  
  191. =over 3
  192.  
  193. =item *
  194.  
  195. The B<code> is a numerical value that encode the overall
  196. outcome of the request.
  197.  
  198. =item *
  199.  
  200. The B<message> is a short (human readable) string that
  201. corresponds to the I<code>.
  202.  
  203. =item *
  204.  
  205. The B<headers> contain additional information about the
  206. response and they also describe the content.
  207.  
  208. =item *
  209.  
  210. The B<content> is an arbitrary amount of data.
  211.  
  212. =back
  213.  
  214. Since we don't want to handle all possible I<code> values directly in
  215. our programs, the libwww-perl response object have methods that can be
  216. used to query what kind of response this is.  The most commonly used
  217. response classification methods are:
  218.  
  219. =over 3
  220.  
  221. =item is_success()
  222.  
  223. The request was was successfully received, understood or accepted.
  224.  
  225. =item is_error()
  226.  
  227. The request failed.  The server or the resource might not be
  228. available, access to the resource might be denied or other things might
  229. have failed for some reason.
  230.  
  231. =back
  232.  
  233. =head2 The User Agent
  234.  
  235. Let us assume that we have created a I<request> object. What do we
  236. actually do with it in order to receive a I<response>?
  237.  
  238. The answer is that you pass it on to a I<user agent> object and this
  239. object will take care of all the things that need to be done
  240. (low-level communication and error handling). The user agent will give
  241. you back a I<response> object. The user agent represents your
  242. application on the network and it provides you with an interface that
  243. can accept I<requests> and will return I<responses>.
  244.  
  245. You should think about the user agent as an interface layer between
  246. your application code and the network.  Through this interface you are
  247. able to access the various servers on the network.
  248.  
  249. The libwww-perl class name for the user agent is
  250. C<LWP::UserAgent>. Every libwww-perl application that wants to
  251. communicate should create at least one object of this kind. The main
  252. method provided by this object is request(). This method takes an
  253. C<HTTP::Request> object as argument and will (eventually) return a
  254. C<HTTP::Response> object.
  255.  
  256. The user agent has many other attributes that lets you
  257. configure how it will interact with the network and with your
  258. application code.
  259.  
  260. =over 3
  261.  
  262. =item *
  263.  
  264. The B<timeout> specify how much time we give remote servers in
  265. creating responses before the library disconnect and creates an
  266. internal I<timeout> response.
  267.  
  268. =item *
  269.  
  270. The B<agent> specify the name that your application should use when it
  271. presents itself on the network.
  272.  
  273. =item *
  274.  
  275. The B<from> attribute can be set to the e-mail address of the person
  276. responsible for running the application.  If this is set, then the
  277. address will be sent to the servers with every request.
  278.  
  279. =item *
  280.  
  281. The B<use_alarm> specify if it is OK for the user agent to use the
  282. alarm(2) system to implement timeouts.
  283.  
  284. =item *
  285.  
  286. The B<use_eval> specify if the agent should raise an
  287. exception (C<die> in perl) if an error condition occur.
  288.  
  289. =item *
  290.  
  291. The B<parse_head> specify whether we should initialize response
  292. headers from the E<lt>head> section of HTML documents.
  293.  
  294. =item *
  295.  
  296. The B<proxy> and B<no_proxy> specify if and when communication should
  297. go through a proxy server. <URL:http://www.w3.org/pub/WWW/Proxies/>
  298.  
  299. =item *
  300.  
  301. The B<credentials> provide a way to set up user names and
  302. passwords that is needed to access certain services.
  303.  
  304. =back
  305.  
  306. Many applications would want even more control over how they interact
  307. with the network and they get this by specializing the
  308. C<LWP::UserAgent> by sub-classing.  The library provide a
  309. specialization called C<LWP::RobotUA> that is used by robot
  310. applications.
  311.  
  312. =head2 An Example
  313.  
  314. This example shows how the user agent, a request and a response are
  315. represented in actual perl code:
  316.  
  317.   # Create a user agent object
  318.   use LWP::UserAgent;
  319.   $ua = new LWP::UserAgent;
  320.   $ua->agent("AgentName/0.1 " . $ua->agent);
  321.  
  322.   # Create a request
  323.   my $req = new HTTP::Request POST => 'http://www.perl.com/cgi-bin/BugGlimpse';
  324.   $req->content_type('application/x-www-form-urlencoded');
  325.   $req->content('match=www&errors=0');
  326.  
  327.   # Pass request to the user agent and get a response back
  328.   my $res = $ua->request($req);
  329.  
  330.   # Check the outcome of the response
  331.   if ($res->is_success) {
  332.       print $res->content;
  333.   } else {
  334.       print "Bad luck this time\n";
  335.   }
  336.  
  337. The $ua is created once when the application starts up.  New request
  338. objects are normally created for each request sent.
  339.  
  340.  
  341. =head1 NETWORK SUPPORT
  342.  
  343. This section goes through the various protocol schemes and describe
  344. the HTTP style methods that are supported and the headers that might
  345. have any effect.
  346.  
  347. For all requests, a "User-Agent" header is added and initialized from
  348. the $ua->agent value before the request is handed to the network
  349. layer.  In the same way, a "From" header is initialized from the
  350. $ua->from value.
  351.  
  352. For all responses, the library will add a header called "Client-Date".
  353. This header will encode the time when the response was received by
  354. your application.  This format and semantics of the header is just
  355. like the server created "Date" header.
  356.  
  357. =head2 HTTP Requests
  358.  
  359. HTTP request are really just handed off to an HTTP server and it will
  360. decide what happens.  Few servers implement methods beside the usual
  361. "GET", "HEAD", "POST" and "PUT" but CGI-scripts can really implement
  362. any method they like.
  363.  
  364. If the server is not available then the library will generate an
  365. internal error response.
  366.  
  367. The library automatically adds a "Host" and a "Content-Length" header
  368. to the HTTP request before it is sent over the network.
  369.  
  370. For GET request you might want to add the "If-Modified-Since" header
  371. to make the request conditional.
  372.  
  373. For POST request you should add the "Content-Type" header.  When you
  374. try to emulate HTML E<lt>FORM> handling you should usually let the value
  375. of the "Content-Type" header be "application/x-www-form-urlencoded".
  376. See L<lwpcook> for examples of this.
  377.  
  378. The libwww-perl HTTP implementation currently support the HTTP/1.0
  379. protocol.  HTTP/0.9 servers are also handled correctly.
  380.  
  381. The library allows you to access proxy server through HTTP.  This
  382. means that you can set up the library to forward all types of request
  383. through the HTTP protocol module.  See L<LWP::UserAgent> for
  384. documentation of this.
  385.  
  386.  
  387.  
  388.  
  389. =head2 FTP Requests
  390.  
  391. The library currently support GET, HEAD and PUT requests.  GET will
  392. retrieve a file or a directory listing from an FTP server.  PUT will
  393. store a file on a ftp server.
  394.  
  395. You can specify a ftp account for servers that want this in addition
  396. user name and password.  This is specified by passing an "Account"
  397. header in the request.
  398.  
  399. User name/password can be specified using basic authorization or be
  400. encoded in the URL.  Bad logins return an UNAUTHORIZED response with
  401. "WWW-Authenticate: Basic" and can be treated as basic authorization
  402. for HTTP.
  403.  
  404. The library support ftp ASCII transfer mode by specifying the "type=a"
  405. parameter in the URL.
  406.  
  407. Directory listings are by default returned unprocessed (as returned
  408. from the ftp server) with the content media type reported to be
  409. "text/ftp-dir-listing". The C<File::Listing> module provide functionality
  410. for parsing of these directory listing.
  411.  
  412. The ftp module is also able to convert directory listings to HTML and
  413. this can be requested via the standard HTTP content negotiation
  414. mechanisms (add an "Accept: text/html" header in the request if you
  415. want this).
  416.  
  417. The normal file retrievals, the "Content-Type" is guessed based on the
  418. file name suffix. See L<LWP::MediaTypes>.
  419.  
  420. The "If-Modified-Since" header is not honored yet.
  421.  
  422. Example:
  423.  
  424.   $req = HTTP::Request->new(GET => 'ftp://me:passwd@ftp.some.where.com/');
  425.   $req->header(Accept => "text/html, */*;q=0.1");
  426.  
  427. =head2 News Requests
  428.  
  429. Access to the USENET News system is implemented through the NNTP
  430. protocol.  The name of the news server is obtained from the
  431. NNTP_SERVER environment variable and defaults to "news".  It is not
  432. possible to specify the hostname of the NNTP server in the news:-URLs.
  433.  
  434. The library support GET and HEAD to retrieve news articles through the
  435. NNTP protocol.  You can also post articles to newsgroups by using
  436. (surprise!) the POST method.
  437.  
  438. GET on newsgroups is not implemented yet.
  439.  
  440. Examples:
  441.  
  442.   $req = HTTP::Request->new(GET => 'news:abc1234@a.sn.no');
  443.  
  444.   $req = HTTP::Request->new(POST => 'news:comp.lang.perl.test');
  445.   $req->header(Subject => 'This is a test',
  446.                From    => 'me@some.where.org');
  447.   $req->content(<<EOT);
  448.   This is the content of the message that we are sending to
  449.   the world.
  450.   EOT
  451.  
  452.  
  453. =head2 Gopher Request
  454.  
  455. The library supports the GET and HEAD method for gopher request.  All
  456. request header values are ignored.  HEAD cheats and will return a
  457. response without even talking to server.
  458.  
  459. Gopher menus are always converted to HTML.
  460.  
  461. The response "Content-Type" is generated from the document type
  462. encoded (as the first letter) in the request URL path itself.
  463.  
  464. Example:
  465.  
  466.   $req = HTTP::Request->new('GET', 'gopher://gopher.sn.no/');
  467.  
  468.  
  469.  
  470. =head2 File Request
  471.  
  472. The library supports GET and HEAD methods for file requests.  The
  473. "If-Modified-Since" header is supported.  All other headers are
  474. ignored.  The I<host> component of the file URL must be empty or set
  475. to "localhost".  Any other I<host> value will be treated as an error.
  476.  
  477. Directories are always converted to an HTML document.  For normal
  478. files, the "Content-Type" and "Content-Encoding" in the response are
  479. guessed based on the file suffix.
  480.  
  481. Example:
  482.  
  483.   $req = HTTP::Request->new(GET => 'file:/etc/passwd');
  484.  
  485.  
  486. =head2 Mailto Request
  487.  
  488. You can send (aka "POST") mail messages using the library.  All
  489. headers specified for the request are passed on to the mail system.
  490. The "To" header is initialized from the mail address in the URL.
  491.  
  492. Example:
  493.  
  494.   $req = HTTP::Request->new(POST => 'mailto:libwww-perl-request@ics.uci.edu');
  495.   $req->header("Subject", "subscribe");
  496.   $req->content("Please subscribe me to the libwww-perl mailing list!\n");
  497.  
  498.  
  499. =head1 OVERVIEW OF CLASSES AND PACKAGES
  500.  
  501. This table should give you a quick overview of the classes provided by the
  502. library. Indentation shows class inheritance.
  503.  
  504.  LWP::MemberMixin   -- Access to member variables of Perl5 classes
  505.    LWP::UserAgent   -- WWW user agent class
  506.      LWP::RobotUA   -- When developing a robot applications
  507.    LWP::Protocol          -- Interface to various protocol schemes
  508.      LWP::Protocol::http  -- http:// access
  509.      LWP::Protocol::file  -- file:// access
  510.      LWP::Protocol::ftp   -- ftp:// access
  511.      ...
  512.  
  513.  LWP::Socket        -- Socket creation and IO
  514.  
  515.  HTTP::Headers      -- MIME/RFC822 style header (used by HTTP::Message)
  516.  HTTP::Message      -- HTTP style message
  517.    HTTP::Request    -- HTTP request
  518.    HTTP::Response   -- HTTP response
  519.  HTTP::Daemon       -- A HTTP server class
  520.  
  521.  URI::URL           -- Uniform Resource Locators
  522.  
  523.  WWW::RobotRules    -- Parse robots.txt files
  524.    WWW::RobotRules::AnyDBM_File -- Persistent RobotRules
  525.  
  526.  HTML::Parser       -- Parse HTML documents
  527.    HTML::TreeBuilder-- Build a HTML syntax tree
  528.    HTML::HeadParser -- Parse the <HEAD> section of a HTML document
  529.    HTML::LinkExtor  -- Extract links from a HTML document
  530.  HTML::Element      -- Building block for the HTML::TreeBuilder
  531.  HTML::Formatter    -- Convert HTML syntax trees to readable formats
  532.    HTML::FormatText -- Output is plain text
  533.    HTML::FormatPS   -- Output is PostScript
  534.  
  535. The following modules provide various functions and definitions.
  536.  
  537.  LWP                -- This file.  Library version number and documentation.
  538.  LWP::MediaTypes    -- MIME types configuration (text/html etc.)
  539.  LWP::Debug         -- Debug logging module
  540.  LWP::Simple        -- Simplified procedural interface for common functions
  541.  HTTP::Status       -- HTTP status code (200 OK etc)
  542.  HTTP::Date         -- Date parsing module for HTTP date formats
  543.  HTTP::Negotiate    -- HTTP content negotiation calculation
  544.  HTML::Entities     -- Expand or unexpand entities in HTML text
  545.  File::Listing      -- Parse directory listings
  546.  
  547. HTTP use the Base64 encoding at some places.  The QuotedPrint module
  548. is just included to make the MIME:: collection more complete.
  549.  
  550.  MIME::Base64       -- Base64 encoding/decoding routines
  551.  MIME::QuotedPrint  -- Quoted Printable encoding/decoding routines
  552.  
  553. The following modules does not have much to do with the World Wide
  554. Web, but are included just because I am lazy and did not bother to
  555. make separate distributions for them.  Regard them as bonus, provided
  556. free for your pleasure.
  557.  
  558.  Font::AFM          -- Parse Adobe Font Metric files
  559.  File::CounterFile  -- Persistent counter class
  560.  
  561.  
  562. =head1 MORE DOCUMENTATION
  563.  
  564. All modules contain detailed information on the interfaces they
  565. provide.  The L<lwpcook> is the libwww-perl cookbook that contain
  566. examples of typical usage of the library.  You might want to take a
  567. look at how the scripts C<lwp-request>, C<lwp-rget> and C<lwp-mirror>
  568. are implemented.
  569.  
  570. =head1 BUGS
  571.  
  572. The library can not handle multiple simultaneous requests yet.  The
  573. HTML:: modules are still experimental.  Also, check out what's left in
  574. the TODO file.
  575.  
  576. =head1 ACKNOWLEDGEMENTS
  577.  
  578. This package owes a lot in motivation, design, and code, to the
  579. libwww-perl library for Perl 4, maintained by Roy Fielding
  580. E<lt>fielding@ics.uci.edu>.
  581.  
  582. That package used work from Alberto Accomazzi, James Casey, Brooks
  583. Cutter, Martijn Koster, Oscar Nierstrasz, Mel Melchner, Gertjan van
  584. Oosten, Jared Rhine, Jack Shirazi, Gene Spafford, Marc VanHeyningen,
  585. Steven E. Brenner, Marion Hakanson, Waldemar Kebsch, Tony Sanders, and
  586. Larry Wall; see the libwww-perl-0.40 library for details.
  587.  
  588. The primary architect for this Perl 5 library is Martijn Koster and
  589. Gisle Aas, with lots of help from Graham Barr, Tim Bunce, Andreas
  590. Koenig, Jared Rhine, and Jack Shirazi.
  591.  
  592.  
  593. =head1 COPYRIGHT
  594.  
  595.   Copyright 1995-1997, Gisle Aas
  596.   Copyright 1995, Martijn Koster
  597.  
  598. This library is free software; you can redistribute it and/or
  599. modify it under the same terms as Perl itself.
  600.  
  601. =head1 AVAILABILITY
  602.  
  603. The latest version of this library is likely to be available from:
  604.  
  605.  http://www.sn.no/libwww-perl/
  606.  
  607. The best place to discuss this code is on the
  608. <libwww-perl@ics.uci.edu> mailing list.
  609.  
  610. =cut
  611.