ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/httpd.pl
(Generate patch)

Comparing Coro/myhttpd/httpd.pl (file contents):
Revision 1.72 by root, Tue Sep 10 03:08:03 2002 UTC vs.
Revision 1.79 by root, Fri Dec 1 03:53:33 2006 UTC

1use Coro; 1use Coro;
2use Coro::Semaphore; 2use Coro::Semaphore;
3use Coro::Event; 3use Coro::Event;
4use Coro::Socket; 4use Coro::Socket;
5use Coro::Signal; 5use Coro::Signal;
6use Coro::AIO ();
6 7
7use HTTP::Date; 8use HTTP::Date;
8use POSIX (); 9use POSIX ();
9 10
10use Compress::Zlib (); 11use Compress::Zlib ();
139package conn; 140package conn;
140 141
141use Socket; 142use Socket;
142use HTTP::Date; 143use HTTP::Date;
143use Convert::Scalar 'weaken'; 144use Convert::Scalar 'weaken';
144use Linux::AIO; 145use IO::AIO;
145 146
146Linux::AIO::min_parallel $::AIO_PARALLEL; 147IO::AIO::min_parallel $::AIO_PARALLEL;
147 148
148Event->io(fd => Linux::AIO::poll_fileno, 149Event->io (fd => IO::AIO::poll_fileno,
149 poll => 'r', async => 1, 150 poll => 'r', async => 1,
150 cb => \&Linux::AIO::poll_cb); 151 cb => \&IO::AIO::poll_cb);
151 152
152our %conn; # $conn{ip}{self} => connobj 153our %conn; # $conn{ip}{self} => connobj
153our %uri; # $uri{ip}{uri}{self} 154our %uri; # $uri{ip}{uri}{self}
154our %blocked; 155our %blocked;
155our %mimetype; 156our %mimetype;
192 193
193sub DESTROY { 194sub DESTROY {
194 #my $self = shift; 195 #my $self = shift;
195 $::conns--; 196 $::conns--;
196} 197}
198
199sub prune_cache {
200 my $hash = $_[0];
201
202 for (keys %$hash) {
203 if (ref $hash->{$_} eq HASH::) {
204 prune_cache($hash->{$_});
205 unless (scalar keys %{$hash->{$_}}) {
206 delete $hash->{$_};
207 $d2++;
208 }
209 }
210 }
211}
212
213sub prune_caches {
214 prune_cache \%conn;
215 prune_cache \%uri;
216
217 for (keys %blocked) {
218 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
219 }
220}
221
222Event->timer(interval => 60, cb => \&prune_caches);
197 223
198sub slog { 224sub slog {
199 my $self = shift; 225 my $self = shift;
200 main::slog($_[0], "$self->{remote_id}> $_[1]"); 226 main::slog($_[0], "$self->{remote_id}> $_[1]");
201} 227}
504 my $self = shift; 530 my $self = shift;
505 my $idx = $self->diridx; 531 my $idx = $self->diridx;
506 532
507 $self->response(200, "ok", 533 $self->response(200, "ok",
508 { 534 {
509 "Content-Type" => "text/html", 535 "Content-Type" => "text/html; charset=utf-8",
510 "Content-Length" => length $idx, 536 "Content-Length" => length $idx,
511 "Last-Modified" => time2str ($self->{stat}[9]), 537 "Last-Modified" => time2str ($self->{stat}[9]),
512 }, 538 },
513 $idx); 539 $idx);
514} 540}
515 541
516sub handle_file { 542sub handle_file {
517 my ($self, $queue, $tbf) = @_; 543 my ($self, $queue, $tbf) = @_;
518 my $length = $self->{stat}[7]; 544 my $length = $self->{stat}[7];
519 my $hdr = { 545 my $hdr = {
520 "Last-Modified" => time2str ((stat _)[9]), 546 "Last-Modified" => time2str ((stat _)[9]),
547 "Accept-Ranges" => "bytes",
521 }; 548 };
522 549
523 my @code = (200, "ok"); 550 my @code = (200, "ok");
524 my ($l, $h); 551 my ($l, $h);
525 552
603 if ($blocked{$self->{remote_id}}) { 630 if ($blocked{$self->{remote_id}}) {
604 $self->{h}{connection} = "close"; 631 $self->{h}{connection} = "close";
605 die bless {}, err::; 632 die bless {}, err::;
606 } 633 }
607 634
608 if (0) { # !AIO 635 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), $buf, 0
609 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
610 or last; 636 or last;
611 } else {
612 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h),
613 $buf, 0, sub {
614 $r = $_[0];
615 Coro::ready($current);
616 });
617 &Coro::schedule;
618 last unless $r;
619 }
620 637
621 $tbf->request(length $buf); 638 $tbf->request (length $buf);
622 my $w = syswrite $self->{fh}, $buf 639 my $w = syswrite $self->{fh}, $buf
623 or last; 640 or last;
624 $::written += $w; 641 $::written += $w;
625 $self->{written} += $w; 642 $self->{written} += $w;
626 $l += $r; 643 $l += $r;
628 645
629 close $fh; 646 close $fh;
630 } 647 }
631} 648}
632 649
6331; 6501
651

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines