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   3538 use strict;
  10         122  
  10         511  
18 10     10   79 use warnings;
  10         25  
  10         628  
19 10     10   70 use warnings::register;
  10         27  
  10         2693  
20 10     10   85 use parent qw( HTTP::Promise::Headers::Generic );
  10         26  
  10         173  
21 10     10   983 our $VERSION = 'v0.1.0';
22             };
23              
24 10     10   65 use strict;
  10         24  
  10         215  
25 10     10   50 use warnings;
  10         22  
  10         4455  
26              
27             sub init
28             {
29 44     44 1 124655 my $self = shift( @_ );
30 44 50 66     515 @_ = () if( @_ == 1 && $self->_is_a( $_[0] => 'Module::Generic::Null' ) );
31 44 100       2148 if( @_ )
32             {
33 42         117 my $str = shift( @_ );
34 42 50 33     334 return( $self->error( "No value was provided for Content-Type field." ) ) if( !defined( $str ) || !length( "$str" ) );
35 42         464 my $params = $self->_get_args_as_hash( @_ );
36 42   50     521 my $hv = $self->_parse_header_value( $str ) ||
37             return( $self->pass_error );
38 42         456 $hv->param( $_ => $params->{ $_ } ) for( keys( %$params ) );
39 42         311 $self->_hv( $hv );
40             }
41 44 50       2636 $self->SUPER::init( @_ ) || return( $self->pass_error );
42 44         280 $self->_field_name( 'Content-Type' );
43 44         36542 return( $self );
44             }
45              
46 28     28 1 302693 sub as_string { return( shift->_hv_as_string( @_ ) ); }
47              
48 46     46 1 41723 sub boundary { return( shift->_set_get_param( boundary => @_ ) ); }
49              
50 25     25 1 75484 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 1362336 my $self = shift( @_ );
61 33 100       129 if( @_ )
62             {
63 1   50     7 my $mime = shift( @_ ) || return( $self->error( "No mime type was provided." ) );
64 1   50     14 my $hv = $self->_new_hv( $mime ) || return( $self->pass_error );
65 1         232 $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     146 my $hv = $self->_hv || return( '' );
72 32         1233 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