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.72 by root, Tue Sep 10 03:08:03 2002 UTC vs.
Revision 1.80 by root, Fri Dec 1 04:18:32 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 ();
21our $accesslog; 22our $accesslog;
22our $errorlog; 23our $errorlog;
23 24
24our $NOW; 25our $NOW;
25our $HTTP_NOW; 26our $HTTP_NOW;
27
28our $ERROR_LOG;
29our $ACCESS_LOG;
26 30
27Event->timer(interval => 1, hard => 1, cb => sub { 31Event->timer(interval => 1, hard => 1, cb => sub {
28 $NOW = time; 32 $NOW = time;
29 $HTTP_NOW = time2str $NOW; 33 $HTTP_NOW = time2str $NOW;
30})->now; 34})->now;
136 listen_on $http_port; 140 listen_on $http_port;
137} 141}
138 142
139package conn; 143package conn;
140 144
145use strict;
146use bytes;
147
141use Socket; 148use Socket;
142use HTTP::Date; 149use HTTP::Date;
143use Convert::Scalar 'weaken'; 150use Convert::Scalar 'weaken';
144use Linux::AIO; 151use IO::AIO;
145 152
146Linux::AIO::min_parallel $::AIO_PARALLEL; 153IO::AIO::min_parallel $::AIO_PARALLEL;
147 154
148Event->io(fd => Linux::AIO::poll_fileno, 155Event->io (fd => IO::AIO::poll_fileno,
149 poll => 'r', async => 1, 156 poll => 'r', async => 1,
150 cb => \&Linux::AIO::poll_cb); 157 cb => \&IO::AIO::poll_cb);
151 158
152our %conn; # $conn{ip}{self} => connobj 159our %conn; # $conn{ip}{self} => connobj
153our %uri; # $uri{ip}{uri}{self} 160our %uri; # $uri{ip}{uri}{self}
154our %blocked; 161our %blocked;
155our %mimetype; 162our %mimetype;
192 199
193sub DESTROY { 200sub DESTROY {
194 #my $self = shift; 201 #my $self = shift;
195 $::conns--; 202 $::conns--;
196} 203}
204
205sub 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
218sub 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
227Event->timer(interval => 60, cb => \&prune_caches);
197 228
198sub slog { 229sub slog {
199 my $self = shift; 230 my $self = shift;
200 main::slog($_[0], "$self->{remote_id}> $_[1]"); 231 main::slog($_[0], "$self->{remote_id}> $_[1]");
201} 232}
504 my $self = shift; 535 my $self = shift;
505 my $idx = $self->diridx; 536 my $idx = $self->diridx;
506 537
507 $self->response(200, "ok", 538 $self->response(200, "ok",
508 { 539 {
509 "Content-Type" => "text/html", 540 "Content-Type" => "text/html; charset=utf-8",
510 "Content-Length" => length $idx, 541 "Content-Length" => length $idx,
511 "Last-Modified" => time2str ($self->{stat}[9]), 542 "Last-Modified" => time2str ($self->{stat}[9]),
512 }, 543 },
513 $idx); 544 $idx);
514} 545}
515 546
516sub handle_file { 547sub handle_file {
517 my ($self, $queue, $tbf) = @_; 548 my ($self, $queue, $tbf) = @_;
518 my $length = $self->{stat}[7]; 549 my $length = $self->{stat}[7];
519 my $hdr = { 550 my $hdr = {
520 "Last-Modified" => time2str ((stat _)[9]), 551 "Last-Modified" => time2str ((stat _)[9]),
552 "Accept-Ranges" => "bytes",
521 }; 553 };
522 554
523 my @code = (200, "ok"); 555 my @code = (200, "ok");
524 my ($l, $h); 556 my ($l, $h);
525 557
570 602
571 if ($self->{method} eq "GET") { 603 if ($self->{method} eq "GET") {
572 $self->{time} = $::NOW; 604 $self->{time} = $::NOW;
573 $self->{written} = 0; 605 $self->{written} = 0;
574 606
575 my $current = $Coro::current; 607 my $fh;
576
577 my ($fh, $buf, $r);
578 608
579 open $fh, "<", $self->{path} 609 open $fh, "<", $self->{path}
580 or die "$self->{path}: late open failure ($!)"; 610 or die "$self->{path}: late open failure ($!)";
581 611
582 $h -= $l - 1; 612 $h -= $l - 1;
603 if ($blocked{$self->{remote_id}}) { 633 if ($blocked{$self->{remote_id}}) {
604 $self->{h}{connection} = "close"; 634 $self->{h}{connection} = "close";
605 die bless {}, err::; 635 die bless {}, err::;
606 } 636 }
607 637
608 if (0) { # !AIO 638 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0
609 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
610 or last; 639 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 640
621 $tbf->request(length $buf); 641 $tbf->request (length $buf);
622 my $w = syswrite $self->{fh}, $buf 642 my $w = syswrite $self->{fh}, $buf
623 or last; 643 or last;
624 $::written += $w; 644 $::written += $w;
625 $self->{written} += $w; 645 $self->{written} += $w;
626 $l += $r; 646 $l += $w;
627 } 647 }
628 648
629 close $fh; 649 close $fh;
630 } 650 }
631} 651}
632 652
6331; 6531
654

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines