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