Skip to content
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

Fix error handling in Stream::getContents() #294

Closed
wants to merge 3 commits into from
Closed
Changes from 1 commit
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
13 changes: 11 additions & 2 deletions src/Stream.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Psr\Http\Message\StreamInterface;
use RuntimeException;

use function error_get_last;
use function fclose;
use function feof;
use function fread;
Expand All @@ -26,7 +27,6 @@
use function is_string;
use function pclose;
use function rewind;
use function stream_get_contents;
use function stream_get_meta_data;
use function strstr;

Expand Down Expand Up @@ -354,7 +354,16 @@ public function getContents(): string
$contents = false;

if ($this->stream) {
$contents = stream_get_contents($this->stream);
$contents = '';

while (!feof($this->stream)) {
$result = @fread($this->stream, 16372);
if ($result === false) {
throw new RuntimeException('Unable to read from stream: ' . (error_get_last()['message'] ?? ''));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas this code checks for false return value as well... (ref Nyholm/psr7#252 (review))

}

$contents .= $result;
}
}

if (is_string($contents)) {
Expand Down