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