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.9 by root, Sat Aug 11 12:49:21 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
80sub read_mimetypes { 90sub read_mimetypes {
81 local *M; 91 local *M;
82 if (open M, "<mimetypes") { 92 if (open M, "<mime_types") {
83 while (<M>) { 93 while (<M>) {
84 if (/^([^#]\S+)\t+(\S+)$/) { 94 if (/^([^#]\S+)\t+(\S+)$/) {
85 $mimetype{lc $1} = $2; 95 $mimetype{lc $1} = $2;
86 } 96 }
87 } 97 }
88 } else { 98 } else {
89 $self->slog(1, "cannot open mimetypes\n"); 99 print "cannot open mime_types\n";
90 } 100 }
91} 101}
102
103read_mimetypes;
92 104
93sub new { 105sub new {
94 my $class = shift; 106 my $class = shift;
95 my $peername = shift; 107 my $peername = shift;
96 my $fh = shift; 108 my $fh = shift;
97 my $self = bless { fh => $fh }, $class; 109 my $self = bless { fh => $fh }, $class;
98 my (undef, $iaddr) = unpack_sockaddr_in $peername 110 my (undef, $iaddr) = unpack_sockaddr_in $peername
99 or $self->err(500, "unable to decode peername"); 111 or $self->err(500, "unable to decode peername");
100 112
101 $self->{remote_addr} = inet_ntoa $iaddr; 113 $self->{remote_addr} = inet_ntoa $iaddr;
114 $self->{time} = $::NOW;
102 115
103 # enter ourselves into various lists 116 # enter ourselves into various lists
104 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 117 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
105 118
119 $::conns++;
120
106 $self; 121 $self;
107} 122}
108 123
109sub DESTROY { 124sub DESTROY {
110 my $self = shift; 125 my $self = shift;
126
127 $::conns--;
128
129 $self->eoconn;
111 delete $conn{$self->{remote_addr}}{$self*1}; 130 delete $conn{$self->{remote_addr}}{$self*1};
131}
132
133# end of connection
134sub eoconn {
112 delete $uri{$self->{uri}}{$self*1}; 135 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
113} 136}
114 137
115sub slog { 138sub slog {
116 my $self = shift; 139 my $self = shift;
117 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 140 main::slog($_[0], "$self->{remote_addr}> $_[1]");
118} 141}
119 142
120sub response { 143sub response {
121 my ($self, $code, $msg, $hdr, $content) = @_; 144 my ($self, $code, $msg, $hdr, $content) = @_;
122 my $res = "HTTP/1.0 $code $msg\015\012"; 145 my $res = "HTTP/1.1 $code $msg\015\012";
123 146
124 $res .= "Connection: close\015\012"; 147 #$res .= "Connection: close\015\012";
125 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 148 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
126 149
127 while (my ($h, $v) = each %$hdr) { 150 while (my ($h, $v) = each %$hdr) {
128 $res .= "$h: $v\015\012" 151 $res .= "$h: $v\015\012"
129 } 152 }
153 $res .= "\015\012";
130 154
131 $res .= "\015\012$content" if defined $content; 155 $res .= $content if defined $content and $self->{method} ne "HEAD";
132 156
133 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#
134 158
159 $self->{written} +=
135 print {$self->{fh}} $res; 160 print {$self->{fh}} $res;
136} 161}
137 162
138sub err { 163sub err {
139 my $self = shift; 164 my $self = shift;
140 my ($code, $msg, $hdr, $content) = @_; 165 my ($code, $msg, $hdr, $content) = @_;
142 unless (defined $content) { 167 unless (defined $content) {
143 $content = "$code $msg"; 168 $content = "$code $msg";
144 $hdr->{"Content-Type"} = "text/plain"; 169 $hdr->{"Content-Type"} = "text/plain";
145 $hdr->{"Content-Length"} = length $content; 170 $hdr->{"Content-Length"} = length $content;
146 } 171 }
172 $hdr->{"Connection"} = "close";
147 173
148 $self->response($code, $msg, $hdr, $content); 174 $self->response($code, $msg, $hdr, $content);
149 175
150 die bless {}, err::; 176 die bless {}, err::;
151} 177}
152 178
153sub err_blocked { 179sub err_blocked {
154 my $self = shift; 180 my $self = shift;
155 my $ip = $self->{remote_addr}; 181 my $ip = $self->{remote_addr};
156 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 182 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
183
184 Coro::Event::do_timer(after => 20*rand);
185
157 $self->err(403, "too many connections", 186 $self->err(401, "too many connections",
158 { 187 {
159 "Content-Type" => "text/html", 188 "Content-Type" => "text/html",
160 "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\"",
161 }, 192 },
162 <<EOF); 193 <<EOF);
163<html><p> 194<html><p>
164You have been blocked because you opened too many connections. You 195You have been blocked because you opened too many connections. You
165may retry at</p> 196may retry at</p>
174 205
175sub handle { 206sub handle {
176 my $self = shift; 207 my $self = shift;
177 my $fh = $self->{fh}; 208 my $fh = $self->{fh};
178 209
210 $fh->timeout($::REQ_TIMEOUT);
179 #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
180 $self->{h} = {}; 225 $self->{h} = {};
181 226
182 # read request and parse first line
183 $fh->timeout($::REQ_TIMEOUT);
184 my $req = $fh->readline("\015\012\015\012");
185 $fh->timeout($::RES_TIMEOUT); 227 $fh->timeout($::RES_TIMEOUT);
186
187 defined $req or
188 $self->err(408, "request timeout");
189
190 my $ip = $self->{remote_addr}; 228 my $ip = $self->{remote_addr};
191 229
192 if ($blocked{$ip}) { 230 if ($blocked{$ip}) {
193 $self->err_blocked($blocked{$ip}) 231 $self->err_blocked($blocked{$ip})
194 if $blocked{$ip} > $::NOW; 232 if $blocked{$ip} > $::NOW;
195 233
196 delete $blocked{$ip}; 234 delete $blocked{$ip};
197 } 235 }
198 236
199 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 237 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
200 $self->slog("blocked ip $ip"); 238 $self->slog(2, "blocked ip $ip");
201 $self->err_blocked; 239 $self->err_blocked;
202 } 240 }
203 241
204 $req =~ /^(?:\015\012)? 242 $req =~ /^(?:\015\012)?
205 (GET|HEAD) \040+ 243 (GET|HEAD) \040+
206 ([^\040]+) \040+ 244 ([^\040]+) \040+
207 HTTP\/([0-9]+\.[0-9]+) 245 HTTP\/([0-9]+\.[0-9]+)
208 \015\012/gx 246 \015\012/gx
209 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 247 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
210
211 $2 ne "1.0"
212 or $self->err(506, "http protocol version not supported");
213 248
214 $self->{method} = $1; 249 $self->{method} = $1;
215 $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");
216 255
217 # parse headers 256 # parse headers
218 { 257 {
219 my (%hdr, $h, $v); 258 my (%hdr, $h, $v);
220 259
233 while ($h, $v) = each %hdr; 272 while ($h, $v) = each %hdr;
234 } 273 }
235 274
236 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 275 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
237 276
238 weaken ($uri{$self->{uri}}{$self*1} = $self); 277 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
239 278
240 $self->map_uri; 279 $self->map_uri;
241 $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);
242 #} 288 }
243} 289}
244 290
245# uri => path mapping 291# uri => path mapping
246sub map_uri { 292sub map_uri {
247 my $self = shift; 293 my $self = shift;
304 if (chdir $::DOCROOT) { 350 if (chdir $::DOCROOT) {
305 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 351 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
306 $ENV{HTTP_HOST} = $self->server_host; 352 $ENV{HTTP_HOST} = $self->server_host;
307 $ENV{HTTP_PORT} = $self->{server_host}; 353 $ENV{HTTP_PORT} = $self->{server_host};
308 $ENV{SCRIPT_NAME} = $self->{name}; 354 $ENV{SCRIPT_NAME} = $self->{name};
309 exec $::INDEXPROG; 355 exec $path;
310 } 356 }
311 Coro::State::_exit(0); 357 Coro::State::_exit(0);
312 } else { 358 } else {
313 } 359 }
314} 360}
317 my $self = shift; 363 my $self = shift;
318 my $path = $self->{path}; 364 my $path = $self->{path};
319 365
320 stat $path 366 stat $path
321 or $self->err(404, "not found"); 367 or $self->err(404, "not found");
368
369 $self->{stat} = [stat _];
322 370
323 # idiotic netscape sends idiotic headers AGAIN 371 # idiotic netscape sends idiotic headers AGAIN
324 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 372 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
325 ? str2time $1 : 0; 373 ? str2time $1 : 0;
326 374
329 if ($path !~ /\/$/) { 377 if ($path !~ /\/$/) {
330 # create a redirect to get the trailing "/" 378 # create a redirect to get the trailing "/"
331 my $host = $self->server_hostport; 379 my $host = $self->server_hostport;
332 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 380 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
333 } else { 381 } else {
334 $ims < (stat _)[9] 382 $ims < $self->{stat}[9]
335 or $self->err(304, "not modified"); 383 or $self->err(304, "not modified");
336 384
337 if ($self->{method} eq "GET") { 385 if ($self->{method} eq "GET") {
338 if (-r "$path/index.html") { 386 if (-r "$path/index.html") {
339 $self->{path} .= "/index.html"; 387 $self->{path} .= "/index.html";
351 } 399 }
352} 400}
353 401
354sub handle_dir { 402sub handle_dir {
355 my $self = shift; 403 my $self = shift;
356 $self->_cgi($::INDEXPROG); 404 my $idx = $self->diridx;
405
406 $self->response(200, "ok",
407 {
408 "Content-Type" => "text/html",
409 "Content-Length" => length $idx,
410 },
411 $idx);
357} 412}
358 413
359sub handle_file { 414sub handle_file {
360 my $self = shift; 415 my $self = shift;
361 my $length = -s _; 416 my $length = -s _;
377 goto ignore; 432 goto ignore;
378 } 433 }
379 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l; 434 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h > $l;
380 } 435 }
381 $hdr->{"Content-Range"} = "bytes */$length"; 436 $hdr->{"Content-Range"} = "bytes */$length";
437 $self->slog(9, "not satisfiable($self->{h}{range}|".$self->{h}{"user-agent"}.")");
382 $self->err(416, "not satisfiable", $hdr); 438 $self->err(416, "not satisfiable", $hdr);
383 439
384satisfiable: 440satisfiable:
385 # check for segmented downloads 441 # check for segmented downloads
386 if ($l && $NO_SEGMENTED) { 442 if ($l && $::NO_SEGMENTED) {
387 if (%{$uri{$self->{uri}}} > 1) { 443 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
388 $self->slog("segmented download refused\n"); 444 Coro::Event::do_timer(after => 15);
445
389 $self->err(400, "segmented downloads are not allowed"); 446 $self->err(400, "segmented downloads are not allowed");
390 } 447 }
391 } 448 }
392 449
393 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 450 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
404 $hdr->{"Content-Length"} = $length; 461 $hdr->{"Content-Length"} = $length;
405 462
406 $self->response(@code, $hdr, ""); 463 $self->response(@code, $hdr, "");
407 464
408 if ($self->{method} eq "GET") { 465 if ($self->{method} eq "GET") {
409 my ($fh, $buf); 466 my ($fh, $buf, $r);
467 my $current = $Coro::current;
410 open $fh, "<", $self->{path} 468 open $fh, "<", $self->{path}
411 or die "$self->{path}: late open failure ($!)"; 469 or die "$self->{path}: late open failure ($!)";
412 470
413 if ($l) {
414 sysseek $fh, $l, 0
415 or die "$self->{path}: cannot seek to $l ($!)";
416 }
417
418 $h -= $l - 1; 471 $h -= $l - 1;
419 472
473 if (0) {
474 if ($l) {
475 sysseek $fh, $l, 0;
476 }
477 }
478
420 while ($h > 0) { 479 while ($h > 0) {
480 if (0) {
421 $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 }
422 $self->{fh}->syswrite($buf) 493 my $w = $self->{fh}->syswrite($buf)
423 or last; 494 or last;
495 $::written += $w;
496 $self->{written} += $w;
497 $l += $r;
424 } 498 }
425 } 499 }
426 500
427 close $fh; 501 close $fh;
428} 502}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines