8000 Merge pull request #150 from clue-labs/err · reactphp/stream@610aa60 · GitHub
[go: up one dir, main page]

Skip to content

Commit 610aa60

Browse files
authored
Merge pull request #150 from clue-labs/err
Fix faulty write buffer behavior when sending large data chunks over TLS (Mac OS X only)
2 parents 1a849e7 + 26d5640 commit 610aa60

File tree

3 files changed

+22
-38
lines changed

3 files changed

+22
-38
lines changed

.travis.yml

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,17 @@ matrix:
1818
- php: hhvm-3.18
1919
install:
2020
- composer require phpunit/phpunit:^5 --dev --no-interaction # requires legacy phpunit
21-
- os: osx
21+
- name: Mac OS X
22+
os: osx
2223
language: generic
23-
php: 7.0 # just to look right on travis
24-
env:
25-
- PACKAGE: php70
24+
before_install:
25+
- curl -s http://getcomposer.org/installer | php
26+
- mv composer.phar /usr/local/bin/composer
2627
allow_failures:
2728
- php: hhvm-3.18
2829
- os: osx
2930

3031
install:
31-
# OSX install inspired by https://github.com/kiler129/TravisCI-OSX-PHP
32-
- |
33-
if [[ "${TRAVIS_OS_NAME}" == "osx" ]]; then
34-
brew tap homebrew/homebrew-php
35-
echo "Installing PHP ..."
36-
brew install "${PACKAGE}"
37-
brew install "${PACKAGE}"-xdebug
38-
brew link "${PACKAGE}"
39-
echo "Installing composer ..."
40-
curl -s http://getcomposer.org/installer | php
41-
mv composer.phar /usr/local/bin/composer
42-
fi
4332
- composer install --no-interaction
4433

4534
script:

src/WritableResourceStream.php

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,8 @@ public function close()
113113
public function handleWrite()
114114
{
115115
$error = null;
116-
\set_error_handler(function ($errno, $errstr, $errfile, $errline) use (&$error) {
117-
$error = array(
118-
'message' => $errstr,
119-
'number' => $errno,
120-
'file' => $errfile,
121-
'line' => $errline
122-
);
116+
\set_error_handler(function ($_, $errstr) use (&$error) {
117+
$error = $errstr;
123118
});
124119

125120
if ($this->writeChunkSize === -1) {
@@ -130,25 +125,16 @@ public function handleWrite()
130125

131126
\restore_error_handler();
132127

133-
// Only report errors if *nothing* could be sent.
128+
// Only report errors if *nothing* could be sent and an error has been raised.
129+
// Ignore non-fatal warnings if *some* data could be sent.
134130
// Any hard (permanent) error will fail to send any data at all.
135131
// Sending excessive amounts of data will only flush *some* data and then
136132
// report a temporary error (EAGAIN) which we do not raise here in order
137133
// to keep the stream open for further tries to write.
138134
// Should this turn out to be a permanent error later, it will eventually
139135
// send *nothing* and we can detect this.
140-
if ($sent === 0 || $sent === false) {
141-
if ($error !== null) {
142-
$error = new \ErrorException(
143-
$error['message'],
144-
0,
145-
$error['number'],
146-
$error['file'],
147-
$error['line']
148-
);
149-
}
150-
151-
$this->emit('error', array(new \RuntimeException('Unable to write to stream: ' . ($error !== null ? $error->getMessage() : 'Unknown error'), 0, $error)));
136+
if (($sent === 0 || $sent === false) && $error !== null) {
137+
$this->emit('error', array(new \RuntimeException('Unable to write to stream: ' . $error)));
152138
$this->close();
153139

154140
return;

tests/FunctionalInternetTest.php

Lines changed: 11 additions & 2 deletions
B06F
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,20 @@ public function testUploadKilobyteSecure()
7878
$this->assertNotEquals('', $buffer);
7979
}
8080

81-
public function testUploadBiggerBlockSecureRequiresSmallerChunkSize()
81+
public function testUploadBiggerBlockSecure()
8282
{
83-
$size = 50 * 1000;
83+
// A few dozen kilobytes should be enough to verify this works.
84+
// Underlying buffer sizes are platform-specific, so let's increase this
85+
// a bit to trigger different behavior on Linux vs Mac OS X.
86+
$size = 136 * 1000;
87+
8488
$stream = stream_socket_client('tls://httpbin.org:443');
8589

90+
// PHP < 7.1.4 (and PHP < 7.0.18) suffers from a bug when writing big
91+
// chunks of data over TLS streams at once.
92+
// We work around this by limiting the write chunk size to 8192 bytes
93+
// here to also support older PHP versions.
94+
// See https://github.com/reactphp/socket/issues/105
8695
$loop = Factory::create();
8796
$stream = new DuplexResourceStream(
8897
$stream,

0 commit comments

Comments
 (0)
0