blib/lib/CGI/Application/Plugin/Stream.pm

filter: Statementstmt 96.9% 64 / 66 Branchbran 81.2% 13 / 16 Conditioncond 87.5% 14 / 16 MC/DCmcdc 77.7% 7 / 9 Subroutinesub 100% 8 / 8 Podpod 100% 2 / 2 totaltotal 92.3% 108 / 117 SCAR 27.1
CC
15
Cov
93%
stream
14.0
stream_file
2.1
BEGIN
1.0
CRAP 15.1
↑ Summary Build »
1 package CGI::Application::Plugin::Stream;
2  
3 3 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 3 ▶use CGI::Application 3.21;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 require Exporter;
10 3 ▶use vars (qw/@ISA @EXPORT_OK/);
Subroutine BEGIN: called CC=1 SCAR=0.0
11 3 @ISA = qw(Exporter);
12  
13 3 @EXPORT_OK = qw(stream stream_file);
14  
15 3 our $VERSION = '3.00_1';
16  
17 sub stream {
18 20 ▶ my ( $self, $file_or_fh, $bytes ) = @_;
Subroutine stream: called CC=14 SCAR=26.4
Pod documented
19  
20 20 ▶ $bytes ||= 1024;
Condition100%2/2
$bytes ||= 1024
l!l
218
MC/DC100%1/1
$bytes ||= 1024
$bytes
Truth table100%2/2
A
A= $bytes
Aresult
11
00
21  
22 # Use unbuffered output, but return the state of $| to its previous state when we are done.
23 20 local $| = 1;
24  
25 20 my ($fh, $basename);
26 20 my $size = (stat( $file_or_fh ))[7];
27  
28 # If we have a file path
29 20 ▶ if ( ref( \$file_or_fh ) eq 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref \$file_or_fh eq 'SCALAR') { } 4 16 20
30 # They passed along a scalar, pointing to the path of the file
31 # So we need to open the file
32 4 ▶ open($fh,"<$file_or_fh" ) || die "failed to open file: $file_or_fh: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<$file_or_fh" or die "failed to open file: ${file_or_fh}: $!" 0 4 4
33 # Now let's go binmode (Thanks, William!)
34 4 binmode $fh;
35 4 $basename = basename( $file_or_fh );
36 }
37 # We have a file handle.
38 else {
39 16 $fh = $file_or_fh;
40 16 $basename = 'FILE';
41 }
42  
43 # Use FileHandle to make File::MMagic happy;
44 # bless the filehandle into the FileHandle package to make File::MMagic happy
45 20 require FileHandle;
46 20 bless $fh, "FileHandle";
47  
48 # Check what headers the user has already set and
49 # don't override them.
50 20 my %existing_headers = $self->header_props();
51  
52 # Check for a existing type header set with or without a hypheout a hyphen
53 20 ▶ unless ( $existing_headers{'-type'} || $existing_headers{'type'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($existing_headers{"-type"} or $existing_headers{"type"}) 16 4 20
Condition100%3/3
$existing_headers{"-type"} or $existing_headers{"type"}
l!l&&r!l&&!r
2216
MC/DC100%2/2
$existing_headers{"-type"} or $existing_headers{"type"}
$existing_headers{"-type"} $existing_headers{"type"}
Truth table100%3/3
A or B
A= $existing_headers{"-type"}B= $existing_headers{"type"}
ABresult
1X1
011
000
54 16 my $mime_type;
55  
56 16 eval {
57 16 require File::MMagic;
58 16 my $magic = File::MMagic->new();
59 16 $mime_type = $magic->checktype_filehandle($fh);
60 };
61 16 ▶ warn "Failed to load File::MMagic module to determine mime type: $@" if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 16 16
62
63 # Set Default
64 16 ▶ $mime_type ||= 'application/octet-stream';
Condition50%1/2
$mime_type ||= "application/octet-stream"
l!l
160
MC/DC0%0/1
$mime_type ||= "application/octet-stream"
$mime_type
Truth table50%1/2
A
A= $mime_type
Aresult
11
00
65  
66 16 $self->header_add('-type' => $mime_type);
67 }
68  
69  
70 20 ▶ unless ( $existing_headers{'Content_Length'}
Branch: 1 branch
BranchTrueFalseTotal
unless ($existing_headers{"Content_Length"} or $existing_headers{"-Content_Length"}) 16 4 20
Condition100%3/3
$existing_headers{"Content_Length"} or $existing_headers{"-Content_Length"}
l!l&&r!l&&!r
2216
MC/DC100%2/2
$existing_headers{"Content_Length"} or $existing_headers{"-Content_Length"}
$existing_headers{"Content_Length"} $existing_headers{"-Content_Length"}
Truth table100%3/3
A or B
A= $existing_headers{"Content_Length"}B= $existing_headers{"-Content_Length"}
ABresult
1X1
011
000
71 || $existing_headers{'-Content_Length'}
72 ) {
73 16 $self->header_add('-Content_Length' => $size);
74 }
75  
76 20 ▶ unless ( $existing_headers{'-attachment'}
Branch: 1 branch
BranchTrueFalseTotal
unless ($existing_headers{"-attachment"} or $existing_headers{"attachment"} or grep /-?content-disposition/i, keys %existing_headers) 16 4 20
Condition83%5/6
$existing_headers{"-attachment"} or $existing_headers{"attachment"} or grep /-?content-disposition/i, keys %existing_headers
l!l&&r!l&&!r
4016
$existing_headers{"-attachment"} or $existing_headers{"attachment"}
l!l&&r!l&&!r
2216
MC/DC66%2/3
$existing_headers{"-attachment"} or $existing_headers{"attachment"} or grep /-?content-disposition/i, keys %existing_headers
$existing_headers{"-attachment"} $existing_headers{"attachment"} grep /-?content-disposition/i, keys %existing_headers
Truth table75%3/4
A or B or C
A= $existing_headers{"-attachment"}B= $existing_headers{"attachment"}C= grep /-?content-disposition/i, keys %existing_headers
ABCresult
1XX1
01X1
0011
0000
77 || $existing_headers{'attachment'}
78 || grep( /-?content-disposition/i, keys %existing_headers )
79 ) {
80 16 $self->header_add('-attachment' => $basename);
81 }
82  
83 20 $self->header_type( 'none' );
84 20 print $self->query->header( $self->header_props() );
85  
86 # This reads in the file in $byte size chunks
87 20 my $first;
88 # File::MMagic may have read some of the file, so seek back to the beginning
89 20 seek($fh,0,0);
90 20 ▶ while ( read( $fh, my $buffer, $bytes ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (read $fh, my $buffer, $bytes) 20 20 40
91 20 print $buffer;
92 }
93  
94 20 print ''; # print a null string at the end
95 20 close ( $fh );
96 20 return 1;
97 }
98  
99 # The old way. Requires manually calling error_mode() if there's a problem,
100 # but error_mode() won't have access to "$@"
101 sub stream_file {
102 10 ▶ my $self = shift;
Subroutine stream_file: called CC=2 SCAR=7.5
Pod documented
103 10 my $out;
104  
105 # Perhaps bad style to not use a method call here,
106 # But this keeps the legacy case working, where only stream_file() was exported.
107 10 eval { stream($self,@_) };
108  
109 # Starting with 3.0, we warn if there's a problem opening the file
110 # instead of ignoring the error.
111 10 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) { } 0 10 10
112 0 warn $@;
113 0 return 0;
114 }
115 else {
116 10 return 1;
117 }
118 }
119  
120 3 1;
121 __END__
↑ Summary Build »