Skip to content

Commit 5e1d1d4

Browse files
authored
Merge pull request #431 from clue-labs/body-in-memory
2 parents e19e987 + 9ed03c4 commit 5e1d1d4

File tree

7 files changed

+32
-20
lines changed

7 files changed

+32
-20
lines changed

src/Io/BufferedBody.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ class BufferedBody implements StreamInterface
1515
private $position = 0;
1616
private $closed = false;
1717

18+
/**
19+
* @param string $buffer
20+
*/
1821
public function __construct($buffer)
1922
{
2023
$this->buffer = $buffer;

src/Io/MultipartParser.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
namespace React\Http\Io;
44

55
use Psr\Http\Message\ServerRequestInterface;
6-
use RingCentral\Psr7;
76

87
/**
98
* [Internal] Parses a string body with "Content-Type: multipart/form-data" into structured data
@@ -190,7 +189,7 @@ private function parseUploadedFile($filename, $contentType, $contents)
190189
}
191190

192191
return new UploadedFile(
193-
Psr7\stream_for(),
192+
new BufferedBody(''),
194193
$size,
195194
\UPLOAD_ERR_NO_FILE,
196195
$filename,
@@ -206,7 +205,7 @@ private function parseUploadedFile($filename, $contentType, $contents)
206205
// file exceeds "upload_max_filesize" ini setting
207206
if ($size > $this->uploadMaxFilesize) {
208207
return new UploadedFile(
209-
Psr7\stream_for(),
208+
new BufferedBody(''),
210209
$size,
211210
\UPLOAD_ERR_INI_SIZE,
212211
$filename,
@@ -217,7 +216,7 @@ private function parseUploadedFile($filename, $contentType, $contents)
217216
// file exceeds MAX_FILE_SIZE value
218217
if ($this->maxFileSize !== null && $size > $this->maxFileSize) {
219218
return new UploadedFile(
220-
Psr7\stream_for(),
219+
new BufferedBody(''),
221220
$size,
222221
\UPLOAD_ERR_FORM_SIZE,
223222
$filename,
@@ -226,7 +225,7 @@ private function parseUploadedFile($filename, $contentType, $contents)
226225
}
227226

228227
return new UploadedFile(
229-
Psr7\stream_for($contents),
228+
new BufferedBody($contents),
230229
$size,
231230
\UPLOAD_ERR_OK,
232231
$filename,

src/Io/Transaction.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ public function bufferResponse(ResponseInterface $response, $deferred)
188188
$maximumSize = $this->maximumSize;
189189
$promise = \React\Promise\Stream\buffer($stream, $maximumSize)->then(
190190
function ($body) use ($response) {
191-
return $response->withBody(\RingCentral\Psr7\stream_for($body));
191+
return $response->withBody(new BufferedBody($body));
192192
},
193193
function ($e) use ($stream, $maximumSize) {
194194
// try to close stream if buffering fails (or is cancelled)

src/Message/Response.php

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@
22

33
namespace React\Http\Message;
44

5+
use Psr\Http\Message\StreamInterface;
6+
use React\Http\Io\BufferedBody;
57
use React\Http\Io\HttpBodyStream;
68
use React\Stream\ReadableStreamInterface;
79
use RingCentral\Psr7\Response as Psr7Response;
8-
use Psr\Http\Message\StreamInterface;
910

1011
/**
1112
* Represents an outgoing server response message.
@@ -48,9 +49,11 @@ public function __construct(
4849
$version = '1.1',
4950
$reason = null
5051
) {
51-
if ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) {
52+
if (\is_string($body)) {
53+
$body = new BufferedBody($body);
54+
} elseif ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) {
5255
$body = new HttpBodyStream($body, null);
53-
} elseif (!\is_string($body) && !$body instanceof StreamInterface) {
56+
} elseif (!$body instanceof StreamInterface) {
5457
throw new \InvalidArgumentException('Invalid response body given');
5558
}
5659

src/Message/ServerRequest.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use Psr\Http\Message\ServerRequestInterface;
66
use Psr\Http\Message\StreamInterface;
77
use Psr\Http\Message\UriInterface;
8+
use React\Http\Io\BufferedBody;
89
use React\Http\Io\HttpBodyStream;
910
use React\Stream\ReadableStreamInterface;
1011
use RingCentral\Psr7\Request;
@@ -57,10 +58,12 @@ public function __construct(
5758
$serverParams = array()
5859
) {
5960
$stream = null;
60-
if ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) {
61+
if (\is_string($body)) {
62+
$body = new BufferedBody($body);
63+
} elseif ($body instanceof ReadableStreamInterface && !$body instanceof StreamInterface) {
6164
$stream = $body;
6265
$body = null;
63-
} elseif (!\is_string($body) && !$body instanceof StreamInterface) {
66+
} elseif (!$body instanceof StreamInterface) {
6467
throw new \InvalidArgumentException('Invalid server request body given');
6568
}
6669

tests/Io/UploadedFileTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22

33
namespace React\Tests\Http\Io;
44

5+
use React\Http\Io\BufferedBody;
56
use React\Http\Io\UploadedFile;
67
Use React\Tests\Http\TestCase;
7-
use RingCentral\Psr7\BufferStream;
88

99
class UploadedFileTest extends TestCase
1010
{
@@ -23,15 +23,15 @@ public function failtyErrorProvider()
2323
*/
2424
public function testFailtyError($error)
2525
{
26-
$stream = new BufferStream();
26+
$stream = new BufferedBody('');
2727

2828
$this->setExpectedException('InvalidArgumentException', 'Invalid error code, must be an UPLOAD_ERR_* constant');
2929
new UploadedFile($stream, 0, $error, 'foo.bar', 'foo/bar');
3030
}
3131

3232
public function testNoMoveFile()
3333
{
34-
$stream = new BufferStream();
34+
$stream = new BufferedBody('');
3535
$uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_OK, 'foo.bar', 'foo/bar');
3636

3737
$this->setExpectedException('RuntimeException', 'Not implemented');
@@ -40,7 +40,7 @@ public function testNoMoveFile()
4040

4141
public function testGetters()
4242
{
43-
$stream = new BufferStream();
43+
$stream = new BufferedBody('');
4444
$uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_OK, 'foo.bar', 'foo/bar');
4545
self::assertSame($stream, $uploadedFile->getStream());
4646
self::assertSame(0, $uploadedFile->getSize());
@@ -51,7 +51,7 @@ public function testGetters()
5151

5252
public function testGetStreamOnFailedUpload()
5353
{
54-
$stream = new BufferStream();
54+
$stream = new BufferedBody('');
5555
$uploadedFile = new UploadedFile($stream, 0, UPLOAD_ERR_NO_FILE, 'foo.bar', 'foo/bar');
5656

5757
$this->setExpectedException('RuntimeException', 'Cannot retrieve stream due to upload error');

tests/Message/ResponseTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,22 @@
99

1010
class ResponseTest extends TestCase
1111
{
12-
13-
public function testStringBodyWillBePsr7Stream()
12+
public function testConstructWithStringBodyWillReturnStreamInstance()
1413
{
1514
$response = new Response(200, array(), 'hello');
16-
$this->assertInstanceOf('RingCentral\Psr7\Stream', $response->getBody());
15+
$body = $response->getBody();
16+
17+
/** @var \Psr\Http\Message\StreamInterface $body */
18+
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $body);
19+
$this->assertEquals('hello', (string) $body);
1720
}
1821

1922
public function testConstructWithStreamingBodyWillReturnReadableBodyStream()
2023
{
2124
$response = new Response(200, array(), new ThroughStream());
22-
2325
$body = $response->getBody();
26+
27+
/** @var \Psr\Http\Message\StreamInterface $body */
2428
$this->assertInstanceOf('Psr\Http\Message\StreamInterface', $body);
2529
$this->assertInstanceof('React\Stream\ReadableStreamInterface', $body);
2630
$this->assertInstanceOf('React\Http\Io\HttpBodyStream', $body);

0 commit comments

Comments
 (0)