Skip to content

[HttpClient] Prevent empty request body stream in HttplugClient and Psr18Client #59691

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

Closed

Conversation

ajgarlag
Copy link
Contributor

@ajgarlag ajgarlag commented Feb 4, 2025

Q A
Branch? 7.2
Bug fix? yes
New feature? no
Deprecations? no
Issues
License MIT

I've detected that several headers related to the body content are sent to upstream HTTP servers while sending any GET request with an empty body since I updated my app to Symfony 7.2

This PR prevents sending these headers when the body is known to be empty.

This prevents adding `Content-Length` and `Transfer-Encoding` headers
when sending a request with an empty body using CurlHttpClient
@nicolas-grekas
Copy link
Member

Thank you @ajgarlag
I've added your commit to #59648 which is patching a very related issue.

@ajgarlag
Copy link
Contributor Author

ajgarlag commented Feb 4, 2025

@nicolas-grekas Thanks. I have a related issue: We have a proxy implementation that forwards the received requests to an upstream server using PSR-7.

The upstream server was responding with a 5xx status code response because we were sending a Content-Type: application/x-www-form-urlencoded in GET requests.

When the ServerRequest object is created in PsrHttpFactory, the body stream wraps the fopen('php://input', 'r') resource which size is unknown ($psr7Request->getBody()->getSize() === null) but I think that when the method is not PATCH, PUT or POST, is safe to convert the body to string instead of streaming it because it will be usually an empty body.

This will prevent sending body content related headers in this case.

WDYT?

@@ -104,7 +104,7 @@ final class Psr18Client implements ClientInterface, RequestFactoryInterface, Str
 
             $options = [
                 'headers' => $headers,
-                'body' => 0 === $size ? '' : static fn (int $size) => $body->read($size),
+                'body' => 0 >= $size && false === array_search($request->getMethod(), ['PATCH', 'POST', 'PUT'], true) ? $body->getContents() : static fn (int $size) => $body->read($size),
             ];
 
             if ('1.0' === $request->getProtocolVersion()) {

@nicolas-grekas
Copy link
Member

I'd say this is on you when you create the request object...

@ajgarlag
Copy link
Contributor Author

ajgarlag commented Feb 4, 2025

I don't control how the request is created. The PSR7 request object is created using the Symfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory which calls Symfony\Component\HttpFoundation\Request::getContent.

I have a middleware to convert the body to string when the method is not expected to have a body, but I think this could be fixed in Symfony side.

$body = $this->streamFactory->createStreamFromResource($symfonyRequest->getContent(true));

return fopen('php://input', 'r');

@nicolas-grekas
Copy link
Member

I don't see how Symfony can do anything about it. GET requests can have a body so that the logic you suggest based on methods might work for you, but it won't for everybody. You're still in control of creating the HttpFoundation's Request object so you can do something about this.

@ajgarlag
Copy link
Contributor Author

ajgarlag commented Feb 4, 2025

I'll try to move my logic to the HttpFoundation request creation.

Thanks for the conversation.

@ajgarlag ajgarlag deleted the prevent-empty-request-body-streaming branch February 7, 2025 11:45
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.

3 participants