From d705e30309ef134be4e6ff817ecb8866a6ec488f Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Fri, 15 Jul 2016 16:18:20 +0200 Subject: [PATCH 1/3] Make sure we rewind stream if we can. This will fix #46 --- src/Formatter/FullHttpMessageFormatter.php | 27 ++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/src/Formatter/FullHttpMessageFormatter.php b/src/Formatter/FullHttpMessageFormatter.php index 0afe38c..d30a169 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; } From b2091107b3a0e125b014583bfc068de1cb748eee Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Fri, 15 Jul 2016 16:24:01 +0200 Subject: [PATCH 2/3] style fix --- src/Formatter/FullHttpMessageFormatter.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Formatter/FullHttpMessageFormatter.php b/src/Formatter/FullHttpMessageFormatter.php index d30a169..3fa1029 100644 --- a/src/Formatter/FullHttpMessageFormatter.php +++ b/src/Formatter/FullHttpMessageFormatter.php @@ -71,7 +71,7 @@ public function formatResponse(ResponseInterface $response) * Add the message body if the stream is seekable. * * @param MessageInterface $request - * @param string $message + * @param string $message * * @return string */ From d7d538c4a12a54251fccfe05255d768ae682738b Mon Sep 17 00:00:00 2001 From: Tobias Nyholm Date: Fri, 15 Jul 2016 16:39:14 +0200 Subject: [PATCH 3/3] Updated changelog with fixes for FullHttpMessageFormatter --- CHANGELOG.md | 7 +++++++ 1 file changed, 7 insertions(+) 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