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.36 by root, Sun Sep 2 00:54:00 2001 UTC vs.
Revision 1.46 by root, Sat Nov 17 04:15:23 2001 UTC

1use Coro; 1use Coro;
2use Coro::Semaphore; 2use Coro::Semaphore;
3use Coro::Event; 3use Coro::Event;
4use Coro::Socket; 4use Coro::Socket;
5use Coro::Signal;
5 6
6use HTTP::Date; 7use HTTP::Date;
7 8
8no utf8; 9no utf8;
9use bytes; 10use bytes;
28 my $format = shift; 29 my $format = shift;
29 printf "---: $format\n", @_; 30 printf "---: $format\n", @_;
30} 31}
31 32
32our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 33our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
34our $httpevent = new Coro::Signal;
33 35
34our $wait_factor = 0.95; 36our $wait_factor = 0.95;
35 37
36our @transfers = ( 38our @transfers = (
37 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], 39 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1],
42my @pool; 44my @pool;
43 45
44# one "execution thread" 46# one "execution thread"
45sub handler { 47sub handler {
46 while () { 48 while () {
47 my $new = pop @newcons;
48 if ($new) { 49 if (@newcons) {
49 eval { 50 eval {
50 conn->new(@$new)->handle; 51 conn->new(@{pop @newcons})->handle;
51 }; 52 };
52 slog 1, "$@" if $@ && !ref $@; 53 slog 1, "$@" if $@ && !ref $@;
53 $connections->up; 54 $connections->up;
54 } else { 55 } else {
55 last if @pool >= $MAX_POOL; 56 last if @pool >= $MAX_POOL;
57 schedule; 58 schedule;
58 } 59 }
59 } 60 }
60} 61}
61 62
63sub listen_on {
64 my $listen = $_[0];
65
66 push @listen_sockets, $listen;
67
68 # the "main thread"
69 async {
70 slog 1, "accepting connections";
71 while () {
72 $connections->down;
73 push @newcons, [$listen->accept];
74 #slog 3, "accepted @$connections ".scalar(@pool);
75 if (@pool) {
76 (pop @pool)->ready;
77 } else {
78 async \&handler;
79 }
80
81 }
82 };
83}
84
62my $http_port = new Coro::Socket 85my $http_port = new Coro::Socket
63 LocalAddr => $SERVER_HOST, 86 LocalAddr => $SERVER_HOST,
64 LocalPort => $SERVER_PORT, 87 LocalPort => $SERVER_PORT,
65 ReuseAddr => 1, 88 ReuseAddr => 1,
66 Listen => 50, 89 Listen => 50,
67 or die "unable to start server"; 90 or die "unable to start server";
68 91
69push @listen_sockets, $http_port; 92listen_on $http_port;
93
94if ($SERVER_PORT2) {
95 my $http_port = new Coro::Socket
96 LocalAddr => $SERVER_HOST,
97 LocalPort => $SERVER_PORT2,
98 ReuseAddr => 1,
99 Listen => 50,
100 or die "unable to start server";
101
102 listen_on $http_port;
103}
70 104
71our $NOW; 105our $NOW;
72our $HTTP_NOW; 106our $HTTP_NOW;
73 107
74Event->timer(interval => 1, hard => 1, cb => sub { 108Event->timer(interval => 1, hard => 1, cb => sub {
75 $NOW = time; 109 $NOW = time;
76 $HTTP_NOW = time2str $NOW; 110 $HTTP_NOW = time2str $NOW;
77})->now; 111})->now;
78 112
79# the "main thread"
80async {
81 slog 1, "accepting connections";
82 while () {
83 $connections->down;
84 push @newcons, [$http_port->accept];
85 #slog 3, "accepted @$connections ".scalar(@pool);
86 if (@pool) {
87 (pop @pool)->ready;
88 } else {
89 async \&handler;
90 }
91
92 }
93};
94
95package conn; 113package conn;
96 114
97use Socket; 115use Socket;
98use HTTP::Date; 116use HTTP::Date;
99use Convert::Scalar 'weaken'; 117use Convert::Scalar 'weaken';
100use Linux::AIO; 118use Linux::AIO;
101 119
102Linux::AIO::min_parallel $::AIO_PARALLEL; 120Linux::AIO::min_parallel $::AIO_PARALLEL;
103
104my $aio_requests = new Coro::Semaphore $::AIO_PARALLEL * 4;
105 121
106Event->io(fd => Linux::AIO::poll_fileno, 122Event->io(fd => Linux::AIO::poll_fileno,
107 poll => 'r', async => 1, 123 poll => 'r', async => 1,
108 cb => \&Linux::AIO::poll_cb); 124 cb => \&Linux::AIO::poll_cb);
109 125
127 143
128read_mimetypes; 144read_mimetypes;
129 145
130sub new { 146sub new {
131 my $class = shift; 147 my $class = shift;
148 my $fh = shift;
132 my $peername = shift; 149 my $peername = shift;
133 my $fh = shift;
134 my $self = bless { fh => $fh }, $class; 150 my $self = bless { fh => $fh }, $class;
135 my (undef, $iaddr) = unpack_sockaddr_in $peername 151 my (undef, $iaddr) = unpack_sockaddr_in $peername
136 or $self->err(500, "unable to decode peername"); 152 or $self->err(500, "unable to decode peername");
137 153
138 $self->{remote_addr} = inet_ntoa $iaddr; 154 $self->{remote_addr} = inet_ntoa $iaddr;
143 $self; 159 $self;
144} 160}
145 161
146sub DESTROY { 162sub DESTROY {
147 my $self = shift; 163 my $self = shift;
148
149 $::conns--; 164 $::conns--;
150
151 $self->eoconn; 165 $self->eoconn;
152} 166}
153 167
154# end of connection 168# end of connection
155sub eoconn { 169sub eoconn {
156 my $self = shift; 170 my $self = shift;
157 171
158 # clean up hints 172 # clean up hints
159 delete $conn{$self->{remote_id}}{$self*1}; 173 delete $conn{$self->{remote_id}}{$self*1};
160 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; 174 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1};
175
176 $httpevent->broadcast;
161} 177}
162 178
163sub slog { 179sub slog {
164 my $self = shift; 180 my $self = shift;
165 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 181 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
167 183
168sub response { 184sub response {
169 my ($self, $code, $msg, $hdr, $content) = @_; 185 my ($self, $code, $msg, $hdr, $content) = @_;
170 my $res = "HTTP/1.1 $code $msg\015\012"; 186 my $res = "HTTP/1.1 $code $msg\015\012";
171 187
172 $self->{h}{connection} ||= $hdr->{Connection}; 188 if (exists $hdr->{Connection}) {
189 if ($hdr->{Connection} =~ /close/) {
190 $self->{h}{connection} = "close"
191 }
192 } else {
193 if ($self->{version} < 1.1) {
194 if ($self->{h}{connection} =~ /keep-alive/i) {
195 $hdr->{Connection} = "Keep-Alive";
196 } else {
197 $self->{h}{connection} = "close"
198 }
199 }
200 }
173 201
174 $res .= "Date: $HTTP_NOW\015\012"; 202 $res .= "Date: $HTTP_NOW\015\012";
175 203
176 while (my ($h, $v) = each %$hdr) { 204 while (my ($h, $v) = each %$hdr) {
177 $res .= "$h: $v\015\012" 205 $res .= "$h: $v\015\012"
280 308
281 delete $blocked{$id}; 309 delete $blocked{$id};
282 } 310 }
283 311
284 if (%{$conn{$id}} >= $::MAX_CONN_IP) { 312 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
285 my $delay = $::PER_TIMEOUT + 15; 313 my $delay = $::PER_TIMEOUT + $::NOW + 15;
286 while (%{$conn{$id}} >= $::MAX_CONN_IP) { 314 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
287 if ($delay <= 0) { 315 if ($delay < $::NOW) {
288 $self->slog(2, "blocked ip $id"); 316 $self->slog(2, "blocked ip $id");
289 $self->err_blocked; 317 $self->err_blocked;
290 } else { 318 } else {
291 Coro::Event::do_timer(after => 4); $delay -= 4; 319 $httpevent->wait;
292 } 320 }
293 } 321 }
294 } 322 }
295 323
296 # find out server name and port 324 # find out server name and port
302 330
303 if (defined $host) { 331 if (defined $host) {
304 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; 332 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80;
305 } else { 333 } else {
306 ($self->{server_port}, $host) 334 ($self->{server_port}, $host)
307 = unpack_sockaddr_in $self->{fh}->getsockname 335 = unpack_sockaddr_in $self->{fh}->sockname
308 or $self->err(500, "unable to get socket name"); 336 or $self->err(500, "unable to get socket name");
309 $host = inet_ntoa $host; 337 $host = inet_ntoa $host;
310 } 338 }
311 339
312 $self->{server_name} = $host; 340 $self->{server_name} = $host;
322 350
323 $self->eoconn; 351 $self->eoconn;
324 352
325 die if $@ && !ref $@; 353 die if $@ && !ref $@;
326 354
327 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; 355 last if $self->{h}{connection} =~ /close/;
356
357 $httpevent->broadcast;
328 358
329 $fh->timeout($::PER_TIMEOUT); 359 $fh->timeout($::PER_TIMEOUT);
330 } 360 }
331} 361}
332 362
402 } else { 432 } else {
403 $ims < $self->{stat}[9] 433 $ims < $self->{stat}[9]
404 or $self->err(304, "not modified"); 434 or $self->err(304, "not modified");
405 435
406 if (-r "$path/index.html") { 436 if (-r "$path/index.html") {
407 $self->{path} .= "/index.html"; 437 # replace directory "size" by index.html filesize
438 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7];
408 $self->handle_file; 439 $self->handle_file;
409 } else { 440 } else {
410 $self->handle_dir; 441 $self->handle_dir;
411 } 442 }
412 } 443 }
458 $self->err(416, "not satisfiable", $hdr, ""); 489 $self->err(416, "not satisfiable", $hdr, "");
459 490
460satisfiable: 491satisfiable:
461 # check for segmented downloads 492 # check for segmented downloads
462 if ($l && $::NO_SEGMENTED) { 493 if ($l && $::NO_SEGMENTED) {
463 my $delay = $::PER_TIMEOUT + 15; 494 my $delay = $::NOW + $::PER_TIMEOUT + 15;
464 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 495 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
465 if ($delay <= 0) { 496 if ($delay <= $::NOW) {
466 $self->err_segmented_download; 497 $self->err_segmented_download;
467 } else { 498 } else {
468 Coro::Event::do_timer(after => 4); $delay -= 4; 499 $httpevent->wait;
469 } 500 }
470 } 501 }
471 } 502 }
472 503
473 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 504 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
519 while ($h > 0) { 550 while ($h > 0) {
520 if (0) { 551 if (0) {
521 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h 552 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
522 or last; 553 or last;
523 } else { 554 } else {
524 undef $buf;
525 $aio_requests->down;
526 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 555 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
527 $buf, 0, sub { 556 $buf, 0, sub {
528 $r = $_[0]; 557 $r = $_[0];
529 $current->ready; 558 Coro::ready($current);
530 }); 559 });
531 &Coro::schedule; 560 &Coro::schedule;
532 $aio_requests->up;
533 last unless $r; 561 last unless $r;
534 } 562 }
535 my $w = $self->{fh}->syswrite($buf) 563 my $w = syswrite $self->{fh}, $buf
536 or last; 564 or last;
537 $::written += $w; 565 $::written += $w;
538 $self->{written} += $w; 566 $self->{written} += $w;
539 $l += $r; 567 $l += $r;
540 } 568 }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines