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