prepare v4.2.5
[m6w6/ext-http] / src / php_http_client_curl.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2014, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 #include "php_http_api.h"
14 #include "php_http_client.h"
15 #include "php_http_client_curl_event.h"
16 #include "php_http_client_curl_user.h"
17
18 #if PHP_HTTP_HAVE_LIBCURL
19
20 #define DEBUG_COOKIES 0
21
22 #if PHP_HTTP_HAVE_LIBCURL_OPENSSL
23 # include <openssl/ssl.h>
24 #endif
25 #if PHP_HTTP_HAVE_LIBCURL_GNUTLS
26 # include <gnutls/gnutls.h>
27 #endif
28
29 typedef struct php_http_client_curl_handler {
30 CURL *handle;
31 php_resource_factory_t *rf;
32 php_http_client_t *client;
33 php_http_client_progress_state_t progress;
34 php_http_client_enqueue_t queue;
35
36 struct {
37 php_http_buffer_t headers;
38 php_http_message_body_t *body;
39 } response;
40
41 struct {
42 HashTable cache;
43
44 struct curl_slist *proxyheaders;
45 struct curl_slist *headers;
46 struct curl_slist *resolve;
47 php_http_buffer_t cookies;
48 php_http_buffer_t ranges;
49
50 struct {
51 uint32_t count;
52 double delay;
53 } retry;
54
55 long redirects;
56 unsigned range_request:1;
57 unsigned encode_cookies:1;
58
59 } options;
60
61 } php_http_client_curl_handler_t;
62
63 typedef struct php_http_curle_storage {
64 char *url;
65 char *cookiestore;
66 CURLcode errorcode;
67 char errorbuffer[0x100];
68 } php_http_curle_storage_t;
69
70 static inline php_http_curle_storage_t *php_http_curle_get_storage(CURL *ch) {
71 php_http_curle_storage_t *st = NULL;
72
73 curl_easy_getinfo(ch, CURLINFO_PRIVATE, &st);
74
75 if (!st) {
76 st = pecalloc(1, sizeof(*st), 1);
77 curl_easy_setopt(ch, CURLOPT_PRIVATE, st);
78 curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, st->errorbuffer);
79 }
80
81 return st;
82 }
83
84 static void *php_http_curle_ctor(void *opaque, void *init_arg)
85 {
86 void *ch;
87
88 if ((ch = curl_easy_init())) {
89 php_http_curle_get_storage(ch);
90 return ch;
91 }
92 return NULL;
93 }
94
95 static void *php_http_curle_copy(void *opaque, void *handle)
96 {
97 void *ch;
98
99 if ((ch = curl_easy_duphandle(handle))) {
100 curl_easy_reset(ch);
101 php_http_curle_get_storage(ch);
102 return ch;
103 }
104 return NULL;
105 }
106
107 static void php_http_curle_dtor(void *opaque, void *handle)
108 {
109 php_http_curle_storage_t *st = php_http_curle_get_storage(handle);
110
111 curl_easy_cleanup(handle);
112
113 if (st) {
114 if (st->url) {
115 pefree(st->url, 1);
116 }
117 if (st->cookiestore) {
118 pefree(st->cookiestore, 1);
119 }
120 pefree(st, 1);
121 }
122 }
123
124 static php_resource_factory_ops_t php_http_curle_resource_factory_ops = {
125 php_http_curle_ctor,
126 php_http_curle_copy,
127 php_http_curle_dtor
128 };
129
130 static void *php_http_curlm_ctor(void *opaque, void *init_arg)
131 {
132 php_http_client_curl_handle_t *curl = calloc(1, sizeof(*curl));
133
134 if (!(curl->multi = curl_multi_init())) {
135 free(curl);
136 return NULL;
137 }
138 if (!(curl->share = curl_share_init())) {
139 curl_multi_cleanup(curl->multi);
140 free(curl);
141 return NULL;
142 }
143 curl_share_setopt(curl->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
144 curl_share_setopt(curl->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
145 return curl;
146 }
147
148 static void php_http_curlm_dtor(void *opaque, void *handle)
149 {
150 php_http_client_curl_handle_t *curl = handle;
151
152 curl_share_cleanup(curl->share);
153 curl_multi_cleanup(curl->multi);
154 free(handle);
155 }
156
157 static php_resource_factory_ops_t php_http_curlm_resource_factory_ops = {
158 php_http_curlm_ctor,
159 NULL,
160 php_http_curlm_dtor
161 };
162
163 /* curl callbacks */
164
165 static size_t php_http_curle_read_callback(void *data, size_t len, size_t n, void *ctx)
166 {
167 php_stream *s = php_http_message_body_stream(ctx);
168
169 if (s) {
170 return php_stream_read(s, data, len * n);
171 }
172 return 0;
173 }
174
175 #if PHP_HTTP_CURL_VERSION(7,32,0)
176 static int php_http_curle_xferinfo_callback(void *ctx, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
177 #else
178 static int php_http_curle_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow)
179 #endif
180 {
181 php_http_client_curl_handler_t *h = ctx;
182
183 if (h->progress.dl.total != dltotal
184 || h->progress.dl.now != dlnow
185 || h->progress.ul.total != ultotal
186 || h->progress.ul.now != ulnow
187 ) {
188 h->progress.dl.total = dltotal;
189 h->progress.dl.now = dlnow;
190 h->progress.ul.total = ultotal;
191 h->progress.ul.now = ulnow;
192 }
193
194 if (h->client->callback.progress.func) {
195 h->client->callback.progress.func(h->client->callback.progress.arg, h->client, &h->queue, &h->progress);
196 }
197
198 return 0;
199 }
200
201 static int php_http_curle_seek_callback(void *userdata, curl_off_t offset, int origin)
202 {
203 php_http_message_body_t *body = userdata;
204
205 if (!body) {
206 return 1;
207 }
208 if (0 == php_stream_seek(php_http_message_body_stream(body), offset, origin)) {
209 return 0;
210 }
211 return 2;
212 }
213
214 static int php_http_curle_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
215 {
216 php_http_client_curl_handler_t *h = ctx;
217 unsigned utype = PHP_HTTP_CLIENT_DEBUG_INFO;
218
219 /* catch progress */
220 switch (type) {
221 case CURLINFO_TEXT:
222 if (data[0] == '-') {
223 goto text;
224 } else if (php_memnstr(data, ZEND_STRL("Adding handle:"), data + length)) {
225 h->progress.info = "setup";
226 } else if (php_memnstr(data, ZEND_STRL("addHandle"), data + length)) {
227 h->progress.info = "setup";
228 } else if (php_memnstr(data, ZEND_STRL("About to connect"), data + length)) {
229 h->progress.info = "resolve";
230 } else if (php_memnstr(data, ZEND_STRL("Trying"), data + length)) {
231 h->progress.info = "connect";
232 } else if (php_memnstr(data, ZEND_STRL("Found bundle for host"), data + length)) {
233 h->progress.info = "connect";
234 } else if (php_memnstr(data, ZEND_STRL("Connected"), data + length)) {
235 h->progress.info = "connected";
236 } else if (php_memnstr(data, ZEND_STRL("Re-using existing connection!"), data + length)) {
237 h->progress.info = "connected";
238 } else if (php_memnstr(data, ZEND_STRL("blacklisted"), data + length)) {
239 h->progress.info = "blacklist check";
240 } else if (php_memnstr(data, ZEND_STRL("TLS"), data + length)) {
241 h->progress.info = "ssl negotiation";
242 } else if (php_memnstr(data, ZEND_STRL("SSL"), data + length)) {
243 h->progress.info = "ssl negotiation";
244 } else if (php_memnstr(data, ZEND_STRL("certificate"), data + length)) {
245 h->progress.info = "ssl negotiation";
246 } else if (php_memnstr(data, ZEND_STRL("ALPN"), data + length)) {
247 h->progress.info = "alpn";
248 } else if (php_memnstr(data, ZEND_STRL("NPN"), data + length)) {
249 h->progress.info = "npn";
250 } else if (php_memnstr(data, ZEND_STRL("upload"), data + length)) {
251 h->progress.info = "uploaded";
252 } else if (php_memnstr(data, ZEND_STRL("left intact"), data + length)) {
253 h->progress.info = "not disconnected";
254 } else if (php_memnstr(data, ZEND_STRL("closed"), data + length)) {
255 h->progress.info = "disconnected";
256 } else if (php_memnstr(data, ZEND_STRL("Issue another request"), data + length)) {
257 h->progress.info = "redirect";
258 } else if (php_memnstr(data, ZEND_STRL("Operation timed out"), data + length)) {
259 h->progress.info = "timeout";
260 } else {
261 text:;
262 #if 0
263 h->progress.info = data;
264 data[length - 1] = '\0';
265 #endif
266 }
267 if (h->client->callback.progress.func) {
268 h->client->callback.progress.func(h->client->callback.progress.arg, h->client, &h->queue, &h->progress);
269 }
270 break;
271
272 case CURLINFO_HEADER_OUT:
273 utype |= PHP_HTTP_CLIENT_DEBUG_HEADER;
274 goto data_out;
275
276 case CURLINFO_SSL_DATA_OUT:
277 utype |= PHP_HTTP_CLIENT_DEBUG_SSL;
278 goto data_out;
279
280 case CURLINFO_DATA_OUT:
281 data_out:
282 utype |= PHP_HTTP_CLIENT_DEBUG_OUT;
283 h->progress.info = "send";
284 break;
285
286 case CURLINFO_HEADER_IN:
287 utype |= PHP_HTTP_CLIENT_DEBUG_HEADER;
288 goto data_in;
289
290 case CURLINFO_SSL_DATA_IN:
291 utype |= PHP_HTTP_CLIENT_DEBUG_SSL;
292 goto data_in;
293
294 case CURLINFO_DATA_IN:
295 data_in:
296 utype |= PHP_HTTP_CLIENT_DEBUG_IN;
297 h->progress.info = "receive";
298 break;
299
300 default:
301 break;
302 }
303
304 if (h->client->callback.debug.func) {
305 h->client->callback.debug.func(h->client->callback.debug.arg, h->client, &h->queue, utype, data, length);
306 }
307
308 #if 0
309 /* debug */
310 _dpf(type, data, length);
311 #endif
312
313 return 0;
314 }
315
316 static int php_http_curle_header_callback(char *data, size_t n, size_t l, void *arg)
317 {
318 php_http_client_curl_handler_t *h = arg;
319
320 return php_http_buffer_append(&h->response.headers, data, n * l);
321 }
322
323 static int php_http_curle_body_callback(char *data, size_t n, size_t l, void *arg)
324 {
325 php_http_client_curl_handler_t *h = arg;
326
327 return php_http_message_body_append(h->response.body, data, n*l);
328 }
329
330 static ZEND_RESULT_CODE php_http_curle_get_info(CURL *ch, HashTable *info)
331 {
332 char *c = NULL;
333 long l = 0;
334 double d = 0;
335 curl_off_t o = 0;
336 struct curl_slist *s = NULL, *p = NULL;
337 zval tmp;
338
339 ZVAL_NULL(&tmp);
340
341 /* BEGIN::CURLINFO */
342 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_EFFECTIVE_URL, &c)) {
343 ZVAL_STRING(&tmp, STR_PTR(c));
344 zend_hash_str_update(info, "effective_url", lenof("effective_url"), &tmp);
345 }
346 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &l)) {
347 ZVAL_LONG(&tmp, l);
348 zend_hash_str_update(info, "response_code", lenof("response_code"), &tmp);
349 }
350 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TOTAL_TIME, &d)) {
351 ZVAL_DOUBLE(&tmp, d);
352 zend_hash_str_update(info, "total_time", lenof("total_time"), &tmp);
353 }
354 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NAMELOOKUP_TIME, &d)) {
355 ZVAL_DOUBLE(&tmp, d);
356 zend_hash_str_update(info, "namelookup_time", lenof("namelookup_time"), &tmp);
357 }
358 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONNECT_TIME, &d)) {
359 ZVAL_DOUBLE(&tmp, d);
360 zend_hash_str_update(info, "connect_time", lenof("connect_time"), &tmp);
361 }
362 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRETRANSFER_TIME, &d)) {
363 ZVAL_DOUBLE(&tmp, d);
364 zend_hash_str_update(info, "pretransfer_time", lenof("pretransfer_time"), &tmp);
365 }
366 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_UPLOAD, &d)) {
367 ZVAL_DOUBLE(&tmp, d);
368 zend_hash_str_update(info, "size_upload", lenof("size_upload"), &tmp);
369 }
370 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_DOWNLOAD, &d)) {
371 ZVAL_DOUBLE(&tmp, d);
372 zend_hash_str_update(info, "size_download", lenof("size_download"), &tmp);
373 }
374 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_DOWNLOAD, &d)) {
375 ZVAL_DOUBLE(&tmp, d);
376 zend_hash_str_update(info, "speed_download", lenof("speed_download"), &tmp);
377 }
378 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_UPLOAD, &d)) {
379 ZVAL_DOUBLE(&tmp, d);
380 zend_hash_str_update(info, "speed_upload", lenof("speed_upload"), &tmp);
381 }
382 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HEADER_SIZE, &l)) {
383 ZVAL_LONG(&tmp, l);
384 zend_hash_str_update(info, "header_size", lenof("header_size"), &tmp);
385 }
386 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REQUEST_SIZE, &l)) {
387 ZVAL_LONG(&tmp, l);
388 zend_hash_str_update(info, "request_size", lenof("request_size"), &tmp);
389 }
390 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_VERIFYRESULT, &l)) {
391 ZVAL_LONG(&tmp, l);
392 zend_hash_str_update(info, "ssl_verifyresult", lenof("ssl_verifyresult"), &tmp);
393 }
394 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_FILETIME, &l)) {
395 ZVAL_LONG(&tmp, l);
396 zend_hash_str_update(info, "filetime", lenof("filetime"), &tmp);
397 }
398 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
399 ZVAL_DOUBLE(&tmp, d);
400 zend_hash_str_update(info, "content_length_download", lenof("content_length_download"), &tmp);
401 }
402 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_UPLOAD, &d)) {
403 ZVAL_DOUBLE(&tmp, d);
404 zend_hash_str_update(info, "content_length_upload", lenof("content_length_upload"), &tmp);
405 }
406 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_STARTTRANSFER_TIME, &d)) {
407 ZVAL_DOUBLE(&tmp, d);
408 zend_hash_str_update(info, "starttransfer_time", lenof("starttransfer_time"), &tmp);
409 }
410 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &c)) {
411 ZVAL_STRING(&tmp, STR_PTR(c));
412 zend_hash_str_update(info, "content_type", lenof("content_type"), &tmp);
413 }
414 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_TIME, &d)) {
415 ZVAL_DOUBLE(&tmp, d);
416 zend_hash_str_update(info, "redirect_time", lenof("redirect_time"), &tmp);
417 }
418 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_COUNT, &l)) {
419 ZVAL_LONG(&tmp, l);
420 zend_hash_str_update(info, "redirect_count", lenof("redirect_count"), &tmp);
421 }
422 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTP_CONNECTCODE, &l)) {
423 ZVAL_LONG(&tmp, l);
424 zend_hash_str_update(info, "connect_code", lenof("connect_code"), &tmp);
425 }
426 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTPAUTH_AVAIL, &l)) {
427 ZVAL_LONG(&tmp, l);
428 zend_hash_str_update(info, "httpauth_avail", lenof("httpauth_avail"), &tmp);
429 }
430 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXYAUTH_AVAIL, &l)) {
431 ZVAL_LONG(&tmp, l);
432 zend_hash_str_update(info, "proxyauth_avail", lenof("proxyauth_avail"), &tmp);
433 }
434 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_OS_ERRNO, &l)) {
435 ZVAL_LONG(&tmp, l);
436 zend_hash_str_update(info, "os_errno", lenof("os_errno"), &tmp);
437 }
438 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NUM_CONNECTS, &l)) {
439 ZVAL_LONG(&tmp, l);
440 zend_hash_str_update(info, "num_connects", lenof("num_connects"), &tmp);
441 }
442 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_ENGINES, &s)) {
443 array_init(&tmp);
444 for (p = s; p; p = p->next) {
445 if (p->data) {
446 add_next_index_string(&tmp, p->data);
447 }
448 }
449 zend_hash_str_update(info, "ssl_engines", lenof("ssl_engines"), &tmp);
450 curl_slist_free_all(s);
451 }
452 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_URL, &c)) {
453 ZVAL_STRING(&tmp, STR_PTR(c));
454 zend_hash_str_update(info, "redirect_url", lenof("redirect_url"), &tmp);
455 }
456 #if PHP_HTTP_CURL_VERSION(7,19,0)
457 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_IP, &c)) {
458 ZVAL_STRING(&tmp, STR_PTR(c));
459 zend_hash_str_update(info, "primary_ip", lenof("primary_ip"), &tmp);
460 }
461 #endif
462 #if PHP_HTTP_CURL_VERSION(7,19,0)
463 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_APPCONNECT_TIME, &d)) {
464 ZVAL_DOUBLE(&tmp, d);
465 zend_hash_str_update(info, "appconnect_time", lenof("appconnect_time"), &tmp);
466 }
467 #endif
468 #if PHP_HTTP_CURL_VERSION(7,19,4)
469 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONDITION_UNMET, &l)) {
470 ZVAL_LONG(&tmp, l);
471 zend_hash_str_update(info, "condition_unmet", lenof("condition_unmet"), &tmp);
472 }
473 #endif
474 #if PHP_HTTP_CURL_VERSION(7,21,0)
475 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_PORT, &l)) {
476 ZVAL_LONG(&tmp, l);
477 zend_hash_str_update(info, "primary_port", lenof("primary_port"), &tmp);
478 }
479 #endif
480 #if PHP_HTTP_CURL_VERSION(7,21,0)
481 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_IP, &c)) {
482 ZVAL_STRING(&tmp, STR_PTR(c));
483 zend_hash_str_update(info, "local_ip", lenof("local_ip"), &tmp);
484 }
485 #endif
486 #if PHP_HTTP_CURL_VERSION(7,21,0)
487 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_PORT, &l)) {
488 ZVAL_LONG(&tmp, l);
489 zend_hash_str_update(info, "local_port", lenof("local_port"), &tmp);
490 }
491 #endif
492 #if PHP_HTTP_CURL_VERSION(7,50,0)
493 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTP_VERSION, &l)) {
494 ZVAL_LONG(&tmp, l);
495 zend_hash_str_update(info, "http_version", lenof("http_version"), &tmp);
496 }
497 #endif
498 #if PHP_HTTP_CURL_VERSION(7,52,0)
499 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXY_SSL_VERIFYRESULT, &l)) {
500 ZVAL_LONG(&tmp, l);
501 zend_hash_str_update(info, "proxy_ssl_verifyresult", lenof("proxy_ssl_verifyresult"), &tmp);
502 }
503 #endif
504 #if PHP_HTTP_CURL_VERSION(7,52,0)
505 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROTOCOL, &l)) {
506 ZVAL_LONG(&tmp, l);
507 zend_hash_str_update(info, "protocol", lenof("protocol"), &tmp);
508 }
509 #endif
510 #if PHP_HTTP_CURL_VERSION(7,52,0)
511 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SCHEME, &c)) {
512 ZVAL_STRING(&tmp, STR_PTR(c));
513 zend_hash_str_update(info, "scheme", lenof("scheme"), &tmp);
514 }
515 #endif
516 #if PHP_HTTP_CURL_VERSION(7,66,0)
517 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_RETRY_AFTER, &o)) {
518 ZVAL_LONG(&tmp, o);
519 zend_hash_str_update(info, "retry_after", lenof("retry_after"), &tmp);
520 }
521 #endif
522 #if PHP_HTTP_CURL_VERSION(7,72,0)
523 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_EFFECTIVE_METHOD, &c)) {
524 ZVAL_STRING(&tmp, STR_PTR(c));
525 zend_hash_str_update(info, "effective_method", lenof("effective_method"), &tmp);
526 }
527 #endif
528 #if PHP_HTTP_CURL_VERSION(7,73,0)
529 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXY_ERROR, &l)) {
530 ZVAL_LONG(&tmp, l);
531 zend_hash_str_update(info, "proxy_error", lenof("proxy_error"), &tmp);
532 }
533 #endif
534
535 /* END::CURLINFO */
536
537 #if PHP_HTTP_CURL_VERSION(7,34,0)
538 {
539 zval ti_array, subarray;
540 struct curl_tlssessioninfo *ti;
541
542 #if PHP_HTTP_CURL_VERSION(7,48,0)
543 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TLS_SSL_PTR, &ti)) {
544 #else
545 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TLS_SESSION, &ti)) {
546 #endif
547 char *backend;
548
549 ZVAL_NULL(&subarray);
550 array_init(&ti_array);
551
552 switch (ti->backend) {
553 case CURLSSLBACKEND_NONE:
554 backend = "none";
555 break;
556 case CURLSSLBACKEND_OPENSSL:
557 backend = "openssl";
558 #if PHP_HTTP_HAVE_LIBCURL_OPENSSL
559 {
560 #if PHP_HTTP_CURL_VERSION(7,48,0)
561 SSL *ssl = ti->internals;
562 SSL_CTX *ctx = ssl ? SSL_get_SSL_CTX(ssl) : NULL;
563 #else
564 SSL_CTX *ctx = ti->internals;
565 #endif
566
567 array_init(&subarray);
568 if (ctx) {
569 add_assoc_long_ex(&subarray, ZEND_STRL("number"), SSL_CTX_sess_number(ctx));
570 add_assoc_long_ex(&subarray, ZEND_STRL("connect"), SSL_CTX_sess_connect(ctx));
571 add_assoc_long_ex(&subarray, ZEND_STRL("connect_good"), SSL_CTX_sess_connect_good(ctx));
572 add_assoc_long_ex(&subarray, ZEND_STRL("connect_renegotiate"), SSL_CTX_sess_connect_renegotiate(ctx));
573 add_assoc_long_ex(&subarray, ZEND_STRL("hits"), SSL_CTX_sess_hits(ctx));
574 add_assoc_long_ex(&subarray, ZEND_STRL("cache_full"), SSL_CTX_sess_cache_full(ctx));
575 }
576 }
577 #endif
578 break;
579 case CURLSSLBACKEND_GNUTLS:
580 backend = "gnutls";
581 #if PHP_HTTP_HAVE_LIBCURL_GNUTLS
582 {
583 gnutls_session_t sess = ti->internals;
584 char *desc;
585
586 array_init(&subarray);
587 if (sess) {
588 if ((desc = gnutls_session_get_desc(sess))) {
589 add_assoc_string_ex(&subarray, ZEND_STRL("desc"), desc);
590 gnutls_free(desc);
591 }
592 add_assoc_bool_ex(&subarray, ZEND_STRL("resumed"), gnutls_session_is_resumed(sess));
593 }
594 }
595 #endif
596 break;
597 case CURLSSLBACKEND_NSS:
598 backend = "nss";
599 break;
600 #if !PHP_HTTP_CURL_VERSION(7,39,0)
601 case CURLSSLBACKEND_QSOSSL:
602 backend = "qsossl";
603 break;
604 #else
605 case CURLSSLBACKEND_GSKIT:
606 backend = "gskit";
607 break;
608 #endif
609 case CURLSSLBACKEND_POLARSSL:
610 backend = "polarssl";
611 break;
612 case CURLSSLBACKEND_CYASSL:
613 backend = "cyassl";
614 break;
615 case CURLSSLBACKEND_SCHANNEL:
616 backend = "schannel";
617 break;
618 case CURLSSLBACKEND_DARWINSSL:
619 backend = "darwinssl";
620 break;
621 default:
622 backend = "unknown";
623 }
624 add_assoc_string_ex(&ti_array, ZEND_STRL("backend"), backend);
625 add_assoc_zval_ex(&ti_array, ZEND_STRL("internals"), &subarray);
626 zend_hash_str_update(info, "tls_session", lenof("tls_session"), &ti_array);
627 }
628 }
629 #endif
630
631 #if (PHP_HTTP_CURL_VERSION(7,19,1) && PHP_HTTP_HAVE_LIBCURL_OPENSSL) || \
632 (PHP_HTTP_CURL_VERSION(7,34,0) && PHP_HTTP_HAVE_LIBCURL_NSS) || \
633 (PHP_HTTP_CURL_VERSION(7,42,0) && PHP_HTTP_HAVE_LIBCURL_GNUTLS) || \
634 (PHP_HTTP_CURL_VERSION(7,39,0) && PHP_HTTP_HAVE_LIBCURL_GSKIT)
635 {
636 int i;
637 zval ci_array, subarray;
638 struct curl_certinfo *ci;
639 char *colon, *keyname;
640
641 if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CERTINFO, &ci)) {
642 array_init(&ci_array);
643
644 for (i = 0; i < ci->num_of_certs; ++i) {
645 s = ci->certinfo[i];
646
647 array_init(&subarray);
648 for (p = s; p; p = p->next) {
649 if (p->data) {
650 if ((colon = strchr(p->data, ':'))) {
651 keyname = estrndup(p->data, colon - p->data);
652 add_assoc_string_ex(&subarray, keyname, colon - p->data, colon + 1);
653 efree(keyname);
654 } else {
655 add_next_index_string(&subarray, p->data);
656 }
657 }
658 }
659 add_next_index_zval(&ci_array, &subarray);
660 }
661 zend_hash_str_update(info, "certinfo", lenof("certinfo"), &ci_array);
662 }
663 }
664 #endif
665 {
666 php_http_curle_storage_t *st = php_http_curle_get_storage(ch);
667
668 ZVAL_LONG(&tmp, st->errorcode);
669 zend_hash_str_update(info, "curlcode", lenof("curlcode"), &tmp);
670 ZVAL_STRING(&tmp, st->errorbuffer);
671 zend_hash_str_update(info, "error", lenof("error"), &tmp);
672 }
673
674 return SUCCESS;
675 }
676
677 static int compare_queue(php_http_client_enqueue_t *e, void *handle)
678 {
679 return handle == ((php_http_client_curl_handler_t *) e->opaque)->handle;
680 }
681
682 static php_http_message_t *php_http_curlm_responseparser(php_http_client_curl_handler_t *h)
683 {
684 php_http_message_t *response;
685 php_http_header_parser_t parser;
686 zval *zh, tmp;
687
688 response = php_http_message_init(NULL, 0, h->response.body);
689 php_http_header_parser_init(&parser);
690 while (h->response.headers.used) {
691 php_http_header_parser_state_t st = php_http_header_parser_parse(&parser,
692 &h->response.headers, PHP_HTTP_HEADER_PARSER_CLEANUP, &response->hdrs,
693 (php_http_info_callback_t) php_http_message_info_callback, (void *) &response);
694 if (PHP_HTTP_HEADER_PARSER_STATE_FAILURE == st) {
695 break;
696 }
697 }
698 php_http_header_parser_dtor(&parser);
699
700 /* move body to right message */
701 if (response->body != h->response.body) {
702 php_http_message_t *ptr = response;
703
704 while (ptr->parent) {
705 ptr = ptr->parent;
706 }
707 php_http_message_body_free(&response->body);
708 response->body = ptr->body;
709 ptr->body = NULL;
710 }
711 php_http_message_body_addref(h->response.body);
712
713 /* let's update the response headers */
714 if ((zh = php_http_message_header(response, ZEND_STRL("Content-Length")))) {
715 ZVAL_COPY(&tmp, zh);
716 zend_hash_str_update(&response->hdrs, "X-Original-Content-Length", lenof("X-Original-Content-Length"), &tmp);
717 }
718 if ((zh = php_http_message_header(response, ZEND_STRL("Transfer-Encoding")))) {
719 ZVAL_COPY(&tmp, zh);
720 zend_hash_str_del(&response->hdrs, "Transfer-Encoding", lenof("Transfer-Encoding"));
721 zend_hash_str_update(&response->hdrs, "X-Original-Transfer-Encoding", lenof("X-Original-Transfer-Encoding"), &tmp);
722 }
723 if ((zh = php_http_message_header(response, ZEND_STRL("Content-Range")))) {
724 ZVAL_COPY(&tmp, zh);
725 zend_hash_str_del(&response->hdrs, "Content-Range", lenof("Content-Range"));
726 zend_hash_str_update(&response->hdrs, "X-Original-Content-Range", lenof("X-Original-Content-Range"), &tmp);
727 }
728 if ((zh = php_http_message_header(response, ZEND_STRL("Content-Encoding")))) {
729 ZVAL_COPY(&tmp, zh);
730 zend_hash_str_del(&response->hdrs, "Content-Encoding", lenof("Content-Encoding"));
731 zend_hash_str_update(&response->hdrs, "X-Original-Content-Encoding", lenof("X-Original-Content-Encoding"), &tmp);
732 }
733 php_http_message_update_headers(response);
734
735 return response;
736 }
737
738 void php_http_client_curl_responsehandler(php_http_client_t *context)
739 {
740 int err_count = 0, remaining = 0;
741 php_http_curle_storage_t *st, *err = NULL;
742 php_http_client_enqueue_t *enqueue;
743 php_http_client_curl_t *curl = context->ctx;
744
745 do {
746 CURLMsg *msg = curl_multi_info_read(curl->handle->multi, &remaining);
747
748 if (msg && CURLMSG_DONE == msg->msg) {
749 if (CURLE_OK != msg->data.result) {
750 st = php_http_curle_get_storage(msg->easy_handle);
751 st->errorcode = msg->data.result;
752
753 /* defer the warnings/exceptions, so the callback is still called for this request */
754 if (!err) {
755 err = ecalloc(remaining + 1, sizeof(*err));
756 }
757 memcpy(&err[err_count], st, sizeof(*st));
758 if (st->url) {
759 err[err_count].url = estrdup(st->url);
760 }
761 err_count++;
762 }
763
764 if ((enqueue = php_http_client_enqueued(context, msg->easy_handle, compare_queue))) {
765 php_http_client_curl_handler_t *handler = enqueue->opaque;
766 php_http_message_t *response = php_http_curlm_responseparser(handler);
767
768 if (response) {
769 context->callback.response.func(context->callback.response.arg, context, &handler->queue, &response);
770 php_http_message_free(&response);
771 }
772 }
773 }
774 } while (remaining);
775
776 if (err_count) {
777 int i = 0;
778
779 do {
780 php_error_docref(NULL, E_WARNING, "%s; %s (%s)", curl_easy_strerror(err[i].errorcode), err[i].errorbuffer, STR_PTR(err[i].url));
781 if (err[i].url) {
782 efree(err[i].url);
783 }
784 } while (++i < err_count);
785
786 efree(err);
787 }
788 }
789
790 void php_http_client_curl_loop(php_http_client_t *client, curl_socket_t s, int curl_action)
791 {
792 CURLMcode rc;
793 php_http_client_curl_t *curl = client->ctx;
794
795 #if DBG_EVENTS
796 fprintf(stderr, "H");
797 #endif
798
799 do {
800 rc = curl_multi_socket_action(curl->handle->multi, s, curl_action, &curl->unfinished);
801 } while (CURLM_CALL_MULTI_PERFORM == rc);
802
803 if (CURLM_OK != rc) {
804 php_error_docref(NULL, E_WARNING, "%s", curl_multi_strerror(rc));
805 }
806
807 php_http_client_curl_responsehandler(client);
808 }
809
810 /* curl options */
811
812 static php_http_options_t php_http_curle_options, php_http_curlm_options;
813
814 #define PHP_HTTP_CURLE_OPTION_CHECK_STRLEN 0x0001
815 #define PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR 0x0002
816 #define PHP_HTTP_CURLE_OPTION_TRANSFORM_MS 0x0004
817 #define PHP_HTTP_CURLE_OPTION_IGNORE_RC 0x0008
818
819 static ZEND_RESULT_CODE php_http_curle_option_set_ssl_verifyhost(php_http_option_t *opt, zval *val, void *userdata)
820 {
821 php_http_client_curl_handler_t *curl = userdata;
822 CURL *ch = curl->handle;
823
824 if (CURLE_OK != curl_easy_setopt(ch, opt->option, Z_TYPE_P(val) == IS_TRUE ? 2 : 0)) {
825 return FAILURE;
826 }
827 return SUCCESS;
828 }
829
830 static ZEND_RESULT_CODE php_http_curle_option_set_cookiesession(php_http_option_t *opt, zval *val, void *userdata)
831 {
832 php_http_client_curl_handler_t *curl = userdata;
833 CURL *ch = curl->handle;
834
835 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIESESSION, (long) (Z_TYPE_P(val) == IS_TRUE))) {
836 return FAILURE;
837 }
838 if (Z_TYPE_P(val) == IS_TRUE) {
839 #if DEBUG_COOKIES
840 fprintf(stderr, "CURLOPT_COOKIELIST: SESS\n");
841 #endif
842 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIELIST, "SESS")) {
843 return FAILURE;
844 }
845 }
846
847 return SUCCESS;
848 }
849
850 static ZEND_RESULT_CODE php_http_curle_option_set_cookiestore(php_http_option_t *opt, zval *val, void *userdata)
851 {
852 php_http_client_curl_handler_t *curl = userdata;
853 CURL *ch = curl->handle;
854 php_http_curle_storage_t *storage = php_http_curle_get_storage(curl->handle);
855
856 if (storage->cookiestore) {
857 pefree(storage->cookiestore, 1);
858 }
859 if (val && Z_TYPE_P(val) == IS_STRING && Z_STRLEN_P(val)) {
860 storage->cookiestore = pestrndup(Z_STRVAL_P(val), Z_STRLEN_P(val), 1);
861 } else {
862 storage->cookiestore = NULL;
863 }
864
865 #if DEBUG_COOKIES
866 fprintf(stderr, "CURLOPT_COOKIEFILE: %s\n", storage->cookiestore);
867 #endif
868 // does NOT enable ch->data.cookies until transfer; adds to ch->stsate.cookielist
869 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIEFILE, storage->cookiestore ? storage->cookiestore : "")) {
870 return FAILURE;
871 }
872 #if DEBUG_COOKIES
873 fprintf(stderr, "CURLOPT_COOKIEJAR: %s\n", storage->cookiestore);
874 #endif
875 // enables ch->data.cookies
876 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIEJAR, storage->cookiestore)) {
877 return FAILURE;
878 }
879
880 return SUCCESS;
881 }
882
883 static ZEND_RESULT_CODE php_http_curle_option_set_cookies(php_http_option_t *opt, zval *val, void *userdata)
884 {
885 php_http_client_curl_handler_t *curl = userdata;
886 CURL *ch = curl->handle;
887
888 if (val && Z_TYPE_P(val) != IS_NULL) {
889 HashTable *ht = HASH_OF(val);
890
891 if (curl->options.encode_cookies) {
892 if (SUCCESS == php_http_url_encode_hash_ex(ht, &curl->options.cookies, ZEND_STRL(";"), ZEND_STRL("="), NULL, 0)) {
893 php_http_buffer_fix(&curl->options.cookies);
894 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data)) {
895 return FAILURE;
896 }
897 } else {
898 return FAILURE;
899 }
900 } else {
901 php_http_arrkey_t cookie_key;
902 zval *cookie_val;
903
904 ZEND_HASH_FOREACH_KEY_VAL(ht, cookie_key.h, cookie_key.key, cookie_val)
905 {
906 zend_string *zs = zval_get_string(cookie_val);
907
908 php_http_arrkey_stringify(&cookie_key, NULL);
909 php_http_buffer_appendf(&curl->options.cookies, "%s=%s; ", cookie_key.key->val, zs->val);
910 php_http_arrkey_dtor(&cookie_key);
911
912 zend_string_release(zs);
913 }
914 ZEND_HASH_FOREACH_END();
915
916 php_http_buffer_fix(&curl->options.cookies);
917 if (curl->options.cookies.used) {
918 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data)) {
919 return FAILURE;
920 }
921 }
922 }
923 } else {
924 php_http_buffer_reset(&curl->options.cookies);
925 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIE, NULL)) {
926 return FAILURE;
927 }
928 }
929 return SUCCESS;
930 }
931
932 static ZEND_RESULT_CODE php_http_curle_option_set_encodecookies(php_http_option_t *opt, zval *val, void *userdata)
933 {
934 php_http_client_curl_handler_t *curl = userdata;
935
936 curl->options.encode_cookies = Z_TYPE_P(val) == IS_TRUE;
937 return SUCCESS;
938 }
939
940 static ZEND_RESULT_CODE php_http_curle_option_set_lastmodified(php_http_option_t *opt, zval *val, void *userdata)
941 {
942 php_http_client_curl_handler_t *curl = userdata;
943 CURL *ch = curl->handle;
944
945 if (Z_LVAL_P(val)) {
946 if (Z_LVAL_P(val) > 0) {
947 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(val))) {
948 return FAILURE;
949 }
950 } else {
951 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) sapi_get_request_time() + Z_LVAL_P(val))) {
952 return FAILURE;
953 }
954 }
955 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMECONDITION, (long) (curl->options.range_request ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE))) {
956 return FAILURE;
957 }
958 } else {
959 if ( CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, 0)
960 || CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMECONDITION, 0)
961 ) {
962 return FAILURE;
963 }
964 }
965 return SUCCESS;
966 }
967
968 #if PHP_HTTP_CURL_VERSION(7,64,1)
969 static ZEND_RESULT_CODE php_http_curle_option_set_altsvc_ctrl(php_http_option_t *opt, zval *val, void *userdata)
970 {
971 php_http_client_curl_handler_t *curl = userdata;
972 CURL *ch = curl->handle;
973
974 if (Z_LVAL_P(val)) {
975 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_ALTSVC_CTRL, Z_LVAL_P(val))) {
976 return FAILURE;
977 }
978 }
979 return SUCCESS;
980 }
981 #endif
982
983 static ZEND_RESULT_CODE php_http_curle_option_set_compress(php_http_option_t *opt, zval *val, void *userdata)
984 {
985 php_http_client_curl_handler_t *curl = userdata;
986 CURL *ch = curl->handle;
987
988 #if !PHP_HTTP_CURL_VERSION(7,21,6)
989 # define CURLOPT_ACCEPT_ENCODING CURLOPT_ENCODING
990 #endif
991 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_ACCEPT_ENCODING, Z_TYPE_P(val) == IS_TRUE ? "" : NULL)) {
992 return FAILURE;
993 }
994 return SUCCESS;
995 }
996
997 static ZEND_RESULT_CODE php_http_curle_option_set_etag(php_http_option_t *opt, zval *val, void *userdata)
998 {
999 php_http_client_curl_handler_t *curl = userdata;
1000 php_http_buffer_t header;
1001
1002 if (val && Z_TYPE_P(val) == IS_STRING && Z_STRLEN_P(val)) {
1003 zend_bool is_quoted = !((Z_STRVAL_P(val)[0] != '"') || (Z_STRVAL_P(val)[Z_STRLEN_P(val)-1] != '"'));
1004 php_http_buffer_init(&header);
1005 php_http_buffer_appendf(&header, is_quoted?"%s: %s":"%s: \"%s\"", curl->options.range_request?"If-Match":"If-None-Match", Z_STRVAL_P(val));
1006 php_http_buffer_fix(&header);
1007 curl->options.headers = curl_slist_append(curl->options.headers, header.data);
1008 php_http_buffer_dtor(&header);
1009 }
1010 return SUCCESS;
1011 }
1012
1013 static ZEND_RESULT_CODE php_http_curle_option_set_range(php_http_option_t *opt, zval *val, void *userdata)
1014 {
1015 php_http_client_curl_handler_t *curl = userdata;
1016 CURL *ch = curl->handle;
1017
1018 php_http_buffer_reset(&curl->options.ranges);
1019
1020 if (val && Z_TYPE_P(val) != IS_NULL) {
1021 zval *rr, *rb, *re;
1022 HashTable *ht = HASH_OF(val);
1023
1024 ZEND_HASH_FOREACH_VAL(ht, rr)
1025 {
1026 if (Z_TYPE_P(rr) == IS_ARRAY) {
1027 if (2 == php_http_array_list(Z_ARRVAL_P(rr), 2, &rb, &re)) {
1028 zend_long rbl = zval_get_long(rb), rel = zval_get_long(re);
1029
1030 if (rbl >= 0) {
1031 if (rel > 0) {
1032 php_http_buffer_appendf(&curl->options.ranges, "%ld-%ld,", rbl, rel);
1033 } else {
1034 php_http_buffer_appendf(&curl->options.ranges, "%ld-", rbl);
1035 }
1036 } else if (rel > 0) {
1037 php_http_buffer_appendf(&curl->options.ranges, "-%ld", rel);
1038 }
1039 }
1040 }
1041 }
1042 ZEND_HASH_FOREACH_END();
1043
1044 if (curl->options.ranges.used) {
1045 curl->options.range_request = 1;
1046 /* ditch last comma */
1047 curl->options.ranges.data[curl->options.ranges.used - 1] = '\0';
1048 }
1049 }
1050
1051 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RANGE, curl->options.ranges.data)) {
1052 return FAILURE;
1053 }
1054 return SUCCESS;
1055 }
1056
1057 static ZEND_RESULT_CODE php_http_curle_option_set_resume(php_http_option_t *opt, zval *val, void *userdata)
1058 {
1059 php_http_client_curl_handler_t *curl = userdata;
1060 CURL *ch = curl->handle;
1061
1062 if (Z_LVAL_P(val) > 0) {
1063 curl->options.range_request = 1;
1064 }
1065 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(val))) {
1066 return FAILURE;
1067 }
1068 return SUCCESS;
1069 }
1070
1071 static ZEND_RESULT_CODE php_http_curle_option_set_retrydelay(php_http_option_t *opt, zval *val, void *userdata)
1072 {
1073 php_http_client_curl_handler_t *curl = userdata;
1074
1075 curl->options.retry.delay = Z_DVAL_P(val);
1076 return SUCCESS;
1077 }
1078
1079 static ZEND_RESULT_CODE php_http_curle_option_set_retrycount(php_http_option_t *opt, zval *val, void *userdata)
1080 {
1081 php_http_client_curl_handler_t *curl = userdata;
1082
1083 curl->options.retry.count = Z_LVAL_P(val);
1084 return SUCCESS;
1085 }
1086
1087 static ZEND_RESULT_CODE php_http_curle_option_set_redirect(php_http_option_t *opt, zval *val, void *userdata)
1088 {
1089 php_http_client_curl_handler_t *curl = userdata;
1090 CURL *ch = curl->handle;
1091
1092 if ( CURLE_OK != curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(val) ? 1L : 0L)
1093 || CURLE_OK != curl_easy_setopt(ch, CURLOPT_MAXREDIRS, curl->options.redirects = Z_LVAL_P(val))
1094 ) {
1095 return FAILURE;
1096 }
1097 return SUCCESS;
1098 }
1099
1100 static ZEND_RESULT_CODE php_http_curle_option_set_portrange(php_http_option_t *opt, zval *val, void *userdata)
1101 {
1102 php_http_client_curl_handler_t *curl = userdata;
1103 CURL *ch = curl->handle;
1104 long localport = 0, localportrange = 0;
1105
1106 if (val && Z_TYPE_P(val) != IS_NULL) {
1107 zval *zps, *zpe;
1108
1109 switch (php_http_array_list(Z_ARRVAL_P(val), 2, &zps, &zpe)) {
1110 case 2:
1111 localportrange = labs(zval_get_long(zps)-zval_get_long(zpe))+1L;
1112 /* no break */
1113 case 1:
1114 localport = (zval_get_long(zpe) > 0) ? MIN(zval_get_long(zps), zval_get_long(zpe)) : zval_get_long(zps);
1115 break;
1116 default:
1117 break;
1118 }
1119 }
1120 if ( CURLE_OK != curl_easy_setopt(ch, CURLOPT_LOCALPORT, localport)
1121 || CURLE_OK != curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, localportrange)
1122 ) {
1123 return FAILURE;
1124 }
1125 return SUCCESS;
1126 }
1127
1128 #if PHP_HTTP_CURL_VERSION(7,37,0)
1129 static ZEND_RESULT_CODE php_http_curle_option_set_proxyheader(php_http_option_t *opt, zval *val, void *userdata)
1130 {
1131 php_http_client_curl_handler_t *curl = userdata;
1132
1133 if (val && Z_TYPE_P(val) != IS_NULL) {
1134 php_http_arrkey_t header_key;
1135 zval *header_val;
1136 php_http_buffer_t header;
1137
1138 php_http_buffer_init(&header);
1139 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(val), header_key.h, header_key.key, header_val)
1140 {
1141 if (header_key.key) {
1142 zend_string *zs = zval_get_string(header_val);
1143
1144 php_http_buffer_appendf(&header, "%s: %s", header_key.key->val, zs->val);
1145 zend_string_release(zs);
1146
1147 php_http_buffer_fix(&header);
1148 curl->options.proxyheaders = curl_slist_append(curl->options.proxyheaders, header.data);
1149 php_http_buffer_reset(&header);
1150
1151 }
1152 }
1153 ZEND_HASH_FOREACH_END();
1154 php_http_buffer_dtor(&header);
1155 }
1156 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_PROXYHEADER, curl->options.proxyheaders)) {
1157 return FAILURE;
1158 }
1159 if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_HEADEROPT, CURLHEADER_SEPARATE)) {
1160 curl_easy_setopt(curl->handle, CURLOPT_PROXYHEADER, NULL);
1161 return FAILURE;
1162 }
1163 return SUCCESS;
1164 }
1165 #endif
1166
1167 #if PHP_HTTP_CURL_VERSION(7,21,3)
1168 static ZEND_RESULT_CODE php_http_curle_option_set_resolve(php_http_option_t *opt, zval *val, void *userdata)
1169 {
1170 php_http_client_curl_handler_t *curl = userdata;
1171 CURL *ch = curl->handle;
1172
1173 if (val && Z_TYPE_P(val) != IS_NULL) {
1174 HashTable *ht = HASH_OF(val);
1175 zval *data;
1176
1177 ZEND_HASH_FOREACH_VAL(ht, data)
1178 {
1179 zend_string *zs = zval_get_string(data);
1180 curl->options.resolve = curl_slist_append(curl->options.resolve, zs->val);
1181 zend_string_release(zs);
1182 }
1183 ZEND_HASH_FOREACH_END();
1184
1185 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESOLVE, curl->options.resolve)) {
1186 return FAILURE;
1187 }
1188 } else {
1189 if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESOLVE, NULL)) {
1190 return FAILURE;
1191 }
1192 }
1193 return SUCCESS;
1194 }
1195 #endif
1196
1197 #if PHP_HTTP_CURL_VERSION(7,21,4) && PHP_HTTP_HAVE_LIBCURL_TLSAUTH_TYPE
1198 static ZEND_RESULT_CODE php_http_curle_option_set_ssl_tlsauthtype(php_http_option_t *opt, zval *val, void *userdata)
1199 {
1200 php_http_client_curl_handler_t *curl = userdata;
1201 CURL *ch = curl->handle;
1202
1203 if (val && Z_LVAL_P(val)) {
1204 switch (Z_LVAL_P(val)) {
1205 case CURL_TLSAUTH_SRP:
1206 if (CURLE_OK == curl_easy_setopt(ch, opt->option, PHP_HTTP_LIBCURL_TLSAUTH_SRP)) {
1207 return SUCCESS;
1208 }
1209 /* no break */
1210 default:
1211 return FAILURE;
1212 }
1213 }
1214 if (CURLE_OK != curl_easy_setopt(ch, opt->option, PHP_HTTP_LIBCURL_TLSAUTH_DEF)) {
1215 return FAILURE;
1216 }
1217 return SUCCESS;
1218 }
1219 #endif
1220
1221 static void php_http_curle_options_init(php_http_options_t *registry)
1222 {
1223 php_http_option_t *opt;
1224
1225 /* url options */
1226 #if PHP_HTTP_CURL_VERSION(7,42,0)
1227 php_http_option_register(registry, ZEND_STRL("path_as_is"), CURLOPT_PATH_AS_IS, _IS_BOOL);
1228 #endif
1229
1230 /* proxy */
1231 php_http_option_register(registry, ZEND_STRL("proxyhost"), CURLOPT_PROXY, IS_STRING);
1232 php_http_option_register(registry, ZEND_STRL("proxytype"), CURLOPT_PROXYTYPE, IS_LONG);
1233 php_http_option_register(registry, ZEND_STRL("proxyport"), CURLOPT_PROXYPORT, IS_LONG);
1234 if ((opt = php_http_option_register(registry, ZEND_STRL("proxyauth"), CURLOPT_PROXYUSERPWD, IS_STRING))) {
1235 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1236 }
1237 if ((opt = php_http_option_register(registry, ZEND_STRL("proxyauthtype"), CURLOPT_PROXYAUTH, IS_LONG))) {
1238 Z_LVAL(opt->defval) = CURLAUTH_ANYSAFE;
1239 }
1240 php_http_option_register(registry, ZEND_STRL("proxytunnel"), CURLOPT_HTTPPROXYTUNNEL, _IS_BOOL);
1241 #if PHP_HTTP_CURL_VERSION(7,19,4)
1242 php_http_option_register(registry, ZEND_STRL("noproxy"), CURLOPT_NOPROXY, IS_STRING);
1243 #endif
1244 #if PHP_HTTP_CURL_VERSION(7,55,0)
1245 php_http_option_register(registry, ZEND_STRL("socks5_auth"), CURLOPT_SOCKS5_AUTH, IS_LONG);
1246 #endif
1247
1248 #if PHP_HTTP_CURL_VERSION(7,37,0)
1249 if ((opt = php_http_option_register(registry, ZEND_STRL("proxyheader"), CURLOPT_PROXYHEADER, IS_ARRAY))) {
1250 opt->setter = php_http_curle_option_set_proxyheader;
1251 }
1252 #endif
1253 #if PHP_HTTP_CURL_VERSION(7,43,0)
1254 if (PHP_HTTP_CURL_FEATURE(CURL_VERSION_GSSAPI)
1255 && (opt = php_http_option_register(registry, ZEND_STRL("proxy_service_name"), CURLOPT_PROXY_SERVICE_NAME, IS_STRING))
1256 ) {
1257 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1258 }
1259 #endif
1260 #if PHP_HTTP_CURL_VERSION(7,60,0)
1261 php_http_option_register(registry, ZEND_STRL("haproxy_protocol"), CURLOPT_HAPROXYPROTOCOL, _IS_BOOL);
1262 #endif
1263
1264 /* unix sockets */
1265 #if PHP_HTTP_CURL_VERSION(7,40,0)
1266 if (PHP_HTTP_CURL_FEATURE(CURL_VERSION_UNIX_SOCKETS)) {
1267 if ((opt = php_http_option_register(registry, ZEND_STRL("unix_socket_path"), CURLOPT_UNIX_SOCKET_PATH, IS_STRING))) {
1268 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1269 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1270 }
1271 #if PHP_HTTP_CURL_VERSION(7,53,0)
1272 if ((opt = php_http_option_register(registry, ZEND_STRL("abstract_unix_socket"), CURLOPT_ABSTRACT_UNIX_SOCKET, IS_STRING))) {
1273 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1274 }
1275 #endif
1276 }
1277 #endif
1278
1279 /* dns */
1280 if ((opt = php_http_option_register(registry, ZEND_STRL("dns_cache_timeout"), CURLOPT_DNS_CACHE_TIMEOUT, IS_LONG))) {
1281 Z_LVAL(opt->defval) = 60;
1282 }
1283 php_http_option_register(registry, ZEND_STRL("ipresolve"), CURLOPT_IPRESOLVE, IS_LONG);
1284 #if PHP_HTTP_CURL_VERSION(7,21,3)
1285 if ((opt = php_http_option_register(registry, ZEND_STRL("resolve"), CURLOPT_RESOLVE, IS_ARRAY))) {
1286 opt->setter = php_http_curle_option_set_resolve;
1287 }
1288 #endif
1289 #if PHP_HTTP_HAVE_LIBCURL_ARES
1290 # if PHP_HTTP_CURL_VERSION(7,24,0)
1291 if ((opt = php_http_option_register(registry, ZEND_STRL("dns_servers"), CURLOPT_DNS_SERVERS, IS_STRING))) {
1292 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1293 }
1294 # endif
1295 # if PHP_HTTP_CURL_VERSION(7,33,0)
1296 if ((opt = php_http_option_register(registry, ZEND_STRL("dns_interface"), CURLOPT_DNS_INTERFACE, IS_STRING))) {
1297 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1298 }
1299 if ((opt = php_http_option_register(registry, ZEND_STRL("dns_local_ip4"), CURLOPT_DNS_LOCAL_IP4, IS_STRING))) {
1300 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1301 }
1302 if ((opt = php_http_option_register(registry, ZEND_STRL("dns_local_ip6"), CURLOPT_DNS_LOCAL_IP6, IS_STRING))) {
1303 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1304 }
1305 # endif
1306 #endif
1307 #if PHP_HTTP_CURL_VERSION(7,60,0)
1308 php_http_option_register(registry, ZEND_STRL("dns_shuffle_addresses"), CURLOPT_DNS_SHUFFLE_ADDRESSES, _IS_BOOL);
1309 #endif
1310 #if PHP_HTTP_CURL_VERSION(7,62,0)
1311 php_http_option_register(registry, ZEND_STRL("doh_url"), CURLOPT_DOH_URL, IS_STRING);
1312 #endif
1313
1314
1315 /* limits */
1316 php_http_option_register(registry, ZEND_STRL("low_speed_limit"), CURLOPT_LOW_SPEED_LIMIT, IS_LONG);
1317 php_http_option_register(registry, ZEND_STRL("low_speed_time"), CURLOPT_LOW_SPEED_TIME, IS_LONG);
1318
1319 /* LSF weirdance
1320 php_http_option_register(registry, ZEND_STRL("max_send_speed"), CURLOPT_MAX_SEND_SPEED_LARGE, IS_LONG);
1321 php_http_option_register(registry, ZEND_STRL("max_recv_speed"), CURLOPT_MAX_RECV_SPEED_LARGE, IS_LONG);
1322 */
1323
1324 /* connection handling */
1325 /* crashes
1326 if ((opt = php_http_option_register(registry, ZEND_STRL("maxconnects"), CURLOPT_MAXCONNECTS, IS_LONG))) {
1327 Z_LVAL(opt->defval) = 5;
1328 }
1329 */
1330 php_http_option_register(registry, ZEND_STRL("fresh_connect"), CURLOPT_FRESH_CONNECT, _IS_BOOL);
1331 php_http_option_register(registry, ZEND_STRL("forbid_reuse"), CURLOPT_FORBID_REUSE, _IS_BOOL);
1332 #if PHP_HTTP_CURL_VERSION(7,65,0)
1333 if ((opt = php_http_option_register(registry, ZEND_STRL("maxage_conn"), CURLOPT_MAXAGE_CONN, IS_LONG))) {
1334 ZVAL_LONG(&opt->defval, 118);
1335 }
1336 #endif
1337
1338 /* outgoing interface */
1339 php_http_option_register(registry, ZEND_STRL("interface"), CURLOPT_INTERFACE, IS_STRING);
1340 if ((opt = php_http_option_register(registry, ZEND_STRL("portrange"), CURLOPT_LOCALPORT, IS_ARRAY))) {
1341 opt->setter = php_http_curle_option_set_portrange;
1342 }
1343
1344 /* another endpoint port */
1345 php_http_option_register(registry, ZEND_STRL("port"), CURLOPT_PORT, IS_LONG);
1346
1347 /* RFC4007 zone_id */
1348 #if PHP_HTTP_CURL_VERSION(7,19,0)
1349 php_http_option_register(registry, ZEND_STRL("address_scope"), CURLOPT_ADDRESS_SCOPE, IS_LONG);
1350 #endif
1351
1352 /* auth */
1353 if ((opt = php_http_option_register(registry, ZEND_STRL("httpauth"), CURLOPT_USERPWD, IS_STRING))) {
1354 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1355 }
1356 if ((opt = php_http_option_register(registry, ZEND_STRL("httpauthtype"), CURLOPT_HTTPAUTH, IS_LONG))) {
1357 Z_LVAL(opt->defval) = CURLAUTH_ANYSAFE;
1358 }
1359 #if PHP_HTTP_CURL_VERSION(7,43,0)
1360 if (PHP_HTTP_CURL_FEATURE(CURL_VERSION_SSPI) || PHP_HTTP_CURL_FEATURE(CURL_VERSION_GSSAPI))
1361 if ((opt = php_http_option_register(registry, ZEND_STRL("service_name"), CURLOPT_SERVICE_NAME, IS_STRING))) {
1362 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1363 }
1364 #endif
1365 #if PHP_HTTP_CURL_VERSION(7,61,0)
1366 if ((opt = php_http_option_register(registry, ZEND_STRL("xoauth2_bearer"), CURLOPT_XOAUTH2_BEARER, IS_STRING))) {
1367 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1368 }
1369 #endif
1370 #if PHP_HTTP_CURL_VERSION(7,75,0)
1371 php_http_option_register(registry, ZEND_STRL("aws_sigv4"), CURLOPT_AWS_SIGV4, IS_STRING);
1372 #endif
1373
1374 /* redirects */
1375 if ((opt = php_http_option_register(registry, ZEND_STRL("redirect"), CURLOPT_FOLLOWLOCATION, IS_LONG))) {
1376 opt->setter = php_http_curle_option_set_redirect;
1377 }
1378 php_http_option_register(registry, ZEND_STRL("unrestricted_auth"), CURLOPT_UNRESTRICTED_AUTH, _IS_BOOL);
1379 #if PHP_HTTP_CURL_VERSION(7,19,1)
1380 php_http_option_register(registry, ZEND_STRL("postredir"), CURLOPT_POSTREDIR, IS_LONG);
1381 #endif
1382
1383 /* retries */
1384 if ((opt = php_http_option_register(registry, ZEND_STRL("retrycount"), 0, IS_LONG))) {
1385 opt->setter = php_http_curle_option_set_retrycount;
1386 }
1387 if ((opt = php_http_option_register(registry, ZEND_STRL("retrydelay"), 0, IS_DOUBLE))) {
1388 opt->setter = php_http_curle_option_set_retrydelay;
1389 }
1390
1391 /* referer */
1392 if ((opt = php_http_option_register(registry, ZEND_STRL("referer"), CURLOPT_REFERER, IS_STRING))) {
1393 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1394 }
1395 if ((opt = php_http_option_register(registry, ZEND_STRL("autoreferer"), CURLOPT_AUTOREFERER, _IS_BOOL))) {
1396 ZVAL_BOOL(&opt->defval, 1);
1397 }
1398
1399 /* useragent */
1400 if ((opt = php_http_option_register(registry, ZEND_STRL("useragent"), CURLOPT_USERAGENT, IS_STRING))) {
1401 /* don't check strlen, to allow sending no useragent at all */
1402 ZVAL_PSTRING(&opt->defval,
1403 "PECL_HTTP/" PHP_PECL_HTTP_VERSION " "
1404 "PHP/" PHP_VERSION " "
1405 "libcurl/" LIBCURL_VERSION);
1406 }
1407
1408 /* resume */
1409 if ((opt = php_http_option_register(registry, ZEND_STRL("resume"), CURLOPT_RESUME_FROM, IS_LONG))) {
1410 opt->setter = php_http_curle_option_set_resume;
1411 }
1412 /* ranges */
1413 if ((opt = php_http_option_register(registry, ZEND_STRL("range"), CURLOPT_RANGE, IS_ARRAY))) {
1414 opt->setter = php_http_curle_option_set_range;
1415 }
1416
1417 /* etag */
1418 if ((opt = php_http_option_register(registry, ZEND_STRL("etag"), 0, IS_STRING))) {
1419 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1420 opt->setter = php_http_curle_option_set_etag;
1421 }
1422
1423 /* compression */
1424 if ((opt = php_http_option_register(registry, ZEND_STRL("compress"), 0, _IS_BOOL))) {
1425 opt->setter = php_http_curle_option_set_compress;
1426 }
1427
1428 /* lastmodified */
1429 if ((opt = php_http_option_register(registry, ZEND_STRL("lastmodified"), 0, IS_LONG))) {
1430 opt->setter = php_http_curle_option_set_lastmodified;
1431 }
1432
1433 /* cookies */
1434 if ((opt = php_http_option_register(registry, ZEND_STRL("encodecookies"), 0, _IS_BOOL))) {
1435 opt->setter = php_http_curle_option_set_encodecookies;
1436 ZVAL_BOOL(&opt->defval, 1);
1437 }
1438 if ((opt = php_http_option_register(registry, ZEND_STRL("cookies"), 0, IS_ARRAY))) {
1439 opt->setter = php_http_curle_option_set_cookies;
1440 }
1441
1442 /* cookiesession, don't load session cookies from cookiestore */
1443 if ((opt = php_http_option_register(registry, ZEND_STRL("cookiesession"), CURLOPT_COOKIESESSION, _IS_BOOL))) {
1444 opt->setter = php_http_curle_option_set_cookiesession;
1445 }
1446 /* cookiestore, read initial cookies from that file and store cookies back into that file */
1447 if ((opt = php_http_option_register(registry, ZEND_STRL("cookiestore"), 0, IS_STRING))) {
1448 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1449 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1450 opt->setter = php_http_curle_option_set_cookiestore;
1451 }
1452
1453 /* maxfilesize */
1454 php_http_option_register(registry, ZEND_STRL("maxfilesize"), CURLOPT_MAXFILESIZE, IS_LONG);
1455
1456 /* http protocol version */
1457 php_http_option_register(registry, ZEND_STRL("protocol"), CURLOPT_HTTP_VERSION, IS_LONG);
1458 #if PHP_HTTP_CURL_VERSION(7,64,0)
1459 php_http_option_register(registry, ZEND_STRL("http09_allowed"), CURLOPT_HTTP09_ALLOWED, _IS_BOOL);
1460 #endif
1461
1462 /* timeouts */
1463 if ((opt = php_http_option_register(registry, ZEND_STRL("timeout"), CURLOPT_TIMEOUT_MS, IS_DOUBLE))) {
1464 opt->flags |= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS;
1465 }
1466 if ((opt = php_http_option_register(registry, ZEND_STRL("connecttimeout"), CURLOPT_CONNECTTIMEOUT_MS, IS_DOUBLE))) {
1467 opt->flags |= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS;
1468 Z_DVAL(opt->defval) = 3;
1469 }
1470 #if PHP_HTTP_CURL_VERSION(7,36,0)
1471 if ((opt = php_http_option_register(registry, ZEND_STRL("expect_100_timeout"), CURLOPT_EXPECT_100_TIMEOUT_MS, IS_DOUBLE))) {
1472 opt->flags |= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS;
1473 Z_DVAL(opt->defval) = 1;
1474 }
1475 #endif
1476
1477 /* tcp */
1478 php_http_option_register(registry, ZEND_STRL("tcp_nodelay"), CURLOPT_TCP_NODELAY, _IS_BOOL);
1479 #if PHP_HTTP_CURL_VERSION(7,25,0)
1480 php_http_option_register(registry, ZEND_STRL("tcp_keepalive"), CURLOPT_TCP_KEEPALIVE, _IS_BOOL);
1481 if ((opt = php_http_option_register(registry, ZEND_STRL("tcp_keepidle"), CURLOPT_TCP_KEEPIDLE, IS_LONG))) {
1482 Z_LVAL(opt->defval) = 60;
1483 }
1484 if ((opt = php_http_option_register(registry, ZEND_STRL("tcp_keepintvl"), CURLOPT_TCP_KEEPINTVL, IS_LONG))) {
1485 Z_LVAL(opt->defval) = 60;
1486 }
1487 #endif
1488 #if PHP_HTTP_CURL_VERSION(7,49,0)
1489 # if defined(linux) || defined(__APPLE__)
1490 /* CURLOPT_TCP_FASTOPEN is not supported (yet) on Windows */
1491 php_http_option_register(registry, ZEND_STRL("tcp_fastopen"), CURLOPT_TCP_FASTOPEN, _IS_BOOL);
1492 # endif
1493 #endif
1494
1495 /* ssl */
1496 if (PHP_HTTP_CURL_FEATURE(CURL_VERSION_SSL)) {
1497 php_http_option_t *ssl_opt, *proxy_opt;
1498
1499 if ((ssl_opt = php_http_option_register(registry, ZEND_STRL("ssl"), 0, IS_ARRAY))) {
1500 php_http_options_t *ssl_registry = &ssl_opt->suboptions;
1501
1502 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("cert"), CURLOPT_SSLCERT, IS_STRING))) {
1503 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1504 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1505 }
1506 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("certtype"), CURLOPT_SSLCERTTYPE, IS_STRING))) {
1507 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1508 ZVAL_PSTRING(&opt->defval, "PEM");
1509 }
1510 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("key"), CURLOPT_SSLKEY, IS_STRING))) {
1511 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1512 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1513 }
1514 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("keytype"), CURLOPT_SSLKEYTYPE, IS_STRING))) {
1515 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1516 ZVAL_PSTRING(&opt->defval, "PEM");
1517 }
1518 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("keypasswd"), CURLOPT_SSLKEYPASSWD, IS_STRING))) {
1519 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1520 }
1521 php_http_option_register(ssl_registry, ZEND_STRL("engine"), CURLOPT_SSLENGINE, IS_STRING);
1522 php_http_option_register(ssl_registry, ZEND_STRL("version"), CURLOPT_SSLVERSION, IS_LONG);
1523 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("verifypeer"), CURLOPT_SSL_VERIFYPEER, _IS_BOOL))) {
1524 ZVAL_BOOL(&opt->defval, 1);
1525 }
1526 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("verifyhost"), CURLOPT_SSL_VERIFYHOST, _IS_BOOL))) {
1527 ZVAL_BOOL(&opt->defval, 1);
1528 opt->setter = php_http_curle_option_set_ssl_verifyhost;
1529 }
1530 #if PHP_HTTP_CURL_VERSION(7,41,0) && (PHP_HTTP_HAVE_LIBCURL_OPENSSL || PHP_HTTP_HAVE_LIBCURL_NSS || PHP_HTTP_HAVE_LIBCURL_GNUTLS)
1531 php_http_option_register(ssl_registry, ZEND_STRL("verifystatus"), CURLOPT_SSL_VERIFYSTATUS, _IS_BOOL);
1532 #endif
1533 php_http_option_register(ssl_registry, ZEND_STRL("cipher_list"), CURLOPT_SSL_CIPHER_LIST, IS_STRING);
1534 #if PHP_HTTP_HAVE_LIBCURL_CAINFO
1535 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("cainfo"), CURLOPT_CAINFO, IS_STRING))) {
1536 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1537 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1538 # ifdef PHP_HTTP_CAINFO
1539 ZVAL_PSTRING(&opt->defval, PHP_HTTP_CAINFO);
1540 # endif
1541 }
1542 #endif
1543 #if PHP_HTTP_HAVE_LIBCURL_CAPATH
1544 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("capath"), CURLOPT_CAPATH, IS_STRING))) {
1545 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1546 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1547 # ifdef PHP_HTTP_CAPATH
1548 ZVAL_PSTRING(&opt->defval, PHP_HTTP_CAPATH);
1549 # endif
1550 }
1551 #endif
1552 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("random_file"), CURLOPT_RANDOM_FILE, IS_STRING))) {
1553 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1554 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1555 }
1556 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("egdsocket"), CURLOPT_EGDSOCKET, IS_STRING))) {
1557 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1558 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1559 }
1560 #if PHP_HTTP_CURL_VERSION(7,19,0)
1561 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("issuercert"), CURLOPT_ISSUERCERT, IS_STRING))) {
1562 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1563 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1564 }
1565 # if PHP_HTTP_HAVE_LIBCURL_OPENSSL
1566 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("crlfile"), CURLOPT_CRLFILE, IS_STRING))) {
1567 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1568 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1569 }
1570 # endif
1571 #endif
1572 #if (PHP_HTTP_CURL_VERSION(7,19,1) && PHP_HTTP_HAVE_LIBCURL_OPENSSL) || (PHP_HTTP_CURL_VERSION(7,34,0) && PHP_HTTP_HAVE_LIBCURL_NSS) || (PHP_HTTP_CURL_VERSION(7,42,0) && defined(PHP_HTTP_HAVE_LIBCURL_GNUTLS)) || (PHP_HTTP_CURL_VERSION(7,39,0) && defined(PHP_HTTP_HAVE_LIBCURL_GSKIT))
1573 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("certinfo"), CURLOPT_CERTINFO, _IS_BOOL))) {
1574 ZVAL_FALSE(&opt->defval);
1575 }
1576 #endif
1577 #if PHP_HTTP_CURL_VERSION(7,36,0)
1578 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("enable_npn"), CURLOPT_SSL_ENABLE_NPN, _IS_BOOL))) {
1579 ZVAL_BOOL(&opt->defval, 1);
1580 }
1581 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("enable_alpn"), CURLOPT_SSL_ENABLE_ALPN, _IS_BOOL))) {
1582 ZVAL_BOOL(&opt->defval, 1);
1583 }
1584 #endif
1585 #if PHP_HTTP_CURL_VERSION(7,39,0)
1586 /* FIXME: see http://curl.haxx.se/libcurl/c/CURLOPT_PINNEDPUBLICKEY.html#AVAILABILITY */
1587 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("pinned_publickey"), CURLOPT_PINNEDPUBLICKEY, IS_STRING))) {
1588 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1589 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1590 }
1591 #endif
1592 #if PHP_HTTP_CURL_VERSION(7,21,4) && PHP_HTTP_HAVE_LIBCURL_TLSAUTH_TYPE
1593 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("tlsauthtype"), CURLOPT_TLSAUTH_TYPE, IS_LONG))) {
1594 opt->setter = php_http_curle_option_set_ssl_tlsauthtype;
1595 }
1596 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("tlsauthuser"), CURLOPT_TLSAUTH_USERNAME, IS_STRING))) {
1597 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1598 }
1599 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("tlsauthpass"), CURLOPT_TLSAUTH_PASSWORD, IS_STRING))) {
1600 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1601 }
1602 #endif
1603 #if PHP_HTTP_CURL_VERSION(7,42,0) && (PHP_HTTP_HAVE_LIBCURL_NSS || PHP_HTTP_HAVE_LIBCURL_SECURETRANSPORT)
1604 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("falsestart"), CURLOPT_SSL_FALSESTART, _IS_BOOL))) {
1605 opt->flags |= PHP_HTTP_CURLE_OPTION_IGNORE_RC;
1606 }
1607 #endif
1608 #if PHP_HTTP_CURL_VERSION(7,61,0) && PHP_HTTP_HAVE_LIBCURL_TLS13_CIPHERS
1609 if ((opt = php_http_option_register(ssl_registry, ZEND_STRL("tls13_ciphers"), CURLOPT_TLS13_CIPHERS, IS_STRING))) {
1610 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1611 }
1612 #endif
1613 }
1614
1615 #if PHP_HTTP_CURL_VERSION(7,52,0)
1616 /* proxy_ssl */
1617 if ((proxy_opt = php_http_option_register(registry, ZEND_STRL("proxy_ssl"), 0, IS_ARRAY))) {
1618 php_http_options_t *proxy_registry = &proxy_opt->suboptions;
1619
1620 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("cert"), CURLOPT_PROXY_SSLCERT, IS_STRING))) {
1621 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1622 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1623 }
1624 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("certtype"), CURLOPT_PROXY_SSLCERTTYPE, IS_STRING))) {
1625 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1626 ZVAL_PSTRING(&opt->defval, "PEM");
1627 }
1628 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("key"), CURLOPT_PROXY_SSLKEY, IS_STRING))) {
1629 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1630 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1631 }
1632 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("keytype"), CURLOPT_PROXY_SSLKEYTYPE, IS_STRING))) {
1633 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1634 ZVAL_PSTRING(&opt->defval, "PEM");
1635 }
1636 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("keypasswd"), CURLOPT_PROXY_KEYPASSWD, IS_STRING))) {
1637 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1638 }
1639 php_http_option_register(proxy_registry, ZEND_STRL("version"), CURLOPT_PROXY_SSLVERSION, IS_LONG);
1640 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("verifypeer"), CURLOPT_PROXY_SSL_VERIFYPEER, _IS_BOOL))) {
1641 ZVAL_BOOL(&opt->defval, 1);
1642 }
1643 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("verifyhost"), CURLOPT_PROXY_SSL_VERIFYHOST, _IS_BOOL))) {
1644 ZVAL_BOOL(&opt->defval, 1);
1645 opt->setter = php_http_curle_option_set_ssl_verifyhost;
1646 }
1647 php_http_option_register(proxy_registry, ZEND_STRL("cipher_list"), CURLOPT_PROXY_SSL_CIPHER_LIST, IS_STRING);
1648 # if PHP_HTTP_CURL_VERSION(7,71,0)
1649 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("issuercert"), CURLOPT_PROXY_ISSUERCERT, IS_STRING))) {
1650 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1651 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1652 }
1653 # endif
1654 # if PHP_HTTP_HAVE_LIBCURL_OPENSSL
1655 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("crlfile"), CURLOPT_PROXY_CRLFILE, IS_STRING))) {
1656 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1657 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1658 }
1659 # endif
1660 # if PHP_HTTP_HAVE_LIBCURL_CAINFO
1661 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("cainfo"), CURLOPT_PROXY_CAINFO, IS_STRING))) {
1662 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1663 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1664 # ifdef PHP_HTTP_CAINFO
1665 ZVAL_PSTRING(&opt->defval, PHP_HTTP_CAINFO);
1666 # endif
1667 }
1668 # endif
1669 # if PHP_HTTP_HAVE_LIBCURL_CAPATH
1670 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("capath"), CURLOPT_PROXY_CAPATH, IS_STRING))) {
1671 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1672 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1673 # ifdef PHP_HTTP_CAPATH
1674 ZVAL_PSTRING(&opt->defval, PHP_HTTP_CAPATH);
1675 # endif
1676 }
1677 # endif
1678
1679 # if PHP_HTTP_HAVE_LIBCURL_TLSAUTH_TYPE
1680 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("tlsauthtype"), CURLOPT_PROXY_TLSAUTH_TYPE, IS_LONG))) {
1681 opt->setter = php_http_curle_option_set_ssl_tlsauthtype;
1682 }
1683 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("tlsauthuser"), CURLOPT_PROXY_TLSAUTH_USERNAME, IS_STRING))) {
1684 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1685 }
1686 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("tlsauthpass"), CURLOPT_PROXY_TLSAUTH_PASSWORD, IS_STRING))) {
1687 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1688 }
1689 # endif
1690 # if PHP_HTTP_CURL_VERSION(7,59,0)
1691 /* FIXME: see http://curl.haxx.se/libcurl/c/CURLOPT_PINNEDPUBLICKEY.html#AVAILABILITY */
1692 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("pinned_publickey"), CURLOPT_PROXY_PINNEDPUBLICKEY, IS_STRING))) {
1693 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1694 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1695 }
1696 # endif
1697 # if PHP_HTTP_CURL_VERSION(7,61,0) && PHP_HTTP_HAVE_LIBCURL_TLS13_CIPHERS
1698 if ((opt = php_http_option_register(proxy_registry, ZEND_STRL("tls13_ciphers"), CURLOPT_PROXY_TLS13_CIPHERS, IS_STRING))) {
1699 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
1700 }
1701 # endif
1702 }
1703 #endif
1704 }
1705
1706 #if PHP_HTTP_CURL_VERSION(7,64,1)
1707 # if !PHP_HTTP_HAVE_LIBCURL_ALT_SVC
1708 if (PHP_HTTP_CURL_FEATURE(CURL_VERSION_ALTSVC)) {
1709 # endif
1710 if ((opt = php_http_option_register(registry, ZEND_STRL("altsvc_ctrl"), CURLOPT_ALTSVC_CTRL, IS_LONG))) {
1711 opt->setter = php_http_curle_option_set_altsvc_ctrl;
1712 }
1713 if ((opt = php_http_option_register(registry, ZEND_STRL("altsvc"), CURLOPT_ALTSVC, IS_STRING))) {
1714 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1715 }
1716 # if !PHP_HTTP_HAVE_LIBCURL_ALT_SVC
1717 }
1718 # endif
1719 #endif
1720 #if PHP_HTTP_CURL_VERSION(7,74,0)
1721 # if !PHP_HTTP_HAVE_LIBCURL_HSTS
1722 if (PHP_HTTP_CURL_FEATURE(CURL_VERSION_HSTS)) {
1723 # endif
1724 php_http_option_register(registry, ZEND_STRL("hsts_ctrl"), CURLOPT_HSTS_CTRL, IS_LONG);
1725 if ((opt = php_http_option_register(registry, ZEND_STRL("hsts"), CURLOPT_HSTS, IS_STRING))) {
1726 opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
1727 }
1728 # if !PHP_HTTP_HAVE_LIBCURL_HSTS
1729 }
1730 # endif
1731 #endif
1732 }
1733
1734 static zval *php_http_curle_get_option(php_http_option_t *opt, HashTable *options, void *userdata)
1735 {
1736 php_http_client_curl_handler_t *curl = userdata;
1737 zval *option;
1738
1739 if ((option = php_http_option_get(opt, options, NULL))) {
1740 zval zopt;
1741
1742 ZVAL_DUP(&zopt, option);
1743 convert_to_explicit_type(&zopt, opt->type);
1744 zend_hash_update(&curl->options.cache, opt->name, &zopt);
1745 return zend_hash_find(&curl->options.cache, opt->name);
1746 }
1747 return option;
1748 }
1749
1750 static ZEND_RESULT_CODE php_http_curle_set_option(php_http_option_t *opt, zval *val, void *userdata)
1751 {
1752 php_http_client_curl_handler_t *curl = userdata;
1753 CURL *ch = curl->handle;
1754 zval tmp;
1755 CURLcode rc = CURLE_UNKNOWN_OPTION;
1756 ZEND_RESULT_CODE rv = SUCCESS;
1757
1758 if (!val) {
1759 val = &opt->defval;
1760 }
1761
1762 switch (opt->type) {
1763 case _IS_BOOL:
1764 if (opt->setter) {
1765 rv = opt->setter(opt, val, curl);
1766 } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) (Z_TYPE_P(val) == IS_TRUE))) {
1767 rv = FAILURE;
1768 }
1769 break;
1770
1771 case IS_LONG:
1772 if (opt->setter) {
1773 rv = opt->setter(opt, val, curl);
1774 } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, Z_LVAL_P(val))) {
1775 rv = FAILURE;
1776 }
1777 break;
1778
1779 case IS_STRING:
1780 if (opt->setter) {
1781 rv = opt->setter(opt, val, curl);
1782 } else if (!val || Z_TYPE_P(val) == IS_NULL) {
1783 if (CURLE_OK != (rc = curl_easy_setopt(ch, opt->option, NULL))) {
1784 rv = FAILURE;
1785 }
1786 } else if ((opt->flags & PHP_HTTP_CURLE_OPTION_CHECK_STRLEN) && !Z_STRLEN_P(val)) {
1787 if (CURLE_OK != (rc = curl_easy_setopt(ch, opt->option, NULL))) {
1788 rv = FAILURE;
1789 }
1790 } else if ((opt->flags & PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR) && Z_STRVAL_P(val) && SUCCESS != php_check_open_basedir(Z_STRVAL_P(val))) {
1791 if (CURLE_OK != (rc = curl_easy_setopt(ch, opt->option, NULL))) {
1792 rv = FAILURE;
1793 }
1794 } else if (CURLE_OK != (rc = curl_easy_setopt(ch, opt->option, Z_STRVAL_P(val)))) {
1795 rv = FAILURE;
1796 }
1797 break;
1798
1799 case IS_DOUBLE:
1800 if (opt->flags & PHP_HTTP_CURLE_OPTION_TRANSFORM_MS) {
1801 tmp = *val;
1802 Z_DVAL(tmp) *= 1000;
1803 val = &tmp;
1804 }
1805 if (opt->setter) {
1806 rv = opt->setter(opt, val, curl);
1807 } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) Z_DVAL_P(val))) {
1808 rv = FAILURE;
1809 }
1810 break;
1811
1812 case IS_ARRAY:
1813 if (opt->setter) {
1814 rv = opt->setter(opt, val, curl);
1815 } else if (Z_TYPE_P(val) != IS_NULL) {
1816 rv = php_http_options_apply(&opt->suboptions, Z_ARRVAL_P(val), curl);
1817 }
1818 break;
1819
1820 default:
1821 if (opt->setter) {
1822 rv = opt->setter(opt, val, curl);
1823 } else {
1824 rv = FAILURE;
1825 }
1826 break;
1827 }
1828 if (rv != SUCCESS) {
1829 if (opt->flags & PHP_HTTP_CURLE_OPTION_IGNORE_RC) {
1830 rv = SUCCESS;
1831 } else {
1832 php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_easy_strerror(rc));
1833 }
1834 }
1835 return rv;
1836 }
1837
1838 #if PHP_HTTP_CURL_VERSION(7,30,0) && !PHP_HTTP_CURL_VERSION(7,62,0)
1839 static ZEND_RESULT_CODE php_http_curlm_option_set_pipelining_bl(php_http_option_t *opt, zval *value, void *userdata)
1840 {
1841 php_http_client_t *client = userdata;
1842 php_http_client_curl_t *curl = client->ctx;
1843 CURLM *ch = curl->handle->multi;
1844 HashTable tmp_ht;
1845 char **bl = NULL;
1846
1847 /* array of char *, ending with a NULL */
1848 if (value && Z_TYPE_P(value) != IS_NULL) {
1849 zval *entry;
1850 HashTable *ht = HASH_OF(value);
1851 int c = zend_hash_num_elements(ht);
1852 char **ptr = ecalloc(c + 1, sizeof(char *));
1853
1854 bl = ptr;
1855
1856 zend_hash_init(&tmp_ht, c, NULL, ZVAL_PTR_DTOR, 0);
1857 array_join(ht, &tmp_ht, 0, ARRAY_JOIN_STRINGIFY);
1858
1859 ZEND_HASH_FOREACH_VAL(&tmp_ht, entry)
1860 {
1861 *ptr++ = Z_STRVAL_P(entry);
1862 }
1863 ZEND_HASH_FOREACH_END();
1864 }
1865
1866 if (CURLM_OK != curl_multi_setopt(ch, opt->option, bl)) {
1867 if (bl) {
1868 efree(bl);
1869 zend_hash_destroy(&tmp_ht);
1870 }
1871 return FAILURE;
1872 }
1873
1874 if (bl) {
1875 efree(bl);
1876 zend_hash_destroy(&tmp_ht);
1877 }
1878 return SUCCESS;
1879 }
1880 #endif
1881
1882 static inline ZEND_RESULT_CODE php_http_curlm_use_eventloop(php_http_client_t *h, php_http_client_curl_ops_t *ev_ops, zval *init_data)
1883 {
1884 php_http_client_curl_t *curl = h->ctx;
1885 void *ev_ctx;
1886
1887 if (ev_ops) {
1888 if (!(ev_ctx = ev_ops->init(h, init_data))) {
1889 return FAILURE;
1890 }
1891 curl->ev_ctx = ev_ctx;
1892 curl->ev_ops = ev_ops;
1893 } else {
1894 if (curl->ev_ops) {
1895 if (curl->ev_ctx) {
1896 curl->ev_ops->dtor(&curl->ev_ctx);
1897 }
1898 curl->ev_ops = NULL;
1899 }
1900 }
1901
1902 return SUCCESS;
1903 }
1904
1905 static ZEND_RESULT_CODE php_http_curlm_option_set_use_eventloop(php_http_option_t *opt, zval *value, void *userdata)
1906 {
1907 php_http_client_t *client = userdata;
1908 php_http_client_curl_ops_t *ev_ops = NULL;
1909
1910 if (value && Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_http_client_curl_user_get_class_entry())) {
1911 ev_ops = php_http_client_curl_user_ops_get();
1912 #if PHP_HTTP_HAVE_LIBEVENT
1913 } else if (value && zend_is_true(value)) {
1914 ev_ops = php_http_client_curl_event_ops_get();
1915 #endif
1916 }
1917
1918 return php_http_curlm_use_eventloop(client, ev_ops, value);
1919 }
1920
1921 static ZEND_RESULT_CODE php_http_curlm_option_set_share_cookies(php_http_option_t *opt, zval *value, void *userdata)
1922 {
1923 php_http_client_t *client = userdata;
1924 php_http_client_curl_t *curl = client->ctx;
1925 CURLSHcode rc;
1926
1927 if (Z_TYPE_P(value) == IS_TRUE) {
1928 rc = curl_share_setopt(curl->handle->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_COOKIE);
1929 } else {
1930 rc = curl_share_setopt(curl->handle->share, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_COOKIE);
1931 }
1932
1933 if (CURLSHE_OK != rc) {
1934 php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_share_strerror(rc));
1935 return FAILURE;
1936 }
1937 return SUCCESS;
1938 }
1939
1940 #if PHP_HTTP_HAVE_LIBCURL_SHARE_SSL
1941 static ZEND_RESULT_CODE php_http_curlm_option_set_share_ssl(php_http_option_t *opt, zval *value, void *userdata)
1942 {
1943 php_http_client_t *client = userdata;
1944 php_http_client_curl_t *curl = client->ctx;
1945 CURLSHcode rc;
1946
1947 if (Z_TYPE_P(value) == IS_TRUE) {
1948 rc = curl_share_setopt(curl->handle->share, CURLSHOPT_SHARE, CURL_LOCK_DATA_SSL_SESSION);
1949 } else {
1950 rc = curl_share_setopt(curl->handle->share, CURLSHOPT_UNSHARE, CURL_LOCK_DATA_SSL_SESSION);
1951 }
1952
1953 if (CURLSHE_OK != rc) {
1954 php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_share_strerror(rc));
1955 return FAILURE;
1956 }
1957 return SUCCESS;
1958 }
1959 #endif
1960
1961 static void php_http_curlm_options_init(php_http_options_t *registry)
1962 {
1963 php_http_option_t *opt;
1964
1965 /* set size of connection cache */
1966 if ((opt = php_http_option_register(registry, ZEND_STRL("maxconnects"), CURLMOPT_MAXCONNECTS, IS_LONG))) {
1967 /* -1 == default, 0 == unlimited */
1968 ZVAL_LONG(&opt->defval, -1);
1969 }
1970 /* set max number of connections to a single host */
1971 #if PHP_HTTP_CURL_VERSION(7,30,0)
1972 php_http_option_register(registry, ZEND_STRL("max_host_connections"), CURLMOPT_MAX_HOST_CONNECTIONS, IS_LONG);
1973 #endif
1974 /* max simultaneously open connections */
1975 #if PHP_HTTP_CURL_VERSION(7,30,0)
1976 php_http_option_register(registry, ZEND_STRL("max_total_connections"), CURLMOPT_MAX_TOTAL_CONNECTIONS, IS_LONG);
1977 #endif
1978 #if PHP_HTTP_CURL_VERSION(7,67,0)
1979 if ((opt = php_http_option_register(registry, ZEND_STRL("max_concurrent_streams"), CURLMOPT_MAX_CONCURRENT_STREAMS, IS_LONG))) {
1980 ZVAL_LONG(&opt->defval, 100);
1981 }
1982 #endif
1983
1984 #if !PHP_HTTP_CURL_VERSION(7,62,0)
1985 /* enable/disable HTTP pipelining */
1986 php_http_option_register(registry, ZEND_STRL("pipelining"), CURLMOPT_PIPELINING, _IS_BOOL);
1987 # if PHP_HTTP_CURL_VERSION(7,30,0)
1988 /* maximum number of requests in a pipeline */
1989 if ((opt = php_http_option_register(registry, ZEND_STRL("max_pipeline_length"), CURLMOPT_MAX_PIPELINE_LENGTH, IS_LONG))) {
1990 ZVAL_LONG(&opt->defval, 5);
1991 }
1992 /* chunk length threshold for pipelining */
1993 php_http_option_register(registry, ZEND_STRL("chunk_length_penalty_size"), CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, IS_LONG);
1994 /* size threshold for pipelining penalty */
1995 php_http_option_register(registry, ZEND_STRL("content_length_penalty_size"), CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE, IS_LONG);
1996 /* pipelining server blacklist */
1997 if ((opt = php_http_option_register(registry, ZEND_STRL("pipelining_server_bl"), CURLMOPT_PIPELINING_SERVER_BL, IS_ARRAY))) {
1998 opt->setter = php_http_curlm_option_set_pipelining_bl;
1999 }
2000 /* pipelining host blacklist */
2001 if ((opt = php_http_option_register(registry, ZEND_STRL("pipelining_site_bl"), CURLMOPT_PIPELINING_SITE_BL, IS_ARRAY))) {
2002 opt->setter = php_http_curlm_option_set_pipelining_bl;
2003 }
2004 # endif
2005 #endif
2006 /* events */
2007 if ((opt = php_http_option_register(registry, ZEND_STRL("use_eventloop"), 0, 0))) {
2008 opt->setter = php_http_curlm_option_set_use_eventloop;
2009 }
2010 /* share */
2011 if ((opt = php_http_option_register(registry, ZEND_STRL("share_cookies"), 0, _IS_BOOL))) {
2012 opt->setter = php_http_curlm_option_set_share_cookies;
2013 ZVAL_TRUE(&opt->defval);
2014 }
2015 #if PHP_HTTP_HAVE_LIBCURL_SHARE_SSL
2016 if ((opt = php_http_option_register(registry, ZEND_STRL("share_ssl"), 0, _IS_BOOL))) {
2017 opt->setter = php_http_curlm_option_set_share_ssl;
2018 ZVAL_TRUE(&opt->defval);
2019 }
2020 #endif
2021 }
2022
2023 static ZEND_RESULT_CODE php_http_curlm_set_option(php_http_option_t *opt, zval *val, void *userdata)
2024 {
2025 php_http_client_t *client = userdata;
2026 php_http_client_curl_t *curl = client->ctx;
2027 CURLM *ch = curl->handle->multi;
2028 zval zopt, *orig = val;
2029 CURLMcode rc = CURLM_UNKNOWN_OPTION;
2030 ZEND_RESULT_CODE rv = SUCCESS;
2031
2032 if (!val) {
2033 val = &opt->defval;
2034 } else if (opt->type && Z_TYPE_P(val) != opt->type && !(Z_TYPE_P(val) == IS_NULL && opt->type == IS_ARRAY)) {
2035 ZVAL_DUP(&zopt, val);
2036 convert_to_explicit_type(&zopt, opt->type);
2037
2038 val = &zopt;
2039 }
2040
2041 if (opt->setter) {
2042 rv = opt->setter(opt, val, client);
2043 } else {
2044 switch (opt->type) {
2045 case _IS_BOOL:
2046 if (CURLM_OK != (rc = curl_multi_setopt(ch, opt->option, (long) zend_is_true(val)))) {
2047 rv = FAILURE;
2048 php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_multi_strerror(rc));
2049 }
2050 break;
2051 case IS_LONG:
2052 if (CURLM_OK != (rc = curl_multi_setopt(ch, opt->option, Z_LVAL_P(val)))) {
2053 rv = FAILURE;
2054 php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_multi_strerror(rc));
2055 }
2056 break;
2057 default:
2058 rv = FAILURE;
2059 php_error_docref(NULL, E_NOTICE, "Could not set option %s", opt->name->val);
2060 break;
2061 }
2062 }
2063
2064 if (val && val != orig && val != &opt->defval) {
2065 zval_ptr_dtor(val);
2066 }
2067
2068 return rv;
2069 }
2070
2071 /* client ops */
2072
2073 static ZEND_RESULT_CODE php_http_client_curl_handler_reset(php_http_client_curl_handler_t *handler)
2074 {
2075 php_http_client_curl_t *curl = handler->client->ctx;
2076 CURL *ch = handler->handle;
2077 php_http_curle_storage_t *st;
2078
2079 if ((st = php_http_curle_get_storage(ch))) {
2080 if (st->url) {
2081 pefree(st->url, 1);
2082 st->url = NULL;
2083 }
2084 if (st->cookiestore) {
2085 pefree(st->cookiestore, 1);
2086 st->cookiestore = NULL;
2087 }
2088 st->errorbuffer[0] = '\0';
2089 st->errorcode = 0;
2090 }
2091
2092 curl_easy_setopt(ch, CURLOPT_URL, NULL);
2093 curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, NULL);
2094 curl_easy_setopt(ch, CURLOPT_HTTPGET, 1L);
2095 curl_easy_setopt(ch, CURLOPT_NOBODY, 0L);
2096 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
2097 #if PHP_HTTP_CURL_VERSION(7,19,1)
2098 curl_easy_setopt(ch, CURLOPT_PROXYUSERNAME, NULL);
2099 curl_easy_setopt(ch, CURLOPT_PROXYPASSWORD, NULL);
2100 curl_easy_setopt(ch, CURLOPT_USERNAME, NULL);
2101 curl_easy_setopt(ch, CURLOPT_PASSWORD, NULL);
2102 #endif
2103
2104 #if PHP_HTTP_CURL_VERSION(7,21,3)
2105 if (handler->options.resolve) {
2106 curl_slist_free_all(handler->options.resolve);
2107 handler->options.resolve = NULL;
2108 }
2109 #endif
2110 handler->options.retry.count = 0;
2111 handler->options.retry.delay = 0;
2112 handler->options.redirects = 0;
2113 handler->options.encode_cookies = 1;
2114
2115 if (handler->options.headers) {
2116 curl_slist_free_all(handler->options.headers);
2117 handler->options.headers = NULL;
2118 }
2119 if (handler->options.proxyheaders) {
2120 curl_slist_free_all(handler->options.proxyheaders);
2121 handler->options.proxyheaders = NULL;
2122 }
2123
2124 php_http_buffer_reset(&handler->options.cookies);
2125 php_http_buffer_reset(&handler->options.ranges);
2126
2127 if (php_http_message_body_size(handler->response.body)) {
2128 php_http_message_body_free(&handler->response.body);
2129 handler->response.body = php_http_message_body_init(NULL, NULL);
2130 }
2131 php_http_buffer_reset(&handler->response.headers);
2132
2133 #if ZTS
2134 curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
2135 #endif
2136 curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
2137 curl_easy_setopt(ch, CURLOPT_FILETIME, 1L);
2138 curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1L);
2139 curl_easy_setopt(ch, CURLOPT_VERBOSE, 1L);
2140 curl_easy_setopt(ch, CURLOPT_NOPROGRESS, 0L);
2141 curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, php_http_curle_header_callback);
2142 curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, php_http_curle_body_callback);
2143 curl_easy_setopt(ch, CURLOPT_DEBUGFUNCTION, php_http_curle_raw_callback);
2144 curl_easy_setopt(ch, CURLOPT_READFUNCTION, php_http_curle_read_callback);
2145 curl_easy_setopt(ch, CURLOPT_SEEKFUNCTION, php_http_curle_seek_callback);
2146 #if PHP_HTTP_CURL_VERSION(7,32,0)
2147 curl_easy_setopt(ch, CURLOPT_XFERINFOFUNCTION, php_http_curle_xferinfo_callback);
2148 curl_easy_setopt(ch, CURLOPT_XFERINFODATA, handler);
2149 #else
2150 curl_easy_setopt(ch, CURLOPT_PROGRESSFUNCTION, php_http_curle_progress_callback);
2151 curl_easy_setopt(ch, CURLOPT_PROGRESSDATA, handler);
2152 #endif
2153 curl_easy_setopt(ch, CURLOPT_DEBUGDATA, handler);
2154 curl_easy_setopt(ch, CURLOPT_WRITEDATA, handler);
2155 curl_easy_setopt(ch, CURLOPT_HEADERDATA, handler);
2156 #if DEBUG_COOKIES
2157 fprintf(stderr, "CURLOPT_SHARE: %p\n", curl->handle->share);
2158 #endif
2159 curl_easy_setopt(ch, CURLOPT_SHARE, curl->handle->share);
2160
2161 return SUCCESS;
2162 }
2163
2164 static php_http_client_curl_handler_t *php_http_client_curl_handler_init(php_http_client_t *h, php_resource_factory_t *rf)
2165 {
2166 void *handle;
2167 php_http_client_curl_handler_t *handler;
2168
2169 if (!(handle = php_resource_factory_handle_ctor(rf, NULL))) {
2170 php_error_docref(NULL, E_WARNING, "Failed to initialize curl handle");
2171 return NULL;
2172 }
2173
2174 handler = ecalloc(1, sizeof(*handler));
2175 handler->rf = rf;
2176 handler->client = h;
2177 handler->handle = handle;
2178 handler->response.body = php_http_message_body_init(NULL, NULL);
2179 php_http_buffer_init(&handler->response.headers);
2180 php_http_buffer_init(&handler->options.cookies);
2181 php_http_buffer_init(&handler->options.ranges);
2182 zend_hash_init(&handler->options.cache, 0, NULL, ZVAL_PTR_DTOR, 0);
2183
2184 php_http_client_curl_handler_reset(handler);
2185
2186 return handler;
2187 }
2188
2189
2190 static ZEND_RESULT_CODE php_http_client_curl_handler_prepare(php_http_client_curl_handler_t *curl, php_http_client_enqueue_t *enqueue)
2191 {
2192 size_t body_size;
2193 php_http_message_t *msg = enqueue->request;
2194 php_http_curle_storage_t *storage = php_http_curle_get_storage(curl->handle);
2195
2196 /* request url */
2197 if (!PHP_HTTP_INFO(msg).request.url) {
2198 php_error_docref(NULL, E_WARNING, "Cannot request empty URL");
2199 return FAILURE;
2200 }
2201 storage->errorbuffer[0] = '\0';
2202 if (storage->url) {
2203 pefree(storage->url, 1);
2204 }
2205 php_http_url_to_string(PHP_HTTP_INFO(msg).request.url, &storage->url, NULL, 1);
2206 curl_easy_setopt(curl->handle, CURLOPT_URL, storage->url);
2207
2208 /* apply options */
2209 if (SUCCESS != php_http_options_apply(&php_http_curle_options, enqueue->options, curl)) {
2210 return FAILURE;
2211 }
2212
2213 /* request headers */
2214 php_http_message_update_headers(msg);
2215 if (zend_hash_num_elements(&msg->hdrs)) {
2216 php_http_arrkey_t header_key;
2217 zval *header_val;
2218 zend_string *header_str;
2219 php_http_buffer_t header;
2220 #if !PHP_HTTP_CURL_VERSION(7,23,0)
2221 zval *ct = zend_hash_str_find(&msg->hdrs, ZEND_STRL("Content-Length"));
2222 #endif
2223
2224 php_http_buffer_init(&header);
2225 ZEND_HASH_FOREACH_KEY_VAL(&msg->hdrs, header_key.h, header_key.key, header_val)
2226 {
2227 if (header_key.key) {
2228 #if !PHP_HTTP_CURL_VERSION(7,23,0)
2229 /* avoid duplicate content-length header */
2230 if (ct && ct == header_val) {
2231 continue;
2232 }
2233 #endif
2234 header_str = zval_get_string(header_val);
2235 php_http_buffer_appendf(&header, "%s: %s", header_key.key->val, header_str->val);
2236 php_http_buffer_fix(&header);
2237 curl->options.headers = curl_slist_append(curl->options.headers, header.data);
2238 php_http_buffer_reset(&header);
2239 zend_string_release(header_str);
2240 }
2241 }
2242 ZEND_HASH_FOREACH_END();
2243 php_http_buffer_dtor(&header);
2244 }
2245 curl_easy_setopt(curl->handle, CURLOPT_HTTPHEADER, curl->options.headers);
2246
2247 /* attach request body */
2248 if ((body_size = php_http_message_body_size(msg->body))) {
2249 /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
2250 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
2251 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
2252 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
2253 * does not allow a request body.
2254 */
2255 php_stream_rewind(php_http_message_body_stream(msg->body));
2256 curl_easy_setopt(curl->handle, CURLOPT_SEEKDATA, msg->body);
2257 curl_easy_setopt(curl->handle, CURLOPT_READDATA, msg->body);
2258 curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, body_size);
2259 curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, body_size);
2260 curl_easy_setopt(curl->handle, CURLOPT_POST, 1L);
2261 } else {
2262 curl_easy_setopt(curl->handle, CURLOPT_SEEKDATA, NULL);
2263 curl_easy_setopt(curl->handle, CURLOPT_READDATA, NULL);
2264 curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, 0L);
2265 curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, 0L);
2266 }
2267
2268 /*
2269 * Always use CUSTOMREQUEST, else curl won't send any request body for GET etc.
2270 * See e.g. bug #69313.
2271 *
2272 * Here's what curl does:
2273 * - CURLOPT_HTTPGET: ignore request body
2274 * - CURLOPT_UPLOAD: set "Expect: 100-continue" header
2275 * - CURLOPT_POST: set "Content-Type: application/x-www-form-urlencoded" header
2276 * Now select the least bad.
2277 *
2278 * See also https://tools.ietf.org/html/rfc7231#section-5.1.1
2279 */
2280 if (PHP_HTTP_INFO(msg).request.method) {
2281 switch(php_http_select_str(PHP_HTTP_INFO(msg).request.method, 2, "HEAD", "PUT")) {
2282 case 0:
2283 curl_easy_setopt(curl->handle, CURLOPT_NOBODY, 1L);
2284 break;
2285 case 1:
2286 curl_easy_setopt(curl->handle, CURLOPT_UPLOAD, 1L);
2287 break;
2288 default:
2289 curl_easy_setopt(curl->handle, CURLOPT_CUSTOMREQUEST, PHP_HTTP_INFO(msg).request.method);
2290 }
2291 } else {
2292 php_error_docref(NULL, E_WARNING, "Cannot use empty request method");
2293 return FAILURE;
2294 }
2295
2296 return SUCCESS;
2297 }
2298
2299 static void php_http_client_curl_handler_clear(php_http_client_curl_handler_t *handler)
2300 {
2301 curl_easy_setopt(handler->handle, CURLOPT_NOPROGRESS, 1L);
2302 #if PHP_HTTP_CURL_VERSION(7,32,0)
2303 curl_easy_setopt(handler->handle, CURLOPT_XFERINFOFUNCTION, NULL);
2304 #else
2305 curl_easy_setopt(handler->handle, CURLOPT_PROGRESSFUNCTION, NULL);
2306 #endif
2307 curl_easy_setopt(handler->handle, CURLOPT_VERBOSE, 0L);
2308 curl_easy_setopt(handler->handle, CURLOPT_DEBUGFUNCTION, NULL);
2309 /* see gh issue #84 */
2310 #if DEBUG_COOKIES
2311 fprintf(stderr, "CURLOPT_COOKIELIST: FLUSH\n");
2312 fprintf(stderr, "CURLOPT_SHARE: (null)\n");
2313 #endif
2314 curl_easy_setopt(handler->handle, CURLOPT_COOKIELIST, "FLUSH");
2315 curl_easy_setopt(handler->handle, CURLOPT_SHARE, NULL);
2316 #if PHP_HTTP_CURL_VERSION(7,63,0) && !PHP_HTTP_CURL_VERSION(7,65,0)
2317 {
2318 php_http_curle_storage_t *st = php_http_curle_get_storage(handler->handle);
2319 curl_easy_setopt(handler->handle, CURLOPT_COOKIEJAR, st ? st->cookiestore : NULL);
2320 }
2321 #endif
2322 }
2323
2324 static void php_http_client_curl_handler_dtor(php_http_client_curl_handler_t *handler)
2325 {
2326 php_http_client_curl_handler_clear(handler);
2327
2328 php_resource_factory_handle_dtor(handler->rf, handler->handle);
2329 php_resource_factory_free(&handler->rf);
2330
2331 php_http_message_body_free(&handler->response.body);
2332 php_http_buffer_dtor(&handler->response.headers);
2333 php_http_buffer_dtor(&handler->options.ranges);
2334 php_http_buffer_dtor(&handler->options.cookies);
2335 zend_hash_destroy(&handler->options.cache);
2336
2337 #if PHP_HTTP_CURL_VERSION(7,21,3)
2338 if (handler->options.resolve) {
2339 curl_slist_free_all(handler->options.resolve);
2340 handler->options.resolve = NULL;
2341 }
2342 #endif
2343
2344 if (handler->options.headers) {
2345 curl_slist_free_all(handler->options.headers);
2346 handler->options.headers = NULL;
2347 }
2348
2349 if (handler->options.proxyheaders) {
2350 curl_slist_free_all(handler->options.proxyheaders);
2351 handler->options.proxyheaders = NULL;
2352 }
2353
2354 efree(handler);
2355 }
2356
2357 static php_http_client_t *php_http_client_curl_init(php_http_client_t *h, void *handle)
2358 {
2359 php_http_client_curl_t *curl;
2360
2361 if (!handle && !(handle = php_resource_factory_handle_ctor(h->rf, NULL))) {
2362 php_error_docref(NULL, E_WARNING, "Failed to initialize curl handle");
2363 return NULL;
2364 }
2365
2366 curl = ecalloc(1, sizeof(*curl));
2367 curl->handle = handle;
2368 curl->unfinished = 0;
2369 h->ctx = curl;
2370
2371 return h;
2372 }
2373
2374 static void php_http_client_curl_dtor(php_http_client_t *h)
2375 {
2376 php_http_client_curl_t *curl = h->ctx;
2377
2378 if (curl->ev_ops) {
2379 curl->ev_ops->dtor(&curl->ev_ctx);
2380 curl->ev_ops = NULL;
2381 }
2382 curl->unfinished = 0;
2383
2384 php_resource_factory_handle_dtor(h->rf, curl->handle);
2385
2386 efree(curl);
2387 h->ctx = NULL;
2388 }
2389
2390 static void queue_dtor(php_http_client_enqueue_t *e)
2391 {
2392 php_http_client_curl_handler_t *handler = e->opaque;
2393
2394 if (handler->queue.dtor) {
2395 e->opaque = handler->queue.opaque;
2396 handler->queue.dtor(e);
2397 }
2398 php_http_client_curl_handler_dtor(handler);
2399 }
2400
2401 static void retire_ch(php_persistent_handle_factory_t *f, void **handle)
2402 {
2403 CURL *ch = *handle;
2404 /* erase all cookies */
2405 if (ch) {
2406 curl_easy_reset(ch);
2407 curl_easy_setopt(ch, CURLOPT_COOKIELIST, "ALL");
2408 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, NULL);
2409 }
2410 }
2411
2412 static php_resource_factory_t *create_rf(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
2413 {
2414 php_persistent_handle_factory_t *pf = NULL;
2415 php_resource_factory_t *rf = NULL;
2416 php_http_url_t *url = enqueue->request->http.info.request.url;
2417
2418 if (!url || (!url->host && !url->path)) {
2419 php_error_docref(NULL, E_WARNING, "Cannot request empty URL");
2420 return NULL;
2421 }
2422
2423 /* only if the client itself is setup for persistence */
2424 if (php_resource_factory_is_persistent(h->rf)) {
2425 zend_string *id;
2426 char *id_str = NULL;
2427 size_t id_len;
2428 int port = url->port ? url->port : 80;
2429 zval *zport;
2430 php_persistent_handle_factory_t *phf = h->rf->data;
2431
2432 if ((zport = zend_hash_str_find(enqueue->options, ZEND_STRL("port")))) {
2433 zend_long lport = zval_get_long(zport);
2434
2435 if (lport > 0) {
2436 port = lport;
2437 }
2438 }
2439
2440 id_len = spprintf(&id_str, 0, "%.*s:%s:%d", (int) phf->ident->len, phf->ident->val, STR_PTR(url->host), port);
2441 id = php_http_cs2zs(id_str, id_len);
2442 pf = php_persistent_handle_concede(NULL, PHP_HTTP_G->client.curl.driver.request_name, id, NULL, retire_ch);
2443 zend_string_release(id);
2444 }
2445
2446 if (pf) {
2447 rf = php_persistent_handle_resource_factory_init(NULL, pf);
2448 } else {
2449 rf = php_resource_factory_init(NULL, &php_http_curle_resource_factory_ops, NULL, NULL);
2450 }
2451
2452 return rf;
2453 }
2454
2455 static ZEND_RESULT_CODE php_http_client_curl_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
2456 {
2457 CURLMcode rs;
2458 php_http_client_curl_t *curl = h->ctx;
2459 php_http_client_curl_handler_t *handler;
2460 php_http_client_progress_state_t *progress;
2461 php_resource_factory_t *rf;
2462
2463 rf = create_rf(h, enqueue);
2464 if (!rf) {
2465 return FAILURE;
2466 }
2467
2468 handler = php_http_client_curl_handler_init(h, rf);
2469 if (!handler) {
2470 return FAILURE;
2471 }
2472
2473 if (SUCCESS != php_http_client_curl_handler_prepare(handler, enqueue)) {
2474 php_http_client_curl_handler_dtor(handler);
2475 return FAILURE;
2476 }
2477
2478 handler->queue = *enqueue;
2479 enqueue->opaque = handler;
2480 enqueue->dtor = queue_dtor;
2481
2482 if (CURLM_OK != (rs = curl_multi_add_handle(curl->handle->multi, handler->handle))) {
2483 php_http_client_curl_handler_dtor(handler);
2484 php_error_docref(NULL, E_WARNING, "Could not enqueue request: %s", curl_multi_strerror(rs));
2485 return FAILURE;
2486 }
2487
2488 zend_llist_add_element(&h->requests, enqueue);
2489 ++curl->unfinished;
2490
2491 if (h->callback.progress.func && SUCCESS == php_http_client_getopt(h, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, enqueue->request, &progress)) {
2492 progress->info = "start";
2493 h->callback.progress.func(h->callback.progress.arg, h, &handler->queue, progress);
2494 progress->started = 1;
2495 }
2496
2497 return SUCCESS;
2498 }
2499
2500 static ZEND_RESULT_CODE php_http_client_curl_requeue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
2501 {
2502 CURLMcode rs;
2503 php_http_client_curl_t *curl = h->ctx;
2504 php_http_client_curl_handler_t *handler = enqueue->opaque;
2505 php_http_client_progress_state_t *progress;
2506
2507 if (SUCCESS != php_http_client_curl_handler_reset(handler)) {
2508 return FAILURE;
2509 }
2510
2511 if (SUCCESS != php_http_client_curl_handler_prepare(handler, enqueue)) {
2512 return FAILURE;
2513 }
2514
2515 if (CURLM_OK != (rs = curl_multi_remove_handle(curl->handle->multi, handler->handle))) {
2516 php_error_docref(NULL, E_WARNING, "Could not dequeue request: %s", curl_multi_strerror(rs));
2517 return FAILURE;
2518 }
2519
2520 if (CURLM_OK != (rs = curl_multi_add_handle(curl->handle->multi, handler->handle))) {
2521 zend_llist_del_element(&h->requests, handler->handle, (int (*)(void *, void *)) compare_queue);
2522 php_error_docref(NULL, E_WARNING, "Could not enqueue request: %s", curl_multi_strerror(rs));
2523 return FAILURE;
2524 }
2525
2526 ++curl->unfinished;
2527
2528 if (h->callback.progress.func && SUCCESS == php_http_client_getopt(h, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, enqueue->request, &progress)) {
2529 progress->info = "start";
2530 h->callback.progress.func(h->callback.progress.arg, h, &handler->queue, progress);
2531 progress->started = 1;
2532 }
2533
2534 return SUCCESS;
2535 }
2536
2537 static ZEND_RESULT_CODE php_http_client_curl_dequeue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
2538 {
2539 CURLMcode rs;
2540 php_http_client_curl_t *curl = h->ctx;
2541 php_http_client_curl_handler_t *handler = enqueue->opaque;
2542
2543 if (h->callback.depth && !CG(unclean_shutdown)) {
2544 php_error_docref(NULL, E_WARNING, "Could not dequeue request while executing callbacks");
2545 return FAILURE;
2546 }
2547
2548 php_http_client_curl_handler_clear(handler);
2549 if (CURLM_OK == (rs = curl_multi_remove_handle(curl->handle->multi, handler->handle))) {
2550 zend_llist_del_element(&h->requests, handler->handle, (int (*)(void *, void *)) compare_queue);
2551 return SUCCESS;
2552 } else {
2553 php_error_docref(NULL, E_WARNING, "Could not dequeue request: %s", curl_multi_strerror(rs));
2554 }
2555
2556 return FAILURE;
2557 }
2558
2559 static void php_http_client_curl_reset(php_http_client_t *h)
2560 {
2561 zend_llist_element *next_el, *this_el;
2562
2563 for (this_el = h->requests.head; this_el; this_el = next_el) {
2564 next_el = this_el->next;
2565 php_http_client_curl_dequeue(h, (void *) this_el->data);
2566 }
2567 }
2568
2569 #if PHP_WIN32
2570 # define SELECT_ERROR SOCKET_ERROR
2571 #else
2572 # define SELECT_ERROR -1
2573 #endif
2574
2575 static ZEND_RESULT_CODE php_http_client_curl_wait(php_http_client_t *h, struct timeval *custom_timeout)
2576 {
2577 int MAX;
2578 fd_set R, W, E;
2579 struct timeval timeout;
2580 php_http_client_curl_t *curl = h->ctx;
2581
2582 if (curl->ev_ops) {
2583 return curl->ev_ops->wait(curl->ev_ctx, custom_timeout);
2584 }
2585
2586 FD_ZERO(&R);
2587 FD_ZERO(&W);
2588 FD_ZERO(&E);
2589
2590 if (CURLM_OK == curl_multi_fdset(curl->handle->multi, &R, &W, &E, &MAX)) {
2591 if (custom_timeout && timerisset(custom_timeout)) {
2592 timeout = *custom_timeout;
2593 } else {
2594 php_http_client_curl_get_timeout(curl, 1000, &timeout);
2595 }
2596
2597 if (MAX == -1) {
2598 php_http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / PHP_HTTP_MCROSEC));
2599 return SUCCESS;
2600 } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
2601 return SUCCESS;
2602 }
2603 }
2604 return FAILURE;
2605 }
2606
2607 static int php_http_client_curl_once(php_http_client_t *h)
2608 {
2609 php_http_client_curl_t *curl = h->ctx;
2610
2611 if (!h->callback.depth) {
2612 if (curl->ev_ops) {
2613 curl->ev_ops->once(curl->ev_ctx);
2614 } else {
2615 while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl->handle->multi, &curl->unfinished));
2616 }
2617
2618 php_http_client_curl_responsehandler(h);
2619 }
2620
2621 return curl->unfinished;
2622 }
2623
2624 static ZEND_RESULT_CODE php_http_client_curl_exec(php_http_client_t *h)
2625 {
2626 php_http_client_curl_t *curl = h->ctx;
2627
2628 if (!h->callback.depth) {
2629 if (curl->ev_ops) {
2630 return curl->ev_ops->exec(curl->ev_ctx);
2631 }
2632
2633 while (php_http_client_curl_once(h) && !EG(exception)) {
2634 if (SUCCESS != php_http_client_curl_wait(h, NULL)) {
2635 #if PHP_WIN32
2636 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
2637 php_error_docref(NULL, E_WARNING, "WinSock error: %d", WSAGetLastError());
2638 #else
2639 php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
2640 #endif
2641 return FAILURE;
2642 }
2643 }
2644 }
2645
2646 return SUCCESS;
2647 }
2648
2649 static ZEND_RESULT_CODE php_http_client_curl_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
2650 {
2651 php_http_client_curl_t *curl = h->ctx;
2652
2653 (void) curl;
2654
2655 switch (opt) {
2656 case PHP_HTTP_CLIENT_OPT_CONFIGURATION:
2657 return php_http_options_apply(&php_http_curlm_options, (HashTable *) arg, h);
2658 break;
2659 #if !PHP_HTTP_CURL_VERSION(7,62,0)
2660 case PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING:
2661 if (CURLM_OK != curl_multi_setopt(curl->handle->multi, CURLMOPT_PIPELINING, (long) *((zend_bool *) arg))) {
2662 return FAILURE;
2663 }
2664 break;
2665 #endif
2666 case PHP_HTTP_CLIENT_OPT_USE_EVENTS:
2667 #if PHP_HTTP_HAVE_LIBEVENT
2668 return php_http_curlm_use_eventloop(h, (*(zend_bool *) arg)
2669 ? php_http_client_curl_event_ops_get()
2670 : NULL, NULL);
2671 break;
2672 #endif
2673
2674 default:
2675 return FAILURE;
2676 }
2677 return SUCCESS;
2678 }
2679
2680 static int apply_available_options(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key)
2681 {
2682 php_http_option_t *opt = Z_PTR_P(pDest);
2683 HashTable *ht;
2684 zval entry;
2685 int c;
2686
2687 ht = va_arg(args, HashTable*);
2688
2689 if ((c = zend_hash_num_elements(&opt->suboptions.options))) {
2690 array_init_size(&entry, c);
2691 zend_hash_apply_with_arguments(&opt->suboptions.options, apply_available_options, 1, Z_ARRVAL(entry));
2692 } else {
2693 /* catch deliberate NULL options */
2694 if (Z_TYPE(opt->defval) == IS_STRING && !Z_STRVAL(opt->defval)) {
2695 ZVAL_NULL(&entry);
2696 } else {
2697 ZVAL_ZVAL(&entry, &opt->defval, 1, 0);
2698 }
2699 }
2700
2701 if (hash_key->key) {
2702 zend_hash_update(ht, hash_key->key, &entry);
2703 } else {
2704 zend_hash_index_update(ht, hash_key->h, &entry);
2705 }
2706
2707 return ZEND_HASH_APPLY_KEEP;
2708 }
2709
2710 static ZEND_RESULT_CODE php_http_client_curl_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void **res)
2711 {
2712 php_http_client_enqueue_t *enqueue;
2713
2714 switch (opt) {
2715 case PHP_HTTP_CLIENT_OPT_PROGRESS_INFO:
2716 if ((enqueue = php_http_client_enqueued(h, arg, NULL))) {
2717 php_http_client_curl_handler_t *handler = enqueue->opaque;
2718
2719 *((php_http_client_progress_state_t **) res) = &handler->progress;
2720 return SUCCESS;
2721 }
2722 break;
2723
2724 case PHP_HTTP_CLIENT_OPT_TRANSFER_INFO:
2725 if ((enqueue = php_http_client_enqueued(h, arg, NULL))) {
2726 php_http_client_curl_handler_t *handler = enqueue->opaque;
2727
2728 php_http_curle_get_info(handler->handle, *(HashTable **) res);
2729 return SUCCESS;
2730 }
2731 break;
2732
2733 case PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS:
2734 zend_hash_apply_with_arguments(&php_http_curle_options.options, apply_available_options, 1, *(HashTable **) res);
2735 break;
2736
2737 case PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION:
2738 zend_hash_apply_with_arguments(&php_http_curlm_options.options, apply_available_options, 1, *(HashTable **) res);
2739 break;
2740
2741 default:
2742 break;
2743 }
2744
2745 return FAILURE;
2746 }
2747
2748 static php_http_client_ops_t php_http_client_curl_ops = {
2749 &php_http_curlm_resource_factory_ops,
2750 php_http_client_curl_init,
2751 NULL /* copy */,
2752 php_http_client_curl_dtor,
2753 php_http_client_curl_reset,
2754 php_http_client_curl_exec,
2755 php_http_client_curl_wait,
2756 php_http_client_curl_once,
2757 php_http_client_curl_enqueue,
2758 php_http_client_curl_dequeue,
2759 php_http_client_curl_requeue,
2760 php_http_client_curl_setopt,
2761 php_http_client_curl_getopt
2762 };
2763
2764 php_http_client_ops_t *php_http_client_curl_get_ops(void)
2765 {
2766 return &php_http_client_curl_ops;
2767 }
2768
2769 #define REGISTER_NS_STRING_OR_NULL_CONSTANT(ns, name, str, flags) \
2770 do { \
2771 if ((str) != NULL) { \
2772 REGISTER_NS_STRING_CONSTANT(ns, name, str, flags); \
2773 } else { \
2774 REGISTER_NS_NULL_CONSTANT(ns, name, flags); \
2775 } \
2776 } while (0)
2777
2778 PHP_MINIT_FUNCTION(http_client_curl)
2779 {
2780 curl_version_info_data *info;
2781 php_http_options_t *options;
2782
2783 PHP_HTTP_G->client.curl.driver.driver_name = zend_string_init(ZEND_STRL("curl"), 1);
2784 PHP_HTTP_G->client.curl.driver.client_name = zend_string_init(ZEND_STRL("http\\Client\\Curl"), 1);
2785 PHP_HTTP_G->client.curl.driver.request_name = zend_string_init(ZEND_STRL("http\\Client\\Curl\\Request"), 1);
2786 PHP_HTTP_G->client.curl.driver.client_ops = &php_http_client_curl_ops;
2787
2788 if (SUCCESS != php_http_client_driver_add(&PHP_HTTP_G->client.curl.driver)) {
2789 return FAILURE;
2790 }
2791
2792 if (SUCCESS != php_persistent_handle_provide(PHP_HTTP_G->client.curl.driver.client_name, &php_http_curlm_resource_factory_ops, NULL, NULL)) {
2793 return FAILURE;
2794 }
2795 if (SUCCESS != php_persistent_handle_provide(PHP_HTTP_G->client.curl.driver.request_name, &php_http_curle_resource_factory_ops, NULL, NULL)) {
2796 return FAILURE;
2797 }
2798
2799 if ((options = php_http_options_init(&php_http_curle_options, 1))) {
2800 options->getter = php_http_curle_get_option;
2801 options->setter = php_http_curle_set_option;
2802
2803 php_http_curle_options_init(options);
2804 }
2805 if ((options = php_http_options_init(&php_http_curlm_options, 1))) {
2806 options->getter = php_http_option_get;
2807 options->setter = php_http_curlm_set_option;
2808
2809 php_http_curlm_options_init(options);
2810 }
2811
2812 if ((info = curl_version_info(CURLVERSION_NOW))) {
2813 char tmp_ver[0x20], *tmp_ptr, *tmp_end;
2814 #define tmp_ver_init() do {\
2815 tmp_ver[0] = 0; \
2816 tmp_ptr = &tmp_ver[0]; \
2817 tmp_end = &tmp_ver[sizeof(tmp_ver) - 1]; \
2818 } while (0)
2819
2820 /*
2821 * Feature constants
2822 */
2823 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "FEATURES", info->features, CONST_CS|CONST_PERSISTENT);
2824
2825 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "IPV6", CURL_VERSION_IPV6, CONST_CS|CONST_PERSISTENT);
2826 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "KERBEROS4", CURL_VERSION_KERBEROS4, CONST_CS|CONST_PERSISTENT);
2827 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "SSL", CURL_VERSION_SSL, CONST_CS|CONST_PERSISTENT);
2828 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "LIBZ", CURL_VERSION_LIBZ, CONST_CS|CONST_PERSISTENT);
2829 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "NTLM", CURL_VERSION_NTLM, CONST_CS|CONST_PERSISTENT);
2830 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "GSSNEGOTIATE", CURL_VERSION_GSSNEGOTIATE, CONST_CS|CONST_PERSISTENT);
2831 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "ASYNCHDNS", CURL_VERSION_ASYNCHDNS, CONST_CS|CONST_PERSISTENT);
2832 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "SPNEGO", CURL_VERSION_SPNEGO, CONST_CS|CONST_PERSISTENT);
2833 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "LARGEFILE", CURL_VERSION_LARGEFILE, CONST_CS|CONST_PERSISTENT);
2834 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "IDN", CURL_VERSION_IDN, CONST_CS|CONST_PERSISTENT);
2835 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "SSPI", CURL_VERSION_SSPI, CONST_CS|CONST_PERSISTENT);
2836 #if PHP_HTTP_CURL_VERSION(7,21,4)
2837 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "TLSAUTH_SRP", CURL_VERSION_TLSAUTH_SRP, CONST_CS|CONST_PERSISTENT);
2838 #endif
2839 #if PHP_HTTP_CURL_VERSION(7,22,0)
2840 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "NTLM_WB", CURL_VERSION_NTLM_WB, CONST_CS|CONST_PERSISTENT);
2841 #endif
2842 #if PHP_HTTP_CURL_VERSION(7,33,0)
2843 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "HTTP2", CURL_VERSION_HTTP2, CONST_CS|CONST_PERSISTENT);
2844 #endif
2845 #if PHP_HTTP_CURL_VERSION(7,38,0)
2846 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "GSSAPI", CURL_VERSION_GSSAPI, CONST_CS|CONST_PERSISTENT);
2847 #endif
2848 #if PHP_HTTP_CURL_VERSION(7,40,0)
2849 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "KERBEROS5", CURL_VERSION_KERBEROS5, CONST_CS|CONST_PERSISTENT);
2850 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "UNIX_SOCKETS", CURL_VERSION_UNIX_SOCKETS, CONST_CS|CONST_PERSISTENT);
2851 #endif
2852 #if PHP_HTTP_CURL_VERSION(7,47,0)
2853 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "PSL", CURL_VERSION_PSL, CONST_CS|CONST_PERSISTENT);
2854 #endif
2855 #if PHP_HTTP_CURL_VERSION(7,52,0)
2856 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "HTTPS_PROXY", CURL_VERSION_HTTPS_PROXY, CONST_CS|CONST_PERSISTENT);
2857 #endif
2858 #if PHP_HTTP_CURL_VERSION(7,56,0)
2859 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "MULTI_SSL", CURL_VERSION_MULTI_SSL, CONST_CS|CONST_PERSISTENT);
2860 #endif
2861 #if PHP_HTTP_CURL_VERSION(7,57,0)
2862 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "BROTLI", CURL_VERSION_BROTLI, CONST_CS|CONST_PERSISTENT);
2863 #endif
2864 #if PHP_HTTP_CURL_VERSION(7,64,1)
2865 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "ALTSVC", CURL_VERSION_ALTSVC, CONST_CS|CONST_PERSISTENT);
2866 #endif
2867 #if PHP_HTTP_CURL_VERSION(7,66,0)
2868 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "HTTP3", CURL_VERSION_HTTP3, CONST_CS|CONST_PERSISTENT);
2869 #endif
2870 #if PHP_HTTP_CURL_VERSION(7,72,0)
2871 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "ZSTD", CURL_VERSION_ZSTD, CONST_CS|CONST_PERSISTENT);
2872 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "UNICODE", CURL_VERSION_UNICODE, CONST_CS|CONST_PERSISTENT);
2873 #endif
2874 #if PHP_HTTP_CURL_VERSION(7,74,0)
2875 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "HSTS", CURL_VERSION_HSTS, CONST_CS|CONST_PERSISTENT);
2876 #endif
2877
2878
2879 /*
2880 * Version constants
2881 */
2882 REGISTER_NS_STRING_CONSTANT("http\\Client\\Curl", "VERSIONS", curl_version(), CONST_CS|CONST_PERSISTENT);
2883 REGISTER_NS_STRING_CONSTANT("http\\Client\\Curl\\Versions", "CURL", info->version, CONST_CS|CONST_PERSISTENT);
2884 REGISTER_NS_STRING_OR_NULL_CONSTANT("http\\Client\\Curl\\Versions", "SSL", info->ssl_version, CONST_CS|CONST_PERSISTENT);
2885 REGISTER_NS_STRING_OR_NULL_CONSTANT("http\\Client\\Curl\\Versions", "LIBZ", info->libz_version, CONST_CS|CONST_PERSISTENT);
2886 REGISTER_NS_STRING_OR_NULL_CONSTANT("http\\Client\\Curl\\Versions", "ARES", info->ares, CONST_CS|CONST_PERSISTENT);
2887 REGISTER_NS_STRING_OR_NULL_CONSTANT("http\\Client\\Curl\\Versions", "IDN", info->libidn, CONST_CS|CONST_PERSISTENT);
2888 tmp_ver_init();
2889 if (info->iconv_ver_num) {
2890 tmp_ptr = zend_print_ulong_to_buf(tmp_end, info->iconv_ver_num & 0xf);
2891 tmp_end = tmp_ptr - 1;
2892 tmp_ptr = zend_print_ulong_to_buf(tmp_end, info->iconv_ver_num >> 8);
2893 *tmp_end = '.';
2894 }
2895 REGISTER_NS_STRING_OR_NULL_CONSTANT("http\\Client\\Curl\\Versions", "ICONV", *tmp_ptr ? tmp_ptr : NULL, CONST_CS|CONST_PERSISTENT);
2896 #if PHP_HTTP_CURL_VERSION(7,57,0)
2897 REGISTER_NS_STRING_OR_NULL_CONSTANT("http\\Client\\Curl\\Versions", "BROTLI", info->brotli_version, CONST_CS|CONST_PERSISTENT);
2898 #endif
2899 #if PHP_HTTP_CURL_VERSION(7,66,0)
2900 REGISTER_NS_STRING_OR_NULL_CONSTANT("http\\Client\\Curl\\Versions", "NGHTTP2", info->nghttp2_version, CONST_CS|CONST_PERSISTENT);
2901 REGISTER_NS_STRING_OR_NULL_CONSTANT("http\\Client\\Curl\\Versions", "QUIC", info->quic_version, CONST_CS|CONST_PERSISTENT);
2902 #endif
2903 #if PHP_HTTP_CURL_VERSION(7,70,0)
2904 REGISTER_NS_STRING_OR_NULL_CONSTANT("http\\Client\\Curl\\Versions", "CAINFO", info->cainfo, CONST_CS|CONST_PERSISTENT);
2905 REGISTER_NS_STRING_OR_NULL_CONSTANT("http\\Client\\Curl\\Versions", "CAPATH", info->capath, CONST_CS|CONST_PERSISTENT);
2906 #endif
2907 #if PHP_HTTP_CURL_VERSION(7,72,0)
2908 REGISTER_NS_STRING_OR_NULL_CONSTANT("http\\Client\\Curl\\Versions", "ZSTD", info->zstd_version, CONST_CS|CONST_PERSISTENT);
2909 #endif
2910 #if PHP_HTTP_CURL_VERSION(7,75,0)
2911 REGISTER_NS_STRING_OR_NULL_CONSTANT("http\\Client\\Curl\\Versions", "HYPER", info->hyper_version, CONST_CS|CONST_PERSISTENT);
2912 #endif
2913
2914 }
2915
2916 /*
2917 * HTTP Protocol Version Constants
2918 */
2919 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0, CONST_CS|CONST_PERSISTENT);
2920 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1, CONST_CS|CONST_PERSISTENT);
2921 #if PHP_HTTP_CURL_VERSION(7,33,0)
2922 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_2_0", CURL_HTTP_VERSION_2_0, CONST_CS|CONST_PERSISTENT);
2923 #endif
2924 #if PHP_HTTP_CURL_VERSION(7,47,0)
2925 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_2TLS", CURL_HTTP_VERSION_2TLS, CONST_CS|CONST_PERSISTENT);
2926 #endif
2927 #if PHP_HTTP_CURL_VERSION(7,49,0)
2928 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_2_PRIOR_KNOWLEDGE", CURL_HTTP_VERSION_2_PRIOR_KNOWLEDGE, CONST_CS|CONST_PERSISTENT);
2929 #endif
2930 #if PHP_HTTP_CURL_VERSION(7,66,0)
2931 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_3", CURL_HTTP_VERSION_3, CONST_CS|CONST_PERSISTENT);
2932 #endif
2933 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_ANY", CURL_HTTP_VERSION_NONE, CONST_CS|CONST_PERSISTENT);
2934
2935 /*
2936 * SSL Version Constants
2937 */
2938 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1, CONST_CS|CONST_PERSISTENT);
2939 #if PHP_HTTP_CURL_VERSION(7,34,0)
2940 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_0", CURL_SSLVERSION_TLSv1_0, CONST_CS|CONST_PERSISTENT);
2941 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_1", CURL_SSLVERSION_TLSv1_1, CONST_CS|CONST_PERSISTENT);
2942 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_2", CURL_SSLVERSION_TLSv1_2, CONST_CS|CONST_PERSISTENT);
2943 #endif
2944 #if PHP_HTTP_CURL_VERSION(7,52,0)
2945 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_3", CURL_SSLVERSION_TLSv1_3, CONST_CS|CONST_PERSISTENT);
2946 #endif
2947 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2, CONST_CS|CONST_PERSISTENT);
2948 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3, CONST_CS|CONST_PERSISTENT);
2949 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT, CONST_CS|CONST_PERSISTENT);
2950 #if PHP_HTTP_CURL_VERSION(7,21,4) && PHP_HTTP_HAVE_LIBCURL_TLSAUTH_TYPE
2951 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "TLSAUTH_SRP", CURL_TLSAUTH_SRP, CONST_CS|CONST_PERSISTENT);
2952 #endif
2953
2954 #if PHP_HTTP_CURL_VERSION(7,54,0)
2955 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_MAX_DEFAULT", CURL_SSLVERSION_MAX_DEFAULT, CONST_CS|CONST_PERSISTENT);
2956 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_MAX_TLSv1_0", CURL_SSLVERSION_MAX_TLSv1_0, CONST_CS|CONST_PERSISTENT);
2957 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_MAX_TLSv1_1", CURL_SSLVERSION_MAX_TLSv1_1, CONST_CS|CONST_PERSISTENT);
2958 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_MAX_TLSv1_2", CURL_SSLVERSION_MAX_TLSv1_2, CONST_CS|CONST_PERSISTENT);
2959 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_MAX_TLSv1_3", CURL_SSLVERSION_MAX_TLSv1_3, CONST_CS|CONST_PERSISTENT);
2960 #endif
2961
2962 /*
2963 * DNS IPvX resolving
2964 */
2965 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_V4", CURL_IPRESOLVE_V4, CONST_CS|CONST_PERSISTENT);
2966 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_V6", CURL_IPRESOLVE_V6, CONST_CS|CONST_PERSISTENT);
2967 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER, CONST_CS|CONST_PERSISTENT);
2968
2969 /*
2970 * Auth Constants
2971 */
2972 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_NONE", CURLAUTH_NONE, CONST_CS|CONST_PERSISTENT);
2973 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_BASIC", CURLAUTH_BASIC, CONST_CS|CONST_PERSISTENT);
2974 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_DIGEST", CURLAUTH_DIGEST, CONST_CS|CONST_PERSISTENT);
2975 #if PHP_HTTP_CURL_VERSION(7,19,3)
2976 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_DIGEST_IE", CURLAUTH_DIGEST_IE, CONST_CS|CONST_PERSISTENT);
2977 #endif
2978 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_NTLM", CURLAUTH_NTLM, CONST_CS|CONST_PERSISTENT);
2979 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE, CONST_CS|CONST_PERSISTENT);
2980 #if PHP_HTTP_CURL_VERSION(7,38,0)
2981 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_SPNEGO", CURLAUTH_NEGOTIATE, CONST_CS|CONST_PERSISTENT);
2982 #endif
2983 #if PHP_HTTP_CURL_VERSION(7,61,0)
2984 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_BEARER", CURLAUTH_BEARER, CONST_CS|CONST_PERSISTENT);
2985 #endif
2986 #if PHP_HTTP_CURL_VERSION(7,75,0)
2987 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AWS_SIGV4", CURLAUTH_AWS_SIGV4, CONST_CS|CONST_PERSISTENT);
2988 #endif
2989 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_ANY", CURLAUTH_ANY, CONST_CS|CONST_PERSISTENT);
2990
2991 /*
2992 * Proxy Type Constants
2993 */
2994 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS4", CURLPROXY_SOCKS4, CONST_CS|CONST_PERSISTENT);
2995 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS4A", CURLPROXY_SOCKS4A, CONST_CS|CONST_PERSISTENT);
2996 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS5_HOSTNAME", CURLPROXY_SOCKS5_HOSTNAME, CONST_CS|CONST_PERSISTENT);
2997 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS5", CURLPROXY_SOCKS5, CONST_CS|CONST_PERSISTENT);
2998 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_HTTP", CURLPROXY_HTTP, CONST_CS|CONST_PERSISTENT);
2999 #if PHP_HTTP_CURL_VERSION(7,19,4)
3000 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_HTTP_1_0", CURLPROXY_HTTP_1_0, CONST_CS|CONST_PERSISTENT);
3001 #endif
3002
3003 /*
3004 * Post Redirection Constants
3005 */
3006 #if PHP_HTTP_CURL_VERSION(7,19,1)
3007 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_301", CURL_REDIR_POST_301, CONST_CS|CONST_PERSISTENT);
3008 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_302", CURL_REDIR_POST_302, CONST_CS|CONST_PERSISTENT);
3009 #if PHP_HTTP_CURL_VERSION(7,26,0)
3010 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_303", CURL_REDIR_POST_303, CONST_CS|CONST_PERSISTENT);
3011 #endif
3012 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_ALL", CURL_REDIR_POST_ALL, CONST_CS|CONST_PERSISTENT);
3013 #endif
3014
3015 #if PHP_HTTP_CURL_VERSION(7,64,1)
3016 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "ALTSVC_READONLYFILE", CURLALTSVC_READONLYFILE, CONST_CS|CONST_PERSISTENT);
3017 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "ALTSVC_H1", CURLALTSVC_H1, CONST_CS|CONST_PERSISTENT);
3018 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "ALTSVC_H2", CURLALTSVC_H2, CONST_CS|CONST_PERSISTENT);
3019 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "ALTSVC_H3", CURLALTSVC_H3, CONST_CS|CONST_PERSISTENT);
3020 #endif
3021 #if PHP_HTTP_CURL_VERSION(7,74,0)
3022 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HSTS_ENABLE", CURLHSTS_ENABLE, CONST_CS|CONST_PERSISTENT);
3023 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HSTS_READONLYFILE", CURLHSTS_READONLYFILE, CONST_CS|CONST_PERSISTENT);
3024 #endif
3025 return SUCCESS;
3026 }
3027
3028 PHP_MSHUTDOWN_FUNCTION(http_client_curl)
3029 {
3030 php_persistent_handle_cleanup(PHP_HTTP_G->client.curl.driver.client_name, NULL);
3031 php_persistent_handle_cleanup(PHP_HTTP_G->client.curl.driver.request_name, NULL);
3032 zend_string_release(PHP_HTTP_G->client.curl.driver.client_name);
3033 zend_string_release(PHP_HTTP_G->client.curl.driver.request_name);
3034 zend_string_release(PHP_HTTP_G->client.curl.driver.driver_name);
3035
3036 php_http_options_dtor(&php_http_curle_options);
3037 php_http_options_dtor(&php_http_curlm_options);
3038
3039 return SUCCESS;
3040 }
3041
3042 #endif /* PHP_HTTP_HAVE_LIBCURL */
3043
3044 /*
3045 * Local variables:
3046 * tab-width: 4
3047 * c-basic-offset: 4
3048 * End:
3049 * vim600: noet sw=4 ts=4 fdm=marker
3050 * vim<600: noet sw=4 ts=4
3051 */