|
|
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 | } |
… | |
… | |
205 | } |
238 | } |
206 | } |
239 | } |
207 | |
240 | |
208 | if ($self->{method} ne "HEAD" |
241 | if ($self->{method} ne "HEAD" |
209 | && $self->{h}{"accept-encoding"} =~ /\bgzip\b/ |
242 | && $self->{h}{"accept-encoding"} =~ /\bgzip\b/ |
|
|
243 | && 400 < length $content |
210 | && $hdr->{"Content-Length"} == length $content |
244 | && $hdr->{"Content-Length"} == length $content |
211 | && !exists $hdr->{"Content-Encoding"} |
245 | && !exists $hdr->{"Content-Encoding"} |
212 | ) { |
246 | ) { |
213 | my $orig = length $content; |
247 | my $orig = length $content; |
214 | $hdr->{"Content-Encoding"} = "gzip"; |
248 | $hdr->{"Content-Encoding"} = "gzip"; |
… | |
… | |
216 | $hdr->{"Content-Length"} = length $content; |
250 | $hdr->{"Content-Length"} = length $content; |
217 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
251 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
218 | } |
252 | } |
219 | |
253 | |
220 | $res .= "Date: $HTTP_NOW\015\012"; |
254 | $res .= "Date: $HTTP_NOW\015\012"; |
|
|
255 | $res .= "Server: $::NAME\015\012"; |
221 | |
256 | |
222 | while (my ($h, $v) = each %$hdr) { |
257 | while (my ($h, $v) = each %$hdr) { |
223 | $res .= "$h: $v\015\012" |
258 | $res .= "$h: $v\015\012" |
224 | } |
259 | } |
225 | $res .= "\015\012"; |
260 | $res .= "\015\012"; |
226 | |
261 | |
227 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
262 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
228 | |
263 | |
229 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW). |
264 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW). |
230 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. |
265 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. |
231 | " \"$self->{h}{referer}\"\n"; |
266 | " \"$self->{h}{referer}\"\n"; |
232 | |
267 | |
233 | print $accesslog $log if $accesslog; |
268 | print $::accesslog $log if $::accesslog; |
234 | print STDERR $log; |
269 | print STDERR $log; |
235 | |
270 | |
236 | $self->{written} += |
271 | $tbf_top->request(length $res, 1e6); |
237 | print {$self->{fh}} $res; |
272 | $self->{written} += print {$self->{fh}} $res; |
238 | } |
273 | } |
239 | |
274 | |
240 | sub err { |
275 | sub err { |
241 | my $self = shift; |
276 | my $self = shift; |
242 | my ($code, $msg, $hdr, $content) = @_; |
277 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
246 | $hdr->{"Content-Type"} = "text/plain"; |
281 | $hdr->{"Content-Type"} = "text/plain"; |
247 | $hdr->{"Content-Length"} = length $content; |
282 | $hdr->{"Content-Length"} = length $content; |
248 | } |
283 | } |
249 | $hdr->{"Connection"} = "close"; |
284 | $hdr->{"Connection"} = "close"; |
250 | |
285 | |
251 | $self->response($code, $msg, $hdr, $content); |
286 | $self->response ($code, $msg, $hdr, $content); |
252 | |
287 | |
253 | die bless {}, err::; |
288 | die bless {}, err:: |
254 | } |
289 | } |
255 | |
290 | |
256 | sub handle { |
291 | sub handle { |
257 | my $self = shift; |
292 | my $self = shift; |
258 | my $fh = $self->{fh}; |
293 | my $fh = $self->{fh}; |
259 | |
294 | |
260 | my $host; |
295 | my $host; |
261 | |
296 | |
262 | $fh->timeout($::REQ_TIMEOUT); |
297 | $fh->timeout($::REQ_TIMEOUT); |
263 | while() { |
298 | while () { |
264 | $self->{reqs}++; |
299 | $self->{reqs}++; |
265 | |
300 | |
266 | # read request and parse first line |
301 | # read request and parse first line |
267 | my $req = $fh->readline("\015\012\015\012"); |
302 | my $req = $fh->readline("\015\012\015\012"); |
268 | |
303 | |
… | |
… | |
297 | my (%hdr, $h, $v); |
332 | my (%hdr, $h, $v); |
298 | |
333 | |
299 | $hdr{lc $1} .= ",$2" |
334 | $hdr{lc $1} .= ",$2" |
300 | while $req =~ /\G |
335 | while $req =~ /\G |
301 | ([^:\000-\040]+): |
336 | ([^:\000-\040]+): |
302 | [\010\040]* |
337 | [\011\040]* |
303 | ((?: [^\015\012]+ | \015\012[\010\040] )*) |
338 | ((?: [^\015\012]+ | \015\012[\011\040] )*) |
304 | \015\012 |
339 | \015\012 |
305 | /gxc; |
340 | /gxc; |
306 | |
341 | |
307 | $req =~ /\G\015\012$/ |
342 | $req =~ /\G\015\012$/ |
308 | or $self->err(400, "bad request"); |
343 | or $self->err(400, "bad request"); |
… | |
… | |
377 | # uri => path mapping |
412 | # uri => path mapping |
378 | sub map_uri { |
413 | sub map_uri { |
379 | my $self = shift; |
414 | my $self = shift; |
380 | my $host = $self->{server_name}; |
415 | my $host = $self->{server_name}; |
381 | my $uri = $self->{uri}; |
416 | my $uri = $self->{uri}; |
|
|
417 | |
|
|
418 | $host =~ /[\/\\]/ |
|
|
419 | and $self->err(400, "bad request"); |
382 | |
420 | |
383 | # some massaging, also makes it more secure |
421 | # some massaging, also makes it more secure |
384 | $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; |
385 | $uri =~ s%//+%/%g; |
423 | $uri =~ s%//+%/%g; |
386 | $uri =~ s%/\.(?=/|$)%%g; |
424 | $uri =~ s%/\.(?=/|$)%%g; |
… | |
… | |
430 | |
468 | |
431 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
469 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
432 | if ($::internal{$1}) { |
470 | if ($::internal{$1}) { |
433 | $::internal{$1}->($self); |
471 | $::internal{$1}->($self); |
434 | } else { |
472 | } else { |
435 | $self->err(404, "not found"); |
473 | $self->err (404, "not found"); |
436 | } |
474 | } |
437 | } else { |
475 | } else { |
438 | |
476 | |
439 | stat $path |
477 | Coro::AIO::aio_stat $path |
440 | or $self->err(404, "not found"); |
478 | and $self->err (404, "not found"); |
441 | |
479 | |
442 | $self->{stat} = [stat _]; |
480 | $self->{stat} = [stat _]; |
443 | |
481 | |
444 | # idiotic netscape sends idiotic headers AGAIN |
482 | # idiotic netscape sends idiotic headers AGAIN |
445 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
483 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
… | |
… | |
448 | if (-d _ && -r _) { |
486 | if (-d _ && -r _) { |
449 | # directory |
487 | # directory |
450 | if ($path !~ /\/$/) { |
488 | if ($path !~ /\/$/) { |
451 | # create a redirect to get the trailing "/" |
489 | # create a redirect to get the trailing "/" |
452 | # we don't try to avoid the :80 |
490 | # we don't try to avoid the :80 |
453 | $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}/" }); |
454 | } else { |
492 | } else { |
455 | $ims < $self->{stat}[9] |
493 | $ims < $self->{stat}[9] |
456 | or $self->err(304, "not modified"); |
494 | or $self->err (304, "not modified"); |
457 | |
495 | |
458 | if (-r "$path/index.html") { |
496 | if (-r "$path/index.html") { |
459 | # replace directory "size" by index.html filesize |
497 | # replace directory "size" by index.html filesize |
460 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
498 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
461 | $self->handle_file($queue_index); |
499 | $self->handle_file ($queue_index, $tbf_top); |
462 | } else { |
500 | } else { |
463 | $self->handle_dir; |
501 | $self->handle_dir; |
464 | } |
502 | } |
465 | } |
503 | } |
466 | } elsif (-f _ && -r _) { |
504 | } elsif (-f _ && -r _) { |
467 | -x _ and $self->err(403, "forbidden"); |
505 | -x _ and $self->err (403, "forbidden"); |
468 | |
506 | |
469 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
507 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
470 | my $timeout = $::NOW + 10; |
508 | my $timeout = $::NOW + 10; |
471 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
509 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
472 | if ($timeout < $::NOW) { |
510 | if ($timeout < $::NOW) { |
… | |
… | |
475 | $httpevent->wait; |
513 | $httpevent->wait; |
476 | } |
514 | } |
477 | } |
515 | } |
478 | } |
516 | } |
479 | |
517 | |
480 | $self->handle_file($queue_file); |
518 | $self->handle_file ($queue_file, $tbf_top); |
481 | } else { |
519 | } else { |
482 | $self->err(404, "not found"); |
520 | $self->err (404, "not found"); |
483 | } |
521 | } |
484 | } |
522 | } |
485 | } |
523 | } |
486 | |
524 | |
487 | sub handle_dir { |
525 | sub handle_dir { |
488 | my $self = shift; |
526 | my $self = shift; |
489 | my $idx = $self->diridx; |
527 | my $idx = $self->diridx; |
490 | |
528 | |
491 | $self->response(200, "ok", |
529 | $self->response (200, "ok", |
492 | { |
530 | { |
493 | "Content-Type" => "text/html", |
531 | "Content-Type" => "text/html; charset=utf-8", |
494 | "Content-Length" => length $idx, |
532 | "Content-Length" => length $idx, |
495 | "Last-Modified" => time2str ($self->{stat}[9]), |
533 | "Last-Modified" => time2str ($self->{stat}[9]), |
496 | }, |
534 | }, |
497 | $idx); |
535 | $idx); |
498 | } |
536 | } |
499 | |
537 | |
500 | sub handle_file { |
538 | sub handle_file { |
501 | my ($self, $queue) = @_; |
539 | my ($self, $queue, $tbf) = @_; |
502 | my $length = $self->{stat}[7]; |
540 | my $length = $self->{stat}[7]; |
503 | my $hdr = { |
541 | my $hdr = { |
504 | "Last-Modified" => time2str ((stat _)[9]), |
542 | "Last-Modified" => time2str ((stat _)[9]), |
|
|
543 | "Accept-Ranges" => "bytes", |
505 | }; |
544 | }; |
506 | |
545 | |
507 | my @code = (200, "ok"); |
546 | my @code = (200, "ok"); |
508 | my ($l, $h); |
547 | my ($l, $h); |
509 | |
548 | |
510 | if ($self->{h}{range} =~ /^bytes=(.*)$/) { |
549 | if ($self->{h}{range} =~ /^bytes=(.*)$/i) { |
511 | for (split /,/, $1) { |
550 | for (split /,/, $1) { |
512 | if (/^-(\d+)$/) { |
551 | if (/^-(\d+)$/) { |
513 | ($l, $h) = ($length - $1, $length - 1); |
552 | ($l, $h) = ($length - $1, $length - 1); |
514 | } elsif (/^(\d+)-(\d*)$/) { |
553 | } elsif (/^(\d+)-(\d*)$/) { |
515 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
554 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
… | |
… | |
519 | } |
558 | } |
520 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
559 | goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l; |
521 | } |
560 | } |
522 | $hdr->{"Content-Range"} = "bytes */$length"; |
561 | $hdr->{"Content-Range"} = "bytes */$length"; |
523 | $hdr->{"Content-Length"} = $length; |
562 | $hdr->{"Content-Length"} = $length; |
524 | $self->err(416, "not satisfiable", $hdr, ""); |
563 | $self->err (416, "not satisfiable", $hdr, ""); |
525 | |
564 | |
526 | satisfiable: |
565 | satisfiable: |
527 | # check for segmented downloads |
566 | # check for segmented downloads |
528 | if ($l && $::NO_SEGMENTED) { |
567 | if ($l && $::NO_SEGMENTED) { |
529 | my $timeout = $::NOW + 15; |
568 | my $timeout = $::NOW + 60; |
530 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
569 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
531 | if ($timeout <= $::NOW) { |
570 | if ($timeout <= $::NOW) { |
532 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
|
|
533 | #$self->err_segmented_download; |
571 | $self->err_segmented_download; |
534 | } else { |
572 | } else { |
535 | $httpevent->wait; |
573 | $httpevent->wait; |
536 | } |
574 | } |
537 | } |
575 | } |
538 | } |
576 | } |
… | |
… | |
548 | |
586 | |
549 | $self->{path} =~ /\.([^.]+)$/; |
587 | $self->{path} =~ /\.([^.]+)$/; |
550 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
588 | $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream"; |
551 | $hdr->{"Content-Length"} = $length; |
589 | $hdr->{"Content-Length"} = $length; |
552 | |
590 | |
553 | $self->response(@code, $hdr, ""); |
591 | $self->response (@code, $hdr, ""); |
554 | |
592 | |
555 | if ($self->{method} eq "GET") { |
593 | if ($self->{method} eq "GET") { |
556 | $self->{time} = $::NOW; |
594 | $self->{time} = $::NOW; |
|
|
595 | $self->{written} = 0; |
557 | |
596 | |
558 | my $current = $Coro::current; |
597 | my $fh = Coro::AIO::aio_open $self->{path}, Fcntl::O_RDONLY, 0 |
559 | |
|
|
560 | my ($fh, $buf, $r); |
|
|
561 | |
|
|
562 | open $fh, "<", $self->{path} |
|
|
563 | or die "$self->{path}: late open failure ($!)"; |
598 | or die "$self->{path}: late open failure ($!)"; |
564 | |
599 | |
565 | $h -= $l - 1; |
600 | $h -= $l - 1; |
566 | |
601 | |
567 | if (0) { # !AIO |
|
|
568 | if ($l) { |
|
|
569 | sysseek $fh, $l, 0; |
|
|
570 | } |
|
|
571 | } |
|
|
572 | |
|
|
573 | my $transfer = $queue->start_transfer($h); |
602 | my $transfer = $queue->start_transfer ($h); |
574 | my $locked; |
603 | my $locked; |
575 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
604 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
576 | |
605 | |
577 | while ($h > 0) { |
606 | while ($h > 0) { |
|
|
607 | Coro::cede; |
|
|
608 | my $transfer_lock = $TRANSFER_LOCK->guard ($self->{remote_id}); |
|
|
609 | |
578 | unless ($locked) { |
610 | unless ($locked) { |
579 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
611 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
580 | $bufsize = $::BUFSIZE; |
612 | $bufsize = $::BUFSIZE; |
581 | $self->{time} = $::NOW; |
613 | $self->{time} = $::NOW; |
|
|
614 | $self->{written} = 0; |
582 | } |
615 | } |
583 | } |
616 | } |
584 | |
617 | |
585 | if ($blocked{$self->{remote_id}}) { |
618 | if ($blocked{$self->{remote_id}}) { |
586 | $self->{h}{connection} = "close"; |
619 | $self->{h}{connection} = "close"; |
587 | die bless {}, err::; |
620 | die bless {}, err::; |
588 | } |
621 | } |
589 | |
622 | |
590 | if (0) { # !AIO |
623 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
591 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
|
|
592 | or last; |
624 | or last; |
593 | } else { |
625 | |
594 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
626 | # readahead to work around rijk disk issues |
595 | $buf, 0, sub { |
627 | IO::AIO::aio_readahead $fh, $l + $bufsize, $bufsize; |
596 | $r = $_[0]; |
628 | |
597 | Coro::ready($current); |
629 | $tbf->request (length $buf); |
598 | }); |
630 | my $w = $self->{fh}->syswrite ($buf) |
599 | &Coro::schedule; |
|
|
600 | last unless $r; |
|
|
601 | } |
|
|
602 | my $w = syswrite $self->{fh}, $buf |
|
|
603 | or last; |
631 | or last; |
604 | $::written += $w; |
632 | $::written += $w; |
605 | $self->{written} += $w; |
633 | $self->{written} += $w; |
606 | $l += $r; |
634 | $l += $w; |
607 | } |
635 | } |
608 | |
636 | |
609 | close $fh; |
637 | close $fh; |
610 | } |
638 | } |
611 | } |
639 | } |
612 | |
640 | |
613 | 1; |
641 | 1 |
|
|
642 | |