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.31 by root, Mon Aug 27 05:05:26 2001 UTC vs.
Revision 1.32 by root, Wed Aug 29 01:32:50 2001 UTC

1use Coro; 1use Coro;
2use Coro::Semaphore; 2use Coro::Semaphore;
3use Coro::Event; 3use Coro::Event;
4use Coro::Socket; 4use Coro::Socket;
5 5
6use HTTP::Date;
7
6no utf8; 8no utf8;
7use bytes; 9use bytes;
10
11our @wait_time = (0); # used to calculcate avg. waiting time
8 12
9# at least on my machine, this thingy serves files 13# at least on my machine, this thingy serves files
10# quite a bit faster than apache, ;) 14# quite a bit faster than apache, ;)
11# and quite a bit slower than thttpd :( 15# and quite a bit slower than thttpd :(
12 16
25 my $level = shift; 29 my $level = shift;
26 my $format = shift; 30 my $format = shift;
27 printf "---: $format\n", @_; 31 printf "---: $format\n", @_;
28} 32}
29 33
30my $connections = new Coro::Semaphore $MAX_CONNECTS; 34our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
35our $transfers = new Coro::Semaphore $MAX_TRANSFER || 50;
31 36
32my @newcons; 37my @newcons;
33my @pool; 38my @pool;
34 39
35# one "execution thread" 40# one "execution thread"
57 Listen => 50, 62 Listen => 50,
58 or die "unable to start server"; 63 or die "unable to start server";
59 64
60push @listen_sockets, $http_port; 65push @listen_sockets, $http_port;
61 66
67our $NOW;
68our $HTTP_NOW;
69
70Event->timer(interval => 1, hard => 1, cb => sub {
71 $NOW = time;
72 $HTTP_NOW = time2str $NOW;
73});
74
62# the "main thread" 75# the "main thread"
63async { 76async {
64 slog 1, "accepting connections"; 77 slog 1, "accepting connections";
65 while () { 78 while () {
66 $connections->down; 79 $connections->down;
67 push @newcons, [$http_port->accept]; 80 push @newcons, [$http_port->accept];
68 #slog 3, "accepted @$connections ".scalar(@pool); 81 #slog 3, "accepted @$connections ".scalar(@pool);
69 $::NOW = time;
70 if (@pool) { 82 if (@pool) {
71 (pop @pool)->ready; 83 (pop @pool)->ready;
72 } else { 84 } else {
73 async \&handler; 85 async \&handler;
74 } 86 }
154 my ($self, $code, $msg, $hdr, $content) = @_; 166 my ($self, $code, $msg, $hdr, $content) = @_;
155 my $res = "HTTP/1.1 $code $msg\015\012"; 167 my $res = "HTTP/1.1 $code $msg\015\012";
156 168
157 $self->{h}{connection} ||= $hdr->{Connection}; 169 $self->{h}{connection} ||= $hdr->{Connection};
158 170
159 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 171 $res .= "Date: $HTTP_NOW\015\012";
160 172
161 while (my ($h, $v) = each %$hdr) { 173 while (my ($h, $v) = each %$hdr) {
162 $res .= "$h: $v\015\012" 174 $res .= "$h: $v\015\012"
163 } 175 }
164 $res .= "\015\012"; 176 $res .= "\015\012";
467 $hdr->{"Content-Length"} = $length; 479 $hdr->{"Content-Length"} = $length;
468 480
469 $self->response(@code, $hdr, ""); 481 $self->response(@code, $hdr, "");
470 482
471 if ($self->{method} eq "GET") { 483 if ($self->{method} eq "GET") {
484 $self->{time} = $::NOW;
485
486 my $transfer = $::transfers->guard;
487 $self->{fh}->writable or return;
488
489 push @::wait_time, $::NOW - $self->{time};
490 shift @::wait_time if @wait_time > 25;
491 $self->{time} = $::NOW;
492
472 my ($fh, $buf, $r); 493 my ($fh, $buf, $r);
473 my $current = $Coro::current; 494 my $current = $Coro::current;
474 open $fh, "<", $self->{path} 495 open $fh, "<", $self->{path}
475 or die "$self->{path}: late open failure ($!)"; 496 or die "$self->{path}: late open failure ($!)";
476 497
502 or last; 523 or last;
503 $::written += $w; 524 $::written += $w;
504 $self->{written} += $w; 525 $self->{written} += $w;
505 $l += $r; 526 $l += $r;
506 } 527 }
507 }
508 528
509 close $fh; 529 close $fh;
530 }
510} 531}
511 532
5121; 5331;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines