← 输入框|github.com|刷新
Skip to content

Commit 459a1f2

Browse files
committed
feat(websocket): add close() to attach streams
Send a masked close frame and close the socket. A peer that has already closed is ignored, and a second close() does nothing. read() returns null afterwards, and write() throws a RuntimeException instead of a TypeError from fwrite(). Closes #355
1 parent 17b620c commit 459a1f2

3 files changed

Lines changed: 64 additions & 4 deletions

File tree

‎docs/reference/streams.mdx‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ TTY-aware decoder or handle all upgraded-response variants. Do not assume
132132
that terminal output or HTTP 101 will work through this wrapper.
133133

134134
`containerAttachWebsocket()` uses a separate `AttachWebsocketStream` with
135-
`read()` and `write()`, not `onStdout()`/`wait()`. Its response handling is also
136-
narrow, and the existing WebSocket integration test is skipped. Treat it as
137-
a feature requiring validation with your daemon and transport, not a tested
138-
interactive-terminal recipe.
135+
`read()`, `write()` and `close()`, not `onStdout()`/`wait()`. Its response
136+
handling is also narrow, and the existing WebSocket integration test is
137+
skipped. Treat it as a feature requiring validation with your daemon and
138+
transport, not a tested interactive-terminal recipe.
139139

140140
`DockerRawStream::onStdin()` is an output-channel callback; it is not a method
141141
for writing commands into a container. Prefer the [exec recipe](/guides/exec)

‎src/Stream/AttachWebsocketStream.php‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,10 @@ public function __construct(StreamInterface $stream)
3030
*/
3131
public function write($data): void
3232
{
33+
if (!\is_resource($this->socket)) {
34+
throw new \RuntimeException('The WebSocket stream is closed.');
35+
}
36+
3337
$rand = random_int(0, 28);
3438
$frame = [
3539
'fin' => 1,
@@ -80,6 +84,23 @@ public function write($data): void
8084
$this->socketWrite($frame['data']);
8185
}
8286

87+
/**
88+
* Send a close frame and close the connection.
89+
*
90+
* Later read() calls return null. Calling close() again does nothing.
91+
*/
92+
public function close(): void
93+
{
94+
if (!\is_resource($this->socket)) {
95+
return;
96+
}
97+
98+
// Masked close frame without a status code (RFC 6455 section 5.5.1).
99+
// The daemon may already have closed its side, so a failed write is ignored.
100+
@$this->socketWrite("\x88\x80".random_bytes(4));
101+
fclose($this->socket);
102+
}
103+
83104
/**
84105
* Block until it receive a frame from websocket or return null if no more connexion.
85106
*

‎tests/Stream/AttachWebsocketStreamTest.php‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,43 @@ public function testReadsEmptyFrameBeforePayload(): void
2222
$this->assertSame('hi', $stream->read());
2323
fclose($server);
2424
}
25+
26+
public function testCloseSendsCloseFrameAndClosesSocket(): void
27+
{
28+
[$client, $server] = stream_socket_pair(\STREAM_PF_UNIX, \STREAM_SOCK_STREAM, \STREAM_IPPROTO_IP);
29+
$stream = new AttachWebsocketStream(Stream::create($client));
30+
31+
$stream->close();
32+
$stream->close();
33+
34+
$frame = fread($server, 16);
35+
$this->assertSame("\x88\x80", substr($frame, 0, 2));
36+
$this->assertSame(6, \strlen($frame));
37+
$this->assertSame('', fread($server, 1));
38+
$this->assertTrue(feof($server));
39+
$this->assertNull($stream->read());
40+
fclose($server);
41+
}
42+
43+
public function testCloseIgnoresPeerThatAlreadyClosed(): void
44+
{
45+
[$client, $server] = stream_socket_pair(\STREAM_PF_UNIX, \STREAM_SOCK_STREAM, \STREAM_IPPROTO_IP);
46+
fclose($server);
47+
$stream = new AttachWebsocketStream(Stream::create($client));
48+
49+
$stream->close();
50+
51+
$this->assertNull($stream->read());
52+
}
53+
54+
public function testWriteAfterCloseThrows(): void
55+
{
56+
[$client, $server] = stream_socket_pair(\STREAM_PF_UNIX, \STREAM_SOCK_STREAM, \STREAM_IPPROTO_IP);
57+
$stream = new AttachWebsocketStream(Stream::create($client));
58+
$stream->close();
59+
fclose($server);
60+
61+
$this->expectException(\RuntimeException::class);
62+
$stream->write('exit');
63+
}
2564
}

0 commit comments

Comments
 (0)