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