ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Coro/myhttpd/httpd.pl
Revision: 1.74
Committed: Fri Oct 25 13:51:39 2002 UTC (21 years, 8 months ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.73: +1 -1 lines
Log Message:
*** empty log message ***

File Contents

# User Rev Content
1 root 1.1 use Coro;
2     use Coro::Semaphore;
3     use Coro::Event;
4     use Coro::Socket;
5 root 1.44 use Coro::Signal;
6 root 1.1
7 root 1.32 use HTTP::Date;
8 root 1.47 use POSIX ();
9 root 1.32
10 root 1.63 use Compress::Zlib ();
11    
12 root 1.1 no utf8;
13     use bytes;
14    
15     # at least on my machine, this thingy serves files
16     # quite a bit faster than apache, ;)
17     # and quite a bit slower than thttpd :(
18    
19     $SIG{PIPE} = 'IGNORE';
20 root 1.27
21     our $accesslog;
22 root 1.56 our $errorlog;
23    
24     our $NOW;
25     our $HTTP_NOW;
26    
27     Event->timer(interval => 1, hard => 1, cb => sub {
28     $NOW = time;
29     $HTTP_NOW = time2str $NOW;
30     })->now;
31    
32     if ($ERROR_LOG) {
33     use IO::Handle;
34     open $errorlog, ">>$ERROR_LOG"
35     or die "$ERROR_LOG: $!";
36     $errorlog->autoflush(1);
37     }
38 root 1.27
39     if ($ACCESS_LOG) {
40     use IO::Handle;
41     open $accesslog, ">>$ACCESS_LOG"
42     or die "$ACCESS_LOG: $!";
43     $accesslog->autoflush(1);
44     }
45    
46 root 1.1 sub slog {
47     my $level = shift;
48     my $format = shift;
49 root 1.56 my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW);
50     printf "$NOW: $format\n", @_;
51     printf $errorlog "$NOW: $format\n", @_ if $errorlog;
52 root 1.1 }
53    
54 root 1.32 our $connections = new Coro::Semaphore $MAX_CONNECTS || 250;
55 root 1.44 our $httpevent = new Coro::Signal;
56 root 1.34
57 root 1.63 our $queue_file = new transferqueue $MAX_TRANSFERS;
58     our $queue_index = new transferqueue 10;
59 root 1.62
60 root 1.72 our $tbf_top = new tbf rate => $TBF_RATE || 100000;
61 root 1.67
62 root 1.68 my $unused_bytes = 0;
63     my $unused_last = time;
64    
65     sub unused_bandwidth {
66     $unused_bytes += $_[0];
67     if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) {
68     $unused_last = $NOW;
69     $unused_bytes = 0;
70     $queue_file->force_wake_next;
71     slog 1, "forced filetransfer due to unused bandwidth";
72     }
73     }
74    
75 root 1.6 my @newcons;
76 root 1.1 my @pool;
77    
78 root 1.2 # one "execution thread"
79 root 1.1 sub handler {
80     while () {
81 root 1.38 if (@newcons) {
82 root 1.1 eval {
83 root 1.38 conn->new(@{pop @newcons})->handle;
84 root 1.1 };
85     slog 1, "$@" if $@ && !ref $@;
86 root 1.56
87     $httpevent->broadcast; # only for testing, but doesn't matter much
88    
89 root 1.1 $connections->up;
90     } else {
91     last if @pool >= $MAX_POOL;
92     push @pool, $Coro::current;
93     schedule;
94     }
95     }
96     }
97    
98 root 1.40 sub listen_on {
99     my $listen = $_[0];
100    
101     push @listen_sockets, $listen;
102    
103     # the "main thread"
104     async {
105     slog 1, "accepting connections";
106     while () {
107     $connections->down;
108     push @newcons, [$listen->accept];
109     #slog 3, "accepted @$connections ".scalar(@pool);
110     if (@pool) {
111     (pop @pool)->ready;
112     } else {
113     async \&handler;
114     }
115     }
116     };
117     }
118    
119 root 1.4 my $http_port = new Coro::Socket
120     LocalAddr => $SERVER_HOST,
121     LocalPort => $SERVER_PORT,
122     ReuseAddr => 1,
123 root 1.13 Listen => 50,
124 root 1.4 or die "unable to start server";
125    
126 root 1.40 listen_on $http_port;
127    
128 root 1.41 if ($SERVER_PORT2) {
129     my $http_port = new Coro::Socket
130     LocalAddr => $SERVER_HOST,
131     LocalPort => $SERVER_PORT2,
132     ReuseAddr => 1,
133     Listen => 50,
134     or die "unable to start server";
135 root 1.40
136 root 1.41 listen_on $http_port;
137     }
138 root 1.4
139 root 1.1 package conn;
140    
141     use Socket;
142     use HTTP::Date;
143 root 1.2 use Convert::Scalar 'weaken';
144 root 1.16 use Linux::AIO;
145    
146     Linux::AIO::min_parallel $::AIO_PARALLEL;
147    
148     Event->io(fd => Linux::AIO::poll_fileno,
149 root 1.17 poll => 'r', async => 1,
150 root 1.21 cb => \&Linux::AIO::poll_cb);
151 root 1.16
152 root 1.26 our %conn; # $conn{ip}{self} => connobj
153     our %uri; # $uri{ip}{uri}{self}
154 root 1.3 our %blocked;
155 root 1.9 our %mimetype;
156    
157     sub read_mimetypes {
158     local *M;
159 root 1.10 if (open M, "<mime_types") {
160 root 1.9 while (<M>) {
161     if (/^([^#]\S+)\t+(\S+)$/) {
162     $mimetype{lc $1} = $2;
163     }
164     }
165     } else {
166 root 1.10 print "cannot open mime_types\n";
167 root 1.9 }
168     }
169 root 1.1
170 root 1.10 read_mimetypes;
171    
172 root 1.1 sub new {
173     my $class = shift;
174 root 1.42 my $fh = shift;
175 root 1.6 my $peername = shift;
176 root 1.2 my $self = bless { fh => $fh }, $class;
177 root 1.6 my (undef, $iaddr) = unpack_sockaddr_in $peername
178     or $self->err(500, "unable to decode peername");
179 root 1.7
180 root 1.50 $self->{remote_addr} =
181     $self->{remote_id} = inet_ntoa $iaddr;
182 root 1.60
183 root 1.11 $self->{time} = $::NOW;
184 root 1.2
185 root 1.49 weaken ($Coro::current->{conn} = $self);
186    
187 root 1.13 $::conns++;
188 root 1.53 $::maxconns = $::conns if $::conns > $::maxconns;
189 root 1.13
190 root 1.2 $self;
191     }
192    
193     sub DESTROY {
194 root 1.56 #my $self = shift;
195 root 1.13 $::conns--;
196 root 1.1 }
197    
198 root 1.73 sub prune_cache {
199     my $hash = $_[0];
200    
201     for (keys %$hash) {
202     if (ref $hash->{$_} eq HASH::) {
203     prune_cache($hash->{$_});
204     unless (scalar keys %{$hash->{$_}}) {
205     delete $hash->{$_};
206     $d2++;
207     }
208     }
209     }
210     }
211    
212     sub prune_caches {
213     prune_cache \%conn;
214     prune_cache \%uri;
215    
216     for (keys %blocked) {
217 root 1.74 delete $blocked{$_} unless $blocked{$_}[0] > $::NOW;
218 root 1.73 }
219     }
220    
221     Event->timer(interval => 60, cb => \&prune_caches);
222    
223 root 1.1 sub slog {
224 root 1.4 my $self = shift;
225 root 1.50 main::slog($_[0], "$self->{remote_id}> $_[1]");
226 root 1.1 }
227    
228 root 1.4 sub response {
229 root 1.1 my ($self, $code, $msg, $hdr, $content) = @_;
230 root 1.17 my $res = "HTTP/1.1 $code $msg\015\012";
231 root 1.63 my $GZ = "";
232 root 1.1
233 root 1.46 if (exists $hdr->{Connection}) {
234     if ($hdr->{Connection} =~ /close/) {
235     $self->{h}{connection} = "close"
236     }
237     } else {
238     if ($self->{version} < 1.1) {
239     if ($self->{h}{connection} =~ /keep-alive/i) {
240     $hdr->{Connection} = "Keep-Alive";
241     } else {
242     $self->{h}{connection} = "close"
243     }
244     }
245     }
246 root 1.28
247 root 1.63 if ($self->{method} ne "HEAD"
248     && $self->{h}{"accept-encoding"} =~ /\bgzip\b/
249 root 1.64 && 400 < length $content
250 root 1.63 && $hdr->{"Content-Length"} == length $content
251     && !exists $hdr->{"Content-Encoding"}
252     ) {
253     my $orig = length $content;
254     $hdr->{"Content-Encoding"} = "gzip";
255     $content = Compress::Zlib::memGzip(\$content);
256     $hdr->{"Content-Length"} = length $content;
257     $GZ = sprintf "GZ%02d", 100 - 100*((length $content) / $orig);
258     }
259    
260 root 1.32 $res .= "Date: $HTTP_NOW\015\012";
261 root 1.71 $res .= "Server: $::NAME\015\012";
262 root 1.1
263     while (my ($h, $v) = each %$hdr) {
264     $res .= "$h: $v\015\012"
265     }
266 root 1.10 $res .= "\015\012";
267 root 1.4
268 root 1.13 $res .= $content if defined $content and $self->{method} ne "HEAD";
269 root 1.1
270 root 1.65 my $log = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW).
271 root 1.63 " $self->{remote_id} \"$self->{uri}\" $code ".$hdr->{"Content-Length"}.$GZ.
272 root 1.50 " \"$self->{h}{referer}\"\n";
273 root 1.27
274 root 1.65 print $::accesslog $log if $::accesslog;
275 root 1.27 print STDERR $log;
276 root 1.2
277 root 1.67 $tbf_top->request(length $res, 1e6);
278     $self->{written} += print {$self->{fh}} $res;
279 root 1.1 }
280    
281     sub err {
282     my $self = shift;
283     my ($code, $msg, $hdr, $content) = @_;
284    
285     unless (defined $content) {
286 root 1.35 $content = "$code $msg\n";
287 root 1.1 $hdr->{"Content-Type"} = "text/plain";
288     $hdr->{"Content-Length"} = length $content;
289     }
290 root 1.17 $hdr->{"Connection"} = "close";
291 root 1.1
292 root 1.4 $self->response($code, $msg, $hdr, $content);
293 root 1.1
294     die bless {}, err::;
295     }
296    
297     sub handle {
298     my $self = shift;
299     my $fh = $self->{fh};
300    
301 root 1.29 my $host;
302    
303 root 1.17 $fh->timeout($::REQ_TIMEOUT);
304     while() {
305     $self->{reqs}++;
306 root 1.1
307     # read request and parse first line
308     my $req = $fh->readline("\015\012\015\012");
309    
310 root 1.17 unless (defined $req) {
311     if (exists $self->{version}) {
312     last;
313     } else {
314     $self->err(408, "request timeout");
315     }
316     }
317    
318     $self->{h} = {};
319 root 1.1
320 root 1.17 $fh->timeout($::RES_TIMEOUT);
321 root 1.3
322 root 1.1 $req =~ /^(?:\015\012)?
323     (GET|HEAD) \040+
324     ([^\040]+) \040+
325     HTTP\/([0-9]+\.[0-9]+)
326     \015\012/gx
327 root 1.14 or $self->err(405, "method not allowed", { Allow => "GET,HEAD" });
328 root 1.1
329     $self->{method} = $1;
330     $self->{uri} = $2;
331 root 1.17 $self->{version} = $3;
332    
333 root 1.20 $3 =~ /^1\./
334 root 1.17 or $self->err(506, "http protocol version $3 not supported");
335 root 1.1
336     # parse headers
337     {
338     my (%hdr, $h, $v);
339    
340     $hdr{lc $1} .= ",$2"
341     while $req =~ /\G
342     ([^:\000-\040]+):
343 root 1.66 [\011\040]*
344     ((?: [^\015\012]+ | \015\012[\011\040] )*)
345 root 1.1 \015\012
346     /gxc;
347    
348     $req =~ /\G\015\012$/
349     or $self->err(400, "bad request");
350    
351     $self->{h}{$h} = substr $v, 1
352     while ($h, $v) = each %hdr;
353     }
354    
355 root 1.36 # remote id should be unique per user
356     my $id = $self->{remote_addr};
357    
358     if (exists $self->{h}{"client-ip"}) {
359     $id .= "[".$self->{h}{"client-ip"}."]";
360     } elsif (exists $self->{h}{"x-forwarded-for"}) {
361     $id .= "[".$self->{h}{"x-forwarded-for"}."]";
362     }
363    
364     $self->{remote_id} = $id;
365    
366 root 1.56 weaken (local $conn{$id}{$self*1} = $self);
367    
368 root 1.36 if ($blocked{$id}) {
369 root 1.56 $self->err_blocked
370     if $blocked{$id}[0] > $::NOW;
371 root 1.36
372     delete $blocked{$id};
373     }
374    
375 root 1.29 # find out server name and port
376     if ($self->{uri} =~ s/^http:\/\/([^\/?#]*)//i) {
377     $host = $1;
378     } else {
379     $host = $self->{h}{host};
380     }
381    
382     if (defined $host) {
383     $self->{server_port} = $host =~ s/:([0-9]+)$// ? $1 : 80;
384     } else {
385     ($self->{server_port}, $host)
386 root 1.43 = unpack_sockaddr_in $self->{fh}->sockname
387 root 1.29 or $self->err(500, "unable to get socket name");
388     $host = inet_ntoa $host;
389     }
390    
391     $self->{server_name} = $host;
392    
393 root 1.56 weaken (local $uri{$id}{$self->{uri}}{$self*1} = $self);
394 root 1.1
395 root 1.24 eval {
396     $self->map_uri;
397     $self->respond;
398     };
399    
400     die if $@ && !ref $@;
401 root 1.17
402 root 1.56 last if $self->{h}{connection} =~ /close/i;
403 root 1.17
404 root 1.44 $httpevent->broadcast;
405    
406 root 1.17 $fh->timeout($::PER_TIMEOUT);
407     }
408 root 1.1 }
409    
410 root 1.56 sub block {
411     my $self = shift;
412    
413     $blocked{$self->{remote_id}} = [$::NOW + $_[0], $_[1]];
414     $self->slog(2, "blocked ip $self->{remote_id}");
415     $self->err_blocked;
416     }
417    
418 root 1.1 # uri => path mapping
419     sub map_uri {
420     my $self = shift;
421 root 1.29 my $host = $self->{server_name};
422 root 1.1 my $uri = $self->{uri};
423    
424     # some massaging, also makes it more secure
425     $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge;
426     $uri =~ s%//+%/%g;
427     $uri =~ s%/\.(?=/|$)%%g;
428     1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%;
429    
430     $uri =~ m%^/?\.\.(?=/|$)%
431     and $self->err(400, "bad request");
432    
433     $self->{name} = $uri;
434    
435     # now do the path mapping
436     $self->{path} = "$::DOCROOT/$host$uri";
437 root 1.7
438     $self->access_check;
439 root 1.1 }
440    
441     sub _cgi {
442     my $self = shift;
443     my $path = shift;
444     my $fh;
445    
446     # no two-way xxx supported
447     if (0 == fork) {
448     open STDOUT, ">&".fileno($self->{fh});
449     if (chdir $::DOCROOT) {
450     $ENV{SERVER_SOFTWARE} = "thttpd-myhttpd"; # we are thttpd-alike
451 root 1.29 $ENV{HTTP_HOST} = $self->{server_name};
452     $ENV{HTTP_PORT} = $self->{server_port};
453 root 1.1 $ENV{SCRIPT_NAME} = $self->{name};
454 root 1.10 exec $path;
455 root 1.1 }
456     Coro::State::_exit(0);
457     } else {
458 root 1.29 die;
459 root 1.1 }
460     }
461    
462 root 1.29 sub server_hostport {
463     $_[0]{server_port} == 80
464     ? $_[0]{server_name}
465     : "$_[0]{server_name}:$_[0]{server_port}";
466     }
467    
468 root 1.1 sub respond {
469     my $self = shift;
470     my $path = $self->{path};
471    
472 root 1.49 if ($self->{name} =~ s%^/internal/([^/]+)%%) {
473     if ($::internal{$1}) {
474     $::internal{$1}->($self);
475     } else {
476     $self->err(404, "not found");
477     }
478     } else {
479 root 1.1
480 root 1.49 stat $path
481     or $self->err(404, "not found");
482 root 1.10
483 root 1.49 $self->{stat} = [stat _];
484 root 1.1
485 root 1.49 # idiotic netscape sends idiotic headers AGAIN
486     my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/
487     ? str2time $1 : 0;
488    
489     if (-d _ && -r _) {
490     # directory
491     if ($path !~ /\/$/) {
492     # create a redirect to get the trailing "/"
493     # we don't try to avoid the :80
494     $self->err(301, "moved permanently", { Location => "http://".$self->server_hostport."$self->{uri}/" });
495 root 1.25 } else {
496 root 1.49 $ims < $self->{stat}[9]
497     or $self->err(304, "not modified");
498    
499     if (-r "$path/index.html") {
500     # replace directory "size" by index.html filesize
501 root 1.55 $self->{stat} = [stat ($self->{path} .= "/index.html")];
502 root 1.67 $self->handle_file($queue_index, $tbf_top);
503 root 1.49 } else {
504     $self->handle_dir;
505     }
506 root 1.1 }
507 root 1.49 } elsif (-f _ && -r _) {
508     -x _ and $self->err(403, "forbidden");
509 root 1.56
510 root 1.58 if (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
511 root 1.56 my $timeout = $::NOW + 10;
512 root 1.58 while (keys %{$conn{$self->{remote_id}}} > $::MAX_TRANSFERS_IP) {
513 root 1.56 if ($timeout < $::NOW) {
514     $self->block($::BLOCKTIME, "too many connections");
515     } else {
516     $httpevent->wait;
517     }
518     }
519     }
520    
521 root 1.67 $self->handle_file($queue_file, $tbf_top);
522 root 1.49 } else {
523     $self->err(404, "not found");
524 root 1.1 }
525     }
526     }
527    
528     sub handle_dir {
529     my $self = shift;
530 root 1.10 my $idx = $self->diridx;
531    
532     $self->response(200, "ok",
533     {
534     "Content-Type" => "text/html",
535     "Content-Length" => length $idx,
536 root 1.55 "Last-Modified" => time2str ($self->{stat}[9]),
537 root 1.10 },
538     $idx);
539 root 1.1 }
540    
541     sub handle_file {
542 root 1.67 my ($self, $queue, $tbf) = @_;
543 root 1.34 my $length = $self->{stat}[7];
544 root 1.1 my $hdr = {
545     "Last-Modified" => time2str ((stat _)[9]),
546     };
547    
548     my @code = (200, "ok");
549     my ($l, $h);
550    
551     if ($self->{h}{range} =~ /^bytes=(.*)$/) {
552     for (split /,/, $1) {
553     if (/^-(\d+)$/) {
554     ($l, $h) = ($length - $1, $length - 1);
555     } elsif (/^(\d+)-(\d*)$/) {
556     ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1);
557     } else {
558     ($l, $h) = (0, $length - 1);
559     goto ignore;
560     }
561 root 1.26 goto satisfiable if $l >= 0 && $l < $length && $h >= 0 && $h >= $l;
562 root 1.1 }
563     $hdr->{"Content-Range"} = "bytes */$length";
564 root 1.24 $hdr->{"Content-Length"} = $length;
565     $self->err(416, "not satisfiable", $hdr, "");
566 root 1.1
567     satisfiable:
568 root 1.4 # check for segmented downloads
569 root 1.10 if ($l && $::NO_SEGMENTED) {
570 root 1.56 my $timeout = $::NOW + 15;
571 root 1.58 while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) {
572 root 1.56 if ($timeout <= $::NOW) {
573     $self->block($::BLOCKTIME, "segmented downloads are forbidden");
574     #$self->err_segmented_download;
575 root 1.29 } else {
576 root 1.46 $httpevent->wait;
577 root 1.29 }
578 root 1.4 }
579     }
580    
581 root 1.1 $hdr->{"Content-Range"} = "bytes $l-$h/$length";
582     @code = (206, "partial content");
583     $length = $h - $l + 1;
584    
585     ignore:
586     } else {
587     ($l, $h) = (0, $length - 1);
588     }
589    
590 root 1.9 $self->{path} =~ /\.([^.]+)$/;
591     $hdr->{"Content-Type"} = $mimetype{lc $1} || "application/octet-stream";
592 root 1.1 $hdr->{"Content-Length"} = $length;
593    
594 root 1.4 $self->response(@code, $hdr, "");
595 root 1.1
596     if ($self->{method} eq "GET") {
597 root 1.32 $self->{time} = $::NOW;
598 root 1.71 $self->{written} = 0;
599 root 1.32
600 root 1.49 my $current = $Coro::current;
601 root 1.35
602 root 1.49 my ($fh, $buf, $r);
603 root 1.32
604 root 1.1 open $fh, "<", $self->{path}
605     or die "$self->{path}: late open failure ($!)";
606    
607     $h -= $l - 1;
608    
609 root 1.49 if (0) { # !AIO
610 root 1.19 if ($l) {
611     sysseek $fh, $l, 0;
612     }
613     }
614 root 1.48
615 root 1.56 my $transfer = $queue->start_transfer($h);
616 root 1.51 my $locked;
617 root 1.48 my $bufsize = $::WAIT_BUFSIZE; # initial buffer size
618 root 1.19
619 root 1.1 while ($h > 0) {
620 root 1.51 unless ($locked) {
621     if ($locked ||= $transfer->try($::WAIT_INTERVAL)) {
622 root 1.48 $bufsize = $::BUFSIZE;
623     $self->{time} = $::NOW;
624 root 1.71 $self->{written} = 0;
625 root 1.48 }
626 root 1.56 }
627    
628     if ($blocked{$self->{remote_id}}) {
629     $self->{h}{connection} = "close";
630 root 1.59 die bless {}, err::;
631 root 1.48 }
632    
633 root 1.49 if (0) { # !AIO
634 root 1.48 sysread $fh, $buf, $h > $bufsize ? $bufsize : $h
635 root 1.19 or last;
636     } else {
637 root 1.48 aio_read($fh, $l, ($h > $bufsize ? $bufsize : $h),
638 root 1.19 $buf, 0, sub {
639     $r = $_[0];
640 root 1.37 Coro::ready($current);
641 root 1.19 });
642     &Coro::schedule;
643     last unless $r;
644     }
645 root 1.67
646     $tbf->request(length $buf);
647 root 1.37 my $w = syswrite $self->{fh}, $buf
648 root 1.1 or last;
649 root 1.11 $::written += $w;
650     $self->{written} += $w;
651 root 1.16 $l += $r;
652 root 1.1 }
653 root 1.32
654     close $fh;
655 root 1.1 }
656 root 1.7 }
657    
658 root 1.2 1;