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