Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpClient] StreamWrapper fails to copy stream for responses without content length #40574

Closed
@rmikalkenas

Description

@rmikalkenas

Symfony version(s) affected: not sure at which version StreamWrapper was introduced

Description
While requesting unknown content length streamed responses, I noticed that such responses converted to php resources (using StreamWrapper::createResource) cannot be copied using stream_copy_to_stream. My guessing is that StreamWrapper::stream_stat method returns size argument as zero which results to stream_copy_to_stream not work

How to reproduce

$client = HttpClient::create();
$response = $client->request('GET', 'https://someUrlWithUnknownContentLengthResponse');
$resource = StreamWrapper::createResource($response, $client);
$outputStream = fopen('php://output', 'wb');
$bytesCopied = stream_copy_to_stream($resource, $outputStream);
// $bytesCopied === 0

My final result is to restream it again, but due to stream_copy_to_stream failure the response is empty:

return new StreamedResponse(
    static function () use ($resource): void {
        $outputStream = fopen('php://output', 'wb');

        stream_copy_to_stream($resource, $outputStream);
    }
);

Possible Solution
Unfortunately have no idea how to fix it, but I would gladly help with PR if anyone suggest a possible fix.

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