1 | <?php |
||
20 | class Curl extends AbstractTransport |
||
21 | { |
||
22 | /** |
||
23 | * cURL error map. |
||
24 | * |
||
25 | * @var array |
||
26 | */ |
||
27 | protected static $errorCategoryMap = [ |
||
28 | CURLE_FTP_WEIRD_SERVER_REPLY => 'FTP', |
||
29 | CURLE_FTP_ACCESS_DENIED => 'FTP', |
||
30 | CURLE_FTP_USER_PASSWORD_INCORRECT => 'FTP', |
||
31 | CURLE_FTP_WEIRD_PASS_REPLY => 'FTP', |
||
32 | CURLE_FTP_WEIRD_USER_REPLY => 'FTP', |
||
33 | CURLE_FTP_WEIRD_PASV_REPLY => 'FTP', |
||
34 | CURLE_FTP_WEIRD_227_FORMAT => 'FTP', |
||
35 | CURLE_FTP_CANT_GET_HOST => 'FTP', |
||
36 | CURLE_FTP_CANT_RECONNECT => 'FTP', |
||
37 | CURLE_FTP_COULDNT_SET_BINARY => 'FTP', |
||
38 | CURLE_FTP_COULDNT_RETR_FILE => 'FTP', |
||
39 | CURLE_FTP_WRITE_ERROR => 'FTP', |
||
40 | CURLE_FTP_QUOTE_ERROR => 'FTP', |
||
41 | CURLE_FTP_COULDNT_STOR_FILE => 'FTP', |
||
42 | CURLE_FTP_COULDNT_SET_ASCII => 'FTP', |
||
43 | CURLE_FTP_PORT_FAILED => 'FTP', |
||
44 | CURLE_FTP_COULDNT_USE_REST => 'FTP', |
||
45 | CURLE_FTP_COULDNT_GET_SIZE => 'FTP', |
||
46 | CURLE_FTP_BAD_DOWNLOAD_RESUME => 'FTP', |
||
47 | CURLE_FTP_SSL_FAILED => 'FTP', |
||
48 | CURLE_SSL_CONNECT_ERROR => 'SSL', |
||
49 | CURLE_SSL_PEER_CERTIFICATE => 'SSL', |
||
50 | CURLE_SSL_ENGINE_NOTFOUND => 'SSL', |
||
51 | CURLE_SSL_ENGINE_SETFAILED => 'SSL', |
||
52 | CURLE_SSL_CERTPROBLEM => 'SSL', |
||
53 | CURLE_SSL_CIPHER => 'SSL', |
||
54 | CURLE_SSL_CACERT => 'SSL', |
||
55 | CURLE_LDAP_CANNOT_BIND => 'LDAP', |
||
56 | CURLE_LDAP_SEARCH_FAILED => 'LDAP', |
||
57 | CURLE_LDAP_INVALID_URL => 'LDAP', |
||
58 | CURLE_COULDNT_RESOLVE_PROXY => 'proxy', |
||
59 | ]; |
||
60 | |||
61 | /** |
||
62 | * Default options. |
||
63 | * |
||
64 | * @var array |
||
65 | */ |
||
66 | protected static $defaultOptions = [ |
||
67 | CURLOPT_VERBOSE => false, |
||
68 | CURLOPT_HTTP_VERSION => 1.1, |
||
69 | CURLOPT_USERAGENT => self::class, |
||
70 | CURLOPT_CONNECTTIMEOUT => 60, |
||
71 | CURLOPT_TIMEOUT => 60, |
||
72 | CURLOPT_CRLF => false, |
||
73 | CURLOPT_SSLVERSION => 0, |
||
74 | CURLOPT_SSL_VERIFYPEER => true, |
||
75 | CURLOPT_SSL_VERIFYHOST => 2, |
||
76 | CURLOPT_AUTOREFERER => true, |
||
77 | CURLOPT_FOLLOWLOCATION => true, |
||
78 | CURLOPT_MAXREDIRS => 10, |
||
79 | CURLOPT_UNRESTRICTED_AUTH => false, |
||
80 | CURLOPT_RETURNTRANSFER => true, |
||
81 | CURLOPT_HEADER => true, |
||
82 | CURLOPT_FORBID_REUSE => false, |
||
83 | CURLOPT_FRESH_CONNECT => false, |
||
84 | CURLOPT_MAXCONNECTS => 5, |
||
85 | ]; |
||
86 | |||
87 | /** |
||
88 | * cURL resource handler. |
||
89 | * |
||
90 | * @var resource |
||
91 | */ |
||
92 | private $handler; |
||
93 | |||
94 | /** |
||
95 | * Create cURL transport manager. |
||
96 | * |
||
97 | * @param array $options |
||
98 | * |
||
99 | * @throws \Jgut\Spiral\Exception\OptionException |
||
100 | */ |
||
101 | public function __construct(array $options = []) |
||
102 | { |
||
103 | $this->setOptions($options); |
||
104 | } |
||
105 | |||
106 | /** |
||
107 | * Create cURL transport manager with default options. |
||
108 | * |
||
109 | * @return static |
||
110 | */ |
||
111 | public static function createFromDefaults() |
||
112 | { |
||
113 | return new static(static::$defaultOptions); |
||
114 | } |
||
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | * |
||
119 | * @throws TransportException |
||
120 | */ |
||
121 | public function request($method, $uri, array $headers = [], $requestBody = null) |
||
122 | { |
||
123 | $this->resetHandler(); |
||
124 | |||
125 | $method = strtoupper($method); |
||
126 | $this->setMethod($method); |
||
127 | |||
128 | $this->forgeOptions($this->options); |
||
129 | $this->forgeHeaders($headers); |
||
130 | |||
131 | if ($requestBody !== null && $requestBody) { |
||
132 | curl_setopt($this->handler, CURLOPT_POSTFIELDS, $requestBody); |
||
133 | } |
||
134 | curl_setopt($this->handler, CURLOPT_URL, $uri); |
||
135 | |||
136 | $response = $this->execute(); |
||
137 | |||
138 | if (curl_errno($this->handler) !== CURLE_OK) { |
||
139 | $errCode = curl_errno($this->handler); |
||
140 | |||
141 | $exception = new TransportException( |
||
142 | curl_error($this->handler), |
||
143 | $errCode, |
||
144 | array_key_exists($errCode, static::$errorCategoryMap) ? static::$errorCategoryMap [$errCode] : '' |
||
145 | ); |
||
146 | |||
147 | throw $exception; |
||
148 | } |
||
149 | |||
150 | return $response; |
||
151 | } |
||
152 | |||
153 | /** |
||
154 | * Create or reuse existing handle |
||
155 | * |
||
156 | * @return resource |
||
157 | */ |
||
158 | protected function resetHandler() |
||
159 | { |
||
160 | if (is_resource($this->handler)) { |
||
161 | if ($this->hasOption(CURLOPT_FORBID_REUSE, true) |
||
162 | || $this->hasOption(CURLOPT_FRESH_CONNECT, true) |
||
163 | ) { |
||
164 | // on using CURLOPT_FRESH_CONNECT or CURLOPT_FORBID_REUSE |
||
165 | // a curl_reset() is 20-30% slower than closing and reinit |
||
166 | $this->close(); |
||
167 | $this->handler = curl_init(); |
||
168 | } else { |
||
169 | curl_reset($this->handler); |
||
170 | } |
||
171 | } else { |
||
172 | $this->handler = curl_init(); |
||
173 | } |
||
174 | return $this->handler; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * Isolate curl execution. |
||
179 | * |
||
180 | * @return string |
||
181 | */ |
||
182 | protected function execute() |
||
186 | |||
187 | /** |
||
188 | * Set HTTP method on handler. |
||
189 | * |
||
190 | * @param string $method |
||
191 | */ |
||
192 | protected function setMethod($method) |
||
212 | |||
213 | /** |
||
214 | * Set cURL options on handler. |
||
215 | * |
||
216 | * @param OptionInterface[] $options |
||
217 | */ |
||
218 | protected function forgeOptions(array $options) |
||
228 | |||
229 | /** |
||
230 | * Set HTTP headers on handler. |
||
231 | * |
||
232 | * @param array $headers |
||
233 | */ |
||
234 | protected function forgeHeaders(array $headers) |
||
244 | |||
245 | /** |
||
246 | * {@inheritdoc} |
||
247 | */ |
||
248 | public function responseInfo($option = null) |
||
260 | |||
261 | /** |
||
262 | * Always free resources on destruction. |
||
263 | */ |
||
264 | public function __destruct() |
||
268 | |||
269 | /** |
||
270 | * {@inheritdoc} |
||
271 | */ |
||
272 | public function close() |
||
280 | } |
||
281 |