PageRenderTime 58ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/ATF2/control-software/epics-3.14.8/extensions/src/ChannelArchiver/ThirdParty/libwww-perl-5.800/blib/script/lwp-request

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