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.17 by root, Wed Aug 15 03:06:21 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 );
82
83Event->add_hooks(prepare => sub {
84 &Coro::cede;
85});
75 86
76our %conn; # $conn{ip}{fh} => connobj 87our %conn; # $conn{ip}{fh} => connobj
77our %blocked; 88our %blocked;
78our %mimetype; 89our %mimetype;
79 90
80sub read_mimetypes { 91sub read_mimetypes {
81 local *M; 92 local *M;
82 if (open M, "<mimetypes") { 93 if (open M, "<mime_types") {
83 while (<M>) { 94 while (<M>) {
84 if (/^([^#]\S+)\t+(\S+)$/) { 95 if (/^([^#]\S+)\t+(\S+)$/) {
85 $mimetype{lc $1} = $2; 96 $mimetype{lc $1} = $2;
86 } 97 }
87 } 98 }
88 } else { 99 } else {
89 $self->slog(1, "cannot open mimetypes\n"); 100 print "cannot open mime_types\n";
90 } 101 }
91} 102}
103
104read_mimetypes;
92 105
93sub new { 106sub new {
94 my $class = shift; 107 my $class = shift;
95 my $peername = shift; 108 my $peername = shift;
96 my $fh = shift; 109 my $fh = shift;
97 my $self = bless { fh => $fh }, $class; 110 my $self = bless { fh => $fh }, $class;
98 my (undef, $iaddr) = unpack_sockaddr_in $peername 111 my (undef, $iaddr) = unpack_sockaddr_in $peername
99 or $self->err(500, "unable to decode peername"); 112 or $self->err(500, "unable to decode peername");
100 113
101 $self->{remote_addr} = inet_ntoa $iaddr; 114 $self->{remote_addr} = inet_ntoa $iaddr;
115 $self->{time} = $::NOW;
102 116
103 # enter ourselves into various lists 117 # enter ourselves into various lists
104 weaken ($conn{$self->{remote_addr}}{$self*1} = $self); 118 weaken ($conn{$self->{remote_addr}}{$self*1} = $self);
105 119
120 $::conns++;
121
106 $self; 122 $self;
107} 123}
108 124
109sub DESTROY { 125sub DESTROY {
110 my $self = shift; 126 my $self = shift;
127
128 $::conns--;
129
111 delete $conn{$self->{remote_addr}}{$self*1}; 130 delete $conn{$self->{remote_addr}}{$self*1};
112 delete $uri{$self->{uri}}{$self*1}; 131 delete $uri{$self->{remote_addr}}{$self->{uri}}{$self*1};
113} 132}
114 133
115sub slog { 134sub slog {
116 my $self = shift; 135 my $self = shift;
117 main::slog($_[0], "$self->{remote_addr}> $_[1]"); 136 main::slog($_[0], "$self->{remote_addr}> $_[1]");
118} 137}
119 138
120sub response { 139sub response {
121 my ($self, $code, $msg, $hdr, $content) = @_; 140 my ($self, $code, $msg, $hdr, $content) = @_;
122 my $res = "HTTP/1.0 $code $msg\015\012"; 141 my $res = "HTTP/1.1 $code $msg\015\012";
123 142
124 $res .= "Connection: close\015\012"; 143 #$res .= "Connection: close\015\012";
125 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :( 144 $res .= "Date: ".(time2str $::NOW)."\015\012"; # slow? nah. :(
126 145
127 while (my ($h, $v) = each %$hdr) { 146 while (my ($h, $v) = each %$hdr) {
128 $res .= "$h: $v\015\012" 147 $res .= "$h: $v\015\012"
129 } 148 }
149 $res .= "\015\012";
130 150
131 $res .= "\015\012$content" if defined $content; 151 $res .= $content if defined $content and $self->{method} ne "HEAD";
132 152
133 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#
134 154
155 $self->{written} +=
135 print {$self->{fh}} $res; 156 print {$self->{fh}} $res;
136} 157}
137 158
138sub err { 159sub err {
139 my $self = shift; 160 my $self = shift;
140 my ($code, $msg, $hdr, $content) = @_; 161 my ($code, $msg, $hdr, $content) = @_;
142 unless (defined $content) { 163 unless (defined $content) {
143 $content = "$code $msg"; 164 $content = "$code $msg";
144 $hdr->{"Content-Type"} = "text/plain"; 165 $hdr->{"Content-Type"} = "text/plain";
145 $hdr->{"Content-Length"} = length $content; 166 $hdr->{"Content-Length"} = length $content;
146 } 167 }
168 $hdr->{"Connection"} = "close";
147 169
148 $self->response($code, $msg, $hdr, $content); 170 $self->response($code, $msg, $hdr, $content);
149 171
150 die bless {}, err::; 172 die bless {}, err::;
151} 173}
152 174
153sub err_blocked { 175sub err_blocked {
154 my $self = shift; 176 my $self = shift;
155 my $ip = $self->{remote_addr}; 177 my $ip = $self->{remote_addr};
156 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME; 178 my $time = time2str $blocked{$ip} = $::NOW + $::BLOCKTIME;
179
180 Coro::Event::do_timer(after => 15);
181
157 $self->err(403, "too many connections", 182 $self->err(401, "too many connections",
158 { 183 {
159 "Content-Type" => "text/html", 184 "Content-Type" => "text/html",
160 "Retry-After" => $::BLOCKTIME 185 "Retry-After" => $::BLOCKTIME
161 }, 186 },
162 <<EOF); 187 <<EOF);
174 199
175sub handle { 200sub handle {
176 my $self = shift; 201 my $self = shift;
177 my $fh = $self->{fh}; 202 my $fh = $self->{fh};
178 203
204 $fh->timeout($::REQ_TIMEOUT);
179 #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
180 $self->{h} = {}; 219 $self->{h} = {};
181 220
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); 221 $fh->timeout($::RES_TIMEOUT);
186
187 defined $req or
188 $self->err(408, "request timeout");
189
190 my $ip = $self->{remote_addr}; 222 my $ip = $self->{remote_addr};
191 223
192 if ($blocked{$ip}) { 224 if ($blocked{$ip}) {
193 $self->err_blocked($blocked{$ip}) 225 $self->err_blocked($blocked{$ip})
194 if $blocked{$ip} > $::NOW; 226 if $blocked{$ip} > $::NOW;
195 227
196 delete $blocked{$ip}; 228 delete $blocked{$ip};
197 } 229 }
198 230
199 if (%{$conn{$ip}} > $::MAX_CONN_IP) { 231 if (%{$conn{$ip}} > $::MAX_CONN_IP) {
200 $self->slog("blocked ip $ip"); 232 $self->slog(2, "blocked ip $ip");
201 $self->err_blocked; 233 $self->err_blocked;
202 } 234 }
203 235
204 $req =~ /^(?:\015\012)? 236 $req =~ /^(?:\015\012)?
205 (GET|HEAD) \040+ 237 (GET|HEAD) \040+
206 ([^\040]+) \040+ 238 ([^\040]+) \040+
207 HTTP\/([0-9]+\.[0-9]+) 239 HTTP\/([0-9]+\.[0-9]+)
208 \015\012/gx 240 \015\012/gx
209 or $self->err(403, "method not allowed", { Allow => "GET,HEAD" }); 241 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 242
214 $self->{method} = $1; 243 $self->{method} = $1;
215 $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");
216 249
217 # parse headers 250 # parse headers
218 { 251 {
219 my (%hdr, $h, $v); 252 my (%hdr, $h, $v);
220 253
233 while ($h, $v) = each %hdr; 266 while ($h, $v) = each %hdr;
234 } 267 }
235 268
236 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80; 269 $self->{server_port} = $self->{h}{host} =~ s/:([0-9]+)$// ? $1 : 80;
237 270
238 weaken ($uri{$self->{uri}}{$self*1} = $self); 271 weaken ($uri{$self->{remote_addr}}{$self->{uri}}{$self*1} = $self);
239 272
240 $self->map_uri; 273 $self->map_uri;
241 $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);
242 #} 280 }
243} 281}
244 282
245# uri => path mapping 283# uri => path mapping
246sub map_uri { 284sub map_uri {
247 my $self = shift; 285 my $self = shift;
304 if (chdir $::DOCROOT) { 342 if (chdir $::DOCROOT) {
305 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike 343 $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
306 $ENV{HTTP_HOST} = $self->server_host; 344 $ENV{HTTP_HOST} = $self->server_host;
307 $ENV{HTTP_PORT} = $self->{server_host}; 345 $ENV{HTTP_PORT} = $self->{server_host};
308 $ENV{SCRIPT_NAME} = $self->{name}; 346 $ENV{SCRIPT_NAME} = $self->{name};
309 exec $::INDEXPROG; 347 exec $path;
310 } 348 }
311 Coro::State::_exit(0); 349 Coro::State::_exit(0);
312 } else { 350 } else {
313 } 351 }
314} 352}
317 my $self = shift; 355 my $self = shift;
318 my $path = $self->{path}; 356 my $path = $self->{path};
319 357
320 stat $path 358 stat $path
321 or $self->err(404, "not found"); 359 or $self->err(404, "not found");
360
361 $self->{stat} = [stat _];
322 362
323 # idiotic netscape sends idiotic headers AGAIN 363 # idiotic netscape sends idiotic headers AGAIN
324 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ 364 my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
325 ? str2time $1 : 0; 365 ? str2time $1 : 0;
326 366
329 if ($path !~ /\/$/) { 369 if ($path !~ /\/$/) {
330 # create a redirect to get the trailing "/" 370 # create a redirect to get the trailing "/"
331 my $host = $self->server_hostport; 371 my $host = $self->server_hostport;
332 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" }); 372 $self->err(301, "moved permanently", { Location => "http://$host$self->{uri}/" });
333 } else { 373 } else {
334 $ims < (stat _)[9] 374 $ims < $self->{stat}[9]
335 or $self->err(304, "not modified"); 375 or $self->err(304, "not modified");
336 376
337 if ($self->{method} eq "GET") { 377 if ($self->{method} eq "GET") {
338 if (-r "$path/index.html") { 378 if (-r "$path/index.html") {
339 $self->{path} .= "/index.html"; 379 $self->{path} .= "/index.html";
351 } 391 }
352} 392}
353 393
354sub handle_dir { 394sub handle_dir {
355 my $self = shift; 395 my $self = shift;
356 $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);
357} 404}
358 405
359sub handle_file { 406sub handle_file {
360 my $self = shift; 407 my $self = shift;
361 my $length = -s _; 408 my $length = -s _;
381 $hdr->{"Content-Range"} = "bytes */$length"; 428 $hdr->{"Content-Range"} = "bytes */$length";
382 $self->err(416, "not satisfiable", $hdr); 429 $self->err(416, "not satisfiable", $hdr);
383 430
384satisfiable: 431satisfiable:
385 # check for segmented downloads 432 # check for segmented downloads
386 if ($l && $NO_SEGMENTED) { 433 if ($l && $::NO_SEGMENTED) {
387 if (%{$uri{$self->{uri}}} > 1) { 434 if (%{$uri{$self->{remote_addr}}{$self->{uri}}} > 1) {
388 $self->slog("segmented download refused\n"); 435 Coro::Event::do_timer(after => 15);
436
389 $self->err(400, "segmented downloads are not allowed"); 437 $self->err(400, "segmented downloads are not allowed");
390 } 438 }
391 } 439 }
392 440
393 $hdr->{"Content-Range"} = "bytes $l-$h/$length"; 441 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
404 $hdr->{"Content-Length"} = $length; 452 $hdr->{"Content-Length"} = $length;
405 453
406 $self->response(@code, $hdr, ""); 454 $self->response(@code, $hdr, "");
407 455
408 if ($self->{method} eq "GET") { 456 if ($self->{method} eq "GET") {
409 my ($fh, $buf); 457 my ($fh, $buf, $r);
458 my $current = $Coro::current;
410 open $fh, "<", $self->{path} 459 open $fh, "<", $self->{path}
411 or die "$self->{path}: late open failure ($!)"; 460 or die "$self->{path}: late open failure ($!)";
412 461
413 if ($l) {
414 sysseek $fh, $l, 0
415 or die "$self->{path}: cannot seek to $l ($!)";
416 }
417
418 $h -= $l - 1; 462 $h -= $l - 1;
419 463
420 while ($h > 0) { 464 while ($h > 0) {
421 $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;
422 $self->{fh}->syswrite($buf) 472 my $w = $self->{fh}->syswrite($buf)
423 or last; 473 or last;
474 $::written += $w;
475 $self->{written} += $w;
476 $l += $r;
424 } 477 }
425 } 478 }
426 479
427 close $fh; 480 close $fh;
428} 481}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines