Skip to content

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

Merged
merged 7 commits into from
Jul 7, 2024

Conversation

tangix
Copy link
Contributor

@tangix tangix commented Jul 4, 2024

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:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@tangix tangix changed the title Allow string as parameter to CURLRequest version fix: allow string as parameter to CURLRequest version Jul 4, 2024
@ddevsr ddevsr added the tests needed Pull requests that need tests label Jul 4, 2024
@ddevsr
Copy link
Collaborator

ddevsr commented Jul 4, 2024

@tangix
Copy link
Contributor Author

tangix commented Jul 4, 2024

@ddevsr ddevsr added bug Verified issues on the current code behavior or pull requests that will fix them and removed tests needed Pull requests that need tests labels Jul 4, 2024
@ddevsr ddevsr requested review from kenjis and michalsn July 4, 2024 15:19
Copy link
Member

@michalsn michalsn left a 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.

Comment on lines 653 to 659
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;
Copy link
Member

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?

Copy link
Contributor Author

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?

Copy link
Member

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.

Copy link
Contributor Author

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.....

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 was quite surprised by the original code actually. Is float 1.0 always exactly 1.0?

Copy link
Member

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.

Copy link
Contributor Author

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

Copy link
Contributor Author

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"

*/

Copy link
Member

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".

Copy link
Member

@michalsn michalsn left a 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.

@tangix
Copy link
Contributor Author

tangix commented Jul 4, 2024

Looks good. We just need a changelog update in the docs.

In what file should I do that or do you do it?

@michalsn
Copy link
Member

michalsn commented Jul 4, 2024

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
Please add an entry in the bug fixes section.

kenjis
kenjis previously approved these changes Jul 4, 2024
Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

LGTM!

@kenjis kenjis dismissed their stale review July 4, 2024 22:14

I will reconsider.

@@ -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']);
Copy link
Member

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

Suggested change
$version = sprintf('%.1f', $config['version']);
$version = sprintf('%.1F, $config['version']);

Copy link
Contributor Author

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.

Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

LGTM!

@kenjis kenjis merged commit c23375b into codeigniter4:develop Jul 7, 2024
42 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Verified issues on the current code behavior or pull requests that will fix them
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants