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