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.15 by root, Tue Aug 14 02:28:51 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 => 50,
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;
104 $self->{time} = $::NOW;
88 105
89 # enter ourselves into various lists 106 # enter ourselves into various lists
90 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 107 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
91 108
92 print "$self->{remote_addr}: ".($self*1)." > ".%{$conn{$self->{remote_addr}}},"\n"; 109 $::conns++;
110
93 $self; 111 $self;
94} 112}
95 113
96sub DESTROY { 114sub DESTROY {
97 my $self = shift; 115 my $self = shift;
116
117 $::conns--;
118
98 delete $conn{$self->{remote_addr}}{$self*1}; 119 delete $conn{$self->{remote_addr}}{$self*1};
99 delete $uri{$self->{uri}}{$self*1}; 120 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
100} 121}
101 122
102sub slog { 123sub slog {
103 main::slog(@_); 124 my $self = shift;
125 main::slog($_[0], "$self->{remote_addr}> $_[1]");
104} 126}
105 127
106sub print_response { 128sub response {
107 my ($self, $code, $msg, $hdr, $content) = @_; 129 my ($self, $code, $msg, $hdr, $content) = @_;
108 my $res = "HTTP/1.0 $code $msg\015\012"; 130 my $res = "HTTP/1.0 $code $msg\015\012";
109 131
110 $hdr->{Date} = time2str $::NOW; # slow? nah. 132 $res .= "Connection: close\015\012";
133 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
111 134
112 while (my ($h, $v) = each %$hdr) { 135 while (my ($h, $v) = each %$hdr) {
113 $res .= "$h: $v\015\012" 136 $res .= "$h: $v\015\012"
114 } 137 }
115 $res .= "\015\012$content" if defined $content; 138 $res .= "\015\012";
139
140 $res .= $content if defined $content and $self->{method} ne "HEAD";
116 141
117 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 142 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d#
118 143
144 $self->{written} +=
119 print {$self->{fh}} $res; 145 print {$self->{fh}} $res;
120} 146}
121 147
122sub err { 148sub err {
123 my $self = shift; 149 my $self = shift;
124 my ($code, $msg, $hdr, $content) = @_; 150 my ($code, $msg, $hdr, $content) = @_;
127 $content = "$code $msg"; 153 $content = "$code $msg";
128 $hdr->{"Content-Type"} = "text/plain"; 154 $hdr->{"Content-Type"} = "text/plain";
129 $hdr->{"Content-Length"} = length $content; 155 $hdr->{"Content-Length"} = length $content;
130 } 156 }
131 157
132 $self->slog($msg) if $code;
133
134 $self->print_response($code, $msg, $hdr, $content); 158 $self->response($code, $msg, $hdr, $content);
135 159
136 die bless {}, err::; 160 die bless {}, err::;
137} 161}
138 162
139sub err_blocked { 163sub err_blocked {
140 my $self = shift; 164 my $self = shift;
141 my $ip = $self->{remote_addr}; 165 my $ip = $self->{remote_addr};
142 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 166 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
167
168 Coro::Event::do_timer(after => 15);
169
143 $self->err(403, "too many connections", 170 $self->err(401, "too many connections",
171 {
172 "Content-Type" => "text/html",
144 { "Retry-After" => $::BLOCKTIME }, 173 "Retry-After" => $::BLOCKTIME
174 },
145 <<EOF); 175 <<EOF);
176<html><p>
146You have been blocked because you opened too many connections. You 177You have been blocked because you opened too many connections. You
147may retry at $time. Until then, 178may retry at</p>
148every new access will renew the block. 179
180 <p><blockquote>$time.</blockquote></p>
181
182<p>Until then, each new access will renew the block. You might want to have a
183look at the <a href="http://www.goof.com/pcg/marc/animefaq.html">FAQ</a>.</p>
184</html>
149EOF 185EOF
150} 186}
151 187
152sub handle { 188sub handle {
153 my $self = shift; 189 my $self = shift;
172 208
173 delete $blocked{$ip}; 209 delete $blocked{$ip};
174 } 210 }
175 211
176 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 212 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
177 $self->slog("blocked ip $ip"); 213 $self->slog(2, "blocked ip $ip");
178 $self->err_blocked; 214 $self->err_blocked;
179 } 215 }
180 216
181 $req =~ /^(?:\015\012)? 217 $req =~ /^(?:\015\012)?
182 (GET|HEAD) \040+ 218 (GET|HEAD) \040+
183 ([^\040]+) \040+ 219 ([^\040]+) \040+
184 HTTP\/([0-9]+\.[0-9]+) 220 HTTP\/([0-9]+\.[0-9]+)
185 \015\012/gx 221 \015\012/gx
186 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 222 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
187 223
188 $2 ne "1.0" 224 $2 ne "1.0"
189 or $self->err(506, "http protocol version not supported"); 225 or $self->err(506, "http protocol version not supported");
190 226
191 $self->{method} = $1; 227 $self->{method} = $1;
210 while ($h, $v) = each %hdr; 246 while ($h, $v) = each %hdr;
211 } 247 }
212 248
213 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 249 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
214 250
215 weaken ($uri{$self->{uri}}{$self*1} = $self); 251 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
216 252
217 $self->map_uri; 253 $self->map_uri;
218
219 Coro::Event::do_timer(after => 5);
220
221 $self->respond; 254 $self->respond;
222 #} 255 #}
223} 256}
224 257
225# uri => path mapping 258# uri => path mapping
239 272
240 $self->{name} = $uri; 273 $self->{name} = $uri;
241 274
242 # now do the path mapping 275 # now do the path mapping
243 $self->{path} = "$::DOCROOT/$host$uri"; 276 $self->{path} = "$::DOCROOT/$host$uri";
277
278 $self->access_check;
244} 279}
245 280
246sub server_address { 281sub server_address {
247 my $self = shift; 282 my $self = shift;
248 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname 283 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname
282 if (chdir $::DOCROOT) { 317 if (chdir $::DOCROOT) {
283 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 318 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
284 $ENV{HTTP_HOST} = $self->server_host; 319 $ENV{HTTP_HOST} = $self->server_host;
285 $ENV{HTTP_PORT} = $self->{server_host}; 320 $ENV{HTTP_PORT} = $self->{server_host};
286 $ENV{SCRIPT_NAME} = $self->{name}; 321 $ENV{SCRIPT_NAME} = $self->{name};
287 exec $::INDEXPROG; 322 exec $path;
288 } 323 }
289 Coro::State::_exit(0); 324 Coro::State::_exit(0);
290 } else { 325 } else {
291 } 326 }
292} 327}
295 my $self = shift; 330 my $self = shift;
296 my $path = $self->{path}; 331 my $path = $self->{path};
297 332
298 stat $path 333 stat $path
299 or $self->err(404, "not found"); 334 or $self->err(404, "not found");
335
336 $self->{stat} = [stat _];
300 337
301 # idiotic netscape sends idiotic headers AGAIN 338 # idiotic netscape sends idiotic headers AGAIN
302 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 339 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
303 ? str2time $1 : 0; 340 ? str2time $1 : 0;
304 341
307 if ($path !~ /\/$/) { 344 if ($path !~ /\/$/) {
308 # create a redirect to get the trailing "/" 345 # create a redirect to get the trailing "/"
309 my $host = $self->server_hostport; 346 my $host = $self->server_hostport;
310 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 347 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
311 } else { 348 } else {
312 $ims < (stat _)[9] 349 $ims < $self->{stat}[9]
313 or $self->err(304, "not modified"); 350 or $self->err(304, "not modified");
314 351
315 if ($self->{method} eq "GET") { 352 if ($self->{method} eq "GET") {
316 if (-r "$path/index.html") { 353 if (-r "$path/index.html") {
317 $self->{path} .= "/index.html"; 354 $self->{path} .= "/index.html";
329 } 366 }
330} 367}
331 368
332sub handle_dir { 369sub handle_dir {
333 my $self = shift; 370 my $self = shift;
334 $self->_cgi($::INDEXPROG); 371 my $idx = $self->diridx;
372
373 $self->response(200, "ok",
374 {
375 "Content-Type" => "text/html",
376 "Content-Length" => length $idx,
377 },
378 $idx);
335} 379}
336 380
337sub handle_file { 381sub handle_file {
338 my $self = shift; 382 my $self = shift;
339 my $length = -s _; 383 my $length = -s _;
358 } 402 }
359 $hdr->{"Content-Range"} = "bytes */$length"; 403 $hdr->{"Content-Range"} = "bytes */$length";
360 $self->err(416, "not satisfiable", $hdr); 404 $self->err(416, "not satisfiable", $hdr);
361 405
362satisfiable: 406satisfiable:
407 # check for segmented downloads
408 if ($l && $::NO_SEGMENTED) {
409 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
410 Coro::Event::do_timer(after => 15);
411
412 $self->err(400, "segmented downloads are not allowed");
413 }
414 }
415
363 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 416 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
364 @code = (206, "partial content"); 417 @code = (206, "partial content");
365 $length = $h - $l + 1; 418 $length = $h - $l + 1;
366 419
367ignore: 420ignore:
368 } else { 421 } else {
369 ($l, $h) = (0, $length - 1); 422 ($l, $h) = (0, $length - 1);
370 } 423 }
371 424
372 if ($self->{path} =~ /\.html$/) { 425 $self->{path} =~ /\.([^.]+)$/;
373 $hdr->{"Content-Type"} = "text/html";
374 } else {
375 $hdr->{"Content-Type"} = "application/octet-stream"; 426 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
376 }
377
378 $hdr->{"Content-Length"} = $length; 427 $hdr->{"Content-Length"} = $length;
379 428
380 $self->print_response(@code, $hdr, ""); 429 $self->response(@code, $hdr, "");
381 430
382 if ($self->{method} eq "GET") { 431 if ($self->{method} eq "GET") {
383 my ($fh, $buf); 432 my ($fh, $buf);
384 open $fh, "<", $self->{path} 433 open $fh, "<", $self->{path}
385 or die "$self->{path}: late open failure ($!)"; 434 or die "$self->{path}: late open failure ($!)";
390 } 439 }
391 440
392 $h -= $l - 1; 441 $h -= $l - 1;
393 442
394 while ($h > 0) { 443 while ($h > 0) {
395 $h -= sysread $fh, $buf, $h > 4096 ? 4096 : $h; 444 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h;
396 print {$self->{fh}} $buf 445 my $w = $self->{fh}->syswrite($buf)
397 or last; 446 or last;
447 $::written += $w;
448 $self->{written} += $w;
398 } 449 }
399 } 450 }
400 451
401 close $fh; 452 close $fh;
402} 453}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines