Skip to content

Navigation Menu

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 bbdf0e0

Browse filesBrowse files
minor #58856 [HttpClient] Stream request body in HttplugClient and Psr18Client (KurtThiemann)
This PR was squashed before being merged into the 7.2 branch. Discussion ---------- [HttpClient] Stream request body in HttplugClient and Psr18Client | Q | A | ------------- | --- | Branch? | 7.2 | Bug fix? | yes | New feature? | no | Deprecations? | no | Issues | - | License | MIT Currently, `Psr18Client` and `HttplugClient` will simply call `getContents()` on the stream, loading the entire stream content into a string before passing it to the underlying `HttpClient`. When sending a request with a large body, this can lead to the PHP process running out of memory. This pull request changes both clients to instead pass a Closure that reads from the request body stream to the underlying `HttpClient`. The HttpClient documentation states that "each time, the closure should return a string smaller than the amount requested as argument", which would mean that if the closure was called requesting a size of 10 bytes, you are only allowed to return at most 9 bytes. I assume that is an inaccuracy in the docs, but please correct me if I'm wrong here. I'm also not fully sure whether this would be considered a new feature or a bug fix. Commits ------- e6d1182 [HttpClient] Stream request body in HttplugClient and Psr18Client
2 parents aef53e9 + e6d1182 commit bbdf0e0
Copy full SHA for bbdf0e0

File tree

2 files changed

+14
-4
lines changed
Filter options

2 files changed

+14
-4
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/HttplugClient.php
+7-2
Original file line numberDiff line numberDiff line change
@@ -229,9 +229,14 @@ private function sendPsr7Request(RequestInterface $request, ?bool $buffer = null
229229
$body->seek(0);
230230
}
231231

232+
$headers = $request->getHeaders();
233+
if (!$request->hasHeader('content-length') && 0 <= $size = $body->getSize() ?? -1) {
234+
$headers['Content-Length'] = [$size];
235+
}
236+
232237
$options = [
233-
'headers' => $request->getHeaders(),
234-
'body' => $body->getContents(),
238+
'headers' => $headers,
239+
'body' => static fn (int $size) => $body->read($size),
235240
'buffer' => $buffer,
236241
];
237242

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Psr18Client.php
+7-2
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,14 @@ public function sendRequest(RequestInterface $request): ResponseInterface
9393
$body->seek(0);
9494
}
9595

96+
$headers = $request->getHeaders();
97+
if (!$request->hasHeader('content-length') && 0 <= $size = $body->getSize() ?? -1) {
98+
$headers['Content-Length'] = [$size];
99+
}
100+
96101
$options = [
97-
'headers' => $request->getHeaders(),
98-
'body' => $body->getContents(),
102+
'headers' => $headers,
103+
'body' => static fn (int $size) => $body->read($size),
99104
];
100105

101106
if ('1.0' === $request->getProtocolVersion()) {

0 commit comments

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