ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/httpd.pl
Revision: 1.80
Committed: Fri Dec 1 04:18:32 2006 UTC (17 years, 7 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.79: +9 -6 lines
Log Message:
*** empty log message ***

File Contents

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