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.

613 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. #line 18
  16. =head1 NAME
  17. lwp-rget - Retrieve WWW documents recursively
  18. =head1 SYNOPSIS
  19. lwp-rget [--verbose] [--auth=USER:PASS] [--depth=N] [--hier] [--iis]
  20. [--keepext=mime/type[,mime/type]] [--limit=N] [--nospace]
  21. [--prefix=URL] [--referer=URL] [--sleep=N] [--tolower] <URL>
  22. lwp-rget --version
  23. =head1 DESCRIPTION
  24. This program will retrieve a document and store it in a local file. It
  25. will follow any links found in the document and store these documents
  26. as well, patching links so that they refer to these local copies.
  27. This process continues until there are no more unvisited links or the
  28. process is stopped by the one or more of the limits which can be
  29. controlled by the command line arguments.
  30. This program is useful if you want to make a local copy of a
  31. collection of documents or want to do web reading off-line.
  32. All documents are stored as plain files in the current directory. The
  33. file names chosen are derived from the last component of URL paths.
  34. The options are:
  35. =over 3
  36. =item --auth=USER:PASS<n>
  37. Set the authentication credentials to user "USER" and password "PASS" if
  38. any restricted parts of the web site are hit. If there are restricted
  39. parts of the web site and authentication credentials are not available,
  40. those pages will not be downloaded.
  41. =item --depth=I<n>
  42. Limit the recursive level. Embedded images are always loaded, even if
  43. they fall outside the I<--depth>. This means that one can use
  44. I<--depth=0> in order to fetch a single document together with all
  45. inline graphics.
  46. The default depth is 5.
  47. =item --hier
  48. Download files into a hierarchy that mimics the web site structure.
  49. The default is to put all files in the current directory.
  50. =item --referer=I<URI>
  51. Set the value of the referer header for the initial request. The
  52. special value C<"NONE"> can be used to suppress the referer header in
  53. any of subsequent requests.
  54. =item --iis
  55. Sends an "Accept: */*" on all URL requests as a workaround for a bug in
  56. IIS 2.0. If no Accept MIME header is present, IIS 2.0 returns with a
  57. "406 No acceptable objects were found" error. Also converts any back
  58. slashes (\\) in URLs to forward slashes (/).
  59. =item --keepext=I<mime/type[,mime/type]>
  60. Keeps the current extension for the list MIME types. Useful when
  61. downloading text/plain documents that shouldn't all be translated to
  62. *.txt files.
  63. =item --limit=I<n>
  64. Limit the number of documents to get. The default limit is 50.
  65. =item --nospace
  66. Changes spaces in all URLs to underscore characters (_). Useful when
  67. downloading files from sites serving URLs with spaces in them. Does not
  68. remove spaces from fragments, e.g., "file.html#somewhere in here".
  69. =item --prefix=I<url_prefix>
  70. Limit the links to follow. Only URLs that start the prefix string are
  71. followed.
  72. The default prefix is set as the "directory" of the initial URL to
  73. follow. For instance if we start lwp-rget with the URL
  74. C<http://www.sn.no/foo/bar.html>, then prefix will be set to
  75. C<http://www.sn.no/foo/>.
  76. Use C<--prefix=''> if you don't want the fetching to be limited by any
  77. prefix.
  78. =item --sleep=I<n>
  79. Sleep I<n> seconds before retrieving each document. This options allows
  80. you to go slowly, not loading the server you visiting too much.
  81. =item --tolower
  82. Translates all links to lowercase. Useful when downloading files from
  83. IIS since it does not serve files in a case sensitive manner.
  84. =item --verbose
  85. Make more noise while running.
  86. =item --quiet
  87. Don't make any noise.
  88. =item --version
  89. Print program version number and quit.
  90. =item --help
  91. Print the usage message and quit.
  92. =back
  93. Before the program exits the name of the file, where the initial URL
  94. is stored, is printed on stdout. All used filenames are also printed
  95. on stderr as they are loaded. This printing can be suppressed with
  96. the I<--quiet> option.
  97. =head1 SEE ALSO
  98. L<lwp-request>, L<LWP>
  99. =head1 AUTHOR
  100. Gisle Aas <[email protected]>
  101. =cut
  102. use strict;
  103. use Getopt::Long qw(GetOptions);
  104. use URI::URL qw(url);
  105. use LWP::MediaTypes qw(media_suffix);
  106. use HTML::Entities ();
  107. use vars qw($VERSION);
  108. use vars qw($MAX_DEPTH $MAX_DOCS $PREFIX $REFERER $VERBOSE $QUIET $SLEEP $HIER $AUTH $IIS $TOLOWER $NOSPACE %KEEPEXT);
  109. my $progname = $0;
  110. $progname =~ s|.*/||; # only basename left
  111. $progname =~ s/\.\w*$//; #strip extension if any
  112. $VERSION = sprintf("%d.%02d", q$Revision: 1.19 $ =~ /(\d+)\.(\d+)/);
  113. #$Getopt::Long::debug = 1;
  114. #$Getopt::Long::ignorecase = 0;
  115. # Defaults
  116. $MAX_DEPTH = 5;
  117. $MAX_DOCS = 50;
  118. GetOptions('version' => \&print_version,
  119. 'help' => \&usage,
  120. 'depth=i' => \$MAX_DEPTH,
  121. 'limit=i' => \$MAX_DOCS,
  122. 'verbose!' => \$VERBOSE,
  123. 'quiet!' => \$QUIET,
  124. 'sleep=i' => \$SLEEP,
  125. 'prefix:s' => \$PREFIX,
  126. 'referer:s'=> \$REFERER,
  127. 'hier' => \$HIER,
  128. 'auth=s' => \$AUTH,
  129. 'iis' => \$IIS,
  130. 'tolower' => \$TOLOWER,
  131. 'nospace' => \$NOSPACE,
  132. 'keepext=s' => \$KEEPEXT{'OPT'},
  133. ) || usage();
  134. sub print_version {
  135. require LWP;
  136. my $DISTNAME = 'libwww-perl-' . LWP::Version();
  137. print <<"EOT";
  138. This is lwp-rget version $VERSION ($DISTNAME)
  139. Copyright 1996-1998, Gisle Aas.
  140. This program is free software; you can redistribute it and/or
  141. modify it under the same terms as Perl itself.
  142. EOT
  143. exit 0;
  144. }
  145. my $start_url = shift || usage();
  146. usage() if @ARGV;
  147. require LWP::UserAgent;
  148. my $ua = new LWP::UserAgent;
  149. $ua->agent("$progname/$VERSION " . $ua->agent);
  150. $ua->env_proxy;
  151. unless (defined $PREFIX) {
  152. $PREFIX = url($start_url); # limit to URLs below this one
  153. eval {
  154. $PREFIX->eparams(undef);
  155. $PREFIX->equery(undef);
  156. };
  157. $_ = $PREFIX->epath;
  158. s|[^/]+$||;
  159. $PREFIX->epath($_);
  160. $PREFIX = $PREFIX->as_string;
  161. }
  162. %KEEPEXT = map { lc($_) => 1 } split(/\s*,\s*/, ($KEEPEXT{'OPT'}||0));
  163. my $SUPPRESS_REFERER;
  164. $SUPPRESS_REFERER++ if ($REFERER || "") eq "NONE";
  165. print <<"" if $VERBOSE;
  166. START = $start_url
  167. MAX_DEPTH = $MAX_DEPTH
  168. MAX_DOCS = $MAX_DOCS
  169. PREFIX = $PREFIX
  170. my $no_docs = 0;
  171. my %seen = (); # mapping from URL => local_file
  172. my $filename = fetch($start_url, undef, $REFERER);
  173. print "$filename\n" unless $QUIET;
  174. sub fetch
  175. {
  176. my($url, $type, $referer, $depth) = @_;
  177. # Fix http://sitename.com/../blah/blah.html to
  178. # http://sitename.com/blah/blah.html
  179. $url = $url->as_string if (ref($url));
  180. while ($url =~ s#(https?://[^/]+/)\.\.\/#$1#) {}
  181. # Fix backslashes (\) in URL if $IIS defined
  182. $url = fix_backslashes($url) if (defined $IIS);
  183. $url = url($url) unless ref($url);
  184. $type ||= 'a';
  185. # Might be the background attribute
  186. $type = 'img' if ($type eq 'body' || $type eq 'td');
  187. $depth ||= 0;
  188. # Print the URL before we start checking...
  189. my $out = (" " x $depth) . $url . " ";
  190. $out .= "." x (60 - length($out));
  191. print STDERR $out . " " if $VERBOSE;
  192. # Can't get mailto things
  193. if ($url->scheme eq 'mailto') {
  194. print STDERR "*skipping mailto*\n" if $VERBOSE;
  195. return $url->as_string;
  196. }
  197. # The $plain_url is a URL without the fragment part
  198. my $plain_url = $url->clone;
  199. $plain_url->frag(undef);
  200. # Check PREFIX, but not for <IMG ...> links
  201. if ($type ne 'img' and $url->as_string !~ /^\Q$PREFIX/o) {
  202. print STDERR "*outsider*\n" if $VERBOSE;
  203. return $url->as_string;
  204. }
  205. # Translate URL to lowercase if $TOLOWER defined
  206. $plain_url = to_lower($plain_url) if (defined $TOLOWER);
  207. # If we already have it, then there is nothing to be done
  208. my $seen = $seen{$plain_url->as_string};
  209. if ($seen) {
  210. my $frag = $url->frag;
  211. $seen .= "#$frag" if defined($frag);
  212. $seen = protect_frag_spaces($seen);
  213. print STDERR "$seen (again)\n" if $VERBOSE;
  214. return $seen;
  215. }
  216. # Too much or too deep
  217. if ($depth > $MAX_DEPTH and $type ne 'img') {
  218. print STDERR "*too deep*\n" if $VERBOSE;
  219. return $url;
  220. }
  221. if ($no_docs > $MAX_DOCS) {
  222. print STDERR "*too many*\n" if $VERBOSE;
  223. return $url;
  224. }
  225. # Fetch document
  226. $no_docs++;
  227. sleep($SLEEP) if $SLEEP;
  228. my $req = HTTP::Request->new(GET => $url);
  229. # See: http://ftp.sunet.se/pub/NT/mirror-microsoft/kb/Q163/7/74.TXT
  230. $req->header ('Accept', '*/*') if (defined $IIS); # GIF/JPG from IIS 2.0
  231. $req->authorization_basic(split (/:/, $AUTH)) if (defined $AUTH);
  232. $req->referer($referer) if $referer && !$SUPPRESS_REFERER;
  233. my $res = $ua->request($req);
  234. # Check outcome
  235. if ($res->is_success) {
  236. my $doc = $res->content;
  237. my $ct = $res->content_type;
  238. my $name = find_name($res->request->url, $ct);
  239. print STDERR "$name\n" unless $QUIET;
  240. $seen{$plain_url->as_string} = $name;
  241. # If the file is HTML, then we look for internal links
  242. if ($ct eq "text/html") {
  243. # Save an unprosessed version of the HTML document. This
  244. # both reserves the name used, and it also ensures that we
  245. # don't loose everything if this program is killed before
  246. # we finish.
  247. save($name, $doc);
  248. my $base = $res->base;
  249. # Follow and substitute links...
  250. $doc =~
  251. s/
  252. (
  253. <(img|a|body|area|frame|td)\b # some interesting tag
  254. [^>]+ # still inside tag (not strictly correct)
  255. \b(?:src|href|background) # some link attribute
  256. \s*=\s* # =
  257. )
  258. (?: # scope of OR-ing
  259. (")([^"]*)" | # value in double quotes OR
  260. (')([^']*)' | # value in single quotes OR
  261. ([^\s>]+) # quoteless value
  262. )
  263. /
  264. new_link($1, lc($2), $3||$5, HTML::Entities::decode($4||$6||$7),
  265. $base, $name, "$url", $depth+1)
  266. /giex;
  267. # XXX
  268. # The regular expression above is not strictly correct.
  269. # It is not really possible to parse HTML with a single
  270. # regular expression, but it is faster. Tags that might
  271. # confuse us include:
  272. # <a alt="href" href=link.html>
  273. # <a alt=">" href="link.html">
  274. #
  275. }
  276. save($name, $doc);
  277. return $name;
  278. } else {
  279. print STDERR $res->code . " " . $res->message . "\n" if $VERBOSE;
  280. $seen{$plain_url->as_string} = $url->as_string;
  281. return $url->as_string;
  282. }
  283. }
  284. sub new_link
  285. {
  286. my($pre, $type, $quote, $url, $base, $localbase, $referer, $depth) = @_;
  287. $url = protect_frag_spaces($url);
  288. $url = fetch(url($url, $base)->abs, $type, $referer, $depth);
  289. $url = url("file:$url", "file:$localbase")->rel
  290. unless $url =~ /^[.+\-\w]+:/;
  291. $url = unprotect_frag_spaces($url);
  292. return $pre . $quote . $url . $quote;
  293. }
  294. sub protect_frag_spaces
  295. {
  296. my ($url) = @_;
  297. $url = $url->as_string if (ref($url));
  298. if ($url =~ m/^([^#]*#)(.+)$/)
  299. {
  300. my ($base, $frag) = ($1, $2);
  301. $frag =~ s/ /%20/g;
  302. $url = $base . $frag;
  303. }
  304. return $url;
  305. }
  306. sub unprotect_frag_spaces
  307. {
  308. my ($url) = @_;
  309. $url = $url->as_string if (ref($url));
  310. if ($url =~ m/^([^#]*#)(.+)$/)
  311. {
  312. my ($base, $frag) = ($1, $2);
  313. $frag =~ s/%20/ /g;
  314. $url = $base . $frag;
  315. }
  316. return $url;
  317. }
  318. sub fix_backslashes
  319. {
  320. my ($url) = @_;
  321. my ($base, $frag);
  322. $url = $url->as_string if (ref($url));
  323. if ($url =~ m/([^#]+)(#.*)/)
  324. {
  325. ($base, $frag) = ($1, $2);
  326. }
  327. else
  328. {
  329. $base = $url;
  330. $frag = "";
  331. }
  332. $base =~ tr/\\/\//;
  333. $base =~ s/%5[cC]/\//g; # URL-encoded back slash is %5C
  334. return $base . $frag;
  335. }
  336. sub to_lower
  337. {
  338. my ($url) = @_;
  339. my $was_object = 0;
  340. if (ref($url))
  341. {
  342. $url = $url->as_string;
  343. $was_object = 1;
  344. }
  345. if ($url =~ m/([^#]+)(#.*)/)
  346. {
  347. $url = lc($1) . $2;
  348. }
  349. else
  350. {
  351. $url = lc($url);
  352. }
  353. if ($was_object == 1)
  354. {
  355. return url($url);
  356. }
  357. else
  358. {
  359. return $url;
  360. }
  361. }
  362. sub translate_spaces
  363. {
  364. my ($url) = @_;
  365. my ($base, $frag);
  366. $url = $url->as_string if (ref($url));
  367. if ($url =~ m/([^#]+)(#.*)/)
  368. {
  369. ($base, $frag) = ($1, $2);
  370. }
  371. else
  372. {
  373. $base = $url;
  374. $frag = "";
  375. }
  376. $base =~ s/^ *//; # Remove initial spaces from base
  377. $base =~ s/ *$//; # Remove trailing spaces from base
  378. $base =~ tr/ /_/;
  379. $base =~ s/%20/_/g; # URL-encoded space is %20
  380. return $base . $frag;
  381. }
  382. sub mkdirp
  383. {
  384. my($directory, $mode) = @_;
  385. my @dirs = split(/\//, $directory);
  386. my $path = shift(@dirs); # build it as we go
  387. my $result = 1; # assume it will work
  388. unless (-d $path) {
  389. $result &&= mkdir($path, $mode);
  390. }
  391. foreach (@dirs) {
  392. $path .= "/$_";
  393. if ( ! -d $path) {
  394. $result &&= mkdir($path, $mode);
  395. }
  396. }
  397. return $result;
  398. }
  399. sub find_name
  400. {
  401. my($url, $type) = @_;
  402. #print "find_name($url, $type)\n";
  403. # Translate spaces in URL to underscores (_) if $NOSPACE defined
  404. $url = translate_spaces($url) if (defined $NOSPACE);
  405. # Translate URL to lowercase if $TOLOWER defined
  406. $url = to_lower($url) if (defined $TOLOWER);
  407. $url = url($url) unless ref($url);
  408. my $path = $url->path;
  409. # trim path until only the basename is left
  410. $path =~ s|(.*/)||;
  411. my $dirname = ".$1";
  412. if (!$HIER) {
  413. $dirname = "";
  414. } elsif (! -d $dirname) {
  415. mkdirp($dirname, 0775);
  416. }
  417. my $extra = ""; # something to make the name unique
  418. my $suffix;
  419. if ($KEEPEXT{lc($type)}) {
  420. $suffix = ($path =~ m/\.(.*)/) ? $1 : "";
  421. } else {
  422. $suffix = media_suffix($type);
  423. }
  424. $path =~ s|\..*||; # trim suffix
  425. $path = "index" unless length $path;
  426. while (1) {
  427. # Construct a new file name
  428. my $file = $dirname . $path . $extra;
  429. $file .= ".$suffix" if $suffix;
  430. # Check if it is unique
  431. return $file unless -f $file;
  432. # Try something extra
  433. unless ($extra) {
  434. $extra = "001";
  435. next;
  436. }
  437. $extra++;
  438. }
  439. }
  440. sub save
  441. {
  442. my $name = shift;
  443. #print "save($name,...)\n";
  444. open(FILE, ">$name") || die "Can't save $name: $!";
  445. binmode FILE;
  446. print FILE $_[0];
  447. close(FILE);
  448. }
  449. sub usage
  450. {
  451. die <<"";
  452. Usage: $progname [options] <URL>
  453. Allowed options are:
  454. --auth=USER:PASS Set authentication credentials for web site
  455. --depth=N Maximum depth to traverse (default is $MAX_DEPTH)
  456. --hier Download into hierarchy (not all files into cwd)
  457. --referer=URI Set initial referer header (or "NONE")
  458. --iis Workaround IIS 2.0 bug by sending "Accept: */*" MIME
  459. header; translates backslashes (\\) to forward slashes (/)
  460. --keepext=type Keep file extension for MIME types (comma-separated list)
  461. --limit=N A limit on the number documents to get (default is $MAX_DOCS)
  462. --nospace Translate spaces URLs (not #fragments) to underscores (_)
  463. --version Print version number and quit
  464. --verbose More output
  465. --quiet No output
  466. --sleep=SECS Sleep between gets, ie. go slowly
  467. --prefix=PREFIX Limit URLs to follow to those which begin with PREFIX
  468. --tolower Translate all URLs to lowercase (useful with IIS servers)
  469. }
  470. __END__
  471. :endofperl