Skip to content

Commit

Permalink
Merge pull request #431 from clue-labs/body-in-memory
Browse files Browse the repository at this point in the history
  • Loading branch information
WyriHaximus authored Nov 7, 2021
2 parents e19e987 + 9ed03c4 commit 5e1d1d4
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
3 changes: 3 additions & 0 deletions src/Io/BufferedBody.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ class BufferedBody implements StreamInterface
private $position = 0;
private $closed = false;

/**
* @param string $buffer
*/
public function __construct($buffer)
{
$this->buffer = $buffer;
Expand Down
9 changes: 4 additions & 5 deletions src/Io/MultipartParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -190,7 +189,7 @@ private function parseUploadedFile($filename, $contentType, $contents)
}

return new UploadedFile(
Psr7\stream_for(),
new BufferedBody(''),
$size,
\UPLOAD_ERR_NO_FILE,
$filename,
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion src/Io/Transaction.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
9 changes: 6 additions & 3 deletions src/Message/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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');
}

Expand Down
7 changes: 5 additions & 2 deletions src/Message/ServerRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}

Expand Down
10 changes: 5 additions & 5 deletions tests/Io/UploadedFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -23,15 +23,15 @@ 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');
}

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');
Expand All @@ -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());
Expand All @@ -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');
Expand Down
12 changes: 8 additions & 4 deletions tests/Message/ResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5e1d1d4

Please sign in to comment.