|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace WSSC\Components; |
|
4
|
|
|
|
|
5
|
|
|
use WSSC\Contracts\CommonsContract; |
|
6
|
|
|
use WSSC\Exceptions\BadOpcodeException; |
|
7
|
|
|
use WSSC\Exceptions\BadUriException; |
|
8
|
|
|
use WSSC\Exceptions\ConnectionException; |
|
9
|
|
|
|
|
10
|
|
|
trait WSClientTrait |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Validates whether server sent valid upgrade response |
|
15
|
|
|
* |
|
16
|
|
|
* @param ClientConfig $config |
|
17
|
|
|
* @param string $pathWithQuery |
|
18
|
|
|
* @param string $key |
|
19
|
|
|
* @throws ConnectionException |
|
20
|
|
|
*/ |
|
21
|
|
|
private function validateResponse(ClientConfig $config, string $pathWithQuery, string $key) |
|
22
|
|
|
{ |
|
23
|
|
|
$response = stream_get_line($this->socket, self::DEFAULT_RESPONSE_HEADER, "\r\n\r\n"); |
|
|
|
|
|
|
24
|
|
|
if (!preg_match(self::SEC_WEBSOCKET_ACCEPT_PTTRN, $response, $matches)) { |
|
25
|
|
|
$address = $config->getScheme() . '://' . $config->getHost() . $pathWithQuery; |
|
26
|
|
|
throw new ConnectionException( |
|
27
|
|
|
"Connection to '{$address}' failed: Server sent invalid upgrade response:\n" |
|
28
|
|
|
. $response, CommonsContract::CLIENT_INVALID_UPGRADE_RESPONSE |
|
29
|
|
|
); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$keyAccept = trim($matches[1]); |
|
33
|
|
|
$expectedResonse = base64_encode(pack('H*', sha1($key . self::SERVER_KEY_ACCEPT))); |
|
34
|
|
|
if ($keyAccept !== $expectedResonse) { |
|
35
|
|
|
throw new ConnectionException('Server sent bad upgrade response.', |
|
36
|
|
|
CommonsContract::CLIENT_INVALID_UPGRADE_RESPONSE); |
|
37
|
|
|
} |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Gets host uri based on protocol |
|
42
|
|
|
* |
|
43
|
|
|
* @param ClientConfig $config |
|
44
|
|
|
* @return string |
|
45
|
|
|
* @throws BadUriException |
|
46
|
|
|
*/ |
|
47
|
|
|
private function getHostUri(ClientConfig $config): string |
|
48
|
|
|
{ |
|
49
|
|
|
if (in_array($config->getScheme(), ['ws', 'wss'], true) === false) { |
|
50
|
|
|
throw new BadUriException( |
|
51
|
|
|
"Url should have scheme ws or wss, not '{$config->getScheme()}' from URI '$this->socketUrl' .", |
|
|
|
|
|
|
52
|
|
|
CommonsContract::CLIENT_INCORRECT_SCHEME |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
return ($config->getScheme() === 'wss' ? 'ssl' : 'tcp') . '://' . $config->getHost(); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $data |
|
61
|
|
|
* @return float|int |
|
62
|
|
|
* @throws ConnectionException |
|
63
|
|
|
*/ |
|
64
|
|
|
private function getPayloadLength(string $data) |
|
65
|
|
|
{ |
|
66
|
|
|
$payloadLength = (int)ord($data[1]) & self::MASK_127; // Bits 1-7 in byte 1 |
|
67
|
|
|
if ($payloadLength > self::MASK_125) { |
|
68
|
|
|
if ($payloadLength === self::MASK_126) { |
|
69
|
|
|
$data = $this->read(2); // 126: Payload is a 16-bit unsigned int |
|
|
|
|
|
|
70
|
|
|
} else { |
|
71
|
|
|
$data = $this->read(8); // 127: Payload is a 64-bit unsigned int |
|
|
|
|
|
|
72
|
|
|
} |
|
73
|
|
|
$payloadLength = bindec(self::sprintB($data)); |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
return $payloadLength; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* @param string $data |
|
81
|
|
|
* @param int $payloadLength |
|
82
|
|
|
* @return string |
|
83
|
|
|
* @throws ConnectionException |
|
84
|
|
|
*/ |
|
85
|
|
|
private function getPayloadData(string $data, int $payloadLength): string |
|
86
|
|
|
{ |
|
87
|
|
|
// Masking? |
|
88
|
|
|
$mask = (bool)(ord($data[1]) >> 7); // Bit 0 in byte 1 |
|
89
|
|
|
$payload = ''; |
|
90
|
|
|
$maskingKey = ''; |
|
91
|
|
|
|
|
92
|
|
|
// Get masking key. |
|
93
|
|
|
if ($mask) { |
|
94
|
|
|
$maskingKey = $this->read(4); |
|
|
|
|
|
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
// Get the actual payload, if any (might not be for e.g. close frames. |
|
98
|
|
|
if ($payloadLength > 0) { |
|
99
|
|
|
$data = $this->read($payloadLength); |
|
|
|
|
|
|
100
|
|
|
|
|
101
|
|
|
if ($mask) { |
|
102
|
|
|
// Unmask payload. |
|
103
|
|
|
for ($i = 0; $i < $payloadLength; $i++) { |
|
104
|
|
|
$payload .= ($data[$i] ^ $maskingKey[$i % 4]); |
|
105
|
|
|
} |
|
106
|
|
|
} else { |
|
107
|
|
|
$payload = $data; |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
return $payload; |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* @return null|string |
|
116
|
|
|
* @throws \WSSC\Exceptions\BadOpcodeException |
|
117
|
|
|
* @throws \InvalidArgumentException |
|
118
|
|
|
* @throws BadOpcodeException |
|
119
|
|
|
* @throws BadUriException |
|
120
|
|
|
* @throws ConnectionException |
|
121
|
|
|
* @throws \Exception |
|
122
|
|
|
*/ |
|
123
|
|
|
protected function receiveFragment() |
|
124
|
|
|
{ |
|
125
|
|
|
// Just read the main fragment information first. |
|
126
|
|
|
$data = $this->read(2); |
|
|
|
|
|
|
127
|
|
|
|
|
128
|
|
|
// Is this the final fragment? // Bit 0 in byte 0 |
|
129
|
|
|
/// @todo Handle huge payloads with multiple fragments. |
|
130
|
|
|
$final = (bool)(ord($data[0]) & 1 << 7); |
|
131
|
|
|
|
|
132
|
|
|
// Parse opcode |
|
133
|
|
|
$opcode_int = ord($data[0]) & 31; // Bits 4-7 |
|
134
|
|
|
$opcode_ints = array_flip(self::$opcodes); |
|
135
|
|
|
if (!array_key_exists($opcode_int, $opcode_ints)) { |
|
136
|
|
|
throw new ConnectionException("Bad opcode in websocket frame: $opcode_int", |
|
137
|
|
|
CommonsContract::CLIENT_BAD_OPCODE); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
$opcode = $opcode_ints[$opcode_int]; |
|
141
|
|
|
|
|
142
|
|
|
// record the opcode if we are not receiving a continutation fragment |
|
143
|
|
|
if ($opcode !== 'continuation') { |
|
144
|
|
|
$this->lastOpcode = $opcode; |
|
|
|
|
|
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
$payloadLength = $this->getPayloadLength($data); |
|
148
|
|
|
$payload = $this->getPayloadData($data, $payloadLength); |
|
149
|
|
|
|
|
150
|
|
|
if ($opcode === CommonsContract::EVENT_TYPE_CLOSE) { |
|
151
|
|
|
// Get the close status. |
|
152
|
|
|
if ($payloadLength >= 2) { |
|
153
|
|
|
$statusBin = $payload[0] . $payload[1]; |
|
154
|
|
|
$status = bindec(sprintf('%08b%08b', ord($payload[0]), ord($payload[1]))); |
|
155
|
|
|
$this->closeStatus = $status; |
|
|
|
|
|
|
156
|
|
|
$payload = substr($payload, 2); |
|
157
|
|
|
|
|
158
|
|
|
if (!$this->isClosing) { |
|
|
|
|
|
|
159
|
|
|
$this->send($statusBin . 'Close acknowledged: ' . $status, |
|
|
|
|
|
|
160
|
|
|
CommonsContract::EVENT_TYPE_CLOSE); // Respond. |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
if ($this->isClosing) { |
|
165
|
|
|
$this->isClosing = false; // A close response, all done. |
|
166
|
|
|
} |
|
167
|
|
|
|
|
168
|
|
|
fclose($this->socket); |
|
169
|
|
|
$this->isConnected = false; |
|
|
|
|
|
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if (!$final) { |
|
173
|
|
|
$this->hugePayload .= $payload; |
|
|
|
|
|
|
174
|
|
|
|
|
175
|
|
|
return NULL; |
|
176
|
|
|
} // this is the last fragment, and we are processing a huge_payload |
|
177
|
|
|
|
|
178
|
|
|
if ($this->hugePayload) { |
|
179
|
|
|
$payload = $this->hugePayload .= $payload; |
|
180
|
|
|
$this->hugePayload = NULL; |
|
181
|
|
|
} |
|
182
|
|
|
|
|
183
|
|
|
return $payload; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* @param $final |
|
188
|
|
|
* @param $payload |
|
189
|
|
|
* @param $opcode |
|
190
|
|
|
* @param $masked |
|
191
|
|
|
* @throws ConnectionException |
|
192
|
|
|
* @throws \Exception |
|
193
|
|
|
*/ |
|
194
|
|
|
protected function sendFragment($final, $payload, $opcode, $masked) |
|
195
|
|
|
{ |
|
196
|
|
|
// Binary string for header. |
|
197
|
|
|
$frameHeadBin = ''; |
|
198
|
|
|
// Write FIN, final fragment bit. |
|
199
|
|
|
$frameHeadBin .= (bool)$final ? '1' : '0'; |
|
200
|
|
|
// RSV 1, 2, & 3 false and unused. |
|
201
|
|
|
$frameHeadBin .= '000'; |
|
202
|
|
|
// Opcode rest of the byte. |
|
203
|
|
|
$frameHeadBin .= sprintf('%04b', self::$opcodes[$opcode]); |
|
204
|
|
|
// Use masking? |
|
205
|
|
|
$frameHeadBin .= $masked ? '1' : '0'; |
|
206
|
|
|
|
|
207
|
|
|
// 7 bits of payload length... |
|
208
|
|
|
$payloadLen = strlen($payload); |
|
209
|
|
|
if ($payloadLen > self::MAX_BYTES_READ) { |
|
210
|
|
|
$frameHeadBin .= decbin(self::MASK_127); |
|
211
|
|
|
$frameHeadBin .= sprintf('%064b', $payloadLen); |
|
212
|
|
|
} else if ($payloadLen > self::MASK_125) { |
|
213
|
|
|
$frameHeadBin .= decbin(self::MASK_126); |
|
214
|
|
|
$frameHeadBin .= sprintf('%016b', $payloadLen); |
|
215
|
|
|
} else { |
|
216
|
|
|
$frameHeadBin .= sprintf('%07b', $payloadLen); |
|
217
|
|
|
} |
|
218
|
|
|
|
|
219
|
|
|
$frame = ''; |
|
220
|
|
|
|
|
221
|
|
|
// Write frame head to frame. |
|
222
|
|
|
foreach (str_split($frameHeadBin, 8) as $binstr) { |
|
223
|
|
|
$frame .= chr(bindec($binstr)); |
|
224
|
|
|
} |
|
225
|
|
|
// Handle masking |
|
226
|
|
|
if ($masked) { |
|
227
|
|
|
// generate a random mask: |
|
228
|
|
|
$mask = ''; |
|
229
|
|
View Code Duplication |
for ($i = 0; $i < 4; $i++) { |
|
|
|
|
|
|
230
|
|
|
$mask .= chr(random_int(0, 255)); |
|
231
|
|
|
} |
|
232
|
|
|
$frame .= $mask; |
|
233
|
|
|
} |
|
234
|
|
|
|
|
235
|
|
|
// Append payload to frame: |
|
236
|
|
View Code Duplication |
for ($i = 0; $i < $payloadLen; $i++) { |
|
|
|
|
|
|
237
|
|
|
$frame .= ($masked === true) ? $payload[$i] ^ $mask[$i % 4] : $payload[$i]; |
|
|
|
|
|
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
$this->write($frame); |
|
|
|
|
|
|
241
|
|
|
} |
|
242
|
|
|
} |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: