From 9ed03c4fcc0573b9c9043b2f4824ed22fe9ef845 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20L=C3=BCck?= Date: Thu, 3 Dec 2020 09:43:36 +0100 Subject: [PATCH] Internal refactoring and improvement to hold message body in memory --- src/Io/BufferedBody.php | 3 +++ src/Io/MultipartParser.php | 9 ++++----- src/Io/Transaction.php | 2 +- src/Message/Response.php | 9 ++++++--- src/Message/ServerRequest.php | 7 +++++-- tests/Io/UploadedFileTest.php | 10 +++++----- tests/Message/ResponseTest.php | 12 ++++++++---- 7 files changed, 32 insertions(+), 20 deletions(-) diff --git a/src/Io/BufferedBody.php b/src/Io/BufferedBody.php index 2f81bc56..4a4d8393 100644 --- a/src/Io/BufferedBody.php +++ b/src/Io/BufferedBody.php @@ -15,6 +15,9 @@ class BufferedBody implements StreamInterface private $position = 0; private $closed = false; + /** + * @param string $buffer + */ public function __construct($buffer) { $this->buffer = $buffer; diff --git a/src/Io/MultipartParser.php b/src/Io/MultipartParser.php index d868ca88..536694fd 100644 --- a/src/Io/MultipartParser.php +++ b/src/Io/MultipartParser.php @@ -3,7 +3,6 @@ namespace React\Http\Io; use Psr\Http\Message\ServerRequestInterface; -use RingCentral\Psr7; /** * [Internal] Parses a string body with "Content-Type: multipart/form-data" into structured data @@ -190,7 +189,7 @@ private function parseUploadedFile($filename, $contentType, $contents) } return new UploadedFile( - Psr7\stream_for(), + new BufferedBody(''), $size, \UPLOAD_ERR_NO_FILE, $filename, @@ -206,7 +205,7 @@ private function parseUploadedFile($filename, $contentType, $contents) // file exceeds "upload_max_filesize" ini setting if ($size > $this->uploadMaxFilesize) { return new UploadedFile( - Psr7\stream_for(), + new BufferedBody(''), $size, \UPLOAD_ERR_INI_SIZE, $filename, @@ -217,7 +216,7 @@ private function parseUploadedFile($filename, $contentType, $contents) // file exceeds MAX_FILE_SIZE value if ($this->maxFileSize !== null && $size > $this->maxFileSize) { return new UploadedFile( - Psr7\stream_for(), + new BufferedBody(''), $size, \UPLOAD_ERR_FORM_SIZE, $filename, @@ -226,7 +225,7 @@ private function parseUploadedFile($filename, $contentType, $contents) } return new UploadedFile( - Psr7\stream_for($contents), + new BufferedBody($contents), $size, \UPLOAD_ERR_OK, $filename, diff --git a/src/Io/Transaction.php b/src/Io/Transaction.php index 9449503f..7bf7008f 100644 --- a/src/Io/Transaction.php +++ b/src/Io/Transaction.php @@ -188,7 +188,7 @@ public function bufferResponse(ResponseInterface $response, $deferred) $maximumSize = $this->maximumSize; $promise = \React\Promise\Stream\buffer($stream, $maximumSize)->then( function ($body) use ($response) { - return $response->withBody(\RingCentral\Psr7\stream_for($body)); + return $response->withBody(new BufferedBody($body)); }, function ($e) use ($stream, $maximumSize) { // try to close stream if buffering fails (or is cancelled) diff --git a/src/Message/Response.php b/src/Message/Response.php index 5d799c58..a9710170 100644 --- a/src/Message/Response.php +++ b/src/Message/Response.php @@ -2,10 +2,11 @@ namespace React\Http\Message; +use Psr\Http\Message\StreamInterface; +use React\Http\Io\BufferedBody; use React\Http\Io\HttpBodyStream; use React\Stream\ReadableStreamInterface; use RingCentral\Psr7\Response as Psr7Response; -use Psr\Http\Message\StreamInterface; /** * Represents an outgoing server response message. @@ -48,9 +49,11 @@ public function __construct( $version = '1.1', $reason = null ) { - if ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) { + if (\is_string($body)) { + $body = new BufferedBody($body); + } elseif ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) { $body = new HttpBodyStream($body, null); - } elseif (!\is_string($body) && !$body instanceof StreamInterface) { + } elseif (!$body instanceof StreamInterface) { throw new \InvalidArgumentException('Invalid response body given'); } diff --git a/src/Message/ServerRequest.php b/src/Message/ServerRequest.php index 5c01819b..f446f24e 100644 --- a/src/Message/ServerRequest.php +++ b/src/Message/ServerRequest.php @@ -5,6 +5,7 @@ use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Message\StreamInterface; use Psr\Http\Message\UriInterface; +use React\Http\Io\BufferedBody; use React\Http\Io\HttpBodyStream; use React\Stream\ReadableStreamInterface; use RingCentral\Psr7\Request; @@ -57,10 +58,12 @@ public function __construct( $serverParams = array() ) { $stream = null; - if ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) { + if (\is_string($body)) { + $body = new BufferedBody($body); + } elseif ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) { $stream = $body; $body = null; - } elseif (!\is_string($body) && !$body instanceof StreamInterface) { + } elseif (!$body instanceof StreamInterface) { throw new \InvalidArgumentException('Invalid server request body given'); } diff --git a/tests/Io/UploadedFileTest.php b/tests/Io/UploadedFileTest.php index 9ff623da..4e9c0dd5 100644 --- a/tests/Io/UploadedFileTest.php +++ b/tests/Io/UploadedFileTest.php @@ -2,9 +2,9 @@ namespace React\Tests\Http\Io; +use React\Http\Io\BufferedBody; use React\Http\Io\UploadedFile; Use React\Tests\Http\TestCase; -use RingCentral\Psr7\BufferStream; class UploadedFileTest extends TestCase { @@ -23,7 +23,7 @@ public function failtyErrorProvider() */ public function testFailtyError($error) { - $stream = new BufferStream(); + $stream = new BufferedBody(''); $this->setExpectedException('InvalidArgumentException', 'Invalid error code, must be an UPLOAD_ERR_* constant'); new UploadedFile($stream, 0, $error, 'foo.bar', 'foo/bar'); @@ -31,7 +31,7 @@ public function testFailtyError($error) public function testNoMoveFile() { - $stream = new BufferStream(); + $stream = new BufferedBody(''); $uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_OK, 'foo.bar', 'foo/bar'); $this->setExpectedException('RuntimeException', 'Not implemented'); @@ -40,7 +40,7 @@ public function testNoMoveFile() public function testGetters() { - $stream = new BufferStream(); + $stream = new BufferedBody(''); $uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_OK, 'foo.bar', 'foo/bar'); self::assertSame($stream, $uploadedFile->getStream()); self::assertSame(0, $uploadedFile->getSize()); @@ -51,7 +51,7 @@ public function testGetters() public function testGetStreamOnFailedUpload() { - $stream = new BufferStream(); + $stream = new BufferedBody(''); $uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_NO_FILE, 'foo.bar', 'foo/bar'); $this->setExpectedException('RuntimeException', 'Cannot retrieve stream due to upload error'); diff --git a/tests/Message/ResponseTest.php b/tests/Message/ResponseTest.php index 457618e9..3c6ad3fd 100644 --- a/tests/Message/ResponseTest.php +++ b/tests/Message/ResponseTest.php @@ -9,18 +9,22 @@ class ResponseTest extends TestCase { - - public function testStringBodyWillBePsr7Stream() + public function testConstructWithStringBodyWillReturnStreamInstance() { $response = new Response(200, array(), 'hello'); - $this->assertInstanceOf('RingCentral\Psr7\Stream', $response->getBody()); + $body = $response->getBody(); + + /** @var \Psr\Http\Message\StreamInterface $body */ + $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $body); + $this->assertEquals('hello', (string) $body); } public function testConstructWithStreamingBodyWillReturnReadableBodyStream() { $response = new Response(200, array(), new ThroughStream()); - $body = $response->getBody(); + + /** @var \Psr\Http\Message\StreamInterface $body */ $this->assertInstanceOf('Psr\Http\Message\StreamInterface', $body); $this->assertInstanceof('React\Stream\ReadableStreamInterface', $body); $this->assertInstanceOf('React\Http\Io\HttpBodyStream', $body);