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