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