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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines