GX/HTTP/Parser/Body/MultiPart.pm

filter: Statementstmt 97.6% 126 / 129 Branchbran 86.5% 45 / 52 Conditioncond 64.2% 9 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 14 / 14 Podpod 100% 1 / 1 totaltotal 89.9% 197 / 219 CC 32 SCAR 19.8
CC
32
Cov
92%
_parse_header
13.2
parse
8.0
_parse_boundary
6.0
CRAP 7.2
« GX/HTTP/Body/Scalar.pm ↑ Summary GX/Route.pm »
1 # ----------------------------------------------------------------------------------------------------------------------
2 # GX Framework (c) 2009-2011 Jörg A. Uzarek <uzarek@runlevelnull.de>
3 # File: GX/HTTP/Parser/Body/MultiPart.pm
4 # ----------------------------------------------------------------------------------------------------------------------
5  
6 package GX::HTTP::Parser::Body::MultiPart;
7  
8 15 ▶use GX::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 15 ▶use GX::HTTP::Constants qw( CRLF CRLFCRLF );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 15 ▶use GX::HTTP::Headers;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 15 ▶use GX::HTTP::Parameters;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 15 ▶use GX::HTTP::Upload;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 15 ▶use GX::HTTP::Uploads;
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 15 ▶use File::Temp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17  
18 # ----------------------------------------------------------------------------------------------------------------------
19 # Constants
20 # ----------------------------------------------------------------------------------------------------------------------
21  
22 15 ▶use constant {
Subroutine BEGIN: called CC=1 SCAR=0.0
23 15 BUFFER_SIZE => 8192,
24 STATE_PARSING => 1,
25 STATE_DONE => 2
26 15 };
27  
28  
29 # ----------------------------------------------------------------------------------------------------------------------
30 # Class setup
31 # ----------------------------------------------------------------------------------------------------------------------
32  
33 15 ▶use GX::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 15 extends 'GX::HTTP::Parser::Body';
36  
37 15 build;
38  
39  
40 # ----------------------------------------------------------------------------------------------------------------------
41 # Public methods
42 # ----------------------------------------------------------------------------------------------------------------------
43  
44 344 ▶sub parse {
Subroutine parse: called CC=8 SCAR=20.8
Pod documented
45  
46 344 my $self = shift;
47 344 my $body = shift;
48 344 ▶ my $buffer_size = shift || BUFFER_SIZE;
Condition100%2/2
shift() || 8192
l!l
32717
MC/DC100%1/1
shift() || 8192
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
49  
50 344 ▶ $self->{'boundary'} or return;
Branch: 1 branch
BranchTrueFalseTotal
$self->{"boundary"} or return 344 0 344
51  
52 344 ▶ my $content = $body->open or return;
Branch: 1 branch
BranchTrueFalseTotal
my $content = $body->open or return 344 0 344
53  
54 344 my $context = {
55 'buffer' => CRLF,
56 'state' => STATE_PARSING,
57 'parse' => \&_parse_boundary,
58 'part' => undef,
59 'result' => {}
60 };
61  
62 READ:
63 344 ▶ while ( $content->read( my $buffer, $buffer_size ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content->read(my $buffer, $buffer_size)) 2228 5 2233
64  
65 2228 $context->{'buffer'} .= $buffer;
66  
67 2228 ▶ while ( $context->{'state'} == STATE_PARSING ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($context->{"state"} == 1) 5900 339 6239
68 5900 ▶ $context->{'parse'}->( $self, $context ) or next READ;
Branch: 1 branch
BranchTrueFalseTotal
$context->{"parse"}->($self, $context) or next READ 4011 1889 5900
69 }
70  
71 339 last READ;
72  
73 }
74  
75 344 ▶ return $context->{'state'} == STATE_DONE ? $context->{'result'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
$context->{'state'} == 2 ? : 339 5 344
76  
77 }
78  
79  
80 # ----------------------------------------------------------------------------------------------------------------------
81 # Private methods
82 # ----------------------------------------------------------------------------------------------------------------------
83  
84 1241 ▶sub _parse_body {
Subroutine _parse_body: called CC=6 SCAR=17.9
85  
86 1241 my $self = shift;
87 1241 my $context = shift;
88  
89 1241 my $part = $context->{'part'};
90  
91 1241 my $index = index( $context->{'buffer'}, $self->{'delimiter'} );
92  
93 1241 ▶ if ( $index < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < 0) 242 999 1241
94  
95 242 ▶ if ( $part->{'fh'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part->{'fh'}) { } 12 230 242
96 12 $part->{'fh'}->print( substr( $context->{'buffer'}, 0, -76, '' ) );
97 }
98 else {
99 230 $part->{'data'} .= substr( $context->{'buffer'}, 0, -76, '' );
100 }
101  
102 242 return 0;
103  
104 }
105  
106 999 ▶ if ( $part->{'fh'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($part->{'fh'}) { } 9 990 999
107  
108 9 my $fh = $part->{'fh'};
109  
110 9 $fh->print( substr( $context->{'buffer'}, 0, $index, '' ) );
111 9 $fh->flush;
112  
113 my $upload = GX::HTTP::Upload->new(
114 file => $fh->filename,
115 filename => $part->{'filename'},
116 name => $part->{'name'},
117 9 headers => $part->{'headers'},
118 cleanup => 1
119 );
120  
121 9 $fh->unlink_on_destroy( 0 );
122  
123 9 %$part = ( 'upload' => $upload );
124  
125 9 ▶ $context->{'result'}{'uploads'} ||= GX::HTTP::Uploads->new;
Condition66%2/3
$context->{"result"}{"uploads"} ||= "GX::HTTP::Uploads"->new
l!l&&r!l&&!r
450
MC/DC0%0/2
$context->{"result"}{"uploads"} ||= "GX::HTTP::Uploads"->new
$context->{"result"}{"uploads"} "GX::HTTP::Uploads"->new
Truth table66%2/3
A ||= B
A= $context->{"result"}{"uploads"}B= "GX::HTTP::Uploads"->new
ABresult
1X1
011
000
126 9 $context->{'result'}{'uploads'}->add( $upload );
127  
128 }
129 else {
130  
131 990 $part->{'data'} .= substr( $context->{'buffer'}, 0, $index, '' );
132  
133 990 ▶ $context->{'result'}{'parameters'} ||= GX::HTTP::Parameters->new;
Condition66%2/3
$context->{"result"}{"parameters"} ||= "GX::HTTP::Parameters"->new
l!l&&r!l&&!r
6583320
MC/DC0%0/2
$context->{"result"}{"parameters"} ||= "GX::HTTP::Parameters"->new
$context->{"result"}{"parameters"} "GX::HTTP::Parameters"->new
Truth table66%2/3
A ||= B
A= $context->{"result"}{"parameters"}B= "GX::HTTP::Parameters"->new
ABresult
1X1
011
000
134 990 $context->{'result'}{'parameters'}->add( $part->{'name'} => $part->{'data'} );
135  
136 }
137  
138 999 $context->{'part'} = undef;
139 999 $context->{'parse'} = \&_parse_boundary;
140  
141 999 return 1;
142  
143 }
144  
145 1809 ▶sub _parse_boundary {
Subroutine _parse_boundary: called CC=6 SCAR=17.9
146  
147 1809 my $self = shift;
148 1809 my $context = shift;
149  
150 1809 my $index = index( $context->{'buffer'}, $self->{'delimiter'} );
151  
152 1809 ▶ if ( $index < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < 0) 79 1730 1809
153 79 substr( $context->{'buffer'}, 0, -76, '' );
154 79 return 0;
155 }
156  
157 1730 ▶ if ( $index > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index > 0) 337 1393 1730
158 337 substr( $context->{'buffer'}, 0, $index, '' );
159 }
160  
161 1730 ▶ if ( length( $context->{'buffer'} ) < length( $self->{'delimiter_end'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $context->{"buffer"} < length $self->{"delimiter_end"}) 54 1676 1730
162 54 return 0;
163 }
164  
165 1676 ▶ if ( index( $context->{'buffer'}, $self->{'delimiter_end'} ) == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (index($context->{"buffer"}, $self->{"delimiter_end"}) == 0) 339 1337 1676
166 339 $context->{'state'} = STATE_DONE;
167 339 $context->{'parse'} = undef;
168 339 return 1;
169 }
170  
171 1337 $index = index( $context->{'buffer'}, CRLF, 2 );
172  
173 1337 ▶ if ( $index < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < 0) 0 1337 1337
174 0 return 0;
175 }
176  
177 1337 substr( $context->{'buffer'}, 0, $index + 2, '' );
178  
179 1337 push( @{$context->{'result'}{'parts'}}, $context->{'part'} = {} );
180  
181 1337 $context->{'parse'} = \&_parse_header;
182  
183 1337 return 1;
184  
185 }
186  
187 2850 ▶sub _parse_header {
Subroutine _parse_header: called CC=13 SCAR=25.8
188  
189 2850 my $self = shift;
190 2850 my $context = shift;
191  
192 2850 ▶ if ( length( $context->{'buffer'} ) < length( $self->{'delimiter'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $context->{"buffer"} < length $self->{"delimiter"}) 325 2525 2850
193 325 return 0;
194 }
195  
196 # No header
197 2525 ▶ if ( substr( $context->{'buffer'}, 0, 2 ) eq CRLF ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($context->{"buffer"}, 0, 2) eq "\r\n") 2 2523 2525
198  
199 2 ▶ if ( substr( $context->{'buffer'}, 0, length( $self->{'delimiter'} ) ) eq $self->{'delimiter'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($context->{'buffer'}, 0, length $self->{'delimiter'}) eq $self->{'delimiter'}) { } 2 0 2
200 2 $context->{'parse'} = \&_parse_boundary;
201 }
202 else {
203 0 substr( $context->{'buffer'}, 0, 2, '' );
204 0 $context->{'parse'} = \&_parse_body;
205 }
206  
207 2 return 1;
208  
209 }
210  
211 2523 my $index = index( $context->{'buffer'}, CRLFCRLF );
212  
213 2523 ▶ if ( $index < 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($index < 0) 928 1595 2523
214 928 return 0;
215 }
216  
217 1595 ▶ if ( length( $context->{'buffer'} ) < $index + length( $self->{'delimiter'} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $context->{"buffer"} < $index + length($self->{"delimiter"})) 261 1334 1595
218 261 return 0;
219 }
220  
221 1334 my $part = $context->{'part'};
222  
223 1334 $part->{'headers'} = GX::HTTP::Headers->parse( substr( $context->{'buffer'}, 0, $index ) );
224  
225 1334 substr( $context->{'buffer'}, 0, $index + 2, '' );
226  
227 1334 my $content_disposition = $part->{'headers'}->content_disposition;
228  
229 1334 ▶ if ( $content_disposition && $content_disposition =~ / name="?([^";\s]+)"?/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_disposition and $content_disposition =~ / name="?([^";\s]+)"?/i) { } 1006 328 1334
Condition66%2/3
$content_disposition and $content_disposition =~ / name="?([^";\s]+)"?/i
!ll&&!rl&&r
32801006
MC/DC50%1/2
$content_disposition and $content_disposition =~ / name="?([^";\s]+)"?/i
$content_disposition $content_disposition =~ / name="?([^";\s]+)"?/i
Truth table66%2/3
A and B
A= $content_dispositionB= $content_disposition =~ / name="?([^";\s]+)"?/i
ABresult
0X0
100
111
230 1006 $part->{'name'} = $1;
231 }
232 else {
233 # Discard this part
234 328 $context->{'parse'} = \&_parse_boundary;
235 328 return 1;
236 }
237  
238 1006 ▶ if ( $content_disposition =~ / filename="?([^"]*)"?/i ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($content_disposition =~ / filename="?([^"]*)"?/i) 12 994 1006
239  
240 12 ▶ if ( $self->{'discard_uploads'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"discard_uploads"}) 3 9 12
241 # Discard this part
242 3 $context->{'parse'} = \&_parse_boundary;
243 3 return 1;
244 }
245  
246 9 $part->{'filename'} = $1;
247  
248 $part->{'fh'} = File::Temp->new(
249 UNLINK => 1,
250 9 ▶ $self->{'tmp_dir'} ? ( DIR => $self->{'tmp_dir'} ) : ()
Branch: 2 branches
BranchTrueFalseTotal
$part->{"fh"} = "File::Temp"->new("UNLINK", 1, $self->{"tmp_dir"} ? ("DIR", $self->{"tmp_dir"}) : ()) or &throw("Cannot create temporary file ($!)") 9 0 9
$self->{'tmp_dir'} ? : 0 9 9
251 ) or throw "Cannot create temporary file ($!)";
252  
253 }
254  
255 1003 ▶ if ( substr( $context->{'buffer'}, 0, length( $self->{'delimiter'} ) ) eq $self->{'delimiter'} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (substr($context->{'buffer'}, 0, length $self->{'delimiter'}) eq $self->{'delimiter'}) { } 4 999 1003
256 4 $context->{'parse'} = \&_parse_boundary;
257 }
258 else {
259 999 substr( $context->{'buffer'}, 0, 2, '' );
260 999 $context->{'parse'} = \&_parse_body;
261 }
262  
263 1003 return 1;
264  
265 }
266  
267  
268 # ----------------------------------------------------------------------------------------------------------------------
269 # Internal methods
270 # ----------------------------------------------------------------------------------------------------------------------
271  
272 346 ▶sub __initialize {
Subroutine __initialize: called CC=3 SCAR=11.4
273  
274 346 my $self = shift;
275 346 my $args = shift;
276  
277 346 ▶ if ( $args->{'content_type'} && $args->{'content_type'} =~ /boundary="?([^";,]+)"?/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"content_type"} and $args->{"content_type"} =~ /boundary="?([^";,]+)"?/) 346 0 346
Condition33%1/3
$args->{"content_type"} and $args->{"content_type"} =~ /boundary="?([^";,]+)"?/
!ll&&!rl&&r
00346
MC/DC0%0/2
$args->{"content_type"} and $args->{"content_type"} =~ /boundary="?([^";,]+)"?/
$args->{"content_type"} $args->{"content_type"} =~ /boundary="?([^";,]+)"?/
Truth table33%1/3
A and B
A= $args->{"content_type"}B= $args->{"content_type"} =~ /boundary="?([^";,]+)"?/
ABresult
0X0
100
111
278 346 $self->{'boundary'} = $1;
279 346 $self->{'delimiter'} = CRLF . '--' . $1;
280 346 $self->{'delimiter_end'} = CRLF . '--' . $1 . '--';
281 }
282  
283 346 return;
284  
285 }
286  
287  
288 15 1;
289  
290 __END__
« GX/HTTP/Body/Scalar.pm ↑ Summary GX/Route.pm »