Skip to content

Commit 88b6909

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

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
@@ -207,9 +207,11 @@ public function request(string $method, string $url, array $options = []): Respo
207207

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

210-
foreach ($options['headers'] as $header) {
210+
foreach ($options['headers'] as $i => $header) {
211211
if ($hasContentLength && 0 === stripos($header, 'Content-Length:')) {
212-
continue; // Let curl handle Content-Length headers
212+
// Let curl handle Content-Length headers
213+
unset($options['headers'][$i]);
214+
continue;
213215
}
214216
if (':' === $header[-2] && \strlen($header) - 2 === strpos($header, ': ')) {
215217
// 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
@@ -399,9 +399,12 @@ private static function createRedirectResolver(array $options, string $host, ?ar
399399
if ('POST' === $options['method'] || 303 === $info['http_code']) {
400400
$info['http_method'] = $options['method'] = 'HEAD' === $options['method'] ? 'HEAD' : 'GET';
401401
$options['content'] = '';
402-
$options['header'] = array_filter($options['header'], static function ($h) {
402+
$filterContentHeaders = static function ($h) {
403403
return 0 !== stripos($h, 'Content-Length:') && 0 !== stripos($h, 'Content-Type:');
404-
});
404+
};
405+
$options['header'] = array_filter($options['header'], $filterContentHeaders);
406+
$redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], $filterContentHeaders);
407+
$redirectHeaders['with_auth'] = array_filter($redirectHeaders['with_auth'], $filterContentHeaders);
405408

406409
stream_context_set_option($context, ['http' => $options]);
407410
}

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)