| 1 | package Apache::File::Resumable; | ||
| 2 | |||
| 3 | require 5.005_62; | ||
| 4 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 5 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | |||
| 7 | require Exporter; | ||
| 8 | |||
| 9 | our @ISA = qw(Exporter); | ||
| 10 | |||
| 11 | # Items to export into callers namespace by default. Note: do not export | ||
| 12 | # names by default without a very good reason. Use EXPORT_OK instead. | ||
| 13 | # Do not simply export all your public functions/methods/constants. | ||
| 14 | |||
| 15 | # This allows declaration use Apache::File::Resumable ':all'; | ||
| 16 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||
| 17 | # will save memory. | ||
| 18 | our %EXPORT_TAGS = ( 'all' => [ qw( | ||
| 19 | |||
| 20 | ) ] ); | ||
| 21 | |||
| 22 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | ||
| 23 | |||
| 24 | our @EXPORT = qw( | ||
| 25 | |||
| 26 | ); | ||
| 27 | our $VERSION = sprintf '%s', 'q$Revision: 1.1.1.1 $' =~ /\S+\s+(\S+)\s+/ ; | ||
| 28 | |||
| 29 | |||
| 30 | # Preloaded methods go here. | ||
| 31 | |||
| 32 | |||
| 33 | 1 | ▶ | use strict ; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 34 | |||
| 35 | 0 | ▶ | use Apache::Constants ; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 36 | |||
| 37 | # | ||
| 38 | # Format time as necessary by http headers | ||
| 39 | # Taken from CGI.pm | ||
| 40 | # | ||
| 41 | |||
| 42 | sub formattime | ||
| 43 | |||
| 44 | { | ||
| 45 | my $time = shift ; | ||
| 46 | my(@MON)=qw/Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec/; | ||
| 47 | my(@WDAY) = qw/Sun Mon Tue Wed Thu Fri Sat/; | ||
| 48 | |||
| 49 | # make HTTP/cookie date string from GMT'ed time | ||
| 50 | my($sec,$min,$hour,$mday,$mon,$year,$wday) = gmtime($time); | ||
| 51 | $year += 1900; | ||
| 52 | return sprintf("%s, %02d %s %04d %02d:%02d:%02d GMT", | ||
| 53 | $WDAY[$wday],$mday,$MON[$mon],$year,$hour,$min,$sec); | ||
| 54 | } | ||
| 55 | |||
| 56 | # | ||
| 57 | # Actual Download | ||
| 58 | # | ||
| 59 | |||
| 60 | sub download { | ||
| 61 | my ($self, $file, $req) = @_; | ||
| 62 | |||
| 63 | open T, "/tmp/headers_in"; | ||
| 64 | print T $req->headers_in; | ||
| 65 | close(T); | ||
| 66 | |||
| 67 | ### Create an ETag | ||
| 68 | ### The ETag is required, otherwise IE won't resume a download | ||
| 69 | ### The ETag can have any value, but it must be garanteed that is | ||
| 70 | ### unique | ||
| 71 | ### for every file and it changes whenever the file changes | ||
| 72 | ### The idea below is copied form Apache ap_make_etag (http_protocol.c) | ||
| 73 | |||
| 74 | # /* | ||
| 75 | # * Make an ETag header out of various pieces of information. We use | ||
| 76 | # * the last-modified date and, if we have a real file, the | ||
| 77 | # * length and inode number - note that this doesn't have to match | ||
| 78 | # * the content-length (i.e. includes), it just has to be unique | ||
| 79 | # * for the file. | ||
| 80 | # * | ||
| 81 | # * If the request was made within a second of the last-modified date, | ||
| 82 | # * we send a weak tag instead of a strong one, since it could | ||
| 83 | # * be modified again later in the second, and the validation | ||
| 84 | # * would be incorrect. | ||
| 85 | # */ | ||
| 86 | |||
| 87 | my ($dev, $ino, $mode, $nlink, $uid, $gid, $rdev, $size, $atime, $mtime) | ||
| 88 | = stat ($file) ; | ||
| 89 | |||
| 90 | my $weak = ($req->request_time - $mtime > 1)? "" : "W/"; | ||
| 91 | my $etag ; | ||
| 92 | if ($mode != 0) | ||
| 93 | { | ||
| 94 | $etag = sprintf ('%s"%x-%x-%x"', $weak, $ino, $size, $mtime) ; | ||
| 95 | } | ||
| 96 | else | ||
| 97 | { | ||
| 98 | $etag = sprintf ('%s"%x"', $weak, $mtime) ; | ||
| 99 | } | ||
| 100 | |||
| 101 | ### Set ETag header, without ETag resuming download doesn't work at all | ||
| 102 | $req->header_out('ETag', $etag) ; | ||
| 103 | |||
| 104 | ### Check is there is an incoming if-none-match header | ||
| 105 | my $if_none_match = $req->header_in('If-None-Match') ; | ||
| 106 | |||
| 107 | if ($if_none_match eq $etag) | ||
| 108 | { | ||
| 109 | ### send not_modified headers in case file doesn't have changed | ||
| 110 | ### and return | ||
| 111 | $req->status (304) ; | ||
| 112 | $req->send_http_header ; | ||
| 113 | return OK ; | ||
| 114 | } | ||
| 115 | |||
| 116 | open PATCH, "$file" or die "$file: $!"; | ||
| 117 | |||
| 118 | ### Check is there is an incoming range and if-range header | ||
| 119 | my $range = $req->header_in('Range') ; | ||
| 120 | my $if_range = $req->header_in('If-Range') ; | ||
| 121 | |||
| 122 | ### If there is a correct range header and the if-range header matches | ||
| 123 | ### the etag | ||
| 124 | ### i.e. the file doesn't have changed, add the correct headers for | ||
| 125 | ### resuming the | ||
| 126 | ### download and advance the file pointer to the correct possition | ||
| 127 | if (($range =~ /bytes=(\d+)-/) && $if_range eq $etag) | ||
| 128 | { # continue download | ||
| 129 | my $start = $1 ; | ||
| 130 | my $end = $size - 1 ; | ||
| 131 | $req->status (206) ; | ||
| 132 | $req->header_out('Content-Range', "bytes $start-$end/$size" ) ; | ||
| 133 | |||
| 134 | $size -= $start ; | ||
| 135 | |||
| 136 | seek PATCH, $start, 0 ; | ||
| 137 | } | ||
| 138 | |||
| 139 | ### To make resuming a download work, we need _all_ of the follwing | ||
| 140 | ### headers! | ||
| 141 | $req->header_out('Accept-Ranges', 'bytes'); | ||
| 142 | $req->header_out('Last-Modified', formattime ($mtime)) ; | ||
| 143 | $req->header_out('Content-Length', $size) ; | ||
| 144 | |||
| 145 | ### Setup the content-type | ||
| 146 | if ($file =~ /\.zip$/i) { | ||
| 147 | $req->content_type('application/zip'); | ||
| 148 | } | ||
| 149 | elsif ($file =~ /\.Z$/i) { | ||
| 150 | $req->content_type('Content-type: application/compress'); | ||
| 151 | } | ||
| 152 | else { | ||
| 153 | $req->content_type('application/octet-stream'); | ||
| 154 | } | ||
| 155 | |||
| 156 | ### Send the headers | ||
| 157 | $req->send_http_header ; | ||
| 158 | |||
| 159 | ### ... and now send the content | ||
| 160 | no strict 'subs'; | ||
| 161 | $req->send_fd(PATCH); | ||
| 162 | close PATCH; | ||
| 163 | |||
| 164 | return OK ; | ||
| 165 | |||
| 166 | } | ||
| 167 | |||
| 168 | sub doit { | ||
| 169 | my $r = shift ; | ||
| 170 | |||
| 171 | download ({}, '/home/httpd/html/old.zip', $r) ; | ||
| 172 | #download ({}, '/home/httpd/html/c.zip', $r) ; | ||
| 173 | } | ||
| 174 | |||
| 175 | #doit; | ||
| 176 | |||
| 177 | 1; | ||
| 178 | __END__ | ||