1
|
|
|
<?php |
2
|
|
|
namespace PHPDaemon\Servers\WebSocket\Protocols; |
3
|
|
|
|
4
|
|
|
use PHPDaemon\Core\Daemon; |
5
|
|
|
use PHPDaemon\Servers\WebSocket\Connection; |
6
|
|
|
use PHPDaemon\Utils\Binary; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Websocket protocol 13 |
10
|
|
|
* @see http://datatracker.ietf.org/doc/rfc6455/?include_text=1 |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
class V13 extends Connection { |
14
|
|
|
const CONTINUATION = 0; |
15
|
|
|
const STRING = 0x1; |
16
|
|
|
const BINARY = 0x2; |
17
|
|
|
const CONNCLOSE = 0x8; |
18
|
|
|
const PING = 0x9; |
19
|
|
|
const PONG = 0xA; |
20
|
|
|
protected static $opcodes = [ |
21
|
|
|
0 => 'CONTINUATION', |
22
|
|
|
0x1 => 'STRING', |
23
|
|
|
0x2 => 'BINARY', |
24
|
|
|
0x8 => 'CONNCLOSE', |
25
|
|
|
0x9 => 'PING', |
26
|
|
|
0xA => 'PONG', |
27
|
|
|
]; |
28
|
|
|
protected $outgoingCompression = 0; |
29
|
|
|
|
30
|
|
|
protected $framebuf = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Sends a handshake message reply |
34
|
|
|
* @param string Received data (no use in this class) |
35
|
|
|
* @return boolean OK? |
36
|
|
|
*/ |
37
|
|
|
public function sendHandshakeReply($extraHeaders = '') { |
38
|
|
|
if (!isset($this->server['HTTP_SEC_WEBSOCKET_KEY']) || !isset($this->server['HTTP_SEC_WEBSOCKET_VERSION'])) { |
39
|
|
|
return false; |
40
|
|
|
} |
41
|
|
|
if ($this->server['HTTP_SEC_WEBSOCKET_VERSION'] !== '13' && $this->server['HTTP_SEC_WEBSOCKET_VERSION'] !== '8') { |
42
|
|
|
return false; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
if (isset($this->server['HTTP_ORIGIN'])) { |
46
|
|
|
$this->server['HTTP_SEC_WEBSOCKET_ORIGIN'] = $this->server['HTTP_ORIGIN']; |
47
|
|
|
} |
48
|
|
|
if (!isset($this->server['HTTP_SEC_WEBSOCKET_ORIGIN'])) { |
49
|
|
|
$this->server['HTTP_SEC_WEBSOCKET_ORIGIN'] = ''; |
50
|
|
|
} |
51
|
|
|
$this->write("HTTP/1.1 101 Switching Protocols\r\n" |
52
|
|
|
. "Upgrade: WebSocket\r\n" |
53
|
|
|
. "Connection: Upgrade\r\n" |
54
|
|
|
. "Date: " . date('r') . "\r\n" |
55
|
|
|
. "Sec-WebSocket-Origin: " . $this->server['HTTP_SEC_WEBSOCKET_ORIGIN'] . "\r\n" |
56
|
|
|
. "Sec-WebSocket-Location: ws://" . $this->server['HTTP_HOST'] . $this->server['REQUEST_URI'] . "\r\n" |
57
|
|
|
. "Sec-WebSocket-Accept: " . base64_encode(sha1(trim($this->server['HTTP_SEC_WEBSOCKET_KEY']) . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true)) . "\r\n" |
|
|
|
|
58
|
|
|
); |
59
|
|
|
if (isset($this->server['HTTP_SEC_WEBSOCKET_PROTOCOL'])) { |
60
|
|
|
$this->writeln("Sec-WebSocket-Protocol: " . $this->server['HTTP_SEC_WEBSOCKET_PROTOCOL']); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
if ($this->pool->config->expose->value) { |
64
|
|
|
$this->writeln('X-Powered-By: phpDaemon/' . Daemon::$version); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
$this->writeln($extraHeaders); |
68
|
|
|
|
69
|
|
|
return true; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Sends a frame. |
75
|
|
|
* @param string $data Frame's data. |
76
|
|
|
* @param string $type Frame's type. ("STRING" OR "BINARY") |
|
|
|
|
77
|
|
|
* @param callable $cb Optional. Callback called when the frame is received by client. |
|
|
|
|
78
|
|
|
* @callback $cb ( ) |
79
|
|
|
* @return boolean Success. |
80
|
|
|
*/ |
81
|
|
|
public function sendFrame($data, $type = null, $cb = null) { |
82
|
|
|
if (!$this->handshaked) { |
83
|
|
|
return false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
if ($this->finished && $type !== 'CONNCLOSE') { |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/*if (in_array($type, ['STRING', 'BINARY']) && ($this->outgoingCompression > 0) && in_array('deflate-frame', $this->extensions)) { |
|
|
|
|
91
|
|
|
//$data = gzcompress($data, $this->outgoingCompression); |
92
|
|
|
//$rsv1 = 1; |
93
|
|
|
}*/ |
94
|
|
|
|
95
|
|
|
$fin = 1; |
96
|
|
|
$rsv1 = 0; |
97
|
|
|
$rsv2 = 0; |
98
|
|
|
$rsv3 = 0; |
99
|
|
|
$this->write(chr(bindec($fin . $rsv1 . $rsv2 . $rsv3 . str_pad(decbin($this->getFrameType($type)), 4, '0', STR_PAD_LEFT)))); |
|
|
|
|
100
|
|
|
$dataLength = strlen($data); |
101
|
|
|
$isMasked = false; |
102
|
|
|
$isMaskedInt = $isMasked ? 128 : 0; |
103
|
|
|
if ($dataLength <= 125) { |
104
|
|
|
$this->write(chr($dataLength + $isMaskedInt)); |
105
|
|
|
} |
106
|
|
|
elseif ($dataLength <= 65535) { |
107
|
|
|
$this->write(chr(126 + $isMaskedInt) . // 126 + 128 |
108
|
|
|
chr($dataLength >> 8) . |
109
|
|
|
chr($dataLength & 0xFF)); |
110
|
|
|
} |
111
|
|
View Code Duplication |
else { |
|
|
|
|
112
|
|
|
$this->write(chr(127 + $isMaskedInt) . // 127 + 128 |
113
|
|
|
chr($dataLength >> 56) . |
114
|
|
|
chr($dataLength >> 48) . |
115
|
|
|
chr($dataLength >> 40) . |
116
|
|
|
chr($dataLength >> 32) . |
117
|
|
|
chr($dataLength >> 24) . |
118
|
|
|
chr($dataLength >> 16) . |
119
|
|
|
chr($dataLength >> 8) . |
120
|
|
|
chr($dataLength & 0xFF)); |
121
|
|
|
} |
122
|
|
View Code Duplication |
if ($isMasked) { |
|
|
|
|
123
|
|
|
$mask = chr(mt_rand(0, 0xFF)) . |
124
|
|
|
chr(mt_rand(0, 0xFF)) . |
125
|
|
|
chr(mt_rand(0, 0xFF)) . |
126
|
|
|
chr(mt_rand(0, 0xFF)); |
127
|
|
|
$this->write($mask . $this->mask($data, $mask)); |
128
|
|
|
} |
129
|
|
|
else { |
130
|
|
|
$this->write($data); |
131
|
|
|
} |
132
|
|
|
if ($cb !== null) { |
133
|
|
|
$this->onWriteOnce($cb); |
134
|
|
|
} |
135
|
|
|
return true; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Apply mask |
140
|
|
|
* @param $data |
141
|
|
|
* @param string|false $mask |
142
|
|
|
* @return mixed |
143
|
|
|
*/ |
144
|
|
|
public function mask($data, $mask) { |
145
|
|
|
for ($i = 0, $l = strlen($data), $ml = strlen($mask); $i < $l; $i++) { |
146
|
|
|
$data[$i] = $data[$i] ^ $mask[$i % $ml]; |
147
|
|
|
} |
148
|
|
|
return $data; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** |
152
|
|
|
* Called when new data received |
153
|
|
|
* @see http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10#page-16 |
154
|
|
|
* @return void |
155
|
|
|
*/ |
156
|
|
|
public function onRead() { |
157
|
|
|
if ($this->state === self::STATE_PREHANDSHAKE) { |
158
|
|
|
if (!$this->handshake()) { |
|
|
|
|
159
|
|
|
return; |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
if ($this->state === self::STATE_HANDSHAKED) { |
163
|
|
|
|
164
|
|
|
while (($buflen = $this->getInputLength()) >= 2) { |
165
|
|
|
$first = ord($this->look(1)); // first byte integer (fin, opcode) |
166
|
|
|
$firstBits = decbin($first); |
167
|
|
|
$opcode = (int)bindec(substr($firstBits, 4, 4)); |
168
|
|
|
if ($opcode === 0x8) { // CLOSE |
169
|
|
|
$this->finish(); |
170
|
|
|
return; |
171
|
|
|
} |
172
|
|
|
$opcodeName = isset(static::$opcodes[$opcode]) ? static::$opcodes[$opcode] : false; |
173
|
|
|
if (!$opcodeName) { |
174
|
|
|
Daemon::log(get_class($this) . ': Undefined opcode ' . $opcode); |
175
|
|
|
$this->finish(); |
176
|
|
|
return; |
177
|
|
|
} |
178
|
|
|
$second = ord($this->look(1, 1)); // second byte integer (masked, payload length) |
179
|
|
|
$fin = (bool)($first >> 7); |
180
|
|
|
$isMasked = (bool)($second >> 7); |
181
|
|
|
$dataLength = $second & 0x7f; |
182
|
|
|
$p = 2; |
183
|
|
|
if ($dataLength === 0x7e) { // 2 bytes-length |
184
|
|
|
if ($buflen < $p + 2) { |
185
|
|
|
return; // not enough data yet |
186
|
|
|
} |
187
|
|
|
$dataLength = Binary::bytes2int($this->look(2, $p), false); |
|
|
|
|
188
|
|
|
$p += 2; |
189
|
|
|
} elseif ($dataLength === 0x7f) { // 8 bytes-length |
190
|
|
|
if ($buflen < $p + 8) { |
191
|
|
|
return; // not enough data yet |
192
|
|
|
} |
193
|
|
|
$dataLength = Binary::bytes2int($this->look(8, $p)); |
|
|
|
|
194
|
|
|
$p += 8; |
195
|
|
|
} |
196
|
|
|
if ($this->pool->maxAllowedPacket <= $dataLength) { |
197
|
|
|
// Too big packet |
198
|
|
|
$this>finish(); |
199
|
|
|
return; |
200
|
|
|
} |
201
|
|
|
if ($isMasked) { |
202
|
|
|
if ($buflen < $p + 4) { |
203
|
|
|
return; // not enough data yet |
204
|
|
|
} |
205
|
|
|
$mask = $this->look(4, $p); |
206
|
|
|
$p += 4; |
207
|
|
|
} |
208
|
|
|
if ($buflen < $p + $dataLength) { |
209
|
|
|
return; // not enough data yet |
210
|
|
|
} |
211
|
|
|
$this->drain($p); |
212
|
|
|
$data = $this->read($dataLength); |
213
|
|
|
if ($isMasked) { |
214
|
|
|
$data = $this->mask($data, $mask); |
|
|
|
|
215
|
|
|
} |
216
|
|
|
//Daemon::log(Debug::dump(array('ext' => $this->extensions, 'rsv1' => $firstBits[1], 'data' => Debug::exportBytes($data)))); |
|
|
|
|
217
|
|
|
/*if ($firstBits[1] && in_array('deflate-frame', $this->extensions)) { // deflate frame |
|
|
|
|
218
|
|
|
$data = gzuncompress($data, $this->pool->maxAllowedPacket); |
219
|
|
|
}*/ |
220
|
|
|
if (!$fin) { |
221
|
|
|
$this->framebuf .= $data; |
222
|
|
|
} else { |
223
|
|
|
$this->onFrame($this->framebuf . $data, $opcodeName); |
224
|
|
|
$this->framebuf = ''; |
225
|
|
|
} |
226
|
|
|
} |
227
|
|
|
} |
228
|
|
|
} |
229
|
|
|
} |
230
|
|
|
|
Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.