Skip to content
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

[HttpClient] Ignore RuntimeExceptions thrown when rewinding the PSR-7 created in HttplugWaitLoop::createPsr7Response #58901

Merged
merged 1 commit into from
Jan 8, 2025

Conversation

KurtThiemann
Copy link
Contributor

@KurtThiemann KurtThiemann commented Nov 16, 2024

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.

@carsonbot carsonbot added this to the 7.2 milestone Nov 16, 2024
@fabpot fabpot modified the milestones: 7.2, 7.3 Nov 20, 2024
@nicolas-grekas nicolas-grekas modified the milestones: 7.3, 7.2 Nov 20, 2024
@nicolas-grekas nicolas-grekas modified the milestones: 7.2, 6.4 Jan 8, 2025
@nicolas-grekas nicolas-grekas changed the base branch from 7.3 to 6.4 January 8, 2025 09:30
Copy link
Member

@nicolas-grekas nicolas-grekas left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess we should do the same in Psr18Client and HttplugClient when seek(0) is called.

… created in HttplugWaitLoop::createPsr7Response
@nicolas-grekas
Copy link
Member

Thank you @KurtThiemann.

@nicolas-grekas nicolas-grekas merged commit e3950a4 into symfony:6.4 Jan 8, 2025
8 checks passed
This was referenced Jan 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants