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.16 by root, Tue Aug 14 19:38:25 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',
81 async => 1,
82 cb => \&Linux::AIO::poll_cb );
83
84Event->add_hooks(prepare => sub {
85 &Coro::cede while &Coro::nready;
86 1e6;
87});
75 88
76our %conn; # $conn{ip}{fh} => connobj 89our %conn; # $conn{ip}{fh} => connobj
77our %blocked; 90our %blocked;
91our %mimetype;
92
93sub read_mimetypes {
94 local *M;
95 if (open M, "<mime_types") {
96 while (<M>) {
97 if (/^([^#]\S+)\t+(\S+)$/) {
98 $mimetype{lc $1} = $2;
99 }
100 }
101 } else {
102 print "cannot open mime_types\n";
103 }
104}
105
106read_mimetypes;
78 107
79sub new { 108sub new {
80 my $class = shift; 109 my $class = shift;
81 my $peername = shift; 110 my $peername = shift;
82 my $fh = shift; 111 my $fh = shift;
83 my $self = bless { fh => $fh }, $class; 112 my $self = bless { fh => $fh }, $class;
84 my (undef, $iaddr) = unpack_sockaddr_in $peername 113 my (undef, $iaddr) = unpack_sockaddr_in $peername
85 or $self->err(500, "unable to decode peername"); 114 or $self->err(500, "unable to decode peername");
115
86 $self->{remote_addr} = inet_ntoa $iaddr; 116 $self->{remote_addr} = inet_ntoa $iaddr;
117 $self->{time} = $::NOW;
87 118
88 # enter ourselves into various lists 119 # enter ourselves into various lists
89 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 120 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
90 121
122 $::conns++;
123
91 $self; 124 $self;
92} 125}
93 126
94sub DESTROY { 127sub DESTROY {
95 my $self = shift; 128 my $self = shift;
129
130 $::conns--;
131
96 delete $conn{$self->{remote_addr}}{$self*1}; 132 delete $conn{$self->{remote_addr}}{$self*1};
97 delete $uri{$self->{uri}}{$self*1}; 133 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
98} 134}
99 135
100sub slog { 136sub slog {
101 my $self = shift; 137 my $self = shift;
102 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 138 main::slog($_[0], "$self->{remote_addr}> $_[1]");
110 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 146 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
111 147
112 while (my ($h, $v) = each %$hdr) { 148 while (my ($h, $v) = each %$hdr) {
113 $res .= "$h: $v\015\012" 149 $res .= "$h: $v\015\012"
114 } 150 }
151 $res .= "\015\012";
115 152
116 $res .= "\015\012$content" if defined $content; 153 $res .= $content if defined $content and $self->{method} ne "HEAD";
117 154
118 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 155 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d#
119 156
157 $self->{written} +=
120 print {$self->{fh}} $res; 158 print {$self->{fh}} $res;
121} 159}
122 160
123sub err { 161sub err {
124 my $self = shift; 162 my $self = shift;
125 my ($code, $msg, $hdr, $content) = @_; 163 my ($code, $msg, $hdr, $content) = @_;
137 175
138sub err_blocked { 176sub err_blocked {
139 my $self = shift; 177 my $self = shift;
140 my $ip = $self->{remote_addr}; 178 my $ip = $self->{remote_addr};
141 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 179 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
180
181 Coro::Event::do_timer(after => 15);
182
142 $self->err(403, "too many connections", 183 $self->err(401, "too many connections",
143 { 184 {
144 "Content-Type" => "text/html", 185 "Content-Type" => "text/html",
145 "Retry-After" => $::BLOCKTIME 186 "Retry-After" => $::BLOCKTIME
146 }, 187 },
147 <<EOF); 188 <<EOF);
180 221
181 delete $blocked{$ip}; 222 delete $blocked{$ip};
182 } 223 }
183 224
184 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 225 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
185 $self->slog("blocked ip $ip"); 226 $self->slog(2, "blocked ip $ip");
186 $self->err_blocked; 227 $self->err_blocked;
187 } 228 }
188 229
189 $req =~ /^(?:\015\012)? 230 $req =~ /^(?:\015\012)?
190 (GET|HEAD) \040+ 231 (GET|HEAD) \040+
191 ([^\040]+) \040+ 232 ([^\040]+) \040+
192 HTTP\/([0-9]+\.[0-9]+) 233 HTTP\/([0-9]+\.[0-9]+)
193 \015\012/gx 234 \015\012/gx
194 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 235 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
195 236
196 $2 ne "1.0" 237 $2 ne "1.0"
197 or $self->err(506, "http protocol version not supported"); 238 or $self->err(506, "http protocol version not supported");
198 239
199 $self->{method} = $1; 240 $self->{method} = $1;
218 while ($h, $v) = each %hdr; 259 while ($h, $v) = each %hdr;
219 } 260 }
220 261
221 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 262 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
222 263
223 weaken ($uri{$self->{uri}}{$self*1} = $self); 264 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
224 265
225 $self->map_uri; 266 $self->map_uri;
226 $self->respond; 267 $self->respond;
227 #} 268 #}
228} 269}
244 285
245 $self->{name} = $uri; 286 $self->{name} = $uri;
246 287
247 # now do the path mapping 288 # now do the path mapping
248 $self->{path} = "$::DOCROOT/$host$uri"; 289 $self->{path} = "$::DOCROOT/$host$uri";
290
291 $self->access_check;
249} 292}
250 293
251sub server_address { 294sub server_address {
252 my $self = shift; 295 my $self = shift;
253 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname 296 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname
287 if (chdir $::DOCROOT) { 330 if (chdir $::DOCROOT) {
288 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 331 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
289 $ENV{HTTP_HOST} = $self->server_host; 332 $ENV{HTTP_HOST} = $self->server_host;
290 $ENV{HTTP_PORT} = $self->{server_host}; 333 $ENV{HTTP_PORT} = $self->{server_host};
291 $ENV{SCRIPT_NAME} = $self->{name}; 334 $ENV{SCRIPT_NAME} = $self->{name};
292 exec $::INDEXPROG; 335 exec $path;
293 } 336 }
294 Coro::State::_exit(0); 337 Coro::State::_exit(0);
295 } else { 338 } else {
296 } 339 }
297} 340}
300 my $self = shift; 343 my $self = shift;
301 my $path = $self->{path}; 344 my $path = $self->{path};
302 345
303 stat $path 346 stat $path
304 or $self->err(404, "not found"); 347 or $self->err(404, "not found");
348
349 $self->{stat} = [stat _];
305 350
306 # idiotic netscape sends idiotic headers AGAIN 351 # idiotic netscape sends idiotic headers AGAIN
307 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 352 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
308 ? str2time $1 : 0; 353 ? str2time $1 : 0;
309 354
312 if ($path !~ /\/$/) { 357 if ($path !~ /\/$/) {
313 # create a redirect to get the trailing "/" 358 # create a redirect to get the trailing "/"
314 my $host = $self->server_hostport; 359 my $host = $self->server_hostport;
315 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 360 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
316 } else { 361 } else {
317 $ims < (stat _)[9] 362 $ims < $self->{stat}[9]
318 or $self->err(304, "not modified"); 363 or $self->err(304, "not modified");
319 364
320 if ($self->{method} eq "GET") { 365 if ($self->{method} eq "GET") {
321 if (-r "$path/index.html") { 366 if (-r "$path/index.html") {
322 $self->{path} .= "/index.html"; 367 $self->{path} .= "/index.html";
334 } 379 }
335} 380}
336 381
337sub handle_dir { 382sub handle_dir {
338 my $self = shift; 383 my $self = shift;
339 $self->_cgi($::INDEXPROG); 384 my $idx = $self->diridx;
385
386 $self->response(200, "ok",
387 {
388 "Content-Type" => "text/html",
389 "Content-Length" => length $idx,
390 },
391 $idx);
340} 392}
341 393
342sub handle_file { 394sub handle_file {
343 my $self = shift; 395 my $self = shift;
344 my $length = -s _; 396 my $length = -s _;
364 $hdr->{"Content-Range"} = "bytes */$length"; 416 $hdr->{"Content-Range"} = "bytes */$length";
365 $self->err(416, "not satisfiable", $hdr); 417 $self->err(416, "not satisfiable", $hdr);
366 418
367satisfiable: 419satisfiable:
368 # check for segmented downloads 420 # check for segmented downloads
369 if ($l && $NO_SEGMENTED) { 421 if ($l && $::NO_SEGMENTED) {
370 if (%{$uri{$self->{uri}}} > 1) { 422 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
371 $self->slog("segmented download refused\n"); 423 Coro::Event::do_timer(after => 15);
424
372 $self->err(400, "segmented downloads are not allowed"); 425 $self->err(400, "segmented downloads are not allowed");
373 } 426 }
374 } 427 }
375 428
376 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 429 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
380ignore: 433ignore:
381 } else { 434 } else {
382 ($l, $h) = (0, $length - 1); 435 ($l, $h) = (0, $length - 1);
383 } 436 }
384 437
385 if ($self->{path} =~ /\.html$/) { 438 $self->{path} =~ /\.([^.]+)$/;
386 $hdr->{"Content-Type"} = "text/html";
387 } else {
388 $hdr->{"Content-Type"} = "application/octet-stream"; 439 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
389 }
390
391 $hdr->{"Content-Length"} = $length; 440 $hdr->{"Content-Length"} = $length;
392 441
393 $self->response(@code, $hdr, ""); 442 $self->response(@code, $hdr, "");
394 443
395 if ($self->{method} eq "GET") { 444 if ($self->{method} eq "GET") {
396 my ($fh, $buf); 445 my ($fh, $buf, $r);
446 my $current = $Coro::current;
397 open $fh, "<", $self->{path} 447 open $fh, "<", $self->{path}
398 or die "$self->{path}: late open failure ($!)"; 448 or die "$self->{path}: late open failure ($!)";
399 449
400 if ($l) {
401 sysseek $fh, $l, 0
402 or die "$self->{path}: cannot seek to $l ($!)";
403 }
404
405 $h -= $l - 1; 450 $h -= $l - 1;
406 451
407 while ($h > 0) { 452 while ($h > 0) {
408 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; 453 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
454 $buf, 0, sub {
455 $r = $_[0];
456 $current->ready;
457 });
458 &Coro::schedule;
459 last unless $r;
409 $self->{fh}->syswrite($buf) 460 my $w = $self->{fh}->syswrite($buf)
410 or last; 461 or last;
462 $::written += $w;
463 $self->{written} += $w;
464 $l += $r;
411 } 465 }
412 } 466 }
413 467
414 close $fh; 468 close $fh;
415} 469}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines