|
|
1 | use AnyEvent (); |
|
|
2 | |
1 | use Coro; |
3 | use Coro; |
2 | use Coro::Semaphore; |
4 | use Coro::Semaphore; |
|
|
5 | use Coro::SemaphoreSet; |
3 | use Coro::Event; |
6 | use Coro::EV; |
4 | use Coro::Socket; |
7 | use Coro::Socket; |
5 | use Coro::Signal; |
8 | use Coro::Signal; |
6 | use Coro::AIO (); |
9 | use Coro::AIO (); |
7 | |
10 | |
|
|
11 | use Fcntl; |
8 | use HTTP::Date; |
12 | use HTTP::Date; |
9 | use POSIX (); |
13 | use POSIX (); |
10 | |
14 | |
11 | use Compress::Zlib (); |
15 | use Compress::Zlib (); |
12 | |
16 | |
13 | no utf8; |
17 | use common::sense; |
14 | use bytes; |
|
|
15 | |
18 | |
16 | # at least on my machine, this thingy serves files |
19 | # at least on my machine, this thingy serves files |
17 | # quite a bit faster than apache, ;) |
20 | # quite a bit faster than apache, ;) |
18 | # and quite a bit slower than thttpd :( |
21 | # and quite a bit slower than thttpd :( |
19 | |
22 | |
20 | $SIG{PIPE} = 'IGNORE'; |
23 | $SIG{PIPE} = 'IGNORE'; |
21 | |
24 | |
22 | our $accesslog; |
25 | our $accesslog; |
23 | our $errorlog; |
26 | our $errorlog; |
|
|
27 | our @listen_sockets; |
24 | |
28 | |
25 | our $NOW; |
29 | our $NOW; |
26 | our $HTTP_NOW; |
30 | our $HTTP_NOW; |
27 | |
31 | |
28 | our $ERROR_LOG; |
32 | our $ERROR_LOG; |
29 | our $ACCESS_LOG; |
33 | our $ACCESS_LOG; |
|
|
34 | our $TRANSFER_LOCK = new Coro::SemaphoreSet; # lock to be acquired per ip |
30 | |
35 | |
31 | Event->timer(interval => 1, hard => 1, cb => sub { |
36 | our $update_time = EV::periodic 0, 1, undef, sub { |
32 | $NOW = time; |
37 | $NOW = time; |
33 | $HTTP_NOW = time2str $NOW; |
38 | $HTTP_NOW = time2str $NOW; |
34 | })->now; |
39 | }; |
|
|
40 | $update_time->invoke; |
35 | |
41 | |
36 | if ($ERROR_LOG) { |
42 | if ($ERROR_LOG) { |
37 | use IO::Handle; |
43 | use IO::Handle; |
38 | open $errorlog, ">>$ERROR_LOG" |
44 | open $errorlog, ">>$ERROR_LOG" |
39 | or die "$ERROR_LOG: $!"; |
45 | or die "$ERROR_LOG: $!"; |
… | |
… | |
48 | } |
54 | } |
49 | |
55 | |
50 | sub slog { |
56 | sub slog { |
51 | my $level = shift; |
57 | my $level = shift; |
52 | my $format = shift; |
58 | my $format = shift; |
|
|
59 | |
|
|
60 | $format = sprintf $format, @_ if @_; |
|
|
61 | |
53 | my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); |
62 | my $NOW = (POSIX::strftime "%Y-%m-%d %H:%M:%S", gmtime $::NOW); |
54 | printf "$NOW: $format\n", @_; |
63 | print "$NOW: $format\n"; |
55 | printf $errorlog "$NOW: $format\n", @_ if $errorlog; |
64 | print $errorlog "$NOW: $format\n", @_ if $errorlog; |
56 | } |
65 | } |
57 | |
66 | |
58 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
67 | our $connections = new Coro::Semaphore $::MAX_CONNECTS || 250; |
59 | our $httpevent = new Coro::Signal; |
68 | our $httpevent = new Coro::Signal; |
60 | |
69 | |
61 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
70 | our $queue_file = new transferqueue $::MAX_TRANSFERS; |
62 | our $queue_index = new transferqueue 10; |
71 | our $queue_index = new transferqueue 10; |
63 | |
72 | |
64 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
73 | our $tbf_top = new tbf rate => $::TBF_RATE || 100000; |
65 | |
74 | |
66 | my $unused_bytes = 0; |
75 | my $unused_bytes = 0; |
67 | my $unused_last = time; |
76 | my $unused_last = time; |
68 | |
77 | |
69 | sub unused_bandwidth { |
78 | sub unused_bandwidth { |
70 | $unused_bytes += $_[0]; |
79 | $unused_bytes += $_[0]; |
71 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
80 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
72 | $unused_last = $NOW; |
81 | $unused_last = $NOW; |
73 | $unused_bytes = 0; |
82 | $unused_bytes = 0; |
74 | $queue_file->force_wake_next; |
83 | $queue_file->force_wake_next |
75 | slog 1, "forced filetransfer due to unused bandwidth"; |
84 | and slog 1, "forced filetransfer due to unused bandwidth"; |
76 | } |
|
|
77 | } |
|
|
78 | |
|
|
79 | my @newcons; |
|
|
80 | my @pool; |
|
|
81 | |
|
|
82 | # one "execution thread" |
|
|
83 | sub handler { |
|
|
84 | while () { |
|
|
85 | if (@newcons) { |
|
|
86 | eval { |
|
|
87 | conn->new (@{pop @newcons})->handle; |
|
|
88 | }; |
|
|
89 | slog 1, "$@" if $@ && !ref $@; |
|
|
90 | |
|
|
91 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
92 | |
|
|
93 | $connections->up; |
|
|
94 | } else { |
|
|
95 | last if @pool >= $MAX_POOL; |
|
|
96 | push @pool, $Coro::current; |
|
|
97 | schedule; |
|
|
98 | } |
|
|
99 | } |
85 | } |
100 | } |
86 | } |
101 | |
87 | |
102 | sub listen_on { |
88 | sub listen_on { |
103 | my $listen = $_[0]; |
89 | my $listen = $_[0]; |
… | |
… | |
107 | # the "main thread" |
93 | # the "main thread" |
108 | async { |
94 | async { |
109 | slog 1, "accepting connections"; |
95 | slog 1, "accepting connections"; |
110 | while () { |
96 | while () { |
111 | $connections->down; |
97 | $connections->down; |
112 | push @newcons, [$listen->accept]; |
98 | my @conn = $listen->accept; |
113 | #slog 3, "accepted @$connections ".scalar(@pool); |
99 | #slog 3, "accepted @$connections ".scalar(@pool); |
114 | if (@pool) { |
100 | |
115 | (pop @pool)->ready; |
101 | async_pool { |
116 | } else { |
102 | eval { |
117 | async \&handler; |
103 | conn->new (@conn)->handle; |
|
|
104 | }; |
|
|
105 | slog 1, "$@" if $@ && !ref $@; |
|
|
106 | |
|
|
107 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
108 | |
|
|
109 | $connections->up; |
118 | } |
110 | } |
119 | } |
111 | } |
120 | }; |
112 | }; |
121 | } |
113 | } |
122 | |
114 | |
123 | my $http_port = new Coro::Socket |
115 | my $http_port = new Coro::Socket |
124 | LocalAddr => $SERVER_HOST, |
116 | LocalAddr => $::SERVER_HOST, |
125 | LocalPort => $SERVER_PORT, |
117 | LocalPort => $::SERVER_PORT, |
126 | ReuseAddr => 1, |
118 | ReuseAddr => 1, |
127 | Listen => 50, |
119 | Listen => 50, |
128 | or die "unable to start server"; |
120 | or die "unable to start server"; |
129 | |
121 | |
130 | listen_on $http_port; |
122 | listen_on $http_port; |
131 | |
123 | |
132 | if ($SERVER_PORT2) { |
124 | if ($::SERVER_PORT2) { |
133 | my $http_port = new Coro::Socket |
125 | my $http_port = new Coro::Socket |
134 | LocalAddr => $SERVER_HOST, |
126 | LocalAddr => $::SERVER_HOST, |
135 | LocalPort => $SERVER_PORT2, |
127 | LocalPort => $::SERVER_PORT2, |
136 | ReuseAddr => 1, |
128 | ReuseAddr => 1, |
137 | Listen => 50, |
129 | Listen => 50, |
138 | or die "unable to start server"; |
130 | or die "unable to start server"; |
139 | |
131 | |
140 | listen_on $http_port; |
132 | listen_on $http_port; |
141 | } |
133 | } |
142 | |
134 | |
143 | package conn; |
135 | package conn; |
144 | |
136 | |
145 | use strict; |
137 | use common::sense; |
146 | use bytes; |
|
|
147 | |
138 | |
148 | use Socket; |
139 | use Socket; |
149 | use HTTP::Date; |
140 | use HTTP::Date; |
150 | use Convert::Scalar 'weaken'; |
141 | use Convert::Scalar 'weaken'; |
151 | use IO::AIO; |
142 | use IO::AIO; |
|
|
143 | use AnyEvent::AIO; |
152 | |
144 | |
153 | IO::AIO::min_parallel $::AIO_PARALLEL; |
145 | IO::AIO::min_parallel $::AIO_PARALLEL; |
154 | |
|
|
155 | Event->io (fd => IO::AIO::poll_fileno, |
|
|
156 | poll => 'r', async => 1, |
|
|
157 | cb => \&IO::AIO::poll_cb); |
|
|
158 | |
146 | |
159 | our %conn; # $conn{ip}{self} => connobj |
147 | our %conn; # $conn{ip}{self} => connobj |
160 | our %uri; # $uri{ip}{uri}{self} |
148 | our %uri; # $uri{ip}{uri}{self} |
161 | our %blocked; |
149 | our %blocked; |
162 | our %mimetype; |
150 | our %mimetype; |
… | |
… | |
197 | } |
185 | } |
198 | |
186 | |
199 | sub DESTROY { |
187 | sub DESTROY { |
200 | my $self = shift; |
188 | my $self = shift; |
201 | |
189 | |
202 | close $self->{fh}; # workaround |
|
|
203 | --$::conns; |
190 | --$::conns; |
204 | } |
191 | } |
205 | |
192 | |
206 | sub prune_cache { |
193 | sub prune_cache { |
207 | my $hash = $_[0]; |
194 | my $hash = $_[0]; |
… | |
… | |
223 | for (keys %blocked) { |
210 | for (keys %blocked) { |
224 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
211 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
225 | } |
212 | } |
226 | } |
213 | } |
227 | |
214 | |
228 | Event->timer (interval => 60, cb => \&prune_caches); |
215 | our $PRUNE_WATCHER = AE::timer 60, 60, \&prune_caches; |
229 | |
216 | |
230 | sub slog { |
217 | sub slog { |
231 | my $self = shift; |
218 | my $self = shift; |
232 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
219 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
233 | } |
220 | } |
… | |
… | |
426 | sub map_uri { |
413 | sub map_uri { |
427 | my $self = shift; |
414 | my $self = shift; |
428 | my $host = $self->{server_name}; |
415 | my $host = $self->{server_name}; |
429 | my $uri = $self->{uri}; |
416 | my $uri = $self->{uri}; |
430 | |
417 | |
|
|
418 | $host =~ /[\/\\]/ |
|
|
419 | and $self->err(400, "bad request"); |
|
|
420 | |
431 | # some massaging, also makes it more secure |
421 | # some massaging, also makes it more secure |
432 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
422 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
433 | $uri =~ s%//+%/%g; |
423 | $uri =~ s%//+%/%g; |
434 | $uri =~ s%/\.(?=/|$)%%g; |
424 | $uri =~ s%/\.(?=/|$)%%g; |
435 | 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; |
425 | 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; |
… | |
… | |
482 | } else { |
472 | } else { |
483 | $self->err (404, "not found"); |
473 | $self->err (404, "not found"); |
484 | } |
474 | } |
485 | } else { |
475 | } else { |
486 | |
476 | |
487 | stat $path |
477 | Coro::AIO::aio_stat $path |
488 | or $self->err (404, "not found"); |
478 | and $self->err (404, "not found"); |
489 | |
479 | |
490 | $self->{stat} = [stat _]; |
480 | $self->{stat} = [stat _]; |
491 | |
481 | |
492 | # idiotic netscape sends idiotic headers AGAIN |
482 | # idiotic netscape sends idiotic headers AGAIN |
493 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
483 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
… | |
… | |
554 | }; |
544 | }; |
555 | |
545 | |
556 | my @code = (200, "ok"); |
546 | my @code = (200, "ok"); |
557 | my ($l, $h); |
547 | my ($l, $h); |
558 | |
548 | |
559 | if ($self->{h}{range} =~ /^bytes=(.*)$/) { |
549 | if ($self->{h}{range} =~ /^bytes=(.*)$/i) { |
560 | for (split /,/, $1) { |
550 | for (split /,/, $1) { |
561 | if (/^-(\d+)$/) { |
551 | if (/^-(\d+)$/) { |
562 | ($l, $h) = ($length - $1, $length - 1); |
552 | ($l, $h) = ($length - $1, $length - 1); |
563 | } elsif (/^(\d+)-(\d*)$/) { |
553 | } elsif (/^(\d+)-(\d*)$/) { |
564 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
554 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
… | |
… | |
573 | $self->err (416, "not satisfiable", $hdr, ""); |
563 | $self->err (416, "not satisfiable", $hdr, ""); |
574 | |
564 | |
575 | satisfiable: |
565 | satisfiable: |
576 | # check for segmented downloads |
566 | # check for segmented downloads |
577 | if ($l && $::NO_SEGMENTED) { |
567 | if ($l && $::NO_SEGMENTED) { |
578 | my $timeout = $::NOW + 15; |
568 | my $timeout = $::NOW + 60; |
579 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
569 | while (keys %{$uri{$self->{remote_id}}{$self->{uri}}} > 1) { |
580 | if ($timeout <= $::NOW) { |
570 | if ($timeout <= $::NOW) { |
581 | $self->block ($::BLOCKTIME, "segmented downloads are forbidden"); |
|
|
582 | #$self->err_segmented_download; |
571 | $self->err_segmented_download; |
583 | } else { |
572 | } else { |
584 | $httpevent->wait; |
573 | $httpevent->wait; |
585 | } |
574 | } |
586 | } |
575 | } |
587 | } |
576 | } |
… | |
… | |
603 | |
592 | |
604 | if ($self->{method} eq "GET") { |
593 | if ($self->{method} eq "GET") { |
605 | $self->{time} = $::NOW; |
594 | $self->{time} = $::NOW; |
606 | $self->{written} = 0; |
595 | $self->{written} = 0; |
607 | |
596 | |
608 | open my $fh, "<", $self->{path} |
597 | my $fh = Coro::AIO::aio_open $self->{path}, Fcntl::O_RDONLY, 0 |
609 | or die "$self->{path}: late open failure ($!)"; |
598 | or die "$self->{path}: late open failure ($!)"; |
610 | |
599 | |
611 | $h -= $l - 1; |
600 | $h -= $l - 1; |
612 | |
601 | |
613 | my $transfer = $queue->start_transfer ($h); |
602 | my $transfer = $queue->start_transfer ($h); |
614 | my $locked; |
603 | my $locked; |
615 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
604 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
616 | |
605 | |
617 | while ($h > 0) { |
606 | while ($h > 0) { |
|
|
607 | Coro::cede; |
|
|
608 | my $transfer_lock = $TRANSFER_LOCK->guard ($self->{remote_id}); |
|
|
609 | |
618 | unless ($locked) { |
610 | unless ($locked) { |
619 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
611 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
620 | $bufsize = $::BUFSIZE; |
612 | $bufsize = $::BUFSIZE; |
621 | $self->{time} = $::NOW; |
613 | $self->{time} = $::NOW; |
622 | $self->{written} = 0; |
614 | $self->{written} = 0; |
… | |
… | |
629 | } |
621 | } |
630 | |
622 | |
631 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
623 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
632 | or last; |
624 | or last; |
633 | |
625 | |
|
|
626 | # readahead to work around rijk disk issues |
|
|
627 | IO::AIO::aio_readahead $fh, $l + $bufsize, $bufsize; |
|
|
628 | |
634 | $tbf->request (length $buf); |
629 | $tbf->request (length $buf); |
635 | my $w = syswrite $self->{fh}, $buf |
630 | my $w = $self->{fh}->syswrite ($buf) |
636 | or last; |
631 | or last; |
637 | $::written += $w; |
632 | $::written += $w; |
638 | $self->{written} += $w; |
633 | $self->{written} += $w; |
639 | $l += $w; |
634 | $l += $w; |
640 | } |
635 | } |