Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

[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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Conversation

ajgarlag
Copy link
Contributor

QA
Branch?7.2
Bug fix?yes
New feature?no
Deprecations?no
Issues
LicenseMIT

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` headerswhen 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
ContributorAuthor

ajgarlag commentedFeb 4, 2025
edited
Loading

@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 aContent-Type: application/x-www-form-urlencoded in GET requests.

When the ServerRequest object is created in PsrHttpFactory, the body stream wraps thefopen('php://input', 'r') resource which size is unknown ($psr7Request->getBody()->getSize() === null) but I think that when the method is notPATCH,PUT orPOST, 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
ContributorAuthor

I don't control how the request is created. The PSR7 request object is created using theSymfony\Bridge\PsrHttpMessage\Factory\PsrHttpFactory which callsSymfony\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));

returnfopen('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
ContributorAuthor

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

Thanks for the conversation.

@ajgarlagajgarlag deleted the prevent-empty-request-body-streaming branchFebruary 7, 2025 11:45
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Projects
None yet
Milestone
7.2
Development

Successfully merging this pull request may close these issues.

3 participants
@ajgarlag@nicolas-grekas@carsonbot

[8]ページ先頭

©2009-2025 Movatter.jp