WebSocket/HeaderValue.pm

filter: Statementstmt 22.1% 21 / 95 Branchbran 0.0% 0 / 40 Conditioncond 0.0% 0 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 40.0% 6 / 15 Podpod 100% 9 / 9 totaltotal 18.5% 36 / 194 CC 31 SCAR 42.2
CC
31
Cov
13%
new_from_header
132.0
as_string
110.0
new_from_multi
56.0
CRAP 68.3
« WebSocket/Request.pm ↑ Summary WebSocket/Handshake/Server.pm »
1 ##----------------------------------------------------------------------------
2 ## WebSocket Client & Server - ~/lib/WebSocket/HeaderValue.pm
3 ## Version v0.1.0
4 ## Copyright(c) 2021 DEGUEST Pte. Ltd.
5 ## Author: Jacques Deguest <jack@deguest.jp>
6 ## Created 2021/09/30
7 ## Modified 2021/09/30
8 ## You can use, copy, modify and redistribute this package and associated
9 ## files under the same terms as Perl itself.
10 ##----------------------------------------------------------------------------
11 package WebSocket::HeaderValue;
12 BEGIN
13 6 ▶{
Subroutine BEGIN: called CC=1 SCAR=0.0
14 6 ▶ use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 6 ▶ use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 6 ▶ use parent qw( Module::Generic );
Subroutine BEGIN: called CC=1 SCAR=0.0
17 6 ▶ use vars qw( $VERSION $QUOTE_REGEXP $TYPE_REGEXP $TOKEN_REGEXP $TEXT_REGEXP );
Subroutine BEGIN: called CC=1 SCAR=0.0
18 6 our $VERSION = 'v0.1.0';
19 6 ▶ use overload (
Subroutine BEGIN: called CC=1 SCAR=0.0
20 6 '""' => 'as_string',
21 fallback => 1,
22 6 );
23 6 our $QUOTE_REGEXP = qr/([\\"])/;
24 #
25 # RegExp to match type in RFC 7231 sec 3.1.1.1
26 #
27 # media-type = type "/" subtype
28 # type = token
29 # subtype = token
30 #
31 6 our $TYPE_REGEXP = qr/^[!#$%&'*+.^_`|~0-9A-Za-z-]+\/[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
32 6 our $TOKEN_REGEXP = qr/^[!#$%&'*+.^_`|~0-9A-Za-z-]+$/;
33 6 our $TEXT_REGEXP = qr/^[\u000b\u0020-\u007e\u0080-\u00ff]+$/;
34 };
35  
36 sub init
37 0 ▶{
Subroutine init: not called CC=3 SCAR=24.8
Pod documented
38 0 my $self = shift( @_ );
39 0 my $value = shift( @_ );
40 0 ▶ return( $self->error( "No value provided." ) ) if( !length( $value ) );
Branch: 1 branch
BranchTrueFalseTotal
unless length $value 0 0 0
41 0 $self->{original} = '';
42 0 $self->{value} = $value;
43 0 ▶ $self->SUPER::init( @_ ) || return( $self->pass_error );
Branch: 1 branch
BranchTrueFalseTotal
$self->SUPER::init(@_) or return $self->pass_error 0 0 0
44 0 $self->{params} = {};
45 0 return( $self );
46 }
47  
48 sub new_from_multi
49 0 ▶{
Subroutine new_from_multi: not called CC=7 SCAR=40.3
Pod documented
50 0 my $self = shift( @_ );
51 0 my $s = shift( @_ );
52 0 ▶ return( $self->error( 'Header value is required' ) ) if( !defined( $s ) || !length( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s 0 0 0
Condition0%0/3
defined $s and length $s
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s and length $s
defined $s length $s
Truth table0%0/3
A and B
A= defined $sB= length $s
ABresult
0X0
100
111
53 0 my @parts = ();
54 0 my $i = 0;
55 0 foreach( split( /(\\.)|\,/, $s ) )
56 {
57 0 ▶ defined( $_ ) ? do{ $parts[$i] .= $_ } : do{ $i++ };
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
58 }
59 0 my $res = $self->new_array;
60 0 ▶ for( my $j = 0; $j < scalar( @parts ); $j++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($j < scalar @parts) 0 0 0
61 {
62 0 my $o = $self->new_from_header( $parts[$j] );
63 0 ▶ return( $self->pass_error ) if( !defined( $o ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o 0 0 0
64 0 $res->push( $o );
65 }
66 0 return( $res );
67 }
68  
69 sub new_from_header
70 0 ▶{
Subroutine new_from_header: not called CC=11 SCAR=48.8
Pod documented
71 0 my $self = shift( @_ );
72 0 my $s = shift( @_ );
73 0 ▶ return( $self->error( 'Header value is required' ) ) if( !defined( $s ) || !length( $s ) );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s and length $s 0 0 0
Condition0%0/3
defined $s and length $s
!ll&&!rl&&r
000
MC/DC0%0/2
defined $s and length $s
defined $s length $s
Truth table0%0/3
A and B
A= defined $sB= length $s
ABresult
0X0
100
111
74 0 ▶ my $sep = @_ ? shift( @_ ) : ';';
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
75 0 my @parts = ();
76 0 my $i = 0;
77 0 foreach( split( /(\\.)|$sep/, $s ) )
78 {
79 0 ▶ defined( $_ ) ? do{ $parts[$i] .= $_ } : do{ $i++ };
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 0 0 0
80 }
81 0 my $header_val = shift( @parts );
82 0 my $obj = WebSocket::HeaderValue->new( $header_val );
83  
84 0 foreach my $frag ( @parts )
85 {
86 0 $frag =~ s/^[[:blank:]\h]+|[[:blank:]\h]+$//g;
87 0 my( $attribute, $value ) = split( /[[:blank:]\h]*\=[[:blank:]\h]*/, $frag, 2 );
88 0 $value =~ s/^\"|\"$//g;
89 # Check character string and length. Should not be more than 255 characters
90 # http://tools.ietf.org/html/rfc1341
91 # http://www.iana.org/assignments/media-types/media-types.xhtml
92 # Won't complain if this does not meet our requirement, but will discard it silently
93 0 ▶ if( $attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ && CORE::length( $attribute ) <= 255 )
Branch: 1 branch
BranchTrueFalseTotal
if ($attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ and length $attribute <= 255) 0 0 0
Condition0%0/3
$attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ and length $attribute <= 255
!ll&&!rl&&r
000
MC/DC0%0/2
$attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ and length $attribute <= 255
$attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ length $attribute <= 255
Truth table0%0/3
A and B
A= $attribute =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/B= length $attribute <= 255
ABresult
0X0
100
111
94 {
95 0 ▶ if( $value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ && CORE::length( $value ) <= 255 )
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ and length $value <= 255) 0 0 0
Condition0%0/3
$value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ and length $value <= 255
!ll&&!rl&&r
000
MC/DC0%0/2
$value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ and length $value <= 255
$value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/ length $value <= 255
Truth table0%0/3
A and B
A= $value =~ /^[a-zA-Z][a-zA-Z0-9\_\-]+$/B= length $value <= 255
ABresult
0X0
100
111
96 {
97 0 $obj->params( lc( $attribute ) => $value );
98 }
99 }
100 }
101 0 return( $obj );
102 }
103  
104 sub as_string
105 0 ▶{
Subroutine as_string: not called CC=10 SCAR=47.0
Pod documented
106 0 my $self = shift( @_ );
107 0 ▶ if( !defined( $self->{original} ) || !length( $self->{original} ) )
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"original"} and length $self->{"original"}) 0 0 0
Condition0%0/3
defined $self->{"original"} and length $self->{"original"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"original"} and length $self->{"original"}
defined $self->{"original"} length $self->{"original"}
Truth table0%0/3
A and B
A= defined $self->{"original"}B= length $self->{"original"}
ABresult
0X0
100
111
108 {
109 0 my $string = '';
110 0 ▶ if( defined( $self->{value} ) && length( $self->{value} ) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"value"} and length $self->{"value"}) 0 0 0
Condition0%0/3
defined $self->{"value"} and length $self->{"value"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $self->{"value"} and length $self->{"value"}
defined $self->{"value"} length $self->{"value"}
Truth table0%0/3
A and B
A= defined $self->{"value"}B= length $self->{"value"}
ABresult
0X0
100
111
111 {
112 0 ▶ if( $self->{value} !~ /^$TYPE_REGEXP$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->{"value"} =~ /^$TYPE_REGEXP$/) 0 0 0
113 {
114 0 return( $self->error( "Invalid value \"$self->{value}\"" ) );
115 }
116 0 $string = $self->{value};
117 }
118  
119 # Append parameters
120 # if( $self->{params} && ref( $self->{params} ) eq 'HASH' )
121 0 ▶ if( $self->params->length )
Branch: 1 branch
BranchTrueFalseTotal
if ($self->params->length) 0 0 0
122 {
123 0 my $params = $self->params->keys->sort;
124 0 ▶ for( my $i = 0; $i < scalar( @$params ); $i++ )
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @$params) 0 0 0
125 {
126 0 ▶ if( $params->[$i] !~ /^$TOKEN_REGEXP$/ )
Branch: 1 branch
BranchTrueFalseTotal
unless ($params->[$i] =~ /^$TOKEN_REGEXP$/) 0 0 0
127 {
128 0 return( $self->error( "Invalid parameter name: \"" . $params->[$i] . "\"" ) );
129 }
130 0 ▶ if( length( $string ) > 0 )
Branch: 1 branch
BranchTrueFalseTotal
if (length $string > 0) 0 0 0
131 {
132 0 $string .= '; ';
133 }
134 0 $string .= $params->[$i] . '=' . $self->qstring( $self->params->get( $params->[$i] ) );
135 }
136 }
137 0 $self->{original} = $string;
138 }
139 0 return( $self->{original} );
140 }
141  
142 0 ▶sub original { return( shift->_set_get_scalar_as_object( 'original', @_ ) ); }
Subroutine original: not called CC=1 SCAR=6.9
Pod documented
143  
144 0 ▶sub params { return( shift->_set_get_hash_as_mix_object( 'params', @_ ) ); }
Subroutine params: not called CC=1 SCAR=6.9
Pod documented
145  
146 sub qstring
147 0 ▶{
Subroutine qstring: not called CC=4 SCAR=30.0
Pod documented
148 0 my $self = shift( @_ );
149 0 my $str = shift( @_ );
150  
151 # no need to quote tokens
152 0 ▶ if( $str =~ /^$TOKEN_REGEXP$/ )
Branch: 1 branch
BranchTrueFalseTotal
if ($str =~ /^$TOKEN_REGEXP$/) 0 0 0
153 {
154 0 return( $str );
155 }
156  
157 0 ▶ if( length( $str ) > 0 && $str !~ /^$TEXT_REGEXP$/ )
Branch: 1 branch
BranchTrueFalseTotal
if (length $str > 0 and not $str =~ /^$TEXT_REGEXP$/) 0 0 0
Condition0%0/3
length $str > 0 and not $str =~ /^$TEXT_REGEXP$/
!ll&&!rl&&r
000
MC/DC0%0/2
length $str > 0 and not $str =~ /^$TEXT_REGEXP$/
length $str > 0 not $str =~ /^$TEXT_REGEXP$/
Truth table0%0/3
A and not(B)
A= length $str > 0B= not $str =~ /^$TEXT_REGEXP$/
ABresult
0X0
100
111
158 {
159 0 return( $self->error( 'Invalid parameter value' ) );
160 }
161
162 0 $str =~ s/$QUOTE_REGEXP/\\$1/g;
163 0 return( '"' . $str . '"' );
164 }
165  
166 sub reset
167 0 ▶{
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
168 0 my $self = shift( @_ );
169 0 $self->{original} = '';
170 0 return( $self );
171 }
172  
173 0 ▶sub value { return( shift->_set_get_scalar_as_object( 'value', @_ ) ); }
Subroutine value: not called CC=1 SCAR=6.9
Pod documented
174  
175 6 1;
176 # NOTE: POD
177 __END__
« WebSocket/Request.pm ↑ Summary WebSocket/Handshake/Server.pm »