| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 2 | ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Headers/Range.pm | ||||||||||||||||||||||||||||
| 3 | ## Version v0.1.0 | ||||||||||||||||||||||||||||
| 4 | ## Copyright(c) 2022 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||||||||||||||||||
| 6 | ## Created 2022/05/08 | ||||||||||||||||||||||||||||
| 7 | ## Modified 2022/05/08 | ||||||||||||||||||||||||||||
| 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::Range; | ||||||||||||||||||||||||||||
| 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=25.6
Pod
documented
| |||||||||||||||||||||||||||||
| 30 | 6 | $self->{ranges} = []; | |||||||||||||||||||||||||||
| 31 | 6 | $self->{unit} = 'bytes'; | |||||||||||||||||||||||||||
| 32 | 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")
| |||||||||||||||||||||||||||||
| 33 | 6 | ▶ | if( @_ ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | { | ||||||||||||||||||||||||||||
| 35 | 5 | my $this = shift( @_ ); | |||||||||||||||||||||||||||
| 36 | 5 | my( $unit, $ref ); | |||||||||||||||||||||||||||
| 37 | 5 | ▶ | if( $self->_is_array( $this ) ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | 0 | $ref = $this; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | else | ||||||||||||||||||||||||||||
| 42 | { | ||||||||||||||||||||||||||||
| 43 | 5 | ▶ | return( $self->error( "Bad argument provided '$this'. You can provide either an array reference or a string." ) ) if( ref( $this ) && !overload::Method( $this, '""' ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
ref $this and not overload::Method($this, "\"\"")
MC/DC0%0/2
ref $this and not overload::Method($this, "\"\"")
ref $this
not overload::Method($this, "\"\"")
Truth table33%1/3
A and not(B)
A= ref $thisB= not overload::Method($this, "\"\"")
| |||||||||||||||||||||||||||||
| 44 | 5 | $this =~ s/^[[:blank:]\h]+|[[:blank:]\h]+$//g; | |||||||||||||||||||||||||||
| 45 | 5 | ▶ | if( $this =~ s/^([a-zA-Z][a-zA-Z\_]+)[[:blank:]\h]*=[[:blank:]\h]*// ) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | { | ||||||||||||||||||||||||||||
| 47 | 5 | $unit = $1; | |||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||
| 49 | 5 | $ref = [split( /[[:blank:]\h]*,[[:blank:]\h]*/, "$this" )]; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 5 | my $ranges = $self->new_array; | |||||||||||||||||||||||||||
| 52 | 5 | foreach( @$ref ) | |||||||||||||||||||||||||||
| 53 | { | ||||||||||||||||||||||||||||
| 54 | 11 | my( $start, $end ) = split( /[[:blank:]\h]*-[[:blank:]\h]*/ ); | |||||||||||||||||||||||||||
| 55 | 11 | ▶ | $start = undef if( !length( $start ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 56 | 11 | ▶ | $end = undef if( !length( $end ) ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | 11 | my $range = HTTP::Promise::Headers::Range::StartEnd->new( $start, $end ); | |||||||||||||||||||||||||||
| 58 | 11 | $ranges->push( $range ); | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | 5 | $self->ranges( $ranges ); | |||||||||||||||||||||||||||
| 61 | } | ||||||||||||||||||||||||||||
| 62 | 6 | $self->{_init_strict_use_sub} = 1; | |||||||||||||||||||||||||||
| 63 | 6 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 6 | $self->_field_name( 'Range' ); | |||||||||||||||||||||||||||
| 65 | 6 | return( $self ); | |||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||
| 67 | |||||||||||||||||||||||||||||
| 68 | sub as_string | ||||||||||||||||||||||||||||
| 69 | { | ||||||||||||||||||||||||||||
| 70 | 7 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
as_string: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 71 | # In the sprintf, we use %s and not %d, because start or end may be undef, and it would inadvertently result in 0 | ||||||||||||||||||||||||||||
| 72 | 7 | ▶ | return( $self->unit->scalar . '=' . $self->ranges->map(sub{ sprintf( '%s-%s', ( $_->start // '' ), ( $_->end // '' ) ); })->join( ', ' )->scalar ); | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Condition100%4/4
$_->start // ""
$_->end // ""
MC/DC100%1/1
$_->start // ""
$_->start
MC/DC100%1/1
$_->end // ""
$_->end
Truth table100%2/2
A
A= $_->start
Truth table100%2/2
A
A= $_->end
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | 0 | ▶ | sub new_range { HTTP::Promise::Headers::Range::StartEnd->new( @_ ) }; | ||||||||||||||||||||||||||
|
Subroutine
new_range: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||
| 77 | 26 | ▶ | sub ranges { return( shift->_set_get_array_as_object( 'ranges', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
ranges: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | 11 | ▶ | sub unit { return( shift->_set_get_scalar_as_object( 'unit', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
unit: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | { | ||||||||||||||||||||||||||||
| 82 | package | ||||||||||||||||||||||||||||
| 83 | HTTP::Promise::Headers::Range::StartEnd; | ||||||||||||||||||||||||||||
| 84 | BEGIN | ||||||||||||||||||||||||||||
| 85 | { | ||||||||||||||||||||||||||||
| 86 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 87 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 88 | 3 | ▶ | use parent qw( Module::Generic ); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 89 | }; | ||||||||||||||||||||||||||||
| 90 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 91 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | sub init | ||||||||||||||||||||||||||||
| 94 | { | ||||||||||||||||||||||||||||
| 95 | 11 | ▶ | my $self = shift( @_ ); | ||||||||||||||||||||||||||
|
Subroutine
init: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 96 | 11 | my @args = @_; | |||||||||||||||||||||||||||
| 97 | 11 | @$self{qw( start end )} = splice( @_, 0, 2 ); | |||||||||||||||||||||||||||
| 98 | 11 | return( $self->SUPER::init( @_ ) ); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 44 | ▶ | sub end { return( shift->_set_get_number( 'end', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
end: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 34 | ▶ | sub start { return( shift->_set_get_number( 'start', @_ ) ); } | ||||||||||||||||||||||||||
|
Subroutine
start: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 104 | } | ||||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 3 | 1; | |||||||||||||||||||||||||||
| 107 | # NOTE: POD | ||||||||||||||||||||||||||||
| 108 | __END__ | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | =encoding utf-8 | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | =head1 NAME | ||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | HTTP::Promise::Headers::Range - Range Header Field | ||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||
| 116 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | use HTTP::Promise::Headers::Range; | ||||||||||||||||||||||||||||
| 119 | my $range = HTTP::Promise::Headers::Range->new || | ||||||||||||||||||||||||||||
| 120 | die( HTTP::Promise::Headers::Range->error, "\n" ); | ||||||||||||||||||||||||||||
| 121 | $range->unit( 'bytes' ): | ||||||||||||||||||||||||||||
| 122 | $range->ranges->push( $range->new_range( 200, 1000 ) ); | ||||||||||||||||||||||||||||
| 123 | my $start = $range->ranges->first->start; # 200 | ||||||||||||||||||||||||||||
| 124 | my $end = $range->ranges->first->end; # 1000 | ||||||||||||||||||||||||||||
| 125 | $range->ranges->push( $range->new_range( 1001, 2000 ) ); | ||||||||||||||||||||||||||||
| 126 | say $range->as_string; | ||||||||||||||||||||||||||||
| 127 | # or | ||||||||||||||||||||||||||||
| 128 | say "$range"; | ||||||||||||||||||||||||||||
| 129 | # bytes=200-1000, 1001-2000 | ||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||
| 131 | =head1 VERSION | ||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||
| 133 | v0.1.0 | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | The following is an extract from Mozilla documentation. | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | The Range HTTP request header indicates the part of a document that the server should return. | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | Example: | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | # Getting multiple ranges | ||||||||||||||||||||||||||||
| 144 | Range: bytes=200-1000, 2000-6576, 19000- | ||||||||||||||||||||||||||||
| 145 | # The last 500 bytes | ||||||||||||||||||||||||||||
| 146 | Range: bytes=0-499, -500 | ||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | Range: bytes=200- | ||||||||||||||||||||||||||||
| 149 | Range: bytes=200-1000 | ||||||||||||||||||||||||||||
| 150 | Range: bytes=200-1000, 1001-2000 | ||||||||||||||||||||||||||||
| 151 | Range: bytes=200-1000, 1001-2000, 2001-3000 | ||||||||||||||||||||||||||||
| 152 | Range: bytes=-4321 | ||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | =head1 METHODS | ||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||
| 156 | =head2 as_string | ||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||
| 158 | Returns a string representation of the C<Range> object. | ||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||
| 160 | =head2 new_range | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | Provided with a start and and offset, and this will return a new C<HTTP::Promise::Headers::Range::StartEnd> object. | ||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | This object has two methods: C<start> and C<end> each capable of setting or returning its value, which may be C<undef> | ||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||
| 166 | =head2 ranges | ||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||
| 168 | Sets or gets the L<array object|Module::Generic::Array> that contains all the C<HTTP::Promise::Headers::Range::StartEnd> objects (see below for a descriptions). Thus you can use all the methods from L<Module::Generic::Array> to manipulate the range objects. | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | =head2 unit | ||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||
| 172 | The unit in which ranges are specified. This is usually C<bytes>. | ||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||
| 174 | =head1 HTTP::Promise::Headers::Range::StartEnd | ||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||
| 176 | =head2 end | ||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | Sets or gets the end of the range as a L<number object|Module::Generic::Number> | ||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||
| 180 | =head2 start | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | Sets or gets the start of the range as a L<number object|Module::Generic::Number> | ||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | =head1 THREAD-SAFETY | ||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries. | ||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | =head1 AUTHOR | ||||||||||||||||||||||||||||
| 189 | |||||||||||||||||||||||||||||
| 190 | Jacques Deguest E<lt>F<jack@deguest.jp>E<gt> | ||||||||||||||||||||||||||||
| 191 | |||||||||||||||||||||||||||||
| 192 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | See also L<rfc7233, section 3.1|https://tools.ietf.org/html/rfc7233#section-3.1> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Range> | ||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||
| 196 | 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> | ||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||
| 198 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | Copyright(c) 2022 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||
| 202 | All rights reserved. | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself. | ||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||
| 206 | =cut | ||||||||||||||||||||||||||||