-
-
Notifications
You must be signed in to change notification settings - Fork 145
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
Handle 'Content-Length' requests #129
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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 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,102 @@ | ||
<?php | ||
|
||
namespace React\Http; | ||
|
||
use Evenement\EventEmitter; | ||
use React\Stream\ReadableStreamInterface; | ||
use React\Stream\WritableStreamInterface; | ||
use React\Stream\Util; | ||
|
||
/** @internal */ | ||
class LengthLimitedStream extends EventEmitter implements ReadableStreamInterface | ||
{ | ||
private $stream; | ||
private $closed = false; | ||
private $encoder; | ||
private $transferredLength = 0; | ||
private $maxLength; | ||
|
||
public function __construct(ReadableStreamInterface $stream, $maxLength) | ||
{ | ||
$this->stream = $stream; | ||
$this->maxLength = $maxLength; | ||
|
||
$this->stream->on('data', array($this, 'handleData')); | ||
$this->stream->on('end', array($this, 'handleEnd')); | ||
$this->stream->on('error', array($this, 'handleError')); | ||
$this->stream->on('close', array($this, 'close')); | ||
} | ||
|
||
public function isReadable() | ||
{ | ||
return !$this->closed && $this->stream->isReadable(); | ||
} | ||
|
||
public function pause() | ||
{ | ||
$this->stream->pause(); | ||
} | ||
|
||
public function resume() | ||
{ | ||
$this->stream->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->stream->close(); | ||
|
||
$this->emit('close'); | ||
$this->removeAllListeners(); | ||
} | ||
|
||
/** @internal */ | ||
public function handleData($data) | ||
{ | ||
if (($this->transferredLength + strlen($data)) > $this->maxLength) { | ||
// Only emit data until the value of 'Content-Length' is reached, the rest will be ignored | ||
$data = (string)substr($data, 0, $this->maxLength - $this->transferredLength); | ||
} | ||
|
||
if ($data !== '') { | ||
$this->transferredLength += strlen($data); | ||
$this->emit('data', array($data)); | ||
} | ||
|
||
if ($this->transferredLength === $this->maxLength) { | ||
// 'Content-Length' reached, stream will end | ||
$this->emit('end'); | ||
$this->close(); | ||
$this->stream->removeListener('data', array($this, 'handleData')); | ||
} | ||
} | ||
|
||
/** @internal */ | ||
public function handleError(\Exception $e) | ||
{ | ||
$this->emit('error', array($e)); | ||
$this->close(); | ||
} | ||
|
||
/** @internal */ | ||
public function handleEnd() | ||
{ | ||
if (!$this->closed) { | ||
$this->handleError(new \Exception('Unexpected end event')); | ||
} | ||
} | ||
|
||
} |
This file contains 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 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,120 @@ | ||
<?php | ||
|
||
namespace React\Tests\Http; | ||
|
||
use React\Http\LengthLimitedStream; | ||
use React\Stream\ReadableStream; | ||
|
||
class LengthLimitedStreamTest extends TestCase | ||
{ | ||
private $input; | ||
private $stream; | ||
|
||
public function setUp() | ||
{ | ||
$this->input = new ReadableStream(); | ||
} | ||
|
||
public function testSimpleChunk() | ||
{ | ||
$stream = new LengthLimitedStream($this->input, 5); | ||
$stream->on('data', $this->expectCallableOnceWith('hello')); | ||
$stream->on('end', $this->expectCallableOnce()); | ||
$this->input->emit('data', array("hello world")); | ||
} | ||
|
||
public function testInputStreamKeepsEmitting() | ||
{ | ||
$stream = new LengthLimitedStream($this->input, 5); | ||
$stream->on('data', $this->expectCallableOnceWith('hello')); | ||
$stream->on('end', $this->expectCallableOnce()); | ||
|
||
$this->input->emit('data', array("hello world")); | ||
$this->input->emit('data', array("world")); | ||
$this->input->emit('data', array("world")); | ||
} | ||
|
||
public function testZeroLengthInContentLengthWillIgnoreEmittedDataEvents() | ||
{ | ||
$stream = new LengthLimitedStream($this->input, 0); | ||
$stream->on('data', $this->expectCallableNever()); | ||
$stream->on('end', $this->expectCallableOnce()); | ||
$this->input->emit('data', array("hello world")); | ||
} | ||
|
||
public function testHandleError() | ||
{ | ||
$stream = new LengthLimitedStream($this->input, 0); | ||
$stream->on('error', $this->expectCallableOnce()); | ||
$stream->on('close', $this->expectCallableOnce()); | ||
|
||
$this->input->emit('error', array(new \RuntimeException())); | ||
|
||
$this->assertFalse($stream->isReadable()); | ||
} | ||
|
||
public function testPauseStream() | ||
{ | ||
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock(); | ||
$input->expects($this->once())->method('pause'); | ||
|
||
$stream = new LengthLimitedStream($input, 0); | ||
$stream->pause(); | ||
} | ||
|
||
public function testResumeStream() | ||
{ | ||
$input = $this->getMockBuilder('React\Stream\ReadableStreamInterface')->getMock(); | ||
$input->expects($this->once())->method('pause'); | ||
|
||
$stream = new LengthLimitedStream($input, 0); | ||
$stream->pause(); | ||
$stream->resume(); | ||
} | ||
|
||
public function testPipeStream() | ||
{ | ||
$stream = new LengthLimitedStream($this->input, 0); | ||
$dest = $this->getMockBuilder('React\Stream\WritableStreamInterface')->getMock(); | ||
|
||
$ret = $stream->pipe($dest); | ||
|
||
$this->assertSame($dest, $ret); | ||
} | ||
|
||
public function testHandleClose() | ||
{ | ||
$stream = new LengthLimitedStream($this->input, 0); | ||
$stream->on('close', $this->expectCallableOnce()); | ||
|
||
$this->input->close(); | ||
$this->input->emit('end', array()); | ||
|
||
$this->assertFalse($stream->isReadable()); | ||
} | ||
|
||
public function testOutputStreamCanCloseInputStream() | ||
{ | ||
$input = new ReadableStream(); | ||
$input->on('close', $this->expectCallableOnce()); | ||
|
||
$stream = new LengthLimitedStream($input, 0); | ||
$stream->on('close', $this->expectCallableOnce()); | ||
|
||
$stream->close(); | ||
|
||
$this->assertFalse($input->isReadable()); | ||
} | ||
|
||
public function testHandleUnexpectedEnd() | ||
{ | ||
$stream = new LengthLimitedStream($this->input, 5); | ||
|
||
$stream->on('data', $this->expectCallableNever()); | ||
$stream->on('close', $this->expectCallableOnce()); | ||
$stream->on('end', $this->expectCallableNever()); | ||
$stream->on('error', $this->expectCallableOnce()); | ||
|
||
$this->input->emit('end'); | ||
} | ||
} |
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.
What if a request has neither chunked transfer encoding nor a
Content-Length
header (such as for simple GET requests)? Is this tested?Should this be part of this PR? (see also #104)
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.
afaik every test with
createGetRequest
would be effected. The tests for this would be the same, to the ones that already exist.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.
Not sure I get what you're suggesting here and I may be missing something obvious here, but I'm specifically talking about this:
https://tools.ietf.org/html/rfc7230#section-3.3.3
Is this correctly covered and tested by this PR or is this not supposed to be part of this PR? See also the PR title and #104.
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.
Edited the first comment. This only closes a part of #104