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.14 by root, Sun Aug 12 14:09:03 2001 UTC vs.
Revision 1.26 by root, Sun Aug 19 22:59:35 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;
75 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);
82
76our %conn; # $conn{ip}{fh} => connobj 83our %conn; # $conn{ip}{self} => connobj
84our %uri; # $uri{ip}{uri}{self}
77our %blocked; 85our %blocked;
78our %mimetype; 86our %mimetype;
79 87
80sub read_mimetypes { 88sub read_mimetypes {
81 local *M; 89 local *M;
114sub DESTROY { 122sub DESTROY {
115 my $self = shift; 123 my $self = shift;
116 124
117 $::conns--; 125 $::conns--;
118 126
127 $self->eoconn;
119 delete $conn{$self->{remote_addr}}{$self*1}; 128 delete $conn{$self->{remote_addr}}{$self*1};
129}
130
131# end of connection
132sub eoconn {
133 my $self = shift;
120 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 134 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
121} 135}
122 136
123sub slog { 137sub slog {
124 my $self = shift; 138 my $self = shift;
125 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 139 main::slog($_[0], "$self->{remote_addr}> $_[1]");
126} 140}
127 141
128sub response { 142sub response {
129 my ($self, $code, $msg, $hdr, $content) = @_; 143 my ($self, $code, $msg, $hdr, $content) = @_;
130 my $res = "HTTP/1.0 $code $msg\015\012"; 144 my $res = "HTTP/1.1 $code $msg\015\012";
131 145
132 $res .= "Connection: close\015\012"; 146 #$res .= "Connection: close\015\012";
133 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 147 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
134 148
135 while (my ($h, $v) = each %$hdr) { 149 while (my ($h, $v) = each %$hdr) {
136 $res .= "$h: $v\015\012" 150 $res .= "$h: $v\015\012"
137 } 151 }
138 $res .= "\015\012"; 152 $res .= "\015\012";
139 153
140 $res .= $content if defined $content and $self->{method} ne "HEAD"; 154 $res .= $content if defined $content and $self->{method} ne "HEAD";
141 155
142 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 156 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";
143 157
144 $self->{written} += 158 $self->{written} +=
145 print {$self->{fh}} $res; 159 print {$self->{fh}} $res;
146} 160}
147 161
152 unless (defined $content) { 166 unless (defined $content) {
153 $content = "$code $msg"; 167 $content = "$code $msg";
154 $hdr->{"Content-Type"} = "text/plain"; 168 $hdr->{"Content-Type"} = "text/plain";
155 $hdr->{"Content-Length"} = length $content; 169 $hdr->{"Content-Length"} = length $content;
156 } 170 }
171 $hdr->{"Connection"} = "close";
157 172
158 $self->response($code, $msg, $hdr, $content); 173 $self->response($code, $msg, $hdr, $content);
159 174
160 die bless {}, err::; 175 die bless {}, err::;
161} 176}
163sub err_blocked { 178sub err_blocked {
164 my $self = shift; 179 my $self = shift;
165 my $ip = $self->{remote_addr}; 180 my $ip = $self->{remote_addr};
166 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 181 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
167 182
168 Coro::Event::do_timer(after => 15); 183 Coro::Event::do_timer(after => 20*rand);
169 184
170 $self->err(401, "too many connections", 185 $self->err(401, "too many connections",
171 { 186 {
172 "Content-Type" => "text/html", 187 "Content-Type" => "text/html",
173 "Retry-After" => $::BLOCKTIME 188 "Retry-After" => $::BLOCKTIME,
189 "Warning" => "Please do NOT retry, you have been blocked",
190 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
174 }, 191 },
175 <<EOF); 192 <<EOF);
176<html><p> 193<html><p>
177You have been blocked because you opened too many connections. You 194You have been blocked because you opened too many connections. You
178may retry at</p> 195may retry at</p>
187 204
188sub handle { 205sub handle {
189 my $self = shift; 206 my $self = shift;
190 my $fh = $self->{fh}; 207 my $fh = $self->{fh};
191 208
209 $fh->timeout($::REQ_TIMEOUT);
192 #while() { 210 while() {
211 $self->{reqs}++;
212
213 # read request and parse first line
214 my $req = $fh->readline("\015\012\015\012");
215
216 unless (defined $req) {
217 if (exists $self->{version}) {
218 last;
219 } else {
220 $self->err(408, "request timeout");
221 }
222 }
223
193 $self->{h} = {}; 224 $self->{h} = {};
194 225
195 # read request and parse first line
196 $fh->timeout($::REQ_TIMEOUT);
197 my $req = $fh->readline("\015\012\015\012");
198 $fh->timeout($::RES_TIMEOUT); 226 $fh->timeout($::RES_TIMEOUT);
199
200 defined $req or
201 $self->err(408, "request timeout");
202
203 my $ip = $self->{remote_addr}; 227 my $ip = $self->{remote_addr};
204 228
205 $self->err_blocked($blocked{$ip});
206 if ($blocked{$ip}) { 229 if ($blocked{$ip}) {
207 $self->err_blocked($blocked{$ip}) 230 $self->err_blocked($blocked{$ip})
208 if $blocked{$ip} > $::NOW; 231 if $blocked{$ip} > $::NOW;
209 232
210 delete $blocked{$ip}; 233 delete $blocked{$ip};
220 ([^\040]+) \040+ 243 ([^\040]+) \040+
221 HTTP\/([0-9]+\.[0-9]+) 244 HTTP\/([0-9]+\.[0-9]+)
222 \015\012/gx 245 \015\012/gx
223 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); 246 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
224 247
225 $2 ne "1.0"
226 or $self->err(506, "http protocol version not supported");
227
228 $self->{method} = $1; 248 $self->{method} = $1;
229 $self->{uri} = $2; 249 $self->{uri} = $2;
250 $self->{version} = $3;
251
252 $3 =~ /^1\./
253 or $self->err(506, "http protocol version $3 not supported");
230 254
231 # parse headers 255 # parse headers
232 { 256 {
233 my (%hdr, $h, $v); 257 my (%hdr, $h, $v);
234 258
249 273
250 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 274 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
251 275
252 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 276 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
253 277
278 eval {
254 $self->map_uri; 279 $self->map_uri;
255 $self->respond; 280 $self->respond;
281 };
282
283 $self->eoconn;
284
285 die if $@ && !ref $@;
286
287 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
288
289 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
290 $fh->timeout($::PER_TIMEOUT);
256 #} 291 }
257} 292}
258 293
259# uri => path mapping 294# uri => path mapping
260sub map_uri { 295sub map_uri {
261 my $self = shift; 296 my $self = shift;
348 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 383 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
349 } else { 384 } else {
350 $ims < $self->{stat}[9] 385 $ims < $self->{stat}[9]
351 or $self->err(304, "not modified"); 386 or $self->err(304, "not modified");
352 387
353 if ($self->{method} eq "GET") {
354 if (-r "$path/index.html") { 388 if (-r "$path/index.html") {
355 $self->{path} .= "/index.html"; 389 $self->{path} .= "/index.html";
356 $self->handle_file; 390 $self->handle_file;
357 } else { 391 } else {
358 $self->handle_dir; 392 $self->handle_dir;
359 }
360 } 393 }
361 } 394 }
362 } elsif (-f _ && -r _) { 395 } elsif (-f _ && -r _) {
363 -x _ and $self->err(403, "forbidden"); 396 -x _ and $self->err(403, "forbidden");
364 $self->handle_file; 397 $self->handle_file;
397 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 430 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
398 } else { 431 } else {
399 ($l, $h) = (0, $length - 1); 432 ($l, $h) = (0, $length - 1);
400 goto ignore; 433 goto ignore;
401 } 434 }
402 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 435 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
403 } 436 }
404 $hdr->{"Content-Range"} = "bytes */$length"; 437 $hdr->{"Content-Range"} = "bytes */$length";
438 $hdr->{"Content-Length"} = $length;
439 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
405 $self->err(416, "not satisfiable", $hdr); 440 $self->err(416, "not satisfiable", $hdr, "");
406 441
407satisfiable: 442satisfiable:
408 # check for segmented downloads 443 # check for segmented downloads
409 if ($l && $::NO_SEGMENTED) { 444 if ($l && $::NO_SEGMENTED) {
410 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 445 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
411 Coro::Event::do_timer(after => 15);
412
413 $self->err(400, "segmented downloads are not allowed"); 446 $self->err(400, "segmented downloads are not allowed");
414 } 447 }
415 } 448 }
416 449
417 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 450 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
428 $hdr->{"Content-Length"} = $length; 461 $hdr->{"Content-Length"} = $length;
429 462
430 $self->response(@code, $hdr, ""); 463 $self->response(@code, $hdr, "");
431 464
432 if ($self->{method} eq "GET") { 465 if ($self->{method} eq "GET") {
433 my ($fh, $buf); 466 my ($fh, $buf, $r);
467 my $current = $Coro::current;
434 open $fh, "<", $self->{path} 468 open $fh, "<", $self->{path}
435 or die "$self->{path}: late open failure ($!)"; 469 or die "$self->{path}: late open failure ($!)";
436 470
437 if ($l) {
438 sysseek $fh, $l, 0
439 or die "$self->{path}: cannot seek to $l ($!)";
440 }
441
442 $h -= $l - 1; 471 $h -= $l - 1;
443 472
473 if (0) {
474 if ($l) {
475 sysseek $fh, $l, 0;
476 }
477 }
478
444 while ($h > 0) { 479 while ($h > 0) {
480 if (0) {
445 $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 }
446 my $w = $self->{fh}->syswrite($buf) 492 my $w = $self->{fh}->syswrite($buf)
447 or last; 493 or last;
448 $::written += $w; 494 $::written += $w;
449 $self->{written} += $w; 495 $self->{written} += $w;
496 $l += $r;
450 } 497 }
451 } 498 }
452 499
453 close $fh; 500 close $fh;
454} 501}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines