| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Mojo::WebSocket; | 
| 2 | 55 |  |  | 55 |  | 400 | use Mojo::Base -strict; | 
|  | 55 |  |  |  |  | 135 |  | 
|  | 55 |  |  |  |  | 390 |  | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 55 |  |  | 55 |  | 366 | use Config; | 
|  | 55 |  |  |  |  | 143 |  | 
|  | 55 |  |  |  |  | 2653 |  | 
| 5 | 55 |  |  | 55 |  | 399 | use Exporter   qw(import); | 
|  | 55 |  |  |  |  | 176 |  | 
|  | 55 |  |  |  |  | 2222 |  | 
| 6 | 55 |  |  | 55 |  | 462 | use Mojo::Util qw(b64_encode dumper sha1_bytes xor_encode); | 
|  | 55 |  |  |  |  | 157 |  | 
|  | 55 |  |  |  |  | 4467 |  | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 55 |  | 50 | 55 |  | 428 | use constant DEBUG => $ENV{MOJO_WEBSOCKET_DEBUG} || 0; | 
|  | 55 |  |  |  |  | 199 |  | 
|  | 55 |  |  |  |  | 4282 |  | 
| 9 |  |  |  |  |  |  |  | 
| 10 |  |  |  |  |  |  | # Unique value from RFC 6455 | 
| 11 | 55 |  |  | 55 |  | 421 | use constant GUID => '258EAFA5-E914-47DA-95CA-C5AB0DC85B11'; | 
|  | 55 |  |  |  |  | 143 |  | 
|  | 55 |  |  |  |  | 4738 |  | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | # Perl with support for quads | 
| 14 | 55 |  | 33 | 55 |  | 435 | use constant MODERN => (($Config{use64bitint} // '') eq 'define' || $Config{longsize} >= 8); | 
|  | 55 |  |  |  |  | 150 |  | 
|  | 55 |  |  |  |  | 9498 |  | 
| 15 |  |  |  |  |  |  |  | 
| 16 |  |  |  |  |  |  | # Opcodes | 
| 17 | 55 |  |  |  |  | 76632 | use constant {WS_CONTINUATION => 0x0, WS_TEXT => 0x1, WS_BINARY => 0x2, WS_CLOSE => 0x8, WS_PING => 0x9, | 
| 18 | 55 |  |  | 55 |  | 472 | WS_PONG => 0xa}; | 
|  | 55 |  |  |  |  | 149 |  | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  | our @EXPORT_OK = ( | 
| 21 |  |  |  |  |  |  | qw(WS_BINARY WS_CLOSE WS_CONTINUATION WS_PING WS_PONG WS_TEXT build_frame challenge client_handshake parse_frame), | 
| 22 |  |  |  |  |  |  | qw(server_handshake) | 
| 23 |  |  |  |  |  |  | ); | 
| 24 |  |  |  |  |  |  |  | 
| 25 |  |  |  |  |  |  | sub build_frame { | 
| 26 | 354 |  |  | 354 | 1 | 72938 | my ($masked, $fin, $rsv1, $rsv2, $rsv3, $op, $payload) = @_; | 
| 27 | 354 |  |  |  |  | 494 | warn "-- Building frame ($fin, $rsv1, $rsv2, $rsv3, $op)\n" if DEBUG; | 
| 28 |  |  |  |  |  |  |  | 
| 29 |  |  |  |  |  |  | # Head | 
| 30 | 354 | 100 |  |  |  | 838 | my $head = $op + ($fin ? 128 : 0); | 
| 31 | 354 | 100 |  |  |  | 712 | $head |= 0b01000000 if $rsv1; | 
| 32 | 354 | 100 |  |  |  | 741 | $head |= 0b00100000 if $rsv2; | 
| 33 | 354 | 100 |  |  |  | 669 | $head |= 0b00010000 if $rsv3; | 
| 34 | 354 |  |  |  |  | 1179 | my $frame = pack 'C', $head; | 
| 35 |  |  |  |  |  |  |  | 
| 36 |  |  |  |  |  |  | # Small payload | 
| 37 | 354 |  |  |  |  | 597 | my $len = length $payload; | 
| 38 | 354 | 100 |  |  |  | 681 | if ($len < 126) { | 
|  |  | 100 |  |  |  |  |  | 
| 39 | 336 |  |  |  |  | 422 | warn "-- Small payload ($len)\n@{[dumper $payload]}" if DEBUG; | 
| 40 | 336 | 100 |  |  |  | 893 | $frame .= pack 'C', $masked ? ($len | 128) : $len; | 
| 41 |  |  |  |  |  |  | } | 
| 42 |  |  |  |  |  |  |  | 
| 43 |  |  |  |  |  |  | # Extended payload (16-bit) | 
| 44 |  |  |  |  |  |  | elsif ($len < 65536) { | 
| 45 | 11 |  |  |  |  | 17 | warn "-- Extended 16-bit payload ($len)\n@{[dumper $payload]}" if DEBUG; | 
| 46 | 11 | 100 |  |  |  | 53 | $frame .= pack 'Cn', $masked ? (126 | 128) : 126, $len; | 
| 47 |  |  |  |  |  |  | } | 
| 48 |  |  |  |  |  |  |  | 
| 49 |  |  |  |  |  |  | # Extended payload (64-bit with 32-bit fallback) | 
| 50 |  |  |  |  |  |  | else { | 
| 51 | 7 |  |  |  |  | 13 | warn "-- Extended 64-bit payload ($len)\n@{[dumper $payload]}" if DEBUG; | 
| 52 | 7 | 100 |  |  |  | 48 | $frame .= pack 'C', $masked ? (127 | 128) : 127; | 
| 53 | 7 |  |  |  |  | 24 | $frame .= MODERN ? pack('Q>', $len) : pack('NN', 0, $len & 0xffffffff); | 
| 54 |  |  |  |  |  |  | } | 
| 55 |  |  |  |  |  |  |  | 
| 56 |  |  |  |  |  |  | # Mask payload | 
| 57 | 354 | 100 |  |  |  | 807 | if ($masked) { | 
| 58 | 168 |  |  |  |  | 638 | my $mask = pack 'N', int(rand 9 x 7); | 
| 59 | 168 |  |  |  |  | 834 | $payload = $mask . xor_encode($payload, $mask x 128); | 
| 60 |  |  |  |  |  |  | } | 
| 61 |  |  |  |  |  |  |  | 
| 62 | 354 |  |  |  |  | 2787 | return $frame . $payload; | 
| 63 |  |  |  |  |  |  | } | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | sub challenge { | 
| 66 | 63 |  |  | 63 | 1 | 127 | my $tx = shift; | 
| 67 |  |  |  |  |  |  |  | 
| 68 |  |  |  |  |  |  | # "permessage-deflate" extension | 
| 69 | 63 |  |  |  |  | 207 | my $headers = $tx->res->headers; | 
| 70 | 63 | 100 | 100 |  |  | 252 | $tx->compressed(1) if ($headers->sec_websocket_extensions // '') =~ /permessage-deflate/; | 
| 71 |  |  |  |  |  |  |  | 
| 72 | 63 |  |  |  |  | 208 | return _challenge($tx->req->headers->sec_websocket_key) eq $headers->sec_websocket_accept; | 
| 73 |  |  |  |  |  |  | } | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  | sub client_handshake { | 
| 76 | 80 |  |  | 80 | 1 | 155 | my $tx = shift; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 80 |  |  |  |  | 222 | my $headers = $tx->req->headers; | 
| 79 | 80 | 50 |  |  |  | 290 | $headers->upgrade('websocket')      unless $headers->upgrade; | 
| 80 | 80 | 50 |  |  |  | 241 | $headers->connection('Upgrade')     unless $headers->connection; | 
| 81 | 80 | 50 |  |  |  | 253 | $headers->sec_websocket_version(13) unless $headers->sec_websocket_version; | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | # Generate 16 byte WebSocket challenge | 
| 84 | 80 |  |  |  |  | 708 | my $challenge = b64_encode sprintf('%16u', int(rand 9 x 16)), ''; | 
| 85 | 80 | 100 |  |  |  | 255 | $headers->sec_websocket_key($challenge) unless $headers->sec_websocket_key; | 
| 86 |  |  |  |  |  |  |  | 
| 87 | 80 |  |  |  |  | 453 | return $tx; | 
| 88 |  |  |  |  |  |  | } | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | sub parse_frame { | 
| 91 | 563 |  |  | 563 | 1 | 13760 | my ($buffer, $max) = @_; | 
| 92 |  |  |  |  |  |  |  | 
| 93 |  |  |  |  |  |  | # Head | 
| 94 | 563 | 100 |  |  |  | 1917 | return undef unless length $$buffer >= 2; | 
| 95 | 275 |  |  |  |  | 959 | my ($first, $second) = unpack 'C2', $$buffer; | 
| 96 |  |  |  |  |  |  |  | 
| 97 |  |  |  |  |  |  | # FIN | 
| 98 | 275 | 100 |  |  |  | 768 | my $fin = ($first & 0b10000000) == 0b10000000 ? 1 : 0; | 
| 99 |  |  |  |  |  |  |  | 
| 100 |  |  |  |  |  |  | # RSV1-3 | 
| 101 | 275 | 100 |  |  |  | 575 | my $rsv1 = ($first & 0b01000000) == 0b01000000 ? 1 : 0; | 
| 102 | 275 | 100 |  |  |  | 541 | my $rsv2 = ($first & 0b00100000) == 0b00100000 ? 1 : 0; | 
| 103 | 275 | 100 |  |  |  | 520 | my $rsv3 = ($first & 0b00010000) == 0b00010000 ? 1 : 0; | 
| 104 |  |  |  |  |  |  |  | 
| 105 |  |  |  |  |  |  | # Opcode | 
| 106 | 275 |  |  |  |  | 385 | my $op = $first & 0b00001111; | 
| 107 | 275 |  |  |  |  | 343 | warn "-- Parsing frame ($fin, $rsv1, $rsv2, $rsv3, $op)\n" if DEBUG; | 
| 108 |  |  |  |  |  |  |  | 
| 109 |  |  |  |  |  |  | # Small payload | 
| 110 | 275 |  |  |  |  | 556 | my ($hlen, $len) = (2, $second & 0b01111111); | 
| 111 | 275 | 100 |  |  |  | 619 | if ($len < 126) { warn "-- Small payload ($len)\n" if DEBUG } | 
|  | 259 | 100 |  |  |  | 398 |  | 
|  |  | 50 |  |  |  |  |  | 
| 112 |  |  |  |  |  |  |  | 
| 113 |  |  |  |  |  |  | # Extended payload (16-bit) | 
| 114 |  |  |  |  |  |  | elsif ($len == 126) { | 
| 115 | 10 | 50 |  |  |  | 38 | return undef unless length $$buffer > 4; | 
| 116 | 10 |  |  |  |  | 22 | $hlen = 4; | 
| 117 | 10 |  |  |  |  | 37 | $len  = unpack 'x2n', $$buffer; | 
| 118 | 10 |  |  |  |  | 17 | warn "-- Extended 16-bit payload ($len)\n" if DEBUG; | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  | # Extended payload (64-bit with 32-bit fallback) | 
| 122 |  |  |  |  |  |  | elsif ($len == 127) { | 
| 123 | 6 | 50 |  |  |  | 26 | return undef unless length $$buffer > 10; | 
| 124 | 6 |  |  |  |  | 15 | $hlen = 10; | 
| 125 | 6 |  |  |  |  | 23 | $len  = MODERN ? unpack('x2Q>', $$buffer) : unpack('x2x4N', $$buffer); | 
| 126 | 6 |  |  |  |  | 8 | warn "-- Extended 64-bit payload ($len)\n" if DEBUG; | 
| 127 |  |  |  |  |  |  | } | 
| 128 |  |  |  |  |  |  |  | 
| 129 |  |  |  |  |  |  | # Check message size | 
| 130 | 275 | 100 |  |  |  | 575 | return 1 if $len > $max; | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  | # Check if whole packet has arrived | 
| 133 | 272 | 100 |  |  |  | 613 | $len += 4 if my $masked = $second & 0b10000000; | 
| 134 | 272 | 100 |  |  |  | 591 | return undef if length $$buffer < ($hlen + $len); | 
| 135 | 271 |  |  |  |  | 623 | substr $$buffer, 0, $hlen, ''; | 
| 136 |  |  |  |  |  |  |  | 
| 137 |  |  |  |  |  |  | # Payload | 
| 138 | 271 | 100 |  |  |  | 989 | my $payload = $len ? substr($$buffer, 0, $len, '') : ''; | 
| 139 | 271 | 100 |  |  |  | 983 | $payload = xor_encode($payload, substr($payload, 0, 4, '') x 128) if $masked; | 
| 140 | 271 |  |  |  |  | 385 | warn dumper $payload if DEBUG; | 
| 141 |  |  |  |  |  |  |  | 
| 142 | 271 |  |  |  |  | 1326 | return [$fin, $rsv1, $rsv2, $rsv3, $op, $payload]; | 
| 143 |  |  |  |  |  |  | } | 
| 144 |  |  |  |  |  |  |  | 
| 145 |  |  |  |  |  |  | sub server_handshake { | 
| 146 | 73 |  |  | 73 | 1 | 288 | my $tx = shift; | 
| 147 |  |  |  |  |  |  |  | 
| 148 | 73 |  |  |  |  | 260 | my $headers = $tx->res->headers; | 
| 149 | 73 |  |  |  |  | 225 | $headers->upgrade('websocket')->connection('Upgrade'); | 
| 150 | 73 |  |  |  |  | 262 | $headers->sec_websocket_accept(_challenge($tx->req->headers->sec_websocket_key)); | 
| 151 |  |  |  |  |  |  |  | 
| 152 | 73 |  |  |  |  | 260 | return $tx; | 
| 153 |  |  |  |  |  |  | } | 
| 154 |  |  |  |  |  |  |  | 
| 155 | 136 |  | 50 | 136 |  | 1712 | sub _challenge { b64_encode(sha1_bytes(($_[0] || '') . GUID), '') } | 
| 156 |  |  |  |  |  |  |  | 
| 157 |  |  |  |  |  |  | 1; | 
| 158 |  |  |  |  |  |  |  | 
| 159 |  |  |  |  |  |  | =encoding utf8 | 
| 160 |  |  |  |  |  |  |  | 
| 161 |  |  |  |  |  |  | =head1 NAME | 
| 162 |  |  |  |  |  |  |  | 
| 163 |  |  |  |  |  |  | Mojo::WebSocket - The WebSocket protocol | 
| 164 |  |  |  |  |  |  |  | 
| 165 |  |  |  |  |  |  | =head1 SYNOPSIS | 
| 166 |  |  |  |  |  |  |  | 
| 167 |  |  |  |  |  |  | use Mojo::WebSocket qw(WS_TEXT build_frame parse_frame); | 
| 168 |  |  |  |  |  |  |  | 
| 169 |  |  |  |  |  |  | my $bytes = build_frame 0, 1, 0, 0, 0, WS_TEXT, 'Hello World!'; | 
| 170 |  |  |  |  |  |  | my $frame = parse_frame \$bytes, 262144; | 
| 171 |  |  |  |  |  |  |  | 
| 172 |  |  |  |  |  |  | =head1 DESCRIPTION | 
| 173 |  |  |  |  |  |  |  | 
| 174 |  |  |  |  |  |  | L implements the WebSocket protocol as described in L. | 
| 175 |  |  |  |  |  |  | Note that 64-bit frames require a Perl with support for quads or they are limited to 32-bit. | 
| 176 |  |  |  |  |  |  |  | 
| 177 |  |  |  |  |  |  | =head1 FUNCTIONS | 
| 178 |  |  |  |  |  |  |  | 
| 179 |  |  |  |  |  |  | L implements the following functions, which can be imported individually. | 
| 180 |  |  |  |  |  |  |  | 
| 181 |  |  |  |  |  |  | =head2 build_frame | 
| 182 |  |  |  |  |  |  |  | 
| 183 |  |  |  |  |  |  | my $bytes = build_frame $masked, $fin, $rsv1, $rsv2, $rsv3, $op, $payload; | 
| 184 |  |  |  |  |  |  |  | 
| 185 |  |  |  |  |  |  | Build WebSocket frame. | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | # Masked binary frame with FIN bit and payload | 
| 188 |  |  |  |  |  |  | say build_frame 1, 1, 0, 0, 0, WS_BINARY, 'Hello World!'; | 
| 189 |  |  |  |  |  |  |  | 
| 190 |  |  |  |  |  |  | # Text frame with payload but without FIN bit | 
| 191 |  |  |  |  |  |  | say build_frame 0, 0, 0, 0, 0, WS_TEXT, 'Hello '; | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | # Continuation frame with FIN bit and payload | 
| 194 |  |  |  |  |  |  | say build_frame 0, 1, 0, 0, 0, WS_CONTINUATION, 'World!'; | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | # Close frame with FIN bit and without payload | 
| 197 |  |  |  |  |  |  | say build_frame 0, 1, 0, 0, 0, WS_CLOSE, ''; | 
| 198 |  |  |  |  |  |  |  | 
| 199 |  |  |  |  |  |  | # Ping frame with FIN bit and payload | 
| 200 |  |  |  |  |  |  | say build_frame 0, 1, 0, 0, 0, WS_PING, 'Test 123'; | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | # Pong frame with FIN bit and payload | 
| 203 |  |  |  |  |  |  | say build_frame 0, 1, 0, 0, 0, WS_PONG, 'Test 123'; | 
| 204 |  |  |  |  |  |  |  | 
| 205 |  |  |  |  |  |  | =head2 challenge | 
| 206 |  |  |  |  |  |  |  | 
| 207 |  |  |  |  |  |  | my $bool = challenge Mojo::Transaction::WebSocket->new; | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  | Check WebSocket handshake challenge. | 
| 210 |  |  |  |  |  |  |  | 
| 211 |  |  |  |  |  |  | =head2 client_handshake | 
| 212 |  |  |  |  |  |  |  | 
| 213 |  |  |  |  |  |  | my $tx = client_handshake Mojo::Transaction::HTTP->new; | 
| 214 |  |  |  |  |  |  |  | 
| 215 |  |  |  |  |  |  | Perform WebSocket handshake client-side. | 
| 216 |  |  |  |  |  |  |  | 
| 217 |  |  |  |  |  |  | =head2 parse_frame | 
| 218 |  |  |  |  |  |  |  | 
| 219 |  |  |  |  |  |  | my $frame = parse_frame \$bytes, $limit; | 
| 220 |  |  |  |  |  |  |  | 
| 221 |  |  |  |  |  |  | Parse WebSocket frame. | 
| 222 |  |  |  |  |  |  |  | 
| 223 |  |  |  |  |  |  | # Parse single frame and remove it from buffer | 
| 224 |  |  |  |  |  |  | my $frame = parse_frame \$buffer, 262144; | 
| 225 |  |  |  |  |  |  | say "FIN: $frame->[0]"; | 
| 226 |  |  |  |  |  |  | say "RSV1: $frame->[1]"; | 
| 227 |  |  |  |  |  |  | say "RSV2: $frame->[2]"; | 
| 228 |  |  |  |  |  |  | say "RSV3: $frame->[3]"; | 
| 229 |  |  |  |  |  |  | say "Opcode: $frame->[4]"; | 
| 230 |  |  |  |  |  |  | say "Payload: $frame->[5]"; | 
| 231 |  |  |  |  |  |  |  | 
| 232 |  |  |  |  |  |  | =head2 server_handshake | 
| 233 |  |  |  |  |  |  |  | 
| 234 |  |  |  |  |  |  | my $tx = server_handshake Mojo::Transaction::HTTP->new; | 
| 235 |  |  |  |  |  |  |  | 
| 236 |  |  |  |  |  |  | Perform WebSocket handshake server-side. | 
| 237 |  |  |  |  |  |  |  | 
| 238 |  |  |  |  |  |  | =head1 CONSTANTS | 
| 239 |  |  |  |  |  |  |  | 
| 240 |  |  |  |  |  |  | L implements the following constants, which can be imported individually. | 
| 241 |  |  |  |  |  |  |  | 
| 242 |  |  |  |  |  |  | =head2 WS_BINARY | 
| 243 |  |  |  |  |  |  |  | 
| 244 |  |  |  |  |  |  | Opcode for C frames. | 
| 245 |  |  |  |  |  |  |  | 
| 246 |  |  |  |  |  |  | =head2 WS_CLOSE | 
| 247 |  |  |  |  |  |  |  | 
| 248 |  |  |  |  |  |  | Opcode for C frames. | 
| 249 |  |  |  |  |  |  |  | 
| 250 |  |  |  |  |  |  | =head2 WS_CONTINUATION | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  | Opcode for C frames. | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | =head2 WS_PING | 
| 255 |  |  |  |  |  |  |  | 
| 256 |  |  |  |  |  |  | Opcode for C frames. | 
| 257 |  |  |  |  |  |  |  | 
| 258 |  |  |  |  |  |  | =head2 WS_PONG | 
| 259 |  |  |  |  |  |  |  | 
| 260 |  |  |  |  |  |  | Opcode for C frames. | 
| 261 |  |  |  |  |  |  |  | 
| 262 |  |  |  |  |  |  | =head2 WS_TEXT | 
| 263 |  |  |  |  |  |  |  | 
| 264 |  |  |  |  |  |  | Opcode for C frames. | 
| 265 |  |  |  |  |  |  |  | 
| 266 |  |  |  |  |  |  | =head1 DEBUGGING | 
| 267 |  |  |  |  |  |  |  | 
| 268 |  |  |  |  |  |  | You can set the C environment variable to get some advanced diagnostics information printed to | 
| 269 |  |  |  |  |  |  | C. | 
| 270 |  |  |  |  |  |  |  | 
| 271 |  |  |  |  |  |  | MOJO_WEBSOCKET_DEBUG=1 | 
| 272 |  |  |  |  |  |  |  | 
| 273 |  |  |  |  |  |  | =head1 SEE ALSO | 
| 274 |  |  |  |  |  |  |  | 
| 275 |  |  |  |  |  |  | L, L, L. | 
| 276 |  |  |  |  |  |  |  | 
| 277 |  |  |  |  |  |  | =cut |