| 1 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||
| 2 | ## WebSocket Client & Server - ~/lib/WebSocket/Message.pm | ||||||||||||||||||||||||||||||||||||||||
| 3 | ## Version v0.1.0 | ||||||||||||||||||||||||||||||||||||||||
| 4 | ## Copyright(c) 2021 DEGUEST Pte. Ltd. | ||||||||||||||||||||||||||||||||||||||||
| 5 | ## Author: Jacques Deguest <jack@deguest.jp> | ||||||||||||||||||||||||||||||||||||||||
| 6 | ## Created 2021/09/16 | ||||||||||||||||||||||||||||||||||||||||
| 7 | ## Modified 2021/09/16 | ||||||||||||||||||||||||||||||||||||||||
| 8 | ## All rights reserved | ||||||||||||||||||||||||||||||||||||||||
| 9 | ## | ||||||||||||||||||||||||||||||||||||||||
| 10 | ## This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||
| 11 | ## under the same terms as Perl itself. | ||||||||||||||||||||||||||||||||||||||||
| 12 | ##---------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||
| 13 | package WebSocket::Common; | ||||||||||||||||||||||||||||||||||||||||
| 14 | BEGIN | ||||||||||||||||||||||||||||||||||||||||
| 15 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 16 | 6 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 17 | 6 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 18 | 6 | ▶ | use vars qw( | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 19 | $MAX_MESSAGE_SIZE | ||||||||||||||||||||||||||||||||||||||||
| 20 | %EXPORT_TAGS @EXPORT_OK $VERSION | ||||||||||||||||||||||||||||||||||||||||
| 21 | 6 | ); | |||||||||||||||||||||||||||||||||||||||
| 22 | 6 | ▶ | use WebSocket qw( WEBSOCKET_DRAFT_VERSION_DEFAULT ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 23 | 6 | ▶ | use parent qw( WebSocket ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 24 | 6 | ▶ | use Digest::MD5 (); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 25 | 6 | ▶ | use HTTP::Status (); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 26 | 6 | ▶ | use MIME::Base64 (); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 27 | 6 | ▶ | use Scalar::Util qw( readonly ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 28 | 6 | ▶ | use WebSocket::Headers; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 29 | 6 | ▶ | use WebSocket::Version; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 30 | 6 | our $MAX_MESSAGE_SIZE = 10 * 2048; | |||||||||||||||||||||||||||||||||||||||
| 31 | # Either one is ok | ||||||||||||||||||||||||||||||||||||||||
| 32 | 6 | ▶ | use constant BYTES_RANDOM_SECURE => eval( "require Bytes::Random::Secure;" ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 33 | 6 | ▶ | use constant MATH_RANDOM_SECURE => eval( "require Math::Random::Secure;" ); | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 34 | # token is (RFC 2616, ASCII) | ||||||||||||||||||||||||||||||||||||||||
| 35 | # our $HEADER_TOKEN = qr/[\x21\x23-\x27\x2a\x2b\x2d\x2e\x30-\x39\x41-\x5a\x5e-\x7a\x7c\x7e]+/; | ||||||||||||||||||||||||||||||||||||||||
| 36 | 6 | ▶ | use constant PARSE_DONE => 0; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 37 | 6 | ▶ | use constant PARSE_INCOMPLETE => -1; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 38 | 6 | ▶ | use constant PARSE_WAITING => -2; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 39 | 6 | ▶ | use constant PARSE_MAYBE_MORE => -3; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 40 | 6 | our %EXPORT_TAGS = ( all => [qw( PARSE_DONE PARSE_INCOMPLETE PARSE_WAITING PARSE_MAYBE_MORE WEBSOCKET_DRAFT_VERSION_DEFAULT )] ); | |||||||||||||||||||||||||||||||||||||||
| 41 | 6 | our @EXPORT_OK = qw( PARSE_DONE PARSE_INCOMPLETE PARSE_WAITING PARSE_MAYBE_MORE WEBSOCKET_DRAFT_VERSION_DEFAULT ); | |||||||||||||||||||||||||||||||||||||||
| 42 | 6 | our $VERSION = 'v0.1.0'; | |||||||||||||||||||||||||||||||||||||||
| 43 | }; | ||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||
| 45 | INIT | ||||||||||||||||||||||||||||||||||||||||
| 46 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
INIT: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 47 | 4 | require WebSocket::Request; | |||||||||||||||||||||||||||||||||||||||
| 48 | 4 | require WebSocket::Response; | |||||||||||||||||||||||||||||||||||||||
| 49 | }; | ||||||||||||||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||||||||||||||
| 51 | sub init | ||||||||||||||||||||||||||||||||||||||||
| 52 | 22 | ▶ | { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
init: called CC=27 SCAR=39.7
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 53 | 22 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||
| 54 | # There is no method for this. We use this as internal buffer | ||||||||||||||||||||||||||||||||||||||||
| 55 | 22 | ▶ | $self->{buffer} = '' unless( length( $self->{buffer} // '' ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"buffer"} // ""
MC/DC0%0/1
$self->{"buffer"} // ""
$self->{"buffer"}
Truth table50%1/2
A
A= $self->{"buffer"}
| |||||||||||||||||||||||||||||||||||||||||
| 56 | 22 | ▶ | $self->{checksum} = undef unless( length( $self->{checksum} // '' ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"checksum"} // ""
MC/DC0%0/1
$self->{"checksum"} // ""
$self->{"checksum"}
Truth table50%1/2
A
A= $self->{"checksum"}
| |||||||||||||||||||||||||||||||||||||||||
| 57 | 22 | ▶ | $self->{cookies} = undef unless( length( $self->{cookies} // '' ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->{"cookies"} // ""
MC/DC100%1/1
$self->{"cookies"} // ""
$self->{"cookies"}
Truth table100%2/2
A
A= $self->{"cookies"}
| |||||||||||||||||||||||||||||||||||||||||
| 58 | 22 | ▶ | $self->{extensions} = [] unless( exists( $self->{extensions} ) && length( $self->{extensions} ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
exists $self->{"extensions"} and length $self->{"extensions"}
MC/DC0%0/2
exists $self->{"extensions"} and length $self->{"extensions"}
exists $self->{"extensions"}
length $self->{"extensions"}
Truth table33%1/3
A and B
A= exists $self->{"extensions"}B= length $self->{"extensions"}
| |||||||||||||||||||||||||||||||||||||||||
| 59 | 22 | ▶ | $self->{max_message_size} = $MAX_MESSAGE_SIZE unless( length( $self->{max_message_size} // '' ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"max_message_size"} // ""
MC/DC0%0/1
$self->{"max_message_size"} // ""
$self->{"max_message_size"}
Truth table50%1/2
A
A= $self->{"max_message_size"}
| |||||||||||||||||||||||||||||||||||||||||
| 60 | 22 | ▶ | $self->{protocol} = '' unless( length( $self->{protocol} // '' ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"protocol"} // ""
MC/DC0%0/1
$self->{"protocol"} // ""
$self->{"protocol"}
Truth table50%1/2
A
A= $self->{"protocol"}
| |||||||||||||||||||||||||||||||||||||||||
| 61 | # Chunk parser | ||||||||||||||||||||||||||||||||||||||||
| 62 | 22 | $self->{state} = 'blank'; | |||||||||||||||||||||||||||||||||||||||
| 63 | # <https://datatracker.ietf.org/doc/html/rfc6455#section-1.9> | ||||||||||||||||||||||||||||||||||||||||
| 64 | 22 | $self->{subprotocol} = []; | |||||||||||||||||||||||||||||||||||||||
| 65 | 22 | ▶ | $self->{uri} = '' unless( length( $self->{uri} // '' ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$self->{"uri"} // ""
MC/DC0%0/1
$self->{"uri"} // ""
$self->{"uri"}
Truth table50%1/2
A
A= $self->{"uri"}
| |||||||||||||||||||||||||||||||||||||||||
| 66 | # $self->{version} = 'draft-ietf-hybi-17' unless( length( $self->{version} ) ); | ||||||||||||||||||||||||||||||||||||||||
| 67 | 22 | ▶ | $self->{version} = '' unless( length( $self->{version} // '' ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%2/2
$self->{"version"} // ""
MC/DC100%1/1
$self->{"version"} // ""
$self->{"version"}
Truth table100%2/2
A
A= $self->{"version"}
| |||||||||||||||||||||||||||||||||||||||||
| 68 | 22 | ▶ | $self->{_exception_class} = 'WebSocket::Exception' unless( defined( $self->{_exception_class} ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 69 | 22 | $self->{_init_strict_use_sub} = 1; | |||||||||||||||||||||||||||||||||||||||
| 70 | 22 | ▶ | $self->SUPER::init( @_ ) || return( $self->pass_error ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 71 | 22 | my $headers = $self->headers; | |||||||||||||||||||||||||||||||||||||||
| 72 | 22 | ▶ | unless( $self->version ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 73 | { | ||||||||||||||||||||||||||||||||||||||||
| 74 | 22 | ▶ | if( $headers->header( 'Sec-WebSocket-Version' )->length && | ||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
Condition66%2/3
$headers->header("Sec-WebSocket-Version")->length and $headers->header("Sec-WebSocket-Version")->match(qr/^\d{1,2}$/)
MC/DC0%0/2
$headers->header("Sec-WebSocket-Version")->length and $headers->header("Sec-WebSocket-Version")->match(qr/^\d{1,2}$/)
$headers->header("Sec-WebSocket-Version")->length
$headers->header("Sec-WebSocket-Version")->match(qr/^\d{1,2}$/)
Truth table33%1/3
A and B
A= $headers->header("Sec-WebSocket-Version")->lengthB= $headers->header("Sec-WebSocket-Version")->match(qr/^\d{1,2}$/)
| |||||||||||||||||||||||||||||||||||||||||
| 75 | $headers->header( 'Sec-WebSocket-Version' )->match( qr/^\d{1,2}$/ ) ) | ||||||||||||||||||||||||||||||||||||||||
| 76 | { | ||||||||||||||||||||||||||||||||||||||||
| 77 | 1 | ▶ | my $v = WebSocket::Version->new( $headers->header( 'Sec-WebSocket-Version' )->scalar ) || | ||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
'WebSocket::Version'->new($headers->header('Sec-WebSocket-Version')->scalar) || (return $self->pass_error('WebSocket::Version'->error))
MC/DC0%0/1
'WebSocket::Version'->new($headers->header('Sec-WebSocket-Version')->scalar) || (return $self->pass_error('WebSocket::Version'->error))
'WebSocket::Version'->new($headers->header('Sec-WebSocket-Version')->scalar)
Truth table50%1/2
A
A= 'WebSocket::Version'->new($headers->header('Sec-WebSocket-Version')->scalar)
| |||||||||||||||||||||||||||||||||||||||||
| 78 | return( $self->pass_error( WebSocket::Version->error ) ); | ||||||||||||||||||||||||||||||||||||||||
| 79 | 1 | $self->version( $v ); | |||||||||||||||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||||||||||||||
| 81 | ▶ | elsif( $headers->header( 'WebSocket-Version' )->length && | |||||||||||||||||||||||||||||||||||||||
|
Condition33%1/3
$headers->header("WebSocket-Version")->length and $headers->header("WebSocket-Version")->match(qr/^\d{1,2}$/)
MC/DC0%0/2
$headers->header("WebSocket-Version")->length and $headers->header("WebSocket-Version")->match(qr/^\d{1,2}$/)
$headers->header("WebSocket-Version")->length
$headers->header("WebSocket-Version")->match(qr/^\d{1,2}$/)
Truth table33%1/3
A and B
A= $headers->header("WebSocket-Version")->lengthB= $headers->header("WebSocket-Version")->match(qr/^\d{1,2}$/)
| |||||||||||||||||||||||||||||||||||||||||
| 82 | $headers->header( 'WebSocket-Version' )->match( qr/^\d{1,2}$/ ) ) | ||||||||||||||||||||||||||||||||||||||||
| 83 | { | ||||||||||||||||||||||||||||||||||||||||
| 84 | 0 | ▶ | my $v = WebSocket::Version->new( $headers->header( 'WebSocket-Version' )->scalar ) || | ||||||||||||||||||||||||||||||||||||||
|
Condition0%0/2
'WebSocket::Version'->new($headers->header('WebSocket-Version')->scalar) || (return $self->pass_error('WebSocket::Version'->error))
MC/DC0%0/1
'WebSocket::Version'->new($headers->header('WebSocket-Version')->scalar) || (return $self->pass_error('WebSocket::Version'->error))
'WebSocket::Version'->new($headers->header('WebSocket-Version')->scalar)
Truth table0%0/2
A
A= 'WebSocket::Version'->new($headers->header('WebSocket-Version')->scalar)
| |||||||||||||||||||||||||||||||||||||||||
| 85 | return( $self->pass_error( WebSocket::Version->error ) ); | ||||||||||||||||||||||||||||||||||||||||
| 86 | 0 | $self->version( $v ); | |||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||
| 88 | else | ||||||||||||||||||||||||||||||||||||||||
| 89 | { | ||||||||||||||||||||||||||||||||||||||||
| 90 | 21 | ▶ | my $v = WebSocket::Version->new( WEBSOCKET_DRAFT_VERSION_DEFAULT ) || | ||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
'WebSocket::Version'->new('draft-ietf-hybi-17') || (return $self->pass_error('WebSocket::Version'->error))
MC/DC0%0/1
'WebSocket::Version'->new('draft-ietf-hybi-17') || (return $self->pass_error('WebSocket::Version'->error))
'WebSocket::Version'->new('draft-ietf-hybi-17')
Truth table50%1/2
A
A= 'WebSocket::Version'->new('draft-ietf-hybi-17')
| |||||||||||||||||||||||||||||||||||||||||
| 91 | return( $self->pass_error( WebSocket::Version->error ) ); | ||||||||||||||||||||||||||||||||||||||||
| 92 | 21 | $self->version( $v ); | |||||||||||||||||||||||||||||||||||||||
| 93 | } | ||||||||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||||||
| 95 | 22 | return( $self ); | |||||||||||||||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||||||||||||||
| 97 | |||||||||||||||||||||||||||||||||||||||||
| 98 | 81 | ▶ | sub buffer { return( shift->_set_get_scalar_as_object( 'buffer', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
buffer: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||
| 100 | 0 | ▶ | sub challenge { return( shift->_set_get_scalar_as_object( 'challenge', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
challenge: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||||
| 102 | sub checksum | ||||||||||||||||||||||||||||||||||||||||
| 103 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
checksum: not called CC=10 SCAR=47.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 104 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||
| 105 | 0 | ▶ | if( @_ ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 106 | { | ||||||||||||||||||||||||||||||||||||||||
| 107 | 0 | $self->{checksum} = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||
| 108 | 0 | return( $self ); | |||||||||||||||||||||||||||||||||||||||
| 109 | } | ||||||||||||||||||||||||||||||||||||||||
| 110 | 0 | ▶ | return( $self->{checksum} ) if( defined( $self->{checksum} ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 111 | 0 | ▶ | return( $self->error( "number1 is required" ) ) unless( defined( $self->number1 ) && length( $self->number1 ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->number1 and length $self->number1
MC/DC0%0/2
defined $self->number1 and length $self->number1
defined $self->number1
length $self->number1
Truth table0%0/3
A and B
A= defined $self->number1B= length $self->number1
| |||||||||||||||||||||||||||||||||||||||||
| 112 | 0 | ▶ | return( $self->error( "number2 is required" ) ) unless( defined( $self->number2 ) && length( $self->number2 ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->number2 and length $self->number2
MC/DC0%0/2
defined $self->number2 and length $self->number2
defined $self->number2
length $self->number2
Truth table0%0/3
A and B
A= defined $self->number2B= length $self->number2
| |||||||||||||||||||||||||||||||||||||||||
| 113 | 0 | ▶ | return( $self->error( "challenge is required" ) ) unless( defined( $self->challenge ) && length( $self->challenge ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $self->challenge and length $self->challenge
MC/DC0%0/2
defined $self->challenge and length $self->challenge
defined $self->challenge
length $self->challenge
Truth table0%0/3
A and B
A= defined $self->challengeB= length $self->challenge
| |||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||
| 115 | 0 | my $checksum = ''; | |||||||||||||||||||||||||||||||||||||||
| 116 | 0 | $checksum .= pack( 'N' => $self->number1 ); | |||||||||||||||||||||||||||||||||||||||
| 117 | 0 | $checksum .= pack( 'N' => $self->number2 ); | |||||||||||||||||||||||||||||||||||||||
| 118 | 0 | $checksum .= $self->challenge; | |||||||||||||||||||||||||||||||||||||||
| 119 | 0 | $checksum = Digest::MD5::md5( $checksum ); | |||||||||||||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||||||||||||||
| 121 | 0 | ▶ | return( $self->{checksum} ||= $checksum ); | ||||||||||||||||||||||||||||||||||||||
|
Condition0%0/3
$self->{"checksum"} ||= $checksum
MC/DC0%0/2
$self->{"checksum"} ||= $checksum
$self->{"checksum"}
$checksum
Truth table0%0/3
A ||= B
A= $self->{"checksum"}B= $checksum
| |||||||||||||||||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||
| 124 | # Alias | ||||||||||||||||||||||||||||||||||||||||
| 125 | 6 | ▶ | sub content { return( shift->buffer( @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
content: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub extensions { return( shift->_set_get_object_array_object( 'extensions', 'WebSocket::Extension', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
extensions: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||
| 129 | sub headers | ||||||||||||||||||||||||||||||||||||||||
| 130 | 75 | ▶ | { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
headers: called CC=5 SCAR=17.1
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 131 | 75 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||
| 132 | 75 | ▶ | if( @_ ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||
| 133 | { | ||||||||||||||||||||||||||||||||||||||||
| 134 | 2 | my $v = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||
| 135 | 2 | ▶ | if( $self->_is_a( $v, 'HTTP::Headers' ) ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||
| 136 | { | ||||||||||||||||||||||||||||||||||||||||
| 137 | 2 | my $h = $v->clone; | |||||||||||||||||||||||||||||||||||||||
| 138 | 2 | $self->{headers} = bless( $h => 'WebSocket::Headers' ); | |||||||||||||||||||||||||||||||||||||||
| 139 | } | ||||||||||||||||||||||||||||||||||||||||
| 140 | elsif( $self->_is_array( $v ) ) | ||||||||||||||||||||||||||||||||||||||||
| 141 | { | ||||||||||||||||||||||||||||||||||||||||
| 142 | 0 | $self->{headers} = WebSocket::Headers->new( @$v ); | |||||||||||||||||||||||||||||||||||||||
| 143 | } | ||||||||||||||||||||||||||||||||||||||||
| 144 | else | ||||||||||||||||||||||||||||||||||||||||
| 145 | { | ||||||||||||||||||||||||||||||||||||||||
| 146 | 0 | return( $self->error( "Bad value for headers. I was expecting either an array reference or a HTTP::Headers object (including WebSocket::Headers) and I got instead '", overload::StrVal( $v ), "'." ) ); | |||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||
| 148 | } | ||||||||||||||||||||||||||||||||||||||||
| 149 | elsif( !$self->{headers} ) | ||||||||||||||||||||||||||||||||||||||||
| 150 | { | ||||||||||||||||||||||||||||||||||||||||
| 151 | 14 | $self->{headers} = WebSocket::Headers->new; | |||||||||||||||||||||||||||||||||||||||
| 152 | } | ||||||||||||||||||||||||||||||||||||||||
| 153 | 75 | return( $self->{headers} ); | |||||||||||||||||||||||||||||||||||||||
| 154 | } | ||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||
| 156 | 0 | ▶ | sub headers_as_string { return( shift->headers->as_string( @_ ) ) }; | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
headers_as_string: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||
| 158 | 15 | ▶ | sub host { return( shift->headers->header( 'Host', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
host: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub is_client_error { return( HTTP::Status::is_client_error( $_[1] ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_client_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||||||||||||||
| 162 | 7 | ▶ | sub is_done { return( shift->_set_get_boolean( 'is_done', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_done: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||
| 164 | 0 | ▶ | sub is_error { return( HTTP::Status::is_error( $_[1] ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 165 | |||||||||||||||||||||||||||||||||||||||||
| 166 | 0 | ▶ | sub is_redirect { return( HTTP::Status::is_redirect( $_[1] ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_redirect: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||
| 168 | 0 | ▶ | sub is_server_error { return( HTTP::Status::is_server_error( $_[1] ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_server_error: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||
| 170 | 0 | ▶ | sub is_success { return( HTTP::Status::is_success( $_[1] ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
is_success: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 171 | |||||||||||||||||||||||||||||||||||||||||
| 172 | 6 | ▶ | sub max_message_size { return( shift->_set_get_number( 'max_message_size', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
max_message_size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 173 | |||||||||||||||||||||||||||||||||||||||||
| 174 | 0 | ▶ | sub number1 { return( shift->_set_get_scalar( 'number1', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
number1: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||
| 176 | 0 | ▶ | sub number2 { return( shift->_set_get_scalar( 'number2', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
number2: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||
| 178 | sub parse_chunk | ||||||||||||||||||||||||||||||||||||||||
| 179 | 15 | ▶ | { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_chunk: called CC=9 SCAR=24.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 180 | 15 | my( $self, $s ) = @_; | |||||||||||||||||||||||||||||||||||||||
| 181 | 15 | ▶ | $s = '' if( !defined( $s ) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 182 | 15 | $self->buffer->append( $s ); | |||||||||||||||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||||||||||||||
| 184 | # pre-header blank lines are allowed (RFC 2616 4.1) | ||||||||||||||||||||||||||||||||||||||||
| 185 | 15 | ▶ | if( $self->{state} eq 'blank' ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 186 | { | ||||||||||||||||||||||||||||||||||||||||
| 187 | 4 | $self->buffer->replace( qr/^(\x0d?\x0a)+/, '' ); | |||||||||||||||||||||||||||||||||||||||
| 188 | 4 | ▶ | return( PARSE_WAITING ) unless( $self->buffer->length ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 189 | # done with blank lines; fall through | ||||||||||||||||||||||||||||||||||||||||
| 190 | 3 | $self->{state} = 'header'; | |||||||||||||||||||||||||||||||||||||||
| 191 | } | ||||||||||||||||||||||||||||||||||||||||
| 192 | |||||||||||||||||||||||||||||||||||||||||
| 193 | # still waiting for the header | ||||||||||||||||||||||||||||||||||||||||
| 194 | 14 | ▶ | if( $self->{state} eq 'header' ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||
| 195 | { | ||||||||||||||||||||||||||||||||||||||||
| 196 | # double line break indicates end of header; parse it | ||||||||||||||||||||||||||||||||||||||||
| 197 | 13 | ▶ | if( my $re = $self->buffer->match( qr/^(.*?)\x0d?\x0a\x0d?\x0a/s ) ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 198 | { | ||||||||||||||||||||||||||||||||||||||||
| 199 | 3 | return( $self->_parse_header_chunk( length( $re->capture->first ) ) ); | |||||||||||||||||||||||||||||||||||||||
| 200 | } | ||||||||||||||||||||||||||||||||||||||||
| 201 | # still waiting for unknown amount of header lines | ||||||||||||||||||||||||||||||||||||||||
| 202 | 10 | return( PARSE_WAITING ); | |||||||||||||||||||||||||||||||||||||||
| 203 | } | ||||||||||||||||||||||||||||||||||||||||
| 204 | # waiting for main body of request | ||||||||||||||||||||||||||||||||||||||||
| 205 | elsif( $self->{state} eq 'body' ) | ||||||||||||||||||||||||||||||||||||||||
| 206 | { | ||||||||||||||||||||||||||||||||||||||||
| 207 | 1 | return( $self->_parse_body_chunk() ); | |||||||||||||||||||||||||||||||||||||||
| 208 | } | ||||||||||||||||||||||||||||||||||||||||
| 209 | # chunked data | ||||||||||||||||||||||||||||||||||||||||
| 210 | # elsif( $self->{state} eq 'chunked' ) | ||||||||||||||||||||||||||||||||||||||||
| 211 | # { | ||||||||||||||||||||||||||||||||||||||||
| 212 | # return( $self->_parse_chunk() ); | ||||||||||||||||||||||||||||||||||||||||
| 213 | # } | ||||||||||||||||||||||||||||||||||||||||
| 214 | # trailers | ||||||||||||||||||||||||||||||||||||||||
| 215 | elsif( $self->{state} eq 'trailer' ) | ||||||||||||||||||||||||||||||||||||||||
| 216 | { | ||||||||||||||||||||||||||||||||||||||||
| 217 | # double line break indicates end of trailer; parse it | ||||||||||||||||||||||||||||||||||||||||
| 218 | 0 | ▶ | if( my $re = $self->buffer->match( qr/^(.*?)\x0d?\x0a\x0d?\x0a/s ) ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 219 | { | ||||||||||||||||||||||||||||||||||||||||
| 220 | 0 | return( $self->_parse_header_chunk( length( $re->capture->first ), 1 ) ); | |||||||||||||||||||||||||||||||||||||||
| 221 | } | ||||||||||||||||||||||||||||||||||||||||
| 222 | # still waiting for unknown amount of trailer data | ||||||||||||||||||||||||||||||||||||||||
| 223 | 0 | return( PARSE_INCOMPLETE ); | |||||||||||||||||||||||||||||||||||||||
| 224 | } | ||||||||||||||||||||||||||||||||||||||||
| 225 | 0 | return( $self->error( "Unknown state '$self->{state}'" ) ); | |||||||||||||||||||||||||||||||||||||||
| 226 | } | ||||||||||||||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||||||||||||||
| 228 | # e.g. HTTP/1.1 | ||||||||||||||||||||||||||||||||||||||||
| 229 | 2 | ▶ | sub protocol { return( shift->_set_get_scalar_as_object( 'protocol', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
protocol: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||
| 231 | 4 | ▶ | sub request { return( shift->_set_get_object( 'request', 'WebSocket::Request', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
request: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||
| 233 | 2 | ▶ | sub response { return( shift->_set_get_object( 'response', 'WebSocket::Response', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
response: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||
| 235 | 2 | ▶ | sub secure { return( shift->_set_get_boolean( 'secure', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
secure: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 236 | |||||||||||||||||||||||||||||||||||||||||
| 237 | 0 | ▶ | sub status_message { return( HTTP::Status::status_message( $_[1] ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
status_message: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||
| 239 | # sub subprotocol { return( shift->_set_get_array_as_object( 'subprotocol', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||||
| 240 | sub subprotocol | ||||||||||||||||||||||||||||||||||||||||
| 241 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
subprotocol: called CC=8 SCAR=22.8
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 242 | 6 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||
| 243 | 6 | ▶ | if( @_ ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 244 | { | ||||||||||||||||||||||||||||||||||||||||
| 245 | 2 | ▶ | my $ref = $self->_is_array( $_[0] ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 4 branches
Condition33%1/3
$self->_is_object($_[0]) and overload::Method($_[0], "\"\"")
MC/DC0%0/2
$self->_is_object($_[0]) and overload::Method($_[0], "\"\"")
$self->_is_object($_[0])
overload::Method($_[0], "\"\"")
Truth table33%1/3
A and B
A= $self->_is_object($_[0])B= overload::Method($_[0], "\"\"")
| |||||||||||||||||||||||||||||||||||||||||
| 246 | ? shift( @_ ) | ||||||||||||||||||||||||||||||||||||||||
| 247 | : @_ == 1 | ||||||||||||||||||||||||||||||||||||||||
| 248 | ? ( $self->_is_object( $_[0] ) && overload::Method( $_[0], '""' ) ) | ||||||||||||||||||||||||||||||||||||||||
| 249 | ? [CORE::split( /[[:blank:]\h]+/, "$_[0]" )] | ||||||||||||||||||||||||||||||||||||||||
| 250 | : ref( $_[0] ) | ||||||||||||||||||||||||||||||||||||||||
| 251 | ? shift( @_ ) | ||||||||||||||||||||||||||||||||||||||||
| 252 | : [CORE::split( /[[:blank:]\h]+/, $_[0] )] | ||||||||||||||||||||||||||||||||||||||||
| 253 | : [@_]; | ||||||||||||||||||||||||||||||||||||||||
| 254 | 2 | ▶ | my $v = $self->_set_get_array_as_object( 'subprotocol', $ref ) || return( $self->pass_error ); | ||||||||||||||||||||||||||||||||||||||
|
Condition50%1/2
$self->_set_get_array_as_object('subprotocol', $ref) || (return $self->pass_error)
MC/DC0%0/1
$self->_set_get_array_as_object('subprotocol', $ref) || (return $self->pass_error)
$self->_set_get_array_as_object('subprotocol', $ref)
Truth table50%1/2
A
A= $self->_set_get_array_as_object('subprotocol', $ref)
| |||||||||||||||||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||||||||||||||
| 256 | 6 | return( $self->_set_get_array_as_object( 'subprotocol' ) ); | |||||||||||||||||||||||||||||||||||||||
| 257 | } | ||||||||||||||||||||||||||||||||||||||||
| 258 | |||||||||||||||||||||||||||||||||||||||||
| 259 | 18 | ▶ | sub uri { return( shift->_set_get_uri( 'uri', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
uri: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 260 | |||||||||||||||||||||||||||||||||||||||||
| 261 | 39 | ▶ | sub version { return( shift->_set_get_object_without_init( 'version', 'WebSocket::Version', @_ ) ); } | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
version: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||||||||||||||
| 263 | sub _append | ||||||||||||||||||||||||||||||||||||||||
| 264 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_append: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||||||||||||||
| 265 | 6 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||
| 266 | 6 | ▶ | return( $self->pass_error ) if( $self->error ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||||||||||||||
| 268 | 6 | ▶ | if( $self->_is_object( $_[0] ) && $_[0]->can( 'read' ) ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$self->_is_object($_[0]) and $_[0]->can("read")
MC/DC0%0/2
$self->_is_object($_[0]) and $_[0]->can("read")
$self->_is_object($_[0])
$_[0]->can("read")
Truth table33%1/3
A and B
A= $self->_is_object($_[0])B= $_[0]->can("read")
| |||||||||||||||||||||||||||||||||||||||||
| 269 | { | ||||||||||||||||||||||||||||||||||||||||
| 270 | 0 | $_[0]->read( my $buf, $self->max_message_size ); | |||||||||||||||||||||||||||||||||||||||
| 271 | 0 | $self->buffer->append( $buf ); | |||||||||||||||||||||||||||||||||||||||
| 272 | } | ||||||||||||||||||||||||||||||||||||||||
| 273 | else | ||||||||||||||||||||||||||||||||||||||||
| 274 | { | ||||||||||||||||||||||||||||||||||||||||
| 275 | 6 | $self->buffer->append( $_[0] ); | |||||||||||||||||||||||||||||||||||||||
| 276 | # NOTE: Emptying implicitly the caller's variable passed is very bad design | ||||||||||||||||||||||||||||||||||||||||
| 277 | # It is up to the caller to decide of the content of its variable, not us. | ||||||||||||||||||||||||||||||||||||||||
| 278 | # This makes for head-scratching troubles and is a way of doing never used in perl | ||||||||||||||||||||||||||||||||||||||||
| 279 | # The only time when the caller's variable content is modified is with perl's read() or sysread() | ||||||||||||||||||||||||||||||||||||||||
| 280 | # and this is an explicit, well documented and optional feature | ||||||||||||||||||||||||||||||||||||||||
| 281 | # $_[0] = '' unless( readonly( $_[0] ) ); | ||||||||||||||||||||||||||||||||||||||||
| 282 | } | ||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||
| 284 | 6 | ▶ | if( $self->buffer->length > $self->max_message_size ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 285 | { | ||||||||||||||||||||||||||||||||||||||||
| 286 | 2 | return( $self->error( "Message is too long" ) ); | |||||||||||||||||||||||||||||||||||||||
| 287 | } | ||||||||||||||||||||||||||||||||||||||||
| 288 | 4 | return( $self ); | |||||||||||||||||||||||||||||||||||||||
| 289 | } | ||||||||||||||||||||||||||||||||||||||||
| 290 | |||||||||||||||||||||||||||||||||||||||||
| 291 | sub _extract_number | ||||||||||||||||||||||||||||||||||||||||
| 292 | 0 | ▶ | { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_extract_number: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||||||||||||||
| 293 | 0 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||
| 294 | 0 | my $key = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||
| 295 | 0 | my $number = join( '' => $key =~ m/\d+/g ); | |||||||||||||||||||||||||||||||||||||||
| 296 | 0 | my $spaces = $key =~ s/ / /g; | |||||||||||||||||||||||||||||||||||||||
| 297 | 0 | ▶ | return if( $spaces == 0 ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 298 | 0 | return( int( $number / $spaces ) ); | |||||||||||||||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||
| 301 | sub _generate_16bit_nonce | ||||||||||||||||||||||||||||||||||||||||
| 302 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_generate_16bit_nonce: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 303 | 1 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||
| 304 | 1 | my $rand = ''; | |||||||||||||||||||||||||||||||||||||||
| 305 | 1 | if( BYTES_RANDOM_SECURE ) | |||||||||||||||||||||||||||||||||||||||
| 306 | { | ||||||||||||||||||||||||||||||||||||||||
| 307 | 1 | $rand = Bytes::Random::Secure::random_bytes_base64(16, ''); | |||||||||||||||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||||||||||||||
| 309 | elsif( MATH_RANDOM_SECURE ) | ||||||||||||||||||||||||||||||||||||||||
| 310 | { | ||||||||||||||||||||||||||||||||||||||||
| 311 | $rand .= chr( Math::Random::Secure::irand(256) ) for( 1 .. 16 ); | ||||||||||||||||||||||||||||||||||||||||
| 312 | $rand = MIME::Base64::encode_base64( $rand, '' ); | ||||||||||||||||||||||||||||||||||||||||
| 313 | } | ||||||||||||||||||||||||||||||||||||||||
| 314 | else | ||||||||||||||||||||||||||||||||||||||||
| 315 | { | ||||||||||||||||||||||||||||||||||||||||
| 316 | $rand .= chr( int( rand(256) ) ) for( 1 .. 16 ); | ||||||||||||||||||||||||||||||||||||||||
| 317 | $rand = MIME::Base64::encode_base64( $rand, '' ); | ||||||||||||||||||||||||||||||||||||||||
| 318 | } | ||||||||||||||||||||||||||||||||||||||||
| 319 | 1 | return( $rand ); | |||||||||||||||||||||||||||||||||||||||
| 320 | } | ||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||
| 322 | sub _parse_body_chunk | ||||||||||||||||||||||||||||||||||||||||
| 323 | 4 | ▶ | { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_body_chunk: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||
| 324 | 4 | my $self = shift( @_ ); | |||||||||||||||||||||||||||||||||||||||
| 325 | 4 | $self->{obj}->content( $self->buffer->scalar ); | |||||||||||||||||||||||||||||||||||||||
| 326 | # $self->{buffer} = ''; | ||||||||||||||||||||||||||||||||||||||||
| 327 | 4 | $self->buffer->empty; | |||||||||||||||||||||||||||||||||||||||
| 328 | # return( $self->{obj} ); | ||||||||||||||||||||||||||||||||||||||||
| 329 | 4 | return( PARSE_DONE ); | |||||||||||||||||||||||||||||||||||||||
| 330 | } | ||||||||||||||||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||||||||||||||
| 332 | sub _parse_header_chunk | ||||||||||||||||||||||||||||||||||||||||
| 333 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||
|
Subroutine
_parse_header_chunk: called CC=13 SCAR=27.8
| |||||||||||||||||||||||||||||||||||||||||
| 334 | 3 | my( $self, $eoh, $trailer ) = @_; | |||||||||||||||||||||||||||||||||||||||
| 335 | 3 | my $header = $self->buffer->substr( 0, $eoh, '' ); | |||||||||||||||||||||||||||||||||||||||
| 336 | 3 | $self->buffer->replace( qr/^\x0d?\x0a\x0d?\x0a/, '' ); | |||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||
| 338 | # parse into lines | ||||||||||||||||||||||||||||||||||||||||
| 339 | 3 | my $headers = $header->split( qr/\x0d?\x0a/ ); | |||||||||||||||||||||||||||||||||||||||
| 340 | 3 | ▶ | my $request = $headers->shift unless( $trailer ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 341 | |||||||||||||||||||||||||||||||||||||||||
| 342 | # join folded lines | ||||||||||||||||||||||||||||||||||||||||
| 343 | 3 | my @out; | |||||||||||||||||||||||||||||||||||||||
| 344 | 3 | for( @$headers ) | |||||||||||||||||||||||||||||||||||||||
| 345 | { | ||||||||||||||||||||||||||||||||||||||||
| 346 | 8 | ▶ | if( s/^[ \t]+// ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 347 | { | ||||||||||||||||||||||||||||||||||||||||
| 348 | 0 | ▶ | return( $self->error( 'Linear white space on first header line' ) ) unless( @out ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 349 | 0 | $out[-1] .= $_; | |||||||||||||||||||||||||||||||||||||||
| 350 | } | ||||||||||||||||||||||||||||||||||||||||
| 351 | else | ||||||||||||||||||||||||||||||||||||||||
| 352 | { | ||||||||||||||||||||||||||||||||||||||||
| 353 | 8 | push( @out, $_ ); | |||||||||||||||||||||||||||||||||||||||
| 354 | } | ||||||||||||||||||||||||||||||||||||||||
| 355 | } | ||||||||||||||||||||||||||||||||||||||||
| 356 | |||||||||||||||||||||||||||||||||||||||||
| 357 | # parse request or response line | ||||||||||||||||||||||||||||||||||||||||
| 358 | 3 | my( $obj, $req, $res ); | |||||||||||||||||||||||||||||||||||||||
| 359 | 3 | ▶ | unless( $trailer ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 360 | { | ||||||||||||||||||||||||||||||||||||||||
| 361 | 3 | my( $major, $minor ); | |||||||||||||||||||||||||||||||||||||||
| 362 | # is it an HTTP response? | ||||||||||||||||||||||||||||||||||||||||
| 363 | 3 | ▶ | if( $request =~ /^HTTP\/(\d+)\.(\d+)/i ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 364 | { | ||||||||||||||||||||||||||||||||||||||||
| 365 | 1 | ( $major, $minor ) = ( $1, $2 ); | |||||||||||||||||||||||||||||||||||||||
| 366 | 1 | $request =~ /^HTTP\/\d+\.\d+ (\d+) (.+)$/; | |||||||||||||||||||||||||||||||||||||||
| 367 | 1 | my $state = $1; | |||||||||||||||||||||||||||||||||||||||
| 368 | 1 | my $msg = $2; | |||||||||||||||||||||||||||||||||||||||
| 369 | 1 | $res = $obj = $self->{obj} = WebSocket::Response->new( $state, $msg ); | |||||||||||||||||||||||||||||||||||||||
| 370 | # perhaps a request? | ||||||||||||||||||||||||||||||||||||||||
| 371 | 1 | $self->response( $res ); | |||||||||||||||||||||||||||||||||||||||
| 372 | } | ||||||||||||||||||||||||||||||||||||||||
| 373 | else | ||||||||||||||||||||||||||||||||||||||||
| 374 | { | ||||||||||||||||||||||||||||||||||||||||
| 375 | 2 | my( $method, $uri, $http ) = split( / /, $request ); | |||||||||||||||||||||||||||||||||||||||
| 376 | 2 | ▶ | unless( $http and $http =~ /^HTTP\/(\d+)\.(\d+)$/i ) | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
$http and $http =~ m[^HTTP/(\d+)\.(\d+)$]i
MC/DC0%0/2
$http and $http =~ m[^HTTP/(\d+)\.(\d+)$]i
$http
$http =~ m[^HTTP/(\d+)\.(\d+)$]i
Truth table33%1/3
A and B
A= $httpB= $http =~ m[^HTTP/(\d+)\.(\d+)$]i
| |||||||||||||||||||||||||||||||||||||||||
| 377 | { | ||||||||||||||||||||||||||||||||||||||||
| 378 | 0 | return( $self->error( "'$request' is not the start of a valid HTTP request or response" ) ); | |||||||||||||||||||||||||||||||||||||||
| 379 | } | ||||||||||||||||||||||||||||||||||||||||
| 380 | 2 | ( $major, $minor ) = ( $1, $2 ); | |||||||||||||||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||||||||||||||
| 382 | # If the Request-URI is an abs_path, we need to tell URI that we don't | ||||||||||||||||||||||||||||||||||||||||
| 383 | # know the scheme, otherwise it will misinterpret paths that start with | ||||||||||||||||||||||||||||||||||||||||
| 384 | # // as being scheme-relative uris, and will interpret the first | ||||||||||||||||||||||||||||||||||||||||
| 385 | # component after // as the host (see rfc 2616) | ||||||||||||||||||||||||||||||||||||||||
| 386 | 2 | ▶ | $uri = "//$uri" if( $uri =~ m(^/) ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 387 | 2 | $req = $obj = $self->{obj} = WebSocket::Request->new( method => $method, uri => $uri ); | |||||||||||||||||||||||||||||||||||||||
| 388 | 2 | $self->request( $req ); | |||||||||||||||||||||||||||||||||||||||
| 389 | } | ||||||||||||||||||||||||||||||||||||||||
| 390 | # pseudo-header | ||||||||||||||||||||||||||||||||||||||||
| 391 | # $obj->header( X_HTTP_Version => "$major.$minor" ); | ||||||||||||||||||||||||||||||||||||||||
| 392 | } | ||||||||||||||||||||||||||||||||||||||||
| 393 | # we've already seen the initial line and created the object | ||||||||||||||||||||||||||||||||||||||||
| 394 | else | ||||||||||||||||||||||||||||||||||||||||
| 395 | { | ||||||||||||||||||||||||||||||||||||||||
| 396 | 0 | $obj = $self->{obj}; | |||||||||||||||||||||||||||||||||||||||
| 397 | } | ||||||||||||||||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||||||||||||||
| 399 | # import headers | ||||||||||||||||||||||||||||||||||||||||
| 400 | 3 | my $token = qr/[^][\x00-\x1f\x7f()<>@,;:\\"\/?={} \t]+/; | |||||||||||||||||||||||||||||||||||||||
| 401 | 3 | for $header( @$headers ) | |||||||||||||||||||||||||||||||||||||||
| 402 | { | ||||||||||||||||||||||||||||||||||||||||
| 403 | 8 | ▶ | return( $self->error( "Bad header name in '$header'" ) ) unless( $header =~ s/^($token):[\t ]*// ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 404 | 8 | $obj->headers->push_header( $1 => $header ); | |||||||||||||||||||||||||||||||||||||||
| 405 | } | ||||||||||||||||||||||||||||||||||||||||
| 406 | # if we're parsing trailers we don't need to look at content | ||||||||||||||||||||||||||||||||||||||||
| 407 | 3 | ▶ | return( $obj ) if( $trailer ); | ||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||
| 409 | 3 | $self->{state} = 'body'; | |||||||||||||||||||||||||||||||||||||||
| 410 | 3 | return( $self->_parse_body_chunk() ); | |||||||||||||||||||||||||||||||||||||||
| 411 | } | ||||||||||||||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||||||||||||||
| 413 | 6 | 1; | |||||||||||||||||||||||||||||||||||||||
| 414 | # NOTE: POD | ||||||||||||||||||||||||||||||||||||||||
| 415 | __END__ | ||||||||||||||||||||||||||||||||||||||||