1 | use Coro; |
1 | use Coro; |
2 | use Coro::Semaphore; |
2 | use Coro::Semaphore; |
3 | use Coro::Event; |
3 | use Coro::EV; |
4 | use Coro::Socket; |
4 | use Coro::Socket; |
5 | use Coro::Signal; |
5 | use Coro::Signal; |
6 | use Coro::AIO (); |
6 | use Coro::AIO (); |
7 | |
7 | |
8 | use HTTP::Date; |
8 | use HTTP::Date; |
… | |
… | |
26 | our $HTTP_NOW; |
26 | our $HTTP_NOW; |
27 | |
27 | |
28 | our $ERROR_LOG; |
28 | our $ERROR_LOG; |
29 | our $ACCESS_LOG; |
29 | our $ACCESS_LOG; |
30 | |
30 | |
31 | Event->timer(interval => 1, hard => 1, cb => sub { |
31 | our $update_time = EV::periodic 0, 1, undef, sub { |
32 | $NOW = time; |
32 | $NOW = time; |
33 | $HTTP_NOW = time2str $NOW; |
33 | $HTTP_NOW = time2str $NOW; |
34 | })->now; |
34 | }; |
|
|
35 | $update_time->invoke; |
35 | |
36 | |
36 | if ($ERROR_LOG) { |
37 | if ($ERROR_LOG) { |
37 | use IO::Handle; |
38 | use IO::Handle; |
38 | open $errorlog, ">>$ERROR_LOG" |
39 | open $errorlog, ">>$ERROR_LOG" |
39 | or die "$ERROR_LOG: $!"; |
40 | or die "$ERROR_LOG: $!"; |
… | |
… | |
74 | $queue_file->force_wake_next; |
75 | $queue_file->force_wake_next; |
75 | slog 1, "forced filetransfer due to unused bandwidth"; |
76 | slog 1, "forced filetransfer due to unused bandwidth"; |
76 | } |
77 | } |
77 | } |
78 | } |
78 | |
79 | |
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 | } |
|
|
100 | } |
|
|
101 | |
|
|
102 | sub listen_on { |
80 | sub listen_on { |
103 | my $listen = $_[0]; |
81 | my $listen = $_[0]; |
104 | |
82 | |
105 | push @listen_sockets, $listen; |
83 | push @listen_sockets, $listen; |
106 | |
84 | |
107 | # the "main thread" |
85 | # the "main thread" |
108 | async { |
86 | async { |
109 | slog 1, "accepting connections"; |
87 | slog 1, "accepting connections"; |
110 | while () { |
88 | while () { |
111 | $connections->down; |
89 | $connections->down; |
112 | push @newcons, [$listen->accept]; |
90 | my @conn = $listen->accept; |
113 | #slog 3, "accepted @$connections ".scalar(@pool); |
91 | #slog 3, "accepted @$connections ".scalar(@pool); |
114 | if (@pool) { |
92 | |
115 | (pop @pool)->ready; |
93 | async_pool { |
116 | } else { |
94 | eval { |
117 | async \&handler; |
95 | conn->new (@conn)->handle; |
|
|
96 | }; |
|
|
97 | slog 1, "$@" if $@ && !ref $@; |
|
|
98 | |
|
|
99 | $httpevent->broadcast; # only for testing, but doesn't matter much |
|
|
100 | |
|
|
101 | $connections->up; |
118 | } |
102 | } |
119 | } |
103 | } |
120 | }; |
104 | }; |
121 | } |
105 | } |
122 | |
106 | |
… | |
… | |
150 | use Convert::Scalar 'weaken'; |
134 | use Convert::Scalar 'weaken'; |
151 | use IO::AIO; |
135 | use IO::AIO; |
152 | |
136 | |
153 | IO::AIO::min_parallel $::AIO_PARALLEL; |
137 | IO::AIO::min_parallel $::AIO_PARALLEL; |
154 | |
138 | |
155 | Event->io (fd => IO::AIO::poll_fileno, |
139 | our $AIO_WATCHER = EV::io IO::AIO::poll_fileno, EV::READ, \&IO::AIO::poll_cb; |
156 | poll => 'r', async => 1, |
|
|
157 | cb => \&IO::AIO::poll_cb); |
|
|
158 | |
140 | |
159 | our %conn; # $conn{ip}{self} => connobj |
141 | our %conn; # $conn{ip}{self} => connobj |
160 | our %uri; # $uri{ip}{uri}{self} |
142 | our %uri; # $uri{ip}{uri}{self} |
161 | our %blocked; |
143 | our %blocked; |
162 | our %mimetype; |
144 | our %mimetype; |
… | |
… | |
222 | for (keys %blocked) { |
204 | for (keys %blocked) { |
223 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
205 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
224 | } |
206 | } |
225 | } |
207 | } |
226 | |
208 | |
227 | Event->timer (interval => 60, cb => \&prune_caches); |
209 | our $PRUNE_WATCHER = EV::timer 60, 60, \&prune_caches; |
228 | |
210 | |
229 | sub slog { |
211 | sub slog { |
230 | my $self = shift; |
212 | my $self = shift; |
231 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
213 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
232 | } |
214 | } |
… | |
… | |
425 | sub map_uri { |
407 | sub map_uri { |
426 | my $self = shift; |
408 | my $self = shift; |
427 | my $host = $self->{server_name}; |
409 | my $host = $self->{server_name}; |
428 | my $uri = $self->{uri}; |
410 | my $uri = $self->{uri}; |
429 | |
411 | |
|
|
412 | $host =~ /[\/\\]/ |
|
|
413 | and $self->err(400, "bad request"); |
|
|
414 | |
430 | # some massaging, also makes it more secure |
415 | # some massaging, also makes it more secure |
431 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
416 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
432 | $uri =~ s%//+%/%g; |
417 | $uri =~ s%//+%/%g; |
433 | $uri =~ s%/\.(?=/|$)%%g; |
418 | $uri =~ s%/\.(?=/|$)%%g; |
434 | 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; |
419 | 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; |
… | |
… | |
553 | }; |
538 | }; |
554 | |
539 | |
555 | my @code = (200, "ok"); |
540 | my @code = (200, "ok"); |
556 | my ($l, $h); |
541 | my ($l, $h); |
557 | |
542 | |
558 | if ($self->{h}{range} =~ /^bytes=(.*)$/) { |
543 | if ($self->{h}{range} =~ /^bytes=(.*)$/i) { |
559 | for (split /,/, $1) { |
544 | for (split /,/, $1) { |
560 | if (/^-(\d+)$/) { |
545 | if (/^-(\d+)$/) { |
561 | ($l, $h) = ($length - $1, $length - 1); |
546 | ($l, $h) = ($length - $1, $length - 1); |
562 | } elsif (/^(\d+)-(\d*)$/) { |
547 | } elsif (/^(\d+)-(\d*)$/) { |
563 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
548 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
… | |
… | |
629 | |
614 | |
630 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
615 | Coro::AIO::aio_read $fh, $l, ($h > $bufsize ? $bufsize : $h), my $buf, 0 |
631 | or last; |
616 | or last; |
632 | |
617 | |
633 | $tbf->request (length $buf); |
618 | $tbf->request (length $buf); |
634 | my $w = syswrite $self->{fh}, $buf |
619 | my $w = $self->{fh}->syswrite ($buf) |
635 | or last; |
620 | or last; |
636 | $::written += $w; |
621 | $::written += $w; |
637 | $self->{written} += $w; |
622 | $self->{written} += $w; |
638 | $l += $w; |
623 | $l += $w; |
639 | } |
624 | } |