| 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 || {}
MC/DC100%1/1
$opt || {}
$opt
Truth table100%2/2
A
A= $opt
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 26 | ); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 28 | 25 | ▶ | if (!$p{cache}) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$ifmod and $ifmod eq $lastmod
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 54 | 19 | $h->{'Content-Disposition'} = 'attachment'; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 55 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 24 | ▶ | if (!($start == 0 && $end == $len-1)) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
$start == 0 and $end == $len - 1
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$allow_range and defined $r->{"ENV"}{"HTTP_RANGE"}
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"}
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 15 | ▶ | if ($r->{ENV}{HTTP_RANGE} =~ /\Abytes=(\d*)-(\d*)\z/ixms) { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
Condition88%8/9
$from ne "" and $to ne "" and $from <= $to and $to < $len
$from ne "" and $to ne "" and $from <= $to
$from ne "" and $to ne ""
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
$from ne "" and $to eq ""
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
$from eq "" and $to ne "" and 0 < $to
$from eq "" and $to ne ""
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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 92 | 24 | $size -= length $buf; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 24 | $data .= $buf; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 95 | 24 | ▶ | croak "read: $!" if !defined $n; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 24 | ▶ | close $fh or croak "close: $!"; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 97 | 24 | return \$data; | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | 1; # Magic true value required at end of module | |||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
| 102 | __END__ | ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||