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.7 by root, Sat Aug 11 00:37:32 2001 UTC vs.
Revision 1.10 by root, Sat Aug 11 16:34:47 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;
111 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 127 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
112 128
113 while (my ($h, $v) = each %$hdr) { 129 while (my ($h, $v) = each %$hdr) {
114 $res .= "$h: $v\015\012" 130 $res .= "$h: $v\015\012"
115 } 131 }
132 $res .= "\015\012";
116 133
117 $res .= "\015\012$content" if defined $content; 134 $res .= $content if defined $content and $self->{method} eq "GET";
118 135
119 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#
120 137
121 print {$self->{fh}} $res; 138 print {$self->{fh}} $res;
122} 139}
138 155
139sub err_blocked { 156sub err_blocked {
140 my $self = shift; 157 my $self = shift;
141 my $ip = $self->{remote_addr}; 158 my $ip = $self->{remote_addr};
142 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 159 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
160
161 Coro::Event::do_timer(after => 5);
162
143 $self->err(403, "too many connections", 163 $self->err(403, "too many connections",
144 { 164 {
145 "Content-Type" => "text/html", 165 "Content-Type" => "text/html",
146 "Retry-After" => $::BLOCKTIME 166 "Retry-After" => $::BLOCKTIME
147 }, 167 },
290 if (chdir $::DOCROOT) { 310 if (chdir $::DOCROOT) {
291 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 311 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
292 $ENV{HTTP_HOST} = $self->server_host; 312 $ENV{HTTP_HOST} = $self->server_host;
293 $ENV{HTTP_PORT} = $self->{server_host}; 313 $ENV{HTTP_PORT} = $self->{server_host};
294 $ENV{SCRIPT_NAME} = $self->{name}; 314 $ENV{SCRIPT_NAME} = $self->{name};
295 exec $::INDEXPROG; 315 exec $path;
296 } 316 }
297 Coro::State::_exit(0); 317 Coro::State::_exit(0);
298 } else { 318 } else {
299 } 319 }
300} 320}
303 my $self = shift; 323 my $self = shift;
304 my $path = $self->{path}; 324 my $path = $self->{path};
305 325
306 stat $path 326 stat $path
307 or $self->err(404, "not found"); 327 or $self->err(404, "not found");
328
329 $self->{stat} = [stat _];
308 330
309 # idiotic netscape sends idiotic headers AGAIN 331 # idiotic netscape sends idiotic headers AGAIN
310 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 332 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
311 ? str2time $1 : 0; 333 ? str2time $1 : 0;
312 334
315 if ($path !~ /\/$/) { 337 if ($path !~ /\/$/) {
316 # create a redirect to get the trailing "/" 338 # create a redirect to get the trailing "/"
317 my $host = $self->server_hostport; 339 my $host = $self->server_hostport;
318 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 340 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
319 } else { 341 } else {
320 $ims < (stat _)[9] 342 $ims < $self->{stat}[9]
321 or $self->err(304, "not modified"); 343 or $self->err(304, "not modified");
322 344
323 if ($self->{method} eq "GET") { 345 if ($self->{method} eq "GET") {
324 if (-r "$path/index.html") { 346 if (-r "$path/index.html") {
325 $self->{path} .= "/index.html"; 347 $self->{path} .= "/index.html";
337 } 359 }
338} 360}
339 361
340sub handle_dir { 362sub handle_dir {
341 my $self = shift; 363 my $self = shift;
342 $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);
343} 372}
344 373
345sub handle_file { 374sub handle_file {
346 my $self = shift; 375 my $self = shift;
347 my $length = -s _; 376 my $length = -s _;
367 $hdr->{"Content-Range"} = "bytes */$length"; 396 $hdr->{"Content-Range"} = "bytes */$length";
368 $self->err(416, "not satisfiable", $hdr); 397 $self->err(416, "not satisfiable", $hdr);
369 398
370satisfiable: 399satisfiable:
371 # check for segmented downloads 400 # check for segmented downloads
372 if ($l && $NO_SEGMENTED) { 401 if ($l && $::NO_SEGMENTED) {
373 if (%{$uri{$self->{uri}}} > 1) { 402 if (%{$uri{$self->{uri}}} > 1) {
374 $self->slog("segmented download refused\n"); 403 $self->slog("segmented download refused\n");
375 $self->err(400, "segmented downloads are not allowed"); 404 $self->err(400, "segmented downloads are not allowed");
376 } 405 }
377 } 406 }
383ignore: 412ignore:
384 } else { 413 } else {
385 ($l, $h) = (0, $length - 1); 414 ($l, $h) = (0, $length - 1);
386 } 415 }
387 416
388 if ($self->{path} =~ /\.html$/) { 417 $self->{path} =~ /\.([^.]+)$/;
389 $hdr->{"Content-Type"} = "text/html";
390 } else {
391 $hdr->{"Content-Type"} = "application/octet-stream"; 418 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
392 }
393
394 $hdr->{"Content-Length"} = $length; 419 $hdr->{"Content-Length"} = $length;
395 420
396 $self->response(@code, $hdr, ""); 421 $self->response(@code, $hdr, "");
397 422
398 if ($self->{method} eq "GET") { 423 if ($self->{method} eq "GET") {
415 } 440 }
416 441
417 close $fh; 442 close $fh;
418} 443}
419 444
420sub access_check {
421 my $self = shift;
422 my $whois = ::ip_request($self->{remote_addr});
423
424 if ($whois =~ /^\*cy: (\S+)/m) {
425 $self->slog(9, "COUNTRY($1)");
426 } else {
427 $self->slog(9, "no country($whois)");
428 }
429}
430
4311; 4451;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines