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.2 by root, Thu Aug 9 19:11:13 2001 UTC vs.
Revision 1.14 by root, Sun Aug 12 14:09:03 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);
60 $::NOW = time;
59 if (@pool) { 61 if (@pool) {
60 (pop @pool)->ready; 62 (pop @pool)->ready;
61 } else { 63 } else {
62 async \&handler; 64 async \&handler;
63 } 65 }
64 66
65 } 67 }
66}; 68};
67 69
68loop;
69print "ende\n";#d#
70
71package conn; 70package conn;
72 71
73use Socket; 72use Socket;
74use HTTP::Date; 73use HTTP::Date;
75use Convert::Scalar 'weaken'; 74use Convert::Scalar 'weaken';
76 75
77my %conn; # $conn{ip}{fh} => connobj 76our %conn; # $conn{ip}{fh} => connobj
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;
97 my $peername = shift;
81 my $fh = shift; 98 my $fh = shift;
82 my $self = bless { fh => $fh }, $class; 99 my $self = bless { fh => $fh }, $class;
83 my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername 100 my (undef, $iaddr) = unpack_sockaddr_in $peername
84 or $self->err(500, "unable to get peername"); 101 or $self->err(500, "unable to decode peername");
102
85 $self->{remote_address} = inet_ntoa $iaddr; 103 $self->{remote_addr} = inet_ntoa $iaddr;
104 $self->{time} = $::NOW;
86 105
87 # enter ourselves into various lists 106 # enter ourselves into various lists
88 weaken ($conn{$self->{remote_address}}{$self*1} = $self); 107 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
89 print $self->{remote_address}.": ".($self*1)." > ".%{$conn{$self->{remote_address}}},"\n"; 108
109 $::conns++;
110
90 $self; 111 $self;
91} 112}
92 113
93sub DESTROY { 114sub DESTROY {
94 my $self = shift; 115 my $self = shift;
116
117 $::conns--;
118
95 delete $conn{$self->{remote_address}}{$self*1}; 119 delete $conn{$self->{remote_addr}}{$self*1};
120 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
96} 121}
97 122
98sub slog { 123sub slog {
99 main::slog(@_); 124 my $self = shift;
125 main::slog($_[0], "$self->{remote_addr}> $_[1]");
100} 126}
101 127
102sub print_response { 128sub response {
103 my ($self, $code, $msg, $hdr, $content) = @_; 129 my ($self, $code, $msg, $hdr, $content) = @_;
104 my $res = "HTTP/1.0 $code $msg\015\012"; 130 my $res = "HTTP/1.0 $code $msg\015\012";
105 131
106 $hdr->{Date} = time2str time; # slow? nah. 132 $res .= "Connection: close\015\012";
133 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
107 134
108 while (my ($h, $v) = each %$hdr) { 135 while (my ($h, $v) = each %$hdr) {
109 $res .= "$h: $v\015\012" 136 $res .= "$h: $v\015\012"
110 } 137 }
111 $res .= "\015\012$content" if defined $content; 138 $res .= "\015\012";
112 139
140 $res .= $content if defined $content and $self->{method} ne "HEAD";
141
113 print STDERR "$self->{remote_address} \"$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#
114 143
144 $self->{written} +=
115 print {$self->{fh}} $res; 145 print {$self->{fh}} $res;
116} 146}
117 147
118sub err { 148sub err {
119 my $self = shift; 149 my $self = shift;
120 my ($code, $msg, $hdr, $content) = @_; 150 my ($code, $msg, $hdr, $content) = @_;
123 $content = "$code $msg"; 153 $content = "$code $msg";
124 $hdr->{"Content-Type"} = "text/plain"; 154 $hdr->{"Content-Type"} = "text/plain";
125 $hdr->{"Content-Length"} = length $content; 155 $hdr->{"Content-Length"} = length $content;
126 } 156 }
127 157
128 $self->slog($msg) if $code;
129
130 $self->print_response($code, $msg, $hdr, $content); 158 $self->response($code, $msg, $hdr, $content);
131 159
132 die bless {}, err::; 160 die bless {}, err::;
161}
162
163sub err_blocked {
164 my $self = shift;
165 my $ip = $self->{remote_addr};
166 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
167
168 Coro::Event::do_timer(after => 15);
169
170 $self->err(401, "too many connections",
171 {
172 "Content-Type" => "text/html",
173 "Retry-After" => $::BLOCKTIME
174 },
175 <<EOF);
176<html><p>
177You have been blocked because you opened too many connections. You
178may retry at</p>
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>
185EOF
133} 186}
134 187
135sub handle { 188sub handle {
136 my $self = shift; 189 my $self = shift;
137 my $fh = $self->{fh}; 190 my $fh = $self->{fh};
144 my $req = $fh->readline("\015\012\015\012"); 197 my $req = $fh->readline("\015\012\015\012");
145 $fh->timeout($::RES_TIMEOUT); 198 $fh->timeout($::RES_TIMEOUT);
146 199
147 defined $req or 200 defined $req or
148 $self->err(408, "request timeout"); 201 $self->err(408, "request timeout");
202
203 my $ip = $self->{remote_addr};
204
205 $self->err_blocked($blocked{$ip});
206 if ($blocked{$ip}) {
207 $self->err_blocked($blocked{$ip})
208 if $blocked{$ip} > $::NOW;
209
210 delete $blocked{$ip};
211 }
212
213 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
214 $self->slog(2, "blocked ip $ip");
215 $self->err_blocked;
216 }
149 217
150 $req =~ /^(?:\015\012)? 218 $req =~ /^(?:\015\012)?
151 (GET|HEAD) \040+ 219 (GET|HEAD) \040+
152 ([^\040]+) \040+ 220 ([^\040]+) \040+
153 HTTP\/([0-9]+\.[0-9]+) 221 HTTP\/([0-9]+\.[0-9]+)
154 \015\012/gx 222 \015\012/gx
155 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 223 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
156 224
157 $2 ne "1.0" 225 $2 ne "1.0"
158 or $self->err(506, "http protocol version not supported"); 226 or $self->err(506, "http protocol version not supported");
159 227
160 $self->{method} = $1; 228 $self->{method} = $1;
179 while ($h, $v) = each %hdr; 247 while ($h, $v) = each %hdr;
180 } 248 }
181 249
182 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 250 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
183 251
252 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
253
184 $self->map_uri; 254 $self->map_uri;
185
186 Coro::Event::do_timer(after => 5);
187
188 $self->respond; 255 $self->respond;
189 #} 256 #}
190} 257}
191 258
192# uri => path mapping 259# uri => path mapping
206 273
207 $self->{name} = $uri; 274 $self->{name} = $uri;
208 275
209 # now do the path mapping 276 # now do the path mapping
210 $self->{path} = "$::DOCROOT/$host$uri"; 277 $self->{path} = "$::DOCROOT/$host$uri";
278
279 $self->access_check;
211} 280}
212 281
213sub server_address { 282sub server_address {
214 my $self = shift; 283 my $self = shift;
215 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname 284 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname
249 if (chdir $::DOCROOT) { 318 if (chdir $::DOCROOT) {
250 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 319 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
251 $ENV{HTTP_HOST} = $self->server_host; 320 $ENV{HTTP_HOST} = $self->server_host;
252 $ENV{HTTP_PORT} = $self->{server_host}; 321 $ENV{HTTP_PORT} = $self->{server_host};
253 $ENV{SCRIPT_NAME} = $self->{name}; 322 $ENV{SCRIPT_NAME} = $self->{name};
254 exec $::INDEXPROG; 323 exec $path;
255 } 324 }
256 Coro::State::_exit(0); 325 Coro::State::_exit(0);
257 } else { 326 } else {
258 } 327 }
259} 328}
262 my $self = shift; 331 my $self = shift;
263 my $path = $self->{path}; 332 my $path = $self->{path};
264 333
265 stat $path 334 stat $path
266 or $self->err(404, "not found"); 335 or $self->err(404, "not found");
336
337 $self->{stat} = [stat _];
267 338
268 # idiotic netscape sends idiotic headers AGAIN 339 # idiotic netscape sends idiotic headers AGAIN
269 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 340 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
270 ? str2time $1 : 0; 341 ? str2time $1 : 0;
271 342
274 if ($path !~ /\/$/) { 345 if ($path !~ /\/$/) {
275 # create a redirect to get the trailing "/" 346 # create a redirect to get the trailing "/"
276 my $host = $self->server_hostport; 347 my $host = $self->server_hostport;
277 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 348 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
278 } else { 349 } else {
279 $ims < (stat _)[9] 350 $ims < $self->{stat}[9]
280 or $self->err(304, "not modified"); 351 or $self->err(304, "not modified");
281 352
282 if ($self->{method} eq "GET") { 353 if ($self->{method} eq "GET") {
283 if (-r "$path/index.html") { 354 if (-r "$path/index.html") {
284 $self->{path} .= "/index.html"; 355 $self->{path} .= "/index.html";
296 } 367 }
297} 368}
298 369
299sub handle_dir { 370sub handle_dir {
300 my $self = shift; 371 my $self = shift;
301 $self->_cgi($::INDEXPROG); 372 my $idx = $self->diridx;
373
374 $self->response(200, "ok",
375 {
376 "Content-Type" => "text/html",
377 "Content-Length" => length $idx,
378 },
379 $idx);
302} 380}
303 381
304sub handle_file { 382sub handle_file {
305 my $self = shift; 383 my $self = shift;
306 my $length = -s _; 384 my $length = -s _;
325 } 403 }
326 $hdr->{"Content-Range"} = "bytes */$length"; 404 $hdr->{"Content-Range"} = "bytes */$length";
327 $self->err(416, "not satisfiable", $hdr); 405 $self->err(416, "not satisfiable", $hdr);
328 406
329satisfiable: 407satisfiable:
408 # check for segmented downloads
409 if ($l && $::NO_SEGMENTED) {
410 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
411 Coro::Event::do_timer(after => 15);
412
413 $self->err(400, "segmented downloads are not allowed");
414 }
415 }
416
330 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 417 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
331 @code = (206, "partial content"); 418 @code = (206, "partial content");
332 $length = $h - $l + 1; 419 $length = $h - $l + 1;
333 420
334ignore: 421ignore:
335 } else { 422 } else {
336 ($l, $h) = (0, $length - 1); 423 ($l, $h) = (0, $length - 1);
337 } 424 }
338 425
339 if ($self->{path} =~ /\.html$/) { 426 $self->{path} =~ /\.([^.]+)$/;
340 $hdr->{"Content-Type"} = "text/html";
341 } else {
342 $hdr->{"Content-Type"} = "application/octet-stream"; 427 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
343 }
344
345 $hdr->{"Content-Length"} = $length; 428 $hdr->{"Content-Length"} = $length;
346 429
347 $self->print_response(@code, $hdr, ""); 430 $self->response(@code, $hdr, "");
348 431
349 if ($self->{method} eq "GET") { 432 if ($self->{method} eq "GET") {
350 my ($fh, $buf); 433 my ($fh, $buf);
351 open $fh, "<", $self->{path} 434 open $fh, "<", $self->{path}
352 or die "$self->{path}: late open failure ($!)"; 435 or die "$self->{path}: late open failure ($!)";
357 } 440 }
358 441
359 $h -= $l - 1; 442 $h -= $l - 1;
360 443
361 while ($h > 0) { 444 while ($h > 0) {
362 $h -= sysread $fh, $buf, $h > 4096 ? 4096 : $h; 445 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h;
363 print {$self->{fh}} $buf 446 my $w = $self->{fh}->syswrite($buf)
364 or last; 447 or last;
448 $::written += $w;
449 $self->{written} += $w;
365 } 450 }
366 } 451 }
367 452
368 close $fh; 453 close $fh;
369} 454}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines