Skip to content

Commit c66fc3b

Browse files
xabbuhnicolas-grekas
authored andcommitted
[HttpClient] On redirections don't send content-related request headers
1 parent 4e7fe5e commit c66fc3b

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

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

207-
foreach ($options['headers'] as $header) {
207+
foreach ($options['headers'] as $i => $header) {
208208
if ($hasContentLength && 0 === stripos($header, 'Content-Length:')) {
209-
continue; // Let curl handle Content-Length headers
209+
// Let curl handle Content-Length headers
210+
unset($options['headers'][$i]);
211+
continue;
210212
}
211213
if (':' === $header[-2] && \strlen($header) - 2 === strpos($header, ': ')) {
212214
// 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
@@ -430,9 +430,12 @@ private static function createRedirectResolver(array $options, string $host, ?ar
430430
if ('POST' === $options['method'] || 303 === $info['http_code']) {
431431
$info['http_method'] = $options['method'] = 'HEAD' === $options['method'] ? 'HEAD' : 'GET';
432432
$options['content'] = '';
433-
$options['header'] = array_filter($options['header'], static function ($h) {
433+
$filterContentHeaders = static function ($h) {
434434
return 0 !== stripos($h, 'Content-Length:') && 0 !== stripos($h, 'Content-Type:');
435-
});
435+
};
436+
$options['header'] = array_filter($options['header'], $filterContentHeaders);
437+
$redirectHeaders['no_auth'] = array_filter($redirectHeaders['no_auth'], $filterContentHeaders);
438+
$redirectHeaders['with_auth'] = array_filter($redirectHeaders['with_auth'], $filterContentHeaders);
436439

437440
stream_context_set_option($context, ['http' => $options]);
438441
}

Tests/HttpClientTestCase.php

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

197+
public function testDropContentRelatedHeadersWhenFollowingRequestIsUsingGet()
198+
{
199+
$client = $this->getHttpClient(__FUNCTION__);
200+
201+
$response = $client->request('POST', 'http://localhost:8057/302', [
202+
'body' => 'foo',
203+
'headers' => ['Content-Length: 3'],
204+
]);
205+
206+
$this->assertSame(200, $response->getStatusCode());
207+
}
208+
197209
public function testNegativeTimeout()
198210
{
199211
$client = $this->getHttpClient(__FUNCTION__);

0 commit comments

Comments
 (0)