diff --git a/CHANGELOG.md b/CHANGELOG.md index 01719d0..b90aaca 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,12 @@ # Change Log +## 1.3.1 - 2016-07-15 + +### Fixed + +- FullHttpMessageFormatter will not read from streams that you cannot rewind (non-seekable) +- FullHttpMessageFormatter will not read from the stream if $maxBodyLength is zero +- FullHttpMessageFormatter rewinds streams after they are read. ## 1.3.0 - 2016-07-14 diff --git a/src/Formatter/FullHttpMessageFormatter.php b/src/Formatter/FullHttpMessageFormatter.php index 0afe38c..3fa1029 100644 --- a/src/Formatter/FullHttpMessageFormatter.php +++ b/src/Formatter/FullHttpMessageFormatter.php @@ -3,6 +3,7 @@ namespace Http\Message\Formatter; use Http\Message\Formatter; +use Psr\Http\Message\MessageInterface; use Psr\Http\Message\RequestInterface; use Psr\Http\Message\ResponseInterface; @@ -44,9 +45,7 @@ public function formatRequest(RequestInterface $request) $message .= $name.': '.implode(', ', $values)."\n"; } - $message .= "\n".mb_substr($request->getBody()->__toString(), 0, $this->maxBodyLength); - - return $message; + return $this->addBody($request, $message); } /** @@ -65,7 +64,27 @@ public function formatResponse(ResponseInterface $response) $message .= $name.': '.implode(', ', $values)."\n"; } - $message .= "\n".mb_substr($response->getBody()->__toString(), 0, $this->maxBodyLength); + return $this->addBody($response, $message); + } + + /** + * Add the message body if the stream is seekable. + * + * @param MessageInterface $request + * @param string $message + * + * @return string + */ + private function addBody(MessageInterface $request, $message) + { + $stream = $request->getBody(); + if (!$stream->isSeekable() || $this->maxBodyLength === 0) { + // Do not read the stream + $message .= "\n"; + } else { + $message .= "\n".mb_substr($stream->__toString(), 0, $this->maxBodyLength); + $stream->rewind(); + } return $message; }