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