… | |
… | |
139 | package conn; |
139 | package conn; |
140 | |
140 | |
141 | use Socket; |
141 | use Socket; |
142 | use HTTP::Date; |
142 | use HTTP::Date; |
143 | use Convert::Scalar 'weaken'; |
143 | use Convert::Scalar 'weaken'; |
144 | use Linux::AIO; |
144 | use IO::AIO; |
145 | |
145 | |
146 | Linux::AIO::min_parallel $::AIO_PARALLEL; |
146 | IO::AIO::min_parallel $::AIO_PARALLEL; |
147 | |
147 | |
148 | Event->io(fd => Linux::AIO::poll_fileno, |
148 | Event->io(fd => IO::AIO::poll_fileno, |
149 | poll => 'r', async => 1, |
149 | poll => 'r', async => 1, |
150 | cb => \&Linux::AIO::poll_cb); |
150 | cb => \&IO::AIO::poll_cb); |
151 | |
151 | |
152 | our %conn; # $conn{ip}{self} => connobj |
152 | our %conn; # $conn{ip}{self} => connobj |
153 | our %uri; # $uri{ip}{uri}{self} |
153 | our %uri; # $uri{ip}{uri}{self} |
154 | our %blocked; |
154 | our %blocked; |
155 | our %mimetype; |
155 | our %mimetype; |
… | |
… | |
192 | |
192 | |
193 | sub DESTROY { |
193 | sub DESTROY { |
194 | #my $self = shift; |
194 | #my $self = shift; |
195 | $::conns--; |
195 | $::conns--; |
196 | } |
196 | } |
|
|
197 | |
|
|
198 | sub prune_cache { |
|
|
199 | my $hash = $_[0]; |
|
|
200 | |
|
|
201 | for (keys %$hash) { |
|
|
202 | if (ref $hash->{$_} eq HASH::) { |
|
|
203 | prune_cache($hash->{$_}); |
|
|
204 | unless (scalar keys %{$hash->{$_}}) { |
|
|
205 | delete $hash->{$_}; |
|
|
206 | $d2++; |
|
|
207 | } |
|
|
208 | } |
|
|
209 | } |
|
|
210 | } |
|
|
211 | |
|
|
212 | sub prune_caches { |
|
|
213 | prune_cache \%conn; |
|
|
214 | prune_cache \%uri; |
|
|
215 | |
|
|
216 | for (keys %blocked) { |
|
|
217 | delete $blocked{$_} unless $blocked{$_}[0] > $::NOW; |
|
|
218 | } |
|
|
219 | } |
|
|
220 | |
|
|
221 | Event->timer(interval => 60, cb => \&prune_caches); |
197 | |
222 | |
198 | sub slog { |
223 | sub slog { |
199 | my $self = shift; |
224 | my $self = shift; |
200 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
225 | main::slog($_[0], "$self->{remote_id}> $_[1]"); |
201 | } |
226 | } |
… | |
… | |
504 | my $self = shift; |
529 | my $self = shift; |
505 | my $idx = $self->diridx; |
530 | my $idx = $self->diridx; |
506 | |
531 | |
507 | $self->response(200, "ok", |
532 | $self->response(200, "ok", |
508 | { |
533 | { |
509 | "Content-Type" => "text/html", |
534 | "Content-Type" => "text/html; charset=utf-8", |
510 | "Content-Length" => length $idx, |
535 | "Content-Length" => length $idx, |
511 | "Last-Modified" => time2str ($self->{stat}[9]), |
536 | "Last-Modified" => time2str ($self->{stat}[9]), |
512 | }, |
537 | }, |
513 | $idx); |
538 | $idx); |
514 | } |
539 | } |
515 | |
540 | |
516 | sub handle_file { |
541 | sub handle_file { |
517 | my ($self, $queue, $tbf) = @_; |
542 | my ($self, $queue, $tbf) = @_; |
518 | my $length = $self->{stat}[7]; |
543 | my $length = $self->{stat}[7]; |
519 | my $hdr = { |
544 | my $hdr = { |
520 | "Last-Modified" => time2str ((stat _)[9]), |
545 | "Last-Modified" => time2str ((stat _)[9]), |
|
|
546 | "Accept-Ranges" => "bytes", |
521 | }; |
547 | }; |
522 | |
548 | |
523 | my @code = (200, "ok"); |
549 | my @code = (200, "ok"); |
524 | my ($l, $h); |
550 | my ($l, $h); |
525 | |
551 | |
… | |
… | |
628 | |
654 | |
629 | close $fh; |
655 | close $fh; |
630 | } |
656 | } |
631 | } |
657 | } |
632 | |
658 | |
633 | 1; |
659 | 1 |