lib/HTTP/Promise/Headers/Link.pm

filter: Statementstmt 75.3% 61 / 81 Branchbran 46.8% 15 / 32 Conditioncond 38.1% 8 / 21 MC/DCmcdc 0.0% 0 / 13 Subroutinesub 75.0% 12 / 16 Podpod 100% 10 / 10 totaltotal 61.2% 106 / 173 CC 25 SCAR 24.3
CC
25
Cov
63%
title_charset
30.0
title
19.1
init
10.1
CRAP 11.3
« lib/HTTP/Promise/Pool.pm ↑ Summary lib/HTTP/Promise/Headers/ContentRange.pm »
1 ##----------------------------------------------------------------------------
2 ## Asynchronous HTTP Request and Promise - ~/lib/HTTP/Promise/Headers/Link.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::Link;
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 5 ▶ my $self = shift( @_ );
Subroutine init: called CC=9 SCAR=23.1
Pod documented
30 5 $self->{anchor} = undef;
31 5 $self->{rel} = undef;
32 5 $self->{title} = undef;
33 5 $self->{uri} = undef;
34 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
230
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
35 5 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 3 2 5
36 {
37 3 my $str = shift( @_ );
38 3 ▶ return( $self->error( "No value was provided for Link field." ) ) if( !defined( $str ) || !length( "$str" ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $str and length "$str" 0 3 3
Condition33%1/3
defined $str and length "$str"
!ll&&!rl&&r
003
MC/DC0%0/2
defined $str and length "$str"
defined $str length "$str"
Truth table33%1/3
A and B
A= defined $strB= length "$str"
ABresult
0X0
100
111
39 3 my $params = $self->_get_args_as_hash( @_ );
40 3 ▶ my $hv = $self->_parse_header_value( $str ) ||
Condition50%1/2
$self->_parse_header_value($str) || (return $self->pass_error)
l!l
30
MC/DC0%0/1
$self->_parse_header_value($str) || (return $self->pass_error)
$self->_parse_header_value($str)
Truth table50%1/2
A
A= $self->_parse_header_value($str)
Aresult
11
00
41 return( $self->pass_error );
42 3 ▶ $hv->params( $params ) if( scalar( keys( %$params ) ) );
Branch: 1 branch
BranchTrueFalseTotal
if scalar keys %$params 0 3 3
43 3 $self->_hv( $hv );
44 }
45 5 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 5 5
46 5 $self->_field_name( 'Link' );
47 5 return( $self );
48 }
49  
50 6 ▶sub as_string { return( shift->_hv_as_string( [qw( rel title title* anchor )] ) ); }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
51  
52 # sub as_string
53 # {
54 # my $self = shift( @_ );
55 # my $uri = $self->uri || return( '' );
56 # my $rel = $self->rel;
57 # return( "<${uri}>; rel=\"${rel}\"" ) if( $rel );
58 # return( "<${uri}>" );
59 # }
60  
61 5 ▶sub anchor { return( shift->_set_get_param( anchor => @_ ) ); }
Subroutine anchor: called CC=1 SCAR=0.0
Pod documented
62  
63 sub link
64 {
65 6 ▶ my $self = shift( @_ );
Subroutine link: called CC=5 SCAR=16.4
Pod documented
66 6 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 5 6
67 {
68 1 my $link = shift( @_ );
69 1 $link =~ s/^\<|(?<!\\)\>$//g;
70 1 my $link2 = qq{<${link}>};
71 1 my $hv = $self->_hv;
72 1 ▶ if( $hv )
Branch: 1 branch
BranchTrueFalseTotal
if ($hv) { } 0 1 1
73 {
74 0 $hv->value( $link2 );
75 }
76 else
77 {
78 1 ▶ $hv = $self->_new_hv( $link2 ) || return( $self->pass_error );
Condition50%1/2
$self->_new_hv($link2) || (return $self->pass_error)
l!l
10
MC/DC0%0/1
$self->_new_hv($link2) || (return $self->pass_error)
$self->_new_hv($link2)
Truth table50%1/2
A
A= $self->_new_hv($link2)
Aresult
11
00
79 1 $self->_hv( $hv );
80 }
81 1 return( $link );
82 }
83 else
84 {
85 # No header value object, means there is just nothing set yet
86 5 ▶ my $hv = $self->_hv || return( '' );
Condition50%1/2
$self->_hv || (return '')
l!l
50
MC/DC0%0/1
$self->_hv || (return '')
$self->_hv
Truth table50%1/2
A
A= $self->_hv
Aresult
11
00
87 5 my $link = $hv->value_data;
88 5 $link =~ s/^\<|(?<!\\)\>$//g;
89 5 return( $link );
90 }
91 }
92  
93 0 ▶sub param { return( shift->_set_get_param( @_ ) ); }
Subroutine param: not called CC=1 SCAR=6.9
Pod documented
94  
95 0 ▶sub params { return( shift->_set_get_params( @_ ) ); }
Subroutine params: not called CC=1 SCAR=6.9
Pod documented
96  
97 6 ▶sub rel { return( shift->_set_get_param( rel => @_ ) ); }
Subroutine rel: called CC=1 SCAR=0.0
Pod documented
98  
99 sub title
100 {
101 5 ▶ my $self = shift( @_ );
Subroutine title: called CC=9 SCAR=29.5
Pod documented
102 5 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) { } 1 4 5
103 {
104 1 my( $title, $lang ) = @_;
105 1 ▶ if( !defined( $title ) )
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $title) { } 0 1 1
106 {
107 0 $self->params->delete( 'title' );
108 0 $self->params->delete( 'title*' );
109 }
110 else
111 {
112 1 ▶ $lang //= $self->title_lang;
Condition33%1/3
$lang //= $self->title_lang
l!l&&r!l&&!r
100
MC/DC0%0/2
$lang //= $self->title_lang
$lang $self->title_lang
Truth table33%1/3
A //= B
A= $langB= $self->title_lang
ABresult
1X1
011
000
113 1 ▶ if( my $enc = $self->_filename_encode( $title, $lang ) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $enc = $self->_filename_encode($title, $lang)) { } 1 0 1
114 {
115 1 ▶ $self->_set_get_param( 'title*' => $enc ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->_set_get_param("title*", $enc) or return $self->pass_error 0 1 1
116 }
117 else
118 {
119 0 $self->_set_get_qparam( title => $title );
120 }
121 }
122 }
123 else
124 {
125 4 my $v = $self->_set_get_qparam( 'title' );
126 4 ▶ if( !defined( $v ) || !length( $v ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $v and length $v) 0 4 4
Condition33%1/3
defined $v and length $v
!ll&&!rl&&r
004
MC/DC0%0/2
defined $v and length $v
defined $v length $v
Truth table33%1/3
A and B
A= defined $vB= length $v
ABresult
0X0
100
111
127 {
128 0 ▶ if( $v = $self->_set_get_param( 'title*' ) )
Branch: 1 branch
BranchTrueFalseTotal
if ($v = $self->_set_get_param("title*")) 0 0 0
129 {
130 0 my( $f_charset, $f_lang );
131 0 ( $v, $f_charset, $f_lang ) = $self->_filename_decode( $v );
132 0 $self->title_charset( $f_charset );
133 0 $self->title_lang( $f_lang );
134 }
135 }
136 4 return( $v );
137 }
138 }
139  
140 sub title_charset
141 {
142 0 ▶ my $self = shift( @_ );
Subroutine title_charset: not called CC=5 SCAR=34.0
Pod documented
143 0 ▶ if( @_ )
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 0 0 0
144 {
145 0 my $v = shift( @_ );
146 0 ▶ return( $self->error( "Only supported charset is 'utf-8'." ) ) if( lc( $v ) ne 'utf-8' && lc( $v ) ne 'utf8' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $v ne "utf-8" and lc $v ne "utf8" 0 0 0
Condition0%0/3
lc $v ne "utf-8" and lc $v ne "utf8"
!ll&&!rl&&r
000
MC/DC0%0/2
lc $v ne "utf-8" and lc $v ne "utf8"
lc $v ne "utf-8" lc $v ne "utf8"
Truth table0%0/3
A and B
A= lc $v ne "utf-8"B= lc $v ne "utf8"
ABresult
0X0
100
111
147 # Convenience
148 0 ▶ $v = 'utf-8' if( lc( $v ) eq 'utf8' );
Branch: 1 branch
BranchTrueFalseTotal
if lc $v eq "utf8" 0 0 0
149 0 $v = uc( $v );
150 0 return( $self->_set_get_scalar_as_object( 'title_charset', $v ) );
151 }
152 0 return( $self->_set_get_scalar_as_object( 'title_charset' ) );
153 }
154  
155 0 ▶sub title_lang { return( shift->_set_get_scalar_as_object( 'title_lang', @_ ) ); }
Subroutine title_lang: not called CC=1 SCAR=6.9
Pod documented
156  
157 3 1;
158 # NOTE: POD
159 __END__
160  
161 =encoding utf-8
162  
163 =head1 NAME
164  
165 HTTP::Promise::Headers::Link - Link Header Field
166  
167 =head1 SYNOPSIS
168  
169 use HTTP::Promise::Headers::Link;
170 my $link = HTTP::Promise::Headers::Link->new ||
171 die( HTTP::Promise::Headers::Link->error, "\n" );
172 my $uri = $link->link;
173 $link->link( 'https://example.org' );
174 $link->rel( 'preconnect' );
175 $h->link( "$link" );
176 # Link: <https://example.org>; rel="preconnect"
177 $link->title( 'Foo' );
178 $link->anchor( '#bar' );
179 $cd->params( rel => 'preconnect', anchor => 'bar' );
180  
181 =head1 VERSION
182  
183 v0.1.0
184  
185 =head1 DESCRIPTION
186  
187 The following is an extract from Mozilla documentation.
188  
189 The HTTP Link entity-header field provides a means for serializing one or more links in HTTP headers. It is semantically equivalent to the HTML C<link> element.
190  
191 Example:
192  
193 Link: <https://example.com>; rel="preconnect"; title="Foo"; anchor="#bar"
194  
195 =head1 METHODS
196  
197 =head2 anchor
198  
199 Sets or gets the C<anchor> property.
200  
201 =head2 as_string
202  
203 Returns a string representation of the C<Link> object.
204  
205 =head2 rel
206  
207 Sets or gets the C<relationship> of the C<Link> as a scalar.
208  
209 =head2 link
210  
211 Sets or gets an URI. It returns the URI value (not an object).
212  
213 When you set this value, it will be automatically surrounded by C<< <> >>
214  
215 =head2 param
216  
217 Sets or gets an arbitrary C<Link> property.
218  
219 Note that if you use this, you bypass other specialised method who do some additional processing, so be mindful.
220  
221 =head2 params
222  
223 Sets or gets multiple arbitrary C<Link> properties at once.
224  
225 If called without any arguments, this returns the L<hash object|Module::Generic::Hash> used to store the C<Link> properties.
226  
227 =head2 title
228  
229 Without any argument, this returns the string containing the original title of the link. The C<title> is always optional.
230  
231 If the property C<title*> is set instead, then it will be decoded and used instead, and the value for L</title_charset> and L</title_lang> will be set.
232  
233 When setting the title value, this takes an optional language iso 639 code (see L<rfc5987|https://tools.ietf.org/html/rfc5987> and L<rfc2231|https://tools.ietf.org/html/rfc2231>).
234 If the title contains non ascii characters, it will be automatically encoded according to L<rfc5987|https://tools.ietf.org/html/rfc5987>. and the property C<title*> set instead. That property, by rfc standard, takes precedence over the C<title> one.
235  
236 See L<rfc8288, section 3|https://tools.ietf.org/html/rfc8288#section-3> for more information.
237  
238 The language provided, if any, will be used then.
239  
240 For example:
241  
242 $h->link( 'https://www.example.com' );
243 $h->rel( 'preconnect' );
244 $h->title( q{Foo} );
245 say "$h";
246 # <https://www.example.com>; rel="preconnect"; title="Foo"
247  
248 $h->link( 'https://www.example.com' );
249 $h->rel( 'previous' );
250 $h->title( q{「お早う」小津安二郎} );
251 say "$h";
252 # https://www.example.com; rel="previous"; title*="UTF-8''%E3%81%8A%E6%97%A9%E3%81%86%E3%80%8D%E5%B0%8F%E6%B4%A5%E5%AE%89%E4%BA%8C%E9%83%8E"
253
254 $h->link( 'https://www.example.com' );
255 $h->rel( 'previous' );
256 $h->title( q{「お早う」小津安二郎}, 'ja-JP' );
257 say "$h";
258 # https://www.example.com; rel="previous"; title*="UTF-8'ja-JP'%E3%81%8A%E6%97%A9%E3%81%86%E3%80%8D%E5%B0%8F%E6%B4%A5%E5%AE%89%E4%BA%8C%E9%83%8E"
259  
260 # Using default value
261 $h->title_lang( 'ja-JP' );
262 $h->link( 'https://www.example.com' );
263 $h->rel( 'previous' );
264 $h->title( q{「お早う」小津安二郎}, 'ja-JP' );
265 say "$h";
266 # https://www.example.com; rel="previous"; title*="UTF-8'ja-JP'%E3%81%8A%E6%97%A9%E3%81%86%E3%80%8D%E5%B0%8F%E6%B4%A5%E5%AE%89%E4%BA%8C%E9%83%8E"
267  
268 $headers->header( Link => "$h" );
269  
270 The C<Link> header value would then contain a property C<title*> (with the trailing wildcard).
271  
272 =head2 title_charset
273  
274 Sets or gets the encoded title charset.
275  
276 This is used when the title contains non-ascii characters, such as Japanese, Korean, or Cyrillic.
277 Although theoretically one can set any character set, by design this only accepts C<UTF-8> (case insensitive).
278  
279 This is set automatically when calling L</title>. You actually need to call L</title> first to have a value set.
280  
281 Returns a L<scalar object|Module::Generic::Scalar> containing the title charset.
282  
283 =head2 title_lang
284  
285 Sets or gets the encoded title language. This takes an iso 639 language code (see L<rfc1766|https://tools.ietf.org/html/rfc1766>).
286  
287 This is set automatically when calling L</title>. You actually need to call L</title> first to have a value set.
288  
289 Returns a L<scalar object|Module::Generic::Scalar> containing the title language.
290  
291 =head1 THREAD-SAFETY
292  
293 This module is thread-safe for all operations, as it operates on per-object state and uses thread-safe external libraries.
294  
295 =head1 AUTHOR
296  
297 Jacques Deguest E<lt>F<jack@deguest.jp>E<gt>
298  
299 =head1 SEE ALSO
300  
301 See also L<rfc8288, section 3|https://tools.ietf.org/html/rfc8288#section-3> and L<Mozilla documentation|https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Link>
302  
303 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>
304  
305 =head1 COPYRIGHT & LICENSE
306  
307 Copyright(c) 2022 DEGUEST Pte. Ltd.
308  
309 All rights reserved.
310  
311 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
312  
313 =cut
« lib/HTTP/Promise/Pool.pm ↑ Summary lib/HTTP/Promise/Headers/ContentRange.pm »