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