Skip to content

[HttpClient] Always set CURLOPT_CUSTOMREQUEST to the correct HTTP method in CurlHttpClient #59062

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
Dec 2, 2024
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
3 changes: 1 addition & 2 deletions src/Symfony/Component/HttpClient/CurlHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,13 +197,12 @@ public function request(string $method, string $url, array $options = []): Respo
$curlopts[\CURLOPT_RESOLVE] = $resolve;
}

$curlopts[\CURLOPT_CUSTOMREQUEST] = $method;
if ('POST' === $method) {
// Use CURLOPT_POST to have browser-like POST-to-GET redirects for 301, 302 and 303
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolas-grekas wouldn't the usage of CURLOPT_CUSTOMREQUEST defeat this comment ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's fine because it's set after.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I, too, thought that, but after testing it just now it seems like it might not be fine...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I looked into this, and it seems like this would be an issue in plain cURL, but actually works correctly in Symfony because redirects are handled in CurlResponse::parseHeaderLine instead of being handled by cURL itself.
I might still make a PR to add a test for this.

$curlopts[\CURLOPT_POST] = true;
} elseif ('HEAD' === $method) {
$curlopts[\CURLOPT_NOBODY] = true;
} else {
$curlopts[\CURLOPT_CUSTOMREQUEST] = $method;
}

if ('\\' !== \DIRECTORY_SEPARATOR && $options['timeout'] < 1) {
Expand Down
22 changes: 22 additions & 0 deletions src/Symfony/Component/HttpClient/Tests/HttpClientTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -651,4 +651,26 @@ public function testDefaultContentType()

$this->assertSame(['abc' => 'def', 'content-type' => 'application/json', 'REQUEST_METHOD' => 'POST'], $response->toArray());
}

public function testHeadRequestWithClosureBody()
{
$p = TestHttpServer::start(8067);

try {
$client = $this->getHttpClient(__FUNCTION__);

$response = $client->request('HEAD', 'http://localhost:8057/head', [
'body' => fn () => '',
]);
$headers = $response->getHeaders();
} finally {
$p->stop();
}

$this->assertArrayHasKey('x-request-vars', $headers);

$vars = json_decode($headers['x-request-vars'][0], true);
$this->assertIsArray($vars);
$this->assertSame('HEAD', $vars['REQUEST_METHOD']);
}
}
2 changes: 1 addition & 1 deletion src/Symfony/Component/HttpClient/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"php": ">=8.1",
"psr/log": "^1|^2|^3",
"symfony/deprecation-contracts": "^2.5|^3",
"symfony/http-client-contracts": "~3.4.3|^3.5.1",
"symfony/http-client-contracts": "~3.4.4|^3.5.2",
"symfony/service-contracts": "^2.5|^3"
},
"require-dev": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
exit;

case '/head':
header('X-Request-Vars: '.json_encode($vars, \JSON_UNESCAPED_SLASHES | \JSON_UNESCAPED_UNICODE));
header('Content-Length: '.strlen($json), true);
break;

Expand Down
Loading