home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / perl_utl.zip / POST.cmd < prev    next >
OS/2 REXX Batch file  |  1997-11-28  |  14KB  |  512 lines

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