Debian Codegraph @

src/.svn/text-base/multipart.pl.svn-base

raw
1 #BSD Copyright (c) 2009, Jeff Forristal (wiretrip.net)
2 #BSD All rights reserved.
3 #BSD
4 #BSD Redistribution and use in source and binary forms, with or without
5 #BSD modification, are permitted provided that the following conditions
6 #BSD are met:
7 #BSD
8 #BSD - Redistributions of source code must retain the above copyright
9 #BSD notice, this list of conditions and the following disclaimer.
10 #BSD
11 #BSD - Redistributions in binary form must reproduce the above copyright
12 #BSD notice, this list of conditions and the following disclaimer in the
13 #BSD documentation and/or other materials provided with the distribution.
14 #BSD
15 #BSD THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 #BSD "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 #BSD LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 #BSD FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 #BSD COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 #BSD INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 #BSD BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 #BSD LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 #BSD CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 #BSD LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
25 #BSD ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 #BSD POSSIBILITY OF SUCH DAMAGE.
27
28 ########################################################################
29
30 =item B<multipart_set>
31
32 Params: \%multi_hash, $param_name, $param_value
33
34 Return: nothing
35
36 This function sets the named parameter to the given value within the
37 supplied multipart hash.
38
39 =cut
40
41 sub multipart_set {
42 my ( $hr, $n, $v ) = @_;
43 return if ( !ref($hr) ); # error check
44 return undef if ( !defined $n || $n eq '' );
45 $$hr{$n} = $v;
46 }
47
48 ########################################################################
49
50 =item B<multipart_get>
51
52 Params: \%multi_hash, $param_name
53
54 Return: $param_value, undef on error
55
56 This function retrieves the named parameter to the given value within the
57 supplied multipart hash. There is a special case where the named
58 parameter is actually a file--in which case the resulting value will be
59 "\0FILE". In general, all special values will be prefixed with a NULL
60 character. In order to get a file's info, use multipart_getfile().
61
62 =cut
63
64 sub multipart_get {
65 my ( $hr, $n ) = @_;
66 return undef if ( !ref($hr) ); # error check
67 return undef if ( !defined $n || $n eq '' );
68 return $$hr{$n};
69 }
70
71 ########################################################################
72
73 =item B<multipart_setfile>
74
75 Params: \%multi_hash, $param_name, $file_path [, $filename]
76
77 Return: undef on error, 1 on success
78
79 NOTE: this function does not actually add the contents of $file_path into
80 the %multi_hash; instead, multipart_write() inserts the content when
81 generating the final request.
82
83 =cut
84
85 sub multipart_setfile {
86 my ( $hr, $n, $path ) = ( shift, shift, shift );
87 my ($fname) = shift;
88
89 return undef if ( !ref($hr) ); # error check
90 return undef if ( !defined $n || $n eq '' );
91 return undef if ( !defined $path );
92 return undef if ( !( -e $path && -f $path ) );
93
94 if ( !defined $fname ) {
95 $path =~ m/[\\\/]([^\\\/]+)$/;
96 $fname = $1 || "whisker-file";
97 }
98
99 $$hr{$n} = "\0FILE";
100 $$hr{"\0$n"} = [ $path, $fname ];
101 return 1;
102 }
103
104 ########################################################################
105
106 =item B<multipart_getfile>
107
108 Params: \%multi_hash, $file_param_name
109
110 Return: $path, $name ($path=undef on error)
111
112 multipart_getfile is used to retrieve information for a file
113 parameter contained in %multi_hash. To use this you would most
114 likely do:
115
116 ($path,$fname)=LW2::multipart_getfile(\%multi,"param_name");
117
118 =cut
119
120 sub multipart_getfile {
121 my ( $hr, $n ) = @_;
122
123 return undef if ( !ref($hr) ); # error check
124 return undef if ( !defined $n || $n eq '' );
125 return undef if ( !defined $$hr{$n} || $$hr{$n} ne "\0FILE" );
126
127 return @{ $$hr{"\0$n"} };
128 }
129
130 ########################################################################
131
132 =item B<multipart_boundary>
133
134 Params: \%multi_hash [, $new_boundary_name]
135
136 Return: $current_boundary_name
137
138 multipart_boundary is used to retrieve, and optionally set, the
139 multipart boundary used for the request.
140
141 NOTE: the function does no checking on the supplied boundary, so if
142 you want things to work make sure it's a legit boundary. Libwhisker
143 does *not* prefix it with any '---' characters.
144
145 =cut
146
147 sub multipart_boundary {
148 my ( $hr, $new ) = @_;
149 my $ret;
150
151 return undef if ( !ref($hr) ); # error check
152
153 if ( !defined $$hr{"\0BOUNDARY"} ) {
154
155 # create boundary on the fly
156 my $b = uc( utils_randstr(20) );
157 my $b2 = '-' x 32;
158 $$hr{"\0BOUNDARY"} = "$b2$b";
159 }
160
161 $ret = $$hr{"\0BOUNDARY"};
162 if ( defined $new ) {
163 $$hr{"\0BOUNDARY"} = $new;
164 }
165
166 return $ret;
167 }
168
169 ########################################################################
170
171 =item B<multipart_write>
172
173 Params: \%multi_hash, \%request
174
175 Return: 1 if successful, undef on error
176
177 multipart_write is used to parse and construct the multipart data
178 contained in %multi_hash, and place it ready to go in the given whisker
179 hash (%request) structure, to be sent to the server.
180
181 NOTE: file contents are read into the final %request, so it's possible for
182 the hash to get *very* large if you have (a) large file(s).
183
184 =cut
185
186 sub multipart_write {
187 my ( $mp, $hr ) = @_;
188
189 return undef if ( !ref($mp) ); # error check
190 return undef if ( !ref($hr) ); # error check
191
192 if ( !defined $$mp{"\0BOUNDARY"} ) {
193
194 # create boundary on the fly
195 my $b = uc( utils_randstr(20) );
196 my $b2 = '-' x 32;
197 $$mp{"\0BOUNDARY"} = "$b2$b";
198 }
199
200 my $B = $$mp{"\0BOUNDARY"};
201 my $EOL = $$hr{whisker}->{http_eol} || "\x0d\x0a";
202
203 my $keycount = 0;
204 foreach ( keys %$mp ) {
205 next if ( substr( $_, 0, 1 ) eq "\0" );
206 $keycount++;
207 if ( $$mp{$_} eq "\0FILE" ) {
208 my ( $path, $name ) = multipart_getfile( $mp, $_ );
209 next if ( !defined $path );
210 $$hr{whisker}->{data} .= "$B$EOL";
211 $$hr{whisker}->{data} .=
212 "Content-Disposition: " . "form-data; name=\"$_\"; ";
213 $$hr{whisker}->{data} .= "filename=\"$name\"$EOL";
214 $$hr{whisker}->{data} .=
215 "Content-Type: " . "application/octet-stream$EOL";
216 $$hr{whisker}->{data} .= $EOL;
217 next if ( !open( IN, "<$path" ) );
218 binmode(IN); # stupid Windows
219
220 while (<IN>) {
221 $$hr{whisker}->{data} .= $_;
222 }
223 close(IN);
224 $$hr{whisker}->{data} .= $EOL; # WARNING: is this right?
225 }
226 else {
227 $$hr{whisker}->{data} .= "$B$EOL";
228 $$hr{whisker}->{data} .=
229 "Content-Disposition: " . "form-data; name=\"$_\"$EOL";
230 $$hr{whisker}->{data} .= "$EOL$$mp{$_}$EOL";
231 }
232 }
233
234 if ($keycount) {
235 $$hr{whisker}->{data} .= "$B--$EOL"; # closing boundary
236 $$hr{"Content-Length"} = length( $$hr{whisker}->{data} );
237 $$hr{"Content-Type"} = "multipart/form-data; boundary=$B";
238 return 1;
239 }
240 else {
241
242 # multipart hash didn't contain params to upload
243 return undef;
244 }
245 }
246
247 ########################################################################
248
249 =item B<multipart_read>
250
251 Params: \%multi_hash, \%hout_response [, $filepath ]
252
253 Return: 1 if successful, undef on error
254
255 multipart_read will parse the data contents of the supplied
256 %hout_response hash, by passing the appropriate info to
257 multipart_read_data(). Please see multipart_read_data() for more
258 info on parameters and behaviour.
259
260 NOTE: this function will return an error if the given %hout_response
261 Content-Type is not set to "multipart/form-data".
262
263 =cut
264
265 sub multipart_read {
266 my ( $mp, $hr, $fp ) = @_;
267
268 return undef if ( !( defined $mp && ref($mp) ) );
269 return undef if ( !( defined $hr && ref($hr) ) );
270
271 my $ctype = utils_find_lowercase_key( $hr, 'content-type' );
272 return undef if ( !defined $ctype );
273 return undef if ( $ctype !~ m#^multipart/form-data#i );
274
275 return multipart_read_data( $mp, \$$hr{'whisker'}->{'data'}, undef, $fp );
276
277 }
278
279 ########################################################################
280
281 =item B<multipart_read_data>
282
283 Params: \%multi_hash, \$data, $boundary [, $filepath ]
284
285 Return: 1 if successful, undef on error
286
287 multipart_read_data parses the contents of the supplied data using
288 the given boundary and puts the values in the supplied %multi_hash.
289 Embedded files will *not* be saved unless a $filepath is given, which
290 should be a directory suitable for writing out temporary files.
291
292 NOTE: currently only application/octet-stream is the only supported
293 file encoding. All other file encodings will not be parsed/saved.
294
295 =cut
296
297 sub multipart_read_data {
298 my ( $mp, $dr, $bound, $fp ) = @_;
299
300 return undef if ( !( defined $mp && ref($mp) ) );
301 return undef if ( !( defined $dr && ref($dr) ) );
302
303 # if $bound is undef, then we'll snag what looks to be
304 # the first boundry from the data.
305 if ( !defined $bound ) {
306 if ( $$dr =~ /([-]{5,}[A-Z0-9]+)[\r\n]/i ) {
307 $bound = $1;
308 }
309 else {
310
311 # we didn't spot a typical boundary; error
312 return undef;
313 }
314 }
315
316 if ( defined $fp && !( -d $fp && -w $fp ) ) {
317 $fp = undef;
318 }
319
320 my $line = utils_getline_crlf( $dr, 0 );
321 return undef if ( !defined $line );
322 return undef if ( index( $line, $bound ) != 0 );
323
324 my $done = 0;
325 while ( !$done ) {
326 $done = _multipart_read_data_part( $mp, $dr, $bound, $fp );
327 }
328
329 return 1;
330 }
331
332 ########################################################################
333
334 sub _multipart_read_data_part {
335 my ( $mp, $dr, $bound, $fp ) = @_;
336
337 my $dispinfo = utils_getline_crlf($dr);
338 return 1 if ( !defined $dispinfo );
339 return 1 if ( length($dispinfo) == 0 );
340 my $lcdisp = lc($dispinfo);
341
342 if ( index( $lcdisp, 'content-disposition: form-data;' ) != 0 ) {
343 return 1;
344 } # bad disposition
345
346 my ( $s, $e, $l );
347
348 $s = index( $lcdisp, 'name="', 30 );
349 $e = index( $lcdisp, '"', $s + 6 );
350 return 1 if ( $s == -1 || $e == -1 );
351 my $NAME = substr( $dispinfo, $s + 6, $e - $s - 6 );
352
353 $s = index( $lcdisp, 'filename="', $e );
354 my $FILENAME = undef;
355 if ( $s != -1 ) {
356 $e = index( $lcdisp, '"', $s + 10 );
357 return 1 if ( $e == -1 ); # puke; malformed filename
358 $FILENAME = substr( $dispinfo, $s + 10, $e - $s - 10 );
359 $s = rindex( $FILENAME, '\\' );
360 $e = rindex( $FILENAME, '/' );
361 $s = $e if ( $e > $s );
362 $FILENAME = substr( $FILENAME, $s + 1, length($FILENAME) - $s );
363 }
364
365 my $CTYPE = utils_getline_crlf($dr);
366
367 return 1 if ( !defined $CTYPE );
368 $CTYPE = lc($CTYPE);
369
370 if ( length($CTYPE) > 0 ) {
371 $s = index( $CTYPE, 'content-type:' );
372 return 1 if ( $s != 0 ); # bad ctype line
373 $CTYPE = substr( $CTYPE, 13, length($CTYPE) - 13 );
374 $CTYPE =~ tr/ \t//d;
375 my $xx = utils_getline_crlf($dr);
376 return 1 if ( !defined $xx );
377 return 1 if ( length($xx) > 0 );
378 }
379 else {
380 $CTYPE = 'application/octet-stream';
381 }
382
383 my $VALUE = '';
384 while ( defined( $l = utils_getline_crlf($dr) ) ) {
385 last if ( index( $l, $bound ) == 0 );
386 $VALUE .= $l;
387 $VALUE .= "\r\n";
388 }
389
390 substr( $VALUE, -2, 2 ) = '';
391
392 if ( !defined $FILENAME ) { # read in param
393 $$mp{$NAME} = $VALUE;
394 return 0;
395
396 }
397 else { # read in file
398 $$mp{$NAME} = "\0FILE";
399 return 0 if ( !defined $fp );
400
401 # TODO: funky content types, like application/x-macbinary
402 if ( $CTYPE ne 'application/octet-stream' ) {
403 return 0;
404 }
405
406 my $rfn = lc( utils_randstr(12) );
407 my $fullpath = "$fp$rfn";
408
409 $$mp{"\0$NAME"} = [ undef, $FILENAME ];
410 return 0 if ( !open( OUT, ">$fullpath" ) ); # error opening file
411 binmode(OUT); # stupid Windows
412 $$mp{"\0$NAME"} = [ $fullpath, $FILENAME ];
413 print OUT $VALUE;
414 close(OUT);
415
416 return 0;
417
418 } # if !defined $FILENAME
419
420 return 0; # um, this should never be reached...
421 }
422
423 ########################################################################
424
425 =item B<multipart_files_list>
426
427 Params: \%multi_hash
428
429 Return: @files
430
431 multipart_files_list returns an array of parameter names for all
432 the files that are contained in %multi_hash.
433
434 =cut
435
436 sub multipart_files_list {
437 my ($mp) = shift;
438 my @ret;
439
440 return () if ( !( defined $mp && ref($mp) ) );
441 while ( my ( $K, $V ) = each(%$mp) ) {
442 push( @ret, $K ) if ( $V eq "\0FILE" );
443 }
444 return @ret;
445 }
446
447 ########################################################################
448
449 =item B<multipart_params_list>
450
451 Params: \%multi_hash
452
453 Return: @params
454
455 multipart_files_list returns an array of parameter names for all
456 the regular parameters (non-file) that are contained in %multi_hash.
457
458 =cut
459
460 sub multipart_params_list {
461 my ($mp) = shift;
462 my @ret;
463
464 return () if ( !( defined $mp && ref($mp) ) );
465 while ( my ( $K, $V ) = each(%$mp) ) {
466 push( @ret, $K ) if ( $V ne "\0FILE"
467 && substr( $K, 0, 1 ) ne "\0" );
468 }
469 return @ret;
470 }
471
472 ########################################################################
473