lib/HTTP/Promise/Body/Form/Data.pm

filter: Statementstmt 58.7% 108 / 184 Branchbran 36.0% 31 / 86 Conditioncond 29.1% 14 / 48 MC/DCmcdc 7.4% 2 / 27 Subroutinesub 63.6% 14 / 22 Podpod 100% 11 / 11 totaltotal 47.6% 180 / 378 CC 69 SCAR 33.6
CC
69
Cov
48%
length
90.0
as_urlencoded
72.0
__ANON__
30.0
CRAP 28.8
« blib/lib/HTTP/Promise/Stream/QuotedPrint.pm ↑ Summary blib/lib/HTTP/Promise/Headers/Range.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Body/Form/Data.pm
3 ## Version v0.2.1
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/06/13
7 ## Modified 2026/03/09
8 ## All rights reserved.
9 ##
10 ##
11 ## This program is free software; you can redistribute it and/or modify it
12 ## under the same terms as Perl itself.
13 ##----------------------------------------------------------------------------
14 package HTTP::Promise::Body::Form::Data;
15 BEGIN
16 {
17 3 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 3 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 3 ▶ warnings::register_categories( 'HTTP::Promise' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 3 ▶ use parent qw( HTTP::Promise::Body::Form );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 3 ▶ use vars qw( $VERSION $CRLF $DEBUG );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 ▶ use Data::UUID;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 3 our $CRLF = "\015\012";
24 3 our $VERSION = 'v0.2.1';
25 3 our $DEBUG = 0; # REMOVE ME
26 };
27  
28 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 sub init
32 {
33 0 ▶ my $self = shift( @_ );
Subroutine init: not called CC=2 SCAR=17.9
Pod documented
34 0 $self->{order} = [];
35 0 $self->{debug} = $DEBUG;
36 0 $self->{_init_strict_use_sub} = 1;
37 0 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 0 0
38 0 return( $self );
39 }
40  
41 sub as_string
42 {
43 1 ▶ my $self = shift( @_ );
Subroutine as_string: called CC=8 SCAR=23.2
Pod documented
44 1 my $opts = $self->_get_args_as_hash( @_ );
45 1 ▶ my $boundary = $opts->{boundary} || $self->boundary || Data::UUID->new->create_str;
Condition33%2/6
$opts->{'boundary'} || $self->boundary || 'Data::UUID'->new->create_str
l!l&&r!l&&!r
100
$opts->{"boundary"} or $self->boundary
l!l&&r!l&&!r
100
MC/DC0%0/3
$opts->{'boundary'} || $self->boundary || 'Data::UUID'->new->create_str
$opts->{"boundary"} $self->boundary 'Data::UUID'->new->create_str
Truth table25%1/4
A or B || C
A= $opts->{"boundary"}B= $self->boundaryC= 'Data::UUID'->new->create_str
ABCresult
1XX1
01X1
0011
0000
46 1 ▶ my $eol = $opts->{eol} || $CRLF;
Condition33%1/3
$opts->{'eol'} || $HTTP::Promise::Body::Form::Data::CRLF
l!l&&r!l&&!r
010
MC/DC0%0/2
$opts->{'eol'} || $HTTP::Promise::Body::Form::Data::CRLF
$opts->{'eol'} $HTTP::Promise::Body::Form::Data::CRLF
Truth table33%1/3
A || B
A= $opts->{'eol'}B= $HTTP::Promise::Body::Form::Data::CRLF
ABresult
1X1
011
000
47 1 my $parts = $self->make_parts( $opts );
48 1 ▶ return( $self->pass_error ) if( !defined( $parts ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parts 0 1 1
49 1 my $res = $self->new_scalar;
50 1 for( @$parts )
51 {
52 4 my $part = $_->as_string( $eol ) . $eol;
53 4 ▶ return( $self->pass_error( $_->error ) ) if( !defined( $part ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part 0 4 4
54 4 $$res .= "--${boundary}" . $eol . $part;
55 }
56 1 ▶ $res .= "--${boundary}--${eol}" if( $res->length );
Branch: 1 branch
BranchTrueFalseTotal
if $res->length 1 0 1
57 1 return( $res );
58 }
59  
60 sub as_urlencoded
61 {
62 0 ▶ my $self = shift( @_ );
Subroutine as_urlencoded: not called CC=8 SCAR=42.8
Pod documented
63 0 my $hash = {};
64 0 my $keys = $self->keys->sort;
65 my $process = sub
66 {
67 0 ▶ my( $n, $v ) = @_;
Subroutine __ANON__: not called CC=5 SCAR=34.0
68 0 ▶ if( $self->_is_a( $v => 'HTTP::Promise::Body::Form::Field' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($v, "HTTP::Promise::Body::Form::Field")) 0 0 0
69 {
70 0 my $this = $v;
71 0 $v = $this->as_string( binmode => 'utf-8' );
72 0 ▶ return( $self->pass_error( $this->error ) ) if( !defined( $v ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 0 0 0
73 }
74 0 ▶ if( exists( $hash->{ $n } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (exists $hash->{$n}) { } 0 0 0
75 {
76 0 ▶ $hash->{ $n } = [$hash->{ $n }] if( ref( $hash->{ $n } ) ne 'ARRAY' );
Branch: 1 branch
BranchTrueFalseTotal
if ref $hash->{$n} ne "ARRAY" 0 0 0
77 0 push( @{$hash->{ $n }}, $v );
78 }
79 else
80 {
81 0 $hash->{ $n } = $v;
82 }
83 0 return(1);
84 0 };
85
86 0 foreach my $n ( @$keys )
87 {
88 0 my $v = $self->{ $n };
89 0 ▶ if( $self->_is_array( $v ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_array($v)) { } 0 0 0
90 {
91 0 foreach my $v2 ( @$v )
92 {
93 0 ▶ $process->( $n, $v2 ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
&$process($n, $v2) or return $self->pass_error 0 0 0
94 }
95 }
96 else
97 {
98 0 ▶ $process->( $n, $v ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
&$process($n, $v) or return $self->pass_error 0 0 0
99 }
100 }
101 0 ▶ $self->_load_class( 'HTTP::Promise::Body::Form' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Body::Form") or return $self->pass_error 0 0 0
102 0 ▶ my $form = HTTP::Promise::Body::Form->new( $hash ) ||
Condition0%0/2
'HTTP::Promise::Body::Form'->new($hash) || (return $self->pass_error('HTTP::Promise::Body::Form'->error))
l!l
00
MC/DC0%0/1
'HTTP::Promise::Body::Form'->new($hash) || (return $self->pass_error('HTTP::Promise::Body::Form'->error))
'HTTP::Promise::Body::Form'->new($hash)
Truth table0%0/2
A
A= 'HTTP::Promise::Body::Form'->new($hash)
Aresult
11
00
103 return( $self->pass_error( HTTP::Promise::Body::Form->error ) );
104 0 return( $form );
105 }
106  
107 0 ▶sub boundary { return( shift->_internal( 'boundary', '_set_get_scalar', @_ ) ); }
Subroutine boundary: not called CC=1 SCAR=6.9
Pod documented
108  
109 # in HTTP::Promise::Body::Form::Data
110  
111 sub length
112 {
113 0 ▶ my $self = shift( @_ );
Subroutine length: not called CC=9 SCAR=45.0
Pod documented
114 0 my $opts = $self->_get_args_as_hash( @_ );
115 0 ▶ my $eol = $opts->{eol} || $CRLF;
Condition0%0/3
$opts->{'eol'} || $HTTP::Promise::Body::Form::Data::CRLF
l!l&&r!l&&!r
000
MC/DC0%0/2
$opts->{'eol'} || $HTTP::Promise::Body::Form::Data::CRLF
$opts->{'eol'} $HTTP::Promise::Body::Form::Data::CRLF
Truth table0%0/3
A || B
A= $opts->{'eol'}B= $HTTP::Promise::Body::Form::Data::CRLF
ABresult
1X1
011
000
116 0 ▶ my $boundary = $opts->{boundary} || $self->boundary || Data::UUID->new->create_str;
Condition0%0/6
$opts->{'boundary'} || $self->boundary || 'Data::UUID'->new->create_str
l!l&&r!l&&!r
000
$opts->{"boundary"} or $self->boundary
l!l&&r!l&&!r
000
MC/DC0%0/3
$opts->{'boundary'} || $self->boundary || 'Data::UUID'->new->create_str
$opts->{"boundary"} $self->boundary 'Data::UUID'->new->create_str
Truth table0%0/4
A or B || C
A= $opts->{"boundary"}B= $self->boundaryC= 'Data::UUID'->new->create_str
ABCresult
1XX1
01X1
0011
0000
117  
118 0 my $total = 0;
119 0 my $sep = '--' . $boundary . $eol;
120 0 my $end = '--' . $boundary . '--' . $eol;
121  
122 0 require bytes;
123 0 my $parts = $self->make_parts( $opts );
124 0 ▶ return( $self->pass_error ) if( !defined( $parts ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $parts 0 0 0
125  
126 0 foreach my $ent ( @$parts )
127 {
128 # leading boundary
129 0 $total += CORE::length( $sep );
130  
131 # body bytes + trailing CRLF
132 0 my $part = $ent->as_string( $eol );
133 0 ▶ return( $self->pass_error( $_->error ) ) if( !defined( $part ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $part 0 0 0
134 # my $blen = bytes::length( $part );
135 # Ensure text parts are ASCII, use bytes::length for binary
136 0 ▶ my $blen = ( ref( $ent ) && $ent->is_binary ) ? bytes::length( $part ) : CORE::length( $part );
Branch: 1 branch
BranchTrueFalseTotal
ref $ent && $ent->is_binary ? : 0 0 0
Condition0%0/3
ref $ent and $ent->is_binary
!ll&&!rl&&r
000
MC/DC0%0/2
ref $ent and $ent->is_binary
ref $ent $ent->is_binary
Truth table0%0/3
A and B
A= ref $entB= $ent->is_binary
ABresult
0X0
100
111
137 0 $total += $blen;
138 0 $total += CORE::length( $eol );
139 }
140  
141 # closing boundary
142 0 $total += CORE::length( $end );
143  
144 0 return( $total );
145 }
146  
147 sub make_parts
148 {
149 4 ▶ my $self = shift( @_ );
Subroutine make_parts: called CC=15 SCAR=29.9
Pod documented
150 4 my $tie_obj = $self->_tie_object;
151 4 my $tie_flag = $tie_obj->enable;
152 4 ▶ $tie_obj->enable(1) if( !$tie_flag );
Branch: 1 branch
BranchTrueFalseTotal
unless $tie_flag 0 4 4
153 4 my $opts = $self->_get_args_as_hash( @_ );
154 4 my $order = $self->order;
155 my $keys = $self->_is_array( $opts->{fields} )
156 ? $self->new_array( $opts->{fields} )
157 4 ▶ : ( defined( $order ) && scalar( @$order ) )
Branch: 2 branches
BranchTrueFalseTotal
$self->_is_array($opts->{'fields'}) ? : 1 3 4
defined $order && scalar @$order ? : 0 3 3
Condition50%1/2
defined $order and scalar @$order
!ll
03
MC/DC0%0/1
defined $order and scalar @$order
defined $order
Truth table50%1/2
A
A= defined $order
Aresult
00
11
158 ? $order
159 : $self->keys->sort;
160 4 ▶ $self->_load_class( 'HTTP::Promise::Entity' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Entity") or return $self->pass_error 0 4 4
161 4 ▶ $self->_load_class( 'HTTP::Promise::Headers' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Headers") or return $self->pass_error 0 4 4
162 4 my $parts = $self->new_array;
163  
164 my $process = sub
165 {
166 7 ▶ my( $n, $v ) = @_;
Subroutine __ANON__: called CC=20 SCAR=31.2
167 7 my( $headers, $body );
168 7 ▶ if( $self->_is_a( $v => 'HTTP::Promise::Body::Form::Field' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($v, 'HTTP::Promise::Body::Form::Field')) { } 4 3 7
169 {
170 4 ▶ $headers = $v->headers || HTTP::Promise::Headers->new;
Condition33%1/3
$v->headers || 'HTTP::Promise::Headers'->new
l!l&&r!l&&!r
400
MC/DC0%0/2
$v->headers || 'HTTP::Promise::Headers'->new
$v->headers 'HTTP::Promise::Headers'->new
Truth table33%1/3
A || B
A= $v->headersB= 'HTTP::Promise::Headers'->new
ABresult
1X1
011
000
171 4 $body = $v->body;
172 }
173 else
174 {
175 3 $headers = HTTP::Promise::Headers->new;
176 # $body = HTTP::Promise::Entity->new_body( string => $v ) ||
177 # return( $self->pass_error( HTTP::Promise::Entity->error ) );
178 3 ▶ if( $self->_is_a( $v => 'Module::Generic::File' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($v, 'Module::Generic::File')) { } 3 0 3
179 {
180 3 $body = HTTP::Promise::Entity->new_body( file => $v );
181 }
182 else
183 {
184 0 $body = HTTP::Promise::Entity->new_body( string => $v );
185 }
186 3 ▶ if( !defined( $body ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $body) 0 3 3
187 {
188 0 return( $self->pass_error( HTTP::Promise::Entity->error ) );
189 }
190 }
191 7 my $dispo = $headers->content_disposition;
192 7 ▶ my $cd = $dispo
Branch: 1 branch
BranchTrueFalseTotal
$dispo ? : 4 3 7
193 ? $headers->new_field( 'Content-Disposition' => $dispo )
194 : $headers->new_field( 'Content-Disposition' );
195 7 ▶ return( $self->pass_error( $headers->error ) ) if( !defined( $cd ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $cd 0 7 7
196 7 $cd->disposition( 'form-data' );
197 7 $cd->name( $n );
198  
199 7 ▶ if( $self->_is_a( $body => 'HTTP::Promise::Body::File' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->_is_a($body, "HTTP::Promise::Body::File")) 3 4 7
200 {
201 3 ▶ if( !$cd->filename )
Branch: 1 branch
BranchTrueFalseTotal
unless ($cd->filename) 3 0 3
202 {
203 3 my $basename = $body->basename;
204 3 $cd->filename( $basename );
205 }
206 # Set Content-Type per part when we can
207 3 ▶ my $raw_mime = $body->finfo->mime_type || 'application/octet-stream';
Condition50%1/2
$body->finfo->mime_type || 'application/octet-stream'
l!l
30
MC/DC0%0/1
$body->finfo->mime_type || 'application/octet-stream'
$body->finfo->mime_type
Truth table50%1/2
A
A= $body->finfo->mime_type
Aresult
11
00
208 3 ▶ my $filename = $body->basename // '';
Condition50%1/2
$body->basename // ""
l!l
30
MC/DC0%0/1
$body->basename // ""
$body->basename
Truth table50%1/2
A
A= $body->basename
Aresult
11
00
209  
210 3 ▶ $self->_load_class( 'HTTP::Promise::Stream' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Stream") or return $self->pass_error 0 3 3
211 3 ▶ my $stream = HTTP::Promise::Stream->new( \'' ) || return( $self->pass_error( HTTP::Promise::Stream->error ) );
Condition50%1/2
'HTTP::Promise::Stream'->new(\'') || (return $self->pass_error('HTTP::Promise::Stream'->error))
l!l
30
MC/DC0%0/1
'HTTP::Promise::Stream'->new(\'') || (return $self->pass_error('HTTP::Promise::Stream'->error))
'HTTP::Promise::Stream'->new(\'')
Truth table50%1/2
A
A= 'HTTP::Promise::Stream'->new(\'')
Aresult
11
00
212  
213 # Determine encoding layers from file extensions (handles multiple encodings)
214 3 my $encodings = $stream->suffix2encoding( $filename );
215  
216 3 my $ctype;
217 3 ▶ if( $encodings->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($encodings->length) { } 3 0 3
218 {
219 3 my $enc_suffix = $stream->_encoding2suffix_map;
220 3 my %enc_suffixes = map{ $enc_suffix->{ $_ } => 1 }
221 3 grep{ exists( $enc_suffix->{ $_ } ) } @$encodings;
222 # Strip the encoding suffixes to find the inner format.
223 # reverse + pop removes the basename, leaving extensions outermost-first.
224 3 my @parts = reverse( split( /\./, $filename ) );
225 3 pop( @parts );
226 # Skip the encoding suffixes to reach the inner format extension
227 3 ▶ while( @parts && exists( $enc_suffixes{ $parts[0] } ) )
Branch: 1 branch
BranchTrueFalseTotal
if (@parts and exists $enc_suffixes{$parts[0]}) 3 3 6
Condition100%3/3
@parts and exists $enc_suffixes{$parts[0]}
!ll&&!rl&&r
123
MC/DC100%2/2
@parts and exists $enc_suffixes{$parts[0]}
@parts exists $enc_suffixes{$parts[0]}
Truth table100%3/3
A and B
A= @partsB= exists $enc_suffixes{$parts[0]}
ABresult
0X0
100
111
228 {
229 3 shift( @parts );
230 }
231 3 ▶ my $inner_ext = @parts ? lc( $parts[0] ) : '';
Branch: 1 branch
BranchTrueFalseTotal
@parts ? : 2 1 3
232 3 ▶ $ctype = ( $inner_ext eq 'tar' ) ? 'application/x-tar' : "$raw_mime";
Branch: 1 branch
BranchTrueFalseTotal
$inner_ext eq 'tar' ? : 2 1 3
233 3 $headers->header( 'Content-Encoding' => join( ', ', @$encodings ) );
234 }
235 else
236 {
237 0 $ctype = "$raw_mime";
238 }
239  
240 3 ▶ my $ctfld = $headers->new_field( 'Content-Type' ) || return( $self->pass_error( $headers->error ) );
Condition50%1/2
$headers->new_field('Content-Type') || (return $self->pass_error($headers->error))
l!l
30
MC/DC0%0/1
$headers->new_field('Content-Type') || (return $self->pass_error($headers->error))
$headers->new_field('Content-Type')
Truth table50%1/2
A
A= $headers->new_field('Content-Type')
Aresult
11
00
241 3 $ctfld->type( $ctype );
242 3 $headers->content_type( "$ctfld" );
243 }
244 7 $headers->content_disposition( "$cd" );
245
246 7 ▶ my $ent = HTTP::Promise::Entity->new( headers => $headers, body => $body ) ||
Condition50%1/2
'HTTP::Promise::Entity'->new('headers', $headers, 'body', $body) || (return $self->pass_error('HTTP::Promise::Entity'->error))
l!l
70
MC/DC0%0/1
'HTTP::Promise::Entity'->new('headers', $headers, 'body', $body) || (return $self->pass_error('HTTP::Promise::Entity'->error))
'HTTP::Promise::Entity'->new('headers', $headers, 'body', $body)
Truth table50%1/2
A
A= 'HTTP::Promise::Entity'->new('headers', $headers, 'body', $body)
Aresult
11
00
247 return( $self->pass_error( HTTP::Promise::Entity->error ) );
248 7 $ent->name( $n );
249 7 return( $ent );
250 4 };
251  
252 4 foreach my $n ( @$keys )
253 {
254 7 my $v = $self->{ $n };
255 7 ▶ if( ref( $v ) eq 'ARRAY' )
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'ARRAY') { } 0 7 7
256 {
257 0 foreach my $v2 ( @$v )
258 {
259 0 my $ent = $process->( $n, $v2 );
260 0 ▶ if( !defined( $ent ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ent) 0 0 0
261 {
262 0 ▶ $tie_obj->enable(0) if( !$tie_flag );
Branch: 1 branch
BranchTrueFalseTotal
unless $tie_flag 0 0 0
263 0 return( $self->pass_error );
264 }
265 0 $ent->name( $n );
266 0 $parts->push( $ent );
267 }
268 }
269 else
270 {
271 7 my $ent = $process->( $n, $v );
272 7 ▶ if( !defined( $ent ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ent) 0 7 7
273 {
274 0 ▶ $tie_obj->enable(0) if( !$tie_flag );
Branch: 1 branch
BranchTrueFalseTotal
unless $tie_flag 0 0 0
275 0 return( $self->pass_error );
276 }
277 7 $ent->name( $n );
278 7 $parts->push( $ent );
279 }
280 }
281 4 ▶ $tie_obj->enable(0) if( !$tie_flag );
Branch: 1 branch
BranchTrueFalseTotal
unless $tie_flag 0 4 4
282 4 return( $parts );
283 }
284  
285 sub new_field
286 {
287 4 ▶ my $self = shift( @_ );
Subroutine new_field: called CC=3 SCAR=11.4
Pod documented
288 4 ▶ $self->_load_class( 'HTTP::Promise::Body::Form::Field' ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_load_class("HTTP::Promise::Body::Form::Field") or return $self->pass_error 0 4 4
289 4 ▶ my $f = HTTP::Promise::Body::Form::Field->new( @_ ) ||
Condition50%1/2
'HTTP::Promise::Body::Form::Field'->new(@_) || (return $self->pass_error('HTTP::Promise::Body::Form::Field'->error))
l!l
40
MC/DC0%0/1
'HTTP::Promise::Body::Form::Field'->new(@_) || (return $self->pass_error('HTTP::Promise::Body::Form::Field'->error))
'HTTP::Promise::Body::Form::Field'->new(@_)
Truth table50%1/2
A
A= 'HTTP::Promise::Body::Form::Field'->new(@_)
Aresult
11
00
290 return( $self->pass_error( HTTP::Promise::Body::Form::Field->error ) );
291 4 return( $f );
292 }
293  
294 sub open
295 {
296 0 ▶ my $self = shift( @_ );
Subroutine open: not called CC=3 SCAR=24.8
Pod documented
297 0 my $s = $self->as_string;
298 0 ▶ return( $self->pass_error ) if( !defined( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 0 0 0
299 0 ▶ my $io = $s->open( @_ ) ||
Condition0%0/2
$s->open(@_) || (return $self->pass_error($s->error))
l!l
00
MC/DC0%0/1
$s->open(@_) || (return $self->pass_error($s->error))
$s->open(@_)
Truth table0%0/2
A
A= $s->open(@_)
Aresult
11
00
300 return( $self->pass_error( $s->error ) );
301 0 return( $io );
302 }
303  
304 # sub order { return( shift->_set_get_array_as_object( 'order', @_ ) ); }
305 4 ▶sub order { return( shift->_internal( 'order', '_set_get_array_as_object', @_ ) ); }
Subroutine order: called CC=1 SCAR=0.0
Pod documented
306 # sub order
307 # {
308 # my $self = shift( @_ );
309 # $self->_tie_object->enable(0);
310 # my $rv = $self->SUPER::_set_get_array_as_object( 'order', @_ );
311 # $self->_tie_object->enable(1);
312 # return( $rv );
313 # }
314  
315 sub print
316 {
317 0 ▶ my( $self, $fh ) = @_;
Subroutine print: not called CC=5 SCAR=34.0
Pod documented
318 0 my $nread;
319 # Get output filehandle, and ensure that it's a printable object:
320 0 ▶ $fh ||= select;
Condition0%0/3
$fh ||= select
l!l&&r!l&&!r
000
MC/DC0%0/2
$fh ||= select
$fh select
Truth table0%0/3
A ||= B
A= $fhB= select
ABresult
1X1
011
000
321 0 ▶ return( $self->error( "Filehandle provided ($fh) is not a valid filehandle." ) ) if( !$self->_is_glob( $fh ) );
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_is_glob($fh) 0 0 0
322 0 my $encoded = $self->as_string;
323 0 ▶ return( $self->pass_error ) if( !defined( $encoded ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $encoded 0 0 0
324 0 ▶ CORE::print( $fh $$encoded ) || return( $self->error( "Unable to print on given filehandle '$fh': $!" ) );
Branch: 1 branch
BranchTrueFalseTotal
print $fh $$encoded or return $self->error("Unable to print on given filehandle '${fh}': $!") 0 0 0
325 0 return(1);
326 }
327  
328 1 ▶sub size { return( shift->Module::Generic::Hash::length ); }
Subroutine size: called CC=1 SCAR=0.0
Pod documented
329  
330 0 ▶sub _is_warnings_enabled { return( warnings::enabled( 'HTTP::Promise' ) ); }
Subroutine _is_warnings_enabled: not called CC=1 SCAR=6.9
331  
332 # NOTE: FREEZE is inherited
333  
334 # NOTE: STORABLE_freeze is inherited
335  
336 # NOTE: STORABLE_thaw is inherited
337  
338 # NOTE: THAW is inherited
339  
340 3 1;
341 # NOTE: POD
342 __END__
343  
344 =encoding utf-8
345  
346 =head1 NAME
347  
348 HTTP::Promise::Body::Form::Data - A multipart/form-data Representation Class
349  
350 =head1 SYNOPSIS
351  
352 use HTTP::Promise::Body::Form;
353 my $form = HTTP::Promise::Body::Form::Data->new;
354 my $form = HTTP::Promise::Body::Form::Data->new({
355 fullname => 'Jigoro Kano',
356 location => HTTP::Promise::Body::Form::Data->new_field(
357 name => 'location',
358 value => 'Tokyo',
359 ),
360 picture => HTTP::Promise::Body::Form::Data->new_field(
361 name => 'picture',
362 file => '/some/where/file.txt',
363 ),
364 });
365 my $form = HTTP::Promise::Body::Form::Data->new( $hash_ref );
366 my $form = HTTP::Promise::Body::Form::Data->new( q{e%3Dmc2} );
367 die( HTTP::Promise::Body::Form->error, "\n" ) if( !defined( $form ) );
368  
369 =head1 VERSION
370  
371 v0.2.1
372  
373 =head1 DESCRIPTION
374  
375 This class represents a C<form-data> content as key-value pairs and is designed to make construction and manipulation of C<multipart/form-data> easier. It inherits from L<HTTP::Promise::Body::Form>
376  
377 For C<x-www-form-urlencoded>, use L<HTTP::Promise::Body::Form> instead.
378  
379 Each key represents a C<form-data> field and its value can either be a simple string or a C<HTTP::Promise::Body::Form::Field> object.
380  
381 C<multipart/form-data> is the only valid Content-Type for sending multiple data. L<rfc7578 in section 4.3|https://tools.ietf.org/html/rfc7578#section-4.3> states: "[RFC2388] suggested that multiple files for a single form field be transmitted using a nested "multipart/mixed" part. This usage is deprecated."
382  
383 See also this L<Stackoverflow discussion|https://stackoverflow.com/questions/36674161/http-multipart-form-data-multiple-files-in-one-input/41204533#41204533> and L<this one too|https://stackoverflow.com/questions/51575746/http-header-content-type-multipart-mixed-causes-400-bad-request>
384  
385 =head1 CONSTRUCTOR
386  
387 =head2 new
388  
389 This takes an optional data, and some options and returns a new L<HTTP::Promise::Body::Form> object.
390  
391 Acceptable data are:
392  
393 =over 4
394  
395 =item An hash reference
396  
397 =item An url encoded string
398  
399 =back
400  
401 If a string is provided, it will be automatically decoded into an hash of name-value pairs. When a name is found more than once, its values are added as an array reference.
402  
403 my $form = HTTP::Promise::Body->new( 'name=John+Doe&foo=bar&foo=baz&foo=' );
404  
405 Would result in a C<HTTP::Promise::Body::Form> object containing:
406  
407 name => 'John Doe', foo => ['bar', 'baz', '']
408  
409 =head1 METHODS
410  
411 L<HTTP::Promise::Body::Form> inherits all the methods from L<Module::Generic::Hash>, and adds or override the following ones.
412  
413 =head2 as_string
414  
415 Provided with an hash or hash reference of options and this returns a L<scalar object|Module::Generic::Scalar> of the C<form-data> properly formatted as multipart elements.
416  
417 Be mindful of the size of the parts and that this is not cached, so each time this is called, it creates the parts.
418  
419 Supported options are:
420  
421 =over 4
422  
423 =item * C<boundary>
424  
425 A string used as a part delimiter. Note, however, that even if you provide this value, it will not replace the C<boundary> value of a C<HTTP::Promise::Body::Form::Field> C<Content-Disposition> field if it is set.
426  
427 If this is not provided, it will use the value set with L</boundary>, or in the last resort, a new one will be automatically generated using L<Data::UUID/create_str>
428  
429 =item * C<eol>
430  
431 The end-of-line terminator. This defaults to C<\015\012>
432  
433 =item * C<fields>
434  
435 An array reference of form field names. This is used to set the order of appearance.
436  
437 If not provided, it will default to alphabetic order.
438  
439 =back
440  
441 =head2 as_urlencoded
442  
443 This returns a new L<HTTP::Promise::Body::Form> object based on the current data, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
444  
445 =head2 boundary
446  
447 Sets or gets a boundary delimiter, used to separate parts in a C<multiparts/form-data>
448  
449 =head2 length
450  
451 Returns the size in bytes of this multipart form data. This is used to compute the total body size of the HTTP request.
452  
453 This is not to be confused with L</size>, which is used to get the number of parts in this object.
454  
455 =head2 make_parts
456  
457 Provided with an hash or hash reference of options and this returns an L<array object|Module::Generic::Array> of L<parts|HTTP::Promise::Entity>
458  
459 Note that at this point, the body is not encoded and the C<Content-Length> is not added. You can use L<HTTP::Promise::Entity/encode_body> on each part to encode a form part value.
460  
461 Supported options are:
462  
463 =over 4
464  
465 =item * C<boundary>
466  
467 A string used as a part delimiter. Note, however, that even if you provide this value, it will not replace the C<boundary> value of a C<HTTP::Promise::Body::Form::Field> C<Content-Disposition> field if it is set.
468  
469 If this is not provided, a new one will be automatically generated using L<Data::UUID/create_str>
470  
471 =item * C<fields>
472  
473 An array reference of form field names. This is used to set the order of appearance.
474  
475 If not provided, it will default to alphabetic order.
476  
477 =back
478  
479 =head2 new_field
480  
481 This takes an hash or hash reference of options and returns the new C<HTTP::Promise::Body::Form::Data> object, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>.
482  
483 Supported options are:
484  
485 =over 4
486  
487 =item * C<headers>
488  
489 This is optional. Either as L<HTTP::Promise::Headers> object or as an array reference.
490  
491 =item * C<name>
492  
493 Field name
494  
495 =item * C<value>
496  
497 Field value as a string, scalar reference or a L<file object|Module::Generic::File>
498  
499 =back
500  
501 =head2 open
502  
503 This transform all the C<form-data> elements into a proper C<multipart/form-data> using L</as_string> and returns a new L<Module::Generic::Scalar::IO> object.
504  
505 It then opens the scalar passing L<Module::Generic::Scalar/open> whatever arguments were provided and returns an L<Module::Generic::Scalar::IO> object.
506  
507 =head2 order
508  
509 Sets or gets an L<array object|Module::Generic::Array> of form fields in the desired order of appearance when stringified.
510  
511 =head2 print
512  
513 Provided with a valid filehandle, and this print the C<form-data> representation of the form fields and their values, to the given filehandle, or upon error, sets an L<error|Module::Generic/error> and returns C<undef>
514  
515 =head2 size
516  
517 Returns the number of parts in this object.
518  
519 This is not to be confused with L</length>, which is used to get the size in bytes of the multipart form-data.
520  
521 =head1 THREAD-SAFETY
522  
523 L<HTTP::Promise::Body::Form::Data> is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
524  
525 =head1 AUTHOR
526  
527 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
528  
529 =head1 SEE ALSO
530  
531 L<HTTP::Promise>, L<HTTP::Promise::Request>, L<HTTP::Promise::Response>, L<HTTP::Promise::Message>, L<HTTP::Promise::Entity>, L<HTTP::Promise::Headers>, L<HTTP::Promise::Body>, L<HTTP::Promise::Body::Form>, L<HTTP::Promise::Body::Form::Data>, L<HTTP::Promise::Body::Form::Field>, L<HTTP::Promise::Status>, L<HTTP::Promise::MIME>, L<HTTP::Promise::Parser>, L<HTTP::Promise::IO>, L<HTTP::Promise::Stream>, L<HTTP::Promise::Exception>
532  
533 L<Module::Generic::Scalar>
534  
535 =head1 COPYRIGHT & LICENSE
536  
537 Copyright(c) 2022 DEGUEST Pte. Ltd.
538  
539 All rights reserved.
540  
541 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
542  
543 =cut
« blib/lib/HTTP/Promise/Stream/QuotedPrint.pm ↑ Summary blib/lib/HTTP/Promise/Headers/Range.pm »