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.20 by root, Thu Aug 16 21:55: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;
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 }
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
168 Coro::Event::do_timer(after => 15); 181 Coro::Event::do_timer(after => 20*rand);
169 182
170 $self->err(401, "too many connections", 183 $self->err(401, "too many connections",
171 { 184 {
172 "Content-Type" => "text/html", 185 "Content-Type" => "text/html",
173 "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\"",
174 }, 189 },
175 <<EOF); 190 <<EOF);
176<html><p> 191<html><p>
177You have been blocked because you opened too many connections. You 192You have been blocked because you opened too many connections. You
178may retry at</p> 193may retry at</p>
187 202
188sub handle { 203sub handle {
189 my $self = shift; 204 my $self = shift;
190 my $fh = $self->{fh}; 205 my $fh = $self->{fh};
191 206
207 $fh->timeout($::REQ_TIMEOUT);
192 #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
193 $self->{h} = {}; 222 $self->{h} = {};
194 223
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); 224 $fh->timeout($::RES_TIMEOUT);
199
200 defined $req or
201 $self->err(408, "request timeout");
202
203 my $ip = $self->{remote_addr}; 225 my $ip = $self->{remote_addr};
204 226
205 $self->err_blocked($blocked{$ip});
206 if ($blocked{$ip}) { 227 if ($blocked{$ip}) {
207 $self->err_blocked($blocked{$ip}) 228 $self->err_blocked($blocked{$ip})
208 if $blocked{$ip} > $::NOW; 229 if $blocked{$ip} > $::NOW;
209 230
210 delete $blocked{$ip}; 231 delete $blocked{$ip};
220 ([^\040]+) \040+ 241 ([^\040]+) \040+
221 HTTP\/([0-9]+\.[0-9]+) 242 HTTP\/([0-9]+\.[0-9]+)
222 \015\012/gx 243 \015\012/gx
223 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); 244 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
224 245
225 $2 ne "1.0"
226 or $self->err(506, "http protocol version not supported");
227
228 $self->{method} = $1; 246 $self->{method} = $1;
229 $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");
230 252
231 # parse headers 253 # parse headers
232 { 254 {
233 my (%hdr, $h, $v); 255 my (%hdr, $h, $v);
234 256
251 273
252 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 274 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
253 275
254 $self->map_uri; 276 $self->map_uri;
255 $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);
256 #} 285 }
257} 286}
258 287
259# uri => path mapping 288# uri => path mapping
260sub map_uri { 289sub map_uri {
261 my $self = shift; 290 my $self = shift;
400 goto ignore; 429 goto ignore;
401 } 430 }
402 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 431 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l;
403 } 432 }
404 $hdr->{"Content-Range"} = "bytes */$length"; 433 $hdr->{"Content-Range"} = "bytes */$length";
434 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
405 $self->err(416, "not satisfiable", $hdr); 435 $self->err(416, "not satisfiable", $hdr);
406 436
407satisfiable: 437satisfiable:
408 # check for segmented downloads 438 # check for segmented downloads
409 if ($l && $::NO_SEGMENTED) { 439 if ($l && $::NO_SEGMENTED) {
428 $hdr->{"Content-Length"} = $length; 458 $hdr->{"Content-Length"} = $length;
429 459
430 $self->response(@code, $hdr, ""); 460 $self->response(@code, $hdr, "");
431 461
432 if ($self->{method} eq "GET") { 462 if ($self->{method} eq "GET") {
433 my ($fh, $buf); 463 my ($fh, $buf, $r);
464 my $current = $Coro::current;
434 open $fh, "<", $self->{path} 465 open $fh, "<", $self->{path}
435 or die "$self->{path}: late open failure ($!)"; 466 or die "$self->{path}: late open failure ($!)";
436 467
437 if ($l) {
438 sysseek $fh, $l, 0
439 or die "$self->{path}: cannot seek to $l ($!)";
440 }
441
442 $h -= $l - 1; 468 $h -= $l - 1;
443 469
470 if (0) {
471 if ($l) {
472 sysseek $fh, $l, 0;
473 }
474 }
475
444 while ($h > 0) { 476 while ($h > 0) {
477 if (0) {
445 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; 478 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
479 or last;
480 } else {
481 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
482 $buf, 0, sub {
483 $r = $_[0];
484 $current->ready;
485 });
486 &Coro::schedule;
487 last unless $r;
488 }
446 my $w = $self->{fh}->syswrite($buf) 489 my $w = $self->{fh}->syswrite($buf)
447 or last; 490 or last;
448 $::written += $w; 491 $::written += $w;
449 $self->{written} += $w; 492 $self->{written} += $w;
493 $l += $r;
450 } 494 }
451 } 495 }
452 496
453 close $fh; 497 close $fh;
454} 498}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines