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.83 by root, Sun Dec 3 17:49:22 2006 UTC vs.
Revision 1.95 by root, Wed Apr 24 23:49:34 2013 UTC

1use AnyEvent ();
2
1use Coro; 3use Coro;
2use Coro::Semaphore; 4use Coro::Semaphore;
5use Coro::SemaphoreSet;
3use Coro::Event; 6use Coro::EV;
4use Coro::Socket; 7use Coro::Socket;
5use Coro::Signal; 8use Coro::Signal;
6use Coro::AIO (); 9use Coro::AIO ();
7 10
11use Fcntl;
8use HTTP::Date; 12use HTTP::Date;
9use POSIX (); 13use POSIX ();
10 14
11use Compress::Zlib (); 15use Compress::Zlib ();
12 16
13no utf8; 17use common::sense;
14use bytes;
15 18
16# at least on my machine, this thingy serves files 19# at least on my machine, this thingy serves files
17# quite a bit faster than apache, ;) 20# quite a bit faster than apache, ;)
18# and quite a bit slower than thttpd :( 21# and quite a bit slower than thttpd :(
19 22
20$SIG{PIPE} = 'IGNORE'; 23$SIG{PIPE} = 'IGNORE';
21 24
22our $accesslog; 25our $accesslog;
23our $errorlog; 26our $errorlog;
27our @listen_sockets;
24 28
25our $NOW; 29our $NOW;
26our $HTTP_NOW; 30our $HTTP_NOW;
27 31
28our $ERROR_LOG; 32our $ERROR_LOG;
29our $ACCESS_LOG; 33our $ACCESS_LOG;
34our $TRANSFER_LOCK = new Coro::SemaphoreSet; # lock to be acquired per ip
30 35
31Event->timer(interval => 1, hard => 1, cb => sub { 36our $update_time = EV::periodic 0, 1, undef, sub {
32 $NOW = time; 37 $NOW = time;
33 $HTTP_NOW = time2str $NOW; 38 $HTTP_NOW = time2str $NOW;
34})->now; 39};
40$update_time->invoke;
35 41
36if ($ERROR_LOG) { 42if ($ERROR_LOG) {
37 use IO::Handle; 43 use IO::Handle;
38 open $errorlog, ">>$ERROR_LOG" 44 open $errorlog, ">>$ERROR_LOG"
39 or die "$ERROR_LOG: $!"; 45 or die "$ERROR_LOG: $!";
48} 54}
49 55
50sub slog { 56sub slog {
51 my $level = shift; 57 my $level = shift;
52 my $format = shift; 58 my $format = shift;
59
60 $format = sprintf $format, @_ if @_;
61
53 my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); 62 my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW);
54 printf "$NOW: $format\n", @_; 63 print "$NOW: $format\n";
55 printf $errorlog "$NOW: $format\n", @_ if $errorlog; 64 print $errorlog "$NOW: $format\n", @_ if $errorlog;
56} 65}
57 66
58our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 67our $connections = new Coro::Semaphore $::MAX_CONNECTS || 250;
59our $httpevent = new Coro::Signal; 68our $httpevent = new Coro::Signal;
60 69
61our $queue_file = new transferqueue $MAX_TRANSFERS; 70our $queue_file = new transferqueue $::MAX_TRANSFERS;
62our $queue_index = new transferqueue 10; 71our $queue_index = new transferqueue 10;
63 72
64our $tbf_top = new tbf rate => $TBF_RATE || 100000; 73our $tbf_top = new tbf rate => $::TBF_RATE || 100000;
65 74
66my $unused_bytes = 0; 75my $unused_bytes = 0;
67my $unused_last = time; 76my $unused_last = time;
68 77
69sub unused_bandwidth { 78sub unused_bandwidth {
70 $unused_bytes += $_[0]; 79 $unused_bytes += $_[0];
71 if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { 80 if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) {
72 $unused_last = $NOW; 81 $unused_last = $NOW;
73 $unused_bytes = 0; 82 $unused_bytes = 0;
74 $queue_file->force_wake_next; 83 $queue_file->force_wake_next
75 slog 1, "forced filetransfer due to unused bandwidth"; 84 and slog 1, "forced filetransfer due to unused bandwidth";
76 }
77}
78
79my @newcons;
80my @pool;
81
82# one "execution thread"
83sub handler {
84 while () {
85 if (@newcons) {
86 eval {
87 conn->new (@{pop @newcons})->handle;
88 };
89 slog 1, "$@" if $@ && !ref $@;
90
91 $httpevent->broadcast; # only for testing, but doesn't matter much
92
93 $connections->up;
94 } else {
95 last if @pool >= $MAX_POOL;
96 push @pool, $Coro::current;
97 schedule;
98 }
99 } 85 }
100} 86}
101 87
102sub listen_on { 88sub listen_on {
103 my $listen = $_[0]; 89 my $listen = $_[0];
107 # the "main thread" 93 # the "main thread"
108 async { 94 async {
109 slog 1, "accepting connections"; 95 slog 1, "accepting connections";
110 while () { 96 while () {
111 $connections->down; 97 $connections->down;
112 push @newcons, [$listen->accept]; 98 my @conn = $listen->accept;
113 #slog 3, "accepted @$connections ".scalar(@pool); 99 #slog 3, "accepted @$connections ".scalar(@pool);
114 if (@pool) { 100
115 (pop @pool)->ready; 101 async_pool {
116 } else { 102 eval {
117 async \&handler; 103 conn->new (@conn)->handle;
104 };
105 slog 1, "$@" if $@ && !ref $@;
106
107 $httpevent->broadcast; # only for testing, but doesn't matter much
108
109 $connections->up;
118 } 110 }
119 } 111 }
120 }; 112 };
121} 113}
122 114
123my $http_port = new Coro::Socket 115my $http_port = new Coro::Socket
124 LocalAddr => $SERVER_HOST, 116 LocalAddr => $::SERVER_HOST,
125 LocalPort => $SERVER_PORT, 117 LocalPort => $::SERVER_PORT,
126 ReuseAddr => 1, 118 ReuseAddr => 1,
127 Listen => 50, 119 Listen => 50,
128 or die "unable to start server"; 120 or die "unable to start server";
129 121
130listen_on $http_port; 122listen_on $http_port;
131 123
132if ($SERVER_PORT2) { 124if ($::SERVER_PORT2) {
133 my $http_port = new Coro::Socket 125 my $http_port = new Coro::Socket
134 LocalAddr => $SERVER_HOST, 126 LocalAddr => $::SERVER_HOST,
135 LocalPort => $SERVER_PORT2, 127 LocalPort => $::SERVER_PORT2,
136 ReuseAddr => 1, 128 ReuseAddr => 1,
137 Listen => 50, 129 Listen => 50,
138 or die "unable to start server"; 130 or die "unable to start server";
139 131
140 listen_on $http_port; 132 listen_on $http_port;
141} 133}
142 134
143package conn; 135package conn;
144 136
145use strict; 137use common::sense;
146use bytes;
147 138
148use Socket; 139use Socket;
149use HTTP::Date; 140use HTTP::Date;
150use Convert::Scalar 'weaken'; 141use Convert::Scalar 'weaken';
151use IO::AIO; 142use IO::AIO;
143use AnyEvent::AIO;
152 144
153IO::AIO::min_parallel $::AIO_PARALLEL; 145IO::AIO::min_parallel $::AIO_PARALLEL;
154
155Event->io (fd => IO::AIO::poll_fileno,
156 poll => 'r', async => 1,
157 cb => \&IO::AIO::poll_cb);
158 146
159our %conn; # $conn{ip}{self} => connobj 147our %conn; # $conn{ip}{self} => connobj
160our %uri; # $uri{ip}{uri}{self} 148our %uri; # $uri{ip}{uri}{self}
161our %blocked; 149our %blocked;
162our %mimetype; 150our %mimetype;
222 for (keys %blocked) { 210 for (keys %blocked) {
223 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; 211 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
224 } 212 }
225} 213}
226 214
227Event->timer (interval => 60, cb => \&prune_caches); 215our $PRUNE_WATCHER = AE::timer 60, 60, \&prune_caches;
228 216
229sub slog { 217sub slog {
230 my $self = shift; 218 my $self = shift;
231 main::slog($_[0], "$self->{remote_id}> $_[1]"); 219 main::slog($_[0], "$self->{remote_id}> $_[1]");
232} 220}
425sub map_uri { 413sub map_uri {
426 my $self = shift; 414 my $self = shift;
427 my $host = $self->{server_name}; 415 my $host = $self->{server_name};
428 my $uri = $self->{uri}; 416 my $uri = $self->{uri};
429 417
418 $host =~ /[\/\\]/
419 and $self->err(400, "bad request");
420
430 # some massaging, also makes it more secure 421 # some massaging, also makes it more secure
431 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; 422 $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge;
432 $uri =~ s%//+%/%g; 423 $uri =~ s%//+%/%g;
433 $uri =~ s%/\.(?=/|$)%%g; 424 $uri =~ s%/\.(?=/|$)%%g;
434 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; 425 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%;
481 } else { 472 } else {
482 $self->err (404, "not found"); 473 $self->err (404, "not found");
483 } 474 }
484 } else { 475 } else {
485 476
486 stat $path 477 Coro::AIO::aio_stat $path
487 or $self->err (404, "not found"); 478 and $self->err (404, "not found");
488 479
489 $self->{stat} = [stat _]; 480 $self->{stat} = [stat _];
490 481
491 # idiotic netscape sends idiotic headers AGAIN 482 # idiotic netscape sends idiotic headers AGAIN
492 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 483 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
553 }; 544 };
554 545
555 my @code = (200, "ok"); 546 my @code = (200, "ok");
556 my ($l, $h); 547 my ($l, $h);
557 548
558 if ($self->{h}{range} =~ /^bytes=(.*)$/) { 549 if ($self->{h}{range} =~ /^bytes=(.*)$/i) {
559 for (split /,/, $1) { 550 for (split /,/, $1) {
560 if (/^-(\d+)$/) { 551 if (/^-(\d+)$/) {
561 ($l, $h) = ($length - $1, $length - 1); 552 ($l, $h) = ($length - $1, $length - 1);
562 } elsif (/^(\d+)-(\d*)$/) { 553 } elsif (/^(\d+)-(\d*)$/) {
563 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 554 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
572 $self->err (416, "not satisfiable", $hdr, ""); 563 $self->err (416, "not satisfiable", $hdr, "");
573 564
574satisfiable: 565satisfiable:
575 # check for segmented downloads 566 # check for segmented downloads
576 if ($l && $::NO_SEGMENTED) { 567 if ($l && $::NO_SEGMENTED) {
577 my $timeout = $::NOW + 15; 568 my $timeout = $::NOW + 60;
578 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 569 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
579 if ($timeout <= $::NOW) { 570 if ($timeout <= $::NOW) {
580 $self->block ($::BLOCKTIME, "segmented downloads are forbidden");
581 #$self->err_segmented_download; 571 $self->err_segmented_download;
582 } else { 572 } else {
583 $httpevent->wait; 573 $httpevent->wait;
584 } 574 }
585 } 575 }
586 } 576 }
602 592
603 if ($self->{method} eq "GET") { 593 if ($self->{method} eq "GET") {
604 $self->{time} = $::NOW; 594 $self->{time} = $::NOW;
605 $self->{written} = 0; 595 $self->{written} = 0;
606 596
607 open my $fh, "<", $self->{path} 597 my $fh = Coro::AIO::aio_open $self->{path}, Fcntl::O_RDONLY, 0
608 or die "$self->{path}: late open failure ($!)"; 598 or die "$self->{path}: late open failure ($!)";
609 599
610 $h -= $l - 1; 600 $h -= $l - 1;
611 601
612 my $transfer = $queue->start_transfer ($h); 602 my $transfer = $queue->start_transfer ($h);
613 my $locked; 603 my $locked;
614 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size 604 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
615 605
616 while ($h > 0) { 606 while ($h > 0) {
607 Coro::cede;
608 my $transfer_lock = $TRANSFER_LOCK->guard ($self->{remote_id});
609
617 unless ($locked) { 610 unless ($locked) {
618 if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { 611 if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) {
619 $bufsize = $::BUFSIZE; 612 $bufsize = $::BUFSIZE;
620 $self->{time} = $::NOW; 613 $self->{time} = $::NOW;
621 $self->{written} = 0; 614 $self->{written} = 0;
628 } 621 }
629 622
630 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 623 Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0
631 or last; 624 or last;
632 625
626 # readahead to work around rijk disk issues
627 IO::AIO::aio_readahead $fh, $l + $bufsize, $bufsize;
628
633 $tbf->request (length $buf); 629 $tbf->request (length $buf);
634 my $w = syswrite $self->{fh}, $buf 630 my $w = $self->{fh}->syswrite ($buf)
635 or last; 631 or last;
636 $::written += $w; 632 $::written += $w;
637 $self->{written} += $w; 633 $self->{written} += $w;
638 $l += $w; 634 $l += $w;
639 } 635 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines