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.10 by root, Sat Aug 11 16:34:47 2001 UTC vs.
Revision 1.23 by root, Sat Aug 18 02:58:38 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;
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
99 my $self = bless { fh => $fh }, $class; 106 my $self = bless { fh => $fh }, $class;
100 my (undef, $iaddr) = unpack_sockaddr_in $peername 107 my (undef, $iaddr) = unpack_sockaddr_in $peername
101 or $self->err(500, "unable to decode peername"); 108 or $self->err(500, "unable to decode peername");
102 109
103 $self->{remote_addr} = inet_ntoa $iaddr; 110 $self->{remote_addr} = inet_ntoa $iaddr;
111 $self->{time} = $::NOW;
104 112
105 # enter ourselves into various lists 113 # enter ourselves into various lists
106 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 114 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
107 115
116 $::conns++;
117
108 $self; 118 $self;
109} 119}
110 120
111sub DESTROY { 121sub DESTROY {
112 my $self = shift; 122 my $self = shift;
123
124 $::conns--;
125
126 $self->eoconn;
113 delete $conn{$self->{remote_addr}}{$self*1}; 127 delete $conn{$self->{remote_addr}}{$self*1};
128}
129
130# end of connection
131sub eoconn {
114 delete $uri{$self->{uri}}{$self*1}; 132 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
115} 133}
116 134
117sub slog { 135sub slog {
118 my $self = shift; 136 my $self = shift;
119 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 137 main::slog($_[0], "$self->{remote_addr}> $_[1]");
120} 138}
121 139
122sub response { 140sub response {
123 my ($self, $code, $msg, $hdr, $content) = @_; 141 my ($self, $code, $msg, $hdr, $content) = @_;
124 my $res = "HTTP/1.0 $code $msg\015\012"; 142 my $res = "HTTP/1.1 $code $msg\015\012";
125 143
126 $res .= "Connection: close\015\012"; 144 #$res .= "Connection: close\015\012";
127 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 145 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
128 146
129 while (my ($h, $v) = each %$hdr) { 147 while (my ($h, $v) = each %$hdr) {
130 $res .= "$h: $v\015\012" 148 $res .= "$h: $v\015\012"
131 } 149 }
132 $res .= "\015\012"; 150 $res .= "\015\012";
133 151
134 $res .= $content if defined $content and $self->{method} eq "GET"; 152 $res .= $content if defined $content and $self->{method} ne "HEAD";
135 153
136 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 154 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d#
137 155
156 $self->{written} +=
138 print {$self->{fh}} $res; 157 print {$self->{fh}} $res;
139} 158}
140 159
141sub err { 160sub err {
142 my $self = shift; 161 my $self = shift;
143 my ($code, $msg, $hdr, $content) = @_; 162 my ($code, $msg, $hdr, $content) = @_;
145 unless (defined $content) { 164 unless (defined $content) {
146 $content = "$code $msg"; 165 $content = "$code $msg";
147 $hdr->{"Content-Type"} = "text/plain"; 166 $hdr->{"Content-Type"} = "text/plain";
148 $hdr->{"Content-Length"} = length $content; 167 $hdr->{"Content-Length"} = length $content;
149 } 168 }
169 $hdr->{"Connection"} = "close";
150 170
151 $self->response($code, $msg, $hdr, $content); 171 $self->response($code, $msg, $hdr, $content);
152 172
153 die bless {}, err::; 173 die bless {}, err::;
154} 174}
156sub err_blocked { 176sub err_blocked {
157 my $self = shift; 177 my $self = shift;
158 my $ip = $self->{remote_addr}; 178 my $ip = $self->{remote_addr};
159 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 179 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
160 180
161 Coro::Event::do_timer(after => 5); 181 Coro::Event::do_timer(after => 20*rand);
162 182
163 $self->err(403, "too many connections", 183 $self->err(401, "too many connections",
164 { 184 {
165 "Content-Type" => "text/html", 185 "Content-Type" => "text/html",
166 "Retry-After" => $::BLOCKTIME 186 "Retry-After" => $::BLOCKTIME,
187 "Warning" => "Please do NOT retry, you have been blocked",
188 "WWW-Authenticate" => "Basic realm=\"Please do NOT retry, you have been blocked\"",
167 }, 189 },
168 <<EOF); 190 <<EOF);
169<html><p> 191<html><p>
170You have been blocked because you opened too many connections. You 192You have been blocked because you opened too many connections. You
171may retry at</p> 193may retry at</p>
180 202
181sub handle { 203sub handle {
182 my $self = shift; 204 my $self = shift;
183 my $fh = $self->{fh}; 205 my $fh = $self->{fh};
184 206
207 $fh->timeout($::REQ_TIMEOUT);
185 #while() { 208 while() {
209 $self->{reqs}++;
210
211 # read request and parse first line
212 my $req = $fh->readline("\015\012\015\012");
213
214 unless (defined $req) {
215 if (exists $self->{version}) {
216 last;
217 } else {
218 $self->err(408, "request timeout");
219 }
220 }
221
186 $self->{h} = {}; 222 $self->{h} = {};
187 223
188 # read request and parse first line
189 $fh->timeout($::REQ_TIMEOUT);
190 my $req = $fh->readline("\015\012\015\012");
191 $fh->timeout($::RES_TIMEOUT); 224 $fh->timeout($::RES_TIMEOUT);
192
193 defined $req or
194 $self->err(408, "request timeout");
195
196 my $ip = $self->{remote_addr}; 225 my $ip = $self->{remote_addr};
197 226
198 if ($blocked{$ip}) { 227 if ($blocked{$ip}) {
199 $self->err_blocked($blocked{$ip}) 228 $self->err_blocked($blocked{$ip})
200 if $blocked{$ip} > $::NOW; 229 if $blocked{$ip} > $::NOW;
201 230
202 delete $blocked{$ip}; 231 delete $blocked{$ip};
203 } 232 }
204 233
205 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 234 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
206 $self->slog("blocked ip $ip"); 235 $self->slog(2, "blocked ip $ip");
207 $self->err_blocked; 236 $self->err_blocked;
208 } 237 }
209 238
210 $req =~ /^(?:\015\012)? 239 $req =~ /^(?:\015\012)?
211 (GET|HEAD) \040+ 240 (GET|HEAD) \040+
212 ([^\040]+) \040+ 241 ([^\040]+) \040+
213 HTTP\/([0-9]+\.[0-9]+) 242 HTTP\/([0-9]+\.[0-9]+)
214 \015\012/gx 243 \015\012/gx
215 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 244 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
216
217 $2 ne "1.0"
218 or $self->err(506, "http protocol version not supported");
219 245
220 $self->{method} = $1; 246 $self->{method} = $1;
221 $self->{uri} = $2; 247 $self->{uri} = $2;
248 $self->{version} = $3;
249
250 $3 =~ /^1\./
251 or $self->err(506, "http protocol version $3 not supported");
222 252
223 # parse headers 253 # parse headers
224 { 254 {
225 my (%hdr, $h, $v); 255 my (%hdr, $h, $v);
226 256
239 while ($h, $v) = each %hdr; 269 while ($h, $v) = each %hdr;
240 } 270 }
241 271
242 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 272 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
243 273
244 weaken ($uri{$self->{uri}}{$self*1} = $self); 274 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
245 275
246 $self->map_uri; 276 $self->map_uri;
247 $self->respond; 277 $self->respond;
278
279 $self->eoconn;
280
281 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
282
283 $self->slog(9, "persistent connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
284 $fh->timeout($::PER_TIMEOUT);
248 #} 285 }
249} 286}
250 287
251# uri => path mapping 288# uri => path mapping
252sub map_uri { 289sub map_uri {
253 my $self = shift; 290 my $self = shift;
392 goto ignore; 429 goto ignore;
393 } 430 }
394 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 431 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l;
395 } 432 }
396 $hdr->{"Content-Range"} = "bytes */$length"; 433 $hdr->{"Content-Range"} = "bytes */$length";
434 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
397 $self->err(416, "not satisfiable", $hdr); 435 $self->err(416, "not satisfiable", $hdr);
398 436
399satisfiable: 437satisfiable:
400 # check for segmented downloads 438 # check for segmented downloads
401 if ($l && $::NO_SEGMENTED) { 439 if ($l && $::NO_SEGMENTED) {
402 if (%{$uri{$self->{uri}}} > 1) { 440 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
403 $self->slog("segmented download refused\n"); 441 Coro::Event::do_timer(after => 15);
442
404 $self->err(400, "segmented downloads are not allowed"); 443 $self->err(400, "segmented downloads are not allowed");
405 } 444 }
406 } 445 }
407 446
408 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 447 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
419 $hdr->{"Content-Length"} = $length; 458 $hdr->{"Content-Length"} = $length;
420 459
421 $self->response(@code, $hdr, ""); 460 $self->response(@code, $hdr, "");
422 461
423 if ($self->{method} eq "GET") { 462 if ($self->{method} eq "GET") {
424 my ($fh, $buf); 463 my ($fh, $buf, $r);
464 my $current = $Coro::current;
425 open $fh, "<", $self->{path} 465 open $fh, "<", $self->{path}
426 or die "$self->{path}: late open failure ($!)"; 466 or die "$self->{path}: late open failure ($!)";
427 467
428 if ($l) {
429 sysseek $fh, $l, 0
430 or die "$self->{path}: cannot seek to $l ($!)";
431 }
432
433 $h -= $l - 1; 468 $h -= $l - 1;
434 469
470 if (0) {
471 if ($l) {
472 sysseek $fh, $l, 0;
473 }
474 }
475
435 while ($h > 0) { 476 while ($h > 0) {
477 if (0) {
436 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; 478 sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h
479 or last;
480 } else {
481 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
482 $buf, 0, sub {
483 $r = $_[0];
484 $current->ready;
485 });
486 &Coro::schedule;
487 last unless $r;
488 }
437 $self->{fh}->syswrite($buf) 489 my $w = $self->{fh}->syswrite($buf)
438 or last; 490 or last;
491 $::written += $w;
492 $self->{written} += $w;
493 $l += $r;
439 } 494 }
440 } 495 }
441 496
442 close $fh; 497 close $fh;
443} 498}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines