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.81 by root, Sat Dec 2 03:29:29 2006 UTC vs.
Revision 1.87 by root, Wed Nov 19 11:41:06 2008 UTC

1use Coro; 1use Coro;
2use Coro::Semaphore; 2use Coro::Semaphore;
3use Coro::Event; 3use Coro::EV;
4use Coro::Socket; 4use Coro::Socket;
5use Coro::Signal; 5use Coro::Signal;
6use Coro::AIO (); 6use Coro::AIO ();
7 7
8use HTTP::Date; 8use HTTP::Date;
26our $HTTP_NOW; 26our $HTTP_NOW;
27 27
28our $ERROR_LOG; 28our $ERROR_LOG;
29our $ACCESS_LOG; 29our $ACCESS_LOG;
30 30
31Event->timer(interval => 1, hard => 1, cb => sub { 31our $update_time = EV::periodic 0, 1, undef, sub {
32 $NOW = time; 32 $NOW = time;
33 $HTTP_NOW = time2str $NOW; 33 $HTTP_NOW = time2str $NOW;
34})->now; 34};
35$update_time->invoke;
35 36
36if ($ERROR_LOG) { 37if ($ERROR_LOG) {
37 use IO::Handle; 38 use IO::Handle;
38 open $errorlog, ">>$ERROR_LOG" 39 open $errorlog, ">>$ERROR_LOG"
39 or die "$ERROR_LOG: $!"; 40 or die "$ERROR_LOG: $!";
150use Convert::Scalar 'weaken'; 151use Convert::Scalar 'weaken';
151use IO::AIO; 152use IO::AIO;
152 153
153IO::AIO::min_parallel $::AIO_PARALLEL; 154IO::AIO::min_parallel $::AIO_PARALLEL;
154 155
155Event->io (fd => IO::AIO::poll_fileno, 156our $AIO_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb;
156 poll => 'r', async => 1,
157 cb => \&IO::AIO::poll_cb);
158 157
159our %conn; # $conn{ip}{self} => connobj 158our %conn; # $conn{ip}{self} => connobj
160our %uri; # $uri{ip}{uri}{self} 159our %uri; # $uri{ip}{uri}{self}
161our %blocked; 160our %blocked;
162our %mimetype; 161our %mimetype;
188 187
189 $self->{time} = $::NOW; 188 $self->{time} = $::NOW;
190 189
191 weaken ($Coro::current->{conn} = $self); 190 weaken ($Coro::current->{conn} = $self);
192 191
193 $::conns++; 192 ++$::conns;
194 $::maxconns = $::conns if $::conns > $::maxconns; 193 $::maxconns = $::conns if $::conns > $::maxconns;
195 194
196 $self 195 $self
197} 196}
198 197
199sub DESTROY { 198sub DESTROY {
200 #my $self = shift; 199 my $self = shift;
201 close $self->{fh}; # workaround 200
202 --$::conns; 201 --$::conns;
203} 202}
204 203
205sub prune_cache { 204sub prune_cache {
206 my $hash = $_[0]; 205 my $hash = $_[0];
222 for (keys %blocked) { 221 for (keys %blocked) {
223 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; 222 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
224 } 223 }
225} 224}
226 225
227Event->timer (interval => 60, cb => \&prune_caches); 226our $PRUNE_WATCHER = EV::timer 60, 60, \&prune_caches;
228 227
229sub slog { 228sub slog {
230 my $self = shift; 229 my $self = shift;
231 main::slog($_[0], "$self->{remote_id}> $_[1]"); 230 main::slog($_[0], "$self->{remote_id}> $_[1]");
232} 231}
425sub map_uri { 424sub map_uri {
426 my $self = shift; 425 my $self = shift;
427 my $host = $self->{server_name}; 426 my $host = $self->{server_name};
428 my $uri = $self->{uri}; 427 my $uri = $self->{uri};
429 428
429 $host =~ /[\/\\]/
430 and $self->err(400, "bad request");
431
430 # some massaging, also makes it more secure 432 # some massaging, also makes it more secure
431 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; 433 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge;
432 $uri =~ s%//+%/%g; 434 $uri =~ s%//+%/%g;
433 $uri =~ s%/\.(?=/|$)%%g; 435 $uri =~ s%/\.(?=/|$)%%g;
434 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; 436 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%;
553 }; 555 };
554 556
555 my @code = (200, "ok"); 557 my @code = (200, "ok");
556 my ($l, $h); 558 my ($l, $h);
557 559
558 if ($self->{h}{range} =~ /^bytes=(.*)$/) { 560 if ($self->{h}{range} =~ /^bytes=(.*)$/i) {
559 for (split /,/, $1) { 561 for (split /,/, $1) {
560 if (/^-(\d+)$/) { 562 if (/^-(\d+)$/) {
561 ($l, $h) = ($length - $1, $length - 1); 563 ($l, $h) = ($length - $1, $length - 1);
562 } elsif (/^(\d+)-(\d*)$/) { 564 } elsif (/^(\d+)-(\d*)$/) {
563 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 565 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
629 631
630 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 632 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0
631 or last; 633 or last;
632 634
633 $tbf->request (length $buf); 635 $tbf->request (length $buf);
634 my $w = syswrite $self->{fh}, $buf 636 my $w = $self->{fh}->syswrite ($buf)
635 or last; 637 or last;
636 $::written += $w; 638 $::written += $w;
637 $self->{written} += $w; 639 $self->{written} += $w;
638 $l += $w; 640 $l += $w;
639 } 641 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines