Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit e3950a4

Browse filesBrowse files
bug #58901 [HttpClient] Ignore RuntimeExceptions thrown when rewinding the PSR-7 created in HttplugWaitLoop::createPsr7Response (KurtThiemann)
This PR was squashed before being merged into the 6.4 branch. Discussion ---------- [HttpClient] Ignore RuntimeExceptions thrown when rewinding the PSR-7 created in HttplugWaitLoop::createPsr7Response | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT To create a PSR-7 response from a regular Symfony HTTP response in Psr18Client and HttplugClient, the static method `HttplugWaitLoop::createPsr7Response` is used. It also converts the response body stream to a PSR-7 stream, which generally happens in two steps: First, the `ResponseStreamInterface` is made into a resource using a custom stream wrapper, and then this resource is made into a PSR-7 stream using the `createStreamFromResource` of whatever stream factory is used. Finally, if the resulting PSR-7 stream is seekable, it is rewound to its start. Depending on the used stream implementation, it is, however, possible that calling `->seek(0)` throws an exception, despite `->isSeekable()` returning true. This is because the way some PSR-7 stream implementations check if the underlying resource is seekable is by looking at the `seekable` stream meta property. For custom stream wrappers, this metadata value appears to be always true, no matter if the stream wrapper implements seeking or not. Some stream implementations, like the one in nyholm/psr7, prevent this by just trying to call `fseek` on the resource when the stream is first created and considering it not seekable if the operation fails. Other implementations like guzzle/psr7 don't do this extra check, which is why using the Symfony Psr18Client with a Guzzle stream factory fails. Since the Psr18Client automatically discovers stream factory implementations if none is specified, the Guzzle implementation can become the default if Guzzle happens to be installed in the project. The solution I'm proposing here is to simply ignore any RuntimeException thrown when trying to seek to the start of the stream. Since rewinding the stream is already optional, this shouldn't cause any issues. Commits ------- 74dc4d2 [HttpClient] Ignore RuntimeExceptions thrown when rewinding the PSR-7 created in HttplugWaitLoop::createPsr7Response
2 parents e46482c + 74dc4d2 commit e3950a4
Copy full SHA for e3950a4

File tree

Expand file treeCollapse file tree

3 files changed

+25
-5
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+25
-5
lines changed

‎src/Symfony/Component/HttpClient/HttplugClient.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/HttplugClient.php
+10-2Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,11 @@ public function createStream($content = ''): StreamInterface
202202
}
203203

204204
if ($stream->isSeekable()) {
205-
$stream->seek(0);
205+
try {
206+
$stream->seek(0);
207+
} catch (\RuntimeException) {
208+
// ignore
209+
}
206210
}
207211

208212
return $stream;
@@ -274,7 +278,11 @@ private function sendPsr7Request(RequestInterface $request, ?bool $buffer = null
274278
$body = $request->getBody();
275279

276280
if ($body->isSeekable()) {
277-
$body->seek(0);
281+
try {
282+
$body->seek(0);
283+
} catch (\RuntimeException) {
284+
// ignore
285+
}
278286
}
279287

280288
$options = [

‎src/Symfony/Component/HttpClient/Internal/HttplugWaitLoop.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Internal/HttplugWaitLoop.php
+5-1Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,11 @@ public static function createPsr7Response(ResponseFactoryInterface $responseFact
145145
}
146146

147147
if ($body->isSeekable()) {
148-
$body->seek(0);
148+
try {
149+
$body->seek(0);
150+
} catch (\RuntimeException) {
151+
// ignore
152+
}
149153
}
150154

151155
return $psrResponse->withBody($body);

‎src/Symfony/Component/HttpClient/Psr18Client.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Psr18Client.php
+10-2Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,11 @@ public function sendRequest(RequestInterface $request): ResponseInterface
9090
$body = $request->getBody();
9191

9292
if ($body->isSeekable()) {
93-
$body->seek(0);
93+
try {
94+
$body->seek(0);
95+
} catch (\RuntimeException) {
96+
// ignore
97+
}
9498
}
9599

96100
$options = [
@@ -136,7 +140,11 @@ public function createStream(string $content = ''): StreamInterface
136140
$stream = $this->streamFactory->createStream($content);
137141

138142
if ($stream->isSeekable()) {
139-
$stream->seek(0);
143+
try {
144+
$stream->seek(0);
145+
} catch (\RuntimeException) {
146+
// ignore
147+
}
140148
}
141149

142150
return $stream;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.