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