diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:44:51 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-28 12:44:51 +0000 |
commit | a1ec78bf0dc93d0e05e5f066f1949dc3baecea06 (patch) | |
tree | ee596ce1bc9840661386f96f9b8d1f919a106317 /vendor/gipfl/curl/src/CurlHandle.php | |
parent | Initial commit. (diff) | |
download | icingaweb2-module-incubator-upstream.tar.xz icingaweb2-module-incubator-upstream.zip |
Adding upstream version 0.20.0.upstream/0.20.0upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gipfl/curl/src/CurlHandle.php')
-rw-r--r-- | vendor/gipfl/curl/src/CurlHandle.php | 76 |
1 files changed, 76 insertions, 0 deletions
diff --git a/vendor/gipfl/curl/src/CurlHandle.php b/vendor/gipfl/curl/src/CurlHandle.php new file mode 100644 index 0000000..d5309f0 --- /dev/null +++ b/vendor/gipfl/curl/src/CurlHandle.php @@ -0,0 +1,76 @@ +<?php + +namespace gipfl\Curl; + +use Psr\Http\Message\RequestInterface; + +class CurlHandle +{ + protected static $curlOptions = [ + CURLOPT_HEADER => true, + CURLOPT_RETURNTRANSFER => true, + CURLOPT_CONNECTTIMEOUT => 5, + CURLOPT_SSL_VERIFYPEER => true, + CURLOPT_SSL_VERIFYHOST => 2, + CURLOPT_ENCODING => 'gzip', + CURLOPT_TCP_NODELAY => true, + CURLINFO_HEADER_OUT => true, + CURLOPT_TCP_KEEPALIVE => 1, + CURLOPT_BUFFERSIZE => 512 * 1024, + ]; + + public static function createForRequest(RequestInterface $request, $curlOptions = []) + { + $headers = []; + foreach ($request->getHeaders() as $name => $values) { + foreach ($values as $value) { + $headers[] = "$name: $value"; + } + } + $body = $request->getBody(); + if ($body->getSize() > 0) { + $body = $body->getContents(); + } else { + $body = null; + } + + + $curl = curl_init(); + $opts = static::prepareCurlOptions( + $request->getMethod(), + (string) $request->getUri(), + $body, + $headers, + $curlOptions + ); + curl_setopt_array($curl, $opts); + + return $curl; + } + + protected static function prepareCurlOptions($method, $url, $body = null, $headers = [], $curlOptions = []) + { + $opts = $curlOptions + [ + CURLOPT_CUSTOMREQUEST => $method, + CURLOPT_URL => $url, + ] + self::$curlOptions; + + if (isset($opts[CURLOPT_HTTPHEADER])) { + $opts[CURLOPT_HTTPHEADER] = array_merge($opts[CURLOPT_HTTPHEADER], $headers); + } else { + $opts[CURLOPT_HTTPHEADER] = $headers; + } + if (isset($opts[CURLOPT_PROXYTYPE]) + && $opts[CURLOPT_PROXYTYPE] === CURLPROXY_HTTP + && defined('CURLOPT_SUPPRESS_CONNECT_HEADERS') + ) { + $opts[CURLOPT_SUPPRESS_CONNECT_HEADERS] = true; + } + + if ($body !== null) { + $opts[CURLOPT_POSTFIELDS] = $body; + } + + return $opts; + } +} |