ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/cvsroot/Coro/myhttpd/httpd.pl
(Generate patch)

Comparing cvsroot/Coro/myhttpd/httpd.pl (file contents):
Revision 1.15 by root, Tue Aug 14 02:28:51 2001 UTC vs.
Revision 1.19 by root, Thu Aug 16 16:40:07 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}
187 200
188sub handle { 201sub handle {
189 my $self = shift; 202 my $self = shift;
190 my $fh = $self->{fh}; 203 my $fh = $self->{fh};
191 204
205 $fh->timeout($::REQ_TIMEOUT);
192 #while() { 206 while() {
207 $self->{reqs}++;
208
209 # read request and parse first line
210 my $req = $fh->readline("\015\012\015\012");
211
212 unless (defined $req) {
213 if (exists $self->{version}) {
214 last;
215 } else {
216 $self->err(408, "request timeout");
217 }
218 }
219
193 $self->{h} = {}; 220 $self->{h} = {};
194 221
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); 222 $fh->timeout($::RES_TIMEOUT);
199
200 defined $req or
201 $self->err(408, "request timeout");
202
203 my $ip = $self->{remote_addr}; 223 my $ip = $self->{remote_addr};
204 224
205 if ($blocked{$ip}) { 225 if ($blocked{$ip}) {
206 $self->err_blocked($blocked{$ip}) 226 $self->err_blocked($blocked{$ip})
207 if $blocked{$ip} > $::NOW; 227 if $blocked{$ip} > $::NOW;
219 ([^\040]+) \040+ 239 ([^\040]+) \040+
220 HTTP\/([0-9]+\.[0-9]+) 240 HTTP\/([0-9]+\.[0-9]+)
221 \015\012/gx 241 \015\012/gx
222 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" }); 242 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
223 243
224 $2 ne "1.0"
225 or $self->err(506, "http protocol version not supported");
226
227 $self->{method} = $1; 244 $self->{method} = $1;
228 $self->{uri} = $2; 245 $self->{uri} = $2;
246 $self->{version} = $3;
247
248 $3 eq "1.0" or $3 eq "1.1"
249 or $self->err(506, "http protocol version $3 not supported");
229 250
230 # parse headers 251 # parse headers
231 { 252 {
232 my (%hdr, $h, $v); 253 my (%hdr, $h, $v);
233 254
250 271
251 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self); 272 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
252 273
253 $self->map_uri; 274 $self->map_uri;
254 $self->respond; 275 $self->respond;
276
277 $self->eoconn;
278
279 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
280
281 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
282 $fh->timeout($::PER_TIMEOUT);
255 #} 283 }
256} 284}
257 285
258# uri => path mapping 286# uri => path mapping
259sub map_uri { 287sub map_uri {
260 my $self = shift; 288 my $self = shift;
427 $hdr->{"Content-Length"} = $length; 455 $hdr->{"Content-Length"} = $length;
428 456
429 $self->response(@code, $hdr, ""); 457 $self->response(@code, $hdr, "");
430 458
431 if ($self->{method} eq "GET") { 459 if ($self->{method} eq "GET") {
432 my ($fh, $buf); 460 my ($fh, $buf, $r);
461 my $current = $Coro::current;
433 open $fh, "<", $self->{path} 462 open $fh, "<", $self->{path}
434 or die "$self->{path}: late open failure ($!)"; 463 or die "$self->{path}: late open failure ($!)";
435 464
436 if ($l) {
437 sysseek $fh, $l, 0
438 or die "$self->{path}: cannot seek to $l ($!)";
439 }
440
441 $h -= $l - 1; 465 $h -= $l - 1;
442 466
467 if (0) {
468 if ($l) {
469 sysseek $fh, $l, 0;
470 }
471 }
472
443 while ($h > 0) { 473 while ($h > 0) {
474 if (0) {
444 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; 475 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
476 or last;
477 } else {
478 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
479 $buf, 0, sub {
480 $r = $_[0];
481 $current->ready;
482 });
483 &Coro::schedule;
484 last unless $r;
485 }
445 my $w = $self->{fh}->syswrite($buf) 486 my $w = $self->{fh}->syswrite($buf)
446 or last; 487 or last;
447 $::written += $w; 488 $::written += $w;
448 $self->{written} += $w; 489 $self->{written} += $w;
490 $l += $r;
449 } 491 }
450 } 492 }
451 493
452 close $fh; 494 close $fh;
453} 495}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines