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