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.16 by root, Tue Aug 14 19:38:25 2001 UTC vs.
Revision 1.23 by root, Sat Aug 18 02:58:38 2001 UTC

75use Linux::AIO; 75use Linux::AIO;
76 76
77Linux::AIO::min_parallel $::AIO_PARALLEL; 77Linux::AIO::min_parallel $::AIO_PARALLEL;
78 78
79Event->io(fd => Linux::AIO::poll_fileno, 79Event->io(fd => Linux::AIO::poll_fileno,
80 poll => 'r', 80 poll => 'r', async => 1,
81 async => 1,
82 cb => \&Linux::AIO::poll_cb ); 81 cb => \&Linux::AIO::poll_cb);
83
84Event->add_hooks(prepare => sub {
85 &Coro::cede while &Coro::nready;
86 1e6;
87});
88 82
89our %conn; # $conn{ip}{fh} => connobj 83our %conn; # $conn{ip}{fh} => connobj
90our %blocked; 84our %blocked;
91our %mimetype; 85our %mimetype;
92 86
127sub DESTROY { 121sub DESTROY {
128 my $self = shift; 122 my $self = shift;
129 123
130 $::conns--; 124 $::conns--;
131 125
126 $self->eoconn;
132 delete $conn{$self->{remote_addr}}{$self*1}; 127 delete $conn{$self->{remote_addr}}{$self*1};
128}
129
130# end of connection
131sub eoconn {
133 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 132 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
134} 133}
135 134
136sub slog { 135sub slog {
137 my $self = shift; 136 my $self = shift;
138 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 137 main::slog($_[0], "$self->{remote_addr}> $_[1]");
139} 138}
140 139
141sub response { 140sub response {
142 my ($self, $code, $msg, $hdr, $content) = @_; 141 my ($self, $code, $msg, $hdr, $content) = @_;
143 my $res = "HTTP/1.0 $code $msg\015\012"; 142 my $res = "HTTP/1.1 $code $msg\015\012";
144 143
145 $res .= "Connection: close\015\012"; 144 #$res .= "Connection: close\015\012";
146 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 145 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
147 146
148 while (my ($h, $v) = each %$hdr) { 147 while (my ($h, $v) = each %$hdr) {
149 $res .= "$h: $v\015\012" 148 $res .= "$h: $v\015\012"
150 } 149 }
165 unless (defined $content) { 164 unless (defined $content) {
166 $content = "$code $msg"; 165 $content = "$code $msg";
167 $hdr->{"Content-Type"} = "text/plain"; 166 $hdr->{"Content-Type"} = "text/plain";
168 $hdr->{"Content-Length"} = length $content; 167 $hdr->{"Content-Length"} = length $content;
169 } 168 }
169 $hdr->{"Connection"} = "close";
170 170
171 $self->response($code, $msg, $hdr, $content); 171 $self->response($code, $msg, $hdr, $content);
172 172
173 die bless {}, err::; 173 die bless {}, err::;
174} 174}
176sub err_blocked { 176sub err_blocked {
177 my $self = shift; 177 my $self = shift;
178 my $ip = $self->{remote_addr}; 178 my $ip = $self->{remote_addr};
179 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 179 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
180 180
181 Coro::Event::do_timer(after => 15); 181 Coro::Event::do_timer(after => 20*rand);
182 182
183 $self->err(401, "too many connections", 183 $self->err(401, "too many connections",
184 { 184 {
185 "Content-Type" => "text/html", 185 "Content-Type" => "text/html",
186 "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\"",
187 }, 189 },
188 <<EOF); 190 <<EOF);
189<html><p> 191<html><p>
190You have been blocked because you opened too many connections. You 192You have been blocked because you opened too many connections. You
191may retry at</p> 193may retry at</p>
200 202
201sub handle { 203sub handle {
202 my $self = shift; 204 my $self = shift;
203 my $fh = $self->{fh}; 205 my $fh = $self->{fh};
204 206
207 $fh->timeout($::REQ_TIMEOUT);
205 #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
206 $self->{h} = {}; 222 $self->{h} = {};
207 223
208 # read request and parse first line
209 $fh->timeout($::REQ_TIMEOUT);
210 my $req = $fh->readline("\015\012\015\012");
211 $fh->timeout($::RES_TIMEOUT); 224 $fh->timeout($::RES_TIMEOUT);
212
213 defined $req or
214 $self->err(408, "request timeout");
215
216 my $ip = $self->{remote_addr}; 225 my $ip = $self->{remote_addr};
217 226
218 if ($blocked{$ip}) { 227 if ($blocked{$ip}) {
219 $self->err_blocked($blocked{$ip}) 228 $self->err_blocked($blocked{$ip})
220 if $blocked{$ip} > $::NOW; 229 if $blocked{$ip} > $::NOW;
232 ([^\040]+) \040+ 241 ([^\040]+) \040+
233 HTTP\/([0-9]+\.[0-9]+) 242 HTTP\/([0-9]+\.[0-9]+)
234 \015\012/gx 243 \015\012/gx
235 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); 244 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
236 245
237 $2 ne "1.0"
238 or $self->err(506, "http protocol version not supported");
239
240 $self->{method} = $1; 246 $self->{method} = $1;
241 $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");
242 252
243 # parse headers 253 # parse headers
244 { 254 {
245 my (%hdr, $h, $v); 255 my (%hdr, $h, $v);
246 256
263 273
264 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 274 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
265 275
266 $self->map_uri; 276 $self->map_uri;
267 $self->respond; 277 $self->respond;
278
279 $self->eoconn;
280
281 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
282
283 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
284 $fh->timeout($::PER_TIMEOUT);
268 #} 285 }
269} 286}
270 287
271# uri => path mapping 288# uri => path mapping
272sub map_uri { 289sub map_uri {
273 my $self = shift; 290 my $self = shift;
412 goto ignore; 429 goto ignore;
413 } 430 }
414 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 431 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l;
415 } 432 }
416 $hdr->{"Content-Range"} = "bytes */$length"; 433 $hdr->{"Content-Range"} = "bytes */$length";
434 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
417 $self->err(416, "not satisfiable", $hdr); 435 $self->err(416, "not satisfiable", $hdr);
418 436
419satisfiable: 437satisfiable:
420 # check for segmented downloads 438 # check for segmented downloads
421 if ($l && $::NO_SEGMENTED) { 439 if ($l && $::NO_SEGMENTED) {
447 open $fh, "<", $self->{path} 465 open $fh, "<", $self->{path}
448 or die "$self->{path}: late open failure ($!)"; 466 or die "$self->{path}: late open failure ($!)";
449 467
450 $h -= $l - 1; 468 $h -= $l - 1;
451 469
470 if (0) {
471 if ($l) {
472 sysseek $fh, $l, 0;
473 }
474 }
475
452 while ($h > 0) { 476 while ($h > 0) {
477 if (0) {
478 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
479 or last;
480 } else {
453 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h), 481 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
454 $buf, 0, sub { 482 $buf, 0, sub {
455 $r = $_[0]; 483 $r = $_[0];
456 $current->ready; 484 $current->ready;
457 }); 485 });
458 &Coro::schedule; 486 &Coro::schedule;
459 last unless $r; 487 last unless $r;
488 }
460 my $w = $self->{fh}->syswrite($buf) 489 my $w = $self->{fh}->syswrite($buf)
461 or last; 490 or last;
462 $::written += $w; 491 $::written += $w;
463 $self->{written} += $w; 492 $self->{written} += $w;
464 $l += $r; 493 $l += $r;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines