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