-
-
Notifications
You must be signed in to change notification settings - Fork 9.6k
[HttpClient] Add $response->toStream() to cast responses to regular PHP streams #32290
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
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
231 changes: 231 additions & 0 deletions
231
src/Symfony/Component/HttpClient/Response/StreamWrapper.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,231 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpClient\Response; | ||
|
||
use Symfony\Contracts\HttpClient\Exception\ExceptionInterface; | ||
use Symfony\Contracts\HttpClient\HttpClientInterface; | ||
use Symfony\Contracts\HttpClient\ResponseInterface; | ||
|
||
/** | ||
* Allows turning ResponseInterface instances to PHP streams. | ||
* | ||
* @author Nicolas Grekas <p@tchwork.com> | ||
*/ | ||
class StreamWrapper | ||
{ | ||
/** @var resource */ | ||
public $context; | ||
|
||
/** @var HttpClientInterface */ | ||
private $client; | ||
|
||
/** @var ResponseInterface */ | ||
private $response; | ||
|
||
/** @var resource|null */ | ||
private $content; | ||
|
||
/** @var resource|null */ | ||
private $handle; | ||
|
||
private $eof = false; | ||
private $offset = 0; | ||
|
||
/** | ||
* Creates a PHP stream resource from a ResponseInterface. | ||
* | ||
* @param resource|null $contentBuffer The seekable resource where the response body is buffered | ||
* @param resource|null $selectHandle The resource handle that should be monitored when | ||
* stream_select() is used on the created stream | ||
* | ||
* @return resource | ||
*/ | ||
public static function createResource(ResponseInterface $response, HttpClientInterface $client = null, $contentBuffer = null, $selectHandle = null) | ||
{ | ||
if (null === $client && !method_exists($response, 'stream')) { | ||
throw new \InvalidArgumentException(sprintf('Providing a client to "%s()" is required when the response doesn\'t have any "stream()" method.', __CLASS__)); | ||
} | ||
|
||
if (false === stream_wrapper_register('symfony', __CLASS__, STREAM_IS_URL)) { | ||
throw new \RuntimeException(error_get_last()['message'] ?? 'Registering the "symfony" stream wrapper failed.'); | ||
} | ||
|
||
try { | ||
$context = [ | ||
'client' => $client ?? $response, | ||
'response' => $response, | ||
'content' => $contentBuffer, | ||
'handle' => $selectHandle, | ||
]; | ||
|
||
return fopen('symfony://'.$response->getInfo('url'), 'r', false, stream_context_create(['symfony' => $context])) ?: null; | ||
} finally { | ||
stream_wrapper_unregister('symfony'); | ||
} | ||
} | ||
|
||
public function stream_open(string $path, string $mode, int $options): bool | ||
{ | ||
if ('r' !== $mode) { | ||
if ($options & STREAM_REPORT_ERRORS) { | ||
trigger_error(sprintf('Invalid mode "%s": only "r" is supported.', $mode), E_USER_WARNING); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
$context = stream_context_get_options($this->context)['symfony'] ?? null; | ||
$this->client = $context['client'] ?? null; | ||
$this->response = $context['response'] ?? null; | ||
$this->content = $context['content'] ?? null; | ||
$this->handle = $context['handle'] ?? null; | ||
$this->context = null; | ||
|
||
if (null !== $this->client && null !== $this->response) { | ||
return true; | ||
} | ||
|
||
if ($options & STREAM_REPORT_ERRORS) { | ||
trigger_error('Missing options "client" or "response" in "symfony" stream context.', E_USER_WARNING); | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function stream_read(int $count) | ||
{ | ||
if (null !== $this->content) { | ||
// Empty the internal activity list | ||
foreach ($this->client->stream([$this->response], 0) as $chunk) { | ||
try { | ||
$chunk->isTimeout(); | ||
} catch (ExceptionInterface $e) { | ||
trigger_error($e->getMessage(), E_USER_WARNING); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
if (0 !== fseek($this->content, $this->offset)) { | ||
return false; | ||
} | ||
|
||
if ('' !== $data = fread($this->content, $count)) { | ||
fseek($this->content, 0, SEEK_END); | ||
$this->offset += \strlen($data); | ||
|
||
return $data; | ||
} | ||
} | ||
|
||
foreach ($this->client->stream([$this->response]) as $chunk) { | ||
try { | ||
$this->eof = true; | ||
$this->eof = !$chunk->isTimeout(); | ||
$this->eof = $chunk->isLast(); | ||
|
||
if ('' !== $data = $chunk->getContent()) { | ||
$this->offset += \strlen($data); | ||
|
||
return $data; | ||
} | ||
} catch (ExceptionInterface $e) { | ||
trigger_error($e->getMessage(), E_USER_WARNING); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
return ''; | ||
} | ||
|
||
public function stream_tell(): int | ||
{ | ||
return $this->offset; | ||
} | ||
|
||
public function stream_eof(): bool | ||
{ | ||
return $this->eof; | ||
} | ||
|
||
public function stream_seek(int $offset, int $whence = SEEK_SET): bool | ||
{ | ||
if (null === $this->content || 0 !== fseek($this->content, 0, SEEK_END)) { | ||
return false; | ||
} | ||
|
||
$size = ftell($this->content); | ||
|
||
if (SEEK_CUR === $whence) { | ||
$offset += $this->offset; | ||
} | ||
|
||
if (SEEK_END === $whence || $size < $offset) { | ||
foreach ($this->client->stream([$this->response]) as $chunk) { | ||
try { | ||
// Chunks are buffered in $this->content already | ||
$size += \strlen($chunk->getContent()); | ||
|
||
if (SEEK_END !== $whence && $offset <= $size) { | ||
break; | ||
} | ||
} catch (ExceptionInterface $e) { | ||
trigger_error($e->getMessage(), E_USER_WARNING); | ||
|
||
return false; | ||
} | ||
} | ||
|
||
if (SEEK_END === $whence) { | ||
$offset += $size; | ||
} | ||
} | ||
|
||
if (0 <= $offset && $offset <= $size) { | ||
$this->eof = false; | ||
$this->offset = $offset; | ||
|
||
return true; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function stream_cast(int $castAs) | ||
{ | ||
if (STREAM_CAST_FOR_SELECT === $castAs) { | ||
return $this->handle ?? false; | ||
} | ||
|
||
return false; | ||
} | ||
|
||
public function stream_stat(): array | ||
{ | ||
return [ | ||
'dev' => 0, | ||
'ino' => 0, | ||
'mode' => 33060, | ||
'nlink' => 0, | ||
'uid' => 0, | ||
'gid' => 0, | ||
'rdev' => 0, | ||
'size' => (int) ($this->response->getHeaders(false)['content-length'][0] ?? 0), | ||
'atime' => 0, | ||
'mtime' => strtotime($this->response->getHeaders(false)['last-modified'][0] ?? '') ?: 0, | ||
'ctime' => 0, | ||
'blksize' => 0, | ||
'blocks' => 0, | ||
]; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Component\HttpClient\Tests; | ||
|
||
use Symfony\Contracts\HttpClient\Test\HttpClientTestCase as BaseHttpClientTestCase; | ||
|
||
abstract class HttpClientTestCase extends BaseHttpClientTestCase | ||
{ | ||
public function testToStream() | ||
{ | ||
$client = $this->getHttpClient(__FUNCTION__); | ||
|
||
$response = $client->request('GET', 'http://localhost:8057'); | ||
|
||
$stream = $response->toStream(); | ||
|
||
$this->assertSame("{\n \"SER", fread($stream, 10)); | ||
$this->assertSame('VER_PROTOCOL', fread($stream, 12)); | ||
$this->assertFalse(feof($stream)); | ||
$this->assertTrue(rewind($stream)); | ||
|
||
$this->assertInternalType('array', json_decode(fread($stream, 1024), true)); | ||
$this->assertSame('', fread($stream, 1)); | ||
$this->assertTrue(feof($stream)); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this purposeful that the methods are
stream_open
and notstreamOpen
. I see this is mixed now . Accidental ?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My bad, I see where this is ending up. https://www.php.net/manual/en/stream.streamwrapper.example-1.php . Sorry about the comment.