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