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.82 by root, Sat Dec 2 03:31:42 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;
197} 196}
198 197
199sub DESTROY { 198sub DESTROY {
200 my $self = shift; 199 my $self = shift;
201 200
202 close $self->{fh}; # workaround
203 --$::conns; 201 --$::conns;
204} 202}
205 203
206sub prune_cache { 204sub prune_cache {
207 my $hash = $_[0]; 205 my $hash = $_[0];
223 for (keys %blocked) { 221 for (keys %blocked) {
224 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; 222 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
225 } 223 }
226} 224}
227 225
228Event->timer (interval => 60, cb => \&prune_caches); 226our $PRUNE_WATCHER = EV::timer 60, 60, \&prune_caches;
229 227
230sub slog { 228sub slog {
231 my $self = shift; 229 my $self = shift;
232 main::slog($_[0], "$self->{remote_id}> $_[1]"); 230 main::slog($_[0], "$self->{remote_id}> $_[1]");
233} 231}
426sub map_uri { 424sub map_uri {
427 my $self = shift; 425 my $self = shift;
428 my $host = $self->{server_name}; 426 my $host = $self->{server_name};
429 my $uri = $self->{uri}; 427 my $uri = $self->{uri};
430 428
429 $host =~ /[\/\\]/
430 and $self->err(400, "bad request");
431
431 # some massaging, also makes it more secure 432 # some massaging, also makes it more secure
432 $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;
433 $uri =~ s%//+%/%g; 434 $uri =~ s%//+%/%g;
434 $uri =~ s%/\.(?=/|$)%%g; 435 $uri =~ s%/\.(?=/|$)%%g;
435 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; 436 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%;
554 }; 555 };
555 556
556 my @code = (200, "ok"); 557 my @code = (200, "ok");
557 my ($l, $h); 558 my ($l, $h);
558 559
559 if ($self->{h}{range} =~ /^bytes=(.*)$/) { 560 if ($self->{h}{range} =~ /^bytes=(.*)$/i) {
560 for (split /,/, $1) { 561 for (split /,/, $1) {
561 if (/^-(\d+)$/) { 562 if (/^-(\d+)$/) {
562 ($l, $h) = ($length - $1, $length - 1); 563 ($l, $h) = ($length - $1, $length - 1);
563 } elsif (/^(\d+)-(\d*)$/) { 564 } elsif (/^(\d+)-(\d*)$/) {
564 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 565 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
630 631
631 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
632 or last; 633 or last;
633 634
634 $tbf->request (length $buf); 635 $tbf->request (length $buf);
635 my $w = syswrite $self->{fh}, $buf 636 my $w = $self->{fh}->syswrite ($buf)
636 or last; 637 or last;
637 $::written += $w; 638 $::written += $w;
638 $self->{written} += $w; 639 $self->{written} += $w;
639 $l += $w; 640 $l += $w;
640 } 641 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines