lib/HTTP/Promise/Headers/ContentDisposition.pm

filter: Statementstmt 94.9% 75 / 79 Branchbran 79.4% 27 / 34 Conditioncond 56.5% 13 / 23 MC/DCmcdc 28.5% 4 / 14 Subroutinesub 86.6% 13 / 15 Podpod 100% 9 / 9 totaltotal 81.0% 141 / 174 CC 27 SCAR 18.3
CC
27
Cov
85%
init
9.5
filename
9.3
filename_charset
6.2
CRAP 6.3
« blib/lib/HTTP/Promise/Headers/StrictTransportSecurity.pm ↑ Summary blib/lib/HTTP/Promise/Body.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Headers/ContentDisposition.pm
3 ## Version v0.1.0
4 ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2022/05/07
7 ## Modified 2022/05/07
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::Headers::ContentDisposition;
15 BEGIN
16 {
17 6 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 6 ▶ warnings::register_categories( 'HTTP::Promise' );
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 ▶ use parent qw( HTTP::Promise::Headers::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 6 our $VERSION = 'v0.1.0';
22 };
23  
24 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
26  
27 sub init
28 {
29 42 ▶ my $self = shift( @_ );
Subroutine init: called CC=9 SCAR=22.5
Pod documented
30 42 $self->{filename_charset} = undef;
31 42 $self->{filename_lang} = undef;
32 42 ▶ @_ = () if( @_ == 1 && $self->_is_a( $_[0] => 'Module::Generic::Null' ) );
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and $self->_is_a($_[0], "Module::Generic::Null") 4 38 42
Condition100%3/3
@_ == 1 and $self->_is_a($_[0], "Module::Generic::Null")
!ll&&!rl&&r
5334
MC/DC100%2/2
@_ == 1 and $self->_is_a($_[0], "Module::Generic::Null")
@_ == 1 $self->_is_a($_[0], "Module::Generic::Null")
Truth table100%3/3
A and B
A= @_ == 1B= $self->_is_a($_[0], "Module::Generic::Null")
ABresult
0X0
100
111
33 42 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 33 9 42
34 {
35 33 my $str = shift( @_ );
36 33 ▶ return( $self->error( "No value was provided for Content-Disposition field." ) ) if( !defined( $str ) || !length( "$str" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and length "$str" 0 33 33
Condition33%1/3
defined $str and length "$str"
!ll&&!rl&&r
0033
MC/DC0%0/2
defined $str and length "$str"
defined $str length "$str"
Truth table33%1/3
A and B
A= defined $strB= length "$str"
ABresult
0X0
100
111
37 33 my $params = $self->_get_args_as_hash( @_ );
38 33 ▶ my $hv = $self->_parse_header_value( $str ) ||
Condition50%1/2
$self->_parse_header_value($str) || (return $self->pass_error)
l!l
330
MC/DC0%0/1
$self->_parse_header_value($str) || (return $self->pass_error)
$self->_parse_header_value($str)
Truth table50%1/2
A
A= $self->_parse_header_value($str)
Aresult
11
00
39 return( $self->pass_error );
40 33 ▶ $hv->params( $params ) if( scalar( keys( %$params ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$params 0 33 33
41 33 $self->_hv( $hv );
42 }
43 42 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 42 42
44 42 $self->_field_name( 'Content-Disposition' );
45 42 return( $self );
46 }
47  
48 13 ▶sub as_string { return( shift->_hv_as_string( [qw( name filename )], { quote => 'name' } ) ); }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
49  
50 sub disposition
51 {
52 17 ▶ my $self = shift( @_ );
Subroutine disposition: called CC=6 SCAR=18.1
Pod documented
53 17 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 8 9 17
54 {
55 8 ▶ my $dispo = shift( @_ ) || return( $self->error( "No content disposition was provided." ) );
Condition50%1/2
shift @_ || (return $self->error('No content disposition was provided.'))
l!l
80
MC/DC0%0/1
shift @_ || (return $self->error('No content disposition was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
56 8 my $hv = $self->_hv;
57 8 ▶ if( $hv )
Branch: 1 branch
BranchTrueFalseTotal
if ($hv) { } 4 4 8
58 {
59 4 $hv->value( $dispo );
60 }
61 else
62 {
63 4 ▶ $hv = $self->_new_hv( $dispo ) || return( $self->pass_error );
Condition50%1/2
$self->_new_hv($dispo) || (return $self->pass_error)
l!l
40
MC/DC0%0/1
$self->_new_hv($dispo) || (return $self->pass_error)
$self->_new_hv($dispo)
Truth table50%1/2
A
A= $self->_new_hv($dispo)
Aresult
11
00
64 4 $self->_hv( $hv );
65 }
66 8 return( $dispo );
67 }
68 else
69 {
70 # No header value object, means there is just nothing set yet
71 9 ▶ my $hv = $self->_hv || return( '' );
Condition50%1/2
$self->_hv || (return '')
l!l
90
MC/DC0%0/1
$self->_hv || (return '')
$self->_hv
Truth table50%1/2
A
A= $self->_hv
Aresult
11
00
72 9 return( $hv->value_data );
73 }
74 }
75  
76 sub filename
77 {
78 14 ▶ my $self = shift( @_ );
Subroutine filename: called CC=9 SCAR=22.3
Pod documented
79 14 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 4 10 14
80 {
81 4 my( $fname, $lang ) = @_;
82 4 ▶ if( !defined( $fname ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $fname) { } 0 4 4
83 {
84 0 $self->params->delete( 'filename' );
85 0 $self->params->delete( 'filename*' );
86 }
87 else
88 {
89 4 ▶ $lang //= $self->filename_lang;
Condition66%2/3
$lang //= $self->filename_lang
l!l&&r!l&&!r
103
MC/DC50%1/2
$lang //= $self->filename_lang
$lang $self->filename_lang
Truth table66%2/3
A //= B
A= $langB= $self->filename_lang
ABresult
1X1
011
000
90 4 ▶ if( my $enc = $self->_filename_encode( $fname, $lang ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $enc = $self->_filename_encode($fname, $lang)) { } 1 3 4
91 {
92 1 ▶ $self->_set_get_param( 'filename*' => $enc ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_set_get_param("filename*", $enc) or return $self->pass_error 0 1 1
93 }
94 else
95 {
96 3 $self->_set_get_qparam( filename => $fname );
97 }
98 }
99 }
100 else
101 {
102 10 my $v = $self->_set_get_qparam( 'filename' );
103 10 ▶ if( defined( $v ) && length( $v ) )
Branch: 2 branches
BranchTrueFalseTotal
if (defined $v and length $v) { } 6 4 10
elsif ($v = $self->_set_get_param('filename*')) { } 1 3 4
Condition66%2/3
defined $v and length $v
!ll&&!rl&&r
046
MC/DC50%1/2
defined $v and length $v
defined $v length $v
Truth table66%2/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
104 {
105 # decode if necessary
106 6 my( $f_charset, $f_lang );
107 6 ( $v, $f_charset, $f_lang ) = $self->_filename_decode( $v );
108 6 $self->filename_charset( $f_charset );
109 6 $self->filename_lang( $f_lang );
110 }
111 elsif( $v = $self->_set_get_param( 'filename*' ) )
112 {
113 1 my( $f_charset, $f_lang );
114 1 ( $v, $f_charset, $f_lang ) = $self->_filename_decode( $v );
115 1 $self->filename_charset( $f_charset );
116 1 $self->filename_lang( $f_lang );
117 }
118 10 return( $v );
119 }
120 }
121  
122 sub filename_charset
123 {
124 8 ▶ my $self = shift( @_ );
Subroutine filename_charset: called CC=6 SCAR=18.3
Pod documented
125 8 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 7 1 8
126 {
127 7 my $v = shift( @_ );
128 7 ▶ if( !defined( $v ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v) 6 1 7
129 {
130 6 return( $self->_set_get_scalar_as_object( 'filename_charset', $v ) );
131 }
132 1 ▶ return( $self->error( "Only supported charset is 'utf-8'." ) ) if( lc( $v ) ne 'utf-8' && lc( $v ) ne 'utf8' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $v ne "utf-8" and lc $v ne "utf8" 0 1 1
Condition33%1/3
lc $v ne "utf-8" and lc $v ne "utf8"
!ll&&!rl&&r
100
MC/DC0%0/2
lc $v ne "utf-8" and lc $v ne "utf8"
lc $v ne "utf-8" lc $v ne "utf8"
Truth table33%1/3
A and B
A= lc $v ne "utf-8"B= lc $v ne "utf8"
ABresult
0X0
100
111
133 # Convenience
134 1 ▶ $v = 'utf-8' if( lc( $v ) eq 'utf8' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $v eq "utf8" 0 1 1
135 1 $v = uc( $v );
136 1 return( $self->_set_get_scalar_as_object( 'filename_charset', $v ) );
137 }
138 1 return( $self->_set_get_scalar_as_object( 'filename_charset' ) );
139 }
140  
141 11 ▶sub filename_lang { return( shift->_set_get_scalar_as_object( 'filename_lang', @_ ) ); }
Subroutine filename_lang: called CC=1 SCAR=0.0
Pod documented
142  
143 21 ▶sub name { return( shift->_set_get_param( name => @_ ) ); }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
144  
145 0 ▶sub param { return( shift->_set_get_param( @_ ) ); }
Subroutine param: not called CC=1 SCAR=6.9
Pod documented
146  
147 0 ▶sub params { return( shift->_set_get_params( @_ ) ); }
Subroutine params: not called CC=1 SCAR=6.9
Pod documented
148  
149 6 1;
150 # NOTE: POD
151 __END__
152  
153 =encoding utf-8
154  
155 =head1 NAME
156  
157 HTTP::Promise::Headers::ContentDisposition - Content-Disposition Header Field
158  
159 =head1 SYNOPSIS
160  
161 use HTTP::Promise::Headers::ContentDisposition;
162 my $cd = HTTP::Promise::Headers::ContentDisposition->new ||
163 die( HTTP::Promise::Headers::ContentDisposition->error, "\n" );
164 my $dispo = $cd->disposition;
165 # For example, attachment
166 $cd->disposition( 'inline' );
167 $cd->filename( 'some-file.txt' );
168 $cd->disposition( 'form-data' );
169 $cd->name( 'someField' );
170 my $name = $cd->name;
171 # Same thing
172 my $name = $cd->param( 'name' );
173 $cd->params( name => 'someField', filename => 'some-file.txt' );
174  
175 =head1 VERSION
176  
177 v0.1.0
178  
179 =head1 DESCRIPTION
180  
181 The following description is taken from Mozilla documentation.
182  
183 Content-Disposition: inline
184 Content-Disposition: attachment
185 Content-Disposition: attachment; filename="filename.jpg"
186 Content-Disposition: form-data; name="fieldName"
187 Content-Disposition: form-data; name="fieldName"; filename="filename.jpg"
188  
189 =head1 METHODS
190  
191 =head2 as_string
192  
193 Returns a string representation of the Content-Disposition object.
194  
195 =head2 disposition
196  
197 Sets or gets the type of C<Content-Disposition> this is. For example: C<attachment>, C<form-data>
198  
199 =head2 name
200  
201 Is followed by a string containing the name of the HTML field in the form that the content of this subpart refers to. When dealing with multiple files in the same field (for example, the multiple attribute of an <input type="file"> element), there can be several subparts with the same name.
202  
203 A name with a value of '_charset_' indicates that the part is not an HTML field, but the default charset to use for parts without explicit charset information.
204  
205 =head2 filename
206  
207 Without any argument, this returns the string containing the original name of the file transmitted. The C<filename> is always optional and must not be used blindly by the application: path information should be stripped, and conversion to the server file system rules should be done. This parameter provides mostly indicative information. When used in combination with C<Content-Disposition: attachment>, it is used as the default C<filename> for an eventual "Save As" dialog presented to the user.
208  
209 If the property C<filename*> is set instead, then it will be decoded and used instead, and the value for L</filename_charset> and L</filename_lang> will be set.
210  
211 When setting the filename value, this takes an optional language iso 639 code (see L<rfc5987|https://tools.ietf.org/html/rfc5987> and L<rfc2231|https://tools.ietf.org/html/rfc2231>).
212 If the filename contains non ascii characters, it will be automatically encoded according to L<rfc5987|https://tools.ietf.org/html/rfc5987>. and the property C<filename*> set instead. That property, by rfc standard, takes precedence over the C<filename> one.
213  
214 The language provided, if any, will be used then.
215  
216 For example:
217  
218 $h->disposition( 'form-data' );
219 $h->name( 'fileField' );
220 $h->filename( q{file.txt} );
221 say "$h";
222 # form-data; name="fileField"; filename="file.txt"
223  
224 $h->disposition( 'form-data' );
225 $h->name( 'fileField' );
226 $h->filename( q{ファイル.txt} );
227 say "$h";
228 # form-data; name="fileField"; filename*="UTF-8''%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.txt"
229
230 $h->disposition( 'form-data' );
231 $h->name( 'fileField' );
232 $h->filename( q{ファイル.txt}, 'ja-JP' );
233 say "$h";
234 # form-data; name="fileField"; filename*="UTF-8'ja-JP'%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.txt"
235  
236 # Using default value
237 $h->filename_lang( 'ja-JP' );
238 $h->disposition( 'form-data' );
239 $h->name( 'fileField' );
240 $h->filename( q{ファイル.txt} );
241 say "$h";
242 # form-data; name="fileField"; filename*="UTF-8'ja-JP'%E3%83%95%E3%82%A1%E3%82%A4%E3%83%AB.txt"
243  
244 $headers->header( Content_Disposition => "$h" );
245  
246 The C<Content-Disposition> header value would then contain a property C<filename*> (with the trailing wildcard).
247  
248 See also L<HTTP::Promise::Headers/decode_filename> and L<HTTP::Promise::Headers/encode_filename> which are used to decode and encode filenames.
249  
250 =head2 filename_charset
251  
252 Sets or gets the encoded filename charset.
253  
254 This is used when the filename contains non-ascii characters, such as Japanese, Korean, or Cyrillic.
255 Although theoretically one can set any character set, by design this only accepts C<UTF-8> (case insensitive).
256  
257 This is set automatically when calling L</filename>. You actually need to call L</filename> first to have a value set.
258  
259 Returns a L<scalar object|Module::Generic::Scalar> containing the filename charset.
260  
261 =head2 filename_lang
262  
263 Sets or gets the encoded filename language. This takes an iso 639 language code (see L<rfc1766|https://tools.ietf.org/html/rfc1766>).
264  
265 This is set automatically when calling L</filename>. You actually need to call L</filename> first to have a value set.
266  
267 Returns a L<scalar object|Module::Generic::Scalar> containing the filename language.
268  
269 =head2 param
270  
271 Sets or gets an arbitrary C<Content-Disposition> property.
272  
273 Note that if you use this, you bypass other specialised method who do some additional processing, so be mindful.
274  
275 =head2 params
276  
277 Sets or gets multiple arbitrary C<Content-Disposition> properties at once.
278  
279 If called without any arguments, this returns the L<hash object|Module::Generic::Hash> used to store the C<Content-Disposition> properties.
280  
281 =head1 THREAD-SAFETY
282  
283 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
284  
285 =head1 AUTHOR
286  
287 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
288  
289 =head1 SEE ALSO
290  
291 See L<rfc6266, section 4|https://tools.ietf.org/html/rfc6266#section-4>, L<rfc7578, section 4.2|https://tools.ietf.org/html/rfc7578#section-4.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Disposition>
292  
293 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>
294  
295 =head1 COPYRIGHT & LICENSE
296  
297 Copyright(c) 2022 DEGUEST Pte. Ltd.
298  
299 All rights reserved.
300  
301 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
302  
303 =cut
« blib/lib/HTTP/Promise/Headers/StrictTransportSecurity.pm ↑ Summary blib/lib/HTTP/Promise/Body.pm »