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

StreamWrapper streams are always seekable #535

Closed
Deltachaos opened this issue Oct 24, 2022 · 1 comment
Closed

StreamWrapper streams are always seekable #535

Deltachaos opened this issue Oct 24, 2022 · 1 comment

Comments

@Deltachaos
Copy link

PHP version: 8.1.11

Description

Resources created by StreamWrapper::getResource are always seekable, even if the underlying Stream object, is not.

How to reproduce

use Psr\Http\Message\StreamInterface;
use GuzzleHttp\Psr7\StreamWrapper;

class TestStream implements StreamInterface
{
    public function __toString()
    {
        return $this->getContents();
    }

    public function close()
    {
    }

    public function detach()
    {
        return null;
    }

    public function getSize()
    {
        return 0;
    }

    public function tell()
    {
        return 0;
    }

    public function eof()
    {
        return true;
    }

    public function isSeekable()
    {
        return false;
    }

    public function seek($offset, $whence = SEEK_SET)
    {
        return false;
    }

    public function rewind()
    {
        return false;
    }

    public function isWritable()
    {
        return false;
    }

    public function write($string)
    {
    }

    public function isReadable()
    {
        return true;
    }

    public function read($length)
    {
        return '';
    }

    public function getContents()
    {
        return '';
    }

    public function getMetadata($key = null)
    {
        return null;
    }
}

$stream = new TestStream();
$metadata = stream_get_meta_data(StreamWrapper::getResource($stream));

assert($metadata['seekable'] == $stream->isSeekable(), "Stream is not seekable");

Possible Solution

Throw an exception in StreamWrapper::getResource if non seekable Stream objects are passed as an argument.

Additional context

As far as my research went, custom stream wrappers are currently always treated as seekable (which does not make any sense IMHO). So therefore this can only be implemented in Guzzle as soon as support for none seekable custom stream wrappers is implemented in PHP.

@stale
Copy link

stale bot commented Mar 11, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed after 2 weeks if no further activity occurs. Thank you for your contributions.

@stale stale bot closed this as completed Apr 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant