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