From 535dbda6b48171f8115b455e54790de94d97d42a Mon Sep 17 00:00:00 2001 From: Murilo Ijanc Date: Sun, 26 Jul 2026 16:41:48 -0300 Subject: Initial import --- lib/cepis/CurlTransport.php | 85 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 lib/cepis/CurlTransport.php (limited to 'lib/cepis/CurlTransport.php') diff --git a/lib/cepis/CurlTransport.php b/lib/cepis/CurlTransport.php new file mode 100644 index 0000000..d22949b --- /dev/null +++ b/lib/cepis/CurlTransport.php @@ -0,0 +1,85 @@ + $value) { + $lines[] = $name . ': ' . $value; + } + + $milliseconds = max(1, (int) round($timeout * 1000)); + $received = []; + + curl_setopt_array($handle, [ + CURLOPT_URL => $url, + CURLOPT_HTTPGET => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_HTTPHEADER => $lines, + CURLOPT_CONNECTTIMEOUT_MS => $milliseconds, + CURLOPT_TIMEOUT_MS => $milliseconds, + CURLOPT_FOLLOWLOCATION => $this->maxRedirects > 0, + CURLOPT_MAXREDIRS => $this->maxRedirects, + CURLOPT_SSL_VERIFYPEER => $this->verifyTls, + CURLOPT_SSL_VERIFYHOST => $this->verifyTls ? 2 : 0, + CURLOPT_NOSIGNAL => true, + CURLOPT_HEADERFUNCTION => static function ($_handle, string $line) use (&$received): int { + $length = strlen($line); + $parts = explode(':', $line, 2); + if (count($parts) === 2) { + $received[trim($parts[0])][] = trim($parts[1]); + } + + return $length; + }, + ]); + + $body = curl_exec($handle); + $error = curl_error($handle); + $status = (int) curl_getinfo($handle, CURLINFO_RESPONSE_CODE); + unset($handle); + + if ($body === false || $status === 0) { + throw new NetworkException(sprintf( + 'could not reach %s: %s', + $url, + $error !== '' ? $error : 'the request did not complete', + )); + } + + return new Response($status, $received, (string) $body); + } +} -- cgit v1.3