| 1 | # ABSTRACT: Responder for static files | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | package Pinto::Server::Responder::File; | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | 13 | ▶ | use Moose; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 13 | ▶ | use Plack::Response; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | 13 | ▶ | use Plack::MIME; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 13 | ▶ | use HTTP::Date (); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | 13 | our $VERSION = '0.14'; # VERSION | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||
| 18 | 13 | extends qw(Pinto::Server::Responder); | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 81 | ▶ | sub respond { | ||||||||||||||||||||||||||
|
Subroutine
respond: called CC=7 SCAR=19.8
Pod
documented
| |||||||||||||||||||||||||||||
| 23 | 81 | my ($self) = @_; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # e.g. /stack_name/modules/02packages.details.txt.gz | ||||||||||||||||||||||||||||
| 26 | 81 | my ( undef, @path_parts ) = split '/', $self->request->path_info; | |||||||||||||||||||||||||||
| 27 | 81 | my $file = $self->root->file(@path_parts); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | 81 | ▶ | return not_found($file) if not -f $file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 30 | 58 | ▶ | return not_found($file) if index($file, '/../') > 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | 57 | my @stat = stat($file); | |||||||||||||||||||||||||||
| 33 | 57 | my $modified_since = HTTP::Date::str2time( $self->request->env->{HTTP_IF_MODIFIED_SINCE} ); | |||||||||||||||||||||||||||
| 34 | 57 | ▶ | return [ 304, [], [] ] if $modified_since && $stat[9] <= $modified_since; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$modified_since and $stat[9] <= $modified_since
MC/DC0%0/2
$modified_since and $stat[9] <= $modified_since
$modified_since
$stat[9] <= $modified_since
Truth table33%1/3
A and B
A= $modified_sinceB= $stat[9] <= $modified_since
| |||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | 57 | my $response = Plack::Response->new; | |||||||||||||||||||||||||||
| 37 | 57 | $response->content_type( Plack::MIME->mime_type($file) ); | |||||||||||||||||||||||||||
| 38 | 57 | $response->content_length( $stat[7] ); | |||||||||||||||||||||||||||
| 39 | 57 | $response->header( 'Last-Modified' => HTTP::Date::time2str( $stat[9] ) ); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 57 | ▶ | $response->header( 'Cache-Control' => 'no-cache' ) if $self->should_not_cache($file); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | 57 | ▶ | $response->body( $file->openr ) unless $self->request->method eq "HEAD"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 44 | 57 | $response->status(200); | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 57 | return $response; | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | 57 | ▶ | sub should_not_cache { | ||||||||||||||||||||||||||
|
Subroutine
should_not_cache: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||||||||||||||||
| 55 | 57 | my ( $self, $file ) = @_; | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | # force caches to always revalidate the indices, i.e. | ||||||||||||||||||||||||||||
| 58 | # 01mailrc.txt.gz, 02packages.details.txt.gz, 03modlist.data.gz | ||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | 57 | my $basename = $file->basename; | |||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 57 | ▶ | return 1 if $basename eq '01mailrc.txt.gz'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | 37 | ▶ | return 1 if $basename eq '02packages.details.txt.gz'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 20 | ▶ | return 1 if $basename eq '03modlist.data.gz'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 65 | 0 | return 0; | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | 24 | ▶ | sub not_found { | ||||||||||||||||||||||||||
|
Subroutine
not_found: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||
| 71 | 24 | my $file = shift; | |||||||||||||||||||||||||||
| 72 | 24 | my $body = "File $file not found"; | |||||||||||||||||||||||||||
| 73 | 24 | my $headers = [ 'Content-Type' => 'text/plain', 'Content-Length' => length($body) ]; | |||||||||||||||||||||||||||
| 74 | 24 | return [ 404, $headers, [$body] ]; | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 13 | __PACKAGE__->meta->make_immutable; | |||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | #------------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | 13 | 1; | |||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | __END__ | ||||||||||||||||||||||||||||