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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines