lib/Drogo/MultiPart.pm

filter: Statementstmt 12.2% 7 / 57 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 2 MC/DCmcdc 0.0% 0 / 1 Subroutinesub 50.0% 2 / 4 Podpod 50.0% 1 / 2 totaltotal 11.1% 10 / 90 CC 15 SCAR 53.0
CC
15
Cov
8%
process
240.0
tmpfilename
2.0
BEGIN
1.0
CRAP 200.2
↑ Summary blib/lib/Drogo/LoadModules.pm »
1 package Drogo::MultiPart;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use IO::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 MODULE
6  
7 Drogo::MultiPart
8  
9 =head1 METHODS
10  
11 =over 4
12  
13 =cut
14  
15 =item process
16  
17 Processes request's multipart data.
18  
19 =cut
20  
21 0 ▶sub tmpfilename { join('-', 'drogomp', $$, time) }
Subroutine tmpfilename: not called CC=1 SCAR=6.9
Pod undocumented
22  
23 sub process
24 0 ▶{
Subroutine process: not called CC=15 SCAR=54.8
Pod documented
25 0 my $server = shift;
26 0 my $fh = $server->input;
27  
28 # rewind
29 0 $fh->seek(0, 0);
30  
31 0 ▶ my $tmpdir = $server->variable('tmpdir') || '/tmp';
Condition0%0/2
$server->variable('tmpdir') || '/tmp'
l!l
00
MC/DC0%0/1
$server->variable('tmpdir') || '/tmp'
$server->variable('tmpdir')
Truth table0%0/2
A
A= $server->variable('tmpdir')
Aresult
11
00
32  
33 # all parts
34 0 my @request_parts;
35  
36 # instance
37 my %request_part;
38  
39 # grab first line
40 0 my $key_line = $fh->getline;
41 0 $key_line =~ s/\s//g;
42  
43 0 my @header;
44 0 my $in_header = 1;
45 0 my $last_line = '';
46  
47 0 ▶ while (my $line = $fh->getline)
Branch: 1 branch
BranchTrueFalseTotal
if (my $line = $fh->getline) 0 0 0
48 {
49 # write from buffer
50  
51 0 ▶ if ($line =~ /^$key_line/)
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^$key_line/) { } 0 0 0
elsif (not $in_header) { } 0 0 0
52 {
53 # write final line
54 0 $last_line =~ s/[\r\n]$//g;
55  
56 0 ▶ if ($request_part{fh})
Branch: 1 branch
BranchTrueFalseTotal
if ($request_part{'fh'}) { } 0 0 0
57 {
58 0 $request_part{fh}->print($last_line);
59 }
60 else
61 {
62 0 $request_part{data} .= $last_line;
63 }
64  
65 # process last record
66 0 ▶ if ($request_part{fh})
Branch: 1 branch
BranchTrueFalseTotal
if ($request_part{"fh"}) 0 0 0
67 {
68 # stop writing
69 0 close($request_part{fh});
70  
71 0 $request_part{size} = -s $request_part{tmp_file};
72  
73 # open for reading only
74 0 open($request_part{fh}, '<' . $request_part{tmp_file});
75 }
76  
77 0 push @request_parts, { %request_part };
78  
79 # reset request_part
80 0 $in_header = 1;
81 0 @header = ();
82 0 %request_part = ();
83 0 $last_line = '';
84  
85 0 next;
86 }
87 elsif (not $in_header)
88 {
89 # if we are not in the header and we're not in a new section, write the line!
90 0 ▶ if ($request_part{fh})
Branch: 1 branch
BranchTrueFalseTotal
if ($request_part{'fh'}) { } 0 0 0
91 {
92 0 $request_part{fh}->print($last_line);
93 }
94 else
95 {
96 0 $request_part{data} .= $last_line;
97 }
98 }
99  
100 0 ▶ if ($in_header)
Branch: 1 branch
BranchTrueFalseTotal
if ($in_header) { } 0 0 0
101 {
102 # strip newlines
103 0 $line =~ s/[\r\n]//g;
104  
105 0 ▶ if ($line eq '')
Branch: 1 branch
BranchTrueFalseTotal
if ($line eq "") 0 0 0
106 {
107 # we're not in the header anymore
108 0 $in_header = 0;
109  
110 # process header and open file
111 0 my $name;
112 my $filename;
113 0 my $is_file = 0;
114 0 for my $h_line (@header)
115 {
116 0 ▶ $name = $2 if $h_line =~ /name=(['"])(.*?)\1/;
Branch: 1 branch
BranchTrueFalseTotal
if $h_line =~ /name=(['"])(.*?)\1/ 0 0 0
117 0 ▶ $filename = $2 if $h_line =~ /filename=(['"])(.*?)\1/;
Branch: 1 branch
BranchTrueFalseTotal
if $h_line =~ /filename=(['"])(.*?)\1/ 0 0 0
118 0 ▶ $is_file = 1 if $h_line =~ /filename=/;
Branch: 1 branch
BranchTrueFalseTotal
if $h_line =~ /filename=/ 0 0 0
119 }
120  
121 # define info in request part
122 0 $request_part{name} = $name;
123  
124 0 ▶ if ($is_file)
Branch: 1 branch
BranchTrueFalseTotal
if ($is_file) { } 0 0 0
125 {
126 0 $request_part{filename} = $filename;
127 0 $request_part{tmp_file} = $tmpdir . '/' . tmpfilename();
128 0 $request_part{fh} = IO::File->new('> ' . $request_part{tmp_file});
129 }
130 else
131 {
132 0 $request_part{data} = '';
133 }
134  
135 0 next;
136 }
137  
138 0 push @header, $line;
139 }
140 else
141 {
142 0 $last_line = $line;
143 }
144 }
145  
146 0 return \@request_parts;
147 }
148  
149 =head1 AUTHORS
150  
151 Bizowie <http://bizowie.com>
152  
153 =head1 COPYRIGHT AND LICENSE
154  
155 Copyright (C) 2013 Bizowie
156  
157 This library is free software. You can redistribute it and/or modify it under the same terms as Perl itself.
158  
159 =cut
160  
161 1 1;
↑ Summary blib/lib/Drogo/LoadModules.pm »