kodedphp /
http
| 1 | <?php |
||
| 2 | |||
| 3 | /* |
||
| 4 | * This file is part of the Koded package. |
||
| 5 | * |
||
| 6 | * (c) Mihail Binev <[email protected]> |
||
| 7 | * |
||
| 8 | * Please view the LICENSE distributed with this source code |
||
| 9 | * for the full copyright and license information. |
||
| 10 | * |
||
| 11 | */ |
||
| 12 | |||
| 13 | namespace Koded\Http\Client; |
||
| 14 | |||
| 15 | use Koded\Http\{ClientRequest, ServerResponse}; |
||
| 16 | use Koded\Http\Interfaces\{HttpRequestClient, HttpStatus, Response}; |
||
| 17 | use Psr\Http\Message\UriInterface; |
||
| 18 | use function Koded\Http\create_stream; |
||
| 19 | use function Koded\Stdlib\json_serialize; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @link http://php.net/manual/en/context.curl.php |
||
| 23 | */ |
||
| 24 | class CurlClient extends ClientRequest implements HttpRequestClient |
||
| 25 | { |
||
| 26 | use EncodingTrait, Psr18ClientTrait; |
||
| 27 | |||
| 28 | private array $options = [ |
||
| 29 | CURLOPT_MAXREDIRS => 20, |
||
| 30 | CURLOPT_RETURNTRANSFER => true, |
||
| 31 | CURLOPT_FOLLOWLOCATION => true, |
||
| 32 | CURLOPT_SSL_VERIFYPEER => 1, |
||
| 33 | CURLOPT_SSL_VERIFYHOST => 2, |
||
| 34 | CURLOPT_USERAGENT => self::USER_AGENT, |
||
| 35 | CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, |
||
| 36 | CURLOPT_FAILONERROR => 0, |
||
| 37 | ]; |
||
| 38 | |||
| 39 | private array $responseHeaders = []; |
||
| 40 | |||
| 41 | public function __construct( |
||
| 42 | string $method, |
||
| 43 | string|UriInterface $uri, |
||
| 44 | string|iterable $body = null, |
||
| 45 | array $headers = []) |
||
| 46 | { |
||
| 47 | parent::__construct($method, $uri, $body, $headers); |
||
| 48 | $this->options[CURLOPT_TIMEOUT] = (\ini_get('default_socket_timeout') ?: 10.0) * 1.0; |
||
| 49 | } |
||
| 50 | |||
| 51 | public function read(): Response |
||
| 52 | { |
||
| 53 | if ($resource = $this->assertSafeMethod()) { |
||
| 54 | return $resource; |
||
| 55 | } |
||
| 56 | $this->prepareRequestBody(); |
||
| 57 | $this->prepareOptions(); |
||
| 58 | try { |
||
| 59 | if (false === $resource = $this->createResource()) { |
||
|
0 ignored issues
–
show
introduced
by
Loading history...
|
|||
| 60 | return $this->getPhpError(HttpStatus::FAILED_DEPENDENCY, |
||
| 61 | 'The HTTP client is not created therefore cannot read anything'); |
||
| 62 | } |
||
| 63 | \curl_setopt_array($resource, $this->options); |
||
| 64 | $response = \curl_exec($resource); |
||
| 65 | if ($this->hasError($resource)) { |
||
| 66 | return $this->getCurlError(HttpStatus::FAILED_DEPENDENCY, $resource); |
||
| 67 | } |
||
| 68 | return new ServerResponse( |
||
| 69 | $response, |
||
| 70 | \curl_getinfo($resource, CURLINFO_RESPONSE_CODE), |
||
| 71 | $this->responseHeaders); |
||
| 72 | } catch (\TypeError $e) { |
||
| 73 | return $this->getPhpError(HttpStatus::FAILED_DEPENDENCY, |
||
| 74 | $e->getMessage()); |
||
| 75 | } catch (\Throwable $e) { |
||
| 76 | return $this->getPhpError(HttpStatus::INTERNAL_SERVER_ERROR, $e->getMessage()); |
||
| 77 | } finally { |
||
| 78 | unset($response); |
||
| 79 | } |
||
| 80 | } |
||
| 81 | |||
| 82 | public function userAgent(string $value): HttpRequestClient |
||
| 83 | { |
||
| 84 | $this->options[CURLOPT_USERAGENT] = $value; |
||
| 85 | return $this; |
||
| 86 | } |
||
| 87 | |||
| 88 | public function followLocation(bool $value): HttpRequestClient |
||
| 89 | { |
||
| 90 | $this->options[CURLOPT_FOLLOWLOCATION] = $value; |
||
| 91 | return $this; |
||
| 92 | } |
||
| 93 | |||
| 94 | public function maxRedirects(int $value): HttpRequestClient |
||
| 95 | { |
||
| 96 | $this->options[CURLOPT_MAXREDIRS] = $value; |
||
| 97 | return $this; |
||
| 98 | } |
||
| 99 | |||
| 100 | public function timeout(float $value): HttpRequestClient |
||
| 101 | { |
||
| 102 | $this->options[CURLOPT_TIMEOUT] = $value; |
||
| 103 | return $this; |
||
| 104 | } |
||
| 105 | |||
| 106 | public function ignoreErrors(bool $value): HttpRequestClient |
||
| 107 | { |
||
| 108 | // false = do not fail on error |
||
| 109 | $this->options[CURLOPT_FAILONERROR] = (int)!$value; |
||
| 110 | return $this; |
||
| 111 | } |
||
| 112 | |||
| 113 | public function verifySslHost(bool $value): HttpRequestClient |
||
| 114 | { |
||
| 115 | $this->options[CURLOPT_SSL_VERIFYHOST] = $value ? 2 : 0; |
||
| 116 | return $this; |
||
| 117 | } |
||
| 118 | |||
| 119 | public function verifySslPeer(bool $value): HttpRequestClient |
||
| 120 | { |
||
| 121 | $this->options[CURLOPT_SSL_VERIFYPEER] = $value ? 1 : 0; |
||
| 122 | return $this; |
||
| 123 | } |
||
| 124 | |||
| 125 | public function withProtocolVersion($version): static |
||
| 126 | { |
||
| 127 | $instance = parent::withProtocolVersion($version); |
||
| 128 | $instance->options[CURLOPT_HTTP_VERSION] = |
||
| 129 | ['1.1' => CURL_HTTP_VERSION_1_1, |
||
| 130 | '1.0' => CURL_HTTP_VERSION_1_0][$version]; |
||
| 131 | return $instance; |
||
| 132 | } |
||
| 133 | |||
| 134 | protected function createResource(): \CurlHandle|bool |
||
| 135 | { |
||
| 136 | return \curl_init((string)$this->getUri()); |
||
|
0 ignored issues
–
show
|
|||
| 137 | } |
||
| 138 | |||
| 139 | protected function hasError($resource): bool |
||
| 140 | { |
||
| 141 | return \curl_errno($resource) > 0; |
||
| 142 | } |
||
| 143 | |||
| 144 | protected function prepareOptions(): void |
||
| 145 | { |
||
| 146 | $this->options[CURLOPT_HEADERFUNCTION] = [$this, 'extractFromResponseHeaders']; |
||
| 147 | $this->options[CURLOPT_CUSTOMREQUEST] = $this->getMethod(); |
||
| 148 | $this->options[CURLOPT_HTTPHEADER] = $this->getFlattenedHeaders(); |
||
| 149 | unset($this->options[CURLOPT_HTTPHEADER][0]); // Host header is always present and first |
||
| 150 | } |
||
| 151 | |||
| 152 | protected function prepareRequestBody(): void |
||
| 153 | { |
||
| 154 | if (!$this->stream->getSize()) { |
||
| 155 | return; |
||
| 156 | } |
||
| 157 | $this->stream->rewind(); |
||
| 158 | if (0 === $this->encoding) { |
||
| 159 | $this->options[CURLOPT_POSTFIELDS] = $this->stream->getContents(); |
||
| 160 | } elseif ($content = \json_decode($this->stream->getContents() ?: '[]', true)) { |
||
| 161 | $this->normalizeHeader('Content-type', self::X_WWW_FORM_URLENCODED, true); |
||
| 162 | $this->options[CURLOPT_POSTFIELDS] = \http_build_query($content, null, '&', $this->encoding); |
||
| 163 | } |
||
| 164 | $this->stream = create_stream($this->options[CURLOPT_POSTFIELDS]); |
||
| 165 | } |
||
| 166 | |||
| 167 | protected function getCurlError(int $status, $resource): Response |
||
| 168 | { |
||
| 169 | //see https://tools.ietf.org/html/rfc7807 |
||
| 170 | return new ServerResponse(json_serialize([ |
||
| 171 | 'title' => \curl_error($resource), |
||
| 172 | 'detail' => \curl_strerror(\curl_errno($resource)), |
||
| 173 | 'instance' => \curl_getinfo($resource, CURLINFO_EFFECTIVE_URL), |
||
| 174 | 'type' => 'https://httpstatuses.com/' . $status, |
||
| 175 | 'status' => $status, |
||
| 176 | ]), $status, ['Content-type' => 'application/problem+json']); |
||
| 177 | } |
||
| 178 | |||
| 179 | /** |
||
| 180 | * Extracts the headers from curl response. |
||
| 181 | * |
||
| 182 | * @param resource $_ curl instance |
||
| 183 | * @param string $header Current header line |
||
| 184 | * |
||
| 185 | * @return int Header length |
||
| 186 | */ |
||
| 187 | protected function extractFromResponseHeaders($_, string $header): int |
||
| 188 | { |
||
| 189 | try { |
||
| 190 | [$k, $v] = \explode(':', $header, 2) + [1 => null]; |
||
| 191 | null === $v || $this->responseHeaders[$k] = $v; |
||
| 192 | } catch (\Throwable) { |
||
| 193 | /** NOOP **/ |
||
| 194 | } finally { |
||
| 195 | return \mb_strlen($header); |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } |
||
| 199 |