Mail/Make/Headers/ContentDisposition.pm

filter: Statementstmt 94.5% 86 / 91 Branchbran 68.4% 26 / 38 Conditioncond 52.3% 11 / 21 MC/DCmcdc 23.0% 3 / 13 Subroutinesub 100% 15 / 15 Podpod 100% 7 / 7 totaltotal 80.0% 148 / 185 CC 28 SCAR 20.1
CC
28
Cov
82%
filename
14.6
disposition
8.2
filename_charset
6.8
CRAP 7.5
« MM/Table.pm ↑ Summary Mail/Make/Body/File.pm »
1 ##----------------------------------------------------------------------------
2 ## MIME Email Builder - ~/lib/Mail/Make/Headers/ContentDisposition.pm
3 ## Version v0.1.1
4 ## Copyright(c) 2026 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2026/03/02
7 ## Modified 2026/05/11
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 Mail::Make::Headers::ContentDisposition;
15 BEGIN
16 10 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
17 10 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 10 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19 10 warnings::register_categories( 'Mail::Make' );
20 10 ▶ use parent qw( Mail::Make::Headers::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
21 10 ▶ use vars qw( $VERSION $EXCEPTION_CLASS $VALID_DISPOSITIONS );
Subroutine BEGIN: called CC=1 SCAR=0.0
22 10 ▶ use Mail::Make::Exception;
Subroutine BEGIN: called CC=1 SCAR=0.0
23 10 our $EXCEPTION_CLASS = 'Mail::Make::Exception';
24 10 our $VALID_DISPOSITIONS = qr/^(?:inline|attachment|form-data)$/i;
25 10 our $VERSION = 'v0.1.1';
26 };
27  
28 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 10 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
30  
31 sub init
32 62 ▶{
Subroutine init: called CC=6 SCAR=18.0
Pod documented
33 62 my $self = shift( @_ );
34 62 $self->{filename_charset} = undef;
35 62 $self->{filename_lang} = undef;
36 62 $self->{_exception_class} = $EXCEPTION_CLASS;
37 62 my $disposition = shift( @_ );
38 62 ▶ if( !defined( $disposition ) ||
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $disposition and length "$disposition") 1 61 62
Condition66%2/3
defined $disposition and length "$disposition"
!ll&&!rl&&r
1061
MC/DC50%1/2
defined $disposition and length "$disposition"
defined $disposition length "$disposition"
Truth table66%2/3
A and B
A= defined $dispositionB= length "$disposition"
ABresult
0X0
100
111
39 !length( "$disposition" ) )
40 {
41 1 return( $self->error( "No value was provided for Content-Disposition field." ) );
42 }
43 61 my $params = $self->_get_args_as_hash( @_ );
44 61 my $debug = delete( $params->{debug} );
45 61 $self->debug( $debug );
46 61 ▶ $self->disposition( $disposition, ( scalar( %$params ) ? %$params : () ) ) || return( $self->pass_error );
Branch: 2 branches
BranchTrueFalseTotal
$self->disposition($disposition, scalar %$params ? %$params : ()) or return $self->pass_error 60 1 61
scalar %$params ? : 0 61 61
47 60 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 60 0 60
48 60 $self->_field_name( 'Content-Disposition' );
49 60 return( $self );
50 }
51  
52 53 ▶sub as_string { return( shift->_hv_as_string( [qw( filename )] ) ); }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
53  
54 sub disposition
55 74 ▶{
Subroutine disposition: called CC=8 SCAR=21.0
Pod documented
56 74 my $self = shift( @_ );
57 74 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 67 7 74
58 {
59 67 ▶ 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
670
MC/DC0%0/1
shift @_ || (return $self->error('No content disposition was provided.'))
shift @_
Truth table50%1/2
A
A= shift @_
Aresult
11
00
60 67 ▶ unless( $dispo =~ $VALID_DISPOSITIONS )
Branch: 1 branch
BranchTrueFalseTotal
unless ($dispo =~ /$Mail::Make::Headers::ContentDisposition::VALID_DISPOSITIONS/) 2 65 67
61 {
62 2 return( $self->error( "Invalid disposition '$dispo': must be one of inline, attachment, form-data." ) );
63 }
64 65 my $params = $self->_get_args_as_hash( @_ );
65 65 $dispo = lc( $dispo );
66 65 my $hv = $self->_hv;
67 65 ▶ if( $hv )
Branch: 1 branch
BranchTrueFalseTotal
if ($hv) { } 5 60 65
68 {
69 5 $hv->value( $dispo );
70 }
71 else
72 {
73 60 ▶ $hv = $self->_parse_header_value( $dispo ) || return( $self->pass_error );
Condition50%1/2
$self->_parse_header_value($dispo) || (return $self->pass_error)
l!l
600
MC/DC0%0/1
$self->_parse_header_value($dispo) || (return $self->pass_error)
$self->_parse_header_value($dispo)
Truth table50%1/2
A
A= $self->_parse_header_value($dispo)
Aresult
11
00
74 60 $self->_hv( $hv );
75 }
76 65 ▶ $hv->params( $params ) if( scalar( keys( %$params ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$params 0 65 65
77 65 return( $dispo );
78 }
79 else
80 {
81 7 ▶ my $hv = $self->_hv || return( '' );
Condition50%1/2
$self->_hv || (return '')
l!l
70
MC/DC0%0/1
$self->_hv || (return '')
$self->_hv
Truth table50%1/2
A
A= $self->_hv
Aresult
11
00
82 7 return( $hv->value_data );
83 }
84 }
85  
86 # Sets or gets the filename parameter.
87 # On assignment, applies RFC 2231 encoding automatically when the filename
88 # contains non-ASCII or RFC 2045 special characters (including commas).
89 # This is the fix for the MIME::Entity silent failure bug.
90 sub filename
91 38 ▶{
Subroutine filename: called CC=11 SCAR=26.8
Pod documented
92 38 my $self = shift( @_ );
93 38 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 37 1 38
94 {
95 37 my( $fname, $lang ) = @_;
96 37 ▶ if( !defined( $fname ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fname) 1 36 37
97 {
98 # Remove both the plain and extended forms
99 1 my $hv = $self->_hv;
100 1 ▶ if( $hv )
Branch: 1 branch
BranchTrueFalseTotal
if ($hv) 1 0 1
101 {
102 1 $hv->params->delete( 'filename' );
103 1 $hv->params->delete( 'filename*' );
104 }
105 1 return( $self );
106 }
107 36 ▶ $lang //= $self->filename_lang;
Condition66%2/3
$lang //= $self->filename_lang
l!l&&r!l&&!r
0135
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
108 36 my $encoded = $self->_filename_encode( $fname, $lang );
109 36 ▶ if( defined( $encoded ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $encoded) { } 7 29 36
110 {
111 # Non-trivial filename: use RFC 2231 extended notation (filename*)
112 7 ▶ $self->_set_get_param( 'filename*' => $encoded ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_set_get_param("filename*", $encoded) or return $self->pass_error 7 0 7
113 }
114 else
115 {
116 # Pure safe ASCII: use plain quoted filename
117 29 $self->_set_get_qparam( filename => $fname );
118 }
119 36 return( $fname );
120 }
121 else
122 {
123 # Prefer filename* (RFC 2231) over plain filename
124 1 my $v = $self->_set_get_param( 'filename*' );
125 1 ▶ if( defined( $v ) && length( "$v" ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and length "$v") 0 1 1
Condition33%1/3
defined $v and length "$v"
!ll&&!rl&&r
100
MC/DC0%0/2
defined $v and length "$v"
defined $v length "$v"
Truth table33%1/3
A and B
A= defined $vB= length "$v"
ABresult
0X0
100
111
126 {
127 0 my( $decoded, $charset, $lang ) = $self->_filename_decode( $v );
128 0 ▶ $self->filename_charset( $charset ) if( defined( $charset ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $charset 0 0 0
129 0 ▶ $self->filename_lang( $lang ) if( defined( $lang ) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $lang 0 0 0
130 0 return( $decoded );
131 }
132 1 $v = $self->_set_get_qparam( 'filename' );
133 1 return( $v );
134 }
135 }
136  
137 sub filename_charset
138 2 ▶{
Subroutine filename_charset: called CC=6 SCAR=19.2
Pod documented
139 2 my $self = shift( @_ );
140 2 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 0 2
141 {
142 2 my $v = shift( @_ );
143 2 ▶ if( defined( $v ) && length( $v ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $v and length $v) 2 0 2
Condition33%1/3
defined $v and length $v
!ll&&!rl&&r
002
MC/DC0%0/2
defined $v and length $v
defined $v length $v
Truth table33%1/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
144 {
145 2 ▶ 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") 1 1 2
Condition66%2/3
lc $v ne "utf-8" and lc $v ne "utf8"
!ll&&!rl&&r
101
MC/DC50%1/2
lc $v ne "utf-8" and lc $v ne "utf8"
lc $v ne "utf-8" lc $v ne "utf8"
Truth table66%2/3
A and B
A= lc $v ne "utf-8"B= lc $v ne "utf8"
ABresult
0X0
100
111
146 {
147 1 return( $self->error( "Only 'utf-8' is supported as filename charset." ) );
148 }
149 1 $v = 'UTF-8';
150 }
151 1 return( $self->_set_get_scalar_as_object( 'filename_charset', $v ) );
152 }
153 0 return( $self->_set_get_scalar_as_object( 'filename_charset' ) );
154 }
155  
156 37 ▶sub filename_lang { return( shift->_set_get_scalar_as_object( 'filename_lang', @_ ) ); }
Subroutine filename_lang: called CC=1 SCAR=0.0
Pod documented
157  
158 2 ▶sub name { return( shift->_set_get_param( name => @_ ) ); }
Subroutine name: called CC=1 SCAR=0.0
Pod documented
159  
160 10 1;
161 # NOTE: POD
162 __END__
« MM/Table.pm ↑ Summary Mail/Make/Body/File.pm »