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