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: $!"; |
… | |
… | |
150 | use Convert::Scalar 'weaken'; |
151 | use Convert::Scalar 'weaken'; |
151 | use IO::AIO; |
152 | use IO::AIO; |
152 | |
153 | |
153 | IO::AIO::min_parallel $::AIO_PARALLEL; |
154 | IO::AIO::min_parallel $::AIO_PARALLEL; |
154 | |
155 | |
155 | Event->io (fd => IO::AIO::poll_fileno, |
156 | 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 | |
157 | |
159 | our %conn; # $conn{ip}{self} => connobj |
158 | our %conn; # $conn{ip}{self} => connobj |
160 | our %uri; # $uri{ip}{uri}{self} |
159 | our %uri; # $uri{ip}{uri}{self} |
161 | our %blocked; |
160 | our %blocked; |
162 | our %mimetype; |
161 | our %mimetype; |
… | |
… | |
222 | for (keys %blocked) { |
221 | for (keys %blocked) { |
223 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
222 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
224 | } |
223 | } |
225 | } |
224 | } |
226 | |
225 | |
227 | Event->timer (interval => 60, cb => \&prune_caches); |
226 | our $PRUNE_WATCHER = EV::timer 60, 60, \&prune_caches; |
228 | |
227 | |
229 | sub slog { |
228 | sub slog { |
230 | my $self = shift; |
229 | my $self = shift; |
231 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
230 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
232 | } |
231 | } |
… | |
… | |
425 | sub map_uri { |
424 | sub map_uri { |
426 | my $self = shift; |
425 | my $self = shift; |
427 | my $host = $self->{server_name}; |
426 | my $host = $self->{server_name}; |
428 | my $uri = $self->{uri}; |
427 | my $uri = $self->{uri}; |
429 | |
428 | |
|
|
429 | $host =~ /[\/\\]/ |
|
|
430 | and $self->err(400, "bad request"); |
|
|
431 | |
430 | # some massaging, also makes it more secure |
432 | # some massaging, also makes it more secure |
431 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
433 | $uri =~ s/%([0-9a-fA-F][0-9a-fA-F])/chr hex $1/ge; |
432 | $uri =~ s%//+%/%g; |
434 | $uri =~ s%//+%/%g; |
433 | $uri =~ s%/\.(?=/|$)%%g; |
435 | $uri =~ s%/\.(?=/|$)%%g; |
434 | 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; |
436 | 1 while $uri =~ s%/[^/]+/\.\.(?=/|$)%%; |
… | |
… | |
553 | }; |
555 | }; |
554 | |
556 | |
555 | my @code = (200, "ok"); |
557 | my @code = (200, "ok"); |
556 | my ($l, $h); |
558 | my ($l, $h); |
557 | |
559 | |
558 | if ($self->{h}{range} =~ /^bytes=(.*)$/) { |
560 | if ($self->{h}{range} =~ /^bytes=(.*)$/i) { |
559 | for (split /,/, $1) { |
561 | for (split /,/, $1) { |
560 | if (/^-(\d+)$/) { |
562 | if (/^-(\d+)$/) { |
561 | ($l, $h) = ($length - $1, $length - 1); |
563 | ($l, $h) = ($length - $1, $length - 1); |
562 | } elsif (/^(\d+)-(\d*)$/) { |
564 | } elsif (/^(\d+)-(\d*)$/) { |
563 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |
565 | ($l, $h) = ($1, ($2 ne "" || $2 >= $length) ? $2 : $length - 1); |