2 +--------------------------------------------------------------------+
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 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
14 #include "php_http_client.h"
15 #include "php_http_client_curl_event.h"
17 #if PHP_HTTP_HAVE_CURL
19 #ifdef PHP_HTTP_HAVE_OPENSSL
20 # include <openssl/ssl.h>
22 #ifdef PHP_HTTP_HAVE_GNUTLS
26 typedef struct php_http_client_curl_handler
{
28 php_resource_factory_t
*rf
;
29 php_http_client_t
*client
;
30 php_http_client_progress_state_t progress
;
31 php_http_client_enqueue_t queue
;
34 php_http_buffer_t headers
;
35 php_http_message_body_t
*body
;
41 struct curl_slist
*proxyheaders
;
42 struct curl_slist
*headers
;
43 struct curl_slist
*resolve
;
44 php_http_buffer_t cookies
;
45 php_http_buffer_t ranges
;
48 unsigned range_request
:1;
49 unsigned encode_cookies
:1;
58 } php_http_client_curl_handler_t
;
60 typedef struct php_http_curle_storage
{
64 char errorbuffer
[0x100];
65 } php_http_curle_storage_t
;
67 static inline php_http_curle_storage_t
*php_http_curle_get_storage(CURL
*ch
) {
68 php_http_curle_storage_t
*st
= NULL
;
70 curl_easy_getinfo(ch
, CURLINFO_PRIVATE
, &st
);
73 st
= pecalloc(1, sizeof(*st
), 1);
74 curl_easy_setopt(ch
, CURLOPT_PRIVATE
, st
);
75 curl_easy_setopt(ch
, CURLOPT_ERRORBUFFER
, st
->errorbuffer
);
81 static void *php_http_curle_ctor(void *opaque
, void *init_arg TSRMLS_DC
)
85 if ((ch
= curl_easy_init())) {
86 php_http_curle_get_storage(ch
);
92 static void *php_http_curle_copy(void *opaque
, void *handle TSRMLS_DC
)
96 if ((ch
= curl_easy_duphandle(handle
))) {
98 php_http_curle_get_storage(ch
);
104 static void php_http_curle_dtor(void *opaque
, void *handle TSRMLS_DC
)
106 php_http_curle_storage_t
*st
= php_http_curle_get_storage(handle
);
108 curl_easy_cleanup(handle
);
114 if (st
->cookiestore
) {
115 pefree(st
->cookiestore
, 1);
121 static php_resource_factory_ops_t php_http_curle_resource_factory_ops
= {
127 static void *php_http_curlm_ctor(void *opaque
, void *init_arg TSRMLS_DC
)
129 php_http_client_curl_handle_t
*curl
= calloc(1, sizeof(*curl
));
131 if (!(curl
->multi
= curl_multi_init())) {
135 if (!(curl
->share
= curl_share_init())) {
136 curl_multi_cleanup(curl
->multi
);
140 curl_share_setopt(curl
->share
, CURLSHOPT_SHARE
, CURL_LOCK_DATA_COOKIE
);
141 curl_share_setopt(curl
->share
, CURLSHOPT_SHARE
, CURL_LOCK_DATA_SSL_SESSION
);
145 static void php_http_curlm_dtor(void *opaque
, void *handle TSRMLS_DC
)
147 php_http_client_curl_handle_t
*curl
= handle
;
149 curl_share_cleanup(curl
->share
);
150 curl_multi_cleanup(curl
->multi
);
154 static php_resource_factory_ops_t php_http_curlm_resource_factory_ops
= {
162 static size_t php_http_curle_read_callback(void *data
, size_t len
, size_t n
, void *ctx
)
164 php_http_message_body_t
*body
= ctx
;
166 if (body
&& body
->stream_id
) {
167 php_stream
*s
= php_http_message_body_stream(body
);
170 TSRMLS_FETCH_FROM_CTX(body
->ts
);
171 return php_stream_read(s
, data
, len
* n
);
177 #if PHP_HTTP_CURL_VERSION(7,32,0)
178 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 static int php_http_curle_progress_callback(void *ctx
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
183 php_http_client_curl_handler_t
*h
= ctx
;
184 zend_bool update
= 0;
186 if (h
->progress
.dl
.total
!= dltotal
187 || h
->progress
.dl
.now
!= dlnow
188 || h
->progress
.ul
.total
!= ultotal
189 || h
->progress
.ul
.now
!= ulnow
193 h
->progress
.dl
.total
= dltotal
;
194 h
->progress
.dl
.now
= dlnow
;
195 h
->progress
.ul
.total
= ultotal
;
196 h
->progress
.ul
.now
= ulnow
;
199 if (update
&& h
->client
->callback
.progress
.func
) {
200 h
->client
->callback
.progress
.func(h
->client
->callback
.progress
.arg
, h
->client
, &h
->queue
, &h
->progress
);
206 static int php_http_curle_seek_callback(void *userdata
, curl_off_t offset
, int origin
)
208 php_http_message_body_t
*body
= userdata
;
209 TSRMLS_FETCH_FROM_CTX(body
->ts
);
214 if (0 == php_stream_seek(php_http_message_body_stream(body
), offset
, origin
)) {
220 static int php_http_curle_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
222 php_http_client_curl_handler_t
*h
= ctx
;
227 if (data
[0] == '-') {
228 } else if (php_memnstr(data
, ZEND_STRL("Adding handle:"), data
+ length
)) {
229 h
->progress
.info
= "setup";
230 } else if (php_memnstr(data
, ZEND_STRL("addHandle"), data
+ length
)) {
231 h
->progress
.info
= "setup";
232 } else if (php_memnstr(data
, ZEND_STRL("About to connect"), data
+ length
)) {
233 h
->progress
.info
= "resolve";
234 } else if (php_memnstr(data
, ZEND_STRL("Trying"), data
+ length
)) {
235 h
->progress
.info
= "connect";
236 } else if (php_memnstr(data
, ZEND_STRL("Found bundle for host"), data
+ length
)) {
237 h
->progress
.info
= "connect";
238 } else if (php_memnstr(data
, ZEND_STRL("Connected"), data
+ length
)) {
239 h
->progress
.info
= "connected";
240 } else if (php_memnstr(data
, ZEND_STRL("Re-using existing connection!"), data
+ length
)) {
241 h
->progress
.info
= "connected";
242 } else if (php_memnstr(data
, ZEND_STRL("blacklisted"), data
+ length
)) {
243 h
->progress
.info
= "blacklist check";
244 } else if (php_memnstr(data
, ZEND_STRL("SSL"), data
+ length
)) {
245 h
->progress
.info
= "ssl negotiation";
246 } else if (php_memnstr(data
, ZEND_STRL("upload"), data
+ length
)) {
247 h
->progress
.info
= "uploaded";
248 } else if (php_memnstr(data
, ZEND_STRL("left intact"), data
+ length
)) {
249 h
->progress
.info
= "not disconnected";
250 } else if (php_memnstr(data
, ZEND_STRL("closed"), data
+ length
)) {
251 h
->progress
.info
= "disconnected";
252 } else if (php_memnstr(data
, ZEND_STRL("Issue another request"), data
+ length
)) {
253 h
->progress
.info
= "redirect";
254 } else if (php_memnstr(data
, ZEND_STRL("Operation timed out"), data
+ length
)) {
255 h
->progress
.info
= "timeout";
258 h
->progress
.info
= data
;
259 data
[length
- 1] = '\0';
262 if (h
->client
->callback
.progress
.func
) {
263 h
->client
->callback
.progress
.func(h
->client
->callback
.progress
.arg
, h
->client
, &h
->queue
, &h
->progress
);
266 case CURLINFO_HEADER_OUT
:
267 case CURLINFO_DATA_OUT
:
268 case CURLINFO_SSL_DATA_OUT
:
269 h
->progress
.info
= "send";
271 case CURLINFO_HEADER_IN
:
272 case CURLINFO_DATA_IN
:
273 case CURLINFO_SSL_DATA_IN
:
274 h
->progress
.info
= "receive";
282 _dpf(type
, data
, length
);
288 static int php_http_curle_header_callback(char *data
, size_t n
, size_t l
, void *arg
)
290 php_http_client_curl_handler_t
*h
= arg
;
292 return php_http_buffer_append(&h
->response
.headers
, data
, n
* l
);
295 static int php_http_curle_body_callback(char *data
, size_t n
, size_t l
, void *arg
)
297 php_http_client_curl_handler_t
*h
= arg
;
299 return php_http_message_body_append(h
->response
.body
, data
, n
*l
);
302 static ZEND_RESULT_CODE
php_http_curle_get_info(CURL
*ch
, HashTable
*info
)
307 struct curl_slist
*s
, *p
;
308 zval
*subarray
, array
;
309 INIT_PZVAL_ARRAY(&array
, info
);
311 /* BEGIN::CURLINFO */
312 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_EFFECTIVE_URL
, &c
)) {
313 add_assoc_string_ex(&array
, "effective_url", sizeof("effective_url"), c
? c
: "", 1);
315 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_RESPONSE_CODE
, &l
)) {
316 add_assoc_long_ex(&array
, "response_code", sizeof("response_code"), l
);
318 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_TOTAL_TIME
, &d
)) {
319 add_assoc_double_ex(&array
, "total_time", sizeof("total_time"), d
);
321 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NAMELOOKUP_TIME
, &d
)) {
322 add_assoc_double_ex(&array
, "namelookup_time", sizeof("namelookup_time"), d
);
324 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONNECT_TIME
, &d
)) {
325 add_assoc_double_ex(&array
, "connect_time", sizeof("connect_time"), d
);
327 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRETRANSFER_TIME
, &d
)) {
328 add_assoc_double_ex(&array
, "pretransfer_time", sizeof("pretransfer_time"), d
);
330 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_UPLOAD
, &d
)) {
331 add_assoc_double_ex(&array
, "size_upload", sizeof("size_upload"), d
);
333 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_DOWNLOAD
, &d
)) {
334 add_assoc_double_ex(&array
, "size_download", sizeof("size_download"), d
);
336 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_DOWNLOAD
, &d
)) {
337 add_assoc_double_ex(&array
, "speed_download", sizeof("speed_download"), d
);
339 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_UPLOAD
, &d
)) {
340 add_assoc_double_ex(&array
, "speed_upload", sizeof("speed_upload"), d
);
342 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HEADER_SIZE
, &l
)) {
343 add_assoc_long_ex(&array
, "header_size", sizeof("header_size"), l
);
345 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REQUEST_SIZE
, &l
)) {
346 add_assoc_long_ex(&array
, "request_size", sizeof("request_size"), l
);
348 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_VERIFYRESULT
, &l
)) {
349 add_assoc_long_ex(&array
, "ssl_verifyresult", sizeof("ssl_verifyresult"), l
);
351 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_FILETIME
, &l
)) {
352 add_assoc_long_ex(&array
, "filetime", sizeof("filetime"), l
);
354 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
355 add_assoc_double_ex(&array
, "content_length_download", sizeof("content_length_download"), d
);
357 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_UPLOAD
, &d
)) {
358 add_assoc_double_ex(&array
, "content_length_upload", sizeof("content_length_upload"), d
);
360 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_STARTTRANSFER_TIME
, &d
)) {
361 add_assoc_double_ex(&array
, "starttransfer_time", sizeof("starttransfer_time"), d
);
363 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_TYPE
, &c
)) {
364 add_assoc_string_ex(&array
, "content_type", sizeof("content_type"), c
? c
: "", 1);
366 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_TIME
, &d
)) {
367 add_assoc_double_ex(&array
, "redirect_time", sizeof("redirect_time"), d
);
369 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_COUNT
, &l
)) {
370 add_assoc_long_ex(&array
, "redirect_count", sizeof("redirect_count"), l
);
372 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTP_CONNECTCODE
, &l
)) {
373 add_assoc_long_ex(&array
, "connect_code", sizeof("connect_code"), l
);
375 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTPAUTH_AVAIL
, &l
)) {
376 add_assoc_long_ex(&array
, "httpauth_avail", sizeof("httpauth_avail"), l
);
378 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PROXYAUTH_AVAIL
, &l
)) {
379 add_assoc_long_ex(&array
, "proxyauth_avail", sizeof("proxyauth_avail"), l
);
381 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_OS_ERRNO
, &l
)) {
382 add_assoc_long_ex(&array
, "os_errno", sizeof("os_errno"), l
);
384 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NUM_CONNECTS
, &l
)) {
385 add_assoc_long_ex(&array
, "num_connects", sizeof("num_connects"), l
);
387 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_ENGINES
, &s
)) {
388 MAKE_STD_ZVAL(subarray
);
389 array_init(subarray
);
390 for (p
= s
; p
; p
= p
->next
) {
392 add_next_index_string(subarray
, p
->data
, 1);
395 add_assoc_zval_ex(&array
, "ssl_engines", sizeof("ssl_engines"), subarray
);
396 curl_slist_free_all(s
);
398 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_URL
, &c
)) {
399 add_assoc_string_ex(&array
, "redirect_url", sizeof("redirect_url"), c
? c
: "", 1);
401 #if PHP_HTTP_CURL_VERSION(7,19,0)
402 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRIMARY_IP
, &c
)) {
403 add_assoc_string_ex(&array
, "primary_ip", sizeof("primary_ip"), c
? c
: "", 1);
406 #if PHP_HTTP_CURL_VERSION(7,19,0)
407 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_APPCONNECT_TIME
, &d
)) {
408 add_assoc_double_ex(&array
, "appconnect_time", sizeof("appconnect_time"), d
);
411 #if PHP_HTTP_CURL_VERSION(7,19,4)
412 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONDITION_UNMET
, &l
)) {
413 add_assoc_long_ex(&array
, "condition_unmet", sizeof("condition_unmet"), l
);
416 #if PHP_HTTP_CURL_VERSION(7,21,0)
417 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRIMARY_PORT
, &l
)) {
418 add_assoc_long_ex(&array
, "primary_port", sizeof("primary_port"), l
);
421 #if PHP_HTTP_CURL_VERSION(7,21,0)
422 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_LOCAL_IP
, &c
)) {
423 add_assoc_string_ex(&array
, "local_ip", sizeof("local_ip"), c
? c
: "", 1);
426 #if PHP_HTTP_CURL_VERSION(7,21,0)
427 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_LOCAL_PORT
, &l
)) {
428 add_assoc_long_ex(&array
, "local_port", sizeof("local_port"), l
);
434 #if PHP_HTTP_CURL_VERSION(7,34,0)
437 struct curl_tlssessioninfo
*ti
;
439 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_TLS_SESSION
, &ti
)) {
442 MAKE_STD_ZVAL(subarray
);
444 MAKE_STD_ZVAL(ti_array
);
445 array_init(ti_array
);
447 switch (ti
->backend
) {
448 case CURLSSLBACKEND_NONE
:
451 case CURLSSLBACKEND_OPENSSL
:
453 #ifdef PHP_HTTP_HAVE_OPENSSL
455 SSL_CTX
*ctx
= ti
->internals
;
457 array_init(subarray
);
458 add_assoc_long_ex(subarray
, ZEND_STRS("number"), SSL_CTX_sess_number(ctx
));
459 add_assoc_long_ex(subarray
, ZEND_STRS("connect"), SSL_CTX_sess_connect(ctx
));
460 add_assoc_long_ex(subarray
, ZEND_STRS("connect_good"), SSL_CTX_sess_connect_good(ctx
));
461 add_assoc_long_ex(subarray
, ZEND_STRS("connect_renegotiate"), SSL_CTX_sess_connect_renegotiate(ctx
));
462 add_assoc_long_ex(subarray
, ZEND_STRS("hits"), SSL_CTX_sess_hits(ctx
));
463 add_assoc_long_ex(subarray
, ZEND_STRS("cache_full"), SSL_CTX_sess_cache_full(ctx
));
467 case CURLSSLBACKEND_GNUTLS
:
469 #ifdef PHP_HTTP_HAVE_GNUTLS
471 gnutls_session_t sess
= ti
->internals
;
474 array_init(subarray
);
475 if ((desc
= gnutls_session_get_desc(sess
))) {
476 add_assoc_string_ex(subarray
, ZEND_STRS("desc"), desc
, 1);
479 add_assoc_bool_ex(subarray
, ZEND_STRS("resumed"), gnutls_session_is_resumed(sess
));
483 case CURLSSLBACKEND_NSS
:
486 #if !PHP_HTTP_CURL_VERSION(7,39,0)
487 case CURLSSLBACKEND_QSOSSL
:
491 case CURLSSLBACKEND_GSKIT
:
495 case CURLSSLBACKEND_POLARSSL
:
496 backend
= "polarssl";
498 case CURLSSLBACKEND_CYASSL
:
501 case CURLSSLBACKEND_SCHANNEL
:
502 backend
= "schannel";
504 case CURLSSLBACKEND_DARWINSSL
:
505 backend
= "darwinssl";
510 add_assoc_string_ex(ti_array
, ZEND_STRS("backend"), estrdup(backend
), 0);
511 add_assoc_zval_ex(ti_array
, ZEND_STRS("internals"), subarray
);
512 add_assoc_zval_ex(&array
, "tls_session", sizeof("tls_session"), ti_array
);
517 #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))
521 struct curl_certinfo
*ci
;
522 char *colon
, *keyname
;
524 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CERTINFO
, &ci
)) {
525 MAKE_STD_ZVAL(ci_array
);
526 array_init(ci_array
);
528 for (i
= 0; i
< ci
->num_of_certs
; ++i
) {
531 MAKE_STD_ZVAL(subarray
);
532 array_init(subarray
);
533 for (p
= s
; p
; p
= p
->next
) {
535 if ((colon
= strchr(p
->data
, ':'))) {
536 keyname
= estrndup(p
->data
, colon
- p
->data
);
537 add_assoc_string_ex(subarray
, keyname
, colon
- p
->data
+ 1, colon
+ 1, 1);
540 add_next_index_string(subarray
, p
->data
, 1);
544 add_next_index_zval(ci_array
, subarray
);
546 add_assoc_zval_ex(&array
, "certinfo", sizeof("certinfo"), ci_array
);
551 php_http_curle_storage_t
*st
= php_http_curle_get_storage(ch
);
553 add_assoc_long_ex(&array
, "curlcode", sizeof("curlcode"), st
->errorcode
);
554 add_assoc_string_ex(&array
, "error", sizeof("error"), st
->errorbuffer
, 1);
560 static int compare_queue(php_http_client_enqueue_t
*e
, void *handle
)
562 return handle
== ((php_http_client_curl_handler_t
*) e
->opaque
)->handle
;
565 static php_http_message_t
*php_http_curlm_responseparser(php_http_client_curl_handler_t
*h TSRMLS_DC
)
567 php_http_message_t
*response
;
568 php_http_header_parser_t parser
;
571 response
= php_http_message_init(NULL
, 0, h
->response
.body TSRMLS_CC
);
572 php_http_header_parser_init(&parser TSRMLS_CC
);
573 while (h
->response
.headers
.used
) {
574 php_http_header_parser_state_t st
= php_http_header_parser_parse(&parser
,
575 &h
->response
.headers
, PHP_HTTP_HEADER_PARSER_CLEANUP
, &response
->hdrs
,
576 (php_http_info_callback_t
) php_http_message_info_callback
, (void *) &response
);
577 if (PHP_HTTP_HEADER_PARSER_STATE_FAILURE
== st
) {
581 php_http_header_parser_dtor(&parser
);
583 /* move body to right message */
584 if (response
->body
!= h
->response
.body
) {
585 php_http_message_t
*ptr
= response
;
587 while (ptr
->parent
) {
590 php_http_message_body_free(&response
->body
);
591 response
->body
= ptr
->body
;
594 php_http_message_body_addref(h
->response
.body
);
596 /* let's update the response headers */
597 if ((zh
= php_http_message_header(response
, ZEND_STRL("Content-Length"), 1))) {
598 zend_hash_update(&response
->hdrs
, "X-Original-Content-Length", sizeof("X-Original-Content-Length"), &zh
, sizeof(zval
*), NULL
);
600 if ((zh
= php_http_message_header(response
, ZEND_STRL("Transfer-Encoding"), 0))) {
601 zend_hash_update(&response
->hdrs
, "X-Original-Transfer-Encoding", sizeof("X-Original-Transfer-Encoding"), (void *) &zh
, sizeof(zval
*), NULL
);
602 zend_hash_del(&response
->hdrs
, "Transfer-Encoding", sizeof("Transfer-Encoding"));
604 if ((zh
= php_http_message_header(response
, ZEND_STRL("Content-Range"), 0))) {
605 zend_hash_update(&response
->hdrs
, "X-Original-Content-Range", sizeof("X-Original-Content-Range"), &zh
, sizeof(zval
*), NULL
);
606 zend_hash_del(&response
->hdrs
, "Content-Range", sizeof("Content-Range"));
608 if ((zh
= php_http_message_header(response
, ZEND_STRL("Content-Encoding"), 0))) {
609 zend_hash_update(&response
->hdrs
, "X-Original-Content-Encoding", sizeof("X-Original-Content-Encoding"), &zh
, sizeof(zval
*), NULL
);
610 zend_hash_del(&response
->hdrs
, "Content-Encoding", sizeof("Content-Encoding"));
612 php_http_message_update_headers(response
);
617 void php_http_client_curl_responsehandler(php_http_client_t
*context
)
619 int err_count
= 0, remaining
= 0;
620 php_http_curle_storage_t
*st
, *err
= NULL
;
621 php_http_client_enqueue_t
*enqueue
;
622 php_http_client_curl_t
*curl
= context
->ctx
;
623 TSRMLS_FETCH_FROM_CTX(context
->ts
);
626 CURLMsg
*msg
= curl_multi_info_read(curl
->handle
->multi
, &remaining
);
628 if (msg
&& CURLMSG_DONE
== msg
->msg
) {
629 if (CURLE_OK
!= msg
->data
.result
) {
630 st
= php_http_curle_get_storage(msg
->easy_handle
);
631 st
->errorcode
= msg
->data
.result
;
633 /* defer the warnings/exceptions, so the callback is still called for this request */
635 err
= ecalloc(remaining
+ 1, sizeof(*err
));
637 memcpy(&err
[err_count
], st
, sizeof(*st
));
639 err
[err_count
].url
= estrdup(st
->url
);
644 if ((enqueue
= php_http_client_enqueued(context
, msg
->easy_handle
, compare_queue
))) {
645 php_http_client_curl_handler_t
*handler
= enqueue
->opaque
;
646 php_http_message_t
*response
= php_http_curlm_responseparser(handler TSRMLS_CC
);
649 context
->callback
.response
.func(context
->callback
.response
.arg
, context
, &handler
->queue
, &response
);
650 php_http_message_free(&response
);
660 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
));
664 } while (++i
< err_count
);
672 static php_http_options_t php_http_curle_options
, php_http_curlm_options
;
674 #define PHP_HTTP_CURLE_OPTION_CHECK_STRLEN 0x0001
675 #define PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR 0x0002
676 #define PHP_HTTP_CURLE_OPTION_TRANSFORM_MS 0x0004
678 static ZEND_RESULT_CODE
php_http_curle_option_set_ssl_verifyhost(php_http_option_t
*opt
, zval
*val
, void *userdata
)
680 php_http_client_curl_handler_t
*curl
= userdata
;
681 CURL
*ch
= curl
->handle
;
683 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_SSL_VERIFYHOST
, Z_BVAL_P(val
) ? 2 : 0)) {
689 static ZEND_RESULT_CODE
php_http_curle_option_set_cookiesession(php_http_option_t
*opt
, zval
*val
, void *userdata
)
691 php_http_client_curl_handler_t
*curl
= userdata
;
692 CURL
*ch
= curl
->handle
;
694 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIESESSION
, (long) Z_BVAL_P(val
))) {
698 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIELIST
, "SESS")) {
706 static ZEND_RESULT_CODE
php_http_curle_option_set_cookiestore(php_http_option_t
*opt
, zval
*val
, void *userdata
)
708 php_http_client_curl_handler_t
*curl
= userdata
;
709 CURL
*ch
= curl
->handle
;
710 php_http_curle_storage_t
*storage
= php_http_curle_get_storage(curl
->handle
);
712 if (storage
->cookiestore
) {
713 pefree(storage
->cookiestore
, 1);
715 if (val
&& Z_STRLEN_P(val
)) {
716 storage
->cookiestore
= pestrndup(Z_STRVAL_P(val
), Z_STRLEN_P(val
), 1);
718 storage
->cookiestore
= NULL
;
720 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, storage
->cookiestore
)
721 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, storage
->cookiestore
)
729 static ZEND_RESULT_CODE
php_http_curle_option_set_cookies(php_http_option_t
*opt
, zval
*val
, void *userdata
)
731 php_http_client_curl_handler_t
*curl
= userdata
;
732 CURL
*ch
= curl
->handle
;
733 TSRMLS_FETCH_FROM_CTX(curl
->client
->ts
);
735 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
736 if (curl
->options
.encode_cookies
) {
737 if (SUCCESS
== php_http_url_encode_hash_ex(HASH_OF(val
), &curl
->options
.cookies
, ZEND_STRL(";"), ZEND_STRL("="), NULL
, 0 TSRMLS_CC
)) {
738 php_http_buffer_fix(&curl
->options
.cookies
);
739 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIE
, curl
->options
.cookies
.data
)) {
747 php_http_array_hashkey_t cookie_key
= php_http_array_hashkey_init(0);
750 FOREACH_KEYVAL(pos
, val
, cookie_key
, cookie_val
) {
751 zval
*zv
= php_http_ztyp(IS_STRING
, *cookie_val
);
753 php_http_array_hashkey_stringify(&cookie_key
);
754 php_http_buffer_appendf(&curl
->options
.cookies
, "%s=%s; ", cookie_key
.str
, Z_STRVAL_P(zv
));
755 php_http_array_hashkey_stringfree(&cookie_key
);
760 php_http_buffer_fix(&curl
->options
.cookies
);
761 if (curl
->options
.cookies
.used
) {
762 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIE
, curl
->options
.cookies
.data
)) {
768 php_http_buffer_reset(&curl
->options
.cookies
);
769 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIE
, NULL
)) {
776 static ZEND_RESULT_CODE
php_http_curle_option_set_encodecookies(php_http_option_t
*opt
, zval
*val
, void *userdata
)
778 php_http_client_curl_handler_t
*curl
= userdata
;
780 curl
->options
.encode_cookies
= Z_BVAL_P(val
);
784 static ZEND_RESULT_CODE
php_http_curle_option_set_lastmodified(php_http_option_t
*opt
, zval
*val
, void *userdata
)
786 php_http_client_curl_handler_t
*curl
= userdata
;
787 CURL
*ch
= curl
->handle
;
788 TSRMLS_FETCH_FROM_CTX(curl
->client
->ts
);
791 if (Z_LVAL_P(val
) > 0) {
792 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, Z_LVAL_P(val
))) {
796 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, (long) sapi_get_request_time(TSRMLS_C
) + Z_LVAL_P(val
))) {
800 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, (long) (curl
->options
.range_request
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
))) {
804 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, 0)
805 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, 0)
813 static ZEND_RESULT_CODE
php_http_curle_option_set_compress(php_http_option_t
*opt
, zval
*val
, void *userdata
)
815 php_http_client_curl_handler_t
*curl
= userdata
;
816 CURL
*ch
= curl
->handle
;
818 #if !PHP_HTTP_CURL_VERSION(7,21,6)
819 # define CURLOPT_ACCEPT_ENCODING CURLOPT_ENCODING
821 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_ACCEPT_ENCODING
, Z_BVAL_P(val
) ? "" : NULL
)) {
827 static ZEND_RESULT_CODE
php_http_curle_option_set_etag(php_http_option_t
*opt
, zval
*val
, void *userdata
)
829 php_http_client_curl_handler_t
*curl
= userdata
;
830 php_http_buffer_t header
;
832 if (Z_STRLEN_P(val
)) {
833 zend_bool is_quoted
= !((Z_STRVAL_P(val
)[0] != '"') || (Z_STRVAL_P(val
)[Z_STRLEN_P(val
)-1] != '"'));
834 php_http_buffer_init(&header
);
835 php_http_buffer_appendf(&header
, is_quoted
?"%s: %s":"%s: \"%s\"", curl
->options
.range_request
?"If-Match":"If-None-Match", Z_STRVAL_P(val
));
836 php_http_buffer_fix(&header
);
837 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, header
.data
);
838 php_http_buffer_dtor(&header
);
843 static ZEND_RESULT_CODE
php_http_curle_option_set_range(php_http_option_t
*opt
, zval
*val
, void *userdata
)
845 php_http_client_curl_handler_t
*curl
= userdata
;
846 CURL
*ch
= curl
->handle
;
847 TSRMLS_FETCH_FROM_CTX(curl
->client
->ts
);
849 php_http_buffer_reset(&curl
->options
.ranges
);
851 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
853 zval
**rr
, **rb
, **re
;
855 FOREACH_VAL(pos
, val
, rr
) {
856 if (Z_TYPE_PP(rr
) == IS_ARRAY
) {
857 if (2 == php_http_array_list(Z_ARRVAL_PP(rr
) TSRMLS_CC
, 2, &rb
, &re
)) {
858 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))) &&
859 ((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)))) {
860 zval
*rbl
= php_http_ztyp(IS_LONG
, *rb
);
861 zval
*rel
= php_http_ztyp(IS_LONG
, *re
);
863 if ((Z_LVAL_P(rbl
) >= 0) && (Z_LVAL_P(rel
) >= 0)) {
864 php_http_buffer_appendf(&curl
->options
.ranges
, "%ld-%ld,", Z_LVAL_P(rbl
), Z_LVAL_P(rel
));
874 if (curl
->options
.ranges
.used
) {
875 curl
->options
.range_request
= 1;
876 /* ditch last comma */
877 curl
->options
.ranges
.data
[curl
->options
.ranges
.used
- 1] = '\0';
881 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RANGE
, curl
->options
.ranges
.data
)) {
887 static ZEND_RESULT_CODE
php_http_curle_option_set_resume(php_http_option_t
*opt
, zval
*val
, void *userdata
)
889 php_http_client_curl_handler_t
*curl
= userdata
;
890 CURL
*ch
= curl
->handle
;
892 if (Z_LVAL_P(val
) > 0) {
893 curl
->options
.range_request
= 1;
895 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RESUME_FROM
, Z_LVAL_P(val
))) {
901 static ZEND_RESULT_CODE
php_http_curle_option_set_retrydelay(php_http_option_t
*opt
, zval
*val
, void *userdata
)
903 php_http_client_curl_handler_t
*curl
= userdata
;
905 curl
->options
.retry
.delay
= Z_DVAL_P(val
);
909 static ZEND_RESULT_CODE
php_http_curle_option_set_retrycount(php_http_option_t
*opt
, zval
*val
, void *userdata
)
911 php_http_client_curl_handler_t
*curl
= userdata
;
913 curl
->options
.retry
.count
= Z_LVAL_P(val
);
917 static ZEND_RESULT_CODE
php_http_curle_option_set_redirect(php_http_option_t
*opt
, zval
*val
, void *userdata
)
919 php_http_client_curl_handler_t
*curl
= userdata
;
920 CURL
*ch
= curl
->handle
;
922 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(val
) ? 1L : 0L)
923 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, curl
->options
.redirects
= Z_LVAL_P(val
))
930 static ZEND_RESULT_CODE
php_http_curle_option_set_portrange(php_http_option_t
*opt
, zval
*val
, void *userdata
)
932 php_http_client_curl_handler_t
*curl
= userdata
;
933 CURL
*ch
= curl
->handle
;
934 long localport
= 0, localportrange
= 0;
935 TSRMLS_FETCH_FROM_CTX(curl
->client
->ts
);
937 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
938 zval
**z_port_start
, *zps_copy
= NULL
, **z_port_end
, *zpe_copy
= NULL
;
940 switch (php_http_array_list(Z_ARRVAL_P(val
) TSRMLS_CC
, 2, &z_port_start
, &z_port_end
)) {
942 zps_copy
= php_http_ztyp(IS_LONG
, *z_port_start
);
943 zpe_copy
= php_http_ztyp(IS_LONG
, *z_port_end
);
944 localportrange
= labs(Z_LVAL_P(zps_copy
)-Z_LVAL_P(zpe_copy
))+1L;
948 zps_copy
= php_http_ztyp(IS_LONG
, *z_port_start
);
950 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
);
951 zval_ptr_dtor(&zps_copy
);
953 zval_ptr_dtor(&zpe_copy
);
960 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_LOCALPORT
, localport
)
961 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_LOCALPORTRANGE
, localportrange
)
968 #if PHP_HTTP_CURL_VERSION(7,37,0)
969 static ZEND_RESULT_CODE
php_http_curle_option_set_proxyheader(php_http_option_t
*opt
, zval
*val
, void *userdata
)
971 php_http_client_curl_handler_t
*curl
= userdata
;
972 TSRMLS_FETCH_FROM_CTX(curl
->client
->ts
);
974 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
975 php_http_array_hashkey_t header_key
= php_http_array_hashkey_init(0);
976 zval
**header_val
, *header_cpy
;
978 php_http_buffer_t header
;
980 php_http_buffer_init(&header
);
981 FOREACH_KEYVAL(pos
, val
, header_key
, header_val
) {
982 if (header_key
.type
== HASH_KEY_IS_STRING
) {
983 header_cpy
= php_http_ztyp(IS_STRING
, *header_val
);
984 php_http_buffer_appendf(&header
, "%s: %s", header_key
.str
, Z_STRVAL_P(header_cpy
));
985 php_http_buffer_fix(&header
);
986 curl
->options
.proxyheaders
= curl_slist_append(curl
->options
.proxyheaders
, header
.data
);
987 php_http_buffer_reset(&header
);
989 zval_ptr_dtor(&header_cpy
);
992 php_http_buffer_dtor(&header
);
994 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_PROXYHEADER
, curl
->options
.proxyheaders
)) {
997 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_HEADEROPT
, CURLHEADER_SEPARATE
)) {
998 curl_easy_setopt(curl
->handle
, CURLOPT_PROXYHEADER
, NULL
);
1005 #if PHP_HTTP_CURL_VERSION(7,21,3)
1006 static ZEND_RESULT_CODE
php_http_curle_option_set_resolve(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1008 php_http_client_curl_handler_t
*curl
= userdata
;
1009 CURL
*ch
= curl
->handle
;
1010 TSRMLS_FETCH_FROM_CTX(curl
->client
->ts
);
1012 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
1013 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
1017 FOREACH_KEYVAL(pos
, val
, key
, data
) {
1018 zval
*cpy
= php_http_ztyp(IS_STRING
, *data
);
1019 curl
->options
.resolve
= curl_slist_append(curl
->options
.resolve
, Z_STRVAL_P(cpy
));
1020 zval_ptr_dtor(&cpy
);
1023 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RESOLVE
, curl
->options
.resolve
)) {
1027 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RESOLVE
, NULL
)) {
1035 #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
1036 static ZEND_RESULT_CODE
php_http_curle_option_set_ssl_tlsauthtype(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1038 php_http_client_curl_handler_t
*curl
= userdata
;
1039 CURL
*ch
= curl
->handle
;
1041 if (val
&& Z_LVAL_P(val
)) {
1042 switch (Z_LVAL_P(val
)) {
1043 case CURL_TLSAUTH_SRP
:
1044 if (CURLE_OK
== curl_easy_setopt(ch
, CURLOPT_TLSAUTH_TYPE
, PHP_HTTP_CURL_TLSAUTH_SRP
)) {
1052 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TLSAUTH_TYPE
, PHP_HTTP_CURL_TLSAUTH_DEF
)) {
1059 static void php_http_curle_options_init(php_http_options_t
*registry TSRMLS_DC
)
1061 php_http_option_t
*opt
;
1064 #if PHP_HTTP_CURL_VERSION(7,42,0)
1065 php_http_option_register(registry
, ZEND_STRL("path_as_is"), CURLOPT_PATH_AS_IS
, IS_BOOL
);
1069 php_http_option_register(registry
, ZEND_STRL("proxyhost"), CURLOPT_PROXY
, IS_STRING
);
1070 php_http_option_register(registry
, ZEND_STRL("proxytype"), CURLOPT_PROXYTYPE
, IS_LONG
);
1071 php_http_option_register(registry
, ZEND_STRL("proxyport"), CURLOPT_PROXYPORT
, IS_LONG
);
1072 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyauth"), CURLOPT_PROXYUSERPWD
, IS_STRING
))) {
1073 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1075 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyauthtype"), CURLOPT_PROXYAUTH
, IS_LONG
))) {
1076 Z_LVAL(opt
->defval
) = CURLAUTH_ANYSAFE
;
1078 php_http_option_register(registry
, ZEND_STRL("proxytunnel"), CURLOPT_HTTPPROXYTUNNEL
, IS_BOOL
);
1079 #if PHP_HTTP_CURL_VERSION(7,19,4)
1080 php_http_option_register(registry
, ZEND_STRL("noproxy"), CURLOPT_NOPROXY
, IS_STRING
);
1083 #if PHP_HTTP_CURL_VERSION(7,37,0)
1084 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyheader"), CURLOPT_PROXYHEADER
, IS_ARRAY
))) {
1085 opt
->setter
= php_http_curle_option_set_proxyheader
;
1088 #if PHP_HTTP_CURL_VERSION(7,43,0)
1089 if (PHP_HTTP_CURL_FEATURE(CURL_VERSION_GSSAPI
)
1090 && (opt
= php_http_option_register(registry
, ZEND_STRL("proxy_service_name"), CURLOPT_PROXY_SERVICE_NAME
, IS_STRING
))
1092 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1096 #if PHP_HTTP_CURL_VERSION(7,40,0)
1097 if (PHP_HTTP_CURL_FEATURE(CURL_VERSION_UNIX_SOCKETS
)) {
1098 if ((opt
= php_http_option_register(registry
, ZEND_STRL("unix_socket_path"), CURLOPT_UNIX_SOCKET_PATH
, IS_STRING
))) {
1099 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1100 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1106 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_cache_timeout"), CURLOPT_DNS_CACHE_TIMEOUT
, IS_LONG
))) {
1107 Z_LVAL(opt
->defval
) = 60;
1109 php_http_option_register(registry
, ZEND_STRL("ipresolve"), CURLOPT_IPRESOLVE
, IS_LONG
);
1110 #if PHP_HTTP_CURL_VERSION(7,21,3)
1111 if ((opt
= php_http_option_register(registry
, ZEND_STRL("resolve"), CURLOPT_RESOLVE
, IS_ARRAY
))) {
1112 opt
->setter
= php_http_curle_option_set_resolve
;
1115 #if PHP_HTTP_HAVE_ARES
1116 # if PHP_HTTP_CURL_VERSION(7,24,0)
1117 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_servers"), CURLOPT_DNS_SERVERS
, IS_STRING
))) {
1118 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1121 # if PHP_HTTP_CURL_VERSION(7,33,0)
1122 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_interface"), CURLOPT_DNS_INTERFACE
, IS_STRING
))) {
1123 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1125 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_local_ip4"), CURLOPT_DNS_LOCAL_IP4
, IS_STRING
))) {
1126 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1128 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_local_ip6"), CURLOPT_DNS_LOCAL_IP6
, IS_STRING
))) {
1129 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1135 php_http_option_register(registry
, ZEND_STRL("low_speed_limit"), CURLOPT_LOW_SPEED_LIMIT
, IS_LONG
);
1136 php_http_option_register(registry
, ZEND_STRL("low_speed_time"), CURLOPT_LOW_SPEED_TIME
, IS_LONG
);
1139 php_http_option_register(registry, ZEND_STRL("max_send_speed"), CURLOPT_MAX_SEND_SPEED_LARGE, IS_LONG);
1140 php_http_option_register(registry, ZEND_STRL("max_recv_speed"), CURLOPT_MAX_RECV_SPEED_LARGE, IS_LONG);
1143 /* connection handling */
1145 if ((opt = php_http_option_register(registry, ZEND_STRL("maxconnects"), CURLOPT_MAXCONNECTS, IS_LONG))) {
1146 Z_LVAL(opt->defval) = 5;
1149 php_http_option_register(registry
, ZEND_STRL("fresh_connect"), CURLOPT_FRESH_CONNECT
, IS_BOOL
);
1150 php_http_option_register(registry
, ZEND_STRL("forbid_reuse"), CURLOPT_FORBID_REUSE
, IS_BOOL
);
1152 /* outgoing interface */
1153 php_http_option_register(registry
, ZEND_STRL("interface"), CURLOPT_INTERFACE
, IS_STRING
);
1154 if ((opt
= php_http_option_register(registry
, ZEND_STRL("portrange"), CURLOPT_LOCALPORT
, IS_ARRAY
))) {
1155 opt
->setter
= php_http_curle_option_set_portrange
;
1158 /* another endpoint port */
1159 php_http_option_register(registry
, ZEND_STRL("port"), CURLOPT_PORT
, IS_LONG
);
1161 /* RFC4007 zone_id */
1162 #if PHP_HTTP_CURL_VERSION(7,19,0)
1163 php_http_option_register(registry
, ZEND_STRL("address_scope"), CURLOPT_ADDRESS_SCOPE
, IS_LONG
);
1167 if ((opt
= php_http_option_register(registry
, ZEND_STRL("httpauth"), CURLOPT_USERPWD
, IS_STRING
))) {
1168 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1170 if ((opt
= php_http_option_register(registry
, ZEND_STRL("httpauthtype"), CURLOPT_HTTPAUTH
, IS_LONG
))) {
1171 Z_LVAL(opt
->defval
) = CURLAUTH_ANYSAFE
;
1173 #if PHP_HTTP_CURL_VERSION(7,43,0)
1174 if (PHP_HTTP_CURL_FEATURE(CURL_VERSION_SSPI
) || PHP_HTTP_CURL_FEATURE(CURL_VERSION_GSSAPI
))
1175 if ((opt
= php_http_option_register(registry
, ZEND_STRL("service_name"), CURLOPT_SERVICE_NAME
, IS_STRING
))) {
1176 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1181 if ((opt
= php_http_option_register(registry
, ZEND_STRL("redirect"), CURLOPT_FOLLOWLOCATION
, IS_LONG
))) {
1182 opt
->setter
= php_http_curle_option_set_redirect
;
1184 php_http_option_register(registry
, ZEND_STRL("unrestricted_auth"), CURLOPT_UNRESTRICTED_AUTH
, IS_BOOL
);
1185 #if PHP_HTTP_CURL_VERSION(7,19,1)
1186 php_http_option_register(registry
, ZEND_STRL("postredir"), CURLOPT_POSTREDIR
, IS_LONG
);
1190 if ((opt
= php_http_option_register(registry
, ZEND_STRL("retrycount"), 0, IS_LONG
))) {
1191 opt
->setter
= php_http_curle_option_set_retrycount
;
1193 if ((opt
= php_http_option_register(registry
, ZEND_STRL("retrydelay"), 0, IS_DOUBLE
))) {
1194 opt
->setter
= php_http_curle_option_set_retrydelay
;
1198 if ((opt
= php_http_option_register(registry
, ZEND_STRL("referer"), CURLOPT_REFERER
, IS_STRING
))) {
1199 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1201 if ((opt
= php_http_option_register(registry
, ZEND_STRL("autoreferer"), CURLOPT_AUTOREFERER
, IS_BOOL
))) {
1202 ZVAL_BOOL(&opt
->defval
, 1);
1206 if ((opt
= php_http_option_register(registry
, ZEND_STRL("useragent"), CURLOPT_USERAGENT
, IS_STRING
))) {
1207 /* don't check strlen, to allow sending no useragent at all */
1208 ZVAL_STRING(&opt
->defval
,
1209 "PECL_HTTP/" PHP_PECL_HTTP_VERSION
" "
1210 "PHP/" PHP_VERSION
" "
1211 "libcurl/" LIBCURL_VERSION
1216 if ((opt
= php_http_option_register(registry
, ZEND_STRL("resume"), CURLOPT_RESUME_FROM
, IS_LONG
))) {
1217 opt
->setter
= php_http_curle_option_set_resume
;
1220 if ((opt
= php_http_option_register(registry
, ZEND_STRL("range"), CURLOPT_RANGE
, IS_ARRAY
))) {
1221 opt
->setter
= php_http_curle_option_set_range
;
1225 if ((opt
= php_http_option_register(registry
, ZEND_STRL("etag"), 0, IS_STRING
))) {
1226 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1227 opt
->setter
= php_http_curle_option_set_etag
;
1231 if ((opt
= php_http_option_register(registry
, ZEND_STRL("compress"), 0, IS_BOOL
))) {
1232 opt
->setter
= php_http_curle_option_set_compress
;
1236 if ((opt
= php_http_option_register(registry
, ZEND_STRL("lastmodified"), 0, IS_LONG
))) {
1237 opt
->setter
= php_http_curle_option_set_lastmodified
;
1241 if ((opt
= php_http_option_register(registry
, ZEND_STRL("encodecookies"), 0, IS_BOOL
))) {
1242 opt
->setter
= php_http_curle_option_set_encodecookies
;
1243 ZVAL_BOOL(&opt
->defval
, 1);
1245 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cookies"), 0, IS_ARRAY
))) {
1246 opt
->setter
= php_http_curle_option_set_cookies
;
1249 /* cookiesession, don't load session cookies from cookiestore */
1250 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cookiesession"), CURLOPT_COOKIESESSION
, IS_BOOL
))) {
1251 opt
->setter
= php_http_curle_option_set_cookiesession
;
1253 /* cookiestore, read initial cookies from that file and store cookies back into that file */
1254 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cookiestore"), 0, IS_STRING
))) {
1255 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1256 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1257 opt
->setter
= php_http_curle_option_set_cookiestore
;
1261 php_http_option_register(registry
, ZEND_STRL("maxfilesize"), CURLOPT_MAXFILESIZE
, IS_LONG
);
1263 /* http protocol version */
1264 php_http_option_register(registry
, ZEND_STRL("protocol"), CURLOPT_HTTP_VERSION
, IS_LONG
);
1267 if ((opt
= php_http_option_register(registry
, ZEND_STRL("timeout"), CURLOPT_TIMEOUT_MS
, IS_DOUBLE
))) {
1268 opt
->flags
|= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS
;
1270 if ((opt
= php_http_option_register(registry
, ZEND_STRL("connecttimeout"), CURLOPT_CONNECTTIMEOUT_MS
, IS_DOUBLE
))) {
1271 opt
->flags
|= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS
;
1272 Z_DVAL(opt
->defval
) = 3;
1274 #if PHP_HTTP_CURL_VERSION(7,36,0)
1275 if ((opt
= php_http_option_register(registry
, ZEND_STRL("expect_100_timeout"), CURLOPT_EXPECT_100_TIMEOUT_MS
, IS_DOUBLE
))) {
1276 opt
->flags
|= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS
;
1277 Z_DVAL(opt
->defval
) = 1;
1282 php_http_option_register(registry
, ZEND_STRL("tcp_nodelay"), CURLOPT_TCP_NODELAY
, IS_BOOL
);
1283 #if PHP_HTTP_CURL_VERSION(7,25,0)
1284 php_http_option_register(registry
, ZEND_STRL("tcp_keepalive"), CURLOPT_TCP_KEEPALIVE
, IS_BOOL
);
1285 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tcp_keepidle"), CURLOPT_TCP_KEEPIDLE
, IS_LONG
))) {
1286 Z_LVAL(opt
->defval
) = 60;
1288 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tcp_keepintvl"), CURLOPT_TCP_KEEPINTVL
, IS_LONG
))) {
1289 Z_LVAL(opt
->defval
) = 60;
1294 if (PHP_HTTP_CURL_FEATURE(CURL_VERSION_SSL
)) {
1295 if ((opt
= php_http_option_register(registry
, ZEND_STRL("ssl"), 0, IS_ARRAY
))) {
1296 registry
= &opt
->suboptions
;
1298 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cert"), CURLOPT_SSLCERT
, IS_STRING
))) {
1299 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1300 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1302 if ((opt
= php_http_option_register(registry
, ZEND_STRL("certtype"), CURLOPT_SSLCERTTYPE
, IS_STRING
))) {
1303 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1304 ZVAL_STRING(&opt
->defval
, "PEM", 0);
1306 if ((opt
= php_http_option_register(registry
, ZEND_STRL("key"), CURLOPT_SSLKEY
, IS_STRING
))) {
1307 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1308 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1310 if ((opt
= php_http_option_register(registry
, ZEND_STRL("keytype"), CURLOPT_SSLKEYTYPE
, IS_STRING
))) {
1311 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1312 ZVAL_STRING(&opt
->defval
, "PEM", 0);
1314 if ((opt
= php_http_option_register(registry
, ZEND_STRL("keypasswd"), CURLOPT_SSLKEYPASSWD
, IS_STRING
))) {
1315 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1317 php_http_option_register(registry
, ZEND_STRL("engine"), CURLOPT_SSLENGINE
, IS_STRING
);
1318 php_http_option_register(registry
, ZEND_STRL("version"), CURLOPT_SSLVERSION
, IS_LONG
);
1319 if ((opt
= php_http_option_register(registry
, ZEND_STRL("verifypeer"), CURLOPT_SSL_VERIFYPEER
, IS_BOOL
))) {
1320 ZVAL_BOOL(&opt
->defval
, 1);
1322 if ((opt
= php_http_option_register(registry
, ZEND_STRL("verifyhost"), CURLOPT_SSL_VERIFYHOST
, IS_BOOL
))) {
1323 ZVAL_BOOL(&opt
->defval
, 1);
1324 opt
->setter
= php_http_curle_option_set_ssl_verifyhost
;
1326 #if PHP_HTTP_CURL_VERSION(7,41,0) && (defined(PHP_HTTP_HAVE_OPENSSL) || defined(PHP_HTTP_HAVE_NSS) || defined(PHP_HTTP_HAVE_GNUTLS))
1327 php_http_option_register(registry
, ZEND_STRL("verifystatus"), CURLOPT_SSL_VERIFYSTATUS
, IS_BOOL
);
1329 php_http_option_register(registry
, ZEND_STRL("cipher_list"), CURLOPT_SSL_CIPHER_LIST
, IS_STRING
);
1330 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cainfo"), CURLOPT_CAINFO
, IS_STRING
))) {
1331 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1332 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1333 #ifdef PHP_HTTP_CURL_CAINFO
1334 ZVAL_STRING(&opt
->defval
, PHP_HTTP_CURL_CAINFO
, 0);
1337 if ((opt
= php_http_option_register(registry
, ZEND_STRL("capath"), CURLOPT_CAPATH
, IS_STRING
))) {
1338 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1339 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1340 #ifdef PHP_HTTP_CURL_CAPATH
1341 ZVAL_STRING(&opt
->defval
, PHP_HTTP_CURL_CAPATH
, 0);
1344 if ((opt
= php_http_option_register(registry
, ZEND_STRL("random_file"), CURLOPT_RANDOM_FILE
, IS_STRING
))) {
1345 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1346 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1348 if ((opt
= php_http_option_register(registry
, ZEND_STRL("egdsocket"), CURLOPT_EGDSOCKET
, IS_STRING
))) {
1349 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1350 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1352 #if PHP_HTTP_CURL_VERSION(7,19,0)
1353 if ((opt
= php_http_option_register(registry
, ZEND_STRL("issuercert"), CURLOPT_ISSUERCERT
, IS_STRING
))) {
1354 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1355 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1357 # ifdef PHP_HTTP_HAVE_OPENSSL
1358 if ((opt
= php_http_option_register(registry
, ZEND_STRL("crlfile"), CURLOPT_CRLFILE
, IS_STRING
))) {
1359 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1360 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1364 #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))
1365 php_http_option_register(registry
, ZEND_STRL("certinfo"), CURLOPT_CERTINFO
, IS_BOOL
);
1367 #if PHP_HTTP_CURL_VERSION(7,36,0)
1368 if ((opt
= php_http_option_register(registry
, ZEND_STRL("enable_npn"), CURLOPT_SSL_ENABLE_NPN
, IS_BOOL
))) {
1369 ZVAL_BOOL(&opt
->defval
, 1);
1371 if ((opt
= php_http_option_register(registry
, ZEND_STRL("enable_alpn"), CURLOPT_SSL_ENABLE_ALPN
, IS_BOOL
))) {
1372 ZVAL_BOOL(&opt
->defval
, 1);
1375 #if PHP_HTTP_CURL_VERSION(7,39,0)
1376 /* FIXME: see http://curl.haxx.se/libcurl/c/CURLOPT_PINNEDPUBLICKEY.html#AVAILABILITY */
1377 if ((opt
= php_http_option_register(registry
, ZEND_STRL("pinned_publickey"), CURLOPT_PINNEDPUBLICKEY
, IS_STRING
))) {
1378 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1379 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1382 #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
1383 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tlsauthtype"), CURLOPT_TLSAUTH_TYPE
, IS_LONG
))) {
1384 opt
->setter
= php_http_curle_option_set_ssl_tlsauthtype
;
1386 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tlsauthuser"), CURLOPT_TLSAUTH_USERNAME
, IS_STRING
))) {
1387 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1389 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tlsauthpass"), CURLOPT_TLSAUTH_PASSWORD
, IS_STRING
))) {
1390 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1393 #if PHP_HTTP_CURL_VERSION(7,42,0) && (defined(PHP_HTTP_HAVE_NSS) || defined(PHP_HTTP_HAVE_DARWINSSL))
1394 php_http_option_register(registry
, ZEND_STRL("falsestart"), CURLOPT_SSL_FALSESTART
, IS_BOOL
);
1400 static zval
*php_http_curle_get_option(php_http_option_t
*opt
, HashTable
*options
, void *userdata
)
1402 php_http_client_curl_handler_t
*curl
= userdata
;
1405 if ((option
= php_http_option_get(opt
, options
, NULL
))) {
1406 option
= php_http_ztyp(opt
->type
, option
);
1407 zend_hash_quick_update(&curl
->options
.cache
, opt
->name
.s
, opt
->name
.l
, opt
->name
.h
, &option
, sizeof(zval
*), NULL
);
1412 static ZEND_RESULT_CODE
php_http_curle_set_option(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1414 php_http_client_curl_handler_t
*curl
= userdata
;
1415 CURL
*ch
= curl
->handle
;
1417 CURLcode rc
= CURLE_UNKNOWN_OPTION
;
1418 ZEND_RESULT_CODE rv
= SUCCESS
;
1419 TSRMLS_FETCH_FROM_CTX(curl
->client
->ts
);
1425 switch (opt
->type
) {
1428 rv
= opt
->setter(opt
, val
, curl
);
1429 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, (long) Z_BVAL_P(val
))) {
1436 rv
= opt
->setter(opt
, val
, curl
);
1437 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, Z_LVAL_P(val
))) {
1444 rv
= opt
->setter(opt
, val
, curl
);
1445 } else if ((opt
->flags
& PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
) && !Z_STRLEN_P(val
)) {
1446 if (CURLE_OK
!= (rc
= curl_easy_setopt(ch
, opt
->option
, NULL
))) {
1449 } 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
)) {
1450 if (CURLE_OK
!= (rc
= curl_easy_setopt(ch
, opt
->option
, NULL
))) {
1453 } else if (CURLE_OK
!= (rc
= curl_easy_setopt(ch
, opt
->option
, Z_STRVAL_P(val
)))) {
1459 if (opt
->flags
& PHP_HTTP_CURLE_OPTION_TRANSFORM_MS
) {
1461 Z_DVAL(tmp
) *= 1000;
1465 rv
= opt
->setter(opt
, val
, curl
);
1466 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, (long) Z_DVAL_P(val
))) {
1473 rv
= opt
->setter(opt
, val
, curl
);
1474 } else if (Z_TYPE_P(val
) != IS_NULL
) {
1475 rv
= php_http_options_apply(&opt
->suboptions
, Z_ARRVAL_P(val
), curl
);
1481 rv
= opt
->setter(opt
, val
, curl
);
1487 if (rv
!= SUCCESS
) {
1488 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Could not set option %s (%s)", opt
->name
.s
, curl_easy_strerror(rc
));
1493 #if PHP_HTTP_CURL_VERSION(7,30,0)
1494 static ZEND_RESULT_CODE
php_http_curlm_option_set_pipelining_bl(php_http_option_t
*opt
, zval
*value
, void *userdata
)
1496 php_http_client_t
*client
= userdata
;
1497 php_http_client_curl_t
*curl
= client
->ctx
;
1498 CURLM
*ch
= curl
->handle
->multi
;
1501 TSRMLS_FETCH_FROM_CTX(client
->ts
);
1503 /* array of char *, ending with a NULL */
1504 if (value
&& Z_TYPE_P(value
) != IS_NULL
) {
1507 HashTable
*ht
= HASH_OF(value
);
1508 int c
= zend_hash_num_elements(ht
);
1509 char **ptr
= ecalloc(c
+ 1, sizeof(char *));
1513 zend_hash_init(&tmp_ht
, c
, NULL
, ZVAL_PTR_DTOR
, 0);
1514 array_join(ht
, &tmp_ht
, 0, ARRAY_JOIN_STRINGIFY
);
1516 FOREACH_HASH_VAL(pos
, &tmp_ht
, entry
) {
1517 *ptr
++ = Z_STRVAL_PP(entry
);
1521 if (CURLM_OK
!= curl_multi_setopt(ch
, opt
->option
, bl
)) {
1524 zend_hash_destroy(&tmp_ht
);
1531 zend_hash_destroy(&tmp_ht
);
1537 #if PHP_HTTP_HAVE_EVENT
1538 static inline ZEND_RESULT_CODE
php_http_curlm_use_eventloop(php_http_client_t
*h
, zend_bool enable
)
1540 php_http_client_curl_t
*curl
= h
->ctx
;
1543 if (!curl
->ev_ops
) {
1544 if (!(curl
->ev_ops
= php_http_client_curl_event_ops_get())) {
1548 if (curl
->ev_ops
&& !curl
->ev_ctx
) {
1549 if (!(curl
->ev_ctx
= curl
->ev_ops
->init(h
))) {
1556 curl
->ev_ops
->dtor(&curl
->ev_ctx
);
1558 curl
->ev_ops
= NULL
;
1565 static ZEND_RESULT_CODE
php_http_curlm_option_set_use_eventloop(php_http_option_t
*opt
, zval
*value
, void *userdata
)
1567 php_http_client_t
*client
= userdata
;
1569 if (Z_TYPE_P(value
) == IS_OBJECT
/* && instanceof_function */) {
1572 return php_http_curlm_use_eventloop(client
, value
&& z_is_true(value
));
1576 static ZEND_RESULT_CODE
php_http_curlm_option_set_share_cookies(php_http_option_t
*opt
, zval
*value
, void *userdata
)
1578 php_http_client_t
*client
= userdata
;
1579 php_http_client_curl_t
*curl
= client
->ctx
;
1582 if (Z_BVAL_P(value
)) {
1583 rc
= curl_share_setopt(curl
->handle
->share
, CURLSHOPT_SHARE
, CURL_LOCK_DATA_COOKIE
);
1585 rc
= curl_share_setopt(curl
->handle
->share
, CURLSHOPT_UNSHARE
, CURL_LOCK_DATA_COOKIE
);
1588 if (CURLSHE_OK
!= rc
) {
1589 TSRMLS_FETCH_FROM_CTX(client
->ts
);
1590 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Could not set option %s (%s)", opt
->name
.s
, curl_share_strerror(rc
));
1596 #if PHP_HTTP_CURL_VERSION(7,23,0)
1597 static ZEND_RESULT_CODE
php_http_curlm_option_set_share_ssl(php_http_option_t
*opt
, zval
*value
, void *userdata
)
1599 php_http_client_t
*client
= userdata
;
1600 php_http_client_curl_t
*curl
= client
->ctx
;
1603 if (Z_BVAL_P(value
)) {
1604 rc
= curl_share_setopt(curl
->handle
->share
, CURLSHOPT_SHARE
, CURL_LOCK_DATA_SSL_SESSION
);
1606 rc
= curl_share_setopt(curl
->handle
->share
, CURLSHOPT_UNSHARE
, CURL_LOCK_DATA_SSL_SESSION
);
1609 if (CURLSHE_OK
!= rc
) {
1610 TSRMLS_FETCH_FROM_CTX(client
->ts
);
1611 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Could not set option %s (%s)", opt
->name
.s
, curl_share_strerror(rc
));
1618 static void php_http_curlm_options_init(php_http_options_t
*registry TSRMLS_DC
)
1620 php_http_option_t
*opt
;
1622 /* set size of connection cache */
1623 if ((opt
= php_http_option_register(registry
, ZEND_STRL("maxconnects"), CURLMOPT_MAXCONNECTS
, IS_LONG
))) {
1624 /* -1 == default, 0 == unlimited */
1625 ZVAL_LONG(&opt
->defval
, -1);
1627 /* set max number of connections to a single host */
1628 #if PHP_HTTP_CURL_VERSION(7,30,0)
1629 php_http_option_register(registry
, ZEND_STRL("max_host_connections"), CURLMOPT_MAX_HOST_CONNECTIONS
, IS_LONG
);
1631 /* maximum number of requests in a pipeline */
1632 #if PHP_HTTP_CURL_VERSION(7,30,0)
1633 if ((opt
= php_http_option_register(registry
, ZEND_STRL("max_pipeline_length"), CURLMOPT_MAX_PIPELINE_LENGTH
, IS_LONG
))) {
1634 ZVAL_LONG(&opt
->defval
, 5);
1637 /* max simultaneously open connections */
1638 #if PHP_HTTP_CURL_VERSION(7,30,0)
1639 php_http_option_register(registry
, ZEND_STRL("max_total_connections"), CURLMOPT_MAX_TOTAL_CONNECTIONS
, IS_LONG
);
1641 /* enable/disable HTTP pipelining */
1642 php_http_option_register(registry
, ZEND_STRL("pipelining"), CURLMOPT_PIPELINING
, IS_BOOL
);
1643 /* chunk length threshold for pipelining */
1644 #if PHP_HTTP_CURL_VERSION(7,30,0)
1645 php_http_option_register(registry
, ZEND_STRL("chunk_length_penalty_size"), CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE
, IS_LONG
);
1647 /* size threshold for pipelining penalty */
1648 #if PHP_HTTP_CURL_VERSION(7,30,0)
1649 php_http_option_register(registry
, ZEND_STRL("content_length_penalty_size"), CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE
, IS_LONG
);
1651 /* pipelining server blacklist */
1652 #if PHP_HTTP_CURL_VERSION(7,30,0)
1653 if ((opt
= php_http_option_register(registry
, ZEND_STRL("pipelining_server_bl"), CURLMOPT_PIPELINING_SERVER_BL
, IS_ARRAY
))) {
1654 opt
->setter
= php_http_curlm_option_set_pipelining_bl
;
1657 /* pipelining host blacklist */
1658 #if PHP_HTTP_CURL_VERSION(7,30,0)
1659 if ((opt
= php_http_option_register(registry
, ZEND_STRL("pipelining_site_bl"), CURLMOPT_PIPELINING_SITE_BL
, IS_ARRAY
))) {
1660 opt
->setter
= php_http_curlm_option_set_pipelining_bl
;
1664 #if PHP_HTTP_HAVE_EVENT
1665 if ((opt
= php_http_option_register(registry
, ZEND_STRL("use_eventloop"), 0, IS_BOOL
))) {
1666 opt
->setter
= php_http_curlm_option_set_use_eventloop
;
1670 if ((opt
= php_http_option_register(registry
, ZEND_STRL("share_cookies"), 0, IS_BOOL
))) {
1671 opt
->setter
= php_http_curlm_option_set_share_cookies
;
1672 ZVAL_BOOL(&opt
->defval
, 1);
1674 #if PHP_HTTP_CURL_VERSION(7,23,0)
1675 if ((opt
= php_http_option_register(registry
, ZEND_STRL("share_ssl"), 0, IS_BOOL
))) {
1676 opt
->setter
= php_http_curlm_option_set_share_ssl
;
1677 ZVAL_BOOL(&opt
->defval
, 1);
1682 static ZEND_RESULT_CODE
php_http_curlm_set_option(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1684 php_http_client_t
*client
= userdata
;
1685 php_http_client_curl_t
*curl
= client
->ctx
;
1686 CURLM
*ch
= curl
->handle
->multi
;
1688 CURLMcode rc
= CURLM_UNKNOWN_OPTION
;
1689 ZEND_RESULT_CODE rv
= SUCCESS
;
1690 TSRMLS_FETCH_FROM_CTX(client
->ts
);
1694 } else if (opt
->type
&& Z_TYPE_P(val
) != opt
->type
&& !(Z_TYPE_P(val
) == IS_NULL
&& opt
->type
== IS_ARRAY
)) {
1695 val
= php_http_ztyp(opt
->type
, val
);
1699 rv
= opt
->setter(opt
, val
, client
);
1701 switch (opt
->type
) {
1703 if (CURLM_OK
!= (rc
= curl_multi_setopt(ch
, opt
->option
, (long) Z_BVAL_P(val
)))) {
1705 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Could not set option %s (%s)", opt
->name
.s
, curl_multi_strerror(rc
));
1709 if (CURLM_OK
!= (rc
= curl_multi_setopt(ch
, opt
->option
, Z_LVAL_P(val
)))) {
1711 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Could not set option %s (%s)", opt
->name
.s
, curl_multi_strerror(rc
));
1716 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Could not set option %s", opt
->name
.s
);
1721 if (val
&& val
!= orig
&& val
!= &opt
->defval
) {
1722 zval_ptr_dtor(&val
);
1730 static ZEND_RESULT_CODE
php_http_client_curl_handler_reset(php_http_client_curl_handler_t
*curl
)
1732 CURL
*ch
= curl
->handle
;
1733 php_http_curle_storage_t
*st
;
1735 if ((st
= php_http_curle_get_storage(ch
))) {
1740 if (st
->cookiestore
) {
1741 pefree(st
->cookiestore
, 1);
1742 st
->cookiestore
= NULL
;
1744 st
->errorbuffer
[0] = '\0';
1748 curl_easy_setopt(ch
, CURLOPT_URL
, NULL
);
1749 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, NULL
);
1750 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1L);
1751 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 0L);
1752 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1753 #if PHP_HTTP_CURL_VERSION(7,19,1)
1754 curl_easy_setopt(ch
, CURLOPT_PROXYUSERNAME
, NULL
);
1755 curl_easy_setopt(ch
, CURLOPT_PROXYPASSWORD
, NULL
);
1756 curl_easy_setopt(ch
, CURLOPT_USERNAME
, NULL
);
1757 curl_easy_setopt(ch
, CURLOPT_PASSWORD
, NULL
);
1760 #if PHP_HTTP_CURL_VERSION(7,21,3)
1761 if (curl
->options
.resolve
) {
1762 curl_slist_free_all(curl
->options
.resolve
);
1763 curl
->options
.resolve
= NULL
;
1766 curl
->options
.retry
.count
= 0;
1767 curl
->options
.retry
.delay
= 0;
1768 curl
->options
.redirects
= 0;
1769 curl
->options
.encode_cookies
= 1;
1771 if (curl
->options
.headers
) {
1772 curl_slist_free_all(curl
->options
.headers
);
1773 curl
->options
.headers
= NULL
;
1775 if (curl
->options
.proxyheaders
) {
1776 curl_slist_free_all(curl
->options
.proxyheaders
);
1777 curl
->options
.proxyheaders
= NULL
;
1780 php_http_buffer_reset(&curl
->options
.cookies
);
1781 php_http_buffer_reset(&curl
->options
.ranges
);
1786 static php_http_client_curl_handler_t
*php_http_client_curl_handler_init(php_http_client_t
*h
, php_resource_factory_t
*rf
)
1789 php_http_client_curl_t
*curl
= h
->ctx
;
1790 php_http_client_curl_handler_t
*handler
;
1791 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1793 if (!(handle
= php_resource_factory_handle_ctor(rf
, NULL TSRMLS_CC
))) {
1794 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to initialize curl handle->multi");
1798 handler
= ecalloc(1, sizeof(*handler
));
1800 handler
->client
= h
;
1801 handler
->handle
= handle
;
1802 handler
->response
.body
= php_http_message_body_init(NULL
, NULL TSRMLS_CC
);
1803 php_http_buffer_init(&handler
->response
.headers
);
1804 php_http_buffer_init(&handler
->options
.cookies
);
1805 php_http_buffer_init(&handler
->options
.ranges
);
1806 zend_hash_init(&handler
->options
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
1809 curl_easy_setopt(handle
, CURLOPT_NOSIGNAL
, 1L);
1811 curl_easy_setopt(handle
, CURLOPT_HEADER
, 0L);
1812 curl_easy_setopt(handle
, CURLOPT_FILETIME
, 1L);
1813 curl_easy_setopt(handle
, CURLOPT_AUTOREFERER
, 1L);
1814 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
1815 curl_easy_setopt(handle
, CURLOPT_NOPROGRESS
, 0L);
1816 curl_easy_setopt(handle
, CURLOPT_HEADERFUNCTION
, php_http_curle_header_callback
);
1817 curl_easy_setopt(handle
, CURLOPT_WRITEFUNCTION
, php_http_curle_body_callback
);
1818 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, php_http_curle_raw_callback
);
1819 curl_easy_setopt(handle
, CURLOPT_READFUNCTION
, php_http_curle_read_callback
);
1820 curl_easy_setopt(handle
, CURLOPT_SEEKFUNCTION
, php_http_curle_seek_callback
);
1821 #if PHP_HTTP_CURL_VERSION(7,32,0)
1822 curl_easy_setopt(handle
, CURLOPT_XFERINFOFUNCTION
, php_http_curle_xferinfo_callback
);
1823 curl_easy_setopt(handle
, CURLOPT_XFERINFODATA
, handler
);
1825 curl_easy_setopt(handle
, CURLOPT_PROGRESSFUNCTION
, php_http_curle_progress_callback
);
1826 curl_easy_setopt(handle
, CURLOPT_PROGRESSDATA
, handler
);
1828 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, handler
);
1829 curl_easy_setopt(handle
, CURLOPT_WRITEDATA
, handler
);
1830 curl_easy_setopt(handle
, CURLOPT_HEADERDATA
, handler
);
1831 curl_easy_setopt(handle
, CURLOPT_SHARE
, curl
->handle
->share
);
1833 php_http_client_curl_handler_reset(handler
);
1839 static ZEND_RESULT_CODE
php_http_client_curl_handler_prepare(php_http_client_curl_handler_t
*curl
, php_http_client_enqueue_t
*enqueue
)
1842 php_http_message_t
*msg
= enqueue
->request
;
1843 php_http_curle_storage_t
*storage
= php_http_curle_get_storage(curl
->handle
);
1844 TSRMLS_FETCH_FROM_CTX(curl
->client
->ts
);
1847 if (!PHP_HTTP_INFO(msg
).request
.url
) {
1848 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Cannot request empty URL");
1851 storage
->errorbuffer
[0] = '\0';
1853 pefree(storage
->url
, 1);
1855 php_http_url_to_string(PHP_HTTP_INFO(msg
).request
.url
, &storage
->url
, NULL
, 1);
1856 curl_easy_setopt(curl
->handle
, CURLOPT_URL
, storage
->url
);
1859 php_http_options_apply(&php_http_curle_options
, enqueue
->options
, curl
);
1861 /* request headers */
1862 php_http_message_update_headers(msg
);
1863 if (zend_hash_num_elements(&msg
->hdrs
)) {
1864 php_http_array_hashkey_t header_key
= php_http_array_hashkey_init(0);
1865 zval
**header_val
, *header_cpy
;
1867 php_http_buffer_t header
;
1868 #if !PHP_HTTP_CURL_VERSION(7,23,0)
1871 zend_hash_find(&msg
->hdrs
, ZEND_STRS("Content-Length"), (void *) &ct
);
1874 php_http_buffer_init(&header
);
1875 FOREACH_HASH_KEYVAL(pos
, &msg
->hdrs
, header_key
, header_val
) {
1876 if (header_key
.type
== HASH_KEY_IS_STRING
) {
1877 #if !PHP_HTTP_CURL_VERSION(7,23,0)
1878 /* avoid duplicate content-length header */
1879 if (ct
&& *ct
== *header_val
) {
1883 header_cpy
= php_http_ztyp(IS_STRING
, *header_val
);
1884 php_http_buffer_appendf(&header
, "%s: %s", header_key
.str
, Z_STRVAL_P(header_cpy
));
1885 php_http_buffer_fix(&header
);
1886 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, header
.data
);
1887 php_http_buffer_reset(&header
);
1889 zval_ptr_dtor(&header_cpy
);
1892 php_http_buffer_dtor(&header
);
1894 curl_easy_setopt(curl
->handle
, CURLOPT_HTTPHEADER
, curl
->options
.headers
);
1896 /* attach request body */
1897 if ((body_size
= php_http_message_body_size(msg
->body
))) {
1898 /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
1899 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
1900 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
1901 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
1902 * does not allow a request body.
1904 php_stream_rewind(php_http_message_body_stream(msg
->body
));
1905 curl_easy_setopt(curl
->handle
, CURLOPT_SEEKDATA
, msg
->body
);
1906 curl_easy_setopt(curl
->handle
, CURLOPT_READDATA
, msg
->body
);
1907 curl_easy_setopt(curl
->handle
, CURLOPT_INFILESIZE
, body_size
);
1908 curl_easy_setopt(curl
->handle
, CURLOPT_POSTFIELDSIZE
, body_size
);
1909 curl_easy_setopt(curl
->handle
, CURLOPT_POST
, 1L);
1911 curl_easy_setopt(curl
->handle
, CURLOPT_SEEKDATA
, NULL
);
1912 curl_easy_setopt(curl
->handle
, CURLOPT_READDATA
, NULL
);
1913 curl_easy_setopt(curl
->handle
, CURLOPT_INFILESIZE
, 0L);
1914 curl_easy_setopt(curl
->handle
, CURLOPT_POSTFIELDSIZE
, 0L);
1918 * Always use CUSTOMREQUEST, else curl won't send any request body for GET etc.
1919 * See e.g. bug #69313.
1921 * Here's what curl does:
1922 * - CURLOPT_HTTPGET: ignore request body
1923 * - CURLOPT_UPLOAD: set "Expect: 100-continue" header
1924 * - CURLOPT_POST: set "Content-Type: application/x-www-form-urlencoded" header
1925 * Now select the least bad.
1927 * See also https://tools.ietf.org/html/rfc7231#section-5.1.1
1929 if (PHP_HTTP_INFO(msg
).request
.method
) {
1930 switch(php_http_select_str(PHP_HTTP_INFO(msg
).request
.method
, 2, "HEAD", "PUT")) {
1932 curl_easy_setopt(curl
->handle
, CURLOPT_NOBODY
, 1L);
1935 curl_easy_setopt(curl
->handle
, CURLOPT_UPLOAD
, 1L);
1938 curl_easy_setopt(curl
->handle
, CURLOPT_CUSTOMREQUEST
, PHP_HTTP_INFO(msg
).request
.method
);
1941 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Cannot use empty request method");
1948 static void php_http_client_curl_handler_clear(php_http_client_curl_handler_t
*handler
)
1950 curl_easy_setopt(handler
->handle
, CURLOPT_NOPROGRESS
, 1L);
1951 #if PHP_HTTP_CURL_VERSION(7,32,0)
1952 curl_easy_setopt(handler
->handle
, CURLOPT_XFERINFOFUNCTION
, NULL
);
1954 curl_easy_setopt(handler
->handle
, CURLOPT_PROGRESSFUNCTION
, NULL
);
1956 curl_easy_setopt(handler
->handle
, CURLOPT_VERBOSE
, 0L);
1957 curl_easy_setopt(handler
->handle
, CURLOPT_DEBUGFUNCTION
, NULL
);
1958 curl_easy_setopt(handler
->handle
, CURLOPT_COOKIELIST
, "FLUSH");
1959 curl_easy_setopt(handler
->handle
, CURLOPT_SHARE
, NULL
);
1962 static void php_http_client_curl_handler_dtor(php_http_client_curl_handler_t
*handler
)
1964 TSRMLS_FETCH_FROM_CTX(handler
->client
->ts
);
1966 php_http_client_curl_handler_clear(handler
);
1968 php_resource_factory_handle_dtor(handler
->rf
, handler
->handle TSRMLS_CC
);
1969 php_resource_factory_free(&handler
->rf
);
1971 php_http_message_body_free(&handler
->response
.body
);
1972 php_http_buffer_dtor(&handler
->response
.headers
);
1973 php_http_buffer_dtor(&handler
->options
.ranges
);
1974 php_http_buffer_dtor(&handler
->options
.cookies
);
1975 zend_hash_destroy(&handler
->options
.cache
);
1977 if (handler
->options
.headers
) {
1978 curl_slist_free_all(handler
->options
.headers
);
1979 handler
->options
.headers
= NULL
;
1985 static php_http_client_t
*php_http_client_curl_init(php_http_client_t
*h
, void *handle
)
1987 php_http_client_curl_t
*curl
;
1988 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1990 if (!handle
&& !(handle
= php_resource_factory_handle_ctor(h
->rf
, NULL TSRMLS_CC
))) {
1991 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Failed to initialize curl handle->multi");
1995 curl
= ecalloc(1, sizeof(*curl
));
1996 curl
->handle
= handle
;
1997 curl
->unfinished
= 0;
2003 static void php_http_client_curl_dtor(php_http_client_t
*h
)
2005 php_http_client_curl_t
*curl
= h
->ctx
;
2006 TSRMLS_FETCH_FROM_CTX(h
->ts
);
2009 curl
->ev_ops
->dtor(&curl
->ev_ctx
);
2011 curl
->unfinished
= 0;
2013 php_resource_factory_handle_dtor(h
->rf
, curl
->handle TSRMLS_CC
);
2019 static void queue_dtor(php_http_client_enqueue_t
*e
)
2021 php_http_client_curl_handler_t
*handler
= e
->opaque
;
2023 if (handler
->queue
.dtor
) {
2024 e
->opaque
= handler
->queue
.opaque
;
2025 handler
->queue
.dtor(e
);
2027 php_http_client_curl_handler_dtor(handler
);
2030 static php_resource_factory_t
*create_rf(php_http_client_t
*h
, php_http_client_enqueue_t
*enqueue TSRMLS_DC
)
2032 php_persistent_handle_factory_t
*pf
= NULL
;
2033 php_resource_factory_t
*rf
= NULL
;
2034 php_http_url_t
*url
= enqueue
->request
->http
.info
.request
.url
;
2036 if (!url
|| (!url
->host
&& !url
->path
)) {
2037 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Cannot request empty URL");
2041 /* only if the client itself is setup for persistence */
2042 if (php_resource_factory_is_persistent(h
->rf
)) {
2043 char *id_str
= NULL
;
2045 int port
= url
->port
? url
->port
: 80;
2047 php_persistent_handle_factory_t
*phf
= h
->rf
->data
;
2049 if (SUCCESS
== zend_hash_find(enqueue
->options
, ZEND_STRS("port"), (void *) &zport
)) {
2050 zval
*zcpy
= php_http_ztyp(IS_LONG
, *zport
);
2052 if (Z_LVAL_P(zcpy
)) {
2053 port
= Z_LVAL_P(zcpy
);
2055 zval_ptr_dtor(&zcpy
);
2058 id_len
= spprintf(&id_str
, 0, "%.*s:%s:%d", (int) phf
->ident
.len
, phf
->ident
.str
, STR_PTR(url
->host
), port
);
2059 pf
= php_persistent_handle_concede(NULL
, ZEND_STRL("http\\Client\\Curl\\Request"), id_str
, id_len
, NULL
, NULL TSRMLS_CC
);
2064 rf
= php_persistent_handle_resource_factory_init(NULL
, pf
);
2066 rf
= php_resource_factory_init(NULL
, &php_http_curle_resource_factory_ops
, NULL
, NULL
);
2072 static ZEND_RESULT_CODE
php_http_client_curl_enqueue(php_http_client_t
*h
, php_http_client_enqueue_t
*enqueue
)
2075 php_http_client_curl_t
*curl
= h
->ctx
;
2076 php_http_client_curl_handler_t
*handler
;
2077 php_http_client_progress_state_t
*progress
;
2078 php_resource_factory_t
*rf
;
2079 TSRMLS_FETCH_FROM_CTX(h
->ts
);
2081 rf
= create_rf(h
, enqueue TSRMLS_CC
);
2086 handler
= php_http_client_curl_handler_init(h
, rf
);
2091 if (SUCCESS
!= php_http_client_curl_handler_prepare(handler
, enqueue
)) {
2092 php_http_client_curl_handler_dtor(handler
);
2096 handler
->queue
= *enqueue
;
2097 enqueue
->opaque
= handler
;
2098 enqueue
->dtor
= queue_dtor
;
2100 if (CURLM_OK
!= (rs
= curl_multi_add_handle(curl
->handle
->multi
, handler
->handle
))) {
2101 php_http_client_curl_handler_dtor(handler
);
2102 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not enqueue request: %s", curl_multi_strerror(rs
));
2106 zend_llist_add_element(&h
->requests
, enqueue
);
2109 if (h
->callback
.progress
.func
&& SUCCESS
== php_http_client_getopt(h
, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
, enqueue
->request
, &progress
)) {
2110 progress
->info
= "start";
2111 h
->callback
.progress
.func(h
->callback
.progress
.arg
, h
, &handler
->queue
, progress
);
2112 progress
->started
= 1;
2118 static ZEND_RESULT_CODE
php_http_client_curl_dequeue(php_http_client_t
*h
, php_http_client_enqueue_t
*enqueue
)
2121 php_http_client_curl_t
*curl
= h
->ctx
;
2122 php_http_client_curl_handler_t
*handler
= enqueue
->opaque
;
2123 TSRMLS_FETCH_FROM_CTX(h
->ts
);
2125 php_http_client_curl_handler_clear(handler
);
2126 if (CURLM_OK
== (rs
= curl_multi_remove_handle(curl
->handle
->multi
, handler
->handle
))) {
2127 zend_llist_del_element(&h
->requests
, handler
->handle
, (int (*)(void *, void *)) compare_queue
);
2130 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "Could not dequeue request: %s", curl_multi_strerror(rs
));
2136 static void php_http_client_curl_reset(php_http_client_t
*h
)
2138 zend_llist_element
*next_el
, *this_el
;
2140 for (this_el
= h
->requests
.head
; this_el
; this_el
= next_el
) {
2141 next_el
= this_el
->next
;
2142 php_http_client_curl_dequeue(h
, (void *) this_el
->data
);
2147 # define SELECT_ERROR SOCKET_ERROR
2149 # define SELECT_ERROR -1
2152 static ZEND_RESULT_CODE
php_http_client_curl_wait(php_http_client_t
*h
, struct timeval
*custom_timeout
)
2156 struct timeval timeout
;
2157 php_http_client_curl_t
*curl
= h
->ctx
;
2160 return curl
->ev_ops
->wait(curl
->ev_ctx
, custom_timeout
);
2167 if (CURLM_OK
== curl_multi_fdset(curl
->handle
->multi
, &R
, &W
, &E
, &MAX
)) {
2168 if (custom_timeout
&& timerisset(custom_timeout
)) {
2169 timeout
= *custom_timeout
;
2171 php_http_client_curl_get_timeout(curl
, 1000, &timeout
);
2175 php_http_sleep((double) timeout
.tv_sec
+ (double) (timeout
.tv_usec
/ PHP_HTTP_MCROSEC
));
2177 } else if (SELECT_ERROR
!= select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) {
2184 static int php_http_client_curl_once(php_http_client_t
*h
)
2186 php_http_client_curl_t
*curl
= h
->ctx
;
2189 curl
->ev_ops
->once(curl
->ev_ctx
);
2191 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(curl
->handle
->multi
, &curl
->unfinished
));
2194 php_http_client_curl_responsehandler(h
);
2196 return curl
->unfinished
;
2200 static ZEND_RESULT_CODE
php_http_client_curl_exec(php_http_client_t
*h
)
2202 php_http_client_curl_t
*curl
= h
->ctx
;
2203 TSRMLS_FETCH_FROM_CTX(h
->ts
);
2206 return curl
->ev_ops
->exec(curl
->ev_ctx
);
2209 while (php_http_client_curl_once(h
) && !EG(exception
)) {
2210 if (SUCCESS
!= php_http_client_curl_wait(h
, NULL
)) {
2212 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
2213 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "WinSock error: %d", WSAGetLastError());
2215 php_error_docref(NULL TSRMLS_CC
, E_WARNING
, "%s", strerror(errno
));
2224 static ZEND_RESULT_CODE
php_http_client_curl_setopt(php_http_client_t
*h
, php_http_client_setopt_opt_t opt
, void *arg
)
2226 php_http_client_curl_t
*curl
= h
->ctx
;
2229 case PHP_HTTP_CLIENT_OPT_CONFIGURATION
:
2230 return php_http_options_apply(&php_http_curlm_options
, (HashTable
*) arg
, h
);
2233 case PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING
:
2234 if (CURLM_OK
!= curl_multi_setopt(curl
->handle
->multi
, CURLMOPT_PIPELINING
, (long) *((zend_bool
*) arg
))) {
2239 case PHP_HTTP_CLIENT_OPT_USE_EVENTS
:
2240 #if PHP_HTTP_HAVE_EVENT
2241 return php_http_curlm_use_eventloop(h
, *(zend_bool
*) arg
);
2251 static int apply_available_options(void *pDest TSRMLS_DC
, int num_args
, va_list args
, zend_hash_key
*hash_key
)
2253 php_http_option_t
*opt
= pDest
;
2258 ht
= va_arg(args
, HashTable
*);
2260 MAKE_STD_ZVAL(entry
);
2262 if ((c
= zend_hash_num_elements(&opt
->suboptions
.options
))) {
2263 array_init_size(entry
, c
);
2264 zend_hash_apply_with_arguments(&opt
->suboptions
.options TSRMLS_CC
, apply_available_options
, 1, Z_ARRVAL_P(entry
));
2266 /* catch deliberate NULL options */
2267 if (Z_TYPE(opt
->defval
) == IS_STRING
&& !Z_STRVAL(opt
->defval
)) {
2270 ZVAL_COPY_VALUE(entry
, &opt
->defval
);
2271 zval_copy_ctor(entry
);
2275 if (hash_key
->nKeyLength
) {
2276 zend_hash_quick_update(ht
, hash_key
->arKey
, hash_key
->nKeyLength
, hash_key
->h
, (void *) &entry
, sizeof(zval
*), NULL
);
2278 zend_hash_index_update(ht
, hash_key
->h
, (void *) &entry
, sizeof(zval
*), NULL
);
2281 return ZEND_HASH_APPLY_KEEP
;
2284 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
)
2286 php_http_client_enqueue_t
*enqueue
;
2287 TSRMLS_FETCH_FROM_CTX(h
->ts
);
2290 case PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
:
2291 if ((enqueue
= php_http_client_enqueued(h
, arg
, NULL
))) {
2292 php_http_client_curl_handler_t
*handler
= enqueue
->opaque
;
2294 *((php_http_client_progress_state_t
**) res
) = &handler
->progress
;
2299 case PHP_HTTP_CLIENT_OPT_TRANSFER_INFO
:
2300 if ((enqueue
= php_http_client_enqueued(h
, arg
, NULL
))) {
2301 php_http_client_curl_handler_t
*handler
= enqueue
->opaque
;
2303 php_http_curle_get_info(handler
->handle
, *(HashTable
**) res
);
2308 case PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS
:
2309 zend_hash_apply_with_arguments(&php_http_curle_options
.options TSRMLS_CC
, apply_available_options
, 1, *(HashTable
**) res
);
2312 case PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION
:
2313 zend_hash_apply_with_arguments(&php_http_curlm_options
.options TSRMLS_CC
, apply_available_options
, 1, *(HashTable
**) res
);
2323 static php_http_client_ops_t php_http_client_curl_ops
= {
2324 &php_http_curlm_resource_factory_ops
,
2325 php_http_client_curl_init
,
2327 php_http_client_curl_dtor
,
2328 php_http_client_curl_reset
,
2329 php_http_client_curl_exec
,
2330 php_http_client_curl_wait
,
2331 php_http_client_curl_once
,
2332 php_http_client_curl_enqueue
,
2333 php_http_client_curl_dequeue
,
2334 php_http_client_curl_setopt
,
2335 php_http_client_curl_getopt
2338 php_http_client_ops_t
*php_http_client_curl_get_ops(void)
2340 return &php_http_client_curl_ops
;
2344 PHP_MINIT_FUNCTION(http_client_curl
)
2346 curl_version_info_data
*info
;
2347 php_http_options_t
*options
;
2348 php_http_client_driver_t driver
= {
2350 &php_http_client_curl_ops
2353 if (SUCCESS
!= php_http_client_driver_add(&driver
)) {
2357 if (SUCCESS
!= php_persistent_handle_provide(ZEND_STRL("http\\Client\\Curl"), &php_http_curlm_resource_factory_ops
, NULL
, NULL TSRMLS_CC
)) {
2360 if (SUCCESS
!= php_persistent_handle_provide(ZEND_STRL("http\\Client\\Curl\\Request"), &php_http_curle_resource_factory_ops
, NULL
, NULL TSRMLS_CC
)) {
2364 if ((options
= php_http_options_init(&php_http_curle_options
, 1))) {
2365 options
->getter
= php_http_curle_get_option
;
2366 options
->setter
= php_http_curle_set_option
;
2368 php_http_curle_options_init(options TSRMLS_CC
);
2370 if ((options
= php_http_options_init(&php_http_curlm_options
, 1))) {
2371 options
->getter
= php_http_option_get
;
2372 options
->setter
= php_http_curlm_set_option
;
2374 php_http_curlm_options_init(options TSRMLS_CC
);
2377 if ((info
= curl_version_info(CURLVERSION_NOW
))) {
2381 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "FEATURES", info
->features
, CONST_CS
|CONST_PERSISTENT
);
2383 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "IPV6", CURL_VERSION_IPV6
, CONST_CS
|CONST_PERSISTENT
);
2384 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "KERBEROS4", CURL_VERSION_KERBEROS4
, CONST_CS
|CONST_PERSISTENT
);
2385 #if PHP_HTTP_CURL_VERSION(7,40,0)
2386 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "KERBEROS5", CURL_VERSION_KERBEROS5
, CONST_CS
|CONST_PERSISTENT
);
2388 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "SSL", CURL_VERSION_SSL
, CONST_CS
|CONST_PERSISTENT
);
2389 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "LIBZ", CURL_VERSION_LIBZ
, CONST_CS
|CONST_PERSISTENT
);
2390 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "NTLM", CURL_VERSION_NTLM
, CONST_CS
|CONST_PERSISTENT
);
2391 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "GSSNEGOTIATE", CURL_VERSION_GSSNEGOTIATE
, CONST_CS
|CONST_PERSISTENT
);
2392 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "ASYNCHDNS", CURL_VERSION_ASYNCHDNS
, CONST_CS
|CONST_PERSISTENT
);
2393 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "SPNEGO", CURL_VERSION_SPNEGO
, CONST_CS
|CONST_PERSISTENT
);
2394 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "LARGEFILE", CURL_VERSION_LARGEFILE
, CONST_CS
|CONST_PERSISTENT
);
2395 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "IDN", CURL_VERSION_IDN
, CONST_CS
|CONST_PERSISTENT
);
2396 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "SSPI", CURL_VERSION_SSPI
, CONST_CS
|CONST_PERSISTENT
);
2397 #if PHP_HTTP_CURL_VERSION(7,38,0)
2398 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "GSSAPI", CURL_VERSION_GSSAPI
, CONST_CS
|CONST_PERSISTENT
);
2400 #if PHP_HTTP_CURL_VERSION(7,21,4)
2401 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "TLSAUTH_SRP", CURL_VERSION_TLSAUTH_SRP
, CONST_CS
|CONST_PERSISTENT
);
2403 #if PHP_HTTP_CURL_VERSION(7,22,0)
2404 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "NTLM_WB", CURL_VERSION_NTLM_WB
, CONST_CS
|CONST_PERSISTENT
);
2406 #if PHP_HTTP_CURL_VERSION(7,33,0)
2407 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "HTTP2", CURL_VERSION_HTTP2
, CONST_CS
|CONST_PERSISTENT
);
2409 #if PHP_HTTP_CURL_VERSION(7,40,0)
2410 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "UNIX_SOCKETS", CURL_VERSION_UNIX_SOCKETS
, CONST_CS
|CONST_PERSISTENT
);
2412 #if PHP_HTTP_CURL_VERSION(7,47,0)
2413 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl\\Features", "PSL", CURL_VERSION_PSL
, CONST_CS
|CONST_PERSISTENT
);
2419 REGISTER_NS_STRING_CONSTANT("http\\Client\\Curl", "VERSIONS", curl_version(), CONST_CS
|CONST_PERSISTENT
);
2420 #if CURLVERSION_NOW >= 0
2421 REGISTER_NS_STRING_CONSTANT("http\\Client\\Curl\\Versions", "CURL", (char *) info
->version
, CONST_CS
|CONST_PERSISTENT
);
2422 REGISTER_NS_STRING_CONSTANT("http\\Client\\Curl\\Versions", "SSL", (char *) info
->ssl_version
, CONST_CS
|CONST_PERSISTENT
);
2423 REGISTER_NS_STRING_CONSTANT("http\\Client\\Curl\\Versions", "LIBZ", (char *) info
->libz_version
, CONST_CS
|CONST_PERSISTENT
);
2424 # if CURLVERSION_NOW >= 1
2425 REGISTER_NS_STRING_CONSTANT("http\\Client\\Curl\\Versions", "ARES", (char *) info
->ares
, CONST_CS
|CONST_PERSISTENT
);
2426 # if CURLVERSION_NOW >= 2
2427 REGISTER_NS_STRING_CONSTANT("http\\Client\\Curl\\Versions", "IDN", (char *) info
->libidn
, CONST_CS
|CONST_PERSISTENT
);
2434 * HTTP Protocol Version Constants
2436 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0
, CONST_CS
|CONST_PERSISTENT
);
2437 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1
, CONST_CS
|CONST_PERSISTENT
);
2438 #if PHP_HTTP_CURL_VERSION(7,33,0)
2439 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_2_0", CURL_HTTP_VERSION_2_0
, CONST_CS
|CONST_PERSISTENT
);
2441 #if PHP_HTTP_CURL_VERSION(7,47,0)
2442 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_2TLS", CURL_HTTP_VERSION_2TLS
, CONST_CS
|CONST_PERSISTENT
);
2444 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_ANY", CURL_HTTP_VERSION_NONE
, CONST_CS
|CONST_PERSISTENT
);
2447 * SSL Version Constants
2449 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1
, CONST_CS
|CONST_PERSISTENT
);
2450 #if PHP_HTTP_CURL_VERSION(7,34,0)
2451 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_0", CURL_SSLVERSION_TLSv1_0
, CONST_CS
|CONST_PERSISTENT
);
2452 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_1", CURL_SSLVERSION_TLSv1_1
, CONST_CS
|CONST_PERSISTENT
);
2453 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_2", CURL_SSLVERSION_TLSv1_2
, CONST_CS
|CONST_PERSISTENT
);
2455 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2
, CONST_CS
|CONST_PERSISTENT
);
2456 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3
, CONST_CS
|CONST_PERSISTENT
);
2457 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT
, CONST_CS
|CONST_PERSISTENT
);
2458 #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
2459 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "TLSAUTH_SRP", CURL_TLSAUTH_SRP
, CONST_CS
|CONST_PERSISTENT
);
2463 * DNS IPvX resolving
2465 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_V4", CURL_IPRESOLVE_V4
, CONST_CS
|CONST_PERSISTENT
);
2466 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_V6", CURL_IPRESOLVE_V6
, CONST_CS
|CONST_PERSISTENT
);
2467 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER
, CONST_CS
|CONST_PERSISTENT
);
2472 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_BASIC", CURLAUTH_BASIC
, CONST_CS
|CONST_PERSISTENT
);
2473 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_DIGEST", CURLAUTH_DIGEST
, CONST_CS
|CONST_PERSISTENT
);
2474 #if PHP_HTTP_CURL_VERSION(7,19,3)
2475 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_DIGEST_IE", CURLAUTH_DIGEST_IE
, CONST_CS
|CONST_PERSISTENT
);
2477 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_NTLM", CURLAUTH_NTLM
, CONST_CS
|CONST_PERSISTENT
);
2478 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE
, CONST_CS
|CONST_PERSISTENT
);
2479 #if PHP_HTTP_CURL_VERSION(7,38,0)
2480 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_SPNEGO", CURLAUTH_NEGOTIATE
, CONST_CS
|CONST_PERSISTENT
);
2482 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_ANY", CURLAUTH_ANY
, CONST_CS
|CONST_PERSISTENT
);
2485 * Proxy Type Constants
2487 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS4", CURLPROXY_SOCKS4
, CONST_CS
|CONST_PERSISTENT
);
2488 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS4A", CURLPROXY_SOCKS5
, CONST_CS
|CONST_PERSISTENT
);
2489 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS5_HOSTNAME", CURLPROXY_SOCKS5
, CONST_CS
|CONST_PERSISTENT
);
2490 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS5", CURLPROXY_SOCKS5
, CONST_CS
|CONST_PERSISTENT
);
2491 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_HTTP", CURLPROXY_HTTP
, CONST_CS
|CONST_PERSISTENT
);
2492 #if PHP_HTTP_CURL_VERSION(7,19,4)
2493 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_HTTP_1_0", CURLPROXY_HTTP_1_0
, CONST_CS
|CONST_PERSISTENT
);
2497 * Post Redirection Constants
2499 #if PHP_HTTP_CURL_VERSION(7,19,1)
2500 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_301", CURL_REDIR_POST_301
, CONST_CS
|CONST_PERSISTENT
);
2501 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_302", CURL_REDIR_POST_302
, CONST_CS
|CONST_PERSISTENT
);
2502 #if PHP_HTTP_CURL_VERSION(7,26,0)
2503 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_303", CURL_REDIR_POST_303
, CONST_CS
|CONST_PERSISTENT
);
2505 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_ALL", CURL_REDIR_POST_ALL
, CONST_CS
|CONST_PERSISTENT
);
2511 PHP_MSHUTDOWN_FUNCTION(http_client_curl
)
2513 php_persistent_handle_cleanup(ZEND_STRL("http\\Client\\Curl"), NULL
, 0 TSRMLS_CC
);
2514 php_persistent_handle_cleanup(ZEND_STRL("http\\Client\\Curl\\Request"), NULL
, 0 TSRMLS_CC
);
2516 php_http_options_dtor(&php_http_curle_options
);
2517 php_http_options_dtor(&php_http_curlm_options
);
2522 #endif /* PHP_HTTP_HAVE_CURL */
2529 * vim600: noet sw=4 ts=4 fdm=marker
2530 * vim<600: noet sw=4 ts=4