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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines