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.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}
187 195
188sub handle { 196sub handle {
189 my $self = shift; 197 my $self = shift;
190 my $fh = $self->{fh}; 198 my $fh = $self->{fh};
191 199
200 $fh->timeout($::REQ_TIMEOUT);
192 #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
193 $self->{h} = {}; 215 $self->{h} = {};
194 216
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); 217 $fh->timeout($::RES_TIMEOUT);
199
200 defined $req or
201 $self->err(408, "request timeout");
202
203 my $ip = $self->{remote_addr}; 218 my $ip = $self->{remote_addr};
204 219
205 if ($blocked{$ip}) { 220 if ($blocked{$ip}) {
206 $self->err_blocked($blocked{$ip}) 221 $self->err_blocked($blocked{$ip})
207 if $blocked{$ip} > $::NOW; 222 if $blocked{$ip} > $::NOW;
219 ([^\040]+) \040+ 234 ([^\040]+) \040+
220 HTTP\/([0-9]+\.[0-9]+) 235 HTTP\/([0-9]+\.[0-9]+)
221 \015\012/gx 236 \015\012/gx
222 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); 237 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
223 238
224 $2 ne "1.0"
225 or $self->err(506, "http protocol version not supported");
226
227 $self->{method} = $1; 239 $self->{method} = $1;
228 $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");
229 245
230 # parse headers 246 # parse headers
231 { 247 {
232 my (%hdr, $h, $v); 248 my (%hdr, $h, $v);
233 249
250 266
251 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 267 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
252 268
253 $self->map_uri; 269 $self->map_uri;
254 $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);
255 #} 276 }
256} 277}
257 278
258# uri => path mapping 279# uri => path mapping
259sub map_uri { 280sub map_uri {
260 my $self = shift; 281 my $self = shift;
427 $hdr->{"Content-Length"} = $length; 448 $hdr->{"Content-Length"} = $length;
428 449
429 $self->response(@code, $hdr, ""); 450 $self->response(@code, $hdr, "");
430 451
431 if ($self->{method} eq "GET") { 452 if ($self->{method} eq "GET") {
432 my ($fh, $buf); 453 my ($fh, $buf, $r);
454 my $current = $Coro::current;
433 open $fh, "<", $self->{path} 455 open $fh, "<", $self->{path}
434 or die "$self->{path}: late open failure ($!)"; 456 or die "$self->{path}: late open failure ($!)";
435 457
436 if ($l) {
437 sysseek $fh, $l, 0
438 or die "$self->{path}: cannot seek to $l ($!)";
439 }
440
441 $h -= $l - 1; 458 $h -= $l - 1;
442 459
443 while ($h > 0) { 460 while ($h > 0) {
444 $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;
445 my $w = $self->{fh}->syswrite($buf) 468 my $w = $self->{fh}->syswrite($buf)
446 or last; 469 or last;
447 $::written += $w; 470 $::written += $w;
448 $self->{written} += $w; 471 $self->{written} += $w;
472 $l += $r;
449 } 473 }
450 } 474 }
451 475
452 close $fh; 476 close $fh;
453} 477}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines