Skip to content

Stream context is lost when custom stream wrapper is being filtered #15155

Closed

Description

@qkdreyer

Description

The following code:

<?php

class DummyWrapper
{
    public $context;
    
    public static function register(): void
    {
        \stream_wrapper_register('dummy', self::class);
    }
    
    public function stream_open(string $path, string $mode, int $options, ?string &$opened_path): bool
    {
        $options = \stream_context_get_options($this->context);
        var_dump($options['dummy']['foo']);
        
        return true;
    }
    
    public function stream_stat()
    {
    }
    
    public function stream_read()
    {
    }
    
    public function stream_eof()
    {
    }
}

DummyWrapper::register();
\file_get_contents('dummy://foo', false, \stream_context_create(['dummy' => ['foo' => 'bar']]));
\file_get_contents('php://filter/resource=dummy://foo', false, \stream_context_create(['dummy' => ['foo' => 'bar']]));

Resulted in this output:

string(3) "bar"

Fatal error: Uncaught TypeError: stream_context_get_options(): Argument #1 ($stream_or_context) must be of type resource, null given in /in/hlqFE:14
Stack trace:
#0 /in/hlqFE(14): stream_context_get_options(NULL)
#1 [internal function]: DummyWrapper->stream_open('dummy://foo', 'rb', 0, NULL)
#2 /in/hlqFE(35): file_get_contents('php://filter/re...', false, Resource id #7)
#3 {main}
  thrown in /in/hlqFE on line 14

But I expected this output instead:

string(3) "bar"
string(3) "bar"

PHP Version

PHP 8.3.9

Operating System

No response

Activity

  1. bukka commented on Jul 29, 2024

    @bukka
    Member

    I think it makes sense to pass context to the resource. Although I would probably see that more as a feature even though it's probably somewhere between. But considering that it could impact behavior in some cases, I think this will be better just for master branch (it means PHP 8.4)

  2. added a commit that references this issue on Aug 6, 2024
    7b32a14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions