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