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.41 by root, Mon Sep 10 22:16:20 2001 UTC vs.
Revision 1.47 by root, Tue Nov 20 01:56:21 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;
8use POSIX ();
7 9
8no utf8; 10no utf8;
9use bytes; 11use bytes;
10 12
11# at least on my machine, this thingy serves files 13# at least on my machine, this thingy serves files
28 my $format = shift; 30 my $format = shift;
29 printf "---: $format\n", @_; 31 printf "---: $format\n", @_;
30} 32}
31 33
32our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; 34our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
35our $httpevent = new Coro::Signal;
33 36
34our $wait_factor = 0.95; 37our $wait_factor = 0.95;
35 38
36our @transfers = ( 39our @transfers = (
37 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1], 40 [(new Coro::Semaphore $MAX_TRANSFERS_SMALL || 50), 1],
141 144
142read_mimetypes; 145read_mimetypes;
143 146
144sub new { 147sub new {
145 my $class = shift; 148 my $class = shift;
149 my $fh = shift;
146 my $peername = shift; 150 my $peername = shift;
147 my $fh = shift;
148 my $self = bless { fh => $fh }, $class; 151 my $self = bless { fh => $fh }, $class;
149 my (undef, $iaddr) = unpack_sockaddr_in $peername 152 my (undef, $iaddr) = unpack_sockaddr_in $peername
150 or $self->err(500, "unable to decode peername"); 153 or $self->err(500, "unable to decode peername");
151 154
152 $self->{remote_addr} = inet_ntoa $iaddr; 155 $self->{remote_addr} = inet_ntoa $iaddr;
168 my $self = shift; 171 my $self = shift;
169 172
170 # clean up hints 173 # clean up hints
171 delete $conn{$self->{remote_id}}{$self*1}; 174 delete $conn{$self->{remote_id}}{$self*1};
172 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1}; 175 delete $uri{$self->{remote_id}}{$self->{uri}}{$self*1};
176
177 $httpevent->broadcast;
173} 178}
174 179
175sub slog { 180sub slog {
176 my $self = shift; 181 my $self = shift;
177 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]"); 182 main::slog($_[0], ($self->{remote_id} || $self->{remote_addr}) ."> $_[1]");
179 184
180sub response { 185sub response {
181 my ($self, $code, $msg, $hdr, $content) = @_; 186 my ($self, $code, $msg, $hdr, $content) = @_;
182 my $res = "HTTP/1.1 $code $msg\015\012"; 187 my $res = "HTTP/1.1 $code $msg\015\012";
183 188
184 $self->{h}{connection} ||= $hdr->{Connection}; 189 if (exists $hdr->{Connection}) {
190 if ($hdr->{Connection} =~ /close/) {
191 $self->{h}{connection} = "close"
192 }
193 } else {
194 if ($self->{version} < 1.1) {
195 if ($self->{h}{connection} =~ /keep-alive/i) {
196 $hdr->{Connection} = "Keep-Alive";
197 } else {
198 $self->{h}{connection} = "close"
199 }
200 }
201 }
185 202
186 $res .= "Date: $HTTP_NOW\015\012"; 203 $res .= "Date: $HTTP_NOW\015\012";
187 204
188 while (my ($h, $v) = each %$hdr) { 205 while (my ($h, $v) = each %$hdr) {
189 $res .= "$h: $v\015\012" 206 $res .= "$h: $v\015\012"
190 } 207 }
191 $res .= "\015\012"; 208 $res .= "\015\012";
192 209
193 $res .= $content if defined $content and $self->{method} ne "HEAD"; 210 $res .= $content if defined $content and $self->{method} ne "HEAD";
194 211
212 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $NOW).
195 my $log = "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n"; 213 " $self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";
196 214
197 print $accesslog $log if $accesslog; 215 print $accesslog $log if $accesslog;
198 print STDERR $log; 216 print STDERR $log;
199 217
200 $self->{written} += 218 $self->{written} +=
292 310
293 delete $blocked{$id}; 311 delete $blocked{$id};
294 } 312 }
295 313
296 if (%{$conn{$id}} >= $::MAX_CONN_IP) { 314 if (%{$conn{$id}} >= $::MAX_CONN_IP) {
297 my $delay = $::PER_TIMEOUT + 15; 315 my $delay = $::PER_TIMEOUT + $::NOW + 15;
298 while (%{$conn{$id}} >= $::MAX_CONN_IP) { 316 while (%{$conn{$id}} >= $::MAX_CONN_IP) {
299 if ($delay <= 0) { 317 if ($delay < $::NOW) {
300 $self->slog(2, "blocked ip $id"); 318 $self->slog(2, "blocked ip $id");
301 $self->err_blocked; 319 $self->err_blocked;
302 } else { 320 } else {
303 Coro::Event::do_timer(after => 4); $delay -= 4; 321 $httpevent->wait;
304 } 322 }
305 } 323 }
306 } 324 }
307 325
308 # find out server name and port 326 # find out server name and port
314 332
315 if (defined $host) { 333 if (defined $host) {
316 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80; 334 $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80;
317 } else { 335 } else {
318 ($self->{server_port}, $host) 336 ($self->{server_port}, $host)
319 = unpack_sockaddr_in $self->{fh}->getsockname 337 = unpack_sockaddr_in $self->{fh}->sockname
320 or $self->err(500, "unable to get socket name"); 338 or $self->err(500, "unable to get socket name");
321 $host = inet_ntoa $host; 339 $host = inet_ntoa $host;
322 } 340 }
323 341
324 $self->{server_name} = $host; 342 $self->{server_name} = $host;
334 352
335 $self->eoconn; 353 $self->eoconn;
336 354
337 die if $@ && !ref $@; 355 die if $@ && !ref $@;
338 356
339 last if $self->{h}{connection} =~ /close/ || $self->{version} < 1.1; 357 last if $self->{h}{connection} =~ /close/;
358
359 $httpevent->broadcast;
340 360
341 $fh->timeout($::PER_TIMEOUT); 361 $fh->timeout($::PER_TIMEOUT);
342 } 362 }
343} 363}
344 364
414 } else { 434 } else {
415 $ims < $self->{stat}[9] 435 $ims < $self->{stat}[9]
416 or $self->err(304, "not modified"); 436 or $self->err(304, "not modified");
417 437
418 if (-r "$path/index.html") { 438 if (-r "$path/index.html") {
419 $self->{path} .= "/index.html"; 439 # replace directory "size" by index.html filesize
440 $self->{stat}[7] = (stat ($self->{path} .= "/index.html"))[7];
420 $self->handle_file; 441 $self->handle_file;
421 } else { 442 } else {
422 $self->handle_dir; 443 $self->handle_dir;
423 } 444 }
424 } 445 }
436 457
437 $self->response(200, "ok", 458 $self->response(200, "ok",
438 { 459 {
439 "Content-Type" => "text/html", 460 "Content-Type" => "text/html",
440 "Content-Length" => length $idx, 461 "Content-Length" => length $idx,
462 "Last-Modified" => time2str ((stat _)[9]),
441 }, 463 },
442 $idx); 464 $idx);
443} 465}
444 466
445sub handle_file { 467sub handle_file {
470 $self->err(416, "not satisfiable", $hdr, ""); 492 $self->err(416, "not satisfiable", $hdr, "");
471 493
472satisfiable: 494satisfiable:
473 # check for segmented downloads 495 # check for segmented downloads
474 if ($l && $::NO_SEGMENTED) { 496 if ($l && $::NO_SEGMENTED) {
475 my $delay = $::PER_TIMEOUT + 15; 497 my $delay = $::NOW + $::PER_TIMEOUT + 15;
476 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { 498 while (%{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
477 if ($delay <= 0) { 499 if ($delay <= $::NOW) {
478 $self->err_segmented_download; 500 $self->err_segmented_download;
479 } else { 501 } else {
480 Coro::Event::do_timer(after => 4); $delay -= 4; 502 $httpevent->wait;
481 } 503 }
482 } 504 }
483 } 505 }
484 506
485 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 507 $hdr->{"Content-Range"} = "bytes $l-$h/$length";

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines