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.21 by root, Fri Aug 17 03:33:00 2001 UTC

43 43
44my $http_port = new Coro::Socket 44my $http_port = new Coro::Socket
45 LocalAddr => $SERVER_HOST, 45 LocalAddr => $SERVER_HOST,
46 LocalPort => $SERVER_PORT, 46 LocalPort => $SERVER_PORT,
47 ReuseAddr => 1, 47 ReuseAddr => 1,
48 Listen => 1, 48 Listen => 50,
49 or die "unable to start server"; 49 or die "unable to start server";
50 50
51push @listen_sockets, $http_port; 51push @listen_sockets, $http_port;
52 52
53# the "main thread" 53# the "main thread"
70package conn; 70package conn;
71 71
72use Socket; 72use Socket;
73use HTTP::Date; 73use HTTP::Date;
74use Convert::Scalar 'weaken'; 74use Convert::Scalar 'weaken';
75use Linux::AIO;
76
77Linux::AIO::min_parallel $::AIO_PARALLEL;
78
79Event->io(fd => Linux::AIO::poll_fileno,
80 poll => 'r', async => 1,
81 cb => \&Linux::AIO::poll_cb);
82my $scheduler = Event->idle(
83 max => 0, min => 0, prio => 6, parked => 1,
84 cb => \&Coro::schedule);
75 85
76our %conn; # $conn{ip}{fh} => connobj 86our %conn; # $conn{ip}{fh} => connobj
77our %blocked; 87our %blocked;
88our %mimetype;
89
90sub read_mimetypes {
91 local *M;
92 if (open M, "<mime_types") {
93 while (<M>) {
94 if (/^([^#]\S+)\t+(\S+)$/) {
95 $mimetype{lc $1} = $2;
96 }
97 }
98 } else {
99 print "cannot open mime_types\n";
100 }
101}
102
103read_mimetypes;
78 104
79sub new { 105sub new {
80 my $class = shift; 106 my $class = shift;
81 my $peername = shift; 107 my $peername = shift;
82 my $fh = shift; 108 my $fh = shift;
83 my $self = bless { fh => $fh }, $class; 109 my $self = bless { fh => $fh }, $class;
84 my (undef, $iaddr) = unpack_sockaddr_in $peername 110 my (undef, $iaddr) = unpack_sockaddr_in $peername
85 or $self->err(500, "unable to decode peername"); 111 or $self->err(500, "unable to decode peername");
112
86 $self->{remote_addr} = inet_ntoa $iaddr; 113 $self->{remote_addr} = inet_ntoa $iaddr;
114 $self->{time} = $::NOW;
87 115
88 # enter ourselves into various lists 116 # enter ourselves into various lists
89 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 117 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
90 118
119 $::conns++;
120
91 $self; 121 $self;
92} 122}
93 123
94sub DESTROY { 124sub DESTROY {
95 my $self = shift; 125 my $self = shift;
126
127 $::conns--;
128
129 $self->eoconn;
96 delete $conn{$self->{remote_addr}}{$self*1}; 130 delete $conn{$self->{remote_addr}}{$self*1};
131}
132
133# end of connection
134sub eoconn {
97 delete $uri{$self->{uri}}{$self*1}; 135 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
98} 136}
99 137
100sub slog { 138sub slog {
101 my $self = shift; 139 my $self = shift;
102 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 140 main::slog($_[0], "$self->{remote_addr}> $_[1]");
103} 141}
104 142
105sub response { 143sub response {
106 my ($self, $code, $msg, $hdr, $content) = @_; 144 my ($self, $code, $msg, $hdr, $content) = @_;
107 my $res = "HTTP/1.0 $code $msg\015\012"; 145 my $res = "HTTP/1.1 $code $msg\015\012";
108 146
109 $res .= "Connection: close\015\012"; 147 #$res .= "Connection: close\015\012";
110 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 148 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
111 149
112 while (my ($h, $v) = each %$hdr) { 150 while (my ($h, $v) = each %$hdr) {
113 $res .= "$h: $v\015\012" 151 $res .= "$h: $v\015\012"
114 } 152 }
153 $res .= "\015\012";
115 154
116 $res .= "\015\012$content" if defined $content; 155 $res .= $content if defined $content and $self->{method} ne "HEAD";
117 156
118 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 157 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d#
119 158
159 $self->{written} +=
120 print {$self->{fh}} $res; 160 print {$self->{fh}} $res;
121} 161}
122 162
123sub err { 163sub err {
124 my $self = shift; 164 my $self = shift;
125 my ($code, $msg, $hdr, $content) = @_; 165 my ($code, $msg, $hdr, $content) = @_;
127 unless (defined $content) { 167 unless (defined $content) {
128 $content = "$code $msg"; 168 $content = "$code $msg";
129 $hdr->{"Content-Type"} = "text/plain"; 169 $hdr->{"Content-Type"} = "text/plain";
130 $hdr->{"Content-Length"} = length $content; 170 $hdr->{"Content-Length"} = length $content;
131 } 171 }
172 $hdr->{"Connection"} = "close";
132 173
133 $self->response($code, $msg, $hdr, $content); 174 $self->response($code, $msg, $hdr, $content);
134 175
135 die bless {}, err::; 176 die bless {}, err::;
136} 177}
137 178
138sub err_blocked { 179sub err_blocked {
139 my $self = shift; 180 my $self = shift;
140 my $ip = $self->{remote_addr}; 181 my $ip = $self->{remote_addr};
141 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 182 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
183
184 Coro::Event::do_timer(after => 20*rand);
185
142 $self->err(403, "too many connections", 186 $self->err(401, "too many connections",
143 { 187 {
144 "Content-Type" => "text/html", 188 "Content-Type" => "text/html",
145 "Retry-After" => $::BLOCKTIME 189 "Retry-After" => $::BLOCKTIME,
190 "Warning" => "Please do NOT retry, you have been blocked",
191 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
146 }, 192 },
147 <<EOF); 193 <<EOF);
148<html><p> 194<html><p>
149You have been blocked because you opened too many connections. You 195You have been blocked because you opened too many connections. You
150may retry at</p> 196may retry at</p>
159 205
160sub handle { 206sub handle {
161 my $self = shift; 207 my $self = shift;
162 my $fh = $self->{fh}; 208 my $fh = $self->{fh};
163 209
210 $fh->timeout($::REQ_TIMEOUT);
164 #while() { 211 while() {
212 $self->{reqs}++;
213
214 # read request and parse first line
215 my $req = $fh->readline("\015\012\015\012");
216
217 unless (defined $req) {
218 if (exists $self->{version}) {
219 last;
220 } else {
221 $self->err(408, "request timeout");
222 }
223 }
224
165 $self->{h} = {}; 225 $self->{h} = {};
166 226
167 # read request and parse first line
168 $fh->timeout($::REQ_TIMEOUT);
169 my $req = $fh->readline("\015\012\015\012");
170 $fh->timeout($::RES_TIMEOUT); 227 $fh->timeout($::RES_TIMEOUT);
171
172 defined $req or
173 $self->err(408, "request timeout");
174
175 my $ip = $self->{remote_addr}; 228 my $ip = $self->{remote_addr};
176 229
177 if ($blocked{$ip}) { 230 if ($blocked{$ip}) {
178 $self->err_blocked($blocked{$ip}) 231 $self->err_blocked($blocked{$ip})
179 if $blocked{$ip} > $::NOW; 232 if $blocked{$ip} > $::NOW;
180 233
181 delete $blocked{$ip}; 234 delete $blocked{$ip};
182 } 235 }
183 236
184 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 237 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
185 $self->slog("blocked ip $ip"); 238 $self->slog(2, "blocked ip $ip");
186 $self->err_blocked; 239 $self->err_blocked;
187 } 240 }
188 241
189 $req =~ /^(?:\015\012)? 242 $req =~ /^(?:\015\012)?
190 (GET|HEAD) \040+ 243 (GET|HEAD) \040+
191 ([^\040]+) \040+ 244 ([^\040]+) \040+
192 HTTP\/([0-9]+\.[0-9]+) 245 HTTP\/([0-9]+\.[0-9]+)
193 \015\012/gx 246 \015\012/gx
194 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 247 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
195
196 $2 ne "1.0"
197 or $self->err(506, "http protocol version not supported");
198 248
199 $self->{method} = $1; 249 $self->{method} = $1;
200 $self->{uri} = $2; 250 $self->{uri} = $2;
251 $self->{version} = $3;
252
253 $3 =~ /^1\./
254 or $self->err(506, "http protocol version $3 not supported");
201 255
202 # parse headers 256 # parse headers
203 { 257 {
204 my (%hdr, $h, $v); 258 my (%hdr, $h, $v);
205 259
218 while ($h, $v) = each %hdr; 272 while ($h, $v) = each %hdr;
219 } 273 }
220 274
221 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 275 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
222 276
223 weaken ($uri{$self->{uri}}{$self*1} = $self); 277 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
224 278
225 $self->map_uri; 279 $self->map_uri;
226 $self->respond; 280 $self->respond;
281
282 $self->eoconn;
283
284 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
285
286 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
287 $fh->timeout($::PER_TIMEOUT);
227 #} 288 }
228} 289}
229 290
230# uri => path mapping 291# uri => path mapping
231sub map_uri { 292sub map_uri {
232 my $self = shift; 293 my $self = shift;
244 305
245 $self->{name} = $uri; 306 $self->{name} = $uri;
246 307
247 # now do the path mapping 308 # now do the path mapping
248 $self->{path} = "$::DOCROOT/$host$uri"; 309 $self->{path} = "$::DOCROOT/$host$uri";
310
311 $self->access_check;
249} 312}
250 313
251sub server_address { 314sub server_address {
252 my $self = shift; 315 my $self = shift;
253 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname 316 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname
287 if (chdir $::DOCROOT) { 350 if (chdir $::DOCROOT) {
288 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 351 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
289 $ENV{HTTP_HOST} = $self->server_host; 352 $ENV{HTTP_HOST} = $self->server_host;
290 $ENV{HTTP_PORT} = $self->{server_host}; 353 $ENV{HTTP_PORT} = $self->{server_host};
291 $ENV{SCRIPT_NAME} = $self->{name}; 354 $ENV{SCRIPT_NAME} = $self->{name};
292 exec $::INDEXPROG; 355 exec $path;
293 } 356 }
294 Coro::State::_exit(0); 357 Coro::State::_exit(0);
295 } else { 358 } else {
296 } 359 }
297} 360}
300 my $self = shift; 363 my $self = shift;
301 my $path = $self->{path}; 364 my $path = $self->{path};
302 365
303 stat $path 366 stat $path
304 or $self->err(404, "not found"); 367 or $self->err(404, "not found");
368
369 $self->{stat} = [stat _];
305 370
306 # idiotic netscape sends idiotic headers AGAIN 371 # idiotic netscape sends idiotic headers AGAIN
307 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 372 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
308 ? str2time $1 : 0; 373 ? str2time $1 : 0;
309 374
312 if ($path !~ /\/$/) { 377 if ($path !~ /\/$/) {
313 # create a redirect to get the trailing "/" 378 # create a redirect to get the trailing "/"
314 my $host = $self->server_hostport; 379 my $host = $self->server_hostport;
315 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 380 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
316 } else { 381 } else {
317 $ims < (stat _)[9] 382 $ims < $self->{stat}[9]
318 or $self->err(304, "not modified"); 383 or $self->err(304, "not modified");
319 384
320 if ($self->{method} eq "GET") { 385 if ($self->{method} eq "GET") {
321 if (-r "$path/index.html") { 386 if (-r "$path/index.html") {
322 $self->{path} .= "/index.html"; 387 $self->{path} .= "/index.html";
334 } 399 }
335} 400}
336 401
337sub handle_dir { 402sub handle_dir {
338 my $self = shift; 403 my $self = shift;
339 $self->_cgi($::INDEXPROG); 404 my $idx = $self->diridx;
405
406 $self->response(200, "ok",
407 {
408 "Content-Type" => "text/html",
409 "Content-Length" => length $idx,
410 },
411 $idx);
340} 412}
341 413
342sub handle_file { 414sub handle_file {
343 my $self = shift; 415 my $self = shift;
344 my $length = -s _; 416 my $length = -s _;
360 goto ignore; 432 goto ignore;
361 } 433 }
362 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 434 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l;
363 } 435 }
364 $hdr->{"Content-Range"} = "bytes */$length"; 436 $hdr->{"Content-Range"} = "bytes */$length";
437 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
365 $self->err(416, "not satisfiable", $hdr); 438 $self->err(416, "not satisfiable", $hdr);
366 439
367satisfiable: 440satisfiable:
368 # check for segmented downloads 441 # check for segmented downloads
369 if ($l && $NO_SEGMENTED) { 442 if ($l && $::NO_SEGMENTED) {
370 if (%{$uri{$self->{uri}}} > 1) { 443 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
371 $self->slog("segmented download refused\n"); 444 Coro::Event::do_timer(after => 15);
445
372 $self->err(400, "segmented downloads are not allowed"); 446 $self->err(400, "segmented downloads are not allowed");
373 } 447 }
374 } 448 }
375 449
376 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 450 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
380ignore: 454ignore:
381 } else { 455 } else {
382 ($l, $h) = (0, $length - 1); 456 ($l, $h) = (0, $length - 1);
383 } 457 }
384 458
385 if ($self->{path} =~ /\.html$/) { 459 $self->{path} =~ /\.([^.]+)$/;
386 $hdr->{"Content-Type"} = "text/html";
387 } else {
388 $hdr->{"Content-Type"} = "application/octet-stream"; 460 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
389 }
390
391 $hdr->{"Content-Length"} = $length; 461 $hdr->{"Content-Length"} = $length;
392 462
393 $self->response(@code, $hdr, ""); 463 $self->response(@code, $hdr, "");
394 464
395 if ($self->{method} eq "GET") { 465 if ($self->{method} eq "GET") {
396 my ($fh, $buf); 466 my ($fh, $buf, $r);
467 my $current = $Coro::current;
397 open $fh, "<", $self->{path} 468 open $fh, "<", $self->{path}
398 or die "$self->{path}: late open failure ($!)"; 469 or die "$self->{path}: late open failure ($!)";
399 470
400 if ($l) {
401 sysseek $fh, $l, 0
402 or die "$self->{path}: cannot seek to $l ($!)";
403 }
404
405 $h -= $l - 1; 471 $h -= $l - 1;
406 472
473 if (0) {
474 if ($l) {
475 sysseek $fh, $l, 0;
476 }
477 }
478
407 while ($h > 0) { 479 while ($h > 0) {
480 if (0) {
408 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; 481 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
482 or last;
483 } else {
484 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
485 $buf, 0, sub {
486 $r = $_[0];
487 $current->ready;
488 $scheduler->now;
489 });
490 &Coro::schedule;
491 last unless $r;
492 }
409 $self->{fh}->syswrite($buf) 493 my $w = $self->{fh}->syswrite($buf)
410 or last; 494 or last;
495 $::written += $w;
496 $self->{written} += $w;
497 $l += $r;
411 } 498 }
412 } 499 }
413 500
414 close $fh; 501 close $fh;
415} 502}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines