blib/lib/MojoX/Transaction/WebSocket76.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 3 Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 121 CC 15 SCAR 29.4
CC
15
Cov
0%
parse_frame
42.0
build_frame
20.0
server_handshake
20.0
CRAP 18.9
1 package MojoX::Transaction::WebSocket76;
2  
3 # Mojo::Base implicitly uses strict and warnings, so quieten perlcritic
4 ## no critic (RequireUseStrict, RequireUseWarnings)
5 use Mojo::Util ('md5_bytes');
6  
7 use Mojo::Base 'Mojo::Transaction::WebSocket';
8  
9  
10 our $VERSION = '0.05';
11  
12  
13 use constant DEBUG => &Mojo::Transaction::WebSocket::DEBUG;
14  
15 use constant {
16 TEXT => &Mojo::Transaction::WebSocket::TEXT,
17 BINARY => &Mojo::Transaction::WebSocket::BINARY,
18 CLOSE => &Mojo::Transaction::WebSocket::CLOSE,
19 };
20  
21  
22 sub build_frame {
23 my ($self, undef, undef, undef, undef, $type, $bytes) = @_;
24  
25 warn("-- Building frame (undef, undef, undef, undef, " . $type . ")\n") if DEBUG;
26  
27 my $length = length($bytes);
28  
29 warn("-- Payload (" . length($bytes) . ")\n" . $bytes . "\n") if DEBUG;
30  
31 return "\xff" if $type == CLOSE;
32 return "\x00" . $bytes . "\xff";
33 }
34  
35 sub parse_frame {
36 my ($self, $buffer) = @_;
37  
38 my $index = index($$buffer, "\xff");
39  
40 return if $index < 0;
41  
42 my $type = $index == 0 ? CLOSE : TEXT;
43 my $length = $index - 1;
44 my $bytes = $length
45 ? substr(substr($$buffer, 0, $index + 1, ''), 1, $length)
46 : '';
47  
48 warn("-- Parsing frame (undef, undef, undef, undef, " . $type . ")\n") if DEBUG;
49 warn("-- Payload (" . $length . ")\n" . $bytes . "\n") if DEBUG;
50  
51 # Result does compatible with Mojo::Transaction::WebSocket.
52 return [1, 0, 0, 0, $type, $bytes];
53 }
54  
55 sub server_handshake {
56 my ($self) = @_;
57  
58 my $req = $self->req;
59 my $content = $req->content;
60  
61 # Fetch request body.
62 $content->headers->content_length(length($content->leftovers));
63 $content->parse_body();
64  
65 my $res = bless($self->res, 'MojoX::Transaction::WebSocket76::_Response');
66 my $headers = $req->headers;
67  
68 $res->code(101);
69 $res->message('WebSocket Protocol Handshake');
70 $res->body(
71 $self->_challenge(
72 scalar($headers->header('Sec-WebSocket-Key1')),
73 scalar($headers->header('Sec-WebSocket-Key2')),
74 $req->body # Key3 data.
75 )
76 );
77  
78 my $url = $req->url;
79 my $scheme = $url->to_abs->scheme eq 'https' ? 'wss' : 'ws';
80 my $location = $url->to_abs->scheme($scheme)->to_string();
81 my $origin = $headers->header('Origin');
82 my $protocol = $headers->sec_websocket_protocol;
83  
84 $headers = $res->headers;
85 $headers->upgrade('WebSocket');
86 $headers->connection('Upgrade');
87 $headers->header('Sec-WebSocket-Location' => $location);
88 $headers->header('Sec-WebSocket-Origin' => $origin) if $origin;
89 $headers->sec_websocket_protocol($protocol) if $protocol;
90  
91 return $self;
92 }
93  
94 sub _challenge {
95 my ($self, $key1, $key2, $key3) = @_;
96  
97 return unless $key1 && $key2 && $key3;
98 return md5_bytes(join('',
99 pack('N', join('', $key1 =~ /(\d)/g) / ($key1 =~ tr/\ //)),
100 pack('N', join('', $key2 =~ /(\d)/g) / ($key2 =~ tr/\ //)),
101 $key3
102 ));
103 }
104  
105  
106 1;
107  
108  
109 package # Hide form PAUSE.
110 MojoX::Transaction::WebSocket76::_Response;
111  
112 use Mojo::Base 'Mojo::Message::Response';
113  
114  
115 sub fix_headers {
116 my ($self) = @_;
117  
118 $self->SUPER::fix_headers(@_[1 .. $#_]);
119 # Suppress "Content-Length" header.
120 $self->headers->remove('Content-Length');
121  
122 return $self;
123 }
124  
125 sub is_empty {
126 my ($self) = @_;
127  
128 return unless my $code = $self->code;
129 # Handshake response has a body.
130 return if $code == 101;
131 return $self->SUPER::is_empty;
132 }
133  
134  
135 1;
136  
137  
138 __END__