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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines