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.

258 lines
6.6 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-download.PL,v 1.10 1999/03/19 14:06:30 gisle Exp $
  16. =head1 NAME
  17. lwp-download - fetch large files from the net
  18. =head1 SYNOPSIS
  19. lwp-download [-a] <url> [<local file>]
  20. =head1 DESCRIPTION
  21. The I<lwp-download> program will down load the document specified by the URL
  22. given as the first command line argument to a local file. The local
  23. filename used to save the document is guessed from the URL unless
  24. specified as the second command line argument.
  25. The I<lwp-download> program is implemented using the I<libwww-perl>
  26. library. It is better suited to down load big files than the
  27. I<lwp-request> program because it does not store the file in memory.
  28. Another benefit is that it will keep you updated about its progress
  29. and that you don't have much options to worry about.
  30. Use the C<-a> option to save the file in text (ascii) mode. Might make a
  31. difference on dosish systems.
  32. =head1 EXAMPLE
  33. Fetch the newest and greatest perl version:
  34. $ lwp-download http://www.perl.com/CPAN/src/latest.tar.gz
  35. Saving to 'latest.tar.gz'...
  36. 1.47 MB received in 22 seconds (68.7 KB/sec)
  37. =head1 AUTHOR
  38. Gisle Aas <[email protected]>
  39. =cut
  40. use strict;
  41. use LWP::UserAgent ();
  42. use LWP::MediaTypes qw(guess_media_type media_suffix);
  43. use URI ();
  44. use HTTP::Date ();
  45. my $progname = $0;
  46. $progname =~ s,.*/,,; # only basename left in progname
  47. $progname =~ s/\.\w*$//; # strip extension if any
  48. #parse option
  49. use Getopt::Std;
  50. my %opt;
  51. unless (getopts('a', \%opt)) {
  52. usage();
  53. }
  54. my $url = URI->new(shift || usage());
  55. my $argfile = shift;
  56. my $version = q$Revision: 1.10 $;
  57. my $ua = new LWP::UserAgent;
  58. $ua->agent("lwp-download/$version " . $ua->agent);
  59. $ua->env_proxy;
  60. my $req = new HTTP::Request GET => $url;
  61. my $file; # name of file we download into
  62. my $length; # total number of bytes to download
  63. my $flength; # formatted length
  64. my $size = 0; # number of bytes received
  65. my $start_t; # start time of download
  66. my $last_dur; # time of last callback
  67. my $shown = 0; # have we called the show() function yet
  68. $SIG{INT} = sub { die "Interrupted\n"; };
  69. $| = 1; # autoflush
  70. my $res = $ua->request($req,
  71. sub {
  72. unless($file) {
  73. my $res = $_[1];
  74. unless ($argfile) {
  75. # must find a suitable name to use. First thing
  76. # to do is to look for the "Content-Disposition"
  77. # header defined by RFC1806. This is also supported
  78. # by Netscape
  79. my $cd = $res->header("Content-Disposition");
  80. if ($cd && $cd =~ /\bfilename\s*=\s*(\S+)/) {
  81. $file = $1;
  82. $file =~ s/;$//;
  83. $file =~ s/^([\"\'])(.*)\1$/$2/;
  84. }
  85. # if this fails we try to make something from the URL
  86. unless ($file) {
  87. my $req = $res->request; # now always there
  88. my $rurl = $req ? $req->url : $url;
  89. $file = ($rurl->path_segments)[-1];
  90. unless (length $file) {
  91. $file = "index";
  92. my $suffix = media_suffix($res->content_type);
  93. $file .= ".$suffix" if $suffix;
  94. } elsif ($rurl->scheme eq 'ftp' ||
  95. $file =~ /\.tgz$/ ||
  96. $file =~ /\.tar(\.(Z|gz))?$/
  97. ) {
  98. # leave the filename as it was
  99. } else {
  100. my $ct = guess_media_type($file);
  101. unless ($ct eq $res->content_type) {
  102. # need a better suffix for this type
  103. my $suffix = media_suffix($res->content_type);
  104. $file .= ".$suffix" if $suffix;
  105. }
  106. }
  107. }
  108. # Check if the file is already present
  109. if (-f $file && -t) {
  110. print "Overwrite $file? [y] ";
  111. my $ans = <STDIN>;
  112. exit if !defined($ans) || !($ans =~ /^y?\n/);
  113. } else {
  114. print "Saving to '$file'...\n";
  115. }
  116. } else {
  117. $file = $argfile;
  118. }
  119. open(FILE, ">$file") || die "Can't open $file: $!";
  120. binmode FILE unless $opt{a};
  121. $length = $res->content_length;
  122. $flength = fbytes($length) if defined $length;
  123. $start_t = time;
  124. $last_dur = 0;
  125. }
  126. $size += length($_[0]);
  127. print FILE $_[0];
  128. if (defined $length) {
  129. my $dur = time - $start_t;
  130. if ($dur != $last_dur) { # don't update too often
  131. $last_dur = $dur;
  132. my $perc = $size / $length;
  133. my $speed;
  134. $speed = fbytes($size/$dur) . "/sec" if $dur > 3;
  135. my $secs_left = fduration($dur/$perc - $dur);
  136. $perc = int($perc*100);
  137. my $show = "$perc% of $flength";
  138. $show .= " (at $speed, $secs_left remaining)" if $speed;
  139. show($show, 1);
  140. }
  141. } else {
  142. show( fbytes($size) . " received");
  143. }
  144. }
  145. );
  146. if ($res->is_success || $res->message =~ /^Interrupted/) {
  147. show(""); # clear text
  148. print "\r";
  149. print fbytes($size);
  150. print " of ", fbytes($length) if defined($length) && $length != $size;
  151. print " received";
  152. my $dur = time - $start_t;
  153. if ($dur) {
  154. my $speed = fbytes($size/$dur) . "/sec";
  155. print " in ", fduration($dur), " ($speed)";
  156. }
  157. print "\n";
  158. my $died = $res->header("X-Died");
  159. if ($died || !$res->is_success) {
  160. if (-t) {
  161. print "Transfer aborted. Delete $file? [n] ";
  162. my $ans = <STDIN>;
  163. unlink($file) if defined($ans) && $ans =~ /^y\n/;
  164. } else {
  165. print "Transfer aborted, $file kept\n";
  166. }
  167. }
  168. } else {
  169. print "\n" if $shown;
  170. print "$progname: ", $res->status_line, "\n";
  171. exit 1;
  172. }
  173. sub fbytes
  174. {
  175. my $n = int(shift);
  176. if ($n >= 1024 * 1024) {
  177. return sprintf "%.3g MB", $n / (1024.0 * 1024);
  178. } elsif ($n >= 1024) {
  179. return sprintf "%.3g KB", $n / 1024.0;
  180. } else {
  181. return "$n bytes";
  182. }
  183. }
  184. sub fduration
  185. {
  186. use integer;
  187. my $secs = int(shift);
  188. my $hours = $secs / (60*60);
  189. $secs -= $hours * 60*60;
  190. my $mins = $secs / 60;
  191. $secs %= 60;
  192. if ($hours) {
  193. return "$hours hours $mins minutes";
  194. } elsif ($mins >= 2) {
  195. return "$mins minutes";
  196. } else {
  197. $secs += $mins * 60;
  198. return "$secs seconds";
  199. }
  200. }
  201. BEGIN {
  202. my @ani = qw(- \ | /);
  203. my $ani = 0;
  204. sub show
  205. {
  206. my($mess, $show_ani) = @_;
  207. print "\r$mess" . (" " x (75 - length $mess));
  208. print $show_ani ? "$ani[$ani++]\b" : " ";
  209. $ani %= @ani;
  210. $shown++;
  211. }
  212. }
  213. sub usage
  214. {
  215. die "Usage: $progname [-a] <url> [<lpath>]\n";
  216. }
  217. __END__
  218. :endofperl