| 1 |
<?php |
| 2 |
|
| 3 |
declare (strict_types=1); |
| 4 |
namespace WCPOS\Vendor\Sentry\HttpClient; |
| 5 |
|
| 6 |
use WCPOS\Vendor\Sentry\Options; |
| 7 |
use WCPOS\Vendor\Sentry\Util\Http; |
| 8 |
/** |
| 9 |
* @internal |
| 10 |
*/ |
| 11 |
class HttpClient implements HttpClientInterface |
| 12 |
{ |
| 13 |
/** |
| 14 |
* @var string The Sentry SDK identifier |
| 15 |
*/ |
| 16 |
protected $sdkIdentifier; |
| 17 |
/** |
| 18 |
* @var string The Sentry SDK identifier |
| 19 |
*/ |
| 20 |
protected $sdkVersion; |
| 21 |
/** |
| 22 |
* Either a persistent share handle or a regular share handle, or null if no share handle can be obtained. |
| 23 |
* |
| 24 |
* @var object|resource|null |
| 25 |
*/ |
| 26 |
private $shareHandle; |
| 27 |
public function __construct(string $sdkIdentifier, string $sdkVersion) |
| 28 |
{ |
| 29 |
$this->sdkIdentifier = $sdkIdentifier; |
| 30 |
$this->sdkVersion = $sdkVersion; |
| 31 |
} |
| 32 |
public function sendRequest(Request $request, Options $options) : Response |
| 33 |
{ |
| 34 |
$dsn = $options->getDsn(); |
| 35 |
if ($dsn === null) { |
| 36 |
throw new \RuntimeException('The DSN option must be set to use the HttpClient.'); |
| 37 |
} |
| 38 |
$requestData = $request->getStringBody(); |
| 39 |
if ($requestData === null) { |
| 40 |
throw new \RuntimeException('The request data is empty.'); |
| 41 |
} |
| 42 |
if (!\extension_loaded('curl')) { |
| 43 |
throw new \RuntimeException('The cURL PHP extension must be enabled to use the HttpClient.'); |
| 44 |
} |
| 45 |
$curlHandle = \curl_init(); |
| 46 |
$requestHeaders = Http::getRequestHeaders($dsn, $this->sdkIdentifier, $this->sdkVersion); |
| 47 |
if (\extension_loaded('zlib') && $options->isHttpCompressionEnabled()) { |
| 48 |
$requestData = \gzcompress($requestData, -1, \ZLIB_ENCODING_GZIP); |
| 49 |
$requestHeaders[] = 'Content-Encoding: gzip'; |
| 50 |
} |
| 51 |
$responseHeaders = []; |
| 52 |
$responseHeaderCallback = static function ($curlHandle, $headerLine) use(&$responseHeaders) : int { |
| 53 |
return Http::parseResponseHeaders($headerLine, $responseHeaders); |
| 54 |
}; |
| 55 |
\curl_setopt($curlHandle, \CURLOPT_URL, $dsn->getEnvelopeApiEndpointUrl()); |
| 56 |
\curl_setopt($curlHandle, \CURLOPT_HTTPHEADER, $requestHeaders); |
| 57 |
\curl_setopt($curlHandle, \CURLOPT_USERAGENT, $this->sdkIdentifier . '/' . $this->sdkVersion); |
| 58 |
\curl_setopt($curlHandle, \CURLOPT_TIMEOUT_MS, $options->getHttpTimeout() * 1000); |
| 59 |
\curl_setopt($curlHandle, \CURLOPT_CONNECTTIMEOUT_MS, $options->getHttpConnectTimeout() * 1000); |
| 60 |
\curl_setopt($curlHandle, \CURLOPT_ENCODING, ''); |
| 61 |
\curl_setopt($curlHandle, \CURLOPT_POST, \true); |
| 62 |
\curl_setopt($curlHandle, \CURLOPT_POSTFIELDS, $requestData); |
| 63 |
\curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, \true); |
| 64 |
\curl_setopt($curlHandle, \CURLOPT_HEADERFUNCTION, $responseHeaderCallback); |
| 65 |
\curl_setopt($curlHandle, \CURLOPT_HTTP_VERSION, \CURL_HTTP_VERSION_1_1); |
| 66 |
if ($options->isShareHandleEnabled()) { |
| 67 |
$shareHandle = $this->getShareHandle(); |
| 68 |
if ($shareHandle !== null) { |
| 69 |
\curl_setopt($curlHandle, \CURLOPT_SHARE, $shareHandle); |
| 70 |
} |
| 71 |
} |
| 72 |
$httpSslVerifyPeer = $options->getHttpSslVerifyPeer(); |
| 73 |
if (!$httpSslVerifyPeer) { |
| 74 |
\curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, \false); |
| 75 |
} |
| 76 |
$httpSslNativeCa = $options->getHttpSslNativeCa(); |
| 77 |
if ($httpSslNativeCa) { |
| 78 |
if (\defined('WCPOS\\Vendor\\CURLSSLOPT_NATIVE_CA') && isset(\curl_version()['version']) && \version_compare(\curl_version()['version'], '7.71', '>=')) { |
| 79 |
\curl_setopt($curlHandle, \CURLOPT_SSL_OPTIONS, \WCPOS\Vendor\CURLSSLOPT_NATIVE_CA); |
| 80 |
} |
| 81 |
} |
| 82 |
$httpProxy = $options->getHttpProxy(); |
| 83 |
if ($httpProxy !== null) { |
| 84 |
\curl_setopt($curlHandle, \CURLOPT_PROXY, $httpProxy); |
| 85 |
\curl_setopt($curlHandle, \CURLOPT_HEADEROPT, \CURLHEADER_SEPARATE); |
| 86 |
} |
| 87 |
$httpProxyAuthentication = $options->getHttpProxyAuthentication(); |
| 88 |
if ($httpProxyAuthentication !== null) { |
| 89 |
\curl_setopt($curlHandle, \CURLOPT_PROXYUSERPWD, $httpProxyAuthentication); |
| 90 |
} |
| 91 |
/** @var string|false $body */ |
| 92 |
$body = \curl_exec($curlHandle); |
| 93 |
if ($body === \false) { |
| 94 |
$errorCode = \curl_errno($curlHandle); |
| 95 |
$error = \curl_error($curlHandle); |
| 96 |
if (\PHP_MAJOR_VERSION < 8) { |
| 97 |
\curl_close($curlHandle); |
| 98 |
} |
| 99 |
$message = 'cURL Error (' . $errorCode . ') ' . $error; |
| 100 |
return new Response(0, [], $message); |
| 101 |
} |
| 102 |
$statusCode = \curl_getinfo($curlHandle, \CURLINFO_HTTP_CODE); |
| 103 |
if (\PHP_MAJOR_VERSION < 8) { |
| 104 |
\curl_close($curlHandle); |
| 105 |
} |
| 106 |
$error = $statusCode >= 400 ? $body : ''; |
| 107 |
return new Response($statusCode, $responseHeaders, $error); |
| 108 |
} |
| 109 |
/** |
| 110 |
* Initializes a share handle for CURL requests. If available, it will always try to use a persistent |
| 111 |
* share handle first and fall back to a regular share handle in case it's unavailable. |
| 112 |
* |
| 113 |
* @return object|resource|null a share handle or null if none could be created |
| 114 |
*/ |
| 115 |
private function getShareHandle() |
| 116 |
{ |
| 117 |
if ($this->shareHandle !== null) { |
| 118 |
return $this->shareHandle; |
| 119 |
} |
| 120 |
if (\function_exists('WCPOS\\Vendor\\curl_share_init_persistent')) { |
| 121 |
$shareOptions = [\CURL_LOCK_DATA_DNS]; |
| 122 |
if (\defined('CURL_LOCK_DATA_CONNECT')) { |
| 123 |
$shareOptions[] = \CURL_LOCK_DATA_CONNECT; |
| 124 |
} |
| 125 |
if (\defined('CURL_LOCK_DATA_SSL_SESSION')) { |
| 126 |
$shareOptions[] = \CURL_LOCK_DATA_SSL_SESSION; |
| 127 |
} |
| 128 |
try { |
| 129 |
$this->shareHandle = curl_share_init_persistent($shareOptions); |
| 130 |
} catch (\Throwable $throwable) { |
| 131 |
// don't crash if the share handle cannot be created |
| 132 |
$this->shareHandle = null; |
| 133 |
} |
| 134 |
} |
| 135 |
// If the persistent share handle cannot be created or doesn't exist |
| 136 |
if ($this->shareHandle === null) { |
| 137 |
try { |
| 138 |
$this->shareHandle = \curl_share_init(); |
| 139 |
\curl_share_setopt($this->shareHandle, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_DNS); |
| 140 |
if (\defined('CURL_LOCK_DATA_CONNECT')) { |
| 141 |
\curl_share_setopt($this->shareHandle, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_CONNECT); |
| 142 |
} |
| 143 |
if (\defined('CURL_LOCK_DATA_SSL_SESSION')) { |
| 144 |
\curl_share_setopt($this->shareHandle, \CURLSHOPT_SHARE, \CURL_LOCK_DATA_SSL_SESSION); |
| 145 |
} |
| 146 |
} catch (\Throwable $throwable) { |
| 147 |
// don't crash if the share handle cannot be created |
| 148 |
$this->shareHandle = null; |
| 149 |
} |
| 150 |
} |
| 151 |
return $this->shareHandle; |
| 152 |
} |
| 153 |
} |
| 154 |
|