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