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