Skip to content

Rewind streams #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 15, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
27 changes: 23 additions & 4 deletions src/Formatter/FullHttpMessageFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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;
}
Expand Down