-
Notifications
You must be signed in to change notification settings - Fork 1.9k
fix: allow string as parameter to CURLRequest version #9021
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
fix: allow string as parameter to CURLRequest version #9021
Conversation
As indicated in the docs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would simplify this a bit.
system/HTTP/CURLRequest.php
Outdated
if (! empty($config['version'])) { | ||
if ($config['version'] === 1.0) { | ||
if ($config['version'] === 1.0 || $config['version'] === '1.0') { | ||
$curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_0; | ||
} elseif ($config['version'] === 1.1) { | ||
} elseif ($config['version'] === 1.1 || $config['version'] === '1.1') { | ||
$curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_1_1; | ||
} elseif ($config['version'] === 2.0) { | ||
} elseif ($config['version'] === 2.0 || $config['version'] === '2.0') { | ||
$curlOptions[CURLOPT_HTTP_VERSION] = CURL_HTTP_VERSION_2_0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can't we just cast it to string
or float
and do only one check?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Glad for the question - that was my first thought, but checking the rest of the code in the Library having all strict comparisons made me reluctant. Would (string)$config['version'] === '1.0'
have any side-effects? is float-to-string cast always locale-independant?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't believe it will cause any problems, we use this config value to set this one option and nothing else.
Casting to string should not have any side effects in this case.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But.... seems like sprintf()
should be used - https://www.php.net/manual/en/function.strval.php
"This function performs no formatting on the returned value. If you are looking for a way to format a numeric value as a string, please see sprintf() or number_format()."
Edge-cases.... brrrr.....
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was quite surprised by the original code actually. Is float 1.0
always exactly 1.0?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, you're right. I guess we should use something like:
$version = sprintf('%.1f', $config['version'])
at the beginning. That should be safe enough.
I was quite surprised by the original code actually. Is float 1.0 always exactly 1.0?
Yes, it's safe in our case. The problem may occur when we start doing operations on float value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pushed a new commit with that conversion
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Seems to be ok...
<?php
$config['version'] = '1.0';
$version = sprintf('%.1f', $config['version']);
var_dump($version);
$config['version'] = 1.0;
$version = sprintf('%.1f', $config['version']);
var_dump($version);
/* returns:
/opt/project/cast.php:5:
string(3) "1.0"
/opt/project/cast.php:9:
string(3) "1.0"
*/
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
As far as I know, (string)
is locale-independent. But var_dump((string) 0.00001);
returns "1.0E-5"
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good. We just need a changelog update in the docs.
In what file should I do that or do you do it? |
It is preferable to include this in the PR: https://github.com/codeigniter4/CodeIgniter4/blob/develop/user_guide_src/source/changelogs/v4.5.4.rst |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
system/HTTP/CURLRequest.php
Outdated
@@ -651,11 +651,12 @@ protected function setCURLOptions(array $curlOptions = [], array $config = []) | |||
|
|||
// version | |||
if (! empty($config['version'])) { | |||
if ($config['version'] === 1.0) { | |||
$version = sprintf('%.1f', $config['version']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The format is wrong. f
is locale aware. See https://www.php.net/manual/en/function.sprintf.php#refsect1-function.sprintf-parameters
$version = sprintf('%.1f', $config['version']); | |
$version = sprintf('%.1F, $config['version']); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You are correct! Fixed and pushed a new update.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM!
Description
The docs (as of 4.5.3) specifies "To set the HTTP protocol to use, you can pass a string or float with the version number". However, the code in the library does a strict comparison with
=== 2.0
so a string value will not work.Adding a strict comparison with
'1.0'
,'1.1'
and'2.0'
since this seems to be the coding style used in the library. (A==
comparison would work, but casting could introduce some issues?)Checklist: