| 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
Condition66%2/3
defined $validator and ref $validator ne "CODE"
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"
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 21 | 54 | ▶ | if(ref($handshake) ne "CODE") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
Condition100%3/3
defined $self->{"ssl_key_file"} or defined $self->{"ssl_cert_file"}
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"}
| |||||||||||||||||||||||||||||
| 46 | 71 | return (); | |||||||||||||||||||||||||||
| 47 | } | ||||||||||||||||||||||||||||
| 48 | 141 | ▶ | if(!defined($self->{ssl_cert_file})) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 73 | 4 | die "handshake error: " . $handshake->error . "\n"; | |||||||||||||||||||||||||||
| 74 | } | ||||||||||||||||||||||||||||
| 75 | 419 | ▶ | return if !$handshake->is_done; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 76 | 204 | ▶ | if($handshake->version ne "draft-ietf-hybi-17") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 81 | 3 | croak "handshake response was undef"; | |||||||||||||||||||||||||||
| 82 | } | ||||||||||||||||||||||||||||
| 83 | 192 | ▶ | if(ref($res) eq "Protocol::WebSocket::Response") { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 125 | 0 | ▶ | if(!defined($fh)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||