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

Handle 'Content-Length' requests #129

Merged
merged 4 commits into from
Feb 22, 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
102 changes: 102 additions & 0 deletions src/LengthLimitedStream.php
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'));
}
}

}
18 changes: 18 additions & 0 deletions src/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ public function handleRequest(ConnectionInterface $conn, Request $request)
if (strtolower(end($transferEncodingHeader)) === 'chunked') {
$stream = new ChunkedDecoder($conn);
}
} elseif ($request->hasHeader('Content-Length')) {
$string = $request->getHeaderLine('Content-Length');

$contentLength = (int)$string;
if ((string)$contentLength !== (string)$string) {
// Content-Length value is not an integer or not a single integer
$this->emit('error', array(new \InvalidArgumentException('The value of `Content-Length` is not valid')));
return $this->writeError($conn, 400);
}

$stream = new LengthLimitedStream($conn, $contentLength);
}
Copy link
Member

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)

Copy link
Contributor Author

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.

Copy link
Member

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:

  1. If this is a request message and none of the above are true, then
    the message body length is zero (no message body is present).

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.

Copy link
Contributor Author

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


// forward pause/resume calls to underlying connection
Expand All @@ -173,6 +184,13 @@ public function handleRequest(ConnectionInterface $conn, Request $request)
});

$this->emit('request', array($request, $response));

if ($stream instanceof LengthLimitedStream && $contentLength === 0) {
// Content-Length is 0 and won't emit further data,
// 'handleData' from LengthLimitedStream won't be called anymore
$stream->emit('end');
$stream->close();
}
}

/** @internal */
Expand Down
120 changes: 120 additions & 0 deletions tests/LengthLimitedStreamTest.php
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');
}
}
Loading