-
-
Notifications
You must be signed in to change notification settings - Fork 463
feat(transport): use share handle #1996
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,10 +22,47 @@ class HttpClient implements HttpClientInterface | |
| */ | ||
| protected $sdkVersion; | ||
|
|
||
| /** | ||
| * Either a persistent share handle or a regular share handle, or null if no share handle can be obtained. | ||
| * | ||
| * @var object|resource|null | ||
| */ | ||
| private $shareHandle; | ||
|
|
||
| public function __construct(string $sdkIdentifier, string $sdkVersion) | ||
| { | ||
| $this->sdkIdentifier = $sdkIdentifier; | ||
| $this->sdkVersion = $sdkVersion; | ||
| if (\function_exists('curl_share_init_persistent')) { | ||
| $shareOptions = [\CURL_LOCK_DATA_DNS]; | ||
| if (\defined('CURL_LOCK_DATA_CONNECT')) { | ||
| $shareOptions[] = \CURL_LOCK_DATA_CONNECT; | ||
| } | ||
| if (\defined('CURL_LOCK_DATA_SSL_SESSION')) { | ||
| $shareOptions[] = \CURL_LOCK_DATA_SSL_SESSION; | ||
| } | ||
| try { | ||
| $this->shareHandle = curl_share_init_persistent($shareOptions); | ||
| } catch (\Throwable $throwable) { | ||
| // don't crash if the share handle cannot be created | ||
| } | ||
| } | ||
|
|
||
| // If the persistent share handle cannot be created or doesn't exist | ||
| if ($this->shareHandle === null) { | ||
| try { | ||
| $this->shareHandle = curl_share_init(); | ||
| curl_share_setopt($this->shareHandle, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_DNS); | ||
| if (\defined('CURL_LOCK_DATA_CONNECT')) { | ||
| curl_share_setopt($this->shareHandle, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_CONNECT); | ||
| } | ||
| if (\defined('CURL_LOCK_DATA_SSL_SESSION')) { | ||
| curl_share_setopt($this->shareHandle, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_SSL_SESSION); | ||
| } | ||
| } catch (\Throwable $throwable) { | ||
| // don't crash if the share handle cannot be created | ||
| } | ||
|
Comment on lines
+58
to
+64
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Bug: The return values of 🔍 Detailed AnalysisThe 💡 Suggested FixCheck the boolean return value of each 🤖 Prompt for AI AgentDid we get this right? 👍 / 👎 to inform future reviews. |
||
| } | ||
| } | ||
|
|
||
| public function sendRequest(Request $request, Options $options): Response | ||
|
|
@@ -72,6 +109,9 @@ public function sendRequest(Request $request, Options $options): Response | |
| curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true); | ||
| curl_setopt($curlHandle, \CURLOPT_HEADERFUNCTION, $responseHeaderCallback); | ||
| curl_setopt($curlHandle, \CURLOPT_HTTP_VERSION, \CURL_HTTP_VERSION_1_1); | ||
| if ($this->shareHandle !== null) { | ||
| curl_setopt($curlHandle, \CURLOPT_SHARE, $this->shareHandle); | ||
| } | ||
|
Comment on lines
+112
to
+114
This comment was marked as outdated.
Sorry, something went wrong.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The documentation doesn't mention that |
||
|
|
||
| $httpSslVerifyPeer = $options->getHttpSslVerifyPeer(); | ||
| if (!$httpSslVerifyPeer) { | ||
|
|
||
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.
Missing check for false return from curl_share_init
Medium Severity
When
curl_share_init()returnsfalse(possible in PHP 7.x on failure),$this->shareHandleis set tofalse. The checkif ($this->shareHandle !== null)passes sincefalse !== nullis true, causingcurl_setopt()to be called withfalseas the share handle. The PR discussion explicitly noted this concern ("an extra check doesn't hurt") but the check wasn't added. This could cause warnings and prevent proper fallback behavior when share handle initialization fails.Additional Locations (1)
src/HttpClient/HttpClient.php#L111-L114