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.12 by root, Sat Aug 11 23:10:56 2001 UTC vs.
Revision 1.26 by root, Sun Aug 19 22:59:35 2001 UTC

43 43
44my $http_port = new Coro::Socket 44my $http_port = new Coro::Socket
45 LocalAddr => $SERVER_HOST, 45 LocalAddr => $SERVER_HOST,
46 LocalPort => $SERVER_PORT, 46 LocalPort => $SERVER_PORT,
47 ReuseAddr => 1, 47 ReuseAddr => 1,
48 Listen => 1, 48 Listen => 50,
49 or die "unable to start server"; 49 or die "unable to start server";
50 50
51push @listen_sockets, $http_port; 51push @listen_sockets, $http_port;
52 52
53# the "main thread" 53# the "main thread"
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;
75 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
76our %conn; # $conn{ip}{fh} => connobj 83our %conn; # $conn{ip}{self} => connobj
84our %uri; # $uri{ip}{uri}{self}
77our %blocked; 85our %blocked;
78our %mimetype; 86our %mimetype;
79 87
80sub read_mimetypes { 88sub read_mimetypes {
81 local *M; 89 local *M;
104 $self->{time} = $::NOW; 112 $self->{time} = $::NOW;
105 113
106 # enter ourselves into various lists 114 # enter ourselves into various lists
107 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 115 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
108 116
117 $::conns++;
118
109 $self; 119 $self;
110} 120}
111 121
112sub DESTROY { 122sub DESTROY {
113 my $self = shift; 123 my $self = shift;
124
125 $::conns--;
126
127 $self->eoconn;
114 delete $conn{$self->{remote_addr}}{$self*1}; 128 delete $conn{$self->{remote_addr}}{$self*1};
129}
130
131# end of connection
132sub eoconn {
133 my $self = shift;
115 delete $uri{$self->{uri}}{$self*1}; 134 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
116} 135}
117 136
118sub slog { 137sub slog {
119 my $self = shift; 138 my $self = shift;
120 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 139 main::slog($_[0], "$self->{remote_addr}> $_[1]");
121} 140}
122 141
123sub response { 142sub response {
124 my ($self, $code, $msg, $hdr, $content) = @_; 143 my ($self, $code, $msg, $hdr, $content) = @_;
125 my $res = "HTTP/1.0 $code $msg\015\012"; 144 my $res = "HTTP/1.1 $code $msg\015\012";
126 145
127 $res .= "Connection: close\015\012"; 146 #$res .= "Connection: close\015\012";
128 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 147 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
129 148
130 while (my ($h, $v) = each %$hdr) { 149 while (my ($h, $v) = each %$hdr) {
131 $res .= "$h: $v\015\012" 150 $res .= "$h: $v\015\012"
132 } 151 }
133 $res .= "\015\012"; 152 $res .= "\015\012";
134 153
135 $res .= $content if defined $content and $self->{method} eq "GET"; 154 $res .= $content if defined $content and $self->{method} ne "HEAD";
136 155
137 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 156 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";
138 157
139 $self->{written} += 158 $self->{written} +=
140 print {$self->{fh}} $res; 159 print {$self->{fh}} $res;
141} 160}
142 161
147 unless (defined $content) { 166 unless (defined $content) {
148 $content = "$code $msg"; 167 $content = "$code $msg";
149 $hdr->{"Content-Type"} = "text/plain"; 168 $hdr->{"Content-Type"} = "text/plain";
150 $hdr->{"Content-Length"} = length $content; 169 $hdr->{"Content-Length"} = length $content;
151 } 170 }
171 $hdr->{"Connection"} = "close";
152 172
153 $self->response($code, $msg, $hdr, $content); 173 $self->response($code, $msg, $hdr, $content);
154 174
155 die bless {}, err::; 175 die bless {}, err::;
156} 176}
158sub err_blocked { 178sub err_blocked {
159 my $self = shift; 179 my $self = shift;
160 my $ip = $self->{remote_addr}; 180 my $ip = $self->{remote_addr};
161 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 181 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
162 182
163 Coro::Event::do_timer(after => 5); 183 Coro::Event::do_timer(after => 20*rand);
164 184
165 $self->err(403, "too many connections", 185 $self->err(401, "too many connections",
166 { 186 {
167 "Content-Type" => "text/html", 187 "Content-Type" => "text/html",
168 "Retry-After" => $::BLOCKTIME 188 "Retry-After" => $::BLOCKTIME,
189 "Warning" => "Please do NOT retry, you have been blocked",
190 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
169 }, 191 },
170 <<EOF); 192 <<EOF);
171<html><p> 193<html><p>
172You have been blocked because you opened too many connections. You 194You have been blocked because you opened too many connections. You
173may retry at</p> 195may retry at</p>
182 204
183sub handle { 205sub handle {
184 my $self = shift; 206 my $self = shift;
185 my $fh = $self->{fh}; 207 my $fh = $self->{fh};
186 208
209 $fh->timeout($::REQ_TIMEOUT);
187 #while() { 210 while() {
211 $self->{reqs}++;
212
213 # read request and parse first line
214 my $req = $fh->readline("\015\012\015\012");
215
216 unless (defined $req) {
217 if (exists $self->{version}) {
218 last;
219 } else {
220 $self->err(408, "request timeout");
221 }
222 }
223
188 $self->{h} = {}; 224 $self->{h} = {};
189 225
190 # read request and parse first line
191 $fh->timeout($::REQ_TIMEOUT);
192 my $req = $fh->readline("\015\012\015\012");
193 $fh->timeout($::RES_TIMEOUT); 226 $fh->timeout($::RES_TIMEOUT);
194
195 defined $req or
196 $self->err(408, "request timeout");
197
198 my $ip = $self->{remote_addr}; 227 my $ip = $self->{remote_addr};
199 228
200 if ($blocked{$ip}) { 229 if ($blocked{$ip}) {
201 $self->err_blocked($blocked{$ip}) 230 $self->err_blocked($blocked{$ip})
202 if $blocked{$ip} > $::NOW; 231 if $blocked{$ip} > $::NOW;
212 $req =~ /^(?:\015\012)? 241 $req =~ /^(?:\015\012)?
213 (GET|HEAD) \040+ 242 (GET|HEAD) \040+
214 ([^\040]+) \040+ 243 ([^\040]+) \040+
215 HTTP\/([0-9]+\.[0-9]+) 244 HTTP\/([0-9]+\.[0-9]+)
216 \015\012/gx 245 \015\012/gx
217 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 246 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
218
219 $2 ne "1.0"
220 or $self->err(506, "http protocol version not supported");
221 247
222 $self->{method} = $1; 248 $self->{method} = $1;
223 $self->{uri} = $2; 249 $self->{uri} = $2;
250 $self->{version} = $3;
251
252 $3 =~ /^1\./
253 or $self->err(506, "http protocol version $3 not supported");
224 254
225 # parse headers 255 # parse headers
226 { 256 {
227 my (%hdr, $h, $v); 257 my (%hdr, $h, $v);
228 258
241 while ($h, $v) = each %hdr; 271 while ($h, $v) = each %hdr;
242 } 272 }
243 273
244 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 274 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
245 275
246 weaken ($uri{$self->{uri}}{$self*1} = $self); 276 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
247 277
278 eval {
248 $self->map_uri; 279 $self->map_uri;
249 $self->respond; 280 $self->respond;
281 };
282
283 $self->eoconn;
284
285 die if $@ && !ref $@;
286
287 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
288
289 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
290 $fh->timeout($::PER_TIMEOUT);
250 #} 291 }
251} 292}
252 293
253# uri => path mapping 294# uri => path mapping
254sub map_uri { 295sub map_uri {
255 my $self = shift; 296 my $self = shift;
342 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 383 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
343 } else { 384 } else {
344 $ims < $self->{stat}[9] 385 $ims < $self->{stat}[9]
345 or $self->err(304, "not modified"); 386 or $self->err(304, "not modified");
346 387
347 if ($self->{method} eq "GET") {
348 if (-r "$path/index.html") { 388 if (-r "$path/index.html") {
349 $self->{path} .= "/index.html"; 389 $self->{path} .= "/index.html";
350 $self->handle_file; 390 $self->handle_file;
351 } else { 391 } else {
352 $self->handle_dir; 392 $self->handle_dir;
353 }
354 } 393 }
355 } 394 }
356 } elsif (-f _ && -r _) { 395 } elsif (-f _ && -r _) {
357 -x _ and $self->err(403, "forbidden"); 396 -x _ and $self->err(403, "forbidden");
358 $self->handle_file; 397 $self->handle_file;
391 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); 430 ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
392 } else { 431 } else {
393 ($l, $h) = (0, $length - 1); 432 ($l, $h) = (0, $length - 1);
394 goto ignore; 433 goto ignore;
395 } 434 }
396 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 435 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
397 } 436 }
398 $hdr->{"Content-Range"} = "bytes */$length"; 437 $hdr->{"Content-Range"} = "bytes */$length";
438 $hdr->{"Content-Length"} = $length;
439 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
399 $self->err(416, "not satisfiable", $hdr); 440 $self->err(416, "not satisfiable", $hdr, "");
400 441
401satisfiable: 442satisfiable:
402 # check for segmented downloads 443 # check for segmented downloads
403 if ($l && $::NO_SEGMENTED) { 444 if ($l && $::NO_SEGMENTED) {
404 if (%{$uri{$self->{uri}}} > 1) { 445 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
405 $self->err(400, "segmented downloads are not allowed"); 446 $self->err(400, "segmented downloads are not allowed");
406 } 447 }
407 } 448 }
408 449
409 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 450 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
420 $hdr->{"Content-Length"} = $length; 461 $hdr->{"Content-Length"} = $length;
421 462
422 $self->response(@code, $hdr, ""); 463 $self->response(@code, $hdr, "");
423 464
424 if ($self->{method} eq "GET") { 465 if ($self->{method} eq "GET") {
425 my ($fh, $buf); 466 my ($fh, $buf, $r);
467 my $current = $Coro::current;
426 open $fh, "<", $self->{path} 468 open $fh, "<", $self->{path}
427 or die "$self->{path}: late open failure ($!)"; 469 or die "$self->{path}: late open failure ($!)";
428 470
429 if ($l) {
430 sysseek $fh, $l, 0
431 or die "$self->{path}: cannot seek to $l ($!)";
432 }
433
434 $h -= $l - 1; 471 $h -= $l - 1;
435 472
473 if (0) {
474 if ($l) {
475 sysseek $fh, $l, 0;
476 }
477 }
478
436 while ($h > 0) { 479 while ($h > 0) {
480 if (0) {
437 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; 481 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
482 or last;
483 } else {
484 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
485 $buf, 0, sub {
486 $r = $_[0];
487 $current->ready;
488 });
489 &Coro::schedule;
490 last unless $r;
491 }
438 my $w = $self->{fh}->syswrite($buf) 492 my $w = $self->{fh}->syswrite($buf)
439 or last; 493 or last;
440 $::written += $w; 494 $::written += $w;
441 $self->{written} += $w; 495 $self->{written} += $w;
496 $l += $r;
442 } 497 }
443 } 498 }
444 499
445 close $fh; 500 close $fh;
446} 501}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines