Skip to content

Navigation Menu

Sign in
Appearance settings

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

[HttpClient] Let curl handle Content-Length headers #45814

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion 7 src/Symfony/Component/HttpClient/CurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,12 @@ public function request(string $method, string $url, array $options = []): Respo
$options['headers'][] = 'Accept-Encoding: gzip'; // Expose only one encoding, some servers mess up when more are provided
}

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

foreach ($options['headers'] as $header) {
if ($hasContentLength && 0 === stripos($header, 'Content-Length:')) {
continue; // Let curl handle Content-Length headers
}
if (':' === $header[-2] && \strlen($header) - 2 === strpos($header, ': ')) {
// curl requires a special syntax to send empty headers
$curlopts[\CURLOPT_HTTPHEADER][] = substr_replace($header, ';', -2);
Expand All @@ -229,7 +234,7 @@ public function request(string $method, string $url, array $options = []): Respo
};
}

if (isset($options['normalized_headers']['content-length'][0])) {
if ($hasContentLength) {
$curlopts[\CURLOPT_INFILESIZE] = substr($options['normalized_headers']['content-length'][0], \strlen('Content-Length: '));
} elseif (!isset($options['normalized_headers']['transfer-encoding'])) {
$curlopts[\CURLOPT_HTTPHEADER][] = 'Transfer-Encoding: chunked'; // Enable chunked request bodies
Expand Down
11 changes: 11 additions & 0 deletions 11 src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,17 @@ public function testNegativeTimeout()
])->getStatusCode());
}

public function testRedirectAfterPost()
{
$client = $this->getHttpClient(__FUNCTION__);

$response = $client->request('POST', 'http://localhost:8057/302/relative', [
'body' => 'abc',
]);

$this->assertSame(200, $response->getStatusCode());
}

public function testNullBody()
{
$client = $this->getHttpClient(__FUNCTION__);
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.