-
-
Notifications
You must be signed in to change notification settings - Fork 167
Protect the TCP connection against close from other streams #133
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
<?php | ||
|
||
namespace React\Http; | ||
|
||
use Evenement\EventEmitter; | ||
use React\Stream\ReadableStreamInterface; | ||
use React\Stream\WritableStreamInterface; | ||
use React\Stream\Util; | ||
|
||
/** @internal | ||
* This stream is used to protect the passed stream against closing. | ||
* */ | ||
class CloseProtectionStream extends EventEmitter implements ReadableStreamInterface | ||
{ | ||
private $connection; | ||
private $closed = false; | ||
|
||
/** | ||
* @param ReadableStreamInterface $input stream that will be paused instead of closed on an 'close' event. | ||
*/ | ||
public function __construct(ReadableStreamInterface $input) | ||
{ | ||
$this->input = $input; | ||
|
||
$this->input->on('data', array($this, 'handleData')); | ||
$this->input->on('end', array($this, 'handleEnd')); | ||
$this->input->on('error', array($this, 'handleError')); | ||
$this->input->on('close', array($this, 'close')); | ||
} | ||
|
||
public function isReadable() | ||
{ | ||
return !$this->closed && $this->input->isReadable(); | ||
} | ||
|
||
public function pause() | ||
{ | ||
if ($this->closed) { | ||
return; | ||
} | ||
|
||
$this->input->pause(); | ||
} | ||
|
||
public function resume() | ||
{ | ||
if ($this->closed) { | ||
return; | ||
} | ||
|
||
$this->input->resume(); | ||
} | ||
|
||
public function pipe(WritableStreamInterface $dest, array $options = array()) | ||
{ | ||
Util::pipe($this, $dest, $options); | ||
|
||
return $dest; | ||
} | ||
|
||
public function close() | ||
{ | ||
if ($this->closed) { | ||
return; | ||
} | ||
|
||
$this->closed = true; | ||
|
||
$this->emit('close'); | ||
|
||
// 'pause' the stream avoids additional traffic transferred by this stream | ||
$this->input->pause(); | ||
|
||
$this->input->removeListener('data', array($this, 'handleData')); | ||
$this->input->removeListener('error', array($this, 'handleError')); | ||
$this->input->removeListener('end', array($this, 'handleEnd')); | ||
$this->input->removeListener('close', array($this, 'close')); | ||
|
||
$this->removeAllListeners(); | ||
} | ||
|
||
/** @internal */ | ||
public function handleData($data) | ||
{ | ||
$this->emit('data', array($data)); | ||
} | ||
|
||
/** @internal */ | ||
public function handleEnd() | ||
{ | ||
$this->emit('end'); | ||
$this->close(); | ||
} | ||
|
||
/** @internal */ | ||
public function handleError(\Exception $e) | ||
{ | ||
$this->emit('error', array($e)); | ||
} | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
<?php | ||
|
||
namespace React\Tests\Http; | ||
|
||
use React\Http\CloseProtectionStream; | ||
use React\Stream\ReadableStream; | ||
|
||
class CloseProtectionStreamTest extends TestCase | ||
{ | ||
public function testClosePausesTheInputStreamInsteadOfClosing() | ||
{ | ||
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->disableOriginalConstructor()->getMock(); | ||
$input->expects($this->once())->method('pause'); | ||
$input->expects($this->never())->method('close'); | ||
|
||
$protection = new CloseProtectionStream($input); | ||
$protection->close(); | ||
} | ||
|
||
public function testErrorWontCloseStream() | ||
{ | ||
$input = new ReadableStream(); | ||
|
||
$protection = new CloseProtectionStream($input); | ||
$protection->on('error', $this->expectCallableOnce()); | ||
$protection->on('close', $this->expectCallableNever()); | ||
|
||
$input->emit('error', array(new \RuntimeException())); | ||
|
||
$this->assertTrue($protection->isReadable()); | ||
$this->assertTrue($input->isReadable()); | ||
} | ||
|
||
public function testResumeStreamWillResumeInputStream() | ||
{ | ||
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock(); | ||
$input->expects($this->once())->method('pause'); | ||
$input->expects($this->once())->method('resume'); | ||
|
||
$protection = new CloseProtectionStream($input); | ||
$protection->pause(); | ||
$protection->resume(); | ||
} | ||
|
||
public function testInputStreamIsNotReadableAfterClose() | ||
{ | ||
$input = new ReadableStream(); | ||
|
||
$protection = new CloseProtectionStream($input); | ||
$protection->on('close', $this->expectCallableOnce()); | ||
|
||
$input->close(); | ||
|
||
$this->assertFalse($protection->isReadable()); | ||
$this->assertFalse($input->isReadable()); | ||
} | ||
|
||
public function testPipeStream() | ||
{ | ||
$input = new ReadableStream(); | ||
|
||
$protection = new CloseProtectionStream($input); | ||
$dest = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock(); | ||
|
||
$ret = $protection->pipe($dest); | ||
|
||
$this->assertSame($dest, $ret); | ||
} | ||
|
||
public function testStopEmittingDataAfterClose() | ||
{ | ||
$input = new ReadableStream(); | ||
|
||
$protection = new CloseProtectionStream($input); | ||
$protection->on('data', $this->expectCallableNever()); | ||
|
||
$protection->on('close', $this->expectCallableOnce()); | ||
|
||
$protection->close(); | ||
|
||
$input->emit('data', array('hello')); | ||
|
||
$this->assertFalse($protection->isReadable()); | ||
$this->assertTrue($input->isReadable()); | ||
} | ||
|
||
public function testErrorIsNeverCalledAfterClose() | ||
{ | ||
$input = new ReadableStream(); | ||
|
||
$protection = new CloseProtectionStream($input); | ||
$protection->on('data', $this->expectCallableNever()); | ||
$protection->on('error', $this->expectCallableNever()); | ||
$protection->on('close', $this->expectCallableOnce()); | ||
|
||
$protection->close(); | ||
|
||
$input->emit('error', array(new \Exception())); | ||
|
||
$this->assertFalse($protection->isReadable()); | ||
$this->assertTrue($input->isReadable()); | ||
} | ||
|
||
public function testEndWontBeEmittedAfterClose() | ||
{ | ||
$input = new ReadableStream(); | ||
|
||
$protection = new CloseProtectionStream($input); | ||
$protection->on('data', $this->expectCallableNever()); | ||
$protection->on('close', $this->expectCallableOnce()); | ||
|
||
$protection->close(); | ||
|
||
$input->emit('end', array()); | ||
|
||
$this->assertFalse($protection->isReadable()); | ||
$this->assertTrue($input->isReadable()); | ||
} | ||
|
||
public function testPauseAfterCloseHasNoEffect() | ||
{ | ||
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock(); | ||
$input->expects($this->once())->method('pause'); | ||
|
||
$protection = new CloseProtectionStream($input); | ||
$protection->on('data', $this->expectCallableNever()); | ||
$protection->on('close', $this->expectCallableOnce()); | ||
|
||
$protection->close(); | ||
$protection->pause(); | ||
} | ||
|
||
public function testResumeAfterCloseHasNoEffect() | ||
{ | ||
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock(); | ||
$input->expects($this->once())->method('pause'); | ||
$input->expects($this->never())->method('resume'); | ||
|
||
$protection = new CloseProtectionStream($input); | ||
$protection->on('data', $this->expectCallableNever()); | ||
$protection->on('close', $this->expectCallableOnce()); | ||
|
||
$protection->close(); | ||
$protection->resume(); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't see this covered, but what happens if I call this:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are right, this wasn't handled correctly. Fixed it and added some tests for it.