Hippie.pm

filter: Statementstmt 40.5% 49 / 121 Branchbran 0.0% 0 / 20 Conditioncond 0.0% 0 / 8 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 59.2% 16 / 27 Podpod 20.0% 1 / 5 totaltotal 35.4% 66 / 186 CC 14 SCAR 31.2
CC
14
Cov
33%
handler_ws
90.0
__ANON__
12.0
call
6.0
CRAP 22.7
« Hippie/Pipe.pm ↑ Summary Hippie/Handle/MXHR.pm »
1 package Web::Hippie;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.008_001;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 our $VERSION = '0.40';
6 1 ▶use parent 'Plack::Middleware';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Plack::Util::Accessor qw( on_error on_message trusted_origin );
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use AnyEvent;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use AnyEvent::Handle;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 1 ▶use Plack::Request;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use JSON;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 1 ▶use HTTP::Date;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 1 ▶use Digest::MD5 qw(md5);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 0 ▶sub call {
Subroutine call: not called CC=2 SCAR=17.9
Pod documented
17 0 my ($self, $env) = @_;
18  
19 0 my (undef, $type, $arg) = split('/', $env->{PATH_INFO}, 3);
20  
21 0 $env->{'hippie.args'} = $arg;
22  
23 0 my $code = $self->can("handler_$type");
24 0 ▶ unless ($code) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($code) 0 0 0
25 0 $env->{'PATH_INFO'} = "/$type";
26 0 return $self->app->($env);
27 }
28  
29 0 return $code->($self, $env, $self->app);
30  
31 }
32  
33  
34 1 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
35  
36 0 ▶sub handler_pub {
Subroutine handler_pub: not called CC=1 SCAR=6.9
Pod undocumented
37 0 my ($self, $env, $handler) = @_;
38 0 my $req = Plack::Request->new($env);
39 $env->{'hippie.message'} =
40 0 JSON::from_json($req->parameters->mixed->{'message'}, { utf8 => 1 });
41 0 $env->{'PATH_INFO'} = '/message';
42  
43 0 $handler->($env);
44 }
45  
46 0 ▶sub handler_mxhr {
Subroutine handler_mxhr: not called CC=2 SCAR=14.0
Pod undocumented
47 0 my ($self, $env, $handler) = @_;
48 0 my $req = Plack::Request->new($env);
49 0 ▶ my $client_id = $req->param('client_id') || rand(1);
Condition0%0/3
$req->param('client_id') || rand 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$req->param('client_id') || rand 1
$req->param('client_id') rand 1
Truth table0%0/3
A || B
A= $req->param('client_id')B= rand 1
ABresult
1X1
011
000
50  
51 0 my $size = 2;
52 1 ▶ use MIME::Base64;
Subroutine BEGIN: called CC=1 SCAR=0.0
53 0 my $boundary = MIME::Base64::encode(join("", map chr(rand(256)), 1..$size*3), "");
54 0 $boundary =~ s/[\W]/X/g; # ensure alnum only
55  
56 1 ▶ use Web::Hippie::Handle::MXHR;
Subroutine BEGIN: called CC=1 SCAR=0.0
57  
58 0 ▶ return sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
59 0 my $respond = shift;
60 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
61 or return $respond->([ 501, [ "Content-Type", "text/plain" ], [ "This server does not support psgix.io extension" ] ]);
62  
63 0 my $h = AnyEvent::Handle->new( fh => $fh );
64 0 $h->on_eof( $self->connection_cleanup($env, $handler, $h) );
65 0 $h->on_error( $self->connection_cleanup($env, $handler, $h) );
66  
67 # XXX: on_error or on_eof are not triggered if there's no rw events registered
68 0 ▶ $h->on_read(sub { die "this should not happen" });
Subroutine __ANON__: not called CC=1 SCAR=6.9
69  
70 0 my $writer = $respond->
71 ([ 200,
72 [ 'Content-Type' => 'multipart/mixed; boundary="' . $boundary . '"',
73 'Cache-Control' => 'no-cache, must-revalidate',
74 'Pragma' => 'no-cache',
75 'Expires' => HTTP::Date::time2str(0),
76 'Last-Modified' => HTTP::Date::time2str(time())
77 ]]);
78 0 $writer->write("--" . $boundary. "\n");
79 0 $env->{'hippie.handle'} = Web::Hippie::Handle::MXHR->new
80 ({ id => $client_id,
81 boundary => $boundary,
82 writer => $writer });
83 0 $env->{'PATH_INFO'} = '/init';
84  
85 0 $handler->($env);
86 0 };
87 }
88  
89 1 ▶use Protocol::WebSocket::Frame;
Subroutine BEGIN: called CC=1 SCAR=0.0
90 1 ▶use Protocol::WebSocket::Handshake::Server;
Subroutine BEGIN: called CC=1 SCAR=0.0
91 1 ▶use Web::Hippie::Handle::WebSocket;
Subroutine BEGIN: called CC=1 SCAR=0.0
92  
93 0 ▶sub handler_ws {
Subroutine handler_ws: not called CC=9 SCAR=45.0
Pod undocumented
94 0 my ($self, $env, $handler) = @_;
95  
96 0 my $req = Plack::Request->new($env);
97 0 my $res = $req->new_response(200);
98  
99 0 my $hs = Protocol::WebSocket::Handshake::Server->new_from_psgi($env);
100 0 ▶ my $client_id = $req->param('client_id') || rand(1);
Condition0%0/3
$req->param('client_id') || rand 1
l!l&&r!l&&!r
000
MC/DC0%0/2
$req->param('client_id') || rand 1
$req->param('client_id') rand 1
Truth table0%0/3
A || B
A= $req->param('client_id')B= rand 1
ABresult
1X1
011
000
101  
102 0 ▶ my $fh = $env->{'psgix.io'}
Branch: 1 branch
BranchTrueFalseTotal
my $fh = $env->{"psgix.io"} or return [501, ["Content-Type", "text/plain"], ["This server does not support psgix.io extension"]] 0 0 0
103 or return [ 501, [ "Content-Type", "text/plain" ], [ "This server does not support psgix.io extension" ] ];
104  
105 0 ▶ return [ 501, [ "Content-Type", "text/plain" ],
Branch: 2 branches
BranchTrueFalseTotal
unless $hs->parse($hs->version eq "draft-ietf-hybi-17" ? "" : $fh) 0 0 0
$hs->version eq 'draft-ietf-hybi-17' ? : 0 0 0
106 [ "Failed to initialize websocket" ] ]
107 unless $hs->parse( $hs->version eq 'draft-ietf-hybi-17' ? '' : $fh);
108  
109 0 ▶ if (my $origin = $hs->req->origin) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $origin = $hs->req->origin) 0 0 0
110 0 ▶ my $trusted_origin = $self->trusted_origin || '.*';
Condition0%0/2
$self->trusted_origin || '.*'
l!l
00
MC/DC0%0/1
$self->trusted_origin || '.*'
$self->trusted_origin
Truth table0%0/2
A
A= $self->trusted_origin
Aresult
11
00
111 0 ▶ if ($origin !~ m/^$trusted_origin/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($origin =~ /^$trusted_origin/) 0 0 0
112 0 $env->{'psgi.errors'}->print("Client origin $origin not allowed.\n");
113 0 return [403, ['Content-Type' => 'text/plain'], ['origin not allowed']];
114 }
115 }
116  
117 0 ▶ return [ 501, [ "Content-Type", "text/plain" ],
Branch: 1 branch
BranchTrueFalseTotal
unless $hs->is_done 0 0 0
118 [ "websocket handshake incomplete" ] ]
119 unless $hs->is_done;
120  
121 0 my $version = $hs->version;
122 0 my $frame = Protocol::WebSocket::Frame->new(version => $version);
123  
124 0 my $h = AnyEvent::Handle->new( fh => $fh, autocork => 1 );
125 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
126 0 my $responder = shift;
127  
128 0 $h->push_write($hs->to_string);
129 0 ▶ $h->on_read(sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
130 shift->push_read(
131 0 ▶ sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
132 0 $frame->append($_[0]->rbuf);
133 0 ▶ while (my $message = $frame->next_bytes) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $message = $frame->next_bytes) 0 0 0
134 0 $env->{'hippie.message'} = eval { JSON::decode_json($message) };
135 0 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 0 0
136 0 warn $@;
137 0 return $h->on_error();
138 }
139  
140 0 $env->{'PATH_INFO'} = '/message';
141 0 $handler->($env);
142 }
143 }
144 0 );
145 0 });
146 0 $env->{'hippie.handle'} = Web::Hippie::Handle::WebSocket->new
147 ({ id => $client_id,
148 version => $version,
149 h => $h });
150 0 $h->on_error( $self->connection_cleanup($env, $handler, $h) );
151  
152 0 $env->{'PATH_INFO'} = '/init';
153 0 $handler->($env);
154  
155 0 };
156 }
157  
158 0 ▶sub connection_cleanup {
Subroutine connection_cleanup: not called CC=1 SCAR=6.9
Pod undocumented
159 0 my ($self, $env, $handler, $h) = @_;
160 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
161 0 $env->{'PATH_INFO'} = '/error';
162 0 $handler->($env);
163 0 $h->destroy;
164 0 undef $env->{'hippie.handle'};
165 0 };
166 }
167  
168  
169 1 1;
170 __END__
« Hippie/Pipe.pm ↑ Summary Hippie/Handle/MXHR.pm »