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.13 by root, Sun Aug 12 01:16:48 2001 UTC vs.
Revision 1.25 by root, Sun Aug 19 22:14:26 2001 UTC

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
114sub DESTROY { 121sub DESTROY {
115 my $self = shift; 122 my $self = shift;
116 123
117 $::conns--; 124 $::conns--;
118 125
126 $self->eoconn;
119 delete $conn{$self->{remote_addr}}{$self*1}; 127 delete $conn{$self->{remote_addr}}{$self*1};
128}
129
130# end of connection
131sub eoconn {
120 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 132 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
121} 133}
122 134
123sub slog { 135sub slog {
124 my $self = shift; 136 my $self = shift;
125 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 137 main::slog($_[0], "$self->{remote_addr}> $_[1]");
126} 138}
127 139
128sub response { 140sub response {
129 my ($self, $code, $msg, $hdr, $content) = @_; 141 my ($self, $code, $msg, $hdr, $content) = @_;
130 my $res = "HTTP/1.0 $code $msg\015\012"; 142 my $res = "HTTP/1.1 $code $msg\015\012";
131 143
132 $res .= "Connection: close\015\012"; 144 #$res .= "Connection: close\015\012";
133 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 145 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
134 146
135 while (my ($h, $v) = each %$hdr) { 147 while (my ($h, $v) = each %$hdr) {
136 $res .= "$h: $v\015\012" 148 $res .= "$h: $v\015\012"
137 } 149 }
138 $res .= "\015\012"; 150 $res .= "\015\012";
139 151
140 $res .= $content if defined $content and $self->{method} ne "HEAD"; 152 $res .= $content if defined $content and $self->{method} ne "HEAD";
141 153
142 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";
143 155
144 $self->{written} += 156 $self->{written} +=
145 print {$self->{fh}} $res; 157 print {$self->{fh}} $res;
146} 158}
147 159
152 unless (defined $content) { 164 unless (defined $content) {
153 $content = "$code $msg"; 165 $content = "$code $msg";
154 $hdr->{"Content-Type"} = "text/plain"; 166 $hdr->{"Content-Type"} = "text/plain";
155 $hdr->{"Content-Length"} = length $content; 167 $hdr->{"Content-Length"} = length $content;
156 } 168 }
169 $hdr->{"Connection"} = "close";
157 170
158 $self->response($code, $msg, $hdr, $content); 171 $self->response($code, $msg, $hdr, $content);
159 172
160 die bless {}, err::; 173 die bless {}, err::;
161} 174}
163sub err_blocked { 176sub err_blocked {
164 my $self = shift; 177 my $self = shift;
165 my $ip = $self->{remote_addr}; 178 my $ip = $self->{remote_addr};
166 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 179 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
167 180
181 Coro::Event::do_timer(after => 20*rand);
182
168 $self->err(403, "too many connections", 183 $self->err(401, "too many connections",
169 { 184 {
170 "Content-Type" => "text/html", 185 "Content-Type" => "text/html",
171 "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\"",
172 }, 189 },
173 <<EOF); 190 <<EOF);
174<html><p> 191<html><p>
175You have been blocked because you opened too many connections. You 192You have been blocked because you opened too many connections. You
176may retry at</p> 193may retry at</p>
185 202
186sub handle { 203sub handle {
187 my $self = shift; 204 my $self = shift;
188 my $fh = $self->{fh}; 205 my $fh = $self->{fh};
189 206
207 $fh->timeout($::REQ_TIMEOUT);
190 #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
191 $self->{h} = {}; 222 $self->{h} = {};
192 223
193 # read request and parse first line
194 $fh->timeout($::REQ_TIMEOUT);
195 my $req = $fh->readline("\015\012\015\012");
196 $fh->timeout($::RES_TIMEOUT); 224 $fh->timeout($::RES_TIMEOUT);
197
198 defined $req or
199 $self->err(408, "request timeout");
200
201 my $ip = $self->{remote_addr}; 225 my $ip = $self->{remote_addr};
202 226
203 if ($blocked{$ip}) { 227 if ($blocked{$ip}) {
204 $self->err_blocked($blocked{$ip}) 228 $self->err_blocked($blocked{$ip})
205 if $blocked{$ip} > $::NOW; 229 if $blocked{$ip} > $::NOW;
215 $req =~ /^(?:\015\012)? 239 $req =~ /^(?:\015\012)?
216 (GET|HEAD) \040+ 240 (GET|HEAD) \040+
217 ([^\040]+) \040+ 241 ([^\040]+) \040+
218 HTTP\/([0-9]+\.[0-9]+) 242 HTTP\/([0-9]+\.[0-9]+)
219 \015\012/gx 243 \015\012/gx
220 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 244 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
221
222 $2 ne "1.0"
223 or $self->err(506, "http protocol version not supported");
224 245
225 $self->{method} = $1; 246 $self->{method} = $1;
226 $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");
227 252
228 # parse headers 253 # parse headers
229 { 254 {
230 my (%hdr, $h, $v); 255 my (%hdr, $h, $v);
231 256
246 271
247 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 272 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
248 273
249 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 274 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
250 275
276 eval {
251 $self->map_uri; 277 $self->map_uri;
252 $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);
253 #} 289 }
254} 290}
255 291
256# uri => path mapping 292# uri => path mapping
257sub map_uri { 293sub map_uri {
258 my $self = shift; 294 my $self = shift;
345 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 381 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
346 } else { 382 } else {
347 $ims < $self->{stat}[9] 383 $ims < $self->{stat}[9]
348 or $self->err(304, "not modified"); 384 or $self->err(304, "not modified");
349 385
350 if ($self->{method} eq "GET") {
351 if (-r "$path/index.html") { 386 if (-r "$path/index.html") {
352 $self->{path} .= "/index.html"; 387 $self->{path} .= "/index.html";
353 $self->handle_file; 388 $self->handle_file;
354 } else { 389 } else {
355 $self->handle_dir; 390 $self->handle_dir;
356 }
357 } 391 }
358 } 392 }
359 } elsif (-f _ && -r _) { 393 } elsif (-f _ && -r _) {
360 -x _ and $self->err(403, "forbidden"); 394 -x _ and $self->err(403, "forbidden");
361 $self->handle_file; 395 $self->handle_file;
397 goto ignore; 431 goto ignore;
398 } 432 }
399 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 433 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l;
400 } 434 }
401 $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"}.")");
402 $self->err(416, "not satisfiable", $hdr); 438 $self->err(416, "not satisfiable", $hdr, "");
403 439
404satisfiable: 440satisfiable:
405 # check for segmented downloads 441 # check for segmented downloads
406 if ($l && $::NO_SEGMENTED) { 442 if ($l && $::NO_SEGMENTED) {
407 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 443 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
444 Coro::Event::do_timer(after => 15);
445
408 $self->err(400, "segmented downloads are not allowed"); 446 $self->err(400, "segmented downloads are not allowed");
409 } 447 }
410 } 448 }
411 449
412 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 450 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
423 $hdr->{"Content-Length"} = $length; 461 $hdr->{"Content-Length"} = $length;
424 462
425 $self->response(@code, $hdr, ""); 463 $self->response(@code, $hdr, "");
426 464
427 if ($self->{method} eq "GET") { 465 if ($self->{method} eq "GET") {
428 my ($fh, $buf); 466 my ($fh, $buf, $r);
467 my $current = $Coro::current;
429 open $fh, "<", $self->{path} 468 open $fh, "<", $self->{path}
430 or die "$self->{path}: late open failure ($!)"; 469 or die "$self->{path}: late open failure ($!)";
431 470
432 if ($l) {
433 sysseek $fh, $l, 0
434 or die "$self->{path}: cannot seek to $l ($!)";
435 }
436
437 $h -= $l - 1; 471 $h -= $l - 1;
438 472
473 if (0) {
474 if ($l) {
475 sysseek $fh, $l, 0;
476 }
477 }
478
439 while ($h > 0) { 479 while ($h > 0) {
480 if (0) {
440 $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 }
441 my $w = $self->{fh}->syswrite($buf) 492 my $w = $self->{fh}->syswrite($buf)
442 or last; 493 or last;
443 $::written += $w; 494 $::written += $w;
444 $self->{written} += $w; 495 $self->{written} += $w;
496 $l += $r;
445 } 497 }
446 } 498 }
447 499
448 close $fh; 500 close $fh;
449} 501}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines