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.

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