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.5 by root, Fri Aug 10 15:58:04 2001 UTC vs.
Revision 1.17 by root, Wed Aug 15 03:06:21 2001 UTC

18 printf "---: $format\n", @_; 18 printf "---: $format\n", @_;
19} 19}
20 20
21my $connections = new Coro::Semaphore $MAX_CONNECTS; 21my $connections = new Coro::Semaphore $MAX_CONNECTS;
22 22
23my @fh; 23my @newcons;
24my @pool; 24my @pool;
25 25
26# one "execution thread" 26# one "execution thread"
27sub handler { 27sub handler {
28 while () { 28 while () {
29 my $fh = pop @fh; 29 my $new = pop @newcons;
30 if ($fh) { 30 if ($new) {
31 eval { 31 eval {
32 conn->new($fh)->handle; 32 conn->new(@$new)->handle;
33 }; 33 };
34 close $fh;
35 slog 1, "$@" if $@ && !ref $@; 34 slog 1, "$@" if $@ && !ref $@;
36 $connections->up; 35 $connections->up;
37 } else { 36 } else {
38 last if @pool >= $MAX_POOL; 37 last if @pool >= $MAX_POOL;
39 push @pool, $Coro::current; 38 push @pool, $Coro::current;
44 43
45my $http_port = new Coro::Socket 44my $http_port = new Coro::Socket
46 LocalAddr => $SERVER_HOST, 45 LocalAddr => $SERVER_HOST,
47 LocalPort => $SERVER_PORT, 46 LocalPort => $SERVER_PORT,
48 ReuseAddr => 1, 47 ReuseAddr => 1,
49 Listen => 1, 48 Listen => 50,
50 or die "unable to start server"; 49 or die "unable to start server";
51 50
52push @listen_sockets, $http_port; 51push @listen_sockets, $http_port;
53 52
54# the "main thread" 53# the "main thread"
55async { 54async {
56 slog 1, "accepting connections"; 55 slog 1, "accepting connections";
57 while () { 56 while () {
58 $connections->down; 57 $connections->down;
59 push @fh, $http_port->accept; 58 push @newcons, [$http_port->accept];
60 #slog 3, "accepted @$connections ".scalar(@pool); 59 #slog 3, "accepted @$connections ".scalar(@pool);
61 $::NOW = time; 60 $::NOW = time;
62 if (@pool) { 61 if (@pool) {
63 (pop @pool)->ready; 62 (pop @pool)->ready;
64 } else { 63 } else {
71package conn; 70package conn;
72 71
73use Socket; 72use Socket;
74use HTTP::Date; 73use HTTP::Date;
75use 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 );
82
83Event->add_hooks(prepare => sub {
84 &Coro::cede;
85});
76 86
77our %conn; # $conn{ip}{fh} => connobj 87our %conn; # $conn{ip}{fh} => connobj
78our %blocked; 88our %blocked;
89our %mimetype;
90
91sub read_mimetypes {
92 local *M;
93 if (open M, "<mime_types") {
94 while (<M>) {
95 if (/^([^#]\S+)\t+(\S+)$/) {
96 $mimetype{lc $1} = $2;
97 }
98 }
99 } else {
100 print "cannot open mime_types\n";
101 }
102}
103
104read_mimetypes;
79 105
80sub new { 106sub new {
81 my $class = shift; 107 my $class = shift;
108 my $peername = shift;
82 my $fh = shift; 109 my $fh = shift;
83 my $self = bless { fh => $fh }, $class; 110 my $self = bless { fh => $fh }, $class;
84 my (undef, $iaddr) = unpack_sockaddr_in $fh->getpeername 111 my (undef, $iaddr) = unpack_sockaddr_in $peername
85 or $self->err(500, "unable to get peername"); 112 or $self->err(500, "unable to decode peername");
113
86 $self->{remote_addr} = inet_ntoa $iaddr; 114 $self->{remote_addr} = inet_ntoa $iaddr;
115 $self->{time} = $::NOW;
87 116
88 # enter ourselves into various lists 117 # enter ourselves into various lists
89 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 118 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
90 119
120 $::conns++;
121
91 $self; 122 $self;
92} 123}
93 124
94sub DESTROY { 125sub DESTROY {
95 my $self = shift; 126 my $self = shift;
127
128 $::conns--;
129
96 delete $conn{$self->{remote_addr}}{$self*1}; 130 delete $conn{$self->{remote_addr}}{$self*1};
97 delete $uri{$self->{uri}}{$self*1}; 131 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
98} 132}
99 133
100sub slog { 134sub slog {
101 my $self = shift; 135 my $self = shift;
102 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 136 main::slog($_[0], "$self->{remote_addr}> $_[1]");
103} 137}
104 138
105sub response { 139sub response {
106 my ($self, $code, $msg, $hdr, $content) = @_; 140 my ($self, $code, $msg, $hdr, $content) = @_;
107 my $res = "HTTP/1.0 $code $msg\015\012"; 141 my $res = "HTTP/1.1 $code $msg\015\012";
108 142
109 $res .= "Connection: close\015\012"; 143 #$res .= "Connection: close\015\012";
110 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 144 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
111 145
112 while (my ($h, $v) = each %$hdr) { 146 while (my ($h, $v) = each %$hdr) {
113 $res .= "$h: $v\015\012" 147 $res .= "$h: $v\015\012"
114 } 148 }
149 $res .= "\015\012";
115 150
116 $res .= "\015\012$content" if defined $content; 151 $res .= $content if defined $content and $self->{method} ne "HEAD";
117 152
118 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d# 153 print STDERR "$self->{remote_addr} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}." \"$self->{h}{referer}\"\n";#d#
119 154
155 $self->{written} +=
120 print {$self->{fh}} $res; 156 print {$self->{fh}} $res;
121} 157}
122 158
123sub err { 159sub err {
124 my $self = shift; 160 my $self = shift;
125 my ($code, $msg, $hdr, $content) = @_; 161 my ($code, $msg, $hdr, $content) = @_;
127 unless (defined $content) { 163 unless (defined $content) {
128 $content = "$code $msg"; 164 $content = "$code $msg";
129 $hdr->{"Content-Type"} = "text/plain"; 165 $hdr->{"Content-Type"} = "text/plain";
130 $hdr->{"Content-Length"} = length $content; 166 $hdr->{"Content-Length"} = length $content;
131 } 167 }
168 $hdr->{"Connection"} = "close";
132 169
133 $self->response($code, $msg, $hdr, $content); 170 $self->response($code, $msg, $hdr, $content);
134 171
135 die bless {}, err::; 172 die bless {}, err::;
136} 173}
137 174
138sub err_blocked { 175sub err_blocked {
139 my $self = shift; 176 my $self = shift;
140 my $ip = $self->{remote_addr}; 177 my $ip = $self->{remote_addr};
141 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 178 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
179
180 Coro::Event::do_timer(after => 15);
181
142 $self->err(403, "too many connections", 182 $self->err(401, "too many connections",
143 { 183 {
144 "Content-Type" => "text/html", 184 "Content-Type" => "text/html",
145 "Retry-After" => $::BLOCKTIME 185 "Retry-After" => $::BLOCKTIME
146 }, 186 },
147 <<EOF); 187 <<EOF);
159 199
160sub handle { 200sub handle {
161 my $self = shift; 201 my $self = shift;
162 my $fh = $self->{fh}; 202 my $fh = $self->{fh};
163 203
204 $fh->timeout($::REQ_TIMEOUT);
164 #while() { 205 while() {
206 $self->{reqs}++;
207
208 # read request and parse first line
209 my $req = $fh->readline("\015\012\015\012");
210
211 unless (defined $req) {
212 if (exists $self->{version}) {
213 last;
214 } else {
215 $self->err(408, "request timeout");
216 }
217 }
218
165 $self->{h} = {}; 219 $self->{h} = {};
166 220
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); 221 $fh->timeout($::RES_TIMEOUT);
171
172 defined $req or
173 $self->err(408, "request timeout");
174
175 my $ip = $self->{remote_addr}; 222 my $ip = $self->{remote_addr};
176 223
177 if ($blocked{$ip}) { 224 if ($blocked{$ip}) {
178 $self->err_blocked($blocked{$ip}) 225 $self->err_blocked($blocked{$ip})
179 if $blocked{$ip} > $::NOW; 226 if $blocked{$ip} > $::NOW;
180 227
181 delete $blocked{$ip}; 228 delete $blocked{$ip};
182 } 229 }
183 230
184 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 231 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
185 $self->slog("blocked ip $ip"); 232 $self->slog(2, "blocked ip $ip");
186 $self->err_blocked; 233 $self->err_blocked;
187 } 234 }
188 235
189 $req =~ /^(?:\015\012)? 236 $req =~ /^(?:\015\012)?
190 (GET|HEAD) \040+ 237 (GET|HEAD) \040+
191 ([^\040]+) \040+ 238 ([^\040]+) \040+
192 HTTP\/([0-9]+\.[0-9]+) 239 HTTP\/([0-9]+\.[0-9]+)
193 \015\012/gx 240 \015\012/gx
194 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 241 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 242
199 $self->{method} = $1; 243 $self->{method} = $1;
200 $self->{uri} = $2; 244 $self->{uri} = $2;
245 $self->{version} = $3;
246
247 $3 eq "1.0" or $3 eq "1.1"
248 or $self->err(506, "http protocol version $3 not supported");
201 249
202 # parse headers 250 # parse headers
203 { 251 {
204 my (%hdr, $h, $v); 252 my (%hdr, $h, $v);
205 253
218 while ($h, $v) = each %hdr; 266 while ($h, $v) = each %hdr;
219 } 267 }
220 268
221 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 269 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
222 270
223 weaken ($uri{$self->{uri}}{$self*1} = $self); 271 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
224 272
225 $self->map_uri; 273 $self->map_uri;
226 $self->respond; 274 $self->respond;
275
276 last if $self->{h}{connection} =~ /close/ || $self->{version} lt "1.1";
277
278 $self->slog(9, "persistant connection [".$self->{h}{"user-agent"}."][$self->{reqs}]");
279 $fh->timeout($::PER_TIMEOUT);
227 #} 280 }
228} 281}
229 282
230# uri => path mapping 283# uri => path mapping
231sub map_uri { 284sub map_uri {
232 my $self = shift; 285 my $self = shift;
244 297
245 $self->{name} = $uri; 298 $self->{name} = $uri;
246 299
247 # now do the path mapping 300 # now do the path mapping
248 $self->{path} = "$::DOCROOT/$host$uri"; 301 $self->{path} = "$::DOCROOT/$host$uri";
302
303 $self->access_check;
249} 304}
250 305
251sub server_address { 306sub server_address {
252 my $self = shift; 307 my $self = shift;
253 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname 308 my ($port, $iaddr) = unpack_sockaddr_in $self->{fh}->getsockname
287 if (chdir $::DOCROOT) { 342 if (chdir $::DOCROOT) {
288 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 343 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
289 $ENV{HTTP_HOST} = $self->server_host; 344 $ENV{HTTP_HOST} = $self->server_host;
290 $ENV{HTTP_PORT} = $self->{server_host}; 345 $ENV{HTTP_PORT} = $self->{server_host};
291 $ENV{SCRIPT_NAME} = $self->{name}; 346 $ENV{SCRIPT_NAME} = $self->{name};
292 exec $::INDEXPROG; 347 exec $path;
293 } 348 }
294 Coro::State::_exit(0); 349 Coro::State::_exit(0);
295 } else { 350 } else {
296 } 351 }
297} 352}
300 my $self = shift; 355 my $self = shift;
301 my $path = $self->{path}; 356 my $path = $self->{path};
302 357
303 stat $path 358 stat $path
304 or $self->err(404, "not found"); 359 or $self->err(404, "not found");
360
361 $self->{stat} = [stat _];
305 362
306 # idiotic netscape sends idiotic headers AGAIN 363 # idiotic netscape sends idiotic headers AGAIN
307 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 364 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
308 ? str2time $1 : 0; 365 ? str2time $1 : 0;
309 366
312 if ($path !~ /\/$/) { 369 if ($path !~ /\/$/) {
313 # create a redirect to get the trailing "/" 370 # create a redirect to get the trailing "/"
314 my $host = $self->server_hostport; 371 my $host = $self->server_hostport;
315 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 372 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
316 } else { 373 } else {
317 $ims < (stat _)[9] 374 $ims < $self->{stat}[9]
318 or $self->err(304, "not modified"); 375 or $self->err(304, "not modified");
319 376
320 if ($self->{method} eq "GET") { 377 if ($self->{method} eq "GET") {
321 if (-r "$path/index.html") { 378 if (-r "$path/index.html") {
322 $self->{path} .= "/index.html"; 379 $self->{path} .= "/index.html";
334 } 391 }
335} 392}
336 393
337sub handle_dir { 394sub handle_dir {
338 my $self = shift; 395 my $self = shift;
339 $self->_cgi($::INDEXPROG); 396 my $idx = $self->diridx;
397
398 $self->response(200, "ok",
399 {
400 "Content-Type" => "text/html",
401 "Content-Length" => length $idx,
402 },
403 $idx);
340} 404}
341 405
342sub handle_file { 406sub handle_file {
343 my $self = shift; 407 my $self = shift;
344 my $length = -s _; 408 my $length = -s _;
364 $hdr->{"Content-Range"} = "bytes */$length"; 428 $hdr->{"Content-Range"} = "bytes */$length";
365 $self->err(416, "not satisfiable", $hdr); 429 $self->err(416, "not satisfiable", $hdr);
366 430
367satisfiable: 431satisfiable:
368 # check for segmented downloads 432 # check for segmented downloads
369 if ($l && $NO_SEGMENTED) { 433 if ($l && $::NO_SEGMENTED) {
370 if (%{$uri{$self->{uri}}} > 1) { 434 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
371 $self->slog("segmented download refused\n"); 435 Coro::Event::do_timer(after => 15);
436
372 $self->err(400, "segmented downloads are not allowed"); 437 $self->err(400, "segmented downloads are not allowed");
373 } 438 }
374 } 439 }
375 440
376 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 441 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
380ignore: 445ignore:
381 } else { 446 } else {
382 ($l, $h) = (0, $length - 1); 447 ($l, $h) = (0, $length - 1);
383 } 448 }
384 449
385 if ($self->{path} =~ /\.html$/) { 450 $self->{path} =~ /\.([^.]+)$/;
386 $hdr->{"Content-Type"} = "text/html";
387 } else {
388 $hdr->{"Content-Type"} = "application/octet-stream"; 451 $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
389 }
390
391 $hdr->{"Content-Length"} = $length; 452 $hdr->{"Content-Length"} = $length;
392 453
393 $self->response(@code, $hdr, ""); 454 $self->response(@code, $hdr, "");
394 455
395 if ($self->{method} eq "GET") { 456 if ($self->{method} eq "GET") {
396 my ($fh, $buf); 457 my ($fh, $buf, $r);
458 my $current = $Coro::current;
397 open $fh, "<", $self->{path} 459 open $fh, "<", $self->{path}
398 or die "$self->{path}: late open failure ($!)"; 460 or die "$self->{path}: late open failure ($!)";
399 461
400 if ($l) {
401 sysseek $fh, $l, 0
402 or die "$self->{path}: cannot seek to $l ($!)";
403 }
404
405 $h -= $l - 1; 462 $h -= $l - 1;
406 463
407 while ($h > 0) { 464 while ($h > 0) {
408 $h -= sysread $fh, $buf, $h > $::BUFSIZE ? $::BUFSIZE : $h; 465 aio_read($fh, $l, ($h > $::BUFSIZE ? $::BUFSIZE : $h),
466 $buf, 0, sub {
467 $r = $_[0];
468 $current->ready;
469 });
470 &Coro::schedule;
471 last unless $r;
409 $self->{fh}->syswrite($buf) 472 my $w = $self->{fh}->syswrite($buf)
410 or last; 473 or last;
474 $::written += $w;
475 $self->{written} += $w;
476 $l += $r;
411 } 477 }
412 } 478 }
413 479
414 close $fh; 480 close $fh;
415} 481}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines