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.40 by root, Mon Sep 3 00:06:06 2001 UTC vs.
Revision 1.45 by root, Sun Nov 11 03:32:19 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],
87 Listen => 50, 89 Listen => 50,
88 or die "unable to start server"; 90 or die "unable to start server";
89 91
90listen_on $http_port; 92listen_on $http_port;
91 93
94if ($SERVER_PORT2) {
92my $http_port = new Coro::Socket 95 my $http_port = new Coro::Socket
93 LocalAddr => $SERVER_HOST, 96 LocalAddr => $SERVER_HOST,
94 LocalPort => $SERVER_PORT2, 97 LocalPort => $SERVER_PORT2,
95 ReuseAddr => 1, 98 ReuseAddr => 1,
96 Listen => 50, 99 Listen => 50,
97 or die "unable to start server"; 100 or die "unable to start server";
98 101
99listen_on $http_port; 102 listen_on $http_port;
103}
100 104
101our $NOW; 105our $NOW;
102our $HTTP_NOW; 106our $HTTP_NOW;
103 107
104Event->timer(interval => 1, hard => 1, cb => sub { 108Event->timer(interval => 1, hard => 1, cb => sub {
139 143
140read_mimetypes; 144read_mimetypes;
141 145
142sub new { 146sub new {
143 my $class = shift; 147 my $class = shift;
148 my $fh = shift;
144 my $peername = shift; 149 my $peername = shift;
145 my $fh = shift;
146 my $self = bless { fh => $fh }, $class; 150 my $self = bless { fh => $fh }, $class;
147 my (undef, $iaddr) = unpack_sockaddr_in $peername 151 my (undef, $iaddr) = unpack_sockaddr_in $peername
148 or $self->err(500, "unable to decode peername"); 152 or $self->err(500, "unable to decode peername");
149 153
150 $self->{remote_addr} = inet_ntoa $iaddr; 154 $self->{remote_addr} = inet_ntoa $iaddr;
166 my $self = shift; 170 my $self = shift;
167 171
168 # clean up hints 172 # clean up hints
169 delete $conn{$self->{remote_id}}{$self*1}; 173 delete $conn{$self->{remote_id}}{$self*1};
170 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; 174 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1};
175
176 $httpevent->broadcast;
171} 177}
172 178
173sub slog { 179sub slog {
174 my $self = shift; 180 my $self = shift;
175 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 181 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
177 183
178sub response { 184sub response {
179 my ($self, $code, $msg, $hdr, $content) = @_; 185 my ($self, $code, $msg, $hdr, $content) = @_;
180 my $res = "HTTP/1.1 $code $msg\015\012"; 186 my $res = "HTTP/1.1 $code $msg\015\012";
181 187
182 $self->{h}{connection} ||= $hdr->{Connection}; 188 $self->{h}{connection} = "close"
189 if exists $hdr->{Connection} # to avoid "empty" header lines due to vivification
190 and $hdr->{Connection} =~ /close/;
183 191
184 $res .= "Date: $HTTP_NOW\015\012"; 192 $res .= "Date: $HTTP_NOW\015\012";
185 193
186 while (my ($h, $v) = each %$hdr) { 194 while (my ($h, $v) = each %$hdr) {
187 $res .= "$h: $v\015\012" 195 $res .= "$h: $v\015\012"
290 298
291 delete $blocked{$id}; 299 delete $blocked{$id};
292 } 300 }
293 301
294 if (%{$conn{$id}} >= $::MAX_CONN_IP) { 302 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
295 my $delay = $::PER_TIMEOUT + 15; 303 my $delay = $::PER_TIMEOUT + $::NOW + 15;
296 while (%{$conn{$id}} >= $::MAX_CONN_IP) { 304 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
297 if ($delay <= 0) { 305 if ($delay < $::NOW) {
298 $self->slog(2, "blocked ip $id"); 306 $self->slog(2, "blocked ip $id");
299 $self->err_blocked; 307 $self->err_blocked;
300 } else { 308 } else {
301 Coro::Event::do_timer(after => 4); $delay -= 4; 309 $httpevent->wait;
302 } 310 }
303 } 311 }
304 } 312 }
305 313
306 # find out server name and port 314 # find out server name and port
312 320
313 if (defined $host) { 321 if (defined $host) {
314 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; 322 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80;
315 } else { 323 } else {
316 ($self->{server_port}, $host) 324 ($self->{server_port}, $host)
317 = unpack_sockaddr_in $self->{fh}->getsockname 325 = unpack_sockaddr_in $self->{fh}->sockname
318 or $self->err(500, "unable to get socket name"); 326 or $self->err(500, "unable to get socket name");
319 $host = inet_ntoa $host; 327 $host = inet_ntoa $host;
320 } 328 }
321 329
322 $self->{server_name} = $host; 330 $self->{server_name} = $host;
333 $self->eoconn; 341 $self->eoconn;
334 342
335 die if $@ && !ref $@; 343 die if $@ && !ref $@;
336 344
337 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; 345 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1;
346
347 $httpevent->broadcast;
338 348
339 $fh->timeout($::PER_TIMEOUT); 349 $fh->timeout($::PER_TIMEOUT);
340 } 350 }
341} 351}
342 352
412 } else { 422 } else {
413 $ims < $self->{stat}[9] 423 $ims < $self->{stat}[9]
414 or $self->err(304, "not modified"); 424 or $self->err(304, "not modified");
415 425
416 if (-r "$path/index.html") { 426 if (-r "$path/index.html") {
417 $self->{path} .= "/index.html"; 427 # replace directory "size" by index.html filesize
428 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7];
418 $self->handle_file; 429 $self->handle_file;
419 } else { 430 } else {
420 $self->handle_dir; 431 $self->handle_dir;
421 } 432 }
422 } 433 }
468 $self->err(416, "not satisfiable", $hdr, ""); 479 $self->err(416, "not satisfiable", $hdr, "");
469 480
470satisfiable: 481satisfiable:
471 # check for segmented downloads 482 # check for segmented downloads
472 if ($l && $::NO_SEGMENTED) { 483 if ($l && $::NO_SEGMENTED) {
473 my $delay = $::PER_TIMEOUT + 15; 484 my $delay = $::NOW + $::PER_TIMEOUT + 15;
474 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 485 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
475 if ($delay <= 0) { 486 if ($delay <= $::NOW) {
476 $self->err_segmented_download; 487 $self->err_segmented_download;
477 } else { 488 } else {
478 Coro::Event::do_timer(after => 4); $delay -= 4; 489 $httpevent->broadcast;
479 } 490 }
480 } 491 }
481 } 492 }
482 493
483 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 494 $hdr->{"Content-Range"} = "bytes $l-$h/$length";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines