Skip to content

Commit a7930c4

Browse files
Merge branch '5.4' into 6.0
* 5.4: [HttpClient] On redirections don't send content-related request headers
2 parents 8272de5 + 88b6909 commit a7930c4

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

CurlHttpClient.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,9 +202,11 @@ public function request(string $method, string $url, array $options = []): Respo
202202

203203
$hasContentLength = isset($options['normalized_headers']['content-length'][0]);
204204

205-
foreach ($options['headers'] as $header) {
205+
foreach ($options['headers'] as $i => $header) {
206206
if ($hasContentLength && 0 === stripos($header, 'Content-Length:')) {
207-
continue; // Let curl handle Content-Length headers
207+
// Let curl handle Content-Length headers
208+
unset($options['headers'][$i]);
209+
continue;
208210
}
209211
if (':' === $header[-2] && \strlen($header) - 2 === strpos($header, ': ')) {
210212
// curl requires a special syntax to send empty headers

NativeHttpClient.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -391,9 +391,12 @@ private static function createRedirectResolver(array $options, string $host, ?ar
391391
if ('POST' === $options['method'] || 303 === $info['http_code']) {
392392
$info['http_method'] = $options['method'] = 'HEAD' === $options['method'] ? 'HEAD' : 'GET';
393393
$options['content'] = '';
394-
$options['header'] = array_filter($options['header'], static function ($h) {
394+
$filterContentHeaders = static function ($h) {
395395
return 0 !== stripos($h, 'Content-Length:') && 0 !== stripos($h, 'Content-Type:');
396-
});
396+
};
397+
$options['header'] = array_filter($options['header'], $filterContentHeaders);
398+
$redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], $filterContentHeaders);
399+
$redirectHeaders['with_auth'] = array_filter($redirectHeaders['with_auth'], $filterContentHeaders);
397400

398401
stream_context_set_option($context, ['http' => $options]);
399402
}

Tests/HttpClientTestCase.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,18 @@ public function testFixContentLength()
388388
$this->assertSame(['abc' => 'def', 'REQUEST_METHOD' => 'POST'], $body);
389389
}
390390

391+
public function testDropContentRelatedHeadersWhenFollowingRequestIsUsingGet()
392+
{
393+
$client = $this->getHttpClient(__FUNCTION__);
394+
395+
$response = $client->request('POST', 'http://localhost:8057/302', [
396+
'body' => 'foo',
397+
'headers' => ['Content-Length: 3'],
398+
]);
399+
400+
$this->assertSame(200, $response->getStatusCode());
401+
}
402+
391403
public function testNegativeTimeout()
392404
{
393405
$client = $this->getHttpClient(__FUNCTION__);

0 commit comments

Comments
 (0)