File Coverage

blib/lib/Plack/Middleware/XSendfile.pm
Criterion Covered Total %
statement 43 54 79.6
branch 7 18 38.8
condition 5 14 35.7
subroutine 11 12 91.6
pod 2 3 66.6
total 68 101 67.3


line stmt bran cond sub pod time code
1             package Plack::Middleware::XSendfile;
2 1     1   5 use strict;
  1         2  
  1         36  
3 1     1   4 use warnings;
  1         1  
  1         82  
4 1     1   5 use parent qw(Plack::Middleware);
  1         1  
  1         7  
5              
6 1     1   52 use Carp ();
  1         1  
  1         11  
7 1     1   3 use Plack::Util;
  1         1  
  1         14  
8 1     1   2 use Scalar::Util;
  1         1  
  1         42  
9 1     1   3 use Plack::Util::Accessor qw( variation );
  1         1  
  1         6  
10              
11             sub new {
12 1     1 1 2 my $class = shift;
13 1         231 Carp::carp("Plack::Middleware::XSendfile is deprecated and will be removed in a future release");
14 1         10 $class->SUPER::new(@_);
15             }
16              
17             sub call {
18 1     1 1 2 my $self = shift;
19 1         2 my $env = shift;
20              
21 1         8 my $res = $self->app->($env);
22             $self->response_cb($res, sub {
23 1     1   2 my $res = shift;
24 1         2 my($status, $headers, $body) = @$res;
25 1 50       2 return unless defined $body;
26              
27 1 50 33     8 if (Scalar::Util::blessed($body) && $body->can('path')) {
28 1   50     3 my $type = $self->_variation($env) || '';
29 1         4 my $h = Plack::Util::headers($headers);
30 1 50 33     13 if ($type && !$h->exists($type)) {
31 1 50 33     9 if ($type eq 'X-Accel-Redirect') {
    50          
32 0         0 my $path = $body->path;
33 0         0 my $url = $self->map_accel_path($env, $path);
34 0 0       0 $h->set($type => $url) if $url;
35 0         0 $h->set('Content-Length', 0);
36 0         0 $body = [];
37             } elsif ($type eq 'X-Sendfile' or $type eq 'X-Lighttpd-Send-File') {
38 1         3 my $path = $body->path;
39 1 50       6 $h->set($type => $path) if defined $path;
40 1         3 $h->set('Content-Length', 0);
41 1         12 $body = [];
42             } else {
43 0         0 $env->{'psgi.errors'}->print("Unknown x-sendfile variation: $type");
44             }
45             }
46             }
47              
48 1         15 @$res = ( $status, $headers, $body );
49 1         11 });
50             }
51              
52             sub map_accel_path {
53 0     0 0 0 my($self, $env, $path) = @_;
54              
55 0 0       0 if (my $mapping = $env->{HTTP_X_ACCEL_MAPPING}) {
56 0         0 my($internal, $external) = split /=/, $mapping, 2;
57 0         0 $path =~ s!^\Q$internal\E!$external!i;
58             }
59              
60 0         0 return $path;
61             }
62              
63             sub _variation {
64 1     1   2 my($self, $env) = @_;
65 1 50 33     3 $self->variation || $env->{'plack.xsendfile.type'} || $env->{HTTP_X_SENDFILE_TYPE};
66             }
67              
68             1;
69              
70             __END__