Source code of Windows XP (NT5)
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.

535 lines
14 KiB

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