1 | use Coro; |
1 | use Coro; |
2 | use Coro::Semaphore; |
2 | use Coro::Semaphore; |
3 | use Coro::Event; |
3 | use Coro::EV; |
4 | use Coro::Socket; |
4 | use Coro::Socket; |
5 | use Coro::Signal; |
5 | use Coro::Signal; |
|
|
6 | use Coro::AIO (); |
6 | |
7 | |
7 | use HTTP::Date; |
8 | use HTTP::Date; |
8 | use POSIX (); |
9 | use POSIX (); |
|
|
10 | |
|
|
11 | use Compress::Zlib (); |
9 | |
12 | |
10 | no utf8; |
13 | no utf8; |
11 | use bytes; |
14 | use bytes; |
12 | |
15 | |
13 | # at least on my machine, this thingy serves files |
16 | # at least on my machine, this thingy serves files |
… | |
… | |
20 | our $errorlog; |
23 | our $errorlog; |
21 | |
24 | |
22 | our $NOW; |
25 | our $NOW; |
23 | our $HTTP_NOW; |
26 | our $HTTP_NOW; |
24 | |
27 | |
25 | Event->timer(interval => 1, hard => 1, cb => sub { |
28 | our $ERROR_LOG; |
|
|
29 | our $ACCESS_LOG; |
|
|
30 | |
|
|
31 | our $update_time = EV::periodic 0, 1, undef, sub { |
26 | $NOW = time; |
32 | $NOW = time; |
27 | $HTTP_NOW = time2str $NOW; |
33 | $HTTP_NOW = time2str $NOW; |
28 | })->now; |
34 | }; |
|
|
35 | $update_time->invoke; |
29 | |
36 | |
30 | if ($ERROR_LOG) { |
37 | if ($ERROR_LOG) { |
31 | use IO::Handle; |
38 | use IO::Handle; |
32 | open $errorlog, ">>$ERROR_LOG" |
39 | open $errorlog, ">>$ERROR_LOG" |
33 | or die "$ERROR_LOG: $!"; |
40 | or die "$ERROR_LOG: $!"; |
… | |
… | |
50 | } |
57 | } |
51 | |
58 | |
52 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
59 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
53 | our $httpevent = new Coro::Signal; |
60 | our $httpevent = new Coro::Signal; |
54 | |
61 | |
55 | our $queue_file = new transferqueue slots => $MAX_TRANSFERS, maxsize => 256*1024*1024; |
62 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
56 | our $queue_index = new transferqueue slots => 10; |
63 | our $queue_index = new transferqueue 10; |
57 | |
64 | |
58 | my @newcons; |
65 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
59 | my @pool; |
|
|
60 | |
66 | |
61 | # one "execution thread" |
67 | my $unused_bytes = 0; |
62 | sub handler { |
68 | my $unused_last = time; |
63 | while () { |
|
|
64 | if (@newcons) { |
|
|
65 | eval { |
|
|
66 | conn->new(@{pop @newcons})->handle; |
|
|
67 | }; |
|
|
68 | slog 1, "$@" if $@ && !ref $@; |
|
|
69 | |
69 | |
70 | $httpevent->broadcast; # only for testing, but doesn't matter much |
70 | sub unused_bandwidth { |
71 | |
71 | $unused_bytes += $_[0]; |
72 | $connections->up; |
72 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
73 | } else { |
73 | $unused_last = $NOW; |
74 | last if @pool >= $MAX_POOL; |
74 | $unused_bytes = 0; |
75 | push @pool, $Coro::current; |
75 | $queue_file->force_wake_next; |
76 | schedule; |
76 | slog 1, "forced filetransfer due to unused bandwidth"; |
77 | } |
|
|
78 | } |
77 | } |
79 | } |
78 | } |
80 | |
79 | |
81 | sub listen_on { |
80 | sub listen_on { |
82 | my $listen = $_[0]; |
81 | my $listen = $_[0]; |
… | |
… | |
86 | # the "main thread" |
85 | # the "main thread" |
87 | async { |
86 | async { |
88 | slog 1, "accepting connections"; |
87 | slog 1, "accepting connections"; |
89 | while () { |
88 | while () { |
90 | $connections->down; |
89 | $connections->down; |
91 | push @newcons, [$listen->accept]; |
90 | my @conn = $listen->accept; |
92 | #slog 3, "accepted @$connections ".scalar(@pool); |
91 | #slog 3, "accepted @$connections ".scalar(@pool); |
93 | if (@pool) { |
92 | |
94 | (pop @pool)->ready; |
93 | async_pool { |
95 | } else { |
94 | eval { |
96 | async \&handler; |
95 | conn->new (@conn)->handle; |
97 | } |
96 | }; |
|
|
97 | slog 1, "$@" if $@ && !ref $@; |
98 | |
98 | |
|
|
99 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
100 | |
|
|
101 | $connections->up; |
|
|
102 | } |
99 | } |
103 | } |
100 | }; |
104 | }; |
101 | } |
105 | } |
102 | |
106 | |
103 | my $http_port = new Coro::Socket |
107 | my $http_port = new Coro::Socket |
104 | LocalAddr => $SERVER_HOST, |
108 | LocalAddr => $SERVER_HOST, |
105 | LocalPort => $SERVER_PORT, |
109 | LocalPort => $SERVER_PORT, |
106 | ReuseAddr => 1, |
110 | ReuseAddr => 1, |
107 | Listen => 50, |
111 | Listen => 50, |
108 | or die "unable to start server"; |
112 | or die "unable to start server"; |
109 | |
113 | |
110 | listen_on $http_port; |
114 | listen_on $http_port; |
111 | |
115 | |
112 | if ($SERVER_PORT2) { |
116 | if ($SERVER_PORT2) { |
113 | my $http_port = new Coro::Socket |
117 | my $http_port = new Coro::Socket |
114 | LocalAddr => $SERVER_HOST, |
118 | LocalAddr => $SERVER_HOST, |
115 | LocalPort => $SERVER_PORT2, |
119 | LocalPort => $SERVER_PORT2, |
116 | ReuseAddr => 1, |
120 | ReuseAddr => 1, |
117 | Listen => 50, |
121 | Listen => 50, |
118 | or die "unable to start server"; |
122 | or die "unable to start server"; |
119 | |
123 | |
120 | listen_on $http_port; |
124 | listen_on $http_port; |
121 | } |
125 | } |
122 | |
126 | |
123 | package conn; |
127 | package conn; |
|
|
128 | |
|
|
129 | use strict; |
|
|
130 | use bytes; |
124 | |
131 | |
125 | use Socket; |
132 | use Socket; |
126 | use HTTP::Date; |
133 | use HTTP::Date; |
127 | use Convert::Scalar 'weaken'; |
134 | use Convert::Scalar 'weaken'; |
128 | use Linux::AIO; |
135 | use IO::AIO; |
129 | |
136 | |
130 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
137 | IO::AIO::min_parallel $::AIO_PARALLEL; |
131 | |
138 | |
132 | Event->io(fd => Linux::AIO::poll_fileno, |
139 | our $AIO_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; |
133 | poll => 'r', async => 1, |
|
|
134 | cb => \&Linux::AIO::poll_cb); |
|
|
135 | |
140 | |
136 | our %conn; # $conn{ip}{self} => connobj |
141 | our %conn; # $conn{ip}{self} => connobj |
137 | our %uri; # $uri{ip}{uri}{self} |
142 | our %uri; # $uri{ip}{uri}{self} |
138 | our %blocked; |
143 | our %blocked; |
139 | our %mimetype; |
144 | our %mimetype; |
140 | |
145 | |
141 | sub read_mimetypes { |
146 | sub read_mimetypes { |
142 | local *M; |
|
|
143 | if (open M, "<mime_types") { |
147 | if (open my $fh, "<mime_types") { |
144 | while (<M>) { |
148 | while (<$fh>) { |
145 | if (/^([^#]\S+)\t+(\S+)$/) { |
149 | if (/^([^#]\S+)\t+(\S+)$/) { |
146 | $mimetype{lc $1} = $2; |
150 | $mimetype{lc $1} = $2; |
147 | } |
151 | } |
148 | } |
152 | } |
149 | } else { |
153 | } else { |
… | |
… | |
157 | my $class = shift; |
161 | my $class = shift; |
158 | my $fh = shift; |
162 | my $fh = shift; |
159 | my $peername = shift; |
163 | my $peername = shift; |
160 | my $self = bless { fh => $fh }, $class; |
164 | my $self = bless { fh => $fh }, $class; |
161 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
165 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
162 | or $self->err(500, "unable to decode peername"); |
166 | or $self->err (500, "unable to decode peername"); |
163 | |
167 | |
164 | $self->{remote_addr} = |
168 | $self->{remote_addr} = |
165 | $self->{remote_id} = inet_ntoa $iaddr; |
169 | $self->{remote_id} = inet_ntoa $iaddr; |
166 | |
170 | |
167 | $self->{time} = $::NOW; |
171 | $self->{time} = $::NOW; |
168 | |
172 | |
169 | weaken ($Coro::current->{conn} = $self); |
173 | weaken ($Coro::current->{conn} = $self); |
170 | |
174 | |
171 | $::conns++; |
175 | ++$::conns; |
172 | $::maxconns = $::conns if $::conns > $::maxconns; |
176 | $::maxconns = $::conns if $::conns > $::maxconns; |
173 | |
177 | |
174 | $self; |
178 | $self |
175 | } |
179 | } |
176 | |
180 | |
177 | sub DESTROY { |
181 | sub DESTROY { |
178 | #my $self = shift; |
182 | my $self = shift; |
|
|
183 | |
179 | $::conns--; |
184 | --$::conns; |
180 | } |
185 | } |
|
|
186 | |
|
|
187 | sub prune_cache { |
|
|
188 | my $hash = $_[0]; |
|
|
189 | |
|
|
190 | for (keys %$hash) { |
|
|
191 | if (ref $hash->{$_} eq HASH::) { |
|
|
192 | prune_cache($hash->{$_}); |
|
|
193 | unless (scalar keys %{$hash->{$_}}) { |
|
|
194 | delete $hash->{$_}; |
|
|
195 | } |
|
|
196 | } |
|
|
197 | } |
|
|
198 | } |
|
|
199 | |
|
|
200 | sub prune_caches { |
|
|
201 | prune_cache \%conn; |
|
|
202 | prune_cache \%uri; |
|
|
203 | |
|
|
204 | for (keys %blocked) { |
|
|
205 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
|
|
206 | } |
|
|
207 | } |
|
|
208 | |
|
|
209 | our $PRUNE_WATCHER = EV::timer 60, 60, \&prune_caches; |
181 | |
210 | |
182 | sub slog { |
211 | sub slog { |
183 | my $self = shift; |
212 | my $self = shift; |
184 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
213 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
185 | } |
214 | } |
186 | |
215 | |
187 | sub response { |
216 | sub response { |
188 | my ($self, $code, $msg, $hdr, $content) = @_; |
217 | my ($self, $code, $msg, $hdr, $content) = @_; |
189 | my $res = "HTTP/1.1 $code $msg\015\012"; |
218 | my $res = "HTTP/1.1 $code $msg\015\012"; |
|
|
219 | my $GZ = ""; |
190 | |
220 | |
191 | if (exists $hdr->{Connection}) { |
221 | if (exists $hdr->{Connection}) { |
192 | if ($hdr->{Connection} =~ /close/) { |
222 | if ($hdr->{Connection} =~ /close/) { |
193 | $self->{h}{connection} = "close" |
223 | $self->{h}{connection} = "close" |
194 | } |
224 | } |
… | |
… | |
200 | $self->{h}{connection} = "close" |
230 | $self->{h}{connection} = "close" |
201 | } |
231 | } |
202 | } |
232 | } |
203 | } |
233 | } |
204 | |
234 | |
|
|
235 | if ($self->{method} ne "HEAD" |
|
|
236 | && $self->{h}{"accept-encoding"} =~ /\bgzip\b/ |
|
|
237 | && 400 < length $content |
|
|
238 | && $hdr->{"Content-Length"} == length $content |
|
|
239 | && !exists $hdr->{"Content-Encoding"} |
|
|
240 | ) { |
|
|
241 | my $orig = length $content; |
|
|
242 | $hdr->{"Content-Encoding"} = "gzip"; |
|
|
243 | $content = Compress::Zlib::memGzip(\$content); |
|
|
244 | $hdr->{"Content-Length"} = length $content; |
|
|
245 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
|
|
246 | } |
|
|
247 | |
205 | $res .= "Date: $HTTP_NOW\015\012"; |
248 | $res .= "Date: $HTTP_NOW\015\012"; |
|
|
249 | $res .= "Server: $::NAME\015\012"; |
206 | |
250 | |
207 | while (my ($h, $v) = each %$hdr) { |
251 | while (my ($h, $v) = each %$hdr) { |
208 | $res .= "$h: $v\015\012" |
252 | $res .= "$h: $v\015\012" |
209 | } |
253 | } |
210 | $res .= "\015\012"; |
254 | $res .= "\015\012"; |
211 | |
255 | |
212 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
256 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
213 | |
257 | |
214 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). |
258 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW). |
215 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}. |
259 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. |
216 | " \"$self->{h}{referer}\"\n"; |
260 | " \"$self->{h}{referer}\"\n"; |
217 | |
261 | |
218 | print $accesslog $log if $accesslog; |
262 | print $::accesslog $log if $::accesslog; |
219 | print STDERR $log; |
263 | print STDERR $log; |
220 | |
264 | |
221 | $self->{written} += |
265 | $tbf_top->request(length $res, 1e6); |
222 | print {$self->{fh}} $res; |
266 | $self->{written} += print {$self->{fh}} $res; |
223 | } |
267 | } |
224 | |
268 | |
225 | sub err { |
269 | sub err { |
226 | my $self = shift; |
270 | my $self = shift; |
227 | my ($code, $msg, $hdr, $content) = @_; |
271 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
231 | $hdr->{"Content-Type"} = "text/plain"; |
275 | $hdr->{"Content-Type"} = "text/plain"; |
232 | $hdr->{"Content-Length"} = length $content; |
276 | $hdr->{"Content-Length"} = length $content; |
233 | } |
277 | } |
234 | $hdr->{"Connection"} = "close"; |
278 | $hdr->{"Connection"} = "close"; |
235 | |
279 | |
236 | $self->response($code, $msg, $hdr, $content); |
280 | $self->response ($code, $msg, $hdr, $content); |
237 | |
281 | |
238 | die bless {}, err::; |
282 | die bless {}, err:: |
239 | } |
283 | } |
240 | |
284 | |
241 | sub handle { |
285 | sub handle { |
242 | my $self = shift; |
286 | my $self = shift; |
243 | my $fh = $self->{fh}; |
287 | my $fh = $self->{fh}; |
244 | |
288 | |
245 | my $host; |
289 | my $host; |
246 | |
290 | |
247 | $fh->timeout($::REQ_TIMEOUT); |
291 | $fh->timeout($::REQ_TIMEOUT); |
248 | while() { |
292 | while () { |
249 | $self->{reqs}++; |
293 | $self->{reqs}++; |
250 | |
294 | |
251 | # read request and parse first line |
295 | # read request and parse first line |
252 | my $req = $fh->readline("\015\012\015\012"); |
296 | my $req = $fh->readline("\015\012\015\012"); |
253 | |
297 | |
… | |
… | |
282 | my (%hdr, $h, $v); |
326 | my (%hdr, $h, $v); |
283 | |
327 | |
284 | $hdr{lc $1} .= ",$2" |
328 | $hdr{lc $1} .= ",$2" |
285 | while $req =~ /\G |
329 | while $req =~ /\G |
286 | ([^:\000-\040]+): |
330 | ([^:\000-\040]+): |
287 | [\008\040]* |
331 | [\011\040]* |
288 | ((?: [^\015\012]+ | \015\012[\008\040] )*) |
332 | ((?: [^\015\012]+ | \015\012[\011\040] )*) |
289 | \015\012 |
333 | \015\012 |
290 | /gxc; |
334 | /gxc; |
291 | |
335 | |
292 | $req =~ /\G\015\012$/ |
336 | $req =~ /\G\015\012$/ |
293 | or $self->err(400, "bad request"); |
337 | or $self->err(400, "bad request"); |
… | |
… | |
362 | # uri => path mapping |
406 | # uri => path mapping |
363 | sub map_uri { |
407 | sub map_uri { |
364 | my $self = shift; |
408 | my $self = shift; |
365 | my $host = $self->{server_name}; |
409 | my $host = $self->{server_name}; |
366 | my $uri = $self->{uri}; |
410 | my $uri = $self->{uri}; |
|
|
411 | |
|
|
412 | $host =~ /[\/\\]/ |
|
|
413 | and $self->err(400, "bad request"); |
367 | |
414 | |
368 | # some massaging, also makes it more secure |
415 | # some massaging, also makes it more secure |
369 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
416 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
370 | $uri =~ s%//+%/%g; |
417 | $uri =~ s%//+%/%g; |
371 | $uri =~ s%/\.(?=/|$)%%g; |
418 | $uri =~ s%/\.(?=/|$)%%g; |
… | |
… | |
415 | |
462 | |
416 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
463 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
417 | if ($::internal{$1}) { |
464 | if ($::internal{$1}) { |
418 | $::internal{$1}->($self); |
465 | $::internal{$1}->($self); |
419 | } else { |
466 | } else { |
420 | $self->err(404, "not found"); |
467 | $self->err (404, "not found"); |
421 | } |
468 | } |
422 | } else { |
469 | } else { |
423 | |
470 | |
424 | stat $path |
471 | Coro::AIO::aio_stat $path |
425 | or $self->err(404, "not found"); |
472 | and $self->err (404, "not found"); |
426 | |
473 | |
427 | $self->{stat} = [stat _]; |
474 | $self->{stat} = [stat _]; |
428 | |
475 | |
429 | # idiotic netscape sends idiotic headers AGAIN |
476 | # idiotic netscape sends idiotic headers AGAIN |
430 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
477 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
… | |
… | |
433 | if (-d _ && -r _) { |
480 | if (-d _ && -r _) { |
434 | # directory |
481 | # directory |
435 | if ($path !~ /\/$/) { |
482 | if ($path !~ /\/$/) { |
436 | # create a redirect to get the trailing "/" |
483 | # create a redirect to get the trailing "/" |
437 | # we don't try to avoid the :80 |
484 | # we don't try to avoid the :80 |
438 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
485 | $self->err (301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
439 | } else { |
486 | } else { |
440 | $ims < $self->{stat}[9] |
487 | $ims < $self->{stat}[9] |
441 | or $self->err(304, "not modified"); |
488 | or $self->err (304, "not modified"); |
442 | |
489 | |
443 | if (-r "$path/index.html") { |
490 | if (-r "$path/index.html") { |
444 | # replace directory "size" by index.html filesize |
491 | # replace directory "size" by index.html filesize |
445 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
492 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
446 | $self->handle_file($queue_index); |
493 | $self->handle_file ($queue_index, $tbf_top); |
447 | } else { |
494 | } else { |
448 | $self->handle_dir; |
495 | $self->handle_dir; |
449 | } |
496 | } |
450 | } |
497 | } |
451 | } elsif (-f _ && -r _) { |
498 | } elsif (-f _ && -r _) { |
452 | -x _ and $self->err(403, "forbidden"); |
499 | -x _ and $self->err (403, "forbidden"); |
453 | |
500 | |
454 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
501 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
455 | my $timeout = $::NOW + 10; |
502 | my $timeout = $::NOW + 10; |
456 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
503 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
457 | if ($timeout < $::NOW) { |
504 | if ($timeout < $::NOW) { |
… | |
… | |
460 | $httpevent->wait; |
507 | $httpevent->wait; |
461 | } |
508 | } |
462 | } |
509 | } |
463 | } |
510 | } |
464 | |
511 | |
465 | $self->handle_file($queue_file); |
512 | $self->handle_file ($queue_file, $tbf_top); |
466 | } else { |
513 | } else { |
467 | $self->err(404, "not found"); |
514 | $self->err (404, "not found"); |
468 | } |
515 | } |
469 | } |
516 | } |
470 | } |
517 | } |
471 | |
518 | |
472 | sub handle_dir { |
519 | sub handle_dir { |
473 | my $self = shift; |
520 | my $self = shift; |
474 | my $idx = $self->diridx; |
521 | my $idx = $self->diridx; |
475 | |
522 | |
476 | $self->response(200, "ok", |
523 | $self->response (200, "ok", |
477 | { |
524 | { |
478 | "Content-Type" => "text/html", |
525 | "Content-Type" => "text/html; charset=utf-8", |
479 | "Content-Length" => length $idx, |
526 | "Content-Length" => length $idx, |
480 | "Last-Modified" => time2str ($self->{stat}[9]), |
527 | "Last-Modified" => time2str ($self->{stat}[9]), |
481 | }, |
528 | }, |
482 | $idx); |
529 | $idx); |
483 | } |
530 | } |
484 | |
531 | |
485 | sub handle_file { |
532 | sub handle_file { |
486 | my ($self, $queue) = @_; |
533 | my ($self, $queue, $tbf) = @_; |
487 | my $length = $self->{stat}[7]; |
534 | my $length = $self->{stat}[7]; |
488 | my $hdr = { |
535 | my $hdr = { |
489 | "Last-Modified" => time2str ((stat _)[9]), |
536 | "Last-Modified" => time2str ((stat _)[9]), |
|
|
537 | "Accept-Ranges" => "bytes", |
490 | }; |
538 | }; |
491 | |
539 | |
492 | my @code = (200, "ok"); |
540 | my @code = (200, "ok"); |
493 | my ($l, $h); |
541 | my ($l, $h); |
494 | |
542 | |
495 | if ($self->{h}{range} =~ /^bytes=(.*)$/) { |
543 | if ($self->{h}{range} =~ /^bytes=(.*)$/i) { |
496 | for (split /,/, $1) { |
544 | for (split /,/, $1) { |
497 | if (/^-(\d+)$/) { |
545 | if (/^-(\d+)$/) { |
498 | ($l, $h) = ($length - $1, $length - 1); |
546 | ($l, $h) = ($length - $1, $length - 1); |
499 | } elsif (/^(\d+)-(\d*)$/) { |
547 | } elsif (/^(\d+)-(\d*)$/) { |
500 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
548 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
… | |
… | |
504 | } |
552 | } |
505 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
553 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
506 | } |
554 | } |
507 | $hdr->{"Content-Range"} = "bytes */$length"; |
555 | $hdr->{"Content-Range"} = "bytes */$length"; |
508 | $hdr->{"Content-Length"} = $length; |
556 | $hdr->{"Content-Length"} = $length; |
509 | $self->err(416, "not satisfiable", $hdr, ""); |
557 | $self->err (416, "not satisfiable", $hdr, ""); |
510 | |
558 | |
511 | satisfiable: |
559 | satisfiable: |
512 | # check for segmented downloads |
560 | # check for segmented downloads |
513 | if ($l && $::NO_SEGMENTED) { |
561 | if ($l && $::NO_SEGMENTED) { |
514 | my $timeout = $::NOW + 15; |
562 | my $timeout = $::NOW + 15; |
515 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
563 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
516 | if ($timeout <= $::NOW) { |
564 | if ($timeout <= $::NOW) { |
517 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
565 | $self->block ($::BLOCKTIME, "segmented downloads are forbidden"); |
518 | #$self->err_segmented_download; |
566 | #$self->err_segmented_download; |
519 | } else { |
567 | } else { |
520 | $httpevent->wait; |
568 | $httpevent->wait; |
521 | } |
569 | } |
522 | } |
570 | } |
… | |
… | |
533 | |
581 | |
534 | $self->{path} =~ /\.([^.]+)$/; |
582 | $self->{path} =~ /\.([^.]+)$/; |
535 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
583 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
536 | $hdr->{"Content-Length"} = $length; |
584 | $hdr->{"Content-Length"} = $length; |
537 | |
585 | |
538 | $self->response(@code, $hdr, ""); |
586 | $self->response (@code, $hdr, ""); |
539 | |
587 | |
540 | if ($self->{method} eq "GET") { |
588 | if ($self->{method} eq "GET") { |
541 | $self->{time} = $::NOW; |
589 | $self->{time} = $::NOW; |
|
|
590 | $self->{written} = 0; |
542 | |
591 | |
543 | my $current = $Coro::current; |
|
|
544 | |
|
|
545 | my ($fh, $buf, $r); |
|
|
546 | |
|
|
547 | open $fh, "<", $self->{path} |
592 | open my $fh, "<", $self->{path} |
548 | or die "$self->{path}: late open failure ($!)"; |
593 | or die "$self->{path}: late open failure ($!)"; |
549 | |
594 | |
550 | $h -= $l - 1; |
595 | $h -= $l - 1; |
551 | |
596 | |
552 | if (0) { # !AIO |
|
|
553 | if ($l) { |
|
|
554 | sysseek $fh, $l, 0; |
|
|
555 | } |
|
|
556 | } |
|
|
557 | |
|
|
558 | my $transfer = $queue->start_transfer($h); |
597 | my $transfer = $queue->start_transfer ($h); |
559 | my $locked; |
598 | my $locked; |
560 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
599 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
561 | |
600 | |
562 | while ($h > 0) { |
601 | while ($h > 0) { |
563 | unless ($locked) { |
602 | unless ($locked) { |
564 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
603 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
565 | $bufsize = $::BUFSIZE; |
604 | $bufsize = $::BUFSIZE; |
566 | $self->{time} = $::NOW; |
605 | $self->{time} = $::NOW; |
|
|
606 | $self->{written} = 0; |
567 | } |
607 | } |
568 | } |
608 | } |
569 | |
609 | |
570 | if ($blocked{$self->{remote_id}}) { |
610 | if ($blocked{$self->{remote_id}}) { |
571 | $self->{h}{connection} = "close"; |
611 | $self->{h}{connection} = "close"; |
572 | die bless {}, err::; |
612 | die bless {}, err::; |
573 | } |
613 | } |
574 | |
614 | |
575 | if (0) { # !AIO |
615 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
576 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
|
|
577 | or last; |
616 | or last; |
578 | } else { |
617 | |
579 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
618 | $tbf->request (length $buf); |
580 | $buf, 0, sub { |
619 | my $w = $self->{fh}->syswrite ($buf) |
581 | $r = $_[0]; |
|
|
582 | Coro::ready($current); |
|
|
583 | }); |
|
|
584 | &Coro::schedule; |
|
|
585 | last unless $r; |
|
|
586 | } |
|
|
587 | my $w = syswrite $self->{fh}, $buf |
|
|
588 | or last; |
620 | or last; |
589 | $::written += $w; |
621 | $::written += $w; |
590 | $self->{written} += $w; |
622 | $self->{written} += $w; |
591 | $l += $r; |
623 | $l += $w; |
592 | } |
624 | } |
593 | |
625 | |
594 | close $fh; |
626 | close $fh; |
595 | } |
627 | } |
596 | } |
628 | } |
597 | |
629 | |
598 | 1; |
630 | 1 |
|
|
631 | |