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.3 by root, Fri Aug 10 02:28:28 2001 UTC vs.
Revision 1.10 by root, Sat Aug 11 16:34:47 2001 UTC

7use bytes; 7use bytes;
8 8
9# at least on my machine, this thingy serves files 9# at least on my machine, this thingy serves files
10# quite a bit faster than apache, ;) 10# quite a bit faster than apache, ;)
11# and quite a bit slower than thttpd :( 11# and quite a bit slower than thttpd :(
12
13my $port = new Coro::Socket
14 LocalAddr => $SERVER_HOST,
15 LocalPort => $SERVER_PORT,
16 ReuseAddr => 1,
17 Listen => 1,
18 or die "unable to start server";
19 12
20$SIG{PIPE} = 'IGNORE'; 13$SIG{PIPE} = 'IGNORE';
21 14
22sub slog { 15sub slog {
23 my $level = shift; 16 my $level = shift;
25 printf "---: $format\n", @_; 18 printf "---: $format\n", @_;
26} 19}
27 20
28my $connections = new Coro::Semaphore $MAX_CONNECTS; 21my $connections = new Coro::Semaphore $MAX_CONNECTS;
29 22
30my @fh; 23my @newcons;
31my @pool; 24my @pool;
32 25
33# one "execution thread" 26# one "execution thread"
34sub handler { 27sub handler {
35 while () { 28 while () {
36 my $fh = pop @fh; 29 my $new = pop @newcons;
37 if ($fh) { 30 if ($new) {
38 eval { 31 eval {
39 conn->new($fh)->handle; 32 conn->new(@$new)->handle;
40 }; 33 };
41 close $fh;
42 slog 1, "$@" if $@ && !ref $@; 34 slog 1, "$@" if $@ && !ref $@;
43 $connections->up; 35 $connections->up;
44 } else { 36 } else {
45 last if @pool >= $MAX_POOL; 37 last if @pool >= $MAX_POOL;
46 push @pool, $Coro::current; 38 push @pool, $Coro::current;
47 schedule; 39 schedule;
48 } 40 }
49 } 41 }
50} 42}
51 43
44my $http_port = new Coro::Socket
45 LocalAddr => $SERVER_HOST,
46 LocalPort => $SERVER_PORT,
47 ReuseAddr => 1,
48 Listen => 1,
49 or die "unable to start server";
50
51push @listen_sockets, $http_port;
52
52# the "main thread" 53# the "main thread"
53async { 54async {
54 slog 1, "accepting connections"; 55 slog 1, "accepting connections";
55 while () { 56 while () {
56 $connections->down; 57 $connections->down;
57 push @fh, $port->accept; 58 push @newcons, [$http_port->accept];
58 #slog 3, "accepted @$connections ".scalar(@pool); 59 #slog 3, "accepted @$connections ".scalar(@pool);
59 $::NOW = time; 60 $::NOW = time;
60 if (@pool) { 61 if (@pool) {
61 (pop @pool)->ready; 62 (pop @pool)->ready;
62 } else { 63 } else {
64 } 65 }
65 66
66 } 67 }
67}; 68};
68 69
69loop;
70print "ende\n";#d#
71
72package conn; 70package conn;
73 71
74use Socket; 72use Socket;
75use HTTP::Date; 73use HTTP::Date;
76use Convert::Scalar 'weaken'; 74use Convert::Scalar 'weaken';
77 75
78our %conn; # $conn{ip}{fh} => connobj 76our %conn; # $conn{ip}{fh} => connobj
79our %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;
80 94
81sub new { 95sub new {
82 my $class = shift; 96 my $class = shift;
97 my $peername = shift;
83 my $fh = shift; 98 my $fh = shift;
84 my $self = bless { fh => $fh }, $class; 99 my $self = bless { fh => $fh }, $class;
85 my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername 100 my (undef, $iaddr) = unpack_sockaddr_in $peername
86 or $self->err(500, "unable to get peername"); 101 or $self->err(500, "unable to decode peername");
102
87 $self->{remote_addr} = inet_ntoa $iaddr; 103 $self->{remote_addr} = inet_ntoa $iaddr;
88 104
89 # enter ourselves into various lists 105 # enter ourselves into various lists
90 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 106 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
91 107
92 print "$self->{remote_addr}: ".($self*1)." > ".%{$conn{$self->{remote_addr}}},"\n";
93 $self; 108 $self;
94} 109}
95 110
96sub DESTROY { 111sub DESTROY {
97 my $self = shift; 112 my $self = shift;
98 delete $conn{$self->{remote_addr}}{$self*1}; 113 delete $conn{$self->{remote_addr}}{$self*1};
99 delete $uri{$self->{uri}}{$self*1}; 114 delete $uri{$self->{uri}}{$self*1};
100} 115}
101 116
102sub slog { 117sub slog {
103 main::slog(@_); 118 my $self = shift;
119 main::slog($_[0], "$self->{remote_addr}> $_[1]");
104} 120}
105 121
106sub print_response { 122sub response {
107 my ($self, $code, $msg, $hdr, $content) = @_; 123 my ($self, $code, $msg, $hdr, $content) = @_;
108 my $res = "HTTP/1.0 $code $msg\015\012"; 124 my $res = "HTTP/1.0 $code $msg\015\012";
109 125
110 $hdr->{Date} = time2str $::NOW; # slow? nah. 126 $res .= "Connection: close\015\012";
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 }
115 $res .= "\015\012$content" if defined $content; 132 $res .= "\015\012";
133
134 $res .= $content if defined $content and $self->{method} eq "GET";
116 135
117 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#
118 137
119 print {$self->{fh}} $res; 138 print {$self->{fh}} $res;
120} 139}
127 $content = "$code $msg"; 146 $content = "$code $msg";
128 $hdr->{"Content-Type"} = "text/plain"; 147 $hdr->{"Content-Type"} = "text/plain";
129 $hdr->{"Content-Length"} = length $content; 148 $hdr->{"Content-Length"} = length $content;
130 } 149 }
131 150
132 $self->slog($msg) if $code;
133
134 $self->print_response($code, $msg, $hdr, $content); 151 $self->response($code, $msg, $hdr, $content);
135 152
136 die bless {}, err::; 153 die bless {}, err::;
137} 154}
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",
164 {
165 "Content-Type" => "text/html",
144 { "Retry-After" => $::BLOCKTIME }, 166 "Retry-After" => $::BLOCKTIME
167 },
145 <<EOF); 168 <<EOF);
169<html><p>
146You have been blocked because you opened too many connections. You 170You have been blocked because you opened too many connections. You
147may retry at $time. Until then, 171may retry at</p>
148every new access will renew the block. 172
173 <p><blockquote>$time.</blockquote></p>
174
175<p>Until then, each new access will renew the block. You might want to have a
176look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p>
177</html>
149EOF 178EOF
150} 179}
151 180
152sub handle { 181sub handle {
153 my $self = shift; 182 my $self = shift;
213 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 242 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
214 243
215 weaken ($uri{$self->{uri}}{$self*1} = $self); 244 weaken ($uri{$self->{uri}}{$self*1} = $self);
216 245
217 $self->map_uri; 246 $self->map_uri;
218
219 Coro::Event::do_timer(after => 5);
220
221 $self->respond; 247 $self->respond;
222 #} 248 #}
223} 249}
224 250
225# uri => path mapping 251# uri => path mapping
239 265
240 $self->{name} = $uri; 266 $self->{name} = $uri;
241 267
242 # now do the path mapping 268 # now do the path mapping
243 $self->{path} = "$::DOCROOT/$host$uri"; 269 $self->{path} = "$::DOCROOT/$host$uri";
270
271 $self->access_check;
244} 272}
245 273
246sub server_address { 274sub server_address {
247 my $self = shift; 275 my $self = shift;
248 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname 276 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname
282 if (chdir $::DOCROOT) { 310 if (chdir $::DOCROOT) {
283 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 311 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
284 $ENV{HTTP_HOST} = $self->server_host; 312 $ENV{HTTP_HOST} = $self->server_host;
285 $ENV{HTTP_PORT} = $self->{server_host}; 313 $ENV{HTTP_PORT} = $self->{server_host};
286 $ENV{SCRIPT_NAME} = $self->{name}; 314 $ENV{SCRIPT_NAME} = $self->{name};
287 exec $::INDEXPROG; 315 exec $path;
288 } 316 }
289 Coro::State::_exit(0); 317 Coro::State::_exit(0);
290 } else { 318 } else {
291 } 319 }
292} 320}
295 my $self = shift; 323 my $self = shift;
296 my $path = $self->{path}; 324 my $path = $self->{path};
297 325
298 stat $path 326 stat $path
299 or $self->err(404, "not found"); 327 or $self->err(404, "not found");
328
329 $self->{stat} = [stat _];
300 330
301 # idiotic netscape sends idiotic headers AGAIN 331 # idiotic netscape sends idiotic headers AGAIN
302 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 332 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
303 ? str2time $1 : 0; 333 ? str2time $1 : 0;
304 334
307 if ($path !~ /\/$/) { 337 if ($path !~ /\/$/) {
308 # create a redirect to get the trailing "/" 338 # create a redirect to get the trailing "/"
309 my $host = $self->server_hostport; 339 my $host = $self->server_hostport;
310 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 340 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
311 } else { 341 } else {
312 $ims < (stat _)[9] 342 $ims < $self->{stat}[9]
313 or $self->err(304, "not modified"); 343 or $self->err(304, "not modified");
314 344
315 if ($self->{method} eq "GET") { 345 if ($self->{method} eq "GET") {
316 if (-r "$path/index.html") { 346 if (-r "$path/index.html") {
317 $self->{path} .= "/index.html"; 347 $self->{path} .= "/index.html";
329 } 359 }
330} 360}
331 361
332sub handle_dir { 362sub handle_dir {
333 my $self = shift; 363 my $self = shift;
334 $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);
335} 372}
336 373
337sub handle_file { 374sub handle_file {
338 my $self = shift; 375 my $self = shift;
339 my $length = -s _; 376 my $length = -s _;
358 } 395 }
359 $hdr->{"Content-Range"} = "bytes */$length"; 396 $hdr->{"Content-Range"} = "bytes */$length";
360 $self->err(416, "not satisfiable", $hdr); 397 $self->err(416, "not satisfiable", $hdr);
361 398
362satisfiable: 399satisfiable:
400 # check for segmented downloads
401 if ($l && $::NO_SEGMENTED) {
402 if (%{$uri{$self->{uri}}} > 1) {
403 $self->slog("segmented download refused\n");
404 $self->err(400, "segmented downloads are not allowed");
405 }
406 }
407
363 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 408 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
364 @code = (206, "partial content"); 409 @code = (206, "partial content");
365 $length = $h - $l + 1; 410 $length = $h - $l + 1;
366 411
367ignore: 412ignore:
368 } else { 413 } else {
369 ($l, $h) = (0, $length - 1); 414 ($l, $h) = (0, $length - 1);
370 } 415 }
371 416
372 if ($self->{path} =~ /\.html$/) { 417 $self->{path} =~ /\.([^.]+)$/;
373 $hdr->{"Content-Type"} = "text/html";
374 } else {
375 $hdr->{"Content-Type"} = "application/octet-stream"; 418 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
376 }
377
378 $hdr->{"Content-Length"} = $length; 419 $hdr->{"Content-Length"} = $length;
379 420
380 $self->print_response(@code, $hdr, ""); 421 $self->response(@code, $hdr, "");
381 422
382 if ($self->{method} eq "GET") { 423 if ($self->{method} eq "GET") {
383 my ($fh, $buf); 424 my ($fh, $buf);
384 open $fh, "<", $self->{path} 425 open $fh, "<", $self->{path}
385 or die "$self->{path}: late open failure ($!)"; 426 or die "$self->{path}: late open failure ($!)";
390 } 431 }
391 432
392 $h -= $l - 1; 433 $h -= $l - 1;
393 434
394 while ($h > 0) { 435 while ($h > 0) {
395 $h -= sysread $fh, $buf, $h > 4096 ? 4096 : $h; 436 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h;
396 print {$self->{fh}} $buf 437 $self->{fh}->syswrite($buf)
397 or last; 438 or last;
398 } 439 }
399 } 440 }
400 441
401 close $fh; 442 close $fh;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines