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