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

Comparing cvsroot/Coro/myhttpd/httpd.pl (file contents):
Revision 1.73 by root, Sat Oct 5 09:02:17 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;
212sub prune_caches { 213sub prune_caches {
213 prune_cache \%conn; 214 prune_cache \%conn;
214 prune_cache \%uri; 215 prune_cache \%uri;
215 216
216 for (keys %blocked) { 217 for (keys %blocked) {
217 delete $blocked{$_} if $blocked{$_}[0] > $::NOW; 218 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
218 } 219 }
219} 220}
220 221
221Event->timer(interval => 60, cb => \&prune_caches); 222Event->timer(interval => 60, cb => \&prune_caches);
222 223
529 my $self = shift; 530 my $self = shift;
530 my $idx = $self->diridx; 531 my $idx = $self->diridx;
531 532
532 $self->response(200, "ok", 533 $self->response(200, "ok",
533 { 534 {
534 "Content-Type" => "text/html", 535 "Content-Type" => "text/html; charset=utf-8",
535 "Content-Length" => length $idx, 536 "Content-Length" => length $idx,
536 "Last-Modified" => time2str ($self->{stat}[9]), 537 "Last-Modified" => time2str ($self->{stat}[9]),
537 }, 538 },
538 $idx); 539 $idx);
539} 540}
540 541
541sub handle_file { 542sub handle_file {
542 my ($self, $queue, $tbf) = @_; 543 my ($self, $queue, $tbf) = @_;
543 my $length = $self->{stat}[7]; 544 my $length = $self->{stat}[7];
544 my $hdr = { 545 my $hdr = {
545 "Last-Modified" => time2str ((stat _)[9]), 546 "Last-Modified" => time2str ((stat _)[9]),
547 "Accept-Ranges" => "bytes",
546 }; 548 };
547 549
548 my @code = (200, "ok"); 550 my @code = (200, "ok");
549 my ($l, $h); 551 my ($l, $h);
550 552
628 if ($blocked{$self->{remote_id}}) { 630 if ($blocked{$self->{remote_id}}) {
629 $self->{h}{connection} = "close"; 631 $self->{h}{connection} = "close";
630 die bless {}, err::; 632 die bless {}, err::;
631 } 633 }
632 634
633 if (0) { # !AIO 635 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), $buf, 0
634 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
635 or last; 636 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 637
646 $tbf->request(length $buf); 638 $tbf->request (length $buf);
647 my $w = syswrite $self->{fh}, $buf 639 my $w = syswrite $self->{fh}, $buf
648 or last; 640 or last;
649 $::written += $w; 641 $::written += $w;
650 $self->{written} += $w; 642 $self->{written} += $w;
651 $l += $r; 643 $l += $r;
653 645
654 close $fh; 646 close $fh;
655 } 647 }
656} 648}
657 649
6581; 6501
651

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines