blib/lib/CGI/Easy/SendFile.pm

filter: Statementstmt 100% 82 / 82 Branchbran 86.6% 26 / 30 Conditioncond 94.2% 33 / 35 MC/DCmcdc 88.8% 16 / 18 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 95.5% 172 / 180 CC 28 SCAR 21.8
CC
28
Cov
96%
_get_range
15.1
send_file
10.0
_read_block
5.1
CRAP 8.8
↑ Summary
1 package CGI::Easy::SendFile;
2 2 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 our $VERSION = 'v2.0.1';
9  
10 2 ▶use Export::Attrs;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::Util qw( min );
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use CGI::Easy::Util qw( date_http );
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶use constant STAT_MTIME => 9;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 ▶use constant BUF_SIZE => 64*1024;
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 25 ▶sub send_file :Export {
Subroutine send_file: called CC=10 SCAR=23.0
Pod documented
19 25 my ($r, $h, $file, $opt) = @_;
20 my %p = (
21 type => 'application/x-download',
22 range => !ref $file,
23 cache => 0,
24 inline => 0,
25 25 ▶ %{$opt || {}},
Condition100%2/2
$opt || {}
l!l
223
MC/DC100%1/1
$opt || {}
$opt
Truth table100%2/2
A
A= $opt
Aresult
11
00
26 );
27  
28 25 ▶ if (!$p{cache}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $p{'cache'}) { } 20 5 25
29 20 $h->{'Expires'} = 'Sat, 01 Jan 2000 00:00:00 GMT';
30 }
31 else {
32 5 delete $h->{'Expires'};
33 5 ▶ if (!ref $file) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $file) 4 1 5
34 4 my $lastmod = date_http((stat $file)[STAT_MTIME]);
35 4 my $ifmod = $r->{ENV}{HTTP_IF_MODIFIED_SINCE};
36 4 ▶ if ($ifmod && $ifmod eq $lastmod) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ifmod and $ifmod eq $lastmod) { } 1 3 4
Condition100%3/3
$ifmod and $ifmod eq $lastmod
!ll&&!rl&&r
121
MC/DC100%2/2
$ifmod and $ifmod eq $lastmod
$ifmod $ifmod eq $lastmod
Truth table100%3/3
A and B
A= $ifmodB= $ifmod eq $lastmod
ABresult
0X0
100
111
37 1 $h->{'Status'} = '304 Not Modified';
38 1 return \q{};
39 }
40 else {
41 3 $h->{'Last-Modified'} = $lastmod;
42 }
43 }
44 }
45  
46 24 ▶ my $len = ref $file ? length ${$file} : -s $file;
Branch: 1 branch
BranchTrueFalseTotal
ref $file ? : 19 5 24
47 24 my ($start, $end) = _get_range($p{range}, $r, $len);
48 24 my $size = $end-$start+1;
49  
50 24 $h->{'Accept-Ranges'} = 'bytes';
51 24 $h->{'Content-Length'} = $size;
52 24 $h->{'Content-Type'} = $p{type};
53 24 ▶ if (!$p{inline}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($p{"inline"}) 19 5 24
54 19 $h->{'Content-Disposition'} = 'attachment';
55 }
56 24 ▶ if (!($start == 0 && $end == $len-1)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($start == 0 and $end == $len - 1) 8 16 24
Condition100%3/3
$start == 0 and $end == $len - 1
!ll&&!rl&&r
6216
MC/DC100%2/2
$start == 0 and $end == $len - 1
$start == 0 $end == $len - 1
Truth table100%3/3
A and B
A= $start == 0B= $end == $len - 1
ABresult
0X0
100
111
57 8 $h->{Status} = '206 Partial Content';
58 8 $h->{'Content-Range'} = "bytes $start-$end/$len";
59 }
60  
61 24 return _read_block($file, $start, $size);
62 2 ▶}
Subroutine BEGIN: called CC=1 SCAR=0.0
63  
64 24 ▶sub _get_range {
Subroutine _get_range: called CC=15 SCAR=27.1
65 24 my ($allow_range, $r, $len) = @_;
66 24 my ($start, $end) = (0, $len-1);
67 24 ▶ if ($allow_range && defined $r->{ENV}{HTTP_RANGE}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($allow_range and defined $r->{"ENV"}{"HTTP_RANGE"}) 15 9 24
Condition100%3/3
$allow_range and defined $r->{"ENV"}{"HTTP_RANGE"}
!ll&&!rl&&r
3615
MC/DC100%2/2
$allow_range and defined $r->{"ENV"}{"HTTP_RANGE"}
$allow_range defined $r->{"ENV"}{"HTTP_RANGE"}
Truth table100%3/3
A and B
A= $allow_rangeB= defined $r->{"ENV"}{"HTTP_RANGE"}
ABresult
0X0
100
111
68 15 ▶ if ($r->{ENV}{HTTP_RANGE} =~ /\Abytes=(\d*)-(\d*)\z/ixms) {
Branch: 1 branch
BranchTrueFalseTotal
if ($r->{"ENV"}{"HTTP_RANGE"} =~ /\Abytes=(\d*)-(\d*)\z/imsx) 15 0 15
69 15 my ($from, $to) = ($1, $2);
70 15 ▶ if ($from ne q{} && $to ne q{} && $from <= $to && $to < $len) { # 0-0, 0-499, 500-999
Branch: 3 branches
BranchTrueFalseTotal
if ($from ne '' and $to ne '' and $from <= $to and $to < $len) { } 5 10 15
elsif ($from ne '' and $to eq '' and $from < $len) { } 3 7 10
elsif ($from eq '' and $to ne '' and 0 < $to and $to <= $len) { } 3 7 10
Condition88%8/9
$from ne "" and $to ne "" and $from <= $to and $to < $len
!ll&&!rl&&r
915
$from ne "" and $to ne "" and $from <= $to
!ll&&!rl&&r
906
$from ne "" and $to ne ""
!ll&&!rl&&r
546
MC/DC75%3/4
$from ne "" and $to ne "" and $from <= $to and $to < $len
$from ne "" $to ne "" $from <= $to $to < $len
Truth table80%4/5
A and B and C and D
A= $from ne ""B= $to ne ""C= $from <= $toD= $to < $len
ABCDresult
0XXX0
10XX0
110X0
11100
11111
71 5 $start = $from;
72 5 $end = $to;
73 }
74 ▶ elsif ($from ne q{} && $to eq q{} && $from < $len) { # 0-, 500-, 999-
Condition100%6/6
$from ne "" and $to eq "" and $from < $len
!ll&&!rl&&r
613
$from ne "" and $to eq ""
!ll&&!rl&&r
514
MC/DC100%3/3
$from ne "" and $to eq "" and $from < $len
$from ne "" $to eq "" $from < $len
Truth table100%4/4
A and B and C
A= $from ne ""B= $to eq ""C= $from < $len
ABCresult
0XX0
10X0
1100
1111
75 3 $start = $from;
76 }
77 ▶ elsif ($from eq q{} && $to ne q{} && 0 < $to && $to <= $len) { # -1, -500, -1000
Condition88%8/9
$from eq "" and $to ne "" and 0 < $to and $to <= $len
!ll&&!rl&&r
313
$from eq "" and $to ne "" and 0 < $to
!ll&&!rl&&r
214
$from eq "" and $to ne ""
!ll&&!rl&&r
205
MC/DC75%3/4
$from eq "" and $to ne "" and 0 < $to and $to <= $len
$from eq "" $to ne "" 0 < $to $to <= $len
Truth table80%4/5
A and B and C and D
A= $from eq ""B= $to ne ""C= 0 < $toD= $to <= $len
ABCDresult
0XXX0
10XX0
110X0
11100
11111
78 3 $start = $len - $to;
79 }
80 }
81 }
82 24 return ($start, $end);
83 }
84  
85 24 ▶sub _read_block {
Subroutine _read_block: called CC=5 SCAR=16.3
86 24 my ($file, $start, $size) = @_;
87 24 my $data = q{};
88 24 ▶ open my $fh, '<', $file or croak "open: $!";
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "<", $file or croak("open: $!") 24 0 24
89 24 seek $fh, $start, 0;
90 24 my ($n, $buf);
91 24 ▶ while ($n = read $fh, $buf, min($size, BUF_SIZE)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($n = read($fh, $buf, &min($size, 65536))) 24 24 48
92 24 $size -= length $buf;
93 24 $data .= $buf;
94 }
95 24 ▶ croak "read: $!" if !defined $n;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $n 0 24 24
96 24 ▶ close $fh or croak "close: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or croak("close: $!") 24 0 24
97 24 return \$data;
98 }
99  
100  
101 2 1; # Magic true value required at end of module
102 __END__
↑ Summary