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