1 | use Coro; |
1 | use Coro; |
2 | use Coro::Semaphore; |
2 | use Coro::Semaphore; |
3 | use Coro::Event; |
3 | use Coro::Event; |
4 | use Coro::Socket; |
4 | use Coro::Socket; |
|
|
5 | use Coro::Signal; |
5 | |
6 | |
6 | use HTTP::Date; |
7 | use HTTP::Date; |
|
|
8 | use POSIX (); |
|
|
9 | |
|
|
10 | use Compress::Zlib (); |
7 | |
11 | |
8 | no utf8; |
12 | no utf8; |
9 | use bytes; |
13 | use bytes; |
10 | |
14 | |
11 | # at least on my machine, this thingy serves files |
15 | # at least on my machine, this thingy serves files |
… | |
… | |
13 | # and quite a bit slower than thttpd :( |
17 | # and quite a bit slower than thttpd :( |
14 | |
18 | |
15 | $SIG{PIPE} = 'IGNORE'; |
19 | $SIG{PIPE} = 'IGNORE'; |
16 | |
20 | |
17 | our $accesslog; |
21 | our $accesslog; |
|
|
22 | our $errorlog; |
|
|
23 | |
|
|
24 | our $NOW; |
|
|
25 | our $HTTP_NOW; |
|
|
26 | |
|
|
27 | Event->timer(interval => 1, hard => 1, cb => sub { |
|
|
28 | $NOW = time; |
|
|
29 | $HTTP_NOW = time2str $NOW; |
|
|
30 | })->now; |
|
|
31 | |
|
|
32 | if ($ERROR_LOG) { |
|
|
33 | use IO::Handle; |
|
|
34 | open $errorlog, ">>$ERROR_LOG" |
|
|
35 | or die "$ERROR_LOG: $!"; |
|
|
36 | $errorlog->autoflush(1); |
|
|
37 | } |
18 | |
38 | |
19 | if ($ACCESS_LOG) { |
39 | if ($ACCESS_LOG) { |
20 | use IO::Handle; |
40 | use IO::Handle; |
21 | open $accesslog, ">>$ACCESS_LOG" |
41 | open $accesslog, ">>$ACCESS_LOG" |
22 | or die "$ACCESS_LOG: $!"; |
42 | or die "$ACCESS_LOG: $!"; |
… | |
… | |
24 | } |
44 | } |
25 | |
45 | |
26 | sub slog { |
46 | sub slog { |
27 | my $level = shift; |
47 | my $level = shift; |
28 | my $format = shift; |
48 | my $format = shift; |
|
|
49 | my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); |
29 | printf "---: $format\n", @_; |
50 | printf "$NOW: $format\n", @_; |
|
|
51 | printf $errorlog "$NOW: $format\n", @_ if $errorlog; |
30 | } |
52 | } |
31 | |
53 | |
32 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
54 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
|
|
55 | our $httpevent = new Coro::Signal; |
33 | |
56 | |
34 | our $wait_factor = 0.95; |
57 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
|
|
58 | our $queue_index = new transferqueue 10; |
35 | |
59 | |
36 | our @transfers = ( |
60 | our $tbf_top = new tbf rate => 200000; |
37 | [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], |
61 | |
38 | [(new Coro::Semaphore $MAX_TRANSFERS_LARGE || 50), 1], |
62 | my $unused_bytes = 0; |
39 | ); |
63 | my $unused_last = time; |
|
|
64 | |
|
|
65 | sub unused_bandwidth { |
|
|
66 | $unused_bytes += $_[0]; |
|
|
67 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
|
|
68 | $unused_last = $NOW; |
|
|
69 | $unused_bytes = 0; |
|
|
70 | $queue_file->force_wake_next; |
|
|
71 | slog 1, "forced filetransfer due to unused bandwidth"; |
|
|
72 | } |
|
|
73 | } |
40 | |
74 | |
41 | my @newcons; |
75 | my @newcons; |
42 | my @pool; |
76 | my @pool; |
43 | |
77 | |
44 | # one "execution thread" |
78 | # one "execution thread" |
45 | sub handler { |
79 | sub handler { |
46 | while () { |
80 | while () { |
47 | my $new = pop @newcons; |
|
|
48 | if ($new) { |
81 | if (@newcons) { |
49 | eval { |
82 | eval { |
50 | conn->new(@$new)->handle; |
83 | conn->new(@{pop @newcons})->handle; |
51 | }; |
84 | }; |
52 | slog 1, "$@" if $@ && !ref $@; |
85 | slog 1, "$@" if $@ && !ref $@; |
|
|
86 | |
|
|
87 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
88 | |
53 | $connections->up; |
89 | $connections->up; |
54 | } else { |
90 | } else { |
55 | last if @pool >= $MAX_POOL; |
91 | last if @pool >= $MAX_POOL; |
56 | push @pool, $Coro::current; |
92 | push @pool, $Coro::current; |
57 | schedule; |
93 | schedule; |
58 | } |
94 | } |
59 | } |
95 | } |
|
|
96 | } |
|
|
97 | |
|
|
98 | sub listen_on { |
|
|
99 | my $listen = $_[0]; |
|
|
100 | |
|
|
101 | push @listen_sockets, $listen; |
|
|
102 | |
|
|
103 | # the "main thread" |
|
|
104 | async { |
|
|
105 | slog 1, "accepting connections"; |
|
|
106 | while () { |
|
|
107 | $connections->down; |
|
|
108 | push @newcons, [$listen->accept]; |
|
|
109 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
110 | if (@pool) { |
|
|
111 | (pop @pool)->ready; |
|
|
112 | } else { |
|
|
113 | async \&handler; |
|
|
114 | } |
|
|
115 | |
|
|
116 | } |
|
|
117 | }; |
60 | } |
118 | } |
61 | |
119 | |
62 | my $http_port = new Coro::Socket |
120 | my $http_port = new Coro::Socket |
63 | LocalAddr => $SERVER_HOST, |
121 | LocalAddr => $SERVER_HOST, |
64 | LocalPort => $SERVER_PORT, |
122 | LocalPort => $SERVER_PORT, |
65 | ReuseAddr => 1, |
123 | ReuseAddr => 1, |
66 | Listen => 50, |
124 | Listen => 50, |
67 | or die "unable to start server"; |
125 | or die "unable to start server"; |
68 | |
126 | |
69 | push @listen_sockets, $http_port; |
127 | listen_on $http_port; |
70 | |
128 | |
71 | our $NOW; |
129 | if ($SERVER_PORT2) { |
72 | our $HTTP_NOW; |
130 | my $http_port = new Coro::Socket |
|
|
131 | LocalAddr => $SERVER_HOST, |
|
|
132 | LocalPort => $SERVER_PORT2, |
|
|
133 | ReuseAddr => 1, |
|
|
134 | Listen => 50, |
|
|
135 | or die "unable to start server"; |
73 | |
136 | |
74 | Event->timer(interval => 1, hard => 1, cb => sub { |
137 | listen_on $http_port; |
75 | $NOW = time; |
138 | } |
76 | $HTTP_NOW = time2str $NOW; |
|
|
77 | })->now; |
|
|
78 | |
|
|
79 | # the "main thread" |
|
|
80 | async { |
|
|
81 | slog 1, "accepting connections"; |
|
|
82 | while () { |
|
|
83 | $connections->down; |
|
|
84 | push @newcons, [$http_port->accept]; |
|
|
85 | #slog 3, "accepted @$connections ".scalar(@pool); |
|
|
86 | if (@pool) { |
|
|
87 | (pop @pool)->ready; |
|
|
88 | } else { |
|
|
89 | async \&handler; |
|
|
90 | } |
|
|
91 | |
|
|
92 | } |
|
|
93 | }; |
|
|
94 | |
139 | |
95 | package conn; |
140 | package conn; |
96 | |
141 | |
97 | use Socket; |
142 | use Socket; |
98 | use HTTP::Date; |
143 | use HTTP::Date; |
… | |
… | |
125 | |
170 | |
126 | read_mimetypes; |
171 | read_mimetypes; |
127 | |
172 | |
128 | sub new { |
173 | sub new { |
129 | my $class = shift; |
174 | my $class = shift; |
|
|
175 | my $fh = shift; |
130 | my $peername = shift; |
176 | my $peername = shift; |
131 | my $fh = shift; |
|
|
132 | my $self = bless { fh => $fh }, $class; |
177 | my $self = bless { fh => $fh }, $class; |
133 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
178 | my (undef, $iaddr) = unpack_sockaddr_in $peername |
134 | or $self->err(500, "unable to decode peername"); |
179 | or $self->err(500, "unable to decode peername"); |
135 | |
180 | |
|
|
181 | $self->{remote_addr} = |
136 | $self->{remote_addr} = inet_ntoa $iaddr; |
182 | $self->{remote_id} = inet_ntoa $iaddr; |
|
|
183 | |
137 | $self->{time} = $::NOW; |
184 | $self->{time} = $::NOW; |
138 | |
185 | |
|
|
186 | weaken ($Coro::current->{conn} = $self); |
|
|
187 | |
139 | $::conns++; |
188 | $::conns++; |
|
|
189 | $::maxconns = $::conns if $::conns > $::maxconns; |
140 | |
190 | |
141 | $self; |
191 | $self; |
142 | } |
192 | } |
143 | |
193 | |
144 | sub DESTROY { |
194 | sub DESTROY { |
145 | my $self = shift; |
195 | #my $self = shift; |
146 | |
|
|
147 | $::conns--; |
196 | $::conns--; |
148 | |
|
|
149 | $self->eoconn; |
|
|
150 | } |
|
|
151 | |
|
|
152 | # end of connection |
|
|
153 | sub eoconn { |
|
|
154 | my $self = shift; |
|
|
155 | |
|
|
156 | # clean up hints |
|
|
157 | delete $conn{$self->{remote_id}}{$self*1}; |
|
|
158 | delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; |
|
|
159 | } |
197 | } |
160 | |
198 | |
161 | sub slog { |
199 | sub slog { |
162 | my $self = shift; |
200 | my $self = shift; |
163 | main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); |
201 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
164 | } |
202 | } |
165 | |
203 | |
166 | sub response { |
204 | sub response { |
167 | my ($self, $code, $msg, $hdr, $content) = @_; |
205 | my ($self, $code, $msg, $hdr, $content) = @_; |
168 | my $res = "HTTP/1.1 $code $msg\015\012"; |
206 | my $res = "HTTP/1.1 $code $msg\015\012"; |
|
|
207 | my $GZ = ""; |
169 | |
208 | |
170 | $self->{h}{connection} ||= $hdr->{Connection}; |
209 | if (exists $hdr->{Connection}) { |
|
|
210 | if ($hdr->{Connection} =~ /close/) { |
|
|
211 | $self->{h}{connection} = "close" |
|
|
212 | } |
|
|
213 | } else { |
|
|
214 | if ($self->{version} < 1.1) { |
|
|
215 | if ($self->{h}{connection} =~ /keep-alive/i) { |
|
|
216 | $hdr->{Connection} = "Keep-Alive"; |
|
|
217 | } else { |
|
|
218 | $self->{h}{connection} = "close" |
|
|
219 | } |
|
|
220 | } |
|
|
221 | } |
|
|
222 | |
|
|
223 | if ($self->{method} ne "HEAD" |
|
|
224 | && $self->{h}{"accept-encoding"} =~ /\bgzip\b/ |
|
|
225 | && 400 < length $content |
|
|
226 | && $hdr->{"Content-Length"} == length $content |
|
|
227 | && !exists $hdr->{"Content-Encoding"} |
|
|
228 | ) { |
|
|
229 | my $orig = length $content; |
|
|
230 | $hdr->{"Content-Encoding"} = "gzip"; |
|
|
231 | $content = Compress::Zlib::memGzip(\$content); |
|
|
232 | $hdr->{"Content-Length"} = length $content; |
|
|
233 | $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig); |
|
|
234 | } |
171 | |
235 | |
172 | $res .= "Date: $HTTP_NOW\015\012"; |
236 | $res .= "Date: $HTTP_NOW\015\012"; |
173 | |
237 | |
174 | while (my ($h, $v) = each %$hdr) { |
238 | while (my ($h, $v) = each %$hdr) { |
175 | $res .= "$h: $v\015\012" |
239 | $res .= "$h: $v\015\012" |
176 | } |
240 | } |
177 | $res .= "\015\012"; |
241 | $res .= "\015\012"; |
178 | |
242 | |
179 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
243 | $res .= $content if defined $content and $self->{method} ne "HEAD"; |
180 | |
244 | |
181 | my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; |
245 | my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW). |
|
|
246 | " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ. |
|
|
247 | " \"$self->{h}{referer}\"\n"; |
182 | |
248 | |
183 | print $accesslog $log if $accesslog; |
249 | print $::accesslog $log if $::accesslog; |
184 | print STDERR $log; |
250 | print STDERR $log; |
185 | |
251 | |
186 | $self->{written} += |
252 | $tbf_top->request(length $res, 1e6); |
187 | print {$self->{fh}} $res; |
253 | $self->{written} += print {$self->{fh}} $res; |
188 | } |
254 | } |
189 | |
255 | |
190 | sub err { |
256 | sub err { |
191 | my $self = shift; |
257 | my $self = shift; |
192 | my ($code, $msg, $hdr, $content) = @_; |
258 | my ($code, $msg, $hdr, $content) = @_; |
… | |
… | |
247 | my (%hdr, $h, $v); |
313 | my (%hdr, $h, $v); |
248 | |
314 | |
249 | $hdr{lc $1} .= ",$2" |
315 | $hdr{lc $1} .= ",$2" |
250 | while $req =~ /\G |
316 | while $req =~ /\G |
251 | ([^:\000-\040]+): |
317 | ([^:\000-\040]+): |
252 | [\008\040]* |
318 | [\011\040]* |
253 | ((?: [^\015\012]+ | \015\012[\008\040] )*) |
319 | ((?: [^\015\012]+ | \015\012[\011\040] )*) |
254 | \015\012 |
320 | \015\012 |
255 | /gxc; |
321 | /gxc; |
256 | |
322 | |
257 | $req =~ /\G\015\012$/ |
323 | $req =~ /\G\015\012$/ |
258 | or $self->err(400, "bad request"); |
324 | or $self->err(400, "bad request"); |
… | |
… | |
270 | $id .= "[".$self->{h}{"x-forwarded-for"}."]"; |
336 | $id .= "[".$self->{h}{"x-forwarded-for"}."]"; |
271 | } |
337 | } |
272 | |
338 | |
273 | $self->{remote_id} = $id; |
339 | $self->{remote_id} = $id; |
274 | |
340 | |
|
|
341 | weaken (local $conn{$id}{$self*1} = $self); |
|
|
342 | |
275 | if ($blocked{$id}) { |
343 | if ($blocked{$id}) { |
276 | $self->err_blocked($blocked{$id}) |
344 | $self->err_blocked |
277 | if $blocked{$id} > $::NOW; |
345 | if $blocked{$id}[0] > $::NOW; |
278 | |
346 | |
279 | delete $blocked{$id}; |
347 | delete $blocked{$id}; |
280 | } |
|
|
281 | |
|
|
282 | if (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
283 | my $delay = $::PER_TIMEOUT + 15; |
|
|
284 | while (%{$conn{$id}} >= $::MAX_CONN_IP) { |
|
|
285 | if ($delay <= 0) { |
|
|
286 | $self->slog(2, "blocked ip $id"); |
|
|
287 | $self->err_blocked; |
|
|
288 | } else { |
|
|
289 | Coro::Event::do_timer(after => 4); $delay -= 4; |
|
|
290 | } |
|
|
291 | } |
|
|
292 | } |
348 | } |
293 | |
349 | |
294 | # find out server name and port |
350 | # find out server name and port |
295 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
351 | if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) { |
296 | $host = $1; |
352 | $host = $1; |
… | |
… | |
300 | |
356 | |
301 | if (defined $host) { |
357 | if (defined $host) { |
302 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
358 | $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; |
303 | } else { |
359 | } else { |
304 | ($self->{server_port}, $host) |
360 | ($self->{server_port}, $host) |
305 | = unpack_sockaddr_in $self->{fh}->getsockname |
361 | = unpack_sockaddr_in $self->{fh}->sockname |
306 | or $self->err(500, "unable to get socket name"); |
362 | or $self->err(500, "unable to get socket name"); |
307 | $host = inet_ntoa $host; |
363 | $host = inet_ntoa $host; |
308 | } |
364 | } |
309 | |
365 | |
310 | $self->{server_name} = $host; |
366 | $self->{server_name} = $host; |
311 | |
367 | |
312 | # enter ourselves into various lists |
|
|
313 | weaken ($conn{$id}{$self*1} = $self); |
|
|
314 | weaken ($uri{$id}{$self->{uri}}{$self*1} = $self); |
368 | weaken (local $uri{$id}{$self->{uri}}{$self*1} = $self); |
315 | |
369 | |
316 | eval { |
370 | eval { |
317 | $self->map_uri; |
371 | $self->map_uri; |
318 | $self->respond; |
372 | $self->respond; |
319 | }; |
373 | }; |
320 | |
374 | |
321 | $self->eoconn; |
|
|
322 | |
|
|
323 | die if $@ && !ref $@; |
375 | die if $@ && !ref $@; |
324 | |
376 | |
325 | last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; |
377 | last if $self->{h}{connection} =~ /close/i; |
|
|
378 | |
|
|
379 | $httpevent->broadcast; |
326 | |
380 | |
327 | $fh->timeout($::PER_TIMEOUT); |
381 | $fh->timeout($::PER_TIMEOUT); |
328 | } |
382 | } |
|
|
383 | } |
|
|
384 | |
|
|
385 | sub block { |
|
|
386 | my $self = shift; |
|
|
387 | |
|
|
388 | $blocked{$self->{remote_id}} = [$::NOW + $_[0], $_[1]]; |
|
|
389 | $self->slog(2, "blocked ip $self->{remote_id}"); |
|
|
390 | $self->err_blocked; |
329 | } |
391 | } |
330 | |
392 | |
331 | # uri => path mapping |
393 | # uri => path mapping |
332 | sub map_uri { |
394 | sub map_uri { |
333 | my $self = shift; |
395 | my $self = shift; |
… | |
… | |
380 | |
442 | |
381 | sub respond { |
443 | sub respond { |
382 | my $self = shift; |
444 | my $self = shift; |
383 | my $path = $self->{path}; |
445 | my $path = $self->{path}; |
384 | |
446 | |
385 | stat $path |
447 | if ($self->{name} =~ s%^/internal/([^/]+)%%) { |
386 | or $self->err(404, "not found"); |
448 | if ($::internal{$1}) { |
387 | |
449 | $::internal{$1}->($self); |
388 | $self->{stat} = [stat _]; |
|
|
389 | |
|
|
390 | # idiotic netscape sends idiotic headers AGAIN |
|
|
391 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
|
|
392 | ? str2time $1 : 0; |
|
|
393 | |
|
|
394 | if (-d _ && -r _) { |
|
|
395 | # directory |
|
|
396 | if ($path !~ /\/$/) { |
|
|
397 | # create a redirect to get the trailing "/" |
|
|
398 | # we don't try to avoid the :80 |
|
|
399 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
|
|
400 | } else { |
450 | } else { |
401 | $ims < $self->{stat}[9] |
451 | $self->err(404, "not found"); |
|
|
452 | } |
|
|
453 | } else { |
|
|
454 | |
|
|
455 | stat $path |
402 | or $self->err(304, "not modified"); |
456 | or $self->err(404, "not found"); |
403 | |
457 | |
404 | if (-r "$path/index.html") { |
458 | $self->{stat} = [stat _]; |
405 | $self->{path} .= "/index.html"; |
459 | |
406 | $self->handle_file; |
460 | # idiotic netscape sends idiotic headers AGAIN |
|
|
461 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
|
|
462 | ? str2time $1 : 0; |
|
|
463 | |
|
|
464 | if (-d _ && -r _) { |
|
|
465 | # directory |
|
|
466 | if ($path !~ /\/$/) { |
|
|
467 | # create a redirect to get the trailing "/" |
|
|
468 | # we don't try to avoid the :80 |
|
|
469 | $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" }); |
407 | } else { |
470 | } else { |
|
|
471 | $ims < $self->{stat}[9] |
|
|
472 | or $self->err(304, "not modified"); |
|
|
473 | |
|
|
474 | if (-r "$path/index.html") { |
|
|
475 | # replace directory "size" by index.html filesize |
|
|
476 | $self->{stat} = [stat ($self->{path} .= "/index.html")]; |
|
|
477 | $self->handle_file($queue_index, $tbf_top); |
|
|
478 | } else { |
408 | $self->handle_dir; |
479 | $self->handle_dir; |
409 | } |
480 | } |
410 | } |
481 | } |
411 | } elsif (-f _ && -r _) { |
482 | } elsif (-f _ && -r _) { |
412 | -x _ and $self->err(403, "forbidden"); |
483 | -x _ and $self->err(403, "forbidden"); |
413 | $self->handle_file; |
484 | |
|
|
485 | if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
|
|
486 | my $timeout = $::NOW + 10; |
|
|
487 | while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) { |
|
|
488 | if ($timeout < $::NOW) { |
|
|
489 | $self->block($::BLOCKTIME, "too many connections"); |
|
|
490 | } else { |
|
|
491 | $httpevent->wait; |
|
|
492 | } |
|
|
493 | } |
|
|
494 | } |
|
|
495 | |
|
|
496 | $self->handle_file($queue_file, $tbf_top); |
414 | } else { |
497 | } else { |
415 | $self->err(404, "not found"); |
498 | $self->err(404, "not found"); |
|
|
499 | } |
416 | } |
500 | } |
417 | } |
501 | } |
418 | |
502 | |
419 | sub handle_dir { |
503 | sub handle_dir { |
420 | my $self = shift; |
504 | my $self = shift; |
… | |
… | |
422 | |
506 | |
423 | $self->response(200, "ok", |
507 | $self->response(200, "ok", |
424 | { |
508 | { |
425 | "Content-Type" => "text/html", |
509 | "Content-Type" => "text/html", |
426 | "Content-Length" => length $idx, |
510 | "Content-Length" => length $idx, |
|
|
511 | "Last-Modified" => time2str ($self->{stat}[9]), |
427 | }, |
512 | }, |
428 | $idx); |
513 | $idx); |
429 | } |
514 | } |
430 | |
515 | |
431 | sub handle_file { |
516 | sub handle_file { |
432 | my $self = shift; |
517 | my ($self, $queue, $tbf) = @_; |
433 | my $length = $self->{stat}[7]; |
518 | my $length = $self->{stat}[7]; |
434 | my $queue = $::transfers[$length >= $::TRANSFER_SMALL]; |
|
|
435 | my $hdr = { |
519 | my $hdr = { |
436 | "Last-Modified" => time2str ((stat _)[9]), |
520 | "Last-Modified" => time2str ((stat _)[9]), |
437 | }; |
521 | }; |
438 | |
522 | |
439 | my @code = (200, "ok"); |
523 | my @code = (200, "ok"); |
… | |
… | |
456 | $self->err(416, "not satisfiable", $hdr, ""); |
540 | $self->err(416, "not satisfiable", $hdr, ""); |
457 | |
541 | |
458 | satisfiable: |
542 | satisfiable: |
459 | # check for segmented downloads |
543 | # check for segmented downloads |
460 | if ($l && $::NO_SEGMENTED) { |
544 | if ($l && $::NO_SEGMENTED) { |
461 | my $delay = $::PER_TIMEOUT + 15; |
545 | my $timeout = $::NOW + 15; |
462 | while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
546 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
463 | if ($delay <= 0) { |
547 | if ($timeout <= $::NOW) { |
|
|
548 | $self->block($::BLOCKTIME, "segmented downloads are forbidden"); |
464 | $self->err_segmented_download; |
549 | #$self->err_segmented_download; |
465 | } else { |
550 | } else { |
466 | Coro::Event::do_timer(after => 4); $delay -= 4; |
551 | $httpevent->wait; |
467 | } |
552 | } |
468 | } |
553 | } |
469 | } |
554 | } |
470 | |
555 | |
471 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
556 | $hdr->{"Content-Range"} = "bytes $l-$h/$length"; |
… | |
… | |
484 | $self->response(@code, $hdr, ""); |
569 | $self->response(@code, $hdr, ""); |
485 | |
570 | |
486 | if ($self->{method} eq "GET") { |
571 | if ($self->{method} eq "GET") { |
487 | $self->{time} = $::NOW; |
572 | $self->{time} = $::NOW; |
488 | |
573 | |
489 | my $fudge = $queue->[0]->waiters; |
574 | my $current = $Coro::current; |
490 | $fudge = $fudge ? ($fudge+1)/$fudge : 1; |
|
|
491 | |
|
|
492 | $queue->[1] *= $fudge; |
|
|
493 | my $transfer = $queue->[0]->guard; |
|
|
494 | |
|
|
495 | if ($fudge != 1) { |
|
|
496 | $queue->[1] /= $fudge; |
|
|
497 | $queue->[1] = $queue->[1] * $::wait_factor |
|
|
498 | + ($::NOW - $self->{time}) * (1 - $::wait_factor); |
|
|
499 | } |
|
|
500 | $self->{time} = $::NOW; |
|
|
501 | |
|
|
502 | $self->{fh}->writable or return; |
|
|
503 | |
575 | |
504 | my ($fh, $buf, $r); |
576 | my ($fh, $buf, $r); |
505 | my $current = $Coro::current; |
577 | |
506 | open $fh, "<", $self->{path} |
578 | open $fh, "<", $self->{path} |
507 | or die "$self->{path}: late open failure ($!)"; |
579 | or die "$self->{path}: late open failure ($!)"; |
508 | |
580 | |
509 | $h -= $l - 1; |
581 | $h -= $l - 1; |
510 | |
582 | |
511 | if (0) { |
583 | if (0) { # !AIO |
512 | if ($l) { |
584 | if ($l) { |
513 | sysseek $fh, $l, 0; |
585 | sysseek $fh, $l, 0; |
514 | } |
586 | } |
515 | } |
587 | } |
|
|
588 | |
|
|
589 | my $transfer = $queue->start_transfer($h); |
|
|
590 | my $locked; |
|
|
591 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
516 | |
592 | |
517 | while ($h > 0) { |
593 | while ($h > 0) { |
|
|
594 | unless ($locked) { |
|
|
595 | if ($locked ||= $transfer->try($::WAIT_INTERVAL)) { |
|
|
596 | $bufsize = $::BUFSIZE; |
|
|
597 | $self->{time} = $::NOW; |
|
|
598 | } |
|
|
599 | } |
|
|
600 | |
|
|
601 | if ($blocked{$self->{remote_id}}) { |
|
|
602 | $self->{h}{connection} = "close"; |
|
|
603 | die bless {}, err::; |
|
|
604 | } |
|
|
605 | |
518 | if (0) { |
606 | if (0) { # !AIO |
519 | sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h |
607 | sysread $fh, $buf, $h > $bufsize ? $bufsize : $h |
520 | or last; |
608 | or last; |
521 | } else { |
609 | } else { |
522 | aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), |
610 | aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h), |
523 | $buf, 0, sub { |
611 | $buf, 0, sub { |
524 | $r = $_[0]; |
612 | $r = $_[0]; |
525 | Coro::ready($current); |
613 | Coro::ready($current); |
526 | }); |
614 | }); |
527 | &Coro::schedule; |
615 | &Coro::schedule; |
528 | last unless $r; |
616 | last unless $r; |
529 | } |
617 | } |
|
|
618 | |
|
|
619 | $tbf->request(length $buf); |
530 | my $w = syswrite $self->{fh}, $buf |
620 | my $w = syswrite $self->{fh}, $buf |
531 | or last; |
621 | or last; |
532 | $::written += $w; |
622 | $::written += $w; |
533 | $self->{written} += $w; |
623 | $self->{written} += $w; |
534 | $l += $r; |
624 | $l += $r; |