Client.pm

filter: Statementstmt 83.1% 99 / 119 Branchbran 65.7% 25 / 38 Conditioncond 66.6% 6 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 95.2% 20 / 21 Podpod 100% 1 / 1 totaltotal 77.8% 151 / 194 CC 24 SCAR 17.7
CC
24
Cov
78%
_make_tcp_connection
19.1
connect
6.5
__ANON__
6.3
CRAP 5.9
↑ Summary Connection.pm »
1 package AnyEvent::WebSocket::Client;
2  
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use AE;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use AnyEvent::Socket ();
Subroutine BEGIN: called CC=1 SCAR=0.0
10 9 ▶use Protocol::WebSocket::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 9 ▶use Protocol::WebSocket::Handshake::Client;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use AnyEvent::WebSocket::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use PerlX::Maybe qw( maybe provided );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 # ABSTRACT: WebSocket client for AnyEvent
16 9 our $VERSION = '0.55'; # VERSION
17  
18  
19 has timeout => (
20 is => 'ro',
21 26 ▶ default => sub { 30 },
Subroutine __ANON__: called CC=1 SCAR=0.0
22 9 );
23  
24  
25 9 has ssl_no_verify => (
26 is => 'ro',
27 );
28  
29  
30 9 has ssl_ca_file => (
31 is => 'ro',
32 );
33  
34  
35 9 has protocol_version => (
36 is => 'ro',
37 );
38  
39  
40 has subprotocol => (
41 is => 'ro',
42 6 ▶ coerce => sub { ref $_[0] ? $_[0] : [$_[0]] },
Subroutine __ANON__: called CC=2 SCAR=6.9
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 5 1 6
43 9 );
44  
45  
46 has http_headers => (
47 is => 'ro',
48 3 ▶ coerce => sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
49 3 ▶ ref $_[0] eq 'ARRAY' ? $_[0] : do {
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'ARRAY' ? : 2 1 3
50 1 my $h = shift;
51 [
52 map {
53 1 my($k,$v) = ($_, $h->{$_});
54 2 ▶ $v = [$v] unless ref $v;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $v 1 1 2
55 2 map { $k => $_ } @$v;
56 # sorted to make testing easier.
57 # may be removed in the future
58 # so do not depend on it.
59 } sort keys %$h
60 ],
61 };
62 },
63 9 );
64  
65  
66 9 has max_payload_size => (
67 is => 'ro',
68 );
69  
70  
71 9 has max_fragments => (
72 is => 'ro',
73 );
74  
75  
76 has env_proxy => (
77 is => 'ro',
78 26 ▶ default => sub { 0 },
Subroutine __ANON__: called CC=1 SCAR=0.0
79 9 );
80  
81  
82  
83 sub connect
84 22 ▶{
Subroutine connect: called CC=6 SCAR=18.8
Pod documented
85 22 my($self, $uri, $host, $port) = @_;
86 22 ▶ unless(ref $uri)
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $uri) 3 19 22
87 {
88 3 require URI;
89 3 $uri = URI->new($uri);
90 }
91  
92 22 my $done = AE::cv;
93  
94 # TODO: should we also accept http and https URLs?
95 # probably.
96 22 ▶ if($uri->scheme ne 'ws' && $uri->scheme ne 'wss')
Branch: 1 branch
BranchTrueFalseTotal
if ($uri->scheme ne "ws" and $uri->scheme ne "wss") 0 22 22
Condition66%2/3
$uri->scheme ne "ws" and $uri->scheme ne "wss"
!ll&&!rl&&r
2020
MC/DC0%0/2
$uri->scheme ne "ws" and $uri->scheme ne "wss"
$uri->scheme ne "ws" $uri->scheme ne "wss"
Truth table66%2/3
A and B
A= $uri->scheme ne "ws"B= $uri->scheme ne "wss"
ABresult
0X0
100
111
97 {
98 0 $done->croak("URI is not a websocket");
99 0 return $done;
100 }
101  
102 22 ▶ $host = $uri->host unless defined $host;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $host 20 2 22
103 22 ▶ $port = $uri->port unless defined $port;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $port 20 2 22
104  
105 22 ▶ $self->_make_tcp_connection($uri->scheme, $host, $port, sub {
Subroutine __ANON__: called CC=5 SCAR=17.1
106 22 my $fh = shift;
107 22 ▶ unless($fh)
Branch: 1 branch
BranchTrueFalseTotal
unless ($fh) 0 22 22
108 {
109 0 $done->croak("unable to connect");
110 0 return;
111 }
112 22 my $req = Protocol::WebSocket::Request->new( maybe headers => $self->http_headers );
113 22 my $handshake = Protocol::WebSocket::Handshake::Client->new(
114 url => $uri->as_string,
115 maybe version => $self->protocol_version,
116 req => $req,
117 );
118  
119 22 my %subprotocol;
120 22 ▶ if($self->subprotocol)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->subprotocol) 3 19 22
121 {
122 3 %subprotocol = map { $_ => 1 } @{ $self->subprotocol };
123 3 $handshake->req->subprotocol(join(',', @{ $self->subprotocol }));
124 }
125  
126 my $hdl = AnyEvent::Handle->new(
127 fh => $fh,
128 provided $uri->secure, tls => 'connect',
129 provided $uri->secure && !$self->ssl_no_verify, peername => $uri->host,
130 provided $uri->secure && !$self->ssl_no_verify, tls_ctx => {
131 verify => 1,
132 verify_peername => "https",
133 maybe ca_file => $self->ssl_ca_file,
134 },
135 0 ▶ on_error => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
136 0 my ($hdl, $fatal, $msg) = @_;
137 0 ▶ if($fatal)
Branch: 1 branch
BranchTrueFalseTotal
if ($fatal) { } 0 0 0
138 0 { $done->croak("connect error: " . $msg) }
139 else
140 0 { warn $msg }
141 },
142 22 ▶ );
Condition66%4/6
$uri->secure && !$self->ssl_no_verify
!ll&&!rl&&r
2020
$uri->secure && !$self->ssl_no_verify
!ll&&!rl&&r
2020
MC/DC0%0/2
$uri->secure && !$self->ssl_no_verify
$uri->secure !$self->ssl_no_verify
MC/DC0%0/2
$uri->secure && !$self->ssl_no_verify
$uri->secure !$self->ssl_no_verify
Truth table66%2/3
A && not(B)
A= $uri->secureB= !$self->ssl_no_verify
ABresult
0X0
100
111
Truth table66%2/3
A && not(B)
A= $uri->secureB= !$self->ssl_no_verify
ABresult
0X0
100
111
143  
144 22 $hdl->push_write($handshake->to_string);
145 22 ▶ $hdl->on_read(sub {
Subroutine __ANON__: called CC=6 SCAR=18.4
146 22 $handshake->parse($_[0]{rbuf});
147 22 ▶ if($handshake->error)
Branch: 2 branches
BranchTrueFalseTotal
if ($handshake->error) { } 0 22 22
elsif ($handshake->is_done) { } 22 0 22
148 {
149 0 $done->croak("handshake error: " . $handshake->error);
150 0 undef $hdl;
151 0 undef $handshake;
152 0 undef $done;
153 }
154 elsif($handshake->is_done)
155 {
156 22 my $sb;
157 22 ▶ if($self->subprotocol)
Branch: 1 branch
BranchTrueFalseTotal
if ($self->subprotocol) 3 19 22
158 {
159 3 $sb = $handshake->res->subprotocol;
160 3 ▶ if(defined $sb)
Branch: 1 branch
BranchTrueFalseTotal
if (defined $sb) { } 2 1 3
161 {
162 2 ▶ unless($subprotocol{$sb})
Branch: 1 branch
BranchTrueFalseTotal
unless ($subprotocol{$sb}) 1 1 2
163 {
164 1 $done->croak("subprotocol mismatch, requested: @{[ join ', ', @{ $self->subprotocol } ]}, got: $sb");
165 }
166 }
167 else
168 {
169 1 $done->croak("no subprotocol in response");
170 }
171 }
172 22 undef $handshake;
173 22 $done->send(
174 AnyEvent::WebSocket::Connection->new(
175 handle => $hdl,
176 masked => 1,
177 maybe subprotocol => $sb,
178 maybe max_payload_size => $self->max_payload_size,
179 maybe max_fragments => $self->max_fragments,
180 )
181 );
182 22 undef $hdl;
183 22 undef $done;
184 }
185 22 });
186 22 ▶ }, sub { $self->timeout });
Subroutine __ANON__: called CC=1 SCAR=0.0
187 22 $done;
188 }
189  
190 sub _make_tcp_connection
191 22 ▶{
Subroutine _make_tcp_connection: called CC=6 SCAR=29.5
192 22 my $self = shift;
193 22 my $scheme = shift;
194 22 my ($host, $port) = @_;
195 22 ▶ if(!$self->env_proxy)
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->env_proxy) 22 0 22
196 {
197 22 return &AnyEvent::Socket::tcp_connect(@_);
198 }
199 0 require AnyEvent::Connector;
200 my @connectors =
201 $scheme eq "ws"
202 0 ▶ ? (map { AnyEvent::Connector->new(env_proxy => $_) } qw(ws http))
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'wss' ? : 0 0 0
203 : $scheme eq "wss"
204 0 ▶ ? (map { AnyEvent::Connector->new(env_proxy => $_) } qw(wss https))
Branch: 1 branch
BranchTrueFalseTotal
$scheme eq 'ws' ? : 0 0 0
205 : ();
206 0 foreach my $connector (@connectors)
207 {
208 0 ▶ if(defined($connector->proxy_for($host, $port)))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $connector->proxy_for($host, $port)) 0 0 0
209 {
210 0 return $connector->tcp_connect(@_);
211 }
212 }
213 0 return &AnyEvent::Socket::tcp_connect(@_);
214 }
215  
216 9 1;
217  
218 __END__
↑ Summary Connection.pm »