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.43 by root, Wed Sep 12 20:29:43 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],
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 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 }
185 201
186 $res .= "Date: $HTTP_NOW\015\012"; 202 $res .= "Date: $HTTP_NOW\015\012";
187 203
188 while (my ($h, $v) = each %$hdr) { 204 while (my ($h, $v) = each %$hdr) {
189 $res .= "$h: $v\015\012" 205 $res .= "$h: $v\015\012"
292 308
293 delete $blocked{$id}; 309 delete $blocked{$id};
294 } 310 }
295 311
296 if (%{$conn{$id}} >= $::MAX_CONN_IP) { 312 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
297 my $delay = $::PER_TIMEOUT + 15; 313 my $delay = $::PER_TIMEOUT + $::NOW + 15;
298 while (%{$conn{$id}} >= $::MAX_CONN_IP) { 314 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
299 if ($delay <= 0) { 315 if ($delay < $::NOW) {
300 $self->slog(2, "blocked ip $id"); 316 $self->slog(2, "blocked ip $id");
301 $self->err_blocked; 317 $self->err_blocked;
302 } else { 318 } else {
303 Coro::Event::do_timer(after => 4); $delay -= 4; 319 $httpevent->wait;
304 } 320 }
305 } 321 }
306 } 322 }
307 323
308 # find out server name and port 324 # find out server name and port
334 350
335 $self->eoconn; 351 $self->eoconn;
336 352
337 die if $@ && !ref $@; 353 die if $@ && !ref $@;
338 354
339 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; 355 last if $self->{h}{connection} =~ /close/;
356
357 $httpevent->broadcast;
340 358
341 $fh->timeout($::PER_TIMEOUT); 359 $fh->timeout($::PER_TIMEOUT);
342 } 360 }
343} 361}
344 362
414 } else { 432 } else {
415 $ims < $self->{stat}[9] 433 $ims < $self->{stat}[9]
416 or $self->err(304, "not modified"); 434 or $self->err(304, "not modified");
417 435
418 if (-r "$path/index.html") { 436 if (-r "$path/index.html") {
419 $self->{path} .= "/index.html"; 437 # replace directory "size" by index.html filesize
438 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7];
420 $self->handle_file; 439 $self->handle_file;
421 } else { 440 } else {
422 $self->handle_dir; 441 $self->handle_dir;
423 } 442 }
424 } 443 }
470 $self->err(416, "not satisfiable", $hdr, ""); 489 $self->err(416, "not satisfiable", $hdr, "");
471 490
472satisfiable: 491satisfiable:
473 # check for segmented downloads 492 # check for segmented downloads
474 if ($l && $::NO_SEGMENTED) { 493 if ($l && $::NO_SEGMENTED) {
475 my $delay = $::PER_TIMEOUT + 15; 494 my $delay = $::NOW + $::PER_TIMEOUT + 15;
476 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 495 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
477 if ($delay <= 0) { 496 if ($delay <= $::NOW) {
478 $self->err_segmented_download; 497 $self->err_segmented_download;
479 } else { 498 } else {
480 Coro::Event::do_timer(after => 4); $delay -= 4; 499 $httpevent->wait;
481 } 500 }
482 } 501 }
483 } 502 }
484 503
485 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 504 $hdr->{"Content-Range"} = "bytes $l-$h/$length";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines