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.

242 lines
6.2 KiB

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