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