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.9 by root, Sat Aug 11 12:49:21 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;
78our %mimetype; 91our %mimetype;
79 92
80sub read_mimetypes { 93sub read_mimetypes {
81 local *M; 94 local *M;
82 if (open M, "<mimetypes") { 95 if (open M, "<mime_types") {
83 while (<M>) { 96 while (<M>) {
84 if (/^([^#]\S+)\t+(\S+)$/) { 97 if (/^([^#]\S+)\t+(\S+)$/) {
85 $mimetype{lc $1} = $2; 98 $mimetype{lc $1} = $2;
86 } 99 }
87 } 100 }
88 } else { 101 } else {
89 $self->slog(1, "cannot open mimetypes\n"); 102 print "cannot open mime_types\n";
90 } 103 }
91} 104}
105
106read_mimetypes;
92 107
93sub new { 108sub new {
94 my $class = shift; 109 my $class = shift;
95 my $peername = shift; 110 my $peername = shift;
96 my $fh = shift; 111 my $fh = shift;
97 my $self = bless { fh => $fh }, $class; 112 my $self = bless { fh => $fh }, $class;
98 my (undef, $iaddr) = unpack_sockaddr_in $peername 113 my (undef, $iaddr) = unpack_sockaddr_in $peername
99 or $self->err(500, "unable to decode peername"); 114 or $self->err(500, "unable to decode peername");
100 115
101 $self->{remote_addr} = inet_ntoa $iaddr; 116 $self->{remote_addr} = inet_ntoa $iaddr;
117 $self->{time} = $::NOW;
102 118
103 # enter ourselves into various lists 119 # enter ourselves into various lists
104 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 120 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
105 121
122 $::conns++;
123
106 $self; 124 $self;
107} 125}
108 126
109sub DESTROY { 127sub DESTROY {
110 my $self = shift; 128 my $self = shift;
129
130 $::conns--;
131
111 delete $conn{$self->{remote_addr}}{$self*1}; 132 delete $conn{$self->{remote_addr}}{$self*1};
112 delete $uri{$self->{uri}}{$self*1}; 133 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
113} 134}
114 135
115sub slog { 136sub slog {
116 my $self = shift; 137 my $self = shift;
117 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 138 main::slog($_[0], "$self->{remote_addr}> $_[1]");
125 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 146 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
126 147
127 while (my ($h, $v) = each %$hdr) { 148 while (my ($h, $v) = each %$hdr) {
128 $res .= "$h: $v\015\012" 149 $res .= "$h: $v\015\012"
129 } 150 }
151 $res .= "\015\012";
130 152
131 $res .= "\015\012$content" if defined $content; 153 $res .= $content if defined $content and $self->{method} ne "HEAD";
132 154
133 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#
134 156
157 $self->{written} +=
135 print {$self->{fh}} $res; 158 print {$self->{fh}} $res;
136} 159}
137 160
138sub err { 161sub err {
139 my $self = shift; 162 my $self = shift;
140 my ($code, $msg, $hdr, $content) = @_; 163 my ($code, $msg, $hdr, $content) = @_;
152 175
153sub err_blocked { 176sub err_blocked {
154 my $self = shift; 177 my $self = shift;
155 my $ip = $self->{remote_addr}; 178 my $ip = $self->{remote_addr};
156 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 179 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
180
181 Coro::Event::do_timer(after => 15);
182
157 $self->err(403, "too many connections", 183 $self->err(401, "too many connections",
158 { 184 {
159 "Content-Type" => "text/html", 185 "Content-Type" => "text/html",
160 "Retry-After" => $::BLOCKTIME 186 "Retry-After" => $::BLOCKTIME
161 }, 187 },
162 <<EOF); 188 <<EOF);
195 221
196 delete $blocked{$ip}; 222 delete $blocked{$ip};
197 } 223 }
198 224
199 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 225 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
200 $self->slog("blocked ip $ip"); 226 $self->slog(2, "blocked ip $ip");
201 $self->err_blocked; 227 $self->err_blocked;
202 } 228 }
203 229
204 $req =~ /^(?:\015\012)? 230 $req =~ /^(?:\015\012)?
205 (GET|HEAD) \040+ 231 (GET|HEAD) \040+
206 ([^\040]+) \040+ 232 ([^\040]+) \040+
207 HTTP\/([0-9]+\.[0-9]+) 233 HTTP\/([0-9]+\.[0-9]+)
208 \015\012/gx 234 \015\012/gx
209 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 235 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
210 236
211 $2 ne "1.0" 237 $2 ne "1.0"
212 or $self->err(506, "http protocol version not supported"); 238 or $self->err(506, "http protocol version not supported");
213 239
214 $self->{method} = $1; 240 $self->{method} = $1;
233 while ($h, $v) = each %hdr; 259 while ($h, $v) = each %hdr;
234 } 260 }
235 261
236 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 262 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
237 263
238 weaken ($uri{$self->{uri}}{$self*1} = $self); 264 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
239 265
240 $self->map_uri; 266 $self->map_uri;
241 $self->respond; 267 $self->respond;
242 #} 268 #}
243} 269}
304 if (chdir $::DOCROOT) { 330 if (chdir $::DOCROOT) {
305 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 331 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
306 $ENV{HTTP_HOST} = $self->server_host; 332 $ENV{HTTP_HOST} = $self->server_host;
307 $ENV{HTTP_PORT} = $self->{server_host}; 333 $ENV{HTTP_PORT} = $self->{server_host};
308 $ENV{SCRIPT_NAME} = $self->{name}; 334 $ENV{SCRIPT_NAME} = $self->{name};
309 exec $::INDEXPROG; 335 exec $path;
310 } 336 }
311 Coro::State::_exit(0); 337 Coro::State::_exit(0);
312 } else { 338 } else {
313 } 339 }
314} 340}
317 my $self = shift; 343 my $self = shift;
318 my $path = $self->{path}; 344 my $path = $self->{path};
319 345
320 stat $path 346 stat $path
321 or $self->err(404, "not found"); 347 or $self->err(404, "not found");
348
349 $self->{stat} = [stat _];
322 350
323 # idiotic netscape sends idiotic headers AGAIN 351 # idiotic netscape sends idiotic headers AGAIN
324 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 352 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
325 ? str2time $1 : 0; 353 ? str2time $1 : 0;
326 354
329 if ($path !~ /\/$/) { 357 if ($path !~ /\/$/) {
330 # create a redirect to get the trailing "/" 358 # create a redirect to get the trailing "/"
331 my $host = $self->server_hostport; 359 my $host = $self->server_hostport;
332 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 360 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
333 } else { 361 } else {
334 $ims < (stat _)[9] 362 $ims < $self->{stat}[9]
335 or $self->err(304, "not modified"); 363 or $self->err(304, "not modified");
336 364
337 if ($self->{method} eq "GET") { 365 if ($self->{method} eq "GET") {
338 if (-r "$path/index.html") { 366 if (-r "$path/index.html") {
339 $self->{path} .= "/index.html"; 367 $self->{path} .= "/index.html";
351 } 379 }
352} 380}
353 381
354sub handle_dir { 382sub handle_dir {
355 my $self = shift; 383 my $self = shift;
356 $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);
357} 392}
358 393
359sub handle_file { 394sub handle_file {
360 my $self = shift; 395 my $self = shift;
361 my $length = -s _; 396 my $length = -s _;
381 $hdr->{"Content-Range"} = "bytes */$length"; 416 $hdr->{"Content-Range"} = "bytes */$length";
382 $self->err(416, "not satisfiable", $hdr); 417 $self->err(416, "not satisfiable", $hdr);
383 418
384satisfiable: 419satisfiable:
385 # check for segmented downloads 420 # check for segmented downloads
386 if ($l && $NO_SEGMENTED) { 421 if ($l && $::NO_SEGMENTED) {
387 if (%{$uri{$self->{uri}}} > 1) { 422 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
388 $self->slog("segmented download refused\n"); 423 Coro::Event::do_timer(after => 15);
424
389 $self->err(400, "segmented downloads are not allowed"); 425 $self->err(400, "segmented downloads are not allowed");
390 } 426 }
391 } 427 }
392 428
393 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 429 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
404 $hdr->{"Content-Length"} = $length; 440 $hdr->{"Content-Length"} = $length;
405 441
406 $self->response(@code, $hdr, ""); 442 $self->response(@code, $hdr, "");
407 443
408 if ($self->{method} eq "GET") { 444 if ($self->{method} eq "GET") {
409 my ($fh, $buf); 445 my ($fh, $buf, $r);
446 my $current = $Coro::current;
410 open $fh, "<", $self->{path} 447 open $fh, "<", $self->{path}
411 or die "$self->{path}: late open failure ($!)"; 448 or die "$self->{path}: late open failure ($!)";
412 449
413 if ($l) {
414 sysseek $fh, $l, 0
415 or die "$self->{path}: cannot seek to $l ($!)";
416 }
417
418 $h -= $l - 1; 450 $h -= $l - 1;
419 451
420 while ($h > 0) { 452 while ($h > 0) {
421 $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;
422 $self->{fh}->syswrite($buf) 460 my $w = $self->{fh}->syswrite($buf)
423 or last; 461 or last;
462 $::written += $w;
463 $self->{written} += $w;
464 $l += $r;
424 } 465 }
425 } 466 }
426 467
427 close $fh; 468 close $fh;
428} 469}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines