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