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