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.43 by root, Wed Sep 12 20:29:43 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],
168 my $self = shift; 170 my $self = shift;
169 171
170 # clean up hints 172 # clean up hints
171 delete $conn{$self->{remote_id}}{$self*1}; 173 delete $conn{$self->{remote_id}}{$self*1};
172 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; 174 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1};
175
176 $httpevent->broadcast;
173} 177}
174 178
175sub slog { 179sub slog {
176 my $self = shift; 180 my $self = shift;
177 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 181 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
179 183
180sub response { 184sub response {
181 my ($self, $code, $msg, $hdr, $content) = @_; 185 my ($self, $code, $msg, $hdr, $content) = @_;
182 my $res = "HTTP/1.1 $code $msg\015\012"; 186 my $res = "HTTP/1.1 $code $msg\015\012";
183 187
184 $self->{h}{connection} = "close" if $hdr->{Connection} =~ /close/; 188 $self->{h}{connection} = "close"
189 if exists $hdr->{Connection} # to avoid "empty" header lines due to vivification
190 and $hdr->{Connection} =~ /close/;
185 191
186 $res .= "Date: $HTTP_NOW\015\012"; 192 $res .= "Date: $HTTP_NOW\015\012";
187 193
188 while (my ($h, $v) = each %$hdr) { 194 while (my ($h, $v) = each %$hdr) {
189 $res .= "$h: $v\015\012" 195 $res .= "$h: $v\015\012"
292 298
293 delete $blocked{$id}; 299 delete $blocked{$id};
294 } 300 }
295 301
296 if (%{$conn{$id}} >= $::MAX_CONN_IP) { 302 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
297 my $delay = $::PER_TIMEOUT + 15; 303 my $delay = $::PER_TIMEOUT + $::NOW + 15;
298 while (%{$conn{$id}} >= $::MAX_CONN_IP) { 304 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
299 if ($delay <= 0) { 305 if ($delay < $::NOW) {
300 $self->slog(2, "blocked ip $id"); 306 $self->slog(2, "blocked ip $id");
301 $self->err_blocked; 307 $self->err_blocked;
302 } else { 308 } else {
303 Coro::Event::do_timer(after => 4); $delay -= 4; 309 $httpevent->wait;
304 } 310 }
305 } 311 }
306 } 312 }
307 313
308 # find out server name and port 314 # find out server name and port
335 $self->eoconn; 341 $self->eoconn;
336 342
337 die if $@ && !ref $@; 343 die if $@ && !ref $@;
338 344
339 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;
340 348
341 $fh->timeout($::PER_TIMEOUT); 349 $fh->timeout($::PER_TIMEOUT);
342 } 350 }
343} 351}
344 352
414 } else { 422 } else {
415 $ims < $self->{stat}[9] 423 $ims < $self->{stat}[9]
416 or $self->err(304, "not modified"); 424 or $self->err(304, "not modified");
417 425
418 if (-r "$path/index.html") { 426 if (-r "$path/index.html") {
419 $self->{path} .= "/index.html"; 427 # replace directory "size" by index.html filesize
428 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7];
420 $self->handle_file; 429 $self->handle_file;
421 } else { 430 } else {
422 $self->handle_dir; 431 $self->handle_dir;
423 } 432 }
424 } 433 }
470 $self->err(416, "not satisfiable", $hdr, ""); 479 $self->err(416, "not satisfiable", $hdr, "");
471 480
472satisfiable: 481satisfiable:
473 # check for segmented downloads 482 # check for segmented downloads
474 if ($l && $::NO_SEGMENTED) { 483 if ($l && $::NO_SEGMENTED) {
475 my $delay = $::PER_TIMEOUT + 15; 484 my $delay = $::NOW + $::PER_TIMEOUT + 15;
476 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 485 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
477 if ($delay <= 0) { 486 if ($delay <= $::NOW) {
478 $self->err_segmented_download; 487 $self->err_segmented_download;
479 } else { 488 } else {
480 Coro::Event::do_timer(after => 4); $delay -= 4; 489 $httpevent->broadcast;
481 } 490 }
482 } 491 }
483 } 492 }
484 493
485 $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