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.6 by root, Fri Aug 10 21:03:41 2001 UTC vs.
Revision 1.11 by root, Sat Aug 11 19:59:20 2001 UTC

73use HTTP::Date; 73use HTTP::Date;
74use Convert::Scalar 'weaken'; 74use Convert::Scalar 'weaken';
75 75
76our %conn; # $conn{ip}{fh} => connobj 76our %conn; # $conn{ip}{fh} => connobj
77our %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;
78 94
79sub new { 95sub new {
80 my $class = shift; 96 my $class = shift;
81 my $peername = 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 $peername 100 my (undef, $iaddr) = unpack_sockaddr_in $peername
85 or $self->err(500, "unable to decode 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;
104 $self->{time} = $::NOW;
87 105
88 # enter ourselves into various lists 106 # enter ourselves into various lists
89 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 107 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
90 108
91 $self; 109 $self;
110 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 128 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
111 129
112 while (my ($h, $v) = each %$hdr) { 130 while (my ($h, $v) = each %$hdr) {
113 $res .= "$h: $v\015\012" 131 $res .= "$h: $v\015\012"
114 } 132 }
133 $res .= "\015\012";
115 134
116 $res .= "\015\012$content" if defined $content; 135 $res .= $content if defined $content and $self->{method} eq "GET";
117 136
118 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 137 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d#
119 138
139 $self->{written} +=
120 print {$self->{fh}} $res; 140 print {$self->{fh}} $res;
121} 141}
122 142
123sub err { 143sub err {
124 my $self = shift; 144 my $self = shift;
125 my ($code, $msg, $hdr, $content) = @_; 145 my ($code, $msg, $hdr, $content) = @_;
137 157
138sub err_blocked { 158sub err_blocked {
139 my $self = shift; 159 my $self = shift;
140 my $ip = $self->{remote_addr}; 160 my $ip = $self->{remote_addr};
141 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 161 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
162
163 Coro::Event::do_timer(after => 5);
164
142 $self->err(403, "too many connections", 165 $self->err(403, "too many connections",
143 { 166 {
144 "Content-Type" => "text/html", 167 "Content-Type" => "text/html",
145 "Retry-After" => $::BLOCKTIME 168 "Retry-After" => $::BLOCKTIME
146 }, 169 },
244 267
245 $self->{name} = $uri; 268 $self->{name} = $uri;
246 269
247 # now do the path mapping 270 # now do the path mapping
248 $self->{path} = "$::DOCROOT/$host$uri"; 271 $self->{path} = "$::DOCROOT/$host$uri";
272
273 $self->access_check;
249} 274}
250 275
251sub server_address { 276sub server_address {
252 my $self = shift; 277 my $self = shift;
253 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname 278 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname
287 if (chdir $::DOCROOT) { 312 if (chdir $::DOCROOT) {
288 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 313 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
289 $ENV{HTTP_HOST} = $self->server_host; 314 $ENV{HTTP_HOST} = $self->server_host;
290 $ENV{HTTP_PORT} = $self->{server_host}; 315 $ENV{HTTP_PORT} = $self->{server_host};
291 $ENV{SCRIPT_NAME} = $self->{name}; 316 $ENV{SCRIPT_NAME} = $self->{name};
292 exec $::INDEXPROG; 317 exec $path;
293 } 318 }
294 Coro::State::_exit(0); 319 Coro::State::_exit(0);
295 } else { 320 } else {
296 } 321 }
297} 322}
300 my $self = shift; 325 my $self = shift;
301 my $path = $self->{path}; 326 my $path = $self->{path};
302 327
303 stat $path 328 stat $path
304 or $self->err(404, "not found"); 329 or $self->err(404, "not found");
330
331 $self->{stat} = [stat _];
305 332
306 # idiotic netscape sends idiotic headers AGAIN 333 # idiotic netscape sends idiotic headers AGAIN
307 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 334 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
308 ? str2time $1 : 0; 335 ? str2time $1 : 0;
309 336
312 if ($path !~ /\/$/) { 339 if ($path !~ /\/$/) {
313 # create a redirect to get the trailing "/" 340 # create a redirect to get the trailing "/"
314 my $host = $self->server_hostport; 341 my $host = $self->server_hostport;
315 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 342 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
316 } else { 343 } else {
317 $ims < (stat _)[9] 344 $ims < $self->{stat}[9]
318 or $self->err(304, "not modified"); 345 or $self->err(304, "not modified");
319 346
320 if ($self->{method} eq "GET") { 347 if ($self->{method} eq "GET") {
321 if (-r "$path/index.html") { 348 if (-r "$path/index.html") {
322 $self->{path} .= "/index.html"; 349 $self->{path} .= "/index.html";
334 } 361 }
335} 362}
336 363
337sub handle_dir { 364sub handle_dir {
338 my $self = shift; 365 my $self = shift;
339 $self->_cgi($::INDEXPROG); 366 my $idx = $self->diridx;
367
368 $self->response(200, "ok",
369 {
370 "Content-Type" => "text/html",
371 "Content-Length" => length $idx,
372 },
373 $idx);
340} 374}
341 375
342sub handle_file { 376sub handle_file {
343 my $self = shift; 377 my $self = shift;
344 my $length = -s _; 378 my $length = -s _;
364 $hdr->{"Content-Range"} = "bytes */$length"; 398 $hdr->{"Content-Range"} = "bytes */$length";
365 $self->err(416, "not satisfiable", $hdr); 399 $self->err(416, "not satisfiable", $hdr);
366 400
367satisfiable: 401satisfiable:
368 # check for segmented downloads 402 # check for segmented downloads
369 if ($l && $NO_SEGMENTED) { 403 if ($l && $::NO_SEGMENTED) {
370 if (%{$uri{$self->{uri}}} > 1) { 404 if (%{$uri{$self->{uri}}} > 1) {
371 $self->slog("segmented download refused\n"); 405 $self->slog("segmented download refused\n");
372 $self->err(400, "segmented downloads are not allowed"); 406 $self->err(400, "segmented downloads are not allowed");
373 } 407 }
374 } 408 }
380ignore: 414ignore:
381 } else { 415 } else {
382 ($l, $h) = (0, $length - 1); 416 ($l, $h) = (0, $length - 1);
383 } 417 }
384 418
385 if ($self->{path} =~ /\.html$/) { 419 $self->{path} =~ /\.([^.]+)$/;
386 $hdr->{"Content-Type"} = "text/html";
387 } else {
388 $hdr->{"Content-Type"} = "application/octet-stream"; 420 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
389 }
390
391 $hdr->{"Content-Length"} = $length; 421 $hdr->{"Content-Length"} = $length;
392 422
393 $self->response(@code, $hdr, ""); 423 $self->response(@code, $hdr, "");
394 424
395 if ($self->{method} eq "GET") { 425 if ($self->{method} eq "GET") {
404 434
405 $h -= $l - 1; 435 $h -= $l - 1;
406 436
407 while ($h > 0) { 437 while ($h > 0) {
408 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; 438 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h;
409 $self->{fh}->syswrite($buf) 439 my $w = $self->{fh}->syswrite($buf)
410 or last; 440 or last;
441 $::written += $w;
442 $self->{written} += $w;
411 } 443 }
412 } 444 }
413 445
414 close $fh; 446 close $fh;
415} 447}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines