From af7955a3d70f4232df8989b54ee8abb05ec67d04 Mon Sep 17 00:00:00 2001 From: Aleksa Nikolic <50905985+nikolicaleksa@users.noreply.github.com> Date: Mon, 28 Oct 2024 15:21:56 +0100 Subject: [PATCH] feat: Add support timeouts in milliseconds for HTTP client (#1783) --- src/HttpClient/HttpClient.php | 16 ++++++++++++++-- tests/OptionsTest.php | 14 ++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/HttpClient/HttpClient.php b/src/HttpClient/HttpClient.php index e94452a14..879e570e0 100644 --- a/src/HttpClient/HttpClient.php +++ b/src/HttpClient/HttpClient.php @@ -64,8 +64,6 @@ public function sendRequest(Request $request, Options $options): Response curl_setopt($curlHandle, \CURLOPT_URL, $dsn->getEnvelopeApiEndpointUrl()); curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, $requestHeaders); curl_setopt($curlHandle, \CURLOPT_USERAGENT, $this->sdkIdentifier . '/' . $this->sdkVersion); - curl_setopt($curlHandle, \CURLOPT_TIMEOUT, $options->getHttpTimeout()); - curl_setopt($curlHandle, \CURLOPT_CONNECTTIMEOUT, $options->getHttpConnectTimeout()); curl_setopt($curlHandle, \CURLOPT_ENCODING, ''); curl_setopt($curlHandle, \CURLOPT_POST, true); curl_setopt($curlHandle, \CURLOPT_POSTFIELDS, $requestData); @@ -73,6 +71,20 @@ public function sendRequest(Request $request, Options $options): Response curl_setopt($curlHandle, \CURLOPT_HEADERFUNCTION, $responseHeaderCallback); curl_setopt($curlHandle, \CURLOPT_HTTP_VERSION, \CURL_HTTP_VERSION_1_1); + $httpTimeout = $options->getHttpTimeout(); + if ($httpTimeout < 1.0) { + curl_setopt($curlHandle, \CURLOPT_TIMEOUT_MS, $httpTimeout * 1000); + } else { + curl_setopt($curlHandle, \CURLOPT_TIMEOUT, $httpTimeout); + } + + $connectTimeout = $options->getHttpConnectTimeout(); + if ($connectTimeout < 1.0) { + curl_setopt($curlHandle, \CURLOPT_CONNECTTIMEOUT_MS, $connectTimeout * 1000); + } else { + curl_setopt($curlHandle, \CURLOPT_CONNECTTIMEOUT, $connectTimeout); + } + $httpSslVerifyPeer = $options->getHttpSslVerifyPeer(); if (!$httpSslVerifyPeer) { curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, false); diff --git a/tests/OptionsTest.php b/tests/OptionsTest.php index ddae2f913..3bbc3c5ea 100644 --- a/tests/OptionsTest.php +++ b/tests/OptionsTest.php @@ -341,6 +341,13 @@ static function (): void {}, 'setHttpTimeout', ]; + yield [ + 'http_timeout', + 0.2, + 'getHttpTimeout', + 'setHttpTimeout', + ]; + yield [ 'http_connect_timeout', 1, @@ -355,6 +362,13 @@ static function (): void {}, 'setHttpConnectTimeout', ]; + yield [ + 'http_connect_timeout', + 0.2, + 'getHttpConnectTimeout', + 'setHttpConnectTimeout', + ]; + yield [ 'http_ssl_verify_peer', false,