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.13 by root, Sun Aug 12 01:16:48 2001 UTC vs.
Revision 1.18 by root, Wed Aug 15 03:13:36 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
125 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 132 main::slog($_[0], "$self->{remote_addr}> $_[1]");
126} 133}
127 134
128sub response { 135sub response {
129 my ($self, $code, $msg, $hdr, $content) = @_; 136 my ($self, $code, $msg, $hdr, $content) = @_;
130 my $res = "HTTP/1.0 $code $msg\015\012"; 137 my $res = "HTTP/1.1 $code $msg\015\012";
131 138
132 $res .= "Connection: close\015\012"; 139 #$res .= "Connection: close\015\012";
133 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 140 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
134 141
135 while (my ($h, $v) = each %$hdr) { 142 while (my ($h, $v) = each %$hdr) {
136 $res .= "$h: $v\015\012" 143 $res .= "$h: $v\015\012"
137 } 144 }
152 unless (defined $content) { 159 unless (defined $content) {
153 $content = "$code $msg"; 160 $content = "$code $msg";
154 $hdr->{"Content-Type"} = "text/plain"; 161 $hdr->{"Content-Type"} = "text/plain";
155 $hdr->{"Content-Length"} = length $content; 162 $hdr->{"Content-Length"} = length $content;
156 } 163 }
164 $hdr->{"Connection"} = "close";
157 165
158 $self->response($code, $msg, $hdr, $content); 166 $self->response($code, $msg, $hdr, $content);
159 167
160 die bless {}, err::; 168 die bless {}, err::;
161} 169}
163sub err_blocked { 171sub err_blocked {
164 my $self = shift; 172 my $self = shift;
165 my $ip = $self->{remote_addr}; 173 my $ip = $self->{remote_addr};
166 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 174 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
167 175
176 Coro::Event::do_timer(after => 15);
177
168 $self->err(403, "too many connections", 178 $self->err(401, "too many connections",
169 { 179 {
170 "Content-Type" => "text/html", 180 "Content-Type" => "text/html",
171 "Retry-After" => $::BLOCKTIME 181 "Retry-After" => $::BLOCKTIME
172 }, 182 },
173 <<EOF); 183 <<EOF);
185 195
186sub handle { 196sub handle {
187 my $self = shift; 197 my $self = shift;
188 my $fh = $self->{fh}; 198 my $fh = $self->{fh};
189 199
200 $fh->timeout($::REQ_TIMEOUT);
190 #while() { 201 while() {
202 $self->{reqs}++;
203
204 # read request and parse first line
205 my $req = $fh->readline("\015\012\015\012");
206
207 unless (defined $req) {
208 if (exists $self->{version}) {
209 last;
210 } else {
211 $self->err(408, "request timeout");
212 }
213 }
214
191 $self->{h} = {}; 215 $self->{h} = {};
192 216
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); 217 $fh->timeout($::RES_TIMEOUT);
197
198 defined $req or
199 $self->err(408, "request timeout");
200
201 my $ip = $self->{remote_addr}; 218 my $ip = $self->{remote_addr};
202 219
203 if ($blocked{$ip}) { 220 if ($blocked{$ip}) {
204 $self->err_blocked($blocked{$ip}) 221 $self->err_blocked($blocked{$ip})
205 if $blocked{$ip} > $::NOW; 222 if $blocked{$ip} > $::NOW;
215 $req =~ /^(?:\015\012)? 232 $req =~ /^(?:\015\012)?
216 (GET|HEAD) \040+ 233 (GET|HEAD) \040+
217 ([^\040]+) \040+ 234 ([^\040]+) \040+
218 HTTP\/([0-9]+\.[0-9]+) 235 HTTP\/([0-9]+\.[0-9]+)
219 \015\012/gx 236 \015\012/gx
220 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 237 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 238
225 $self->{method} = $1; 239 $self->{method} = $1;
226 $self->{uri} = $2; 240 $self->{uri} = $2;
241 $self->{version} = $3;
242
243 $3 eq "1.0" or $3 eq "1.1"
244 or $self->err(506, "http protocol version $3 not supported");
227 245
228 # parse headers 246 # parse headers
229 { 247 {
230 my (%hdr, $h, $v); 248 my (%hdr, $h, $v);
231 249
248 266
249 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 267 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
250 268
251 $self->map_uri; 269 $self->map_uri;
252 $self->respond; 270 $self->respond;
271
272 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
273
274 $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
275 $fh->timeout($::PER_TIMEOUT);
253 #} 276 }
254} 277}
255 278
256# uri => path mapping 279# uri => path mapping
257sub map_uri { 280sub map_uri {
258 my $self = shift; 281 my $self = shift;
403 426
404satisfiable: 427satisfiable:
405 # check for segmented downloads 428 # check for segmented downloads
406 if ($l && $::NO_SEGMENTED) { 429 if ($l && $::NO_SEGMENTED) {
407 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) { 430 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
431 Coro::Event::do_timer(after => 15);
432
408 $self->err(400, "segmented downloads are not allowed"); 433 $self->err(400, "segmented downloads are not allowed");
409 } 434 }
410 } 435 }
411 436
412 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 437 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
423 $hdr->{"Content-Length"} = $length; 448 $hdr->{"Content-Length"} = $length;
424 449
425 $self->response(@code, $hdr, ""); 450 $self->response(@code, $hdr, "");
426 451
427 if ($self->{method} eq "GET") { 452 if ($self->{method} eq "GET") {
428 my ($fh, $buf); 453 my ($fh, $buf, $r);
454 my $current = $Coro::current;
429 open $fh, "<", $self->{path} 455 open $fh, "<", $self->{path}
430 or die "$self->{path}: late open failure ($!)"; 456 or die "$self->{path}: late open failure ($!)";
431 457
432 if ($l) {
433 sysseek $fh, $l, 0
434 or die "$self->{path}: cannot seek to $l ($!)";
435 }
436
437 $h -= $l - 1; 458 $h -= $l - 1;
438 459
439 while ($h > 0) { 460 while ($h > 0) {
440 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; 461 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
462 $buf, 0, sub {
463 $r = $_[0];
464 $current->ready;
465 });
466 &Coro::schedule;
467 last unless $r;
441 my $w = $self->{fh}->syswrite($buf) 468 my $w = $self->{fh}->syswrite($buf)
442 or last; 469 or last;
443 $::written += $w; 470 $::written += $w;
444 $self->{written} += $w; 471 $self->{written} += $w;
472 $l += $r;
445 } 473 }
446 } 474 }
447 475
448 close $fh; 476 close $fh;
449} 477}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines