Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

586 lines
19 KiB

  1. #
  2. # $Id: LWP.pm,v 1.97 2001/03/14 21:25:28 gisle Exp $
  3. package LWP;
  4. $VERSION = "5.51";
  5. sub Version { $VERSION; }
  6. require 5.004;
  7. require LWP::UserAgent; # this should load everything you need
  8. 1;
  9. __END__
  10. =head1 NAME
  11. LWP - Library for WWW access in Perl
  12. =head1 SYNOPSIS
  13. use LWP;
  14. print "This is libwww-perl-$LWP::VERSION\n";
  15. =head1 DESCRIPTION
  16. Libwww-perl is a collection of Perl modules which provides a simple
  17. and consistent application programming interface (API) to the World-Wide Web. The
  18. main focus of the library is to provide classes and functions that
  19. allow you to write WWW clients, thus libwww-perl is a WWW
  20. client library. The library also contain modules that are of more
  21. general use.
  22. Most modules in this library are object oriented. The user
  23. agent, requests sent and responses received from the WWW server are
  24. all represented by objects. This makes a simple and powerful
  25. interface to these services. The interface should be easy to extend
  26. and customize for your needs.
  27. The main features of the library are:
  28. =over 3
  29. =item *
  30. Contains various reusable components (modules) that can be
  31. used separately or together.
  32. =item *
  33. Provides an object oriented model of HTTP-style communication. Within
  34. this framework we currently support access to http, https, gopher, ftp, news,
  35. file, and mailto resources.
  36. =item *
  37. Provides a full object oriented interface or
  38. a very simple procedural interface.
  39. =item *
  40. Supports the basic and digest authorization schemes.
  41. =item *
  42. Supports transparent redirect handling.
  43. =item *
  44. Supports access through proxy servers.
  45. =item *
  46. Provides parser for F<robots.txt> files and a framework for constructing robots.
  47. =item *
  48. Cooperates with Tk. A simple Tk-based GUI browser
  49. called 'tkweb' is distributed with the Tk extension for perl.
  50. =item *
  51. Implements HTTP content negotiation algorithm that can
  52. be used both in protocol modules and in server scripts (like CGI
  53. scripts).
  54. =item *
  55. Supports HTTP cookies.
  56. =item *
  57. A simple command line client application called C<lwp-request>.
  58. =back
  59. =head1 HTTP STYLE COMMUNICATION
  60. The libwww-perl library is based on HTTP style communication. This
  61. section tries to describe what that means.
  62. Let us start with this quote from the HTTP specification document
  63. <URL:http://www.w3.org/pub/WWW/Protocols/>:
  64. =over 3
  65. =item
  66. The HTTP protocol is based on a request/response paradigm. A client
  67. establishes a connection with a server and sends a request to the
  68. server in the form of a request method, URI, and protocol version,
  69. followed by a MIME-like message containing request modifiers, client
  70. information, and possible body content. The server responds with a
  71. status line, including the message's protocol version and a success or
  72. error code, followed by a MIME-like message containing server
  73. information, entity meta-information, and possible body content.
  74. =back
  75. What this means to libwww-perl is that communication always take place
  76. through these steps: First a I<request> object is created and
  77. configured. This object is then passed to a server and we get a
  78. I<response> object in return that we can examine. A request is always
  79. independent of any previous requests, i.e. the service is stateless.
  80. The same simple model is used for any kind of service we want to
  81. access.
  82. For example, if we want to fetch a document from a remote file server,
  83. then we send it a request that contains a name for that document and
  84. the response will contain the document itself. If we access a search
  85. engine, then the content of the request will contain the query
  86. parameters and the response will contain the query result. If we want
  87. to send a mail message to somebody then we send a request object which
  88. contains our message to the mail server and the response object will
  89. contain an acknowledgment that tells us that the message has been
  90. accepted and will be forwarded to the recipient(s).
  91. It is as simple as that!
  92. =head2 The Request Object
  93. The libwww-perl request object has the class name C<HTTP::Request>.
  94. The fact that the class name uses C<HTTP::> as a
  95. prefix only implies that we use the HTTP model of communication. It
  96. does not limit the kind of services we can try to pass this I<request>
  97. to. For instance, we will send C<HTTP::Request>s both to ftp and
  98. gopher servers, as well as to the local file system.
  99. The main attributes of the request objects are:
  100. =over 3
  101. =item *
  102. The B<method> is a short string that tells what kind of
  103. request this is. The most used methods are B<GET>, B<PUT>,
  104. B<POST> and B<HEAD>.
  105. =item *
  106. The B<url> is a string denoting the protocol, server and
  107. the name of the "document" we want to access. The B<url> might
  108. also encode various other parameters.
  109. =item *
  110. The B<headers> contain additional information about the
  111. request and can also used to describe the content. The headers
  112. are a set of keyword/value pairs.
  113. =item *
  114. The B<content> is an arbitrary amount of data.
  115. =back
  116. =head2 The Response Object
  117. The libwww-perl response object has the class name C<HTTP::Response>.
  118. The main attributes of objects of this class are:
  119. =over 3
  120. =item *
  121. The B<code> is a numerical value that indicates the overall
  122. outcome of the request.
  123. =item *
  124. The B<message> is a short, human readable string that
  125. corresponds to the I<code>.
  126. =item *
  127. The B<headers> contain additional information about the
  128. response and describe the content.
  129. =item *
  130. The B<content> is an arbitrary amount of data.
  131. =back
  132. Since we don't want to handle all possible I<code> values directly in
  133. our programs, a libwww-perl response object has methods that can be
  134. used to query what kind of response this is. The most commonly used
  135. response classification methods are:
  136. =over 3
  137. =item is_success()
  138. The request was was successfully received, understood or accepted.
  139. =item is_error()
  140. The request failed. The server or the resource might not be
  141. available, access to the resource might be denied or other things might
  142. have failed for some reason.
  143. =back
  144. =head2 The User Agent
  145. Let us assume that we have created a I<request> object. What do we
  146. actually do with it in order to receive a I<response>?
  147. The answer is that you pass it to a I<user agent> object and this
  148. object takes care of all the things that need to be done
  149. (like low-level communication and error handling) and returns
  150. a I<response> object. The user agent represents your
  151. application on the network and provides you with an interface that
  152. can accept I<requests> and return I<responses>.
  153. The user agent is an interface layer between
  154. your application code and the network. Through this interface you are
  155. able to access the various servers on the network.
  156. The libwww-perl class name for the user agent is
  157. C<LWP::UserAgent>. Every libwww-perl application that wants to
  158. communicate should create at least one object of this class. The main
  159. method provided by this object is request(). This method takes an
  160. C<HTTP::Request> object as argument and (eventually) returns a
  161. C<HTTP::Response> object.
  162. The user agent has many other attributes that let you
  163. configure how it will interact with the network and with your
  164. application.
  165. =over 3
  166. =item *
  167. The B<timeout> specifies how much time we give remote servers to
  168. respond before the library disconnects and creates an
  169. internal I<timeout> response.
  170. =item *
  171. The B<agent> specifies the name that your application should use when it
  172. presents itself on the network.
  173. =item *
  174. The B<from> attribute can be set to the e-mail address of the person
  175. responsible for running the application. If this is set, then the
  176. address will be sent to the servers with every request.
  177. =item *
  178. The B<parse_head> specifies whether we should initialize response
  179. headers from the E<lt>head> section of HTML documents.
  180. =item *
  181. The B<proxy> and B<no_proxy> attributes specify if and when to go through
  182. a proxy server. <URL:http://www.w3.org/pub/WWW/Proxies/>
  183. =item *
  184. The B<credentials> provide a way to set up user names and
  185. passwords needed to access certain services.
  186. =back
  187. Many applications want even more control over how they interact
  188. with the network and they get this by sub-classing
  189. C<LWP::UserAgent>. The library includes a
  190. sub-class, C<LWP::RobotUA>, for robot applications.
  191. =head2 An Example
  192. This example shows how the user agent, a request and a response are
  193. represented in actual perl code:
  194. # Create a user agent object
  195. use LWP::UserAgent;
  196. $ua = new LWP::UserAgent;
  197. $ua->agent("AgentName/0.1 " . $ua->agent);
  198. # Create a request
  199. my $req = new HTTP::Request POST => 'http://www.perl.com/cgi-bin/BugGlimpse';
  200. $req->content_type('application/x-www-form-urlencoded');
  201. $req->content('match=www&errors=0');
  202. # Pass request to the user agent and get a response back
  203. my $res = $ua->request($req);
  204. # Check the outcome of the response
  205. if ($res->is_success) {
  206. print $res->content;
  207. } else {
  208. print "Bad luck this time\n";
  209. }
  210. The $ua is created once when the application starts up. New request
  211. objects should normally created for each request sent.
  212. =head1 NETWORK SUPPORT
  213. This section discusses the various protocol schemes and
  214. the HTTP style methods that headers may be used for each.
  215. For all requests, a "User-Agent" header is added and initialized from
  216. the $ua->agent attribute before the request is handed to the network
  217. layer. In the same way, a "From" header is initialized from the
  218. $ua->from attribute.
  219. For all responses, the library adds a header called "Client-Date".
  220. This header holds the time when the response was received by
  221. your application. The format and semantics of the header are the
  222. same as the server created "Date" header. You may also encounter other
  223. "Client-XXX" headers. They are all generated by the library
  224. internally and are not received from the servers.
  225. =head2 HTTP Requests
  226. HTTP requests are just handed off to an HTTP server and it
  227. decides what happens. Few servers implement methods beside the usual
  228. "GET", "HEAD", "POST" and "PUT", but CGI-scripts may implement
  229. any method they like.
  230. If the server is not available then the library will generate an
  231. internal error response.
  232. The library automatically adds a "Host" and a "Content-Length" header
  233. to the HTTP request before it is sent over the network.
  234. For GET request you might want to add the "If-Modified-Since" header
  235. to make the request conditional.
  236. For POST request you should add the "Content-Type" header. When you
  237. try to emulate HTML E<lt>FORM> handling you should usually let the value
  238. of the "Content-Type" header be "application/x-www-form-urlencoded".
  239. See L<lwpcook> for examples of this.
  240. The libwww-perl HTTP implementation currently support the HTTP/1.0
  241. protocol. HTTP/0.9 servers are also handled correctly.
  242. The library allows you to access proxy server through HTTP. This
  243. means that you can set up the library to forward all types of request
  244. through the HTTP protocol module. See L<LWP::UserAgent> for
  245. documentation of this.
  246. =head2 HTTPS Requests
  247. HTTPS requests are HTTP requests over an encrypted network connection
  248. using the SSL protocol developed by Netscape. Everything about HTTP
  249. requests above also apply to HTTPS requests. In addition the library
  250. will add the headers "Client-SSL-Cipher", "Client-SSL-Cert-Subject" and
  251. "Client-SSL-Cert-Issuer" to the response. These headers denote the
  252. encryption method used and the name of the server owner.
  253. The request can contain the header "If-SSL-Cert-Subject" in order to
  254. make the request conditional on the content of the server certificate.
  255. If the certificate subject does not match, no request is sent to the
  256. server and an internally generated error response is returned. The
  257. value of the "If-SSL-Cert-Subject" header is interpreted as a Perl
  258. regular expression.
  259. =head2 FTP Requests
  260. The library currently supports GET, HEAD and PUT requests. GET
  261. retrieves a file or a directory listing from an FTP server. PUT
  262. stores a file on a ftp server.
  263. You can specify a ftp account for servers that want this in addition
  264. to user name and password. This is specified by including an "Account"
  265. header in the request.
  266. User name/password can be specified using basic authorization or be
  267. encoded in the URL. Failed logins return an UNAUTHORIZED response with
  268. "WWW-Authenticate: Basic" and can be treated like basic authorization
  269. for HTTP.
  270. The library supports ftp ASCII transfer mode by specifying the "type=a"
  271. parameter in the URL.
  272. Directory listings are by default returned unprocessed (as returned
  273. from the ftp server) with the content media type reported to be
  274. "text/ftp-dir-listing". The C<File::Listing> module provides methods
  275. for parsing of these directory listing.
  276. The ftp module is also able to convert directory listings to HTML and
  277. this can be requested via the standard HTTP content negotiation
  278. mechanisms (add an "Accept: text/html" header in the request if you
  279. want this).
  280. For normal file retrievals, the "Content-Type" is guessed based on the
  281. file name suffix. See L<LWP::MediaTypes>.
  282. The "If-Modified-Since" request header works for servers that implement
  283. the MDTM command. It will probably not work for directory listings though.
  284. Example:
  285. $req = HTTP::Request->new(GET => 'ftp://me:[email protected]/');
  286. $req->header(Accept => "text/html, */*;q=0.1");
  287. =head2 News Requests
  288. Access to the USENET News system is implemented through the NNTP
  289. protocol. The name of the news server is obtained from the
  290. NNTP_SERVER environment variable and defaults to "news". It is not
  291. possible to specify the hostname of the NNTP server in news: URLs.
  292. The library supports GET and HEAD to retrieve news articles through the
  293. NNTP protocol. You can also post articles to newsgroups by using
  294. (surprise!) the POST method.
  295. GET on newsgroups is not implemented yet.
  296. Examples:
  297. $req = HTTP::Request->new(GET => 'news:[email protected]');
  298. $req = HTTP::Request->new(POST => 'news:comp.lang.perl.test');
  299. $req->header(Subject => 'This is a test',
  300. From => '[email protected]');
  301. $req->content(<<EOT);
  302. This is the content of the message that we are sending to
  303. the world.
  304. EOT
  305. =head2 Gopher Request
  306. The library supports the GET and HEAD methods for gopher requests. All
  307. request header values are ignored. HEAD cheats and returns a
  308. response without even talking to server.
  309. Gopher menus are always converted to HTML.
  310. The response "Content-Type" is generated from the document type
  311. encoded (as the first letter) in the request URL path itself.
  312. Example:
  313. $req = HTTP::Request->new(GET => 'gopher://gopher.sn.no/');
  314. =head2 File Request
  315. The library supports GET and HEAD methods for file requests. The
  316. "If-Modified-Since" header is supported. All other headers are
  317. ignored. The I<host> component of the file URL must be empty or set
  318. to "localhost". Any other I<host> value will be treated as an error.
  319. Directories are always converted to an HTML document. For normal
  320. files, the "Content-Type" and "Content-Encoding" in the response are
  321. guessed based on the file suffix.
  322. Example:
  323. $req = HTTP::Request->new(GET => 'file:/etc/passwd');
  324. =head2 Mailto Request
  325. You can send (aka "POST") mail messages using the library. All
  326. headers specified for the request are passed on to the mail system.
  327. The "To" header is initialized from the mail address in the URL.
  328. Example:
  329. $req = HTTP::Request->new(POST => 'mailto:[email protected]');
  330. $req->header(Subject => "subscribe");
  331. $req->content("Please subscribe me to the libwww-perl mailing list!\n");
  332. =head1 OVERVIEW OF CLASSES AND PACKAGES
  333. This table should give you a quick overview of the classes provided by the
  334. library. Indentation shows class inheritance.
  335. LWP::MemberMixin -- Access to member variables of Perl5 classes
  336. LWP::UserAgent -- WWW user agent class
  337. LWP::RobotUA -- When developing a robot applications
  338. LWP::Protocol -- Interface to various protocol schemes
  339. LWP::Protocol::http -- http:// access
  340. LWP::Protocol::file -- file:// access
  341. LWP::Protocol::ftp -- ftp:// access
  342. ...
  343. LWP::Authen::Basic -- Handle 401 and 407 responses
  344. LWP::Authen::Digest
  345. HTTP::Headers -- MIME/RFC822 style header (used by HTTP::Message)
  346. HTTP::Message -- HTTP style message
  347. HTTP::Request -- HTTP request
  348. HTTP::Response -- HTTP response
  349. HTTP::Daemon -- A HTTP server class
  350. WWW::RobotRules -- Parse robots.txt files
  351. WWW::RobotRules::AnyDBM_File -- Persistent RobotRules
  352. The following modules provide various functions and definitions.
  353. LWP -- This file. Library version number and documentation.
  354. LWP::MediaTypes -- MIME types configuration (text/html etc.)
  355. LWP::Debug -- Debug logging module
  356. LWP::Simple -- Simplified procedural interface for common functions
  357. HTTP::Status -- HTTP status code (200 OK etc)
  358. HTTP::Date -- Date parsing module for HTTP date formats
  359. HTTP::Negotiate -- HTTP content negotiation calculation
  360. File::Listing -- Parse directory listings
  361. =head1 MORE DOCUMENTATION
  362. All modules contain detailed information on the interfaces they
  363. provide. The I<lwpcook> manpage is the libwww-perl cookbook that contain
  364. examples of typical usage of the library. You might want to take a
  365. look at how the scripts C<lwp-request>, C<lwp-rget> and C<lwp-mirror>
  366. are implemented.
  367. =head1 BUGS
  368. The library can not handle multiple simultaneous requests yet. Also,
  369. check out what's left in the TODO file.
  370. =head1 ACKNOWLEDGEMENTS
  371. This package owes a lot in motivation, design, and code, to the
  372. libwww-perl library for Perl 4, maintained by Roy Fielding
  373. E<lt>fielding@ics.uci.edu>.
  374. That package used work from Alberto Accomazzi, James Casey, Brooks
  375. Cutter, Martijn Koster, Oscar Nierstrasz, Mel Melchner, Gertjan van
  376. Oosten, Jared Rhine, Jack Shirazi, Gene Spafford, Marc VanHeyningen,
  377. Steven E. Brenner, Marion Hakanson, Waldemar Kebsch, Tony Sanders, and
  378. Larry Wall; see the libwww-perl-0.40 library for details.
  379. The primary architect for this Perl 5 library is Martijn Koster and
  380. Gisle Aas, with lots of help from Graham Barr, Tim Bunce, Andreas
  381. Koenig, Jared Rhine, and Jack Shirazi.
  382. =head1 COPYRIGHT
  383. Copyright 1995-2001, Gisle Aas
  384. Copyright 1995, Martijn Koster
  385. This library is free software; you can redistribute it and/or
  386. modify it under the same terms as Perl itself.
  387. =head1 AVAILABILITY
  388. The latest version of this library is likely to be available from CPAN
  389. as well as:
  390. http://www.linpro.no/lwp/
  391. The best place to discuss this code is on the <[email protected]>
  392. mailing list.
  393. =cut