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.15 by root, Tue Aug 14 02:28:51 2001 UTC vs.
Revision 1.21 by root, Fri Aug 17 03:33:00 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);
82my $scheduler = Event->idle(
83 max => 0, min => 0, prio => 6, parked => 1,
84 cb => \&Coro::schedule);
75 85
76our %conn; # $conn{ip}{fh} => connobj 86our %conn; # $conn{ip}{fh} => connobj
77our %blocked; 87our %blocked;
78our %mimetype; 88our %mimetype;
79 89
114sub DESTROY { 124sub DESTROY {
115 my $self = shift; 125 my $self = shift;
116 126
117 $::conns--; 127 $::conns--;
118 128
129 $self->eoconn;
119 delete $conn{$self->{remote_addr}}{$self*1}; 130 delete $conn{$self->{remote_addr}}{$self*1};
131}
132
133# end of connection
134sub eoconn {
120 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1}; 135 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
121} 136}
122 137
123sub slog { 138sub slog {
124 my $self = shift; 139 my $self = shift;
125 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 140 main::slog($_[0], "$self->{remote_addr}> $_[1]");
126} 141}
127 142
128sub response { 143sub response {
129 my ($self, $code, $msg, $hdr, $content) = @_; 144 my ($self, $code, $msg, $hdr, $content) = @_;
130 my $res = "HTTP/1.0 $code $msg\015\012"; 145 my $res = "HTTP/1.1 $code $msg\015\012";
131 146
132 $res .= "Connection: close\015\012"; 147 #$res .= "Connection: close\015\012";
133 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 148 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
134 149
135 while (my ($h, $v) = each %$hdr) { 150 while (my ($h, $v) = each %$hdr) {
136 $res .= "$h: $v\015\012" 151 $res .= "$h: $v\015\012"
137 } 152 }
152 unless (defined $content) { 167 unless (defined $content) {
153 $content = "$code $msg"; 168 $content = "$code $msg";
154 $hdr->{"Content-Type"} = "text/plain"; 169 $hdr->{"Content-Type"} = "text/plain";
155 $hdr->{"Content-Length"} = length $content; 170 $hdr->{"Content-Length"} = length $content;
156 } 171 }
172 $hdr->{"Connection"} = "close";
157 173
158 $self->response($code, $msg, $hdr, $content); 174 $self->response($code, $msg, $hdr, $content);
159 175
160 die bless {}, err::; 176 die bless {}, err::;
161} 177}
163sub err_blocked { 179sub err_blocked {
164 my $self = shift; 180 my $self = shift;
165 my $ip = $self->{remote_addr}; 181 my $ip = $self->{remote_addr};
166 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 182 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
167 183
168 Coro::Event::do_timer(after => 15); 184 Coro::Event::do_timer(after => 20*rand);
169 185
170 $self->err(401, "too many connections", 186 $self->err(401, "too many connections",
171 { 187 {
172 "Content-Type" => "text/html", 188 "Content-Type" => "text/html",
173 "Retry-After" => $::BLOCKTIME 189 "Retry-After" => $::BLOCKTIME,
190 "Warning" => "Please do NOT retry, you have been blocked",
191 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
174 }, 192 },
175 <<EOF); 193 <<EOF);
176<html><p> 194<html><p>
177You have been blocked because you opened too many connections. You 195You have been blocked because you opened too many connections. You
178may retry at</p> 196may retry at</p>
187 205
188sub handle { 206sub handle {
189 my $self = shift; 207 my $self = shift;
190 my $fh = $self->{fh}; 208 my $fh = $self->{fh};
191 209
210 $fh->timeout($::REQ_TIMEOUT);
192 #while() { 211 while() {
212 $self->{reqs}++;
213
214 # read request and parse first line
215 my $req = $fh->readline("\015\012\015\012");
216
217 unless (defined $req) {
218 if (exists $self->{version}) {
219 last;
220 } else {
221 $self->err(408, "request timeout");
222 }
223 }
224
193 $self->{h} = {}; 225 $self->{h} = {};
194 226
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); 227 $fh->timeout($::RES_TIMEOUT);
199
200 defined $req or
201 $self->err(408, "request timeout");
202
203 my $ip = $self->{remote_addr}; 228 my $ip = $self->{remote_addr};
204 229
205 if ($blocked{$ip}) { 230 if ($blocked{$ip}) {
206 $self->err_blocked($blocked{$ip}) 231 $self->err_blocked($blocked{$ip})
207 if $blocked{$ip} > $::NOW; 232 if $blocked{$ip} > $::NOW;
219 ([^\040]+) \040+ 244 ([^\040]+) \040+
220 HTTP\/([0-9]+\.[0-9]+) 245 HTTP\/([0-9]+\.[0-9]+)
221 \015\012/gx 246 \015\012/gx
222 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); 247 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
223 248
224 $2 ne "1.0"
225 or $self->err(506, "http protocol version not supported");
226
227 $self->{method} = $1; 249 $self->{method} = $1;
228 $self->{uri} = $2; 250 $self->{uri} = $2;
251 $self->{version} = $3;
252
253 $3 =~ /^1\./
254 or $self->err(506, "http protocol version $3 not supported");
229 255
230 # parse headers 256 # parse headers
231 { 257 {
232 my (%hdr, $h, $v); 258 my (%hdr, $h, $v);
233 259
250 276
251 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 277 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
252 278
253 $self->map_uri; 279 $self->map_uri;
254 $self->respond; 280 $self->respond;
281
282 $self->eoconn;
283
284 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
285
286 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
287 $fh->timeout($::PER_TIMEOUT);
255 #} 288 }
256} 289}
257 290
258# uri => path mapping 291# uri => path mapping
259sub map_uri { 292sub map_uri {
260 my $self = shift; 293 my $self = shift;
399 goto ignore; 432 goto ignore;
400 } 433 }
401 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 434 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l;
402 } 435 }
403 $hdr->{"Content-Range"} = "bytes */$length"; 436 $hdr->{"Content-Range"} = "bytes */$length";
437 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
404 $self->err(416, "not satisfiable", $hdr); 438 $self->err(416, "not satisfiable", $hdr);
405 439
406satisfiable: 440satisfiable:
407 # check for segmented downloads 441 # check for segmented downloads
408 if ($l && $::NO_SEGMENTED) { 442 if ($l && $::NO_SEGMENTED) {
427 $hdr->{"Content-Length"} = $length; 461 $hdr->{"Content-Length"} = $length;
428 462
429 $self->response(@code, $hdr, ""); 463 $self->response(@code, $hdr, "");
430 464
431 if ($self->{method} eq "GET") { 465 if ($self->{method} eq "GET") {
432 my ($fh, $buf); 466 my ($fh, $buf, $r);
467 my $current = $Coro::current;
433 open $fh, "<", $self->{path} 468 open $fh, "<", $self->{path}
434 or die "$self->{path}: late open failure ($!)"; 469 or die "$self->{path}: late open failure ($!)";
435 470
436 if ($l) {
437 sysseek $fh, $l, 0
438 or die "$self->{path}: cannot seek to $l ($!)";
439 }
440
441 $h -= $l - 1; 471 $h -= $l - 1;
442 472
473 if (0) {
474 if ($l) {
475 sysseek $fh, $l, 0;
476 }
477 }
478
443 while ($h > 0) { 479 while ($h > 0) {
480 if (0) {
444 $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 $scheduler->now;
489 });
490 &Coro::schedule;
491 last unless $r;
492 }
445 my $w = $self->{fh}->syswrite($buf) 493 my $w = $self->{fh}->syswrite($buf)
446 or last; 494 or last;
447 $::written += $w; 495 $::written += $w;
448 $self->{written} += $w; 496 $self->{written} += $w;
497 $l += $r;
449 } 498 }
450 } 499 }
451 500
452 close $fh; 501 close $fh;
453} 502}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines