Skip to content

Commit f0bd619

Browse files
committed
Do not expose $master property
Interfering with this (previously public) property could introduce some very subtle bugs, so it's best to simply avoid exposing it. Empirical evidence seems to suggest this isn't used much outside of this package anyway. Inside this package, we can safely replace all references to it with references to child sockets created upon connection.
1 parent f4d7314 commit f0bd619

File tree

5 files changed

+20
-29
lines changed

5 files changed

+20
-29
lines changed

src/SecureServer.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ class SecureServer extends EventEmitter implements ServerInterface
5656
{
5757
private $tcp;
5858
private $encryption;
59+
private $context;
5960

6061
/**
6162
* Creates a secure TLS server and starts waiting for incoming connections
@@ -106,27 +107,19 @@ class SecureServer extends EventEmitter implements ServerInterface
106107
* @param Server $tcp
107108
* @param LoopInterface $loop
108109
* @param array $context
109-
* @throws ConnectionException
110110
* @see Server
111111
* @link http://php.net/manual/en/context.ssl.php for TLS context options
112112
*/
113113
public function __construct(Server $tcp, LoopInterface $loop, array $context)
114114
{
115-
if (!is_resource($tcp->master)) {
116-
throw new ConnectionException('TCP server already shut down');
117-
}
118-
119115
// default to empty passphrase to surpress blocking passphrase prompt
120116
$context += array(
121117
'passphrase' => ''
122118
);
123119

124-
foreach ($context as $name => $value) {
125-
stream_context_set_option($tcp->master, 'ssl', $name, $value);
126-
}
127-
128120
$this->tcp = $tcp;
129121
$this->encryption = new StreamEncryption($loop);
122+
$this->context = $context;
130123

131124
$that = $this;
132125
$this->tcp->on('connection', function ($connection) use ($that) {
@@ -156,6 +149,10 @@ public function handleConnection(ConnectionInterface $connection)
156149
return;
157150
}
158151

152+
foreach ($this->context as $name => $value) {
153+
stream_context_set_option($connection->stream, 'ssl', $name, $value);
154+
}
155+
159156
$that = $this;
160157

161158
$this->encryption->enable($connection)->then(

src/Server.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
*/
3737
class Server extends EventEmitter implements ServerInterface
3838
{
39-
public $master;
39+
private $master;
4040
private $loop;
4141

4242
/**

tests/FunctionalSecureServerTest.php

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -368,19 +368,6 @@ public function testEmitsErrorIfConnectionIsNotSecureHandshake()
368368
Block\sleep(self::TIMEOUT, $loop);
369369
}
370370

371-
/**
372-
* @expectedException React\Socket\ConnectionException
373-
*/
374-
public function testFailsToCreateSecureServerOnClosedSocket()
375-
{
376-
$loop = Factory::create();
377-
378-
$server = new Server(0, $loop);
379-
$server->close();
380-
381-
new SecureServer($server, $loop, array());
382-
}
383-
384371
private function getPort(ServerInterface $server)
385372
{
386373
return parse_url($server->getAddress(), PHP_URL_PORT);

tests/FunctionalServerTest.php

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ public function testEmitsConnectionWithLocalIpv6()
229229
$this->assertEquals($server->getAddress(), $local);
230230
}
231231

232-
public function testAppliesContextOptionsToSocketStreamResource()
232+
public function testEmitsConnectionWithInheritedContextOptions()
233233
{
234234
if (defined('HHVM_VERSION') && version_compare(HHVM_VERSION, '3.13', '<')) {
235235
// https://3v4l.org/hB4Tc
@@ -242,7 +242,18 @@ public function testAppliesContextOptionsToSocketStreamResource()
242242
'backlog' => 4
243243
));
244244

245-
$all = stream_context_get_options($server->master);
245+
$all = null;
246+
$server->on('connection', function (ConnectionInterface $conn) use (&$all) {
247+
$all = stream_context_get_options($conn->stream);
248+
});
249+
$port = $this->getPort($server);
250+
251+
$connector = new TcpConnector($loop);
252+
$promise = $connector->create('127.0.0.1', $port);
253+
254+
$promise->then($this->expectCallableOnce());
255+
256+
Block\sleep(0.1, $loop);
246257

247258
$this->assertEquals(array('socket' => array('backlog' => 4)), $all);
248259
}

tests/SecureServerTest.php

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ public function testGetAddressWillBePassedThroughToTcpServer()
1717
{
1818
$tcp = $this->getMockBuilder('React\Socket\Server')->disableOriginalConstructor()->getMock();
1919
$tcp->expects($this->once())->method('getAddress')->willReturn('127.0.0.1:1234');
20-
$tcp->master = stream_socket_server('tcp://localhost:0');
2120

2221
$loop = $this->getMock('React\EventLoop\LoopInterface');
2322

@@ -30,7 +29,6 @@ public function testCloseWillBePassedThroughToTcpServer()
3029
{
3130
$tcp = $this->getMockBuilder('React\Socket\Server')->disableOriginalConstructor()->getMock();
3231
$tcp->expects($this->once())->method('close');
33-
$tcp->master = stream_socket_server('tcp://localhost:0');
3432

3533
$loop = $this->getMock('React\EventLoop\LoopInterface');
3634

@@ -42,7 +40,6 @@ public function testCloseWillBePassedThroughToTcpServer()
4240
public function testConnectionWillBeEndedWithErrorIfItIsNotAStream()
4341
{
4442
$tcp = $this->getMockBuilder('React\Socket\Server')->disableOriginalConstructor()->setMethods(null)->getMock();
45-
$tcp->master = stream_socket_server('tcp://localhost:0');
4643

4744
$loop = $this->getMock('React\EventLoop\LoopInterface');
4845

@@ -59,7 +56,6 @@ public function testConnectionWillBeEndedWithErrorIfItIsNotAStream()
5956
public function testSocketErrorWillBeForwarded()
6057
{
6158
$tcp = $this->getMockBuilder('React\Socket\Server')->disableOriginalConstructor()->setMethods(null)->getMock();
62-
$tcp->master = stream_socket_server('tcp://localhost:0');
6359

6460
$loop = $this->getMock('React\EventLoop\LoopInterface');
6561

0 commit comments

Comments
 (0)