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