Test/Input/Multipart.pm

filter: Statementstmt 84.6% 44 / 52 Branchbran 33.3% 2 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 88.8% 8 / 9 Podpod 25.0% 1 / 4 totaltotal 77.4% 55 / 71 CC 6 SCAR 12.7
CC
6
Cov
79%
_build_data
6.8
make
2.0
BEGIN
1.0
CRAP 3.6
« Test/Form/Widget/Input/Text_Field.pm ↑ Summary Test/Page/Real.pm »
1 package CGI::Test::Input::Multipart;
2 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 ####################################################################
5 # $Id: Multipart.pm 411 2011-09-26 11:19:30Z nohuhu@nohuhu.org $
6 # $Name: cgi-test_0-104_t1 $
7 ####################################################################
8 #
9 # Copyright (c) 2001, Raphael Manfredi
10 #
11 # You may redistribute only under the terms of the Artistic License,
12 # as specified in the README file that comes with the distribution.
13 #
14  
15 #
16 # POST input data to be encoded with "multipart/form-data".
17 #
18  
19 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 2 ▶use base qw(CGI::Test::Input);
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 #
24 # ->new
25 #
26 # Creation routine
27 #
28 sub new
29 2 ▶{
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
30 2 my $this = bless {}, shift;
31 2 $this->_init;
32 $this->{boundary} =
33 2 "-------------cgi-test--------------"
34 . int(rand(1 << 31)) . '-'
35 . int(rand(1 << 31));
36 2 return $this;
37 }
38  
39 # DEPRECATED METHOD
40 sub make
41 0 ▶{ #
Subroutine make: not called CC=1 SCAR=6.9
Pod undocumented
42 0 my $class = shift;
43 0 return $class->new(@_);
44 }
45  
46 #
47 # Attribute access
48 #
49  
50 sub boundary
51 3 ▶{
Subroutine boundary: called CC=1 SCAR=0.0
Pod undocumented
52 3 my $this = shift;
53 3 return $this->{boundary};
54 }
55  
56 #
57 # Defined interface
58 #
59  
60 sub mime_type
61 1 ▶{
Subroutine mime_type: called CC=1 SCAR=0.0
Pod documented
62 1 my $this = shift;
63 1 "multipart/form-data; boundary=" . $this->boundary();
64 }
65  
66 #
67 # ->_build_data
68 #
69 # Rebuild data buffer from input fields.
70 #
71 sub _build_data
72 2 ▶{
Subroutine _build_data: called CC=6 SCAR=19.2
73 2 my $this = shift;
74  
75 2 my $CRLF = "\015\012";
76 2 my $data = '';
77 2 my $fmt = 'Content-Disposition: form-data; name="%s"';
78 2 my $boundary = "--" . $this->boundary(); # With extra "--" per MIME specs
79  
80 # XXX field name encoding of special chars?
81 # XXX does not escape "" in filenames
82  
83 2 foreach my $tuple (@{$this->_fields()})
84 {
85 32 my ($name, $value) = @$tuple;
86 32 $data .= $boundary . $CRLF;
87 32 $data .= sprintf($fmt, $name) . $CRLF . $CRLF;
88 32 $data .= $value . $CRLF;
89 }
90  
91 2 foreach my $tuple (@{$this->_files()})
92 {
93 2 my ($name, $value, $content) = @$tuple;
94 2 $data .= $boundary . $CRLF;
95 2 $data .= sprintf($fmt, $name);
96 2 $data .= sprintf('; filename="%s"', $value) . $CRLF;
97 2 $data .= "Content-Type: application/octet-stream" . $CRLF . $CRLF;
98 2 ▶ if (defined $content)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $content) { } 0 2 2
99 {
100 0 $data .= $content;
101 }
102 else
103 {
104 2 local *FILE;
105 2 ▶ if (open(FILE, $value))
Branch: 1 branch
BranchTrueFalseTotal
if (open FILE, $value) 0 2 2
106 { # Might not exist, but that's OK
107 0 binmode FILE;
108 0 local $_;
109 0 ▶ while (<FILE>)
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline FILE)) 0 0 0
110 {
111 0 $data .= $_;
112 }
113 0 close FILE;
114 }
115 }
116 }
117  
118 2 $data .= $boundary . $CRLF;
119  
120 2 return $data;
121 }
122  
123 2 1;
124  
125 =head1 NAME
126  
127 CGI::Test::Input::Multipart - POST input encoded as multipart/form-data
128  
129 =head1 SYNOPSIS
130  
131 # Inherits from CGI::Test::Input
132 require CGI::Test::Input::Multipart;
133  
134 my $input = CGI::Test::Input::Multipart->new();
135  
136 =head1 DESCRIPTION
137  
138 This class represents the input for HTTP POST requests, encoded
139 as C<multipart/form-data>.
140  
141 Please see L<CGI::Test::Input> for interface details.
142  
143 =head1 AUTHORS
144  
145 The original author is Raphael Manfredi.
146  
147 Steven Hilton was long time maintainer of this module.
148  
149 Current maintainer is Alexander Tokarev F<E<lt>tokarev@cpan.orgE<gt>>.
150  
151 =head1 SEE ALSO
152  
153 CGI::Test::Input(3).
154  
155 =cut
156  
« Test/Form/Widget/Input/Text_Field.pm ↑ Summary Test/Page/Real.pm »