|
|
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::EV; |
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 | our $update_time = EV::periodic 0, 1, undef, 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 | }; |
39 | }; |
… | |
… | |
49 | } |
54 | } |
50 | |
55 | |
51 | sub slog { |
56 | sub slog { |
52 | my $level = shift; |
57 | my $level = shift; |
53 | my $format = shift; |
58 | my $format = shift; |
|
|
59 | |
|
|
60 | $format = sprintf $format, @_ if @_; |
|
|
61 | |
54 | 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); |
55 | printf "$NOW: $format\n", @_; |
63 | print "$NOW: $format\n"; |
56 | printf $errorlog "$NOW: $format\n", @_ if $errorlog; |
64 | print $errorlog "$NOW: $format\n", @_ if $errorlog; |
57 | } |
65 | } |
58 | |
66 | |
59 | our $connections = new Coro::Semaphore $MAX_CONNECTS || 250; |
67 | our $connections = new Coro::Semaphore $::MAX_CONNECTS || 250; |
60 | our $httpevent = new Coro::Signal; |
68 | our $httpevent = new Coro::Signal; |
61 | |
69 | |
62 | our $queue_file = new transferqueue $MAX_TRANSFERS; |
70 | our $queue_file = new transferqueue $::MAX_TRANSFERS; |
63 | our $queue_index = new transferqueue 10; |
71 | our $queue_index = new transferqueue 10; |
64 | |
72 | |
65 | our $tbf_top = new tbf rate => $TBF_RATE || 100000; |
73 | our $tbf_top = new tbf rate => $::TBF_RATE || 100000; |
66 | |
74 | |
67 | my $unused_bytes = 0; |
75 | my $unused_bytes = 0; |
68 | my $unused_last = time; |
76 | my $unused_last = time; |
69 | |
77 | |
70 | sub unused_bandwidth { |
78 | sub unused_bandwidth { |
71 | $unused_bytes += $_[0]; |
79 | $unused_bytes += $_[0]; |
72 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
80 | if ($unused_last < $NOW - 30 && $unused_bytes / ($NOW - $unused_last) > 50000) { |
73 | $unused_last = $NOW; |
81 | $unused_last = $NOW; |
74 | $unused_bytes = 0; |
82 | $unused_bytes = 0; |
75 | $queue_file->force_wake_next; |
83 | $queue_file->force_wake_next |
76 | slog 1, "forced filetransfer due to unused bandwidth"; |
84 | and slog 1, "forced filetransfer due to unused bandwidth"; |
77 | } |
|
|
78 | } |
|
|
79 | |
|
|
80 | my @newcons; |
|
|
81 | my @pool; |
|
|
82 | |
|
|
83 | # one "execution thread" |
|
|
84 | sub handler { |
|
|
85 | while () { |
|
|
86 | if (@newcons) { |
|
|
87 | eval { |
|
|
88 | conn->new (@{pop @newcons})->handle; |
|
|
89 | }; |
|
|
90 | slog 1, "$@" if $@ && !ref $@; |
|
|
91 | |
|
|
92 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
93 | |
|
|
94 | $connections->up; |
|
|
95 | } else { |
|
|
96 | last if @pool >= $MAX_POOL; |
|
|
97 | push @pool, $Coro::current; |
|
|
98 | schedule; |
|
|
99 | } |
|
|
100 | } |
85 | } |
101 | } |
86 | } |
102 | |
87 | |
103 | sub listen_on { |
88 | sub listen_on { |
104 | my $listen = $_[0]; |
89 | my $listen = $_[0]; |
… | |
… | |
108 | # the "main thread" |
93 | # the "main thread" |
109 | async { |
94 | async { |
110 | slog 1, "accepting connections"; |
95 | slog 1, "accepting connections"; |
111 | while () { |
96 | while () { |
112 | $connections->down; |
97 | $connections->down; |
113 | push @newcons, [$listen->accept]; |
98 | my @conn = $listen->accept; |
114 | #slog 3, "accepted @$connections ".scalar(@pool); |
99 | #slog 3, "accepted @$connections ".scalar(@pool); |
115 | if (@pool) { |
100 | |
116 | (pop @pool)->ready; |
101 | async_pool { |
117 | } else { |
102 | eval { |
118 | 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; |
119 | } |
110 | } |
120 | } |
111 | } |
121 | }; |
112 | }; |
122 | } |
113 | } |
123 | |
114 | |
124 | my $http_port = new Coro::Socket |
115 | my $http_port = new Coro::Socket |
125 | LocalAddr => $SERVER_HOST, |
116 | LocalAddr => $::SERVER_HOST, |
126 | LocalPort => $SERVER_PORT, |
117 | LocalPort => $::SERVER_PORT, |
127 | ReuseAddr => 1, |
118 | ReuseAddr => 1, |
128 | Listen => 50, |
119 | Listen => 50, |
129 | or die "unable to start server"; |
120 | or die "unable to start server"; |
130 | |
121 | |
131 | listen_on $http_port; |
122 | listen_on $http_port; |
132 | |
123 | |
133 | if ($SERVER_PORT2) { |
124 | if ($::SERVER_PORT2) { |
134 | my $http_port = new Coro::Socket |
125 | my $http_port = new Coro::Socket |
135 | LocalAddr => $SERVER_HOST, |
126 | LocalAddr => $::SERVER_HOST, |
136 | LocalPort => $SERVER_PORT2, |
127 | LocalPort => $::SERVER_PORT2, |
137 | ReuseAddr => 1, |
128 | ReuseAddr => 1, |
138 | Listen => 50, |
129 | Listen => 50, |
139 | or die "unable to start server"; |
130 | or die "unable to start server"; |
140 | |
131 | |
141 | listen_on $http_port; |
132 | listen_on $http_port; |
142 | } |
133 | } |
143 | |
134 | |
144 | package conn; |
135 | package conn; |
145 | |
136 | |
146 | use strict; |
137 | use common::sense; |
147 | use bytes; |
|
|
148 | |
138 | |
149 | use Socket; |
139 | use Socket; |
150 | use HTTP::Date; |
140 | use HTTP::Date; |
151 | use Convert::Scalar 'weaken'; |
141 | use Convert::Scalar 'weaken'; |
152 | use IO::AIO; |
142 | use IO::AIO; |
|
|
143 | use AnyEvent::AIO; |
153 | |
144 | |
154 | IO::AIO::min_parallel $::AIO_PARALLEL; |
145 | IO::AIO::min_parallel $::AIO_PARALLEL; |
155 | |
|
|
156 | our $AIO_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; |
|
|
157 | |
146 | |
158 | our %conn; # $conn{ip}{self} => connobj |
147 | our %conn; # $conn{ip}{self} => connobj |
159 | our %uri; # $uri{ip}{uri}{self} |
148 | our %uri; # $uri{ip}{uri}{self} |
160 | our %blocked; |
149 | our %blocked; |
161 | our %mimetype; |
150 | our %mimetype; |
… | |
… | |
221 | for (keys %blocked) { |
210 | for (keys %blocked) { |
222 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
211 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
223 | } |
212 | } |
224 | } |
213 | } |
225 | |
214 | |
226 | our $PRUNE_WATCHER = EV::timer 60, 60, \&prune_caches; |
215 | our $PRUNE_WATCHER = AE::timer 60, 60, \&prune_caches; |
227 | |
216 | |
228 | sub slog { |
217 | sub slog { |
229 | my $self = shift; |
218 | my $self = shift; |
230 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
219 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
231 | } |
220 | } |
… | |
… | |
483 | } else { |
472 | } else { |
484 | $self->err (404, "not found"); |
473 | $self->err (404, "not found"); |
485 | } |
474 | } |
486 | } else { |
475 | } else { |
487 | |
476 | |
488 | stat $path |
477 | Coro::AIO::aio_stat $path |
489 | or $self->err (404, "not found"); |
478 | and $self->err (404, "not found"); |
490 | |
479 | |
491 | $self->{stat} = [stat _]; |
480 | $self->{stat} = [stat _]; |
492 | |
481 | |
493 | # idiotic netscape sends idiotic headers AGAIN |
482 | # idiotic netscape sends idiotic headers AGAIN |
494 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
483 | my $ims = $self->{h}{"if-modified-since"} =~ /^([^;]+)/ |
… | |
… | |
604 | |
593 | |
605 | if ($self->{method} eq "GET") { |
594 | if ($self->{method} eq "GET") { |
606 | $self->{time} = $::NOW; |
595 | $self->{time} = $::NOW; |
607 | $self->{written} = 0; |
596 | $self->{written} = 0; |
608 | |
597 | |
609 | open my $fh, "<", $self->{path} |
598 | my $fh = Coro::AIO::aio_open $self->{path}, Fcntl::O_RDONLY, 0 |
610 | or die "$self->{path}: late open failure ($!)"; |
599 | or die "$self->{path}: late open failure ($!)"; |
611 | |
600 | |
612 | $h -= $l - 1; |
601 | $h -= $l - 1; |
613 | |
602 | |
614 | my $transfer = $queue->start_transfer ($h); |
603 | my $transfer = $queue->start_transfer ($h); |
615 | my $locked; |
604 | my $locked; |
616 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
605 | my $bufsize = $::WAIT_BUFSIZE; # initial buffer size |
617 | |
606 | |
618 | while ($h > 0) { |
607 | while ($h > 0) { |
|
|
608 | Coro::cede; |
|
|
609 | my $transfer_lock = $TRANSFER_LOCK->guard ($self->{remote_id}); |
|
|
610 | |
619 | unless ($locked) { |
611 | unless ($locked) { |
620 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
612 | if ($locked ||= $transfer->try ($::WAIT_INTERVAL)) { |
621 | $bufsize = $::BUFSIZE; |
613 | $bufsize = $::BUFSIZE; |
622 | $self->{time} = $::NOW; |
614 | $self->{time} = $::NOW; |
623 | $self->{written} = 0; |
615 | $self->{written} = 0; |