|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Utopia\Fetch\Adapter; |
| 6 | + |
| 7 | +use CurlHandle; |
| 8 | +use Utopia\Fetch\Adapter; |
| 9 | +use Utopia\Fetch\Chunk; |
| 10 | +use Utopia\Fetch\Exception; |
| 11 | +use Utopia\Fetch\Options\Curl as CurlOptions; |
| 12 | +use Utopia\Fetch\Options\Request as RequestOptions; |
| 13 | +use Utopia\Fetch\Response; |
| 14 | + |
| 15 | +/** |
| 16 | + * Curl Adapter |
| 17 | + * HTTP adapter using PHP's cURL extension |
| 18 | + * @package Utopia\Fetch\Adapter |
| 19 | + */ |
| 20 | +class Curl implements Adapter |
| 21 | +{ |
| 22 | + private ?CurlHandle $handle = null; |
| 23 | + |
| 24 | + /** |
| 25 | + * @var array<int, mixed> |
| 26 | + */ |
| 27 | + private array $config = []; |
| 28 | + |
| 29 | + /** |
| 30 | + * Create a new Curl adapter |
| 31 | + * |
| 32 | + * @param CurlOptions|null $options Curl adapter options |
| 33 | + */ |
| 34 | + public function __construct(?CurlOptions $options = null) |
| 35 | + { |
| 36 | + $options ??= new CurlOptions(); |
| 37 | + |
| 38 | + $this->config[CURLOPT_SSL_VERIFYPEER] = $options->getSslVerifyPeer(); |
| 39 | + $this->config[CURLOPT_SSL_VERIFYHOST] = $options->getSslVerifyHost() ? 2 : 0; |
| 40 | + |
| 41 | + if ($options->getSslCertificate() !== null) { |
| 42 | + $this->config[CURLOPT_SSLCERT] = $options->getSslCertificate(); |
| 43 | + } |
| 44 | + |
| 45 | + if ($options->getSslKey() !== null) { |
| 46 | + $this->config[CURLOPT_SSLKEY] = $options->getSslKey(); |
| 47 | + } |
| 48 | + |
| 49 | + if ($options->getCaInfo() !== null) { |
| 50 | + $this->config[CURLOPT_CAINFO] = $options->getCaInfo(); |
| 51 | + } |
| 52 | + |
| 53 | + if ($options->getCaPath() !== null) { |
| 54 | + $this->config[CURLOPT_CAPATH] = $options->getCaPath(); |
| 55 | + } |
| 56 | + |
| 57 | + if ($options->getProxy() !== null) { |
| 58 | + $this->config[CURLOPT_PROXY] = $options->getProxy(); |
| 59 | + $this->config[CURLOPT_PROXYTYPE] = $options->getProxyType(); |
| 60 | + |
| 61 | + if ($options->getProxyUserPwd() !== null) { |
| 62 | + $this->config[CURLOPT_PROXYUSERPWD] = $options->getProxyUserPwd(); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + $this->config[CURLOPT_HTTP_VERSION] = $options->getHttpVersion(); |
| 67 | + $this->config[CURLOPT_TCP_KEEPALIVE] = $options->getTcpKeepAlive() ? 1 : 0; |
| 68 | + $this->config[CURLOPT_TCP_KEEPIDLE] = $options->getTcpKeepIdle(); |
| 69 | + $this->config[CURLOPT_TCP_KEEPINTVL] = $options->getTcpKeepInterval(); |
| 70 | + $this->config[CURLOPT_BUFFERSIZE] = $options->getBufferSize(); |
| 71 | + $this->config[CURLOPT_VERBOSE] = $options->getVerbose(); |
| 72 | + } |
| 73 | + |
| 74 | + /** |
| 75 | + * Get or create the cURL handle |
| 76 | + * |
| 77 | + * @return CurlHandle |
| 78 | + * @throws Exception If cURL initialization fails |
| 79 | + */ |
| 80 | + private function getHandle(): CurlHandle |
| 81 | + { |
| 82 | + if ($this->handle === null) { |
| 83 | + $handle = curl_init(); |
| 84 | + if ($handle === false) { |
| 85 | + throw new Exception('Failed to initialize cURL handle'); |
| 86 | + } |
| 87 | + $this->handle = $handle; |
| 88 | + } else { |
| 89 | + curl_reset($this->handle); |
| 90 | + } |
| 91 | + |
| 92 | + return $this->handle; |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * Send an HTTP request using cURL |
| 97 | + * |
| 98 | + * @param string $url The URL to send the request to |
| 99 | + * @param string $method The HTTP method (GET, POST, etc.) |
| 100 | + * @param mixed $body The request body (string, array, or null) |
| 101 | + * @param array<string, string> $headers The request headers (formatted as key-value pairs) |
| 102 | + * @param RequestOptions $options Request options (timeout, connectTimeout, maxRedirects, allowRedirects, userAgent) |
| 103 | + * @param callable|null $chunkCallback Optional callback for streaming chunks |
| 104 | + * @return Response The HTTP response |
| 105 | + * @throws Exception If the request fails |
| 106 | + */ |
| 107 | + public function send( |
| 108 | + string $url, |
| 109 | + string $method, |
| 110 | + mixed $body, |
| 111 | + array $headers, |
| 112 | + RequestOptions $options, |
| 113 | + ?callable $chunkCallback = null |
| 114 | + ): Response { |
| 115 | + $formattedHeaders = array_map(function ($key, $value) { |
| 116 | + return $key . ':' . $value; |
| 117 | + }, array_keys($headers), $headers); |
| 118 | + |
| 119 | + $responseHeaders = []; |
| 120 | + $responseBody = ''; |
| 121 | + $chunkIndex = 0; |
| 122 | + |
| 123 | + $ch = $this->getHandle(); |
| 124 | + $curlOptions = [ |
| 125 | + CURLOPT_URL => $url, |
| 126 | + CURLOPT_HTTPHEADER => $formattedHeaders, |
| 127 | + CURLOPT_CUSTOMREQUEST => $method, |
| 128 | + CURLOPT_HEADERFUNCTION => function ($curl, $header) use (&$responseHeaders) { |
| 129 | + $len = strlen($header); |
| 130 | + $header = explode(':', $header, 2); |
| 131 | + if (count($header) < 2) { |
| 132 | + return $len; |
| 133 | + } |
| 134 | + $responseHeaders[strtolower(trim($header[0]))] = trim($header[1]); |
| 135 | + return $len; |
| 136 | + }, |
| 137 | + CURLOPT_WRITEFUNCTION => function ($ch, $data) use ($chunkCallback, &$responseBody, &$chunkIndex) { |
| 138 | + if ($chunkCallback !== null) { |
| 139 | + $chunk = new Chunk( |
| 140 | + data: $data, |
| 141 | + size: strlen($data), |
| 142 | + timestamp: microtime(true), |
| 143 | + index: $chunkIndex++ |
| 144 | + ); |
| 145 | + $chunkCallback($chunk); |
| 146 | + } else { |
| 147 | + $responseBody .= $data; |
| 148 | + } |
| 149 | + return strlen($data); |
| 150 | + }, |
| 151 | + CURLOPT_CONNECTTIMEOUT_MS => $options->getConnectTimeout(), |
| 152 | + CURLOPT_TIMEOUT_MS => $options->getTimeout(), |
| 153 | + CURLOPT_MAXREDIRS => $options->getMaxRedirects(), |
| 154 | + CURLOPT_FOLLOWLOCATION => $options->getAllowRedirects(), |
| 155 | + CURLOPT_USERAGENT => $options->getUserAgent() |
| 156 | + ]; |
| 157 | + |
| 158 | + if ($body !== null && $body !== [] && $body !== '') { |
| 159 | + $curlOptions[CURLOPT_POSTFIELDS] = $body; |
| 160 | + } |
| 161 | + |
| 162 | + // Merge adapter config (adapter config takes precedence) |
| 163 | + $curlOptions = $this->config + $curlOptions; |
| 164 | + |
| 165 | + foreach ($curlOptions as $option => $value) { |
| 166 | + curl_setopt($ch, $option, $value); |
| 167 | + } |
| 168 | + |
| 169 | + $success = curl_exec($ch); |
| 170 | + if ($success === false) { |
| 171 | + $errorMsg = curl_error($ch); |
| 172 | + throw new Exception($errorMsg); |
| 173 | + } |
| 174 | + |
| 175 | + $responseStatusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); |
| 176 | + |
| 177 | + return new Response( |
| 178 | + statusCode: $responseStatusCode, |
| 179 | + headers: $responseHeaders, |
| 180 | + body: $responseBody |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Close the cURL handle when the adapter is destroyed |
| 186 | + */ |
| 187 | + public function __destruct() |
| 188 | + { |
| 189 | + if ($this->handle !== null) { |
| 190 | + curl_close($this->handle); |
| 191 | + $this->handle = null; |
| 192 | + } |
| 193 | + } |
| 194 | +} |
0 commit comments