GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — feat(websocket): add close() to attach streams · docker-php/docker-php@459a1f2

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • docs/reference
    • streams.mdx
  • src/Stream
    • AttachWebsocketStream.php
  • tests/Stream
    • AttachWebsocketStreamTest.php
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎docs/reference/streams.mdx‎

Copy file name to clipboardExpand all lines: docs/reference/streams.mdx
+4-4Lines changed: 4 additions & 4 deletions
  • Display the source diff
  • Display the rich diff
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)
Collapse file

‎src/Stream/AttachWebsocketStream.php‎

Copy file name to clipboardExpand all lines: src/Stream/AttachWebsocketStream.php
+21Lines 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
*
Collapse file

‎tests/Stream/AttachWebsocketStreamTest.php‎

Copy file name to clipboardExpand all lines: tests/Stream/AttachWebsocketStreamTest.php
+39Lines 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 (0)
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.