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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines