Clio/Server/HTTP/Client/WebSocket.pm

filter: Statementstmt 38.3% 23 / 60 Branchbran 0.0% 0 / 8 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 46.1% 6 / 13 Podpod 100% 2 / 2 totaltotal 37.3% 31 / 83 CC 5 SCAR 16.4
CC
5
Cov
34%
__ANON__
12.0
__ANON__
12.0
__ANON__
2.0
CRAP 5.2
« Clio/Server/HTTP.pm ↑ Summary Clio/Server/HTTP/Client/Stream.pm »
1  
2 package Clio::Server::HTTP::Client::WebSocket;
3 1 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 $Clio::Server::HTTP::Client::WebSocket::AUTHORITY = 'cpan:AJGB';
5 }
6 {
7 1 $Clio::Server::HTTP::Client::WebSocket::VERSION = '0.02';
8 }
9 # ABSTRACT: Clio HTTP Client for WebSocket connections
10  
11 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Moo;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 1 ▶use Protocol::WebSocket::Handshake::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 1 ▶use Protocol::WebSocket::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 1 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 1 extends qw( Clio::Server::HTTP::Client::Stream );
19  
20  
21 1 has 'version' => (
22 is => 'rw',
23 );
24  
25 1 require AnyEvent::Handle;
26 AnyEvent::Handle::register_read_type(
27 0 ▶ websocket => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
28 0 my ($self, $cb, $version) = @_;
29 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
30 0 ▶ exists $_[0]{rbuf} or return;
Branch: 1 branch
BranchTrueFalseTotal
exists $_[0]{"rbuf"} or return 0 0 0
31  
32 my $frame = Protocol::WebSocket::Frame->new(
33 buffer => delete $_[0]{rbuf},
34 0 version => $version
35 );
36  
37 0 ▶ while (my $in = $frame->next) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $in = $frame->next) 0 0 0
38 0 $cb->($_[0], $in);
39 }
40
41 0 return 1;
42 }
43 0 }
44 1 );
45  
46  
47 0 ▶sub write {
Subroutine write: not called CC=1 SCAR=6.9
Pod documented
48 0 my $self = shift;
49  
50 0 $self->log->trace("WebSocket client ", $self->id, " writing '@_'");
51  
52 0 my $frame = Protocol::WebSocket::Frame->new(
53 version => $self->version,
54 );
55 0 $frame->append(@_);
56 0 $self->writer->push_write( $frame->to_bytes );
57 }
58  
59  
60 0 ▶sub respond {
Subroutine respond: not called CC=1 SCAR=6.9
Pod documented
61 0 my $self = shift;
62  
63 0 my $env = $self->req->env;
64  
65 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
66 0 my $respond = shift;
67  
68 0 ▶ my $fh = $env->{'psgix.io'}
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $env->{"psgix.io"} or return &$respond([501, ["Content-Type", "text/plain"], ["This server does not support psgix.io extension"]]) 0 0 0
69 or return $respond->([ 501, [ "Content-Type", "text/plain" ], [ "This server does not support psgix.io extension" ] ]);
70  
71 0 my $hs = Protocol::WebSocket::Handshake::Server->new_from_psgi($env);
72 0 ▶ unless ( $hs->parse($fh) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($hs->parse($fh)) 0 0 0
73 0 my $err = $hs->error;
74 0 $self->log->fatal("Cannot parse $fh for handshake: $err");
75 0 return [400, [ "Content-Type", "text/plain" ], [$err]];
76 }
77  
78 0 my $h = AnyEvent::Handle->new(fh => $fh);
79 0 $self->writer( $h );
80  
81 0 ▶ $h->on_error(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
82 0 my ($handle, $fatal, $message) = @_;
83 0 $self->_handle_client_error($message);
84 0 });
85  
86 0 $self->version( $hs->version );
87  
88 # handshake
89 0 $self->log->trace("Sending handshake (version: ", $hs->version,"): ", $hs->to_string, " to client: ", $self->id);
90 0 $self->writer->push_write($hs->to_string);
91  
92 0 $self->_process->add_client( $self );
93  
94 0 ▶ my $reader; $reader = sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
95 0 my ($handle, $message) = @_;
96
97 0 $self->_process->write( $message );
98  
99 0 $h->push_read( websocket => $self->version => $reader );
100 0 };
101 0 $h->push_read( websocket => $self->version => $reader );
102 0 };
103 };
104  
105  
106 1 1;
107  
108  
109 __END__
« Clio/Server/HTTP.pm ↑ Summary Clio/Server/HTTP/Client/Stream.pm »