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.77 by root, Sat Sep 17 20:21:11 2005 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 IO::AIO; 151use IO::AIO;
145 152
146IO::AIO::min_parallel $::AIO_PARALLEL; 153IO::AIO::min_parallel $::AIO_PARALLEL;
147 154
148Event->io(fd => IO::AIO::poll_fileno, 155Event->io (fd => IO::AIO::poll_fileno,
149 poll => 'r', async => 1, 156 poll => 'r', async => 1,
150 cb => \&IO::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;
201 for (keys %$hash) { 208 for (keys %$hash) {
202 if (ref $hash->{$_} eq HASH::) { 209 if (ref $hash->{$_} eq HASH::) {
203 prune_cache($hash->{$_}); 210 prune_cache($hash->{$_});
204 unless (scalar keys %{$hash->{$_}}) { 211 unless (scalar keys %{$hash->{$_}}) {
205 delete $hash->{$_}; 212 delete $hash->{$_};
206 $d2++;
207 } 213 }
208 } 214 }
209 } 215 }
210} 216}
211 217
596 602
597 if ($self->{method} eq "GET") { 603 if ($self->{method} eq "GET") {
598 $self->{time} = $::NOW; 604 $self->{time} = $::NOW;
599 $self->{written} = 0; 605 $self->{written} = 0;
600 606
601 my $current = $Coro::current; 607 my $fh;
602
603 my ($fh, $buf, $r);
604 608
605 open $fh, "<", $self->{path} 609 open $fh, "<", $self->{path}
606 or die "$self->{path}: late open failure ($!)"; 610 or die "$self->{path}: late open failure ($!)";
607 611
608 $h -= $l - 1; 612 $h -= $l - 1;
629 if ($blocked{$self->{remote_id}}) { 633 if ($blocked{$self->{remote_id}}) {
630 $self->{h}{connection} = "close"; 634 $self->{h}{connection} = "close";
631 die bless {}, err::; 635 die bless {}, err::;
632 } 636 }
633 637
634 if (0) { # !AIO 638 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0
635 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
636 or last; 639 or last;
637 } else {
638 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h),
639 $buf, 0, sub {
640 $r = $_[0];
641 Coro::ready($current);
642 });
643 &Coro::schedule;
644 last unless $r;
645 }
646 640
647 $tbf->request(length $buf); 641 $tbf->request (length $buf);
648 my $w = syswrite $self->{fh}, $buf 642 my $w = syswrite $self->{fh}, $buf
649 or last; 643 or last;
650 $::written += $w; 644 $::written += $w;
651 $self->{written} += $w; 645 $self->{written} += $w;
652 $l += $r; 646 $l += $w;
653 } 647 }
654 648
655 close $fh; 649 close $fh;
656 } 650 }
657} 651}
658 652
6591; 6531
654

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines