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