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.5 by root, Fri Aug 10 15:58:04 2001 UTC vs.
Revision 1.10 by root, Sat Aug 11 16:34:47 2001 UTC

18 printf "---: $format\n", @_; 18 printf "---: $format\n", @_;
19} 19}
20 20
21my $connections = new Coro::Semaphore $MAX_CONNECTS; 21my $connections = new Coro::Semaphore $MAX_CONNECTS;
22 22
23my @fh; 23my @newcons;
24my @pool; 24my @pool;
25 25
26# one "execution thread" 26# one "execution thread"
27sub handler { 27sub handler {
28 while () { 28 while () {
29 my $fh = pop @fh; 29 my $new = pop @newcons;
30 if ($fh) { 30 if ($new) {
31 eval { 31 eval {
32 conn->new($fh)->handle; 32 conn->new(@$new)->handle;
33 }; 33 };
34 close $fh;
35 slog 1, "$@" if $@ && !ref $@; 34 slog 1, "$@" if $@ && !ref $@;
36 $connections->up; 35 $connections->up;
37 } else { 36 } else {
38 last if @pool >= $MAX_POOL; 37 last if @pool >= $MAX_POOL;
39 push @pool, $Coro::current; 38 push @pool, $Coro::current;
54# the "main thread" 53# the "main thread"
55async { 54async {
56 slog 1, "accepting connections"; 55 slog 1, "accepting connections";
57 while () { 56 while () {
58 $connections->down; 57 $connections->down;
59 push @fh, $http_port->accept; 58 push @newcons, [$http_port->accept];
60 #slog 3, "accepted @$connections ".scalar(@pool); 59 #slog 3, "accepted @$connections ".scalar(@pool);
61 $::NOW = time; 60 $::NOW = time;
62 if (@pool) { 61 if (@pool) {
63 (pop @pool)->ready; 62 (pop @pool)->ready;
64 } else { 63 } else {
74use HTTP::Date; 73use HTTP::Date;
75use Convert::Scalar 'weaken'; 74use Convert::Scalar 'weaken';
76 75
77our %conn; # $conn{ip}{fh} => connobj 76our %conn; # $conn{ip}{fh} => connobj
78our %blocked; 77our %blocked;
78our %mimetype;
79
80sub read_mimetypes {
81 local *M;
82 if (open M, "<mime_types") {
83 while (<M>) {
84 if (/^([^#]\S+)\t+(\S+)$/) {
85 $mimetype{lc $1} = $2;
86 }
87 }
88 } else {
89 print "cannot open mime_types\n";
90 }
91}
92
93read_mimetypes;
79 94
80sub new { 95sub new {
81 my $class = shift; 96 my $class = shift;
97 my $peername = shift;
82 my $fh = shift; 98 my $fh = shift;
83 my $self = bless { fh => $fh }, $class; 99 my $self = bless { fh => $fh }, $class;
84 my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername 100 my (undef, $iaddr) = unpack_sockaddr_in $peername
85 or $self->err(500, "unable to get peername"); 101 or $self->err(500, "unable to decode peername");
102
86 $self->{remote_addr} = inet_ntoa $iaddr; 103 $self->{remote_addr} = inet_ntoa $iaddr;
87 104
88 # enter ourselves into various lists 105 # enter ourselves into various lists
89 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 106 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
90 107
110 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 127 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
111 128
112 while (my ($h, $v) = each %$hdr) { 129 while (my ($h, $v) = each %$hdr) {
113 $res .= "$h: $v\015\012" 130 $res .= "$h: $v\015\012"
114 } 131 }
132 $res .= "\015\012";
115 133
116 $res .= "\015\012$content" if defined $content; 134 $res .= $content if defined $content and $self->{method} eq "GET";
117 135
118 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 136 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d#
119 137
120 print {$self->{fh}} $res; 138 print {$self->{fh}} $res;
121} 139}
137 155
138sub err_blocked { 156sub err_blocked {
139 my $self = shift; 157 my $self = shift;
140 my $ip = $self->{remote_addr}; 158 my $ip = $self->{remote_addr};
141 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 159 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
160
161 Coro::Event::do_timer(after => 5);
162
142 $self->err(403, "too many connections", 163 $self->err(403, "too many connections",
143 { 164 {
144 "Content-Type" => "text/html", 165 "Content-Type" => "text/html",
145 "Retry-After" => $::BLOCKTIME 166 "Retry-After" => $::BLOCKTIME
146 }, 167 },
244 265
245 $self->{name} = $uri; 266 $self->{name} = $uri;
246 267
247 # now do the path mapping 268 # now do the path mapping
248 $self->{path} = "$::DOCROOT/$host$uri"; 269 $self->{path} = "$::DOCROOT/$host$uri";
270
271 $self->access_check;
249} 272}
250 273
251sub server_address { 274sub server_address {
252 my $self = shift; 275 my $self = shift;
253 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname 276 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname
287 if (chdir $::DOCROOT) { 310 if (chdir $::DOCROOT) {
288 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 311 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
289 $ENV{HTTP_HOST} = $self->server_host; 312 $ENV{HTTP_HOST} = $self->server_host;
290 $ENV{HTTP_PORT} = $self->{server_host}; 313 $ENV{HTTP_PORT} = $self->{server_host};
291 $ENV{SCRIPT_NAME} = $self->{name}; 314 $ENV{SCRIPT_NAME} = $self->{name};
292 exec $::INDEXPROG; 315 exec $path;
293 } 316 }
294 Coro::State::_exit(0); 317 Coro::State::_exit(0);
295 } else { 318 } else {
296 } 319 }
297} 320}
300 my $self = shift; 323 my $self = shift;
301 my $path = $self->{path}; 324 my $path = $self->{path};
302 325
303 stat $path 326 stat $path
304 or $self->err(404, "not found"); 327 or $self->err(404, "not found");
328
329 $self->{stat} = [stat _];
305 330
306 # idiotic netscape sends idiotic headers AGAIN 331 # idiotic netscape sends idiotic headers AGAIN
307 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 332 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
308 ? str2time $1 : 0; 333 ? str2time $1 : 0;
309 334
312 if ($path !~ /\/$/) { 337 if ($path !~ /\/$/) {
313 # create a redirect to get the trailing "/" 338 # create a redirect to get the trailing "/"
314 my $host = $self->server_hostport; 339 my $host = $self->server_hostport;
315 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 340 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
316 } else { 341 } else {
317 $ims < (stat _)[9] 342 $ims < $self->{stat}[9]
318 or $self->err(304, "not modified"); 343 or $self->err(304, "not modified");
319 344
320 if ($self->{method} eq "GET") { 345 if ($self->{method} eq "GET") {
321 if (-r "$path/index.html") { 346 if (-r "$path/index.html") {
322 $self->{path} .= "/index.html"; 347 $self->{path} .= "/index.html";
334 } 359 }
335} 360}
336 361
337sub handle_dir { 362sub handle_dir {
338 my $self = shift; 363 my $self = shift;
339 $self->_cgi($::INDEXPROG); 364 my $idx = $self->diridx;
365
366 $self->response(200, "ok",
367 {
368 "Content-Type" => "text/html",
369 "Content-Length" => length $idx,
370 },
371 $idx);
340} 372}
341 373
342sub handle_file { 374sub handle_file {
343 my $self = shift; 375 my $self = shift;
344 my $length = -s _; 376 my $length = -s _;
364 $hdr->{"Content-Range"} = "bytes */$length"; 396 $hdr->{"Content-Range"} = "bytes */$length";
365 $self->err(416, "not satisfiable", $hdr); 397 $self->err(416, "not satisfiable", $hdr);
366 398
367satisfiable: 399satisfiable:
368 # check for segmented downloads 400 # check for segmented downloads
369 if ($l && $NO_SEGMENTED) { 401 if ($l && $::NO_SEGMENTED) {
370 if (%{$uri{$self->{uri}}} > 1) { 402 if (%{$uri{$self->{uri}}} > 1) {
371 $self->slog("segmented download refused\n"); 403 $self->slog("segmented download refused\n");
372 $self->err(400, "segmented downloads are not allowed"); 404 $self->err(400, "segmented downloads are not allowed");
373 } 405 }
374 } 406 }
380ignore: 412ignore:
381 } else { 413 } else {
382 ($l, $h) = (0, $length - 1); 414 ($l, $h) = (0, $length - 1);
383 } 415 }
384 416
385 if ($self->{path} =~ /\.html$/) { 417 $self->{path} =~ /\.([^.]+)$/;
386 $hdr->{"Content-Type"} = "text/html";
387 } else {
388 $hdr->{"Content-Type"} = "application/octet-stream"; 418 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
389 }
390
391 $hdr->{"Content-Length"} = $length; 419 $hdr->{"Content-Length"} = $length;
392 420
393 $self->response(@code, $hdr, ""); 421 $self->response(@code, $hdr, "");
394 422
395 if ($self->{method} eq "GET") { 423 if ($self->{method} eq "GET") {

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines