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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines