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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines