home *** CD-ROM | disk | FTP | other *** search
/ CLIX - Fazer Clix Custa Nix / CLIX-CD.cdr / mac / lib / LWP / Simple.pm < prev    next >
Text File  |  1997-11-18  |  5KB  |  230 lines

  1. #
  2. # $Id: Simple.pm,v 1.1 1997/11/18 00:33:10 neeri Exp $
  3.  
  4. =head1 NAME
  5.  
  6. get, head, getprint, getstore, mirror - Procedural LWP interface
  7.  
  8. =head1 SYNOPSIS
  9.  
  10.  perl -MLWP::Simple -e 'getprint "http://www.sn.no"'
  11.  
  12.  use LWP::Simple;
  13.  $content = get("http://www.sn.no/")
  14.  if (mirror("http://www.sn.no/", "foo") == RC_NOT_MODIFIED) {
  15.      ...
  16.  }
  17.  if (is_success(getprint("http://www.sn.no/"))) {
  18.      ...
  19.  }
  20.  
  21. =head1 DESCRIPTION
  22.  
  23. This interface is intended for those who want a simplified view of the
  24. libwww-perl library.  This interface should also be suitable for
  25. one-liners.  If you need more control or access to the header fields
  26. in the requests sent and responses received you should use the full OO
  27. interface provided by the LWP::UserAgent module.
  28.  
  29. This following functions are provided (and exported) by this module:
  30.  
  31. =over 3
  32.  
  33. =item get($url)
  34.  
  35. This function will get the document identified by the given URL.  The
  36. get() function will return the document if successful or 'undef' if it
  37. fails.  The $url argument can be either a simple string or a reference
  38. to a URI::URL object.
  39.  
  40. You will not be able to examine the response code or response headers
  41. (like I<Content-Type>) when you are accessing the web using this
  42. function.  If you need this you should use the full OO interface.
  43.  
  44. =item head($url)
  45.  
  46. Get document headers. Returns the following values if successful:
  47. ($content_type, $document_length, $modified_time, $expires, $server)
  48.  
  49. Returns an empty list if it fails.
  50.  
  51. =item getprint($url)
  52.  
  53. Get and print a document identified by a URL. The document is printet
  54. on STDOUT. The error message (formatted as HTML) is printed on STDERR
  55. if the request fails.  The return value is the HTTP response code.
  56.  
  57. =item getstore($url, $file)
  58.  
  59. Gets a document identified by a URL and stores it in the file. The
  60. return value is the HTTP response code.
  61.  
  62. =item mirror($url, $file)
  63.  
  64. Get and store a document identified by a URL, using
  65. I<If-modified-since>, and checking of the I<Content-Length>.  Returns
  66. the HTTP response code.
  67.  
  68. =back
  69.  
  70. This module also exports the HTTP::Status constants and
  71. procedures.  These can be used when you check the response code from
  72. getprint(), getstore() and mirror().  The constants are:
  73.  
  74.    RC_CONTINUE
  75.    RC_SWITCHING_PROTOCOLS
  76.    RC_OK
  77.    RC_CREATED
  78.    RC_ACCEPTED
  79.    RC_NON_AUTHORITATIVE_INFORMATION
  80.    RC_NO_CONTENT
  81.    RC_RESET_CONTENT
  82.    RC_PARTIAL_CONTENT
  83.    RC_MULTIPLE_CHOICES
  84.    RC_MOVED_PERMANENTLY
  85.    RC_MOVED_TEMPORARILY
  86.    RC_SEE_OTHER
  87.    RC_NOT_MODIFIED
  88.    RC_USE_PROXY
  89.    RC_BAD_REQUEST
  90.    RC_UNAUTHORIZED
  91.    RC_PAYMENT_REQUIRED
  92.    RC_FORBIDDEN
  93.    RC_NOT_FOUND
  94.    RC_METHOD_NOT_ALLOWED
  95.    RC_NOT_ACCEPTABLE
  96.    RC_PROXY_AUTHENTICATION_REQUIRED
  97.    RC_REQUEST_TIMEOUT
  98.    RC_CONFLICT
  99.    RC_GONE
  100.    RC_LENGTH_REQUIRED
  101.    RC_PRECONDITION_FAILED
  102.    RC_REQUEST_ENTITY_TOO_LARGE
  103.    RC_REQUEST_URI_TOO_LARGE
  104.    RC_UNSUPPORTED_MEDIA_TYPE
  105.    RC_INTERNAL_SERVER_ERROR
  106.    RC_NOT_IMPLEMENTED
  107.    RC_BAD_GATEWAY
  108.    RC_SERVICE_UNAVAILABLE
  109.    RC_GATEWAY_TIMEOUT
  110.    RC_HTTP_VERSION_NOT_SUPPORTED
  111.  
  112. The HTTP::Status classification functions are:
  113.  
  114. =over 3
  115.  
  116. =item is_success($rc)
  117.  
  118. Check if response code indicated successfull request.
  119.  
  120. =item is_error($rc)
  121.  
  122. Check if response code indicated that an error occured.
  123.  
  124. =back
  125.  
  126. The module will also export the LWP::UserAgent object as C<$ua> if you
  127. ask for it explicitly.
  128.  
  129. The user agent created by this module will identify itself as
  130. "LWP::Simple/0.00" and will initialize its proxy defaults from the
  131. environment (by calling $ua->env_proxy).
  132.  
  133. =head1 SEE ALSO
  134.  
  135. L<LWP>, L<LWP::UserAgent>, L<HTTP::Status>, L<request>, L<mirror>
  136.  
  137. =cut
  138.  
  139.  
  140. package LWP::Simple;
  141.  
  142. require Exporter;
  143. @ISA = qw(Exporter);
  144. @EXPORT = qw(get head getprint getstore mirror);  # note additions below
  145. @EXPORT_OK = qw($ua);
  146.  
  147. # We also export everything from HTTP::Status
  148. use HTTP::Status;
  149. push(@EXPORT, @HTTP::Status::EXPORT);
  150.  
  151. require LWP;
  152. require LWP::UserAgent;
  153. use HTTP::Date qw(str2time);
  154. use Carp;
  155.  
  156. $ua = new LWP::UserAgent;  # we create a global UserAgent object
  157. $ua->agent("LWP::Simple/$LWP::VERSION");
  158. $ua->env_proxy;
  159.  
  160.  
  161. sub get ($)
  162. {
  163.     my($url) = @_;
  164.  
  165.     my $request = new HTTP::Request 'GET', $url;
  166.     my $response = $ua->request($request);
  167.  
  168.     return $response->content if $response->is_success;
  169.     return undef;
  170. }
  171.  
  172.  
  173. sub head ($)
  174. {
  175.     my($url) = @_;
  176.  
  177.     my $request = new HTTP::Request HEAD => $url;
  178.     my $response = $ua->request($request);
  179.  
  180.     if ($response->is_success) {
  181.     return $response unless wantarray;
  182.     return ($response->header('Content-Type'),
  183.         $response->header('Content-Length'),
  184.         str2time($response->header('Last-Modified')),
  185.         str2time($response->header('Expires')),
  186.         $response->header('Server'),
  187.            );
  188.     } else {
  189.     return wantarray ? () : '';
  190.     }
  191. }
  192.  
  193.  
  194. sub getprint ($)
  195. {
  196.     my($url) = @_;
  197.  
  198.     my $request = new HTTP::Request 'GET', $url;
  199.     my $response = $ua->request($request);
  200.     local($\) = ""; # ensure standard $OUTPUT_RECORD_SEPARATOR
  201.     if ($response->is_success) {
  202.     my $content;
  203.     ($content = $response->content) =~ s/\012\015?|\015/\n/g;
  204.     print $content;
  205.     } else {
  206.     print STDERR $response->error_as_HTML;
  207.     }
  208.     $response->code;
  209. }
  210.  
  211.  
  212. sub getstore ($$)
  213. {
  214.     my($url, $file) = @_;
  215.  
  216.     my $request = new HTTP::Request 'GET', $url;
  217.     my $response = $ua->request($request, $file);
  218.  
  219.     $response->code;
  220. }
  221.  
  222. sub mirror ($$)
  223. {
  224.     my($url, $file) = @_;
  225.     my $response = $ua->mirror($url, $file);
  226.     $response->code;
  227. }
  228.  
  229. 1;
  230.