Repository navigation
Expand file tree
/
Copy pathAttachWebsocketStream.php
More file actions
231 lines (200 loc) · 7.16 KB
/
Copy pathAttachWebsocketStream.php
File metadata and controls
231 lines (200 loc) · 7.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
<?php
declare(strict_types=1);
namespace Docker\Stream;
use Psr\Http\Message\StreamInterface;
/**
* An interactive stream is used when communicating with an attached docker container.
*
* It helps dealing with encoding and decoding frame from websocket protocol (hybi 10)
*
* @see https://tools.ietf.org/html/rfc6455#section-5.2
*/
class AttachWebsocketStream
{
private const OPCODE_BINARY = 0x2;
private const OPCODE_CLOSE = 0x8;
private const OPCODE_PING = 0x9;
private const OPCODE_PONG = 0xA;
/** @var resource The underlying socket */
private $socket;
public function __construct(StreamInterface $stream)
{
$this->socket = $stream->detach();
}
/**
* Send input to the container as one binary frame.
*
* @param string $data Data to send
*/
public function write($data): void
{
if (!\is_resource($this->socket)) {
throw new \RuntimeException('The WebSocket stream is closed.');
}
$this->sendFrame(self::OPCODE_BINARY, (string) $data);
}
/**
* Send a close frame and close the connection.
*
* Later read() calls return null. Calling close() again does nothing.
*/
public function close(): void
{
if (!\is_resource($this->socket)) {
return;
}
try {
// A close frame without a status code (RFC 6455 section 5.5.1). The
// daemon may already have closed its side, so a failed write is ignored.
$this->sendFrame(self::OPCODE_CLOSE, '');
} catch (\RuntimeException) {
}
fclose($this->socket);
}
/**
* Wait for the next data frame.
*
* Ping frames are answered with a pong and a close frame closes the stream;
* neither is returned. Docker sends output as it is written, so a frame is a
* chunk of output rather than a line or a whole message.
*
* @param int $waitTime Time to wait in seconds before return false
* @param int $waitMicroTime Time to wait in microseconds before return false
* @param bool $getFrame Whether to return the frame of websocket or only the data
*
* @return false|string|array|null Null once the stream is closed, false if no frame arrived in time, the frame data, or the frame array if $getFrame is set to true
*/
public function read($waitTime = 0, $waitMicroTime = 200000, $getFrame = false)
{
while (true) {
if (!\is_resource($this->socket) || feof($this->socket)) {
return null;
}
$read = [$this->socket];
$write = null;
$expect = null;
$selected = @stream_select($read, $write, $expect, $waitTime, $waitMicroTime);
if (false === $selected) {
throw new \RuntimeException('Cannot wait for the WebSocket stream.');
}
if (0 === $selected) {
return false;
}
$frame = $this->readFrame();
if (null === $frame) {
// The daemon closed the connection before sending a whole frame.
fclose($this->socket);
return null;
}
if (self::OPCODE_CLOSE === $frame['opcode']) {
$this->close();
return null;
}
if (self::OPCODE_PING === $frame['opcode']) {
$this->sendFrame(self::OPCODE_PONG, $frame['data']);
continue;
}
if (self::OPCODE_PONG === $frame['opcode']) {
continue;
}
return $getFrame ? $frame : $frame['data'];
}
}
/**
* Read one frame, or return null when the connection ends first.
*/
private function readFrame(): ?array
{
$header = $this->socketRead(2);
if (2 !== \strlen($header)) {
return null;
}
$firstByte = \ord($header[0]);
$secondByte = \ord($header[1]);
$frame = [
'fin' => ($firstByte & 128) >> 7,
'rsv1' => ($firstByte & 64) >> 6,
'rsv2' => ($firstByte & 32) >> 5,
'rsv3' => ($firstByte & 16) >> 4,
'opcode' => $firstByte & 15,
'mask' => ($secondByte & 128) >> 7,
'len' => $secondByte & 127,
];
if (126 === $frame['len'] || 127 === $frame['len']) {
$size = 126 === $frame['len'] ? 2 : 8;
$length = $this->socketRead($size);
if ($size !== \strlen($length)) {
return null;
}
$frame['len'] = unpack(2 === $size ? 'n' : 'J', $length)[1];
}
if (1 === $frame['mask']) {
$frame['mask_key'] = $this->socketRead(4);
if (4 !== \strlen($frame['mask_key'])) {
return null;
}
}
$frame['data'] = $this->socketRead($frame['len']);
if ($frame['len'] !== \strlen($frame['data'])) {
return null;
}
if (1 === $frame['mask']) {
$frame['data'] = $this->mask($frame['data'], $frame['mask_key']);
}
return $frame;
}
/**
* Send one masked frame, as clients must (RFC 6455 section 5.3).
*/
private function sendFrame(int $opcode, string $data): void
{
$length = \strlen($data);
// 126 carries a 16-bit length, so anything above 0xFFFF needs the 64-bit form.
if ($length > 0xFFFF) {
$header = \chr(0x80 | 127).pack('J', $length);
} elseif ($length > 125) {
$header = \chr(0x80 | 126).pack('n', $length);
} else {
$header = \chr(0x80 | $length);
}
$maskKey = random_bytes(4);
$this->socketWrite(\chr(0x80 | $opcode).$header.$maskKey.$this->mask($data, $maskKey));
}
private function mask(string $data, string $maskKey): string
{
if ('' === $data) {
return '';
}
return $data ^ str_repeat($maskKey, intdiv(\strlen($data), 4) + 1);
}
/**
* Read exactly $length bytes, or fewer if the connection ends first.
*/
private function socketRead(int $length): string
{
$read = '';
// Empty frames carry no payload; fread() rejects a zero length.
while (\strlen($read) < $length && !feof($this->socket)) {
$chunk = @fread($this->socket, $length - \strlen($read));
// A stalled read returns false or '' depending on the PHP version.
if (stream_get_meta_data($this->socket)['timed_out']) {
throw new \RuntimeException('Timed out reading a WebSocket frame.');
}
if (false === $chunk) {
throw new \RuntimeException('Cannot read from the WebSocket stream.');
}
$read .= $chunk;
}
return $read;
}
private function socketWrite(string $data): void
{
while ('' !== $data) {
$written = @fwrite($this->socket, $data);
if (false === $written || 0 === $written) {
throw new \RuntimeException('Cannot write to the WebSocket stream.');
}
$data = (string) substr($data, $written);
}
}
}