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