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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines