-
-
Notifications
You must be signed in to change notification settings - Fork 466
Expand file tree
/
Copy pathHttpClient.php
More file actions
128 lines (102 loc) · 4.27 KB
/
HttpClient.php
File metadata and controls
128 lines (102 loc) · 4.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
<?php
declare(strict_types=1);
namespace Sentry\HttpClient;
use Sentry\Options;
use Sentry\Util\Http;
/**
* @internal
*/
class HttpClient implements HttpClientInterface
{
/**
* @var string The Sentry SDK identifier
*/
protected $sdkIdentifier;
/**
* @var string The Sentry SDK identifier
*/
protected $sdkVersion;
public function __construct(string $sdkIdentifier, string $sdkVersion)
{
$this->sdkIdentifier = $sdkIdentifier;
$this->sdkVersion = $sdkVersion;
}
public function sendRequest(Request $request, Options $options): Response
{
$dsn = $options->getDsn();
if ($dsn === null) {
throw new \RuntimeException('The DSN option must be set to use the HttpClient.');
}
$requestData = $request->getStringBody();
if ($requestData === null) {
throw new \RuntimeException('The request data is empty.');
}
if (!\extension_loaded('curl')) {
throw new \RuntimeException('The cURL PHP extension must be enabled to use the HttpClient.');
}
$curlHandle = curl_init();
$requestHeaders = Http::getRequestHeaders($dsn, $this->sdkIdentifier, $this->sdkVersion);
if (
\extension_loaded('zlib')
&& $options->isHttpCompressionEnabled()
) {
$requestData = gzcompress($requestData, -1, \ZLIB_ENCODING_GZIP);
$requestHeaders[] = 'Content-Encoding: gzip';
}
$responseHeaders = [];
$responseHeaderCallback = static function ($curlHandle, $headerLine) use (&$responseHeaders): int {
return Http::parseResponseHeaders($headerLine, $responseHeaders);
};
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_MS, $options->getHttpTimeout() * 1000);
curl_setopt($curlHandle, \CURLOPT_CONNECTTIMEOUT_MS, $options->getHttpConnectTimeout() * 1000);
curl_setopt($curlHandle, \CURLOPT_ENCODING, '');
curl_setopt($curlHandle, \CURLOPT_POST, true);
curl_setopt($curlHandle, \CURLOPT_POSTFIELDS, $requestData);
curl_setopt($curlHandle, \CURLOPT_RETURNTRANSFER, true);
curl_setopt($curlHandle, \CURLOPT_HEADERFUNCTION, $responseHeaderCallback);
curl_setopt($curlHandle, \CURLOPT_HTTP_VERSION, \CURL_HTTP_VERSION_1_1);
$httpSslVerifyPeer = $options->getHttpSslVerifyPeer();
if (!$httpSslVerifyPeer) {
curl_setopt($curlHandle, \CURLOPT_SSL_VERIFYPEER, false);
}
$httpSslNativeCa = $options->getHttpSslNativeCa();
if ($httpSslNativeCa) {
if (
\defined('CURLSSLOPT_NATIVE_CA')
&& isset(curl_version()['version'])
&& version_compare(curl_version()['version'], '7.71', '>=')
) {
curl_setopt($curlHandle, \CURLOPT_SSL_OPTIONS, \CURLSSLOPT_NATIVE_CA);
}
}
$httpProxy = $options->getHttpProxy();
if ($httpProxy !== null) {
curl_setopt($curlHandle, \CURLOPT_PROXY, $httpProxy);
curl_setopt($curlHandle, \CURLOPT_HEADEROPT, \CURLHEADER_SEPARATE);
}
$httpProxyAuthentication = $options->getHttpProxyAuthentication();
if ($httpProxyAuthentication !== null) {
curl_setopt($curlHandle, \CURLOPT_PROXYUSERPWD, $httpProxyAuthentication);
}
/** @var string|false $body */
$body = curl_exec($curlHandle);
if ($body === false) {
$errorCode = curl_errno($curlHandle);
$error = curl_error($curlHandle);
if (\PHP_MAJOR_VERSION < 8) {
curl_close($curlHandle);
}
$message = 'cURL Error (' . $errorCode . ') ' . $error;
return new Response(0, [], $message);
}
$statusCode = curl_getinfo($curlHandle, \CURLINFO_HTTP_CODE);
if (\PHP_MAJOR_VERSION < 8) {
curl_close($curlHandle);
}
$error = $statusCode >= 400 ? $body : '';
return new Response($statusCode, $responseHeaders, $error);
}
}