Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Work around write chunk size for TLS streams for PHP < 7.1.4 #114

Merged
merged 1 commit into from
Sep 8, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,13 @@ IRC over TLS, but should not affect HTTP over TLS (HTTPS).
Further investigation of this issue is needed.
For more insights, this issue is also covered by our test suite.

PHP < 7.1.4 (and PHP < 7.0.18) suffers from a bug when writing big
chunks of data over TLS streams at once.
We try to work around this by limiting the write chunk size to 8192
bytes for older PHP versions only.
This is only a work-around and has a noticable performance penalty on
affected versions.

This project also supports running on HHVM.
Note that really old HHVM < 3.8 does not support secure TLS connections, as it
lacks the required `stream_socket_enable_crypto()` function.
Expand Down
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@
"evenement/evenement": "^3.0 || ^2.0 || ^1.0",
"react/dns": "^0.4.11",
"react/event-loop": "^1.0 || ^0.5 || ^0.4 || ^0.3.5",
"react/stream": "^1.0 || ^0.7 || ^0.6 || ^0.5 || ^0.4.5",
"react/stream": "^1.0 || ^0.7.1",
"react/promise": "^2.1 || ^1.2",
"react/promise-timer": "~1.0"
},
"require-dev": {
"clue/block-react": "^1.1",
"phpunit/phpunit": "~4.8",
"react/stream": "^1.0 || ^0.7 || ^0.6"
"phpunit/phpunit": "~4.8"
},
"autoload": {
"psr-4": {
Expand Down
34 changes: 19 additions & 15 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
use Evenement\EventEmitter;
use React\EventLoop\LoopInterface;
use React\Stream\DuplexResourceStream;
use React\Stream\Stream;
use React\Stream\Util;
use React\Stream\WritableStreamInterface;
use React\Stream\WritableResourceStream;

/**
* The actual connection implementation for ConnectionInterface
Expand Down Expand Up @@ -50,20 +50,24 @@ public function __construct($resource, LoopInterface $loop)
// See https://bugs.php.net/bug.php?id=65137
// https://bugs.php.net/bug.php?id=41631
// https://github.com/reactphp/socket-client/issues/24
$clearCompleteBuffer = (version_compare(PHP_VERSION, '5.6.8', '<'));

// @codeCoverageIgnoreStart
if (class_exists('React\Stream\Stream')) {
// legacy react/stream < 0.7 requires additional buffer property
$this->input = new Stream($resource, $loop);
if ($clearCompleteBuffer) {
$this->input->bufferSize = null;
}
} else {
// preferred react/stream >= 0.7 accepts buffer parameter
$this->input = new DuplexResourceStream($resource, $loop, $clearCompleteBuffer ? -1 : null);
}
// @codeCoverageIgnoreEnd
$clearCompleteBuffer = PHP_VERSION_ID < 50608;

// PHP < 7.1.4 (and PHP < 7.0.18) suffers from a bug when writing big
// chunks of data over TLS streams at once.
// We try to work around this by limiting the write chunk size to 8192
// bytes for older PHP versions only.
// This is only a work-around and has a noticable performance penalty on
// affected versions. Please update your PHP version.
// This applies to all streams because TLS may be enabled later on.
// See https://github.com/reactphp/socket/issues/105
$limitWriteChunks = (PHP_VERSION_ID < 70018 || (PHP_VERSION_ID >= 70100 && PHP_VERSION_ID < 70104));

$this->input = new DuplexResourceStream(
$resource,
$loop,
$clearCompleteBuffer ? -1 : null,
new WritableResourceStream($resource, $loop, null, $limitWriteChunks ? 8192 : null)
);

$this->stream = $resource;

Expand Down
32 changes: 32 additions & 0 deletions tests/FunctionalSecureServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,38 @@ public function testWritesDataInMultipleChunksToConnection()
$this->assertEquals(400000, $received);
}

public function testWritesMoreDataInMultipleChunksToConnection()
{
$loop = Factory::create();

$server = new TcpServer(0, $loop);
$server = new SecureServer($server, $loop, array(
'local_cert' => __DIR__ . '/../examples/localhost.pem'
));
$server->on('connection', $this->expectCallableOnce());

$server->on('connection', function (ConnectionInterface $conn) {
$conn->write(str_repeat('*', 2000000));
});

$connector = new SecureConnector(new TcpConnector($loop), $loop, array(
'verify_peer' => false
));
$promise = $connector->connect($server->getAddress());

$local = Block\await($promise, $loop, self::TIMEOUT);
/* @var $local React\Stream\Stream */

$received = 0;
$local->on('data', function ($chunk) use (&$received) {
$received += strlen($chunk);
});

Block\sleep(self::TIMEOUT, $loop);

$this->assertEquals(2000000, $received);
}

public function testEmitsDataFromConnection()
{
$loop = Factory::create();
Expand Down