blib/lib/HTTP/Promise/Headers/ContentRange.pm untested

filter: Statementstmt 0.0% 0 / 68 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 31 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 9 totaltotal 0.0% 0 / 146 CC 13 SCAR 30.9
CC
13
Cov
0%
init
56.0
as_string
20.0
_set_get_number_or_wildcard
20.0
CRAP 22.1
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Headers/ContentRange.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::ContentRange;
15 BEGIN
16 {
17 use strict;
18 use warnings;
19 warnings::register_categories( 'HTTP::Promise' );
20 use parent qw( HTTP::Promise::Headers::Generic );
21 our $VERSION = 'v0.1.0';
22 };
23  
24 use strict;
25 use warnings;
26  
27 sub init
28 {
29 my $self = shift( @_ );
30 $self->{range_end} = undef;
31 $self->{range_start} = undef;
32 $self->{size} = undef;
33 $self->{unit} = undef;
34 @_ = () if( @_ == 1 && $self->_is_a( $_[0] => 'Module::Generic::Null' ) );
35 if( @_ )
36 {
37 my $str = shift( @_ );
38 if( $str =~ m,^(?<unit>\S+)[[:blank:]]+(?:(?:(?<start>\d+)-(?<end>\d+))|\*)/(?<size>\d+|\*), )
39 {
40 $self->unit( $+{unit} );
41 $self->range_start( $+{start} ) if( defined( $+{start} ) && length( $+{start} ) );
42 $self->range_end( $+{end} ) if( defined( $+{end} ) && length( $+{end} ) );
43 $self->size( $+{size} ) if( defined( $+{size} ) && length( $+{size} ) );
44 }
45 else
46 {
47 return( $self->error( "Unsupported Content-Range value format '$str'." ) );
48 }
49 }
50 $self->{_init_strict_use_sub} = 1;
51 $self->SUPER::init( @_ ) || return( $self->pass_error );
52 $self->_field_name( 'Content-Range' );
53 return( $self );
54 }
55  
56 sub as_string
57 {
58 my $self = shift( @_ );
59 my $unit = $self->unit || 'bytes';
60 my $start = $self->range_start;
61 my $end = $self->range_end;
62 my $size = $self->size;
63 my @parts = ();
64 if( defined( $start ) || defined( $end ) )
65 {
66 if( $start eq '*' && $end eq '*' )
67 {
68 push( @parts, '*' );
69 }
70 else
71 {
72 $start //= '*';
73 $end //= '*';
74 push( @parts, "${start}-${end}" );
75 }
76 }
77 else
78 {
79 push( @parts, '*' );
80 }
81 push( @parts, defined( $size ) ? $size : '*' );
82 return( join( ' ', $unit, join( '/', @parts ) ) );
83 }
84  
85 sub range_end { return( shift->_set_get_scalar_as_object( 'range_end', @_ ) ); }
86  
87 sub range_start { return( shift->_set_get_scalar_as_object( 'range_start', @_ ) ); }
88  
89 sub size { return( shift->_set_get_scalar_as_object( 'size', @_ ) ); }
90  
91 sub start_end
92 {
93 my $self = shift( @_ );
94 my( $start, $end ) = @_;
95 $self->range_start( $start );
96 $self->range_end( $end // $start );
97 return( $self );
98 }
99  
100 sub unit { return( shift->_set_get_scalar_as_object( 'unit', @_ ) ); }
101  
102 sub _set_get_number_or_wildcard
103 {
104 my $self = shift( @_ );
105 my $prop = shift( @_ ) || return( $self->error( "No property name was provided." ) );
106 if( @_ )
107 {
108 my $v = shift( @_ );
109 if( defined( $v ) )
110 {
111 return( $self->error( "Value provided for property \"${prop}\" is invalid. I was expecting either an integer or a wildcard." ) ) if( $v !~ /^\d+|\*$/ );
112 }
113 return( $self->_set_get_scalar_as_object( $prop => $v ) );
114 }
115 return( $self->_set_get_scalar_as_object( $prop ) );
116 }
117  
118 1;
119 # NOTE: POD
120 __END__
121  
122 =encoding utf-8
123  
124 =head1 NAME
125  
126 HTTP::Promise::Headers::ContentRange - Content-Range Header Field
127  
128 =head1 SYNOPSIS
129  
130 use HTTP::Promise::Headers::ContentRange;
131 my $range = HTTP::Promise::Headers::ContentRange->new ||
132 die( HTTP::Promise::Headers::ContentRange->error, "\n" );
133 $range->unit( 'bytes' ):
134 $range->range_start(500);
135 $range->range_end(1000);
136 $range->size(2048);
137 say $range->as_string;
138 # or
139 say "$range";
140  
141 # 416 Range Not Satisfiable
142 # <https://tools.ietf.org/html/rfc7233#section-4.4>
143 $range->start_end( undef );
144 $range->size(2048);
145 say "$range";
146 # bytes */2048
147  
148 =head1 VERSION
149  
150 v0.1.0
151  
152 =head1 DESCRIPTION
153  
154 The following description is taken from Mozilla documentation.
155  
156 Content-Range: bytes 200-1000/67589
157 # When the complete range is unknown:
158 Content-Range: bytes 42-1233/*
159  
160 # The first 500 bytes:
161 Content-Range: bytes 0-499/1234
162 # The second 500 bytes:
163 Content-Range: bytes 500-999/1234
164 # All except for the first 500 bytes:
165 Content-Range: bytes 500-1233/1234
166 # The last 500 bytes:
167 Content-Range: bytes 734-1233/1234
168 # Unsatisfiable range value
169 Content-Range: bytes */1234
170  
171 =head1 METHODS
172  
173 =head2 as_string
174  
175 Returns a string representation of the object.
176  
177 If both C<range-start> and C<range-end> properties are C<undef>, they will be replaced by a C<*> (wildcard)
178  
179 If C<size> property is C<undef> it will be replaced by a C<*> (wildcard)
180  
181 =head2 range_end
182  
183 An integer in the given unit indicating the end position (zero-indexed & inclusive) of the requested range.
184  
185 =head2 range_start
186  
187 An integer in the given unit indicating the start position (zero-indexed & inclusive) of the request range.
188  
189 =head2 size
190  
191 The total length of the document (or '*' if unknown).
192  
193 =head2 start_end
194  
195 This is a convenience method to set both the C<range-start> and C<range-end> property.
196  
197 =head2 unit
198  
199 The unit in which ranges are specified. This is usually C<bytes>.
200  
201 =head1 THREAD-SAFETY
202  
203 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
204  
205 =head1 AUTHOR
206  
207 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
208  
209 =head1 SEE ALSO
210  
211 C<Accept-Range>, C<If-Range>, C<Range>
212  
213 See L<rfc7233, section 4.2|https://tools.ietf.org/html/rfc7233#section-4.2> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Content-Range>
214  
215 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>
216  
217 =head1 COPYRIGHT & LICENSE
218  
219 Copyright(c) 2022 DEGUEST Pte. Ltd.
220  
221 All rights reserved.
222  
223 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
224  
225 =cut