Skip to content

Commit 62e681f

Browse files
[HttpClient] Let curl handle Content-Length headers
1 parent ebf1ccb commit 62e681f

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

CurlHttpClient.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,12 @@ public function request(string $method, string $url, array $options = []): Respo
202202
$options['headers'][] = 'Accept-Encoding: gzip'; // Expose only one encoding, some servers mess up when more are provided
203203
}
204204

205+
$hasContentLength = isset($options['normalized_headers']['content-length'][0]);
206+
205207
foreach ($options['headers'] as $header) {
208+
if ($hasContentLength && 0 === stripos($header, 'Content-Length:')) {
209+
continue; // Let curl handle Content-Length headers
210+
}
206211
if (':' === $header[-2] && \strlen($header) - 2 === strpos($header, ': ')) {
207212
// curl requires a special syntax to send empty headers
208213
$curlopts[\CURLOPT_HTTPHEADER][] = substr_replace($header, ';', -2);
@@ -229,7 +234,7 @@ public function request(string $method, string $url, array $options = []): Respo
229234
};
230235
}
231236

232-
if (isset($options['normalized_headers']['content-length'][0])) {
237+
if ($hasContentLength) {
233238
$curlopts[\CURLOPT_INFILESIZE] = substr($options['normalized_headers']['content-length'][0], \strlen('Content-Length: '));
234239
} elseif (!isset($options['normalized_headers']['transfer-encoding'])) {
235240
$curlopts[\CURLOPT_HTTPHEADER][] = 'Transfer-Encoding: chunked'; // Enable chunked request bodies

Tests/HttpClientTestCase.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ public function testNegativeTimeout()
203203
])->getStatusCode());
204204
}
205205

206+
public function testRedirectAfterPost()
207+
{
208+
$client = $this->getHttpClient(__FUNCTION__);
209+
210+
$response = $client->request('POST', 'http://localhost:8057/302/relative', [
211+
'body' => 'abc',
212+
]);
213+
214+
$this->assertSame(200, $response->getStatusCode());
215+
}
216+
206217
public function testNullBody()
207218
{
208219
$client = $this->getHttpClient(__FUNCTION__);

0 commit comments

Comments
 (0)