Skip to content

Commit e86dcfa

Browse files
[HttpClient] fix casting responses to PHP streams
1 parent 47f7cdc commit e86dcfa

File tree

3 files changed

+20
-11
lines changed

3 files changed

+20
-11
lines changed

src/Symfony/Component/HttpClient/NativeHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ public function request(string $method, string $url, array $options = []): Respo
169169
$this->multi->dnsCache = $options['resolve'] + $this->multi->dnsCache;
170170
}
171171

172-
$this->logger && $this->logger->info(sprintf('Request: %s %s', $method, implode('', $url)));
172+
$this->logger && $this->logger->info(sprintf('Request: "%s %s"', $method, implode('', $url)));
173173

174174
[$host, $port, $url['authority']] = self::dnsResolve($url, $this->multi, $info, $onProgress);
175175

src/Symfony/Component/HttpClient/Response/ResponseTrait.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,7 +204,11 @@ public function toStream(bool $throw = true)
204204
$this->getHeaders($throw);
205205
}
206206

207-
return StreamWrapper::createResource($this, null, $this->content, $this->handle && 'stream' === get_resource_type($this->handle) ? $this->handle : null);
207+
$stream = StreamWrapper::createResource($this);
208+
stream_get_meta_data($stream)['wrapper_data']
209+
->bindHandles($this->handle, $this->content);
210+
211+
return $stream;
208212
}
209213

210214
/**

src/Symfony/Component/HttpClient/Response/StreamWrapper.php

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,9 @@ class StreamWrapper
4343
/**
4444
* Creates a PHP stream resource from a ResponseInterface.
4545
*
46-
* @param resource|null $contentBuffer The seekable resource where the response body is buffered
47-
* @param resource|null $selectHandle The resource handle that should be monitored when
48-
* stream_select() is used on the created stream
49-
*
5046
* @return resource
5147
*/
52-
public static function createResource(ResponseInterface $response, HttpClientInterface $client = null, $contentBuffer = null, $selectHandle = null)
48+
public static function createResource(ResponseInterface $response, HttpClientInterface $client = null)
5349
{
5450
if (null === $client && !method_exists($response, 'stream')) {
5551
throw new \InvalidArgumentException(sprintf('Providing a client to "%s()" is required when the response doesn\'t have any "stream()" method.', __CLASS__));
@@ -63,8 +59,6 @@ public static function createResource(ResponseInterface $response, HttpClientInt
6359
$context = [
6460
'client' => $client ?? $response,
6561
'response' => $response,
66-
'content' => $contentBuffer,
67-
'handle' => $selectHandle,
6862
];
6963

7064
return fopen('symfony://'.$response->getInfo('url'), 'r', false, stream_context_create(['symfony' => $context])) ?: null;
@@ -78,6 +72,17 @@ public function getResponse(): ResponseInterface
7872
return $this->response;
7973
}
8074

75+
/**
76+
* @param resource|null $handle The resource handle that should be monitored when
77+
* stream_select() is used on the created stream
78+
* @param resource|null $content The seekable resource where the response body is buffered
79+
*/
80+
public function bindHandles(&$handle, &$content): void
81+
{
82+
$this->handle = &$handle;
83+
$this->content = &$content;
84+
}
85+
8186
public function stream_open(string $path, string $mode, int $options): bool
8287
{
8388
if ('r' !== $mode) {
@@ -91,8 +96,6 @@ public function stream_open(string $path, string $mode, int $options): bool
9196
$context = stream_context_get_options($this->context)['symfony'] ?? null;
9297
$this->client = $context['client'] ?? null;
9398
$this->response = $context['response'] ?? null;
94-
$this->content = $context['content'] ?? null;
95-
$this->handle = $context['handle'] ?? null;
9699
$this->context = null;
97100

98101
if (null !== $this->client && null !== $this->response) {
@@ -238,6 +241,8 @@ public function stream_seek(int $offset, int $whence = SEEK_SET): bool
238241
public function stream_cast(int $castAs)
239242
{
240243
if (STREAM_CAST_FOR_SELECT === $castAs) {
244+
$this->response->getHeaders(false);
245+
241246
return $this->handle ?? false;
242247
}
243248

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy