|
4 | 4 |
|
5 | 5 | use Evenement\EventEmitter; |
6 | 6 | use Psr\Http\Message\RequestInterface; |
| 7 | +use Psr\Http\Message\ResponseInterface; |
| 8 | +use React\Http\Message\Response; |
7 | 9 | use React\Promise; |
8 | 10 | use React\Socket\ConnectionInterface; |
9 | 11 | use React\Socket\ConnectorInterface; |
@@ -172,8 +174,26 @@ public function handleData($data) |
172 | 174 |
|
173 | 175 | $this->stream->on('close', array($this, 'handleClose')); |
174 | 176 |
|
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)); |
176 | 195 |
|
| 196 | + // re-emit HTTP response body to trigger body parsing if parts of it are buffered |
177 | 197 | $this->stream->emit('data', array($bodyChunk)); |
178 | 198 | } |
179 | 199 | } |
|
0 commit comments