home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / bin / lwp-request.bat < prev    next >
Encoding:
DOS Batch File  |  1997-08-10  |  13.6 KB  |  511 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. perl -x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
  4. goto endofperl
  5. @rem ';
  6. #!perl
  7. #line 8
  8. #perl -w
  9.     eval 'exec perl -S $0 "$@"'
  10.     if 0;
  11.  
  12. # $Id: lwp-request.PL,v 1.26 1996/05/19 11:23:05 aas Exp $
  13. #
  14. # Simple user agent using LWP library.
  15.  
  16. =head1 NAME
  17.  
  18. lwp-request, GET, HEAD, POST - Simple WWW user agent
  19.  
  20. =head1 SYNOPSIS
  21.  
  22.  lwp-request [-eEdvhx] [-m method] [-b <base URL>] [-t <timeout>]
  23.              [-i <if-modified-since>] [-c <content-type>] [-C <credentials>]
  24.              [-p <proxy-url>] [-o <format>] <url>...
  25.  
  26. =head1 DESCRIPTION
  27.  
  28. This program can be used to send requests to WWW servers and your
  29. local file system. The request content for POST, PUT and CHECKIN
  30. methods is read from stdin.  The content of the response is printed on
  31. stdout.  Error messages are printed on stderr.  The program returns a
  32. status value indicating the number of URLs that failed.
  33.  
  34. The options are:
  35.  
  36. =over 4
  37.  
  38. =item -m <method>
  39.  
  40. Set which method to use for the request.  If this option is not used,
  41. then the method is derived from the name of the program.
  42.  
  43. =item -f
  44.  
  45. Force request through, even if the program believes that the method is
  46. illegal.  The server will probably reject the request.
  47.  
  48. =item -b <url>
  49.  
  50. This URL will be used as the base URL for the URLs that the method is
  51. applied to.  The base URL only takes effect for relative URLs.  If you
  52. do not provide this option and the URLs are relative, then they will
  53. be treated as files in the local file system.
  54.  
  55. =item -t <timeout>
  56.  
  57. Set the timeout value for the requests.  The timeout is the amount of
  58. time that the program will wait for a response from the remote server
  59. before it fails.  The default unit for the timeout value is seconds.
  60. You might append "m" or "h" to the timeout value to make it minutes or
  61. hours, respectively.  The default timeout is '3m', i.e. 3 minutes.
  62.  
  63. =item -i <time>
  64.  
  65. Set the If-Modified-Since header in the request. If I<time> it the
  66. name of a file, use the modification timestamp for this file. If
  67. I<time> is not a file, it is parsed as a literal date. Take a look at
  68. L<HTTP::Date> for recogniced formats.
  69.  
  70. =item -c <content-type>
  71.  
  72. Set the Content-Type for the request.  This option is only allowed for
  73. requests that take a content, i.e. POST, PUT and CHECKIN.  You can
  74. force methods to take content by using the C<-f> option together with
  75. C<-c>.  The default Content-Type for POST is
  76. C<application/x-www-form-urlencoded>.  The default Content-type for
  77. the others is C<text/plain>.
  78.  
  79. =item -p <proxy-url>
  80.  
  81. Set the proxy to be used for the requests.  The program also loads
  82. proxy settings from the environment.  You can disable this with the
  83. C<-P> option.
  84.  
  85. =item -C <username>:<password>
  86.  
  87. Provide credentials for documents that are protected by Basic
  88. Authentication.  If the document is protected and you did not specify
  89. the username and password with this option, then you will be prompted
  90. to provide these values.
  91.  
  92. =back
  93.  
  94. The following options controls what is displayed by the program:
  95.  
  96. =over 4
  97.  
  98. =item -u
  99.  
  100. Print request method and absolute URL as requests are made.
  101.  
  102. =item -U
  103.  
  104. Print requeset headers in addition to request method and absolute URL.
  105.  
  106. =item -s
  107.  
  108. Print response status code.  This option is always on for HEAD requests.
  109.  
  110. =item -S
  111.  
  112. Print response status chain. This shows redirect and autorization
  113. requests that are handled by the library.
  114.  
  115. =item -e
  116.  
  117. Print response headers.  This option is always on for HEAD requests.
  118.  
  119. =item -d
  120.  
  121. Do B<not> print the content of the response.
  122.  
  123. =item -o <format>
  124.  
  125. Process HTML content in various ways before printing it.  If the
  126. content type of the response is not HTML, then this option has no
  127. effect.  The legal format values are; I<text>, I<ps>, I<links>,
  128. I<html> and I<dump>.
  129.  
  130. If you specify the I<text> format then the HTML will be formatted as
  131. plain latin1 text.  If you specify the I<ps> format then it will be
  132. formatted as Postscript.
  133.  
  134. The I<links> format will output all links found in the HTML document.
  135. Relative links will be expanded to absolute ones.
  136.  
  137. The I<html> format will reformat the HTML code and the I<dump> format
  138. will just dump the HTML syntax tree.
  139.  
  140. =item -v
  141.  
  142. Print the version number of the program and quit.
  143.  
  144. =item -h
  145.  
  146. Print usage message and quit.
  147.  
  148. =item -x
  149.  
  150. Extra debugging output.
  151.  
  152. =back
  153.  
  154. Because this program is implemented using the LWP library, it will
  155. only support the protocols that LWP supports.
  156.  
  157. =head1 SEE ALSO
  158.  
  159. L<lwp-mirror>, L<LWP>
  160.  
  161. =head1 COPYRIGHT
  162.  
  163. Copyright (c) 1995, 1996 Gisle Aas. All rights reserved.
  164.  
  165. This library is free software; you can redistribute it and/or
  166. modify it under the same terms as Perl itself.
  167.  
  168. =head1 AUTHOR
  169.  
  170. Gisle Aas <aas@a.sn.no>
  171.  
  172. =cut
  173.  
  174.  
  175. $0 =~ s,.*/,,;  # use basename only
  176. $VERSION = sprintf("%d.%02d", q$Revision: 1.26 $ =~ /(\d+)\.(\d+)/);
  177.  
  178.  
  179. require LWP;
  180. require LWP::Debug;
  181. require URI::URL;
  182.  
  183. use HTTP::Status qw(status_message);
  184. use HTTP::Date qw(time2str str2time);
  185.  
  186.  
  187. # This table lists the methods that are allowed.  It should really be
  188. # a superset for all methods supported for every scheme that may be
  189. # supported by the library.  Currently it might be a bit too HTTP
  190. # specific.  You might use the -f option to force a method through.
  191. #
  192. # "" = No content in request, "C" = Needs content in request
  193. #
  194. %allowed_methods = (
  195.     GET        => "",   
  196.     HEAD       => "",
  197.     POST       => "C",   
  198.     PUT        => "C",
  199.     DELETE     => "",   
  200.     LINK       => "",
  201.     UNLINK     => "",
  202.     CHECKIN    => "C",
  203.     CHECKOUT   => "",
  204.     SHOWMETHOD => "",
  205. );
  206.  
  207.  
  208. # We make our own specialization of LWP::UserAgent that asks for
  209. # user/password if document is protected.
  210. {
  211.     package RequestAgent;
  212.     @ISA = qw(LWP::UserAgent);
  213.  
  214.     sub new
  215.     { 
  216.     my $self = LWP::UserAgent::new(@_);
  217.     $self->agent("lwp-request/$main::VERSION");
  218.     $self;
  219.     }
  220.  
  221.     sub get_basic_credentials
  222.     {
  223.     my($self, $realm, $uri) = @_;
  224.     if ($main::opt_C) {
  225.         return split(':', $main::opt_C, 2);
  226.     } elsif (-t) {
  227.         my $netloc = $uri->netloc;
  228.         print "Enter username for $realm at $netloc: ";
  229.         my $user = <STDIN>;
  230.         chomp($user);
  231.         return (undef, undef) unless length $user;
  232.         print "Password: ";
  233.         system("stty -echo");
  234.         my $password = <STDIN>;
  235.         system("stty echo");
  236.         print "\n";  # because we disabled echo
  237.         chomp($password);
  238.         return ($user, $password);
  239.     } else {
  240.         return (undef, undef)
  241.     }
  242.     }
  243. }
  244.  
  245. $method = uc($0 eq "lwp-request" ? "GET" : $0);
  246.  
  247. # Parse command line
  248. use Getopt::Std;
  249.  
  250. $opt_m = undef;  # set method
  251. $opt_f = undef;  # make request even if method is now in %allowed_methods
  252. $opt_b = undef;  # base url
  253. $opt_t = undef;  # timeout
  254. $opt_i = undef;  # if-modified-since
  255. $opt_c = undef;  # content type for POST
  256. $opt_C = undef;  # credidentials for basic authorization
  257.  
  258. $opt_u = undef;  # display method, URL and headers of request
  259. $opt_U = undef;  # display request headers also
  260. $opt_s = undef;  # display status code
  261. $opt_S = undef;  # display whole chain of status codes
  262. $opt_e = undef;  # display response headers (default for HEAD)
  263. $opt_d = undef;  # don't display content
  264.  
  265. $opt_h = undef;  # print usage
  266. $opt_v = undef;  # print version
  267.  
  268. $opt_x = undef;  # extra debugging info
  269. $opt_p = undef;  # proxy URL
  270. $opt_P = undef;  # don't load proxy setting from environment
  271.  
  272. $opt_o = undef;  # output format
  273.  
  274. unless (getopts("xhvuUsSedPp:b:t:i:c:C:m:fo:")) {
  275.     usage();
  276. }
  277.  
  278. if ($opt_v) {
  279.     require LWP;
  280.     my $DISTNAME = 'libwww-perl-' . LWP::Version();
  281.     die <<"EOT";
  282. This is lwp-request version $VERSION ($DISTNAME)
  283.  
  284. Copyright 1995-1996, Gisle Aas.
  285.  
  286. This program is free software; you can redistribute it and/or
  287. modify it under the same terms as Perl itself.
  288. EOT
  289. }
  290.  
  291. usage() if $opt_h || !@ARGV;
  292.  
  293. LWP::Debug::level('+') if $opt_x;
  294.  
  295. # Create the user agent object
  296. $ua = new RequestAgent;
  297.  
  298. # Load proxy settings from *_proxy environment variables.
  299. $ua->env_proxy unless $opt_P;
  300.  
  301. $method = uc($opt_m) if defined $opt_m;
  302.  
  303. if ($opt_f) {
  304.     if ($opt_c) {
  305.         $allowed_methods{$method} = "C";  # force content
  306.     } else {
  307.         $allowed_methods{$method} = "";
  308.     }
  309. } elsif (!defined $allowed_methods{$method}) {
  310.     die "$0: $method is not an allowed method\n";
  311. }
  312.  
  313. if ($method eq "HEAD") {
  314.     $opt_s = 1;
  315.     $opt_e = 1 unless $opt_d;
  316.     $opt_d = 1;
  317. }
  318.  
  319. if (defined $opt_t) {
  320.     $opt_t =~ /^(\d+)([smh])?/;
  321.     die "$0: Illegal timeout value!\n" unless defined $1;
  322.     $timeout = $1;
  323.     $timeout *= 60   if ($2 eq "m");
  324.     $timeout *= 3600 if ($2 eq "h");
  325.     $ua->timeout($timeout);
  326. }
  327.  
  328. if (defined $opt_i) {
  329.     if (-e $opt_i) {
  330.         $time = (stat _)[9];
  331.     } else {
  332.         $time = str2time($opt_i);
  333.         die "$0: Illegal time syntax for -i option\n"
  334.             unless defined $time;
  335.     }
  336.     $opt_i = time2str($time);
  337. }
  338.  
  339. $content = undef;
  340. if ($allowed_methods{$method} eq "C") {
  341.     # This request needs some content
  342.     unless (defined $opt_c) {
  343.         # set default content type
  344.         $opt_c = ($method eq "POST") ?
  345.               "application/x-www-form-urlencoded"
  346.             : "text/plain";
  347.     } else {
  348.         die "$0: Illegal Content-type format\n"
  349.             unless $opt_c =~ m,^[\w\-]+/[\w\-]+$,
  350.     }
  351.     print "Please enter content ($opt_c) to be ${method}ed:\n"
  352.         if -t;
  353.     $content = join("", <STDIN>);
  354. } else {
  355.     die "$0: Can't set Content-type for $method requests\n"
  356.         if defined $opt_c;
  357. }
  358.  
  359. # Set up a request.  We will use the same request object for all URLs.
  360. $request = new HTTP::Request $method;
  361. $request->header('If-Modified-Since', $opt_i) if defined $opt_i;
  362. #$request->header('Accept', '*/*');
  363. if ($opt_c) { # will always be set for request that wants content
  364.     $request->header('Content-Type', $opt_c);
  365.     $request->header('Content-Length', length $content);  # Not really needed
  366.     $request->content($content);
  367. }
  368.  
  369.  
  370. $errors = 0;
  371.  
  372. # Ok, now we perform the requests, one URL at a time
  373. while ($url = shift) {
  374.     # Create the URL object, but protect us against bad URLs
  375.     eval {
  376.     if ($url =~ /^\w+:/ || $opt_b) {  # is there any scheme specification
  377.         $url = new URI::URL $url, $opt_b;
  378.     } else {
  379.         # No scheme specified for the URL (treat it as a file)
  380.         $url = newlocal URI::URL $url;
  381.     }
  382.     };
  383.     if ($@) {
  384.     $@ =~ s/at\s+\S+\s+line\s\d+//;
  385.     print STDERR $@;
  386.     $errors++;
  387.     next;
  388.     }
  389.  
  390.     $ua->proxy($url->scheme, $opt_p) if $opt_p;
  391.  
  392.     # Send the request and get a response back from the server
  393.     $request->url($url);
  394.     $response = $ua->request($request);
  395.  
  396.     if ($opt_u || $opt_U) {
  397.         my $url = $response->request->url->as_string;
  398.         print "$method $url\n";
  399.         print $response->request->headers_as_string, "\n" if $opt_U;
  400.     }
  401.  
  402.     if ($opt_S) {
  403.     printResponseChain($response);
  404.     } elsif ($opt_s) {
  405.         # Display status code
  406.         my $code = $response->code;
  407.         print $code, " ", status_message($code), "\n";
  408.         
  409.     }
  410.  
  411.     if ($opt_e) {
  412.         # Display headers
  413.         print $response->headers_as_string;
  414.         print "\n";  # separate headers and content
  415.     }
  416.  
  417.     if ($response->is_success) {
  418.     unless ($opt_d) {
  419.         if ($opt_o &&
  420.         $response->content_type eq 'text/html') {
  421.         require HTML::Parse;
  422.         my $html = HTML::Parse::parse_html($response->content);
  423.         {
  424.             $opt_o eq 'ps' && do {
  425.             require HTML::FormatPS;
  426.             my $f = new HTML::FormatPS;
  427.             print $f->format($html);
  428.             last;
  429.             };
  430.             $opt_o eq 'text' && do {
  431.             require HTML::FormatText;
  432.             my $f = new HTML::FormatText;
  433.             print $f->format($html);
  434.             last;
  435.             };
  436.             $opt_o eq 'html' && do {
  437.             print $html->as_HTML;
  438.             last;
  439.             };
  440.             $opt_o eq 'links' && do {
  441.             my $base = $response->base;
  442.             for ( @{ $html->extract_links } ) {
  443.                 my($link, $elem) = @$_;
  444.                 my $tag = uc $elem->tag;
  445.                 $link = new URI::URL $link, $base;
  446.                 print "$tag\t", $link->abs->as_string, "\n";
  447.             }
  448.             last;
  449.             };
  450.             $opt_o eq 'dump' && do {
  451.             $html->dump;
  452.             last;
  453.             };
  454.             # It is bad to not notice this before now :-(
  455.             die "Illegal -o option value ($opt_o)\n";
  456.         }
  457.         } else {
  458.         print $response->content;
  459.         }
  460.     }
  461.     } else {
  462.         print STDERR $response->error_as_HTML unless $opt_d;
  463.         $errors++;
  464.     }
  465. }
  466.  
  467. exit $errors;
  468.  
  469.  
  470. sub printResponseChain
  471. {
  472.     my($response) = @_;
  473.     return unless defined $response;
  474.     printResponseChain($response->previous);
  475.     my $method = $response->request->method;
  476.     my $url = $response->request->url->as_string;
  477.     my $code = $response->code;
  478.     print "$method $url --> $code ", status_message($code), "\n";
  479. }
  480.  
  481.  
  482. sub usage
  483. {
  484.     die <<"EOT";
  485. Usage: $0 [-options] <url>...
  486.     -m <method>   use method for the request (default is '$method')
  487.     -f            make request even if $0 believes method is illegal
  488.     -b <base>     Use the specified URL as base
  489.     -t <timeout>  Set timeout value
  490.     -i <time>     Set the If-Modified-Since header on the request
  491.     -c <conttype> use this content-type for POST, PUT, CHECKIN
  492.     -p <proxyurl> use this as a proxy
  493.     -P            don't load proxy settings from environment
  494.  
  495.     -u            Display method and URL before any response
  496.     -U            Display request headers (implies -u)
  497.     -s            Display response status code
  498.     -S            Display response status chain
  499.     -e            Display response headers
  500.     -d            Do not display content
  501.     -o <format>   Process HTML content in various ways
  502.  
  503.     -v            Show program version
  504.     -h            Print this message
  505.  
  506.     -x            Extra debugging output
  507. EOT
  508. }
  509. __END__
  510. :endofperl
  511.