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