home *** CD-ROM | disk | FTP | other *** search
/ Chip: Windows 2000 Professional Resource Kit / W2KPRK.iso / apps / perl / ActivePerl.exe / data.z / lwp-request.bat < prev    next >
Encoding:
DOS Batch File  |  1999-10-16  |  14.8 KB  |  536 lines

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