File Coverage

lib/HTTP/Promise/Headers/ContentType.pm
Criterion Covered Total %
statement 42 46 91.3
branch 7 10 70.0
condition 7 14 50.0
subroutine 12 16 75.0
pod 9 9 100.0
total 77 95 81.0


line stmt bran cond sub pod time code
1             ##----------------------------------------------------------------------------
2             ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Headers/ContentType.pm
3             ## Version v0.1.0
4             ## Copyright(c) 2022 DEGUEST Pte. Ltd.
5             ## Author: Jacques Deguest <jack@deguest.jp>
6             ## Created 2022/05/06
7             ## Modified 2022/05/06
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::ContentType;
15             BEGIN
16             {
17 10     10   3717 use strict;
  10         110  
  10         460  
18 10     10   70 use warnings;
  10         26  
  10         511  
19 10     10   85 use warnings::register;
  10         30  
  10         2759  
20 10     10   81 use parent qw( HTTP::Promise::Headers::Generic );
  10         27  
  10         166  
21 10     10   1022 our $VERSION = 'v0.1.0';
22             };
23              
24 10     10   62 use strict;
  10         34  
  10         247  
25 10     10   53 use warnings;
  10         50  
  10         4616  
26              
27             sub init
28             {
29 44     44 1 129356 my $self = shift( @_ );
30 44 50 66     516 @_ = () if( @_ == 1 && $self->_is_a( $_[0] => 'Module::Generic::Null' ) );
31 44 100       2108 if( @_ )
32             {
33 42         109 my $str = shift( @_ );
34 42 50 33     309 return( $self->error( "No value was provided for Content-Type field." ) ) if( !defined( $str ) || !length( "$str" ) );
35 42         447 my $params = $self->_get_args_as_hash( @_ );
36 42   50     487 my $hv = $self->_parse_header_value( $str ) ||
37             return( $self->pass_error );
38 42         447 $hv->param( $_ => $params->{ $_ } ) for( keys( %$params ) );
39 42         274 $self->_hv( $hv );
40             }
41 44 50       2821 $self->SUPER::init( @_ ) || return( $self->pass_error );
42 44         350 $self->_field_name( 'Content-Type' );
43 44         35797 return( $self );
44             }
45              
46 28     28 1 300485 sub as_string { return( shift->_hv_as_string( @_ ) ); }
47              
48 46     46 1 42872 sub boundary { return( shift->_set_get_param( boundary => @_ ) ); }
49              
50 25     25 1 75672 sub charset { return( shift->_set_get_param( charset => @_ ) ); }
51              
52 0     0 1 0 sub make_boundary { return( shift->_make_boundary ); }
53              
54 0     0 1 0 sub param { return( shift->_set_get_param( @_ ) ); }
55              
56 0     0 1 0 sub params { return( shift->_set_get_params( @_ ) ); }
57              
58             sub type
59             {
60 33     33 1 1387571 my $self = shift( @_ );
61 33 100       117 if( @_ )
62             {
63 1   50     6 my $mime = shift( @_ ) || return( $self->error( "No mime type was provided." ) );
64 1   50     22 my $hv = $self->_new_hv( $mime ) || return( $self->pass_error );
65 1         238 $self->_hv( $hv );
66 1         50 return( $mime );
67             }
68             else
69             {
70             # No header value object, means there is just nothing set yet
71 32   50     119 my $hv = $self->_hv || return( '' );
72 32         1255 return( $hv->value_data );
73             }
74             }
75              
76             # Basically same thing as type()
77 0     0 1   sub value { return( shift->_set_get_value( @_ ) ); }
78              
79             1;
80             # NOTE: POD
81             __END__
82              
83             =encoding utf-8
84              
85             =head1 NAME
86              
87             HTTP::Promise::Headers::ContentType - Content-Type Header Field
88              
89             =head1 SYNOPSIS
90              
91             use HTTP::Promise::Headers::ContentType;
92             my $ct = HTTP::Promise::Headers::ContentType->new ||
93             die( HTTP::Promise::Headers::ContentType->error, "\n" );
94             $ct->value( 'text/plain' );
95              
96             =head1 VERSION
97              
98             v0.1.0
99              
100             =head1 DESCRIPTION
101              
102             The following description is taken from Mozilla documentation.
103              
104             Content-Type: text/html; charset=UTF-8
105             Content-Type: application/octet-stream
106             Content-Type: multipart/form-data; boundary=something
107             Content-Type: application/x-www-form-urlencoded
108             # Used with 206 Partial Content; rfc7233, section 5.4.1
109             Content-Type: multipart/byteranges
110              
111             =head1 METHODS
112              
113             =head2 as_string
114              
115             Returns a string representation of the C<Content-Type> object.
116              
117             =head2 boundary
118              
119             Sets or gets the boundary used for C<multipart/form-data>.
120              
121             If the value is C<undef>, it will be removed.
122              
123             =head2 charset
124              
125             Sets or gets the charset associated with this C<Content-Type>
126              
127             =head2 make_boundary
128              
129             Returns a unique auto-generated boundary. Such auto-generated boundary is actually an uuid.
130              
131             =head2 param
132              
133             Set or get an arbitrary name-value pair attribute.
134              
135             =head2 params
136              
137             Set or get multiple name-value parameters.
138              
139             Calling this without any parameters, retrieves the associated L<hash object|Module::Generic::Hash>
140              
141             =head2 type
142              
143             Sets or gets the mime-type for this field.
144              
145             =head2 value
146              
147             Sets or gets the mime-type for this C<Content-Type>. This is effectively the same as L</type>
148              
149             =head1 AUTHOR
150              
151             Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
152              
153             =head1 SEE ALSO
154              
155             See also L<rfc7233, section 4.1|https://tools.ietf.org/html/rfc7233#section-4.1>, L<rfc7231, section 3.1.1.5|https://tools.ietf.org/html/rfc7231#section-3.1.1.5> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/MIME_types>, and L<this Mozilla documentation too|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Type>
156              
157             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>
158              
159             L<HTTP::Promise>, L<HTTP::Promise::Headers>
160              
161             =head1 COPYRIGHT & LICENSE
162              
163             Copyright(c) 2022 DEGUEST Pte. Ltd.
164              
165             All rights reserved.
166              
167             This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
168              
169             =cut