Leaked source code of windows server 2003
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.

523 lines
14 KiB

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