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