blib/lib/AnyEvent/WebSocket/Server.pm

filter: Statementstmt 81.9% 77 / 94 Branchbran 66.6% 24 / 36 Conditioncond 83.3% 5 / 6 MC/DCmcdc 50.0% 2 / 4 Subroutinesub 94.7% 18 / 19 Podpod 100% 3 / 3 totaltotal 79.6% 129 / 162 CC 21 SCAR 16.5
CC
21
Cov
78%
establish_psgi
20.0
new
6.3
__ANON__
6.0
CRAP 5.2
↑ Summary
1 package AnyEvent::WebSocket::Server;
2 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶use Protocol::WebSocket::Handshake::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Try::Tiny;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use AnyEvent::WebSocket::Connection;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 9 our $VERSION = "0.10";
11  
12 54 ▶sub new {
Subroutine new: called CC=6 SCAR=18.4
Pod documented
13 54 my ($class, %args) = @_;
14 54 my $validator = $args{validator};
15 54 ▶ if(defined($validator) && ref($validator) ne "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $validator and ref $validator ne "CODE") 0 54 54
Condition66%2/3
defined $validator and ref $validator ne "CODE"
!ll&&!rl&&r
5130
MC/DC0%0/2
defined $validator and ref $validator ne "CODE"
defined $validator ref $validator ne "CODE"
Truth table66%2/3
A and B
A= defined $validatorB= ref $validator ne "CODE"
ABresult
0X0
100
111
16 0 croak "validator parameter must be a code-ref";
17 }
18 my $handshake = defined($args{handshake}) ? $args{handshake}
19 9 ▶ : defined($validator) ? sub { my ($req, $res) = @_; return ($res, $validator->($req)); }
Subroutine __ANON__: called CC=1 SCAR=0.0
20 54 ▶ : sub { $_[1] };
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 2 branches
BranchTrueFalseTotal
defined $args{'handshake'} ? : 21 33 54
defined $validator ? : 3 30 33
21 54 ▶ if(ref($handshake) ne "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $handshake ne "CODE") 0 54 54
22 0 croak "handshake parameter must be a code-ref";
23 }
24 my $self = bless {
25 handshake => $handshake,
26 54 map { ($_ => $args{$_}) } qw(ssl_key_file ssl_cert_file max_payload_size),
27 }, $class;
28 54 return $self;
29 }
30  
31 212 ▶sub _create_on_error {
Subroutine _create_on_error: called CC=1 SCAR=0.2
32 212 my ($cv) = @_;
33 4 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.6
34 4 my ($handle, $fatal, $message) = @_;
35 4 ▶ if($fatal) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fatal) { } 4 0 4
36 4 $cv->croak("connection error: $message");
37 }else {
38 0 warn $message;
39 }
40 212 };
41 }
42  
43 212 ▶sub _handle_args_tls {
Subroutine _handle_args_tls: called CC=5 SCAR=16.2
44 212 my ($self) = @_;
45 212 ▶ if(!defined($self->{ssl_key_file}) && !defined($self->{ssl_cert_file})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ssl_key_file"} or defined $self->{"ssl_cert_file"}) 71 141 212
Condition100%3/3
defined $self->{"ssl_key_file"} or defined $self->{"ssl_cert_file"}
l!l&&r!l&&!r
707171
MC/DC100%2/2
defined $self->{"ssl_key_file"} or defined $self->{"ssl_cert_file"}
defined $self->{"ssl_key_file"} defined $self->{"ssl_cert_file"}
Truth table100%3/3
A or B
A= defined $self->{"ssl_key_file"}B= defined $self->{"ssl_cert_file"}
ABresult
1X1
011
000
46 71 return ();
47 }
48 141 ▶ if(!defined($self->{ssl_cert_file})) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"ssl_cert_file"}) 0 141 141
49 0 croak "Only ssl_key_file is specified. You need to specify ssl_cert_file, too.";
50 }
51 return (
52 tls => "accept",
53 tls_ctx => {
54 cert_file => $self->{ssl_cert_file},
55 141 ▶ defined($self->{ssl_key_file}) ? (key_file => $self->{ssl_key_file}) : ()
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'ssl_key_file'} ? : 70 71 141
56 }
57 );
58 }
59  
60 212 ▶sub _do_handshake {
Subroutine _do_handshake: called CC=2 SCAR=6.9
61 212 my ($self, $cv_connection, $fh, $handshake) = @_;
62 212 my $handshake_code = $self->{handshake};
63 212 my $handle = AnyEvent::Handle->new(
64 $self->_handle_args_tls,
65 fh => $fh, on_error => _create_on_error($cv_connection)
66 );
67 423 ▶ my $read_cb = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
68 ## We don't receive handle object as an argument here. $handle
69 ## is imported in this closure so that $handle becomes
70 ## half-immortal.
71 423 ▶ try {
Subroutine __ANON__: called CC=6 SCAR=18.0
72 423 ▶ if(!defined($handshake->parse($handle->{rbuf}))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $handshake->parse($handle->{"rbuf"})) 4 419 423
73 4 die "handshake error: " . $handshake->error . "\n";
74 }
75 419 ▶ return if !$handshake->is_done;
Branch: 1 branch
BranchTrueFalseTotal
unless $handshake->is_done 215 204 419
76 204 ▶ if($handshake->version ne "draft-ietf-hybi-17") {
Branch: 1 branch
BranchTrueFalseTotal
if ($handshake->version ne "draft-ietf-hybi-17") 0 204 204
77 0 die "handshake error: unsupported WebSocket protocol version " . $handshake->version . "\n";
78 }
79 204 my ($res, @other_results) = $handshake_code->($handshake->req, $handshake->res);
80 195 ▶ if(!defined($res)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $res) 3 192 195
81 3 croak "handshake response was undef";
82 }
83 192 ▶ if(ref($res) eq "Protocol::WebSocket::Response") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $res eq "Protocol::WebSocket::Response") 189 3 192
84 189 $res = $res->to_string;
85 }
86 192 $handle->push_write("$res");
87 $cv_connection->send(
88 192 AnyEvent::WebSocket::Connection->new(handle => $handle, max_payload_size => $self->{max_payload_size}),
89 @other_results
90 );
91 192 undef $handle;
92 192 undef $cv_connection;
93 16 ▶ }catch {
Subroutine __ANON__: called CC=1 SCAR=0.0
94 16 my $e = shift;
95 16 $cv_connection->croak($e);
96 16 undef $handle;
97 16 undef $cv_connection;
98 423 };
99 212 };
100 212 $handle->{rbuf} = "";
101 212 $read_cb->(); ## in case the whole request is already consumed
102 212 ▶ $handle->on_read($read_cb) if defined $handle;
Branch: 1 branch
BranchTrueFalseTotal
if defined $handle 212 0 212
103 }
104  
105 213 ▶sub establish {
Subroutine establish: called CC=2 SCAR=6.9
Pod documented
106 213 my ($self, $fh) = @_;
107 213 my $cv_connection = AnyEvent->condvar;
108 213 ▶ if(!defined($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 1 212 213
109 1 $cv_connection->croak("fh parameter is mandatory for establish() method");
110 1 return $cv_connection;
111 }
112 212 my $handshake = Protocol::WebSocket::Handshake::Server->new;
113 212 $self->_do_handshake($cv_connection, $fh, $handshake);
114 212 return $cv_connection;
115 }
116  
117 0 ▶sub establish_psgi {
Subroutine establish_psgi: not called CC=4 SCAR=30.0
Pod documented
118 0 my ($self, $env, $fh) = @_;
119 0 my $cv_connection = AnyEvent->condvar;
120 0 ▶ if(!defined($env)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $env) 0 0 0
121 0 $cv_connection->croak("psgi_env parameter is mandatory");
122 0 return $cv_connection;
123 }
124 0 ▶ $fh = $env->{"psgix.io"} if not defined $fh;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $fh 0 0 0
125 0 ▶ if(!defined($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 0 0
126 0 $cv_connection->croak("No connection file handle provided. Maybe the PSGI server does not support psgix.io extension.");
127 0 return $cv_connection;
128 }
129 0 my $handshake = Protocol::WebSocket::Handshake::Server->new_from_psgi($env);
130 0 $self->_do_handshake($cv_connection, $fh, $handshake);
131 0 return $cv_connection;
132 }
133  
134 9 1;
135  
136 __END__
↑ Summary