| 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 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 3 | ▶ | warnings::register_categories( 'HTTP::Promise' ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | 3 | ▶ | use parent qw( HTTP::Promise::Headers::Generic ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 21 | 3 | our $VERSION = 'v0.1.0'; | |||||||||||||||||||||||||||
| 22 | }; | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 25 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | sub init | ||||||||||||||||||||||||||||
| 28 | { | ||||||||||||||||||||||||||||
| 29 | 6 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=12 SCAR=26.2
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 6 | $self->{range_end} = undef; | |||||||||||||||||||||||||||
| 31 | 6 | $self->{range_start} = undef; | |||||||||||||||||||||||||||
| 32 | 6 | $self->{size} = undef; | |||||||||||||||||||||||||||
| 33 | 6 | $self->{unit} = undef; | |||||||||||||||||||||||||||
| 34 | 6 | ▶ | @_ = () if( @_ == 1 && $self->_is_a( $_[0] => 'Module::Generic::Null' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
@_ == 1 and $self->_is_a($_[0], "Module::Generic::Null")
MC/DC0%0/2
@_ == 1 and $self->_is_a($_[0], "Module::Generic::Null")
@_ == 1
$self->_is_a($_[0], "Module::Generic::Null")
Truth table66%2/3
A and B
A= @_ == 1B= $self->_is_a($_[0], "Module::Generic::Null")
| |||||||||||||||||||||||||||||
| 35 | 6 | ▶ | if( @_ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | { | ||||||||||||||||||||||||||||
| 37 | 5 | my $str = shift( @_ ); | |||||||||||||||||||||||||||
| 38 | 5 | ▶ | if( $str =~ m,^(?<unit>\S+)[[:blank:]]+(?:(?:(?<start>\d+)-(?<end>\d+))|\*)/(?<size>\d+|\*), ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 39 | { | ||||||||||||||||||||||||||||
| 40 | 5 | $self->unit( $+{unit} ); | |||||||||||||||||||||||||||
| 41 | 5 | ▶ | $self->range_start( $+{start} ) if( defined( $+{start} ) && length( $+{start} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $+{"start"} and length $+{"start"}
MC/DC50%1/2
defined $+{"start"} and length $+{"start"}
defined $+{"start"}
length $+{"start"}
Truth table66%2/3
A and B
A= defined $+{"start"}B= length $+{"start"}
| |||||||||||||||||||||||||||||
| 42 | 5 | ▶ | $self->range_end( $+{end} ) if( defined( $+{end} ) && length( $+{end} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $+{"end"} and length $+{"end"}
MC/DC50%1/2
defined $+{"end"} and length $+{"end"}
defined $+{"end"}
length $+{"end"}
Truth table66%2/3
A and B
A= defined $+{"end"}B= length $+{"end"}
| |||||||||||||||||||||||||||||
| 43 | 5 | ▶ | $self->size( $+{size} ) if( defined( $+{size} ) && length( $+{size} ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
defined $+{"size"} and length $+{"size"}
MC/DC0%0/2
defined $+{"size"} and length $+{"size"}
defined $+{"size"}
length $+{"size"}
Truth table33%1/3
A and B
A= defined $+{"size"}B= length $+{"size"}
| |||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | else | ||||||||||||||||||||||||||||
| 46 | { | ||||||||||||||||||||||||||||
| 47 | 0 | return( $self->error( "Unsupported Content-Range value format '$str'." ) ); | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||||||||
| 50 | 6 | $self->{_init_strict_use_sub} = 1; | |||||||||||||||||||||||||||
| 51 | 6 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 6 | $self->_field_name( 'Content-Range' ); | |||||||||||||||||||||||||||
| 53 | 6 | return( $self ); | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | sub as_string | ||||||||||||||||||||||||||||
| 57 | { | ||||||||||||||||||||||||||||
| 58 | 7 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=9 SCAR=23.6
Pod
documented
| |||||||||||||||||||||||||||||
| 59 | 7 | ▶ | my $unit = $self->unit || 'bytes'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$self->unit || 'bytes'
MC/DC0%0/1
$self->unit || 'bytes'
$self->unit
Truth table50%1/2
A
A= $self->unit
| |||||||||||||||||||||||||||||
| 60 | 7 | my $start = $self->range_start; | |||||||||||||||||||||||||||
| 61 | 7 | my $end = $self->range_end; | |||||||||||||||||||||||||||
| 62 | 7 | my $size = $self->size; | |||||||||||||||||||||||||||
| 63 | 7 | my @parts = (); | |||||||||||||||||||||||||||
| 64 | 7 | ▶ | if( defined( $start ) || defined( $end ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $start or defined $end
MC/DC50%1/2
defined $start or defined $end
defined $start
defined $end
Truth table66%2/3
A or B
A= defined $startB= defined $end
| |||||||||||||||||||||||||||||
| 65 | { | ||||||||||||||||||||||||||||
| 66 | 6 | ▶ | if( $start eq '*' && $end eq '*' ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$start eq "*" and $end eq "*"
MC/DC0%0/2
$start eq "*" and $end eq "*"
$start eq "*"
$end eq "*"
Truth table33%1/3
A and B
A= $start eq "*"B= $end eq "*"
| |||||||||||||||||||||||||||||
| 67 | { | ||||||||||||||||||||||||||||
| 68 | 0 | push( @parts, '*' ); | |||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||
| 70 | else | ||||||||||||||||||||||||||||
| 71 | { | ||||||||||||||||||||||||||||
| 72 | 6 | ▶ | $start //= '*'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$start //= "*"
MC/DC0%0/1
$start //= "*"
$start
Truth table50%1/2
A
A= $start
| |||||||||||||||||||||||||||||
| 73 | 6 | ▶ | $end //= '*'; | ||||||||||||||||||||||||||
|
Condition50%1/2
$end //= "*"
MC/DC0%0/1
$end //= "*"
$end
Truth table50%1/2
A
A= $end
| |||||||||||||||||||||||||||||
| 74 | 6 | push( @parts, "${start}-${end}" ); | |||||||||||||||||||||||||||
| 75 | } | ||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | else | ||||||||||||||||||||||||||||
| 78 | { | ||||||||||||||||||||||||||||
| 79 | 1 | push( @parts, '*' ); | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | 7 | ▶ | push( @parts, defined( $size ) ? $size : '*' ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 82 | 7 | return( join( ' ', $unit, join( '/', @parts ) ) ); | |||||||||||||||||||||||||||
| 83 | } | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 17 | ▶ | sub range_end { return( shift->_set_get_scalar_as_object( 'range_end', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
range_end: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 18 | ▶ | sub range_start { return( shift->_set_get_scalar_as_object( 'range_start', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
range_start: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 19 | ▶ | sub size { return( shift->_set_get_scalar_as_object( 'size', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | sub start_end | ||||||||||||||||||||||||||||
| 92 | { | ||||||||||||||||||||||||||||
| 93 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
start_end: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||
| 94 | 0 | my( $start, $end ) = @_; | |||||||||||||||||||||||||||
| 95 | 0 | $self->range_start( $start ); | |||||||||||||||||||||||||||
| 96 | 0 | ▶ | $self->range_end( $end // $start ); | ||||||||||||||||||||||||||
|
Condition0%0/3
$end // $start
MC/DC0%0/2
$end // $start
$end
$start
Truth table0%0/3
A // B
A= $endB= $start
| |||||||||||||||||||||||||||||
| 97 | 0 | return( $self ); | |||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||
| 100 | 19 | ▶ | sub unit { return( shift->_set_get_scalar_as_object( 'unit', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
unit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | sub _set_get_number_or_wildcard | ||||||||||||||||||||||||||||
| 103 | { | ||||||||||||||||||||||||||||
| 104 | 0 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
_set_get_number_or_wildcard: not called CC=5 SCAR=34.0
| |||||||||||||||||||||||||||||
| 105 | 0 | ▶ | my $prop = shift( @_ ) || return( $self->error( "No property name was provided." ) ); | ||||||||||||||||||||||||||
|
Condition0%0/2
shift @_ || (return $self->error('No property name was provided.'))
MC/DC0%0/1
shift @_ || (return $self->error('No property name was provided.'))
shift @_
Truth table0%0/2
A
A= shift @_
| |||||||||||||||||||||||||||||
| 106 | 0 | ▶ | if( @_ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | { | ||||||||||||||||||||||||||||
| 108 | 0 | my $v = shift( @_ ); | |||||||||||||||||||||||||||
| 109 | 0 | ▶ | if( defined( $v ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | { | ||||||||||||||||||||||||||||
| 111 | 0 | ▶ | return( $self->error( "Value provided for property \"${prop}\" is invalid. I was expecting either an integer or a wildcard." ) ) if( $v !~ /^\d+|\*$/ ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 112 | } | ||||||||||||||||||||||||||||
| 113 | 0 | return( $self->_set_get_scalar_as_object( $prop => $v ) ); | |||||||||||||||||||||||||||
| 114 | } | ||||||||||||||||||||||||||||
| 115 | 0 | return( $self->_set_get_scalar_as_object( $prop ) ); | |||||||||||||||||||||||||||
| 116 | } | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 3 | 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 | ||||||||||||||||||||||||||||