lib/HTTP/Promise/Headers/AltSvc.pm

filter: Statementstmt 54.5% 42 / 77 Branchbran 25.0% 10 / 40 Conditioncond 42.1% 8 / 19 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 61.1% 11 / 18 Podpod 100% 9 / 9 totaltotal 46.2% 80 / 173 CC 28 SCAR 38.8
CC
28
Cov
44%
alternative
156.0
protocol
19.8
init
14.6
CRAP 48.6
« blib/lib/HTTP/Promise/Stream/UU.pm ↑ Summary lib/HTTP/Promise/Headers/Generic.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Headers/AltSvc.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::AltSvc;
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 ▶ use URI::Escape::XS ();
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 our $VERSION = 'v0.1.0';
23 };
24  
25 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
26 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
27  
28 sub init
29 {
30 5 ▶ my $self = shift( @_ );
Subroutine init: called CC=12 SCAR=26.8
Pod documented
31 5 ▶ @_ = () if( @_ == 1 && $self->_is_a( $_[0] => 'Module::Generic::Null' ) );
Branch: 1 branch
BranchTrueFalseTotal
if @_ == 1 and $self->_is_a($_[0], "Module::Generic::Null") 0 5 5
Condition66%2/3
@_ == 1 and $self->_is_a($_[0], "Module::Generic::Null")
!ll&&!rl&&r
140
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")
ABresult
0X0
100
111
32 5 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 4 1 5
33 {
34 4 my $this = shift( @_ );
35 4 my $params = $self->_get_args_as_hash( @_ );
36 4 ▶ unless( ( $self->_is_array( $this ) && scalar( @$this ) == 2 ) ||
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->_is_array($this) and scalar @$this == 2 or not ref $this or overload::Method($this, "''")) 0 4 4
Condition55%5/9
$self->_is_array($this) and scalar @$this == 2 or not ref $this or overload::Method($this, "''")
l!l&&r!l&&!r
400
$self->_is_array($this) and scalar @$this == 2 or not ref $this
l!l&&r!l&&!r
130
$self->_is_array($this) and scalar @$this == 2
!ll&&!rl&&r
301
MC/DC0%0/4
$self->_is_array($this) and scalar @$this == 2 or not ref $this or overload::Method($this, "''")
$self->_is_array($this) scalar @$this == 2 not ref $this overload::Method($this, "''")
Truth table28%2/7
A and B or not(C) or D
A= $self->_is_array($this)B= scalar @$this == 2C= not ref $thisD= overload::Method($this, "''")
ABCDresult
11XX1
0X1X1
101X1
0X011
10011
0X000
10000
37 !ref( $this ) ||
38 overload::Method( $this, "''" ) )
39 {
40 0 return( $self->error( "Wrong alternate server name-value provided '$this'. I was expecting either a name=value string or an array reference with 2 elements." ) );
41 }
42 4 ▶ my $hv = $self->_is_array( $this ) ? $self->_new_hv( $this ) : $self->_parse_header_value( $this );
Branch: 1 branch
BranchTrueFalseTotal
$self->_is_array($this) ? : 1 3 4
43 4 ▶ return( $self->pass_error ) if( !defined( $hv ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $hv 0 4 4
44 4 ▶ $hv->_set_get_params( $params ) if( scalar( keys( %$params ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$params 0 4 4
45 4 $hv->encode(1);
46 4 $self->_hv( $hv );
47 }
48 5 $self->{_init_strict_use_sub} = 1;
49 5 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 5 5
50 5 $self->_field_name( 'Alt-Svc' );
51 5 return( $self );
52 }
53  
54 6 ▶sub as_string { return( shift->_hv_as_string( @_ ) ); }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
55  
56 sub alternative
57 {
58 0 ▶ my $self = shift( @_ );
Subroutine alternative: not called CC=12 SCAR=50.5
Pod documented
59 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 0 0
60 {
61 0 ▶ return( $self->error( "Bad arguments provided. Usage: \$h->alternative( \$proto, \$auth )" ) ) if( @_ > 2 );
Branch: 1 branch
BranchTrueFalseTotal
if @_ > 2 0 0 0
62 0 ▶ my( $proto, $auth ) = @_ > 1 ? @_[0,1] : $_[0];
Branch: 1 branch
BranchTrueFalseTotal
@_ > 1 ? : 0 0 0
63 # need escaping?
64 0 ▶ if( @_ == 1 )
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) 0 0 0
65 {
66 0 ▶ return( $self->error( "Bad argument provided. You need to provide a protocol=authority." ) ) if( index( $proto, '=' ) == -1 );
Branch: 1 branch
BranchTrueFalseTotal
if (index($proto, "=") == -1) 0 0 0
67 0 ( $proto, $auth ) = split( /=/, $proto, 2 );
68 0 ▶ $proto = $self->_unescape( $proto ) if( $proto =~ /\%(?=\d{2})/ );
Branch: 1 branch
BranchTrueFalseTotal
if $proto =~ /\%(?=\d{2})/ 0 0 0
69 }
70 0 my $hv;
71 0 ▶ if( $hv = $self->_hv )
Branch: 1 branch
BranchTrueFalseTotal
if ($hv = $self->_hv) { } 0 0 0
72 {
73 0 $hv->value( [ $proto, $auth ] );
74 }
75 else
76 {
77 0 $hv = $self->_new_hv( [ $proto, $auth ] );
78 0 $hv->encode(1);
79 0 $self->_hv( $hv );
80 }
81 }
82 else
83 {
84 0 ▶ my $hv = $self->_hv || return( '' );
Condition0%0/2
$self->_hv || (return '')
l!l
00
MC/DC0%0/1
$self->_hv || (return '')
$self->_hv
Truth table0%0/2
A
A= $self->_hv
Aresult
11
00
85 0 my $ref = $hv->value;
86 0 ▶ return( wantarray() ? () : '' ) if( $ref->is_empty );
Branch: 2 branches
BranchTrueFalseTotal
if $ref->is_empty 0 0 0
wantarray ? : 0 0 0
87 0 ▶ return( $ref->list ) if( wantarray() );
Branch: 1 branch
BranchTrueFalseTotal
if wantarray 0 0 0
88 0 my( $proto, $auth ) = $ref->list;
89 # $proto = $self->_escape( $proto );
90 0 $proto = $hv->token_escape( $proto );
91 0 return( join( '=', $proto, $auth ) );
92 }
93 }
94  
95 # This needs a protocol to be set first
96 2 ▶sub authority { return( shift->_hv->value_data( @_ ) ); }
Subroutine authority: called CC=1 SCAR=0.0
Pod documented
97  
98 0 ▶sub ma { return( shift->_set_get_param( ma => @_ ) ); }
Subroutine ma: not called CC=1 SCAR=6.9
Pod documented
99  
100 0 ▶sub param { return( shift->_set_get_param( @_ ) ); }
Subroutine param: not called CC=1 SCAR=6.9
Pod documented
101  
102 0 ▶sub params { return( shift->_set_get_params( @_ ) ); }
Subroutine params: not called CC=1 SCAR=6.9
Pod documented
103  
104 0 ▶sub persist { return( shift->_set_get_param( persist => @_ ) ); }
Subroutine persist: not called CC=1 SCAR=6.9
Pod documented
105  
106 sub protocol
107 {
108 2 ▶ my $self = shift( @_ );
Subroutine protocol: called CC=6 SCAR=29.9
Pod documented
109 2 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 0 2 2
110 {
111 0 my $proto = shift( @_ );
112 0 ▶ return( $self->error( "Value provided for protocol is empty." ) ) if( !defined( $proto ) || !length( "$proto" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $proto and length "$proto" 0 0 0
Condition0%0/3
defined $proto and length "$proto"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $proto and length "$proto"
defined $proto length "$proto"
Truth table0%0/3
A and B
A= defined $protoB= length "$proto"
ABresult
0X0
100
111
113 0 my $hv = $self->_hv;
114 0 ▶ if( $hv )
Branch: 1 branch
BranchTrueFalseTotal
if ($hv) { } 0 0 0
115 {
116 0 $hv->value_name( $proto );
117 }
118 else
119 {
120 0 $hv = $self->_new_hv( $proto );
121 0 $self->_hv( $hv );
122 }
123 }
124 else
125 {
126 2 ▶ my $hv = $self->_hv || return( '' );
Condition50%1/2
$self->_hv || (return '')
l!l
20
MC/DC0%0/1
$self->_hv || (return '')
$self->_hv
Truth table50%1/2
A
A= $self->_hv
Aresult
11
00
127 2 return( $hv->value_name );
128 }
129 }
130  
131 # As per rfc7838, section 3: <https://tools.ietf.org/html/rfc7838#section-3>
132 # sub _escape
133 # {
134 # my $self = shift( @_ );
135 # my $v = shift( @_ );
136 # $v =~ s/([=:%]+)/sprintf("%%%02X", ord($1))/ge;
137 # return( $v );
138 # }
139 0 ▶sub _escape { return( URI::Escape::XS::uri_escape( $_[1] ) ); }
Subroutine _escape: not called CC=1 SCAR=6.9
140  
141 # sub _unescape
142 # {
143 # my $self = shift( @_ );
144 # my $v = shift( @_ );
145 # $v =~ s/%([0-9A-Fa-f]{2})/chr(hex($1))/eg;
146 # return( $v );
147 # }
148 0 ▶sub _unescape { return( URI::Escape::XS::uri_unescape( $_[1] ) ); }
Subroutine _unescape: not called CC=1 SCAR=6.9
149  
150 3 1;
151 # NOTE: POD
152 __END__
153  
154 =encoding utf-8
155  
156 =head1 NAME
157  
158 HTTP::Promise::Headers::AltSvc - AltSvc Header Field
159  
160 =head1 SYNOPSIS
161  
162 use HTTP::Promise::Headers::AltSvc;
163 my $alt = HTTP::Promise::Headers::AltSvc->new ||
164 die( HTTP::Promise::Headers::AltSvc->error, "\n" );
165 $alt->alternative( q{h2="new.example.org:80"} );
166 $alt->alternative( 'h2', 'new.example.org:80' );
167 my $def = $alt->alternative; # h2="new.example.org:80"
168 $alt->ma(2592000);
169 $alt->persist(1);
170 $alt->authority( 'new.example.org:443' );
171 $alt->protocol( 'h2' );
172 say "$alt"; # stringifies
173 say $alt->as_string; # same
174  
175 =head1 VERSION
176  
177 v0.1.0
178  
179 =head1 DESCRIPTION
180  
181 The following description is taken from Mozilla documentation.
182  
183 Alt-Svc: clear
184 Alt-Svc: <protocol-id>=<alt-authority>
185  
186 The special value clear indicates that the origin requests all alternative services for that origin to be invalidated.
187  
188 C<protocol-id> is the C<ALPN> protocol identifier. Examples include h2 for HTTP/2 and h3-25 for draft 25 of the HTTP/3 protocol.
189  
190 C<alt-authority> is the quoted string specifying the alternative authority which consists of an optional host override, a colon, and a mandatory port number.
191  
192 Alt-Svc: h2=":443"; ma=2592000;
193 Alt-Svc: h2=":443"; ma=2592000; persist=1
194 Alt-Svc: h2="alt.example.com:443", h2=":443"
195 Alt-Svc: h3-25=":443"; ma=3600, h2=":443"; ma=3600
196  
197 Multiple entries can be specified in a single C<Alt-Svc> header using comma as separator. In that case, early entries are considered more preferable.
198  
199 You can achieve this the following way:
200  
201 my $alt1 = HTTP::Promise::Headers::AltSvc->new( q{h2="alt.example.com:443"} );
202 $alt1->ma(3600);
203 $alt1->persist(1);
204 my $alt2 = HTTP::Promise::Headers::AltSvc->new( q{h2=":443"} );
205 $alt2->ma(3600);
206 my $headers = HTTP::Promise::Headers->new;
207 $headers->push_header( alt_svc => "$alt1", alt_svc => "$alt2" );
208  
209 =head1 CONSTRUCTOR
210  
211 =head2 new
212  
213 You can create a new instance of this class without passing any parameter, and set them afterward.
214  
215 If you want to set parameters upon object instantiation, this takes either an array reference with 2 values (C<protocol> and C<authority>), or a string (or something that stringifies, and an optional hash or hash reference of parameters and it returns a new object.
216  
217 If you provide a string, it will be parsed, so be careful what you provide, and make sure that non-ascii characters are escaped first. For example:
218  
219 my $alt = HTTP::Promise::Headers::AltSvc->new( 'w=x:y#z' );
220  
221 It will be interpreted, wrongly, as C<w> being the protocol and C<x:y#z>, so instead you would need to either escape it before (with L<URI::Escape::XS> for example), or provide it as an array of 2 elements (protocol and authority), such as:
222  
223 my $alt = HTTP::Promise::Headers::AltSvc->new( ['w=x:y#z', 'new.example.org:443'] );
224  
225 =head1 METHODS
226  
227 =head2 alternative
228  
229 Sets or gets the alternative protocol and authority.
230  
231 For example:
232  
233 $h->alternative( $proto, $auth );
234 my $alt = $h->alternative; # h2="alt.example.com:443"
235  
236 =head2 authority
237  
238 Sets or gets the authority, which is the value in the equal assignment, such as:
239  
240 h2="alt.example.com:443"
241  
242 Here the authority would be C<alt.example.com:443>
243  
244 my $u = URI->new( 'https://alt.example.com' );
245 $h->authority( $u->host_port );
246  
247 =head2 ma
248  
249 This is optional and takes a number.
250  
251 The number of seconds for which the alternative service is considered fresh. If omitted, it defaults to 24 hours. Alternative service entries can be cached for up to <max-age> seconds, minus the age of the response (from the Age header). Once the cached entry expires, the client can no longer use this alternative service for new connections.
252  
253 =head2 param
254  
255 Set or get an arbitrary name-value pair attribute.
256  
257 =head2 params
258  
259 Set or get multiple name-value parameters.
260  
261 Calling this without any parameters, retrieves the associated L<hash object|Module::Generic::Hash>
262  
263 =head2 persist
264  
265 This is optional and takes a number.
266  
267 Usually cached alternative service entries are cleared on network configuration changes. Use of the persist=1 parameter requests that the entry not be deleted by such changes.
268  
269 =head2 protocol
270  
271 Sets or gets the protocol. For example:
272  
273 $alt->protocol( 'h2' );
274  
275 Here, C<h2> is the protocol and means HTTP/2. C<h3-25> would be for draft 25 of the HTTP/3 protocol.
276  
277 You can even pass unsafe characters. They will be encoded upon stringification:
278  
279 $alt->protocol( 'w=x:y#z' ); # example from rfc7838
280  
281 =head1 THREAD-SAFETY
282  
283 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
284  
285 =head1 AUTHOR
286  
287 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
288  
289 =head1 SEE ALSO
290  
291 L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Alt-Svc>, L<rfc7838, section 3|https://tools.ietf.org/html/rfc7838#section-3>
292  
293 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>
294  
295 =head1 COPYRIGHT & LICENSE
296  
297 Copyright(c) 2022 DEGUEST Pte. Ltd.
298  
299 All rights reserved.
300  
301 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
302  
303 =cut
« blib/lib/HTTP/Promise/Stream/UU.pm ↑ Summary lib/HTTP/Promise/Headers/Generic.pm »