Skip to content

Commit 1fbe922

Browse files
committed
Refactor to move response body handling to ClientRequestStream
1 parent ce16c02 commit 1fbe922

File tree

3 files changed

+25
-15
lines changed

3 files changed

+25
-15
lines changed

src/Io/ClientRequestStream.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
use Evenement\EventEmitter;
66
use Psr\Http\Message\RequestInterface;
7+
use Psr\Http\Message\ResponseInterface;
8+
use React\Http\Message\Response;
79
use React\Promise;
810
use React\Socket\ConnectionInterface;
911
use React\Socket\ConnectorInterface;
@@ -172,8 +174,26 @@ public function handleData($data)
172174

173175
$this->stream->on('close', array($this, 'handleClose'));
174176

175-
$this->emit('response', array($response, $this->stream));
177+
assert($response instanceof ResponseInterface);
178+
assert($this->stream instanceof ConnectionInterface);
179+
$body = $this->stream;
180+
181+
// determine length of response body
182+
$length = null;
183+
$code = $response->getStatusCode();
184+
if ($this->request->getMethod() === 'HEAD' || ($code >= 100 && $code < 200) || $code == Response::STATUS_NO_CONTENT || $code == Response::STATUS_NOT_MODIFIED) {
185+
$length = 0;
186+
} elseif (\strtolower($response->getHeaderLine('Transfer-Encoding')) === 'chunked') {
187+
$body = new ChunkedDecoder($body);
188+
} elseif ($response->hasHeader('Content-Length')) {
189+
$length = (int) $response->getHeaderLine('Content-Length');
190+
}
191+
$response = $response->withBody($body = new ReadableBodyStream($body, $length));
192+
193+
// emit response with streaming response body (see `Sender`)
194+
$this->emit('response', array($response, $body));
176195

196+
// re-emit HTTP response body to trigger body parsing if parts of it are buffered
177197
$this->stream->emit('data', array($bodyChunk));
178198
}
179199
}

src/Io/Sender.php

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
use Psr\Http\Message\ResponseInterface;
77
use React\EventLoop\LoopInterface;
88
use React\Http\Client\Client as HttpClient;
9-
use React\Http\Message\Response;
109
use React\Promise\PromiseInterface;
1110
use React\Promise\Deferred;
1211
use React\Socket\ConnectorInterface;
@@ -116,18 +115,8 @@ public function send(RequestInterface $request)
116115
$deferred->reject($error);
117116
});
118117

119-
$requestStream->on('response', function (ResponseInterface $response, ReadableStreamInterface $body) use ($deferred, $request) {
120-
$length = null;
121-
$code = $response->getStatusCode();
122-
if ($request->getMethod() === 'HEAD' || ($code >= 100 && $code < 200) || $code == Response::STATUS_NO_CONTENT || $code == Response::STATUS_NOT_MODIFIED) {
123-
$length = 0;
124-
} elseif (\strtolower($response->getHeaderLine('Transfer-Encoding')) === 'chunked') {
125-
$body = new ChunkedDecoder($body);
126-
} elseif ($response->hasHeader('Content-Length')) {
127-
$length = (int) $response->getHeaderLine('Content-Length');
128-
}
129-
130-
$deferred->resolve($response->withBody(new ReadableBodyStream($body, $length)));
118+
$requestStream->on('response', function (ResponseInterface $response) use ($deferred, $request) {
119+
$deferred->resolve($response);
131120
});
132121

133122
if ($body instanceof ReadableStreamInterface) {

tests/Io/ClientRequestStreamTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,12 @@ public function requestShouldBindToStreamEventsAndUseconnector()
3535

3636
$this->successfulConnectionMock();
3737

38-
$this->stream->expects($this->exactly(6))->method('on')->withConsecutive(
38+
$this->stream->expects($this->atLeast(6))->method('on')->withConsecutive(
3939
array('drain', $this->identicalTo(array($request, 'handleDrain'))),
4040
array('data', $this->identicalTo(array($request, 'handleData'))),
4141
array('end', $this->identicalTo(array($request, 'handleEnd'))),
4242
array('error', $this->identicalTo(array($request, 'handleError'))),
43+
array('close', $this->identicalTo(array($request, 'handleClose'))),
4344
array('close', $this->identicalTo(array($request, 'handleClose')))
4445
);
4546

0 commit comments

Comments
 (0)