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"
16 #if PHP_HTTP_HAVE_CURL
18 #if PHP_HTTP_HAVE_EVENT
19 # if !PHP_HTTP_HAVE_EVENT2 && /* just be really sure */ !(LIBEVENT_VERSION_NUMBER >= 0x02000000)
21 # define event_base_new event_init
22 # define event_assign(e, b, s, a, cb, d) do {\
23 event_set(e, s, a, cb, d); \
24 event_base_set(b, e); \
27 # if PHP_HTTP_HAVE_EVENT2
28 # include <event2/event.h>
29 # include <event2/event_struct.h>
31 # error "libevent presence is unknown"
39 #ifdef PHP_HTTP_HAVE_OPENSSL
40 # include <openssl/ssl.h>
42 #ifdef PHP_HTTP_HAVE_GNUTLS
46 typedef struct php_http_client_curl
{
49 int unfinished
; /* int because of curl_multi_perform() */
51 #if PHP_HTTP_HAVE_EVENT
52 struct event_base
*evbase
;
53 struct event
*timeout
;
56 } php_http_client_curl_t
;
58 typedef struct php_http_client_curl_handler
{
60 php_resource_factory_t
*rf
;
61 php_http_client_t
*client
;
62 php_http_client_progress_state_t progress
;
63 php_http_client_enqueue_t queue
;
66 php_http_buffer_t headers
;
67 php_http_message_body_t
*body
;
73 struct curl_slist
*proxyheaders
;
74 struct curl_slist
*headers
;
75 struct curl_slist
*resolve
;
76 php_http_buffer_t cookies
;
77 php_http_buffer_t ranges
;
85 unsigned range_request
:1;
86 unsigned encode_cookies
:1;
90 } php_http_client_curl_handler_t
;
92 typedef struct php_http_curle_storage
{
96 char errorbuffer
[0x100];
97 } php_http_curle_storage_t
;
99 static inline php_http_curle_storage_t
*php_http_curle_get_storage(CURL
*ch
) {
100 php_http_curle_storage_t
*st
= NULL
;
102 curl_easy_getinfo(ch
, CURLINFO_PRIVATE
, &st
);
105 st
= pecalloc(1, sizeof(*st
), 1);
106 curl_easy_setopt(ch
, CURLOPT_PRIVATE
, st
);
107 curl_easy_setopt(ch
, CURLOPT_ERRORBUFFER
, st
->errorbuffer
);
113 static void *php_http_curle_ctor(void *opaque
, void *init_arg
)
117 if ((ch
= curl_easy_init())) {
118 php_http_curle_get_storage(ch
);
124 static void *php_http_curle_copy(void *opaque
, void *handle
)
128 if ((ch
= curl_easy_duphandle(handle
))) {
130 php_http_curle_get_storage(ch
);
136 static void php_http_curle_dtor(void *opaque
, void *handle
)
138 php_http_curle_storage_t
*st
= php_http_curle_get_storage(handle
);
140 curl_easy_cleanup(handle
);
146 if (st
->cookiestore
) {
147 pefree(st
->cookiestore
, 1);
153 static php_resource_factory_ops_t php_http_curle_resource_factory_ops
= {
159 static void *php_http_curlm_ctor(void *opaque
, void *init_arg
)
161 return curl_multi_init();
164 static void php_http_curlm_dtor(void *opaque
, void *handle
)
166 curl_multi_cleanup(handle
);
169 static php_resource_factory_ops_t php_http_curlm_resource_factory_ops
= {
177 static size_t php_http_curle_read_callback(void *data
, size_t len
, size_t n
, void *ctx
)
179 php_http_message_body_t
*body
= ctx
;
181 if (body
&& body
->res
) {
182 php_stream
*s
= php_http_message_body_stream(body
);
185 return php_stream_read(s
, data
, len
* n
);
191 #if PHP_HTTP_CURL_VERSION(7,32,0)
192 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
)
194 static int php_http_curle_progress_callback(void *ctx
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
197 php_http_client_curl_handler_t
*h
= ctx
;
198 zend_bool update
= 0;
200 if (h
->progress
.dl
.total
!= dltotal
201 || h
->progress
.dl
.now
!= dlnow
202 || h
->progress
.ul
.total
!= ultotal
203 || h
->progress
.ul
.now
!= ulnow
207 h
->progress
.dl
.total
= dltotal
;
208 h
->progress
.dl
.now
= dlnow
;
209 h
->progress
.ul
.total
= ultotal
;
210 h
->progress
.ul
.now
= ulnow
;
213 if (update
&& h
->client
->callback
.progress
.func
) {
214 h
->client
->callback
.progress
.func(h
->client
->callback
.progress
.arg
, h
->client
, &h
->queue
, &h
->progress
);
220 static int php_http_curle_seek_callback(void *userdata
, curl_off_t offset
, int origin
)
222 php_http_message_body_t
*body
= userdata
;
223 TSRMLS_FETCH_FROM_CTX(body
->ts
);
228 if (0 == php_stream_seek(php_http_message_body_stream(body
), offset
, origin
)) {
234 static int php_http_curle_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
236 php_http_client_curl_handler_t
*h
= ctx
;
241 if (data
[0] == '-') {
242 } else if (php_memnstr(data
, ZEND_STRL("Adding handle:"), data
+ length
)) {
243 h
->progress
.info
= "setup";
244 } else if (php_memnstr(data
, ZEND_STRL("addHandle"), data
+ length
)) {
245 h
->progress
.info
= "setup";
246 } else if (php_memnstr(data
, ZEND_STRL("About to connect"), data
+ length
)) {
247 h
->progress
.info
= "resolve";
248 } else if (php_memnstr(data
, ZEND_STRL("Trying"), data
+ length
)) {
249 h
->progress
.info
= "connect";
250 } else if (php_memnstr(data
, ZEND_STRL("Found bundle for host"), data
+ length
)) {
251 h
->progress
.info
= "connect";
252 } else if (php_memnstr(data
, ZEND_STRL("Connected"), data
+ length
)) {
253 h
->progress
.info
= "connected";
254 } else if (php_memnstr(data
, ZEND_STRL("Re-using existing connection!"), data
+ length
)) {
255 h
->progress
.info
= "connected";
256 } else if (php_memnstr(data
, ZEND_STRL("blacklisted"), data
+ length
)) {
257 h
->progress
.info
= "blacklist check";
258 } else if (php_memnstr(data
, ZEND_STRL("SSL"), data
+ length
)) {
259 h
->progress
.info
= "ssl negotiation";
260 } else if (php_memnstr(data
, ZEND_STRL("upload"), data
+ length
)) {
261 h
->progress
.info
= "uploaded";
262 } else if (php_memnstr(data
, ZEND_STRL("left intact"), data
+ length
)) {
263 h
->progress
.info
= "not disconnected";
264 } else if (php_memnstr(data
, ZEND_STRL("closed"), data
+ length
)) {
265 h
->progress
.info
= "disconnected";
266 } else if (php_memnstr(data
, ZEND_STRL("Issue another request"), data
+ length
)) {
267 h
->progress
.info
= "redirect";
268 } else if (php_memnstr(data
, ZEND_STRL("Operation timed out"), data
+ length
)) {
269 h
->progress
.info
= "timeout";
272 h
->progress
.info
= data
;
273 data
[length
- 1] = '\0';
276 if (h
->client
->callback
.progress
.func
) {
277 h
->client
->callback
.progress
.func(h
->client
->callback
.progress
.arg
, h
->client
, &h
->queue
, &h
->progress
);
280 case CURLINFO_HEADER_OUT
:
281 case CURLINFO_DATA_OUT
:
282 case CURLINFO_SSL_DATA_OUT
:
283 h
->progress
.info
= "send";
285 case CURLINFO_HEADER_IN
:
286 case CURLINFO_DATA_IN
:
287 case CURLINFO_SSL_DATA_IN
:
288 h
->progress
.info
= "receive";
296 _dpf(type
, data
, length
);
302 static int php_http_curle_header_callback(char *data
, size_t n
, size_t l
, void *arg
)
304 php_http_client_curl_handler_t
*h
= arg
;
306 return php_http_buffer_append(&h
->response
.headers
, data
, n
* l
);
309 static int php_http_curle_body_callback(char *data
, size_t n
, size_t l
, void *arg
)
311 php_http_client_curl_handler_t
*h
= arg
;
313 return php_http_message_body_append(h
->response
.body
, data
, n
*l
);
316 static ZEND_RESULT_CODE
php_http_curle_get_info(CURL
*ch
, HashTable
*info
)
321 struct curl_slist
*s
= NULL
, *p
= NULL
;
324 /* BEGIN::CURLINFO */
325 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_EFFECTIVE_URL
, &c
)) {
326 ZVAL_STRING(&tmp
, STR_PTR(c
));
327 zend_hash_str_update(info
, "effective_url", lenof("effective_url"), &tmp
);
329 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_RESPONSE_CODE
, &l
)) {
331 zend_hash_str_update(info
, "response_code", lenof("response_code"), &tmp
);
333 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_TOTAL_TIME
, &d
)) {
334 ZVAL_DOUBLE(&tmp
, d
);
335 zend_hash_str_update(info
, "total_time", lenof("total_time"), &tmp
);
337 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NAMELOOKUP_TIME
, &d
)) {
338 ZVAL_DOUBLE(&tmp
, d
);
339 zend_hash_str_update(info
, "namelookup_time", lenof("namelookup_time"), &tmp
);
341 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONNECT_TIME
, &d
)) {
342 ZVAL_DOUBLE(&tmp
, d
);
343 zend_hash_str_update(info
, "connect_time", lenof("connect_time"), &tmp
);
345 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRETRANSFER_TIME
, &d
)) {
346 ZVAL_DOUBLE(&tmp
, d
);
347 zend_hash_str_update(info
, "pretransfer_time", lenof("pretransfer_time"), &tmp
);
349 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_UPLOAD
, &d
)) {
350 ZVAL_DOUBLE(&tmp
, d
);
351 zend_hash_str_update(info
, "size_upload", lenof("size_upload"), &tmp
);
353 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_DOWNLOAD
, &d
)) {
354 ZVAL_DOUBLE(&tmp
, d
);
355 zend_hash_str_update(info
, "size_download", lenof("size_download"), &tmp
);
357 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_DOWNLOAD
, &d
)) {
358 ZVAL_DOUBLE(&tmp
, d
);
359 zend_hash_str_update(info
, "speed_download", lenof("speed_download"), &tmp
);
361 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_UPLOAD
, &d
)) {
362 ZVAL_DOUBLE(&tmp
, d
);
363 zend_hash_str_update(info
, "speed_upload", lenof("speed_upload"), &tmp
);
365 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HEADER_SIZE
, &l
)) {
367 zend_hash_str_update(info
, "header_size", lenof("header_size"), &tmp
);
369 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REQUEST_SIZE
, &l
)) {
371 zend_hash_str_update(info
, "request_size", lenof("request_size"), &tmp
);
373 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_VERIFYRESULT
, &l
)) {
375 zend_hash_str_update(info
, "ssl_verifyresult", lenof("ssl_verifyresult"), &tmp
);
377 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_FILETIME
, &l
)) {
379 zend_hash_str_update(info
, "filetime", lenof("filetime"), &tmp
);
381 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
382 ZVAL_DOUBLE(&tmp
, d
);
383 zend_hash_str_update(info
, "content_length_download", lenof("content_length_download"), &tmp
);
385 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_UPLOAD
, &d
)) {
386 ZVAL_DOUBLE(&tmp
, d
);
387 zend_hash_str_update(info
, "content_length_upload", lenof("content_length_upload"), &tmp
);
389 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_STARTTRANSFER_TIME
, &d
)) {
390 ZVAL_DOUBLE(&tmp
, d
);
391 zend_hash_str_update(info
, "starttransfer_time", lenof("starttransfer_time"), &tmp
);
393 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_TYPE
, &c
)) {
394 ZVAL_STRING(&tmp
, STR_PTR(c
));
395 zend_hash_str_update(info
, "content_type", lenof("content_type"), &tmp
);
397 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_TIME
, &d
)) {
398 ZVAL_DOUBLE(&tmp
, d
);
399 zend_hash_str_update(info
, "redirect_time", lenof("redirect_time"), &tmp
);
401 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_COUNT
, &l
)) {
403 zend_hash_str_update(info
, "redirect_count", lenof("redirect_count"), &tmp
);
405 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTP_CONNECTCODE
, &l
)) {
407 zend_hash_str_update(info
, "connect_code", lenof("connect_code"), &tmp
);
409 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTPAUTH_AVAIL
, &l
)) {
411 zend_hash_str_update(info
, "httpauth_avail", lenof("httpauth_avail"), &tmp
);
413 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PROXYAUTH_AVAIL
, &l
)) {
415 zend_hash_str_update(info
, "proxyauth_avail", lenof("proxyauth_avail"), &tmp
);
417 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_OS_ERRNO
, &l
)) {
419 zend_hash_str_update(info
, "os_errno", lenof("os_errno"), &tmp
);
421 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NUM_CONNECTS
, &l
)) {
423 zend_hash_str_update(info
, "num_connects", lenof("num_connects"), &tmp
);
425 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_ENGINES
, &s
)) {
427 for (p
= s
; p
; p
= p
->next
) {
429 add_next_index_string(&tmp
, p
->data
);
432 zend_hash_str_update(info
, "ssl_engines", lenof("ssl_engines"), &tmp
);
433 curl_slist_free_all(s
);
435 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_URL
, &c
)) {
436 ZVAL_STRING(&tmp
, STR_PTR(c
));
437 zend_hash_str_update(info
, "redirect_url", lenof("redirect_url"), &tmp
);
439 #if PHP_HTTP_CURL_VERSION(7,19,0)
440 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRIMARY_IP
, &c
)) {
441 ZVAL_STRING(&tmp
, STR_PTR(c
));
442 zend_hash_str_update(info
, "primary_ip", lenof("primary_ip"), &tmp
);
445 #if PHP_HTTP_CURL_VERSION(7,19,0)
446 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_APPCONNECT_TIME
, &d
)) {
447 ZVAL_DOUBLE(&tmp
, d
);
448 zend_hash_str_update(info
, "appconnect_time", lenof("appconnect_time"), &tmp
);
451 #if PHP_HTTP_CURL_VERSION(7,19,4)
452 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONDITION_UNMET
, &l
)) {
454 zend_hash_str_update(info
, "condition_unmet", lenof("condition_unmet"), &tmp
);
457 #if PHP_HTTP_CURL_VERSION(7,21,0)
458 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRIMARY_PORT
, &l
)) {
460 zend_hash_str_update(info
, "primary_port", lenof("primary_port"), &tmp
);
463 #if PHP_HTTP_CURL_VERSION(7,21,0)
464 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_LOCAL_IP
, &c
)) {
465 ZVAL_STRING(&tmp
, STR_PTR(c
));
466 zend_hash_str_update(info
, "local_ip", lenof("local_ip"), &tmp
);
469 #if PHP_HTTP_CURL_VERSION(7,21,0)
470 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_LOCAL_PORT
, &l
)) {
472 zend_hash_str_update(info
, "local_port", lenof("local_port"), &tmp
);
478 #if PHP_HTTP_CURL_VERSION(7,34,0)
480 zval ti_array
, subarray
;
481 struct curl_tlssessioninfo
*ti
;
483 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_TLS_SESSION
, &ti
)) {
486 ZVAL_NULL(&subarray
);
487 array_init(&ti_array
);
489 switch (ti
->backend
) {
490 case CURLSSLBACKEND_NONE
:
493 case CURLSSLBACKEND_OPENSSL
:
495 #ifdef PHP_HTTP_HAVE_OPENSSL
497 SSL_CTX
*ctx
= ti
->internals
;
499 array_init(&subarray
);
500 add_assoc_long_ex(&subarray
, ZEND_STRL("number"), SSL_CTX_sess_number(ctx
));
501 add_assoc_long_ex(&subarray
, ZEND_STRL("connect"), SSL_CTX_sess_connect(ctx
));
502 add_assoc_long_ex(&subarray
, ZEND_STRL("connect_good"), SSL_CTX_sess_connect_good(ctx
));
503 add_assoc_long_ex(&subarray
, ZEND_STRL("connect_renegotiate"), SSL_CTX_sess_connect_renegotiate(ctx
));
504 add_assoc_long_ex(&subarray
, ZEND_STRL("hits"), SSL_CTX_sess_hits(ctx
));
505 add_assoc_long_ex(&subarray
, ZEND_STRL("cache_full"), SSL_CTX_sess_cache_full(ctx
));
509 case CURLSSLBACKEND_GNUTLS
:
511 #ifdef PHP_HTTP_HAVE_GNUTLS
513 gnutls_session_t sess
= ti
->internals
;
516 array_init(&subarray
);
517 if ((desc
= gnutls_session_get_desc(sess
))) {
518 add_assoc_string_ex(&subarray
, ZEND_STRL("desc"), desc
);
521 add_assoc_bool_ex(&subarray
, ZEND_STRL("resumed"), gnutls_session_is_resumed(sess
));
525 case CURLSSLBACKEND_NSS
:
528 #if !PHP_HTTP_CURL_VERSION(7,39,0)
529 case CURLSSLBACKEND_QSOSSL
:
533 case CURLSSLBACKEND_GSKIT
:
536 case CURLSSLBACKEND_POLARSSL
:
537 backend
= "polarssl";
539 case CURLSSLBACKEND_CYASSL
:
542 case CURLSSLBACKEND_SCHANNEL
:
543 backend
= "schannel";
545 case CURLSSLBACKEND_DARWINSSL
:
546 backend
= "darwinssl";
551 add_assoc_string_ex(&ti_array
, ZEND_STRL("backend"), backend
);
552 add_assoc_zval_ex(&ti_array
, ZEND_STRL("internals"), &subarray
);
553 zend_hash_str_update(info
, "tls_session", lenof("tls_session"), &ti_array
);
558 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
561 zval ci_array
, subarray
;
562 struct curl_certinfo
*ci
;
563 char *colon
, *keyname
;
565 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CERTINFO
, &ci
)) {
566 array_init(&ci_array
);
568 for (i
= 0; i
< ci
->num_of_certs
; ++i
) {
571 array_init(&subarray
);
572 for (p
= s
; p
; p
= p
->next
) {
574 if ((colon
= strchr(p
->data
, ':'))) {
575 keyname
= estrndup(p
->data
, colon
- p
->data
);
576 add_assoc_string_ex(&subarray
, keyname
, colon
- p
->data
, colon
+ 1);
579 add_next_index_string(&subarray
, p
->data
);
583 add_next_index_zval(&ci_array
, &subarray
);
585 zend_hash_str_update(info
, "certinfo", lenof("certinfo"), &ci_array
);
590 php_http_curle_storage_t
*st
= php_http_curle_get_storage(ch
);
592 ZVAL_LONG(&tmp
, st
->errorcode
);
593 zend_hash_str_update(info
, "curlcode", lenof("curlcode"), &tmp
);
594 ZVAL_STRING(&tmp
, st
->errorbuffer
);
595 zend_hash_str_update(info
, "error", lenof("error"), &tmp
);
601 static int compare_queue(php_http_client_enqueue_t
*e
, void *handle
)
603 return handle
== ((php_http_client_curl_handler_t
*) e
->opaque
)->handle
;
606 static php_http_message_t
*php_http_curlm_responseparser(php_http_client_curl_handler_t
*h TSRMLS_DC
)
608 php_http_message_t
*response
;
609 php_http_header_parser_t parser
;
612 response
= php_http_message_init(NULL
, 0, h
->response
.body TSRMLS_CC
);
613 php_http_header_parser_init(&parser TSRMLS_CC
);
614 php_http_header_parser_parse(&parser
, &h
->response
.headers
, PHP_HTTP_HEADER_PARSER_CLEANUP
, &response
->hdrs
, (php_http_info_callback_t
) php_http_message_info_callback
, (void *) &response
);
615 php_http_header_parser_dtor(&parser
);
617 /* move body to right message */
618 if (response
->body
!= h
->response
.body
) {
619 php_http_message_t
*ptr
= response
;
621 while (ptr
->parent
) {
624 response
->body
= ptr
->body
;
627 php_http_message_body_addref(h
->response
.body
);
629 /* let's update the response headers */
630 if ((zh
= php_http_message_header(response
, ZEND_STRL("Content-Length")))) {
632 zend_hash_str_update(&response
->hdrs
, "X-Original-Content-Length", lenof("X-Original-Content-Length"), zh
);
634 if ((zh
= php_http_message_header(response
, ZEND_STRL("Transfer-Encoding")))) {
636 zend_hash_str_update(&response
->hdrs
, "X-Original-Transfer-Encoding", lenof("X-Original-Transfer-Encoding"), zh
);
637 zend_hash_str_del(&response
->hdrs
, "Transfer-Encoding", lenof("Transfer-Encoding"));
639 if ((zh
= php_http_message_header(response
, ZEND_STRL("Content-Range")))) {
641 zend_hash_str_update(&response
->hdrs
, "X-Original-Content-Range", lenof("X-Original-Content-Range"), zh
);
642 zend_hash_str_del(&response
->hdrs
, "Content-Range", lenof("Content-Range"));
644 if ((zh
= php_http_message_header(response
, ZEND_STRL("Content-Encoding")))) {
646 zend_hash_str_update(&response
->hdrs
, "X-Original-Content-Encoding", lenof("X-Original-Content-Encoding"), zh
);
647 zend_hash_str_del(&response
->hdrs
, "Content-Encoding", lenof("Content-Encoding"));
649 php_http_message_update_headers(response
);
654 static void php_http_curlm_responsehandler(php_http_client_t
*context
)
657 php_http_client_enqueue_t
*enqueue
;
658 php_http_client_curl_t
*curl
= context
->ctx
;
661 CURLMsg
*msg
= curl_multi_info_read(curl
->handle
, &remaining
);
663 if (msg
&& CURLMSG_DONE
== msg
->msg
) {
664 if (CURLE_OK
!= msg
->data
.result
) {
665 php_http_curle_storage_t
*st
= php_http_curle_get_storage(msg
->easy_handle
);
666 php_error_docref(NULL
, E_WARNING
, "%s; %s (%s)", curl_easy_strerror(st
->errorcode
= msg
->data
.result
), STR_PTR(st
->errorbuffer
), STR_PTR(st
->url
));
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
);
674 context
->callback
.response
.func(context
->callback
.response
.arg
, context
, &handler
->queue
, &response
);
675 php_http_message_free(&response
);
682 #if PHP_HTTP_HAVE_EVENT
684 typedef struct php_http_curlm_event
{
686 php_http_client_t
*context
;
687 } php_http_curlm_event_t
;
689 static inline int etoca(short action
) {
690 switch (action
& (EV_READ
|EV_WRITE
)) {
692 return CURL_CSELECT_IN
;
695 return CURL_CSELECT_OUT
;
697 case EV_READ
|EV_WRITE
:
698 return CURL_CSELECT_IN
|CURL_CSELECT_OUT
;
705 static void php_http_curlm_timeout_callback(int socket
, short action
, void *event_data
)
707 php_http_client_t
*context
= event_data
;
708 php_http_client_curl_t
*curl
= context
->ctx
;
711 fprintf(stderr
, "T");
713 if (curl
->useevents
) {
716 /* ignore and use -1,0 on timeout */
720 while (CURLM_CALL_MULTI_PERFORM
== (rc
= curl_multi_socket_action(curl
->handle
, CURL_SOCKET_TIMEOUT
, 0, &curl
->unfinished
)));
722 if (CURLM_OK
!= rc
) {
723 php_error_docref(NULL
, E_WARNING
, "%s", curl_multi_strerror(rc
));
726 php_http_curlm_responsehandler(context
);
730 static void php_http_curlm_event_callback(int socket
, short action
, void *event_data
)
732 php_http_client_t
*context
= event_data
;
733 php_http_client_curl_t
*curl
= context
->ctx
;
736 fprintf(stderr
, "E");
738 if (curl
->useevents
) {
739 CURLMcode rc
= CURLM_OK
;
741 while (CURLM_CALL_MULTI_PERFORM
== (rc
= curl_multi_socket_action(curl
->handle
, socket
, etoca(action
), &curl
->unfinished
)));
743 if (CURLM_OK
!= rc
) {
744 php_error_docref(NULL
, E_WARNING
, "%s", curl_multi_strerror(rc
));
747 php_http_curlm_responsehandler(context
);
749 /* remove timeout if there are no transfers left */
750 if (!curl
->unfinished
&& event_initialized(curl
->timeout
) && event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
751 event_del(curl
->timeout
);
756 static int php_http_curlm_socket_callback(CURL
*easy
, curl_socket_t sock
, int action
, void *socket_data
, void *assign_data
)
758 php_http_client_t
*context
= socket_data
;
759 php_http_client_curl_t
*curl
= context
->ctx
;
762 fprintf(stderr
, "S");
764 if (curl
->useevents
) {
765 int events
= EV_PERSIST
;
766 php_http_curlm_event_t
*ev
= assign_data
;
769 ev
= ecalloc(1, sizeof(php_http_curlm_event_t
));
770 ev
->context
= context
;
771 curl_multi_assign(curl
->handle
, sock
, ev
);
773 event_del(&ev
->evnt
);
783 case CURL_POLL_INOUT
:
784 events
|= EV_READ
|EV_WRITE
;
787 case CURL_POLL_REMOVE
:
794 php_error_docref(NULL
, E_WARNING
, "Unknown socket action %d", action
);
798 event_assign(&ev
->evnt
, curl
->evbase
, sock
, events
, php_http_curlm_event_callback
, context
);
799 event_add(&ev
->evnt
, NULL
);
805 static void php_http_curlm_timer_callback(CURLM
*multi
, long timeout_ms
, void *timer_data
)
807 php_http_client_t
*context
= timer_data
;
808 php_http_client_curl_t
*curl
= context
->ctx
;
811 fprintf(stderr
, "\ntimer <- timeout_ms: %ld\n", timeout_ms
);
813 if (curl
->useevents
) {
815 if (timeout_ms
< 0) {
816 php_http_curlm_timeout_callback(CURL_SOCKET_TIMEOUT
, /*EV_READ|EV_WRITE*/0, context
);
817 } else if (timeout_ms
> 0 || !event_initialized(curl
->timeout
) || !event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
818 struct timeval timeout
;
820 if (!event_initialized(curl
->timeout
)) {
821 event_assign(curl
->timeout
, curl
->evbase
, CURL_SOCKET_TIMEOUT
, 0, php_http_curlm_timeout_callback
, context
);
824 timeout
.tv_sec
= timeout_ms
/ 1000;
825 timeout
.tv_usec
= (timeout_ms
% 1000) * 1000;
827 event_add(curl
->timeout
, &timeout
);
832 #endif /* HAVE_EVENT */
836 static php_http_options_t php_http_curle_options
, php_http_curlm_options
;
838 #define PHP_HTTP_CURLE_OPTION_CHECK_STRLEN 0x0001
839 #define PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR 0x0002
840 #define PHP_HTTP_CURLE_OPTION_TRANSFORM_MS 0x0004
842 static ZEND_RESULT_CODE
php_http_curle_option_set_ssl_verifyhost(php_http_option_t
*opt
, zval
*val
, void *userdata
)
844 php_http_client_curl_handler_t
*curl
= userdata
;
845 CURL
*ch
= curl
->handle
;
847 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_SSL_VERIFYHOST
, Z_TYPE_P(val
) == IS_TRUE
? 2 : 0)) {
853 static ZEND_RESULT_CODE
php_http_curle_option_set_cookiestore(php_http_option_t
*opt
, zval
*val
, void *userdata
)
855 php_http_client_curl_handler_t
*curl
= userdata
;
856 CURL
*ch
= curl
->handle
;
857 php_http_curle_storage_t
*storage
= php_http_curle_get_storage(curl
->handle
);
859 if (storage
->cookiestore
) {
860 pefree(storage
->cookiestore
, 1);
862 if (val
&& Z_TYPE_P(val
) == IS_STRING
&& Z_STRLEN_P(val
)) {
863 storage
->cookiestore
= pestrndup(Z_STRVAL_P(val
), Z_STRLEN_P(val
), 1);
865 storage
->cookiestore
= NULL
;
867 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, storage
->cookiestore
)
868 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, storage
->cookiestore
)
875 static ZEND_RESULT_CODE
php_http_curle_option_set_cookies(php_http_option_t
*opt
, zval
*val
, void *userdata
)
877 php_http_client_curl_handler_t
*curl
= userdata
;
878 CURL
*ch
= curl
->handle
;
880 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
881 HashTable
*ht
= HASH_OF(val
);
883 if (curl
->options
.encode_cookies
) {
884 if (SUCCESS
== php_http_url_encode_hash_ex(ht
, &curl
->options
.cookies
, ZEND_STRL(";"), ZEND_STRL("="), NULL
, 0)) {
885 php_http_buffer_fix(&curl
->options
.cookies
);
886 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIE
, curl
->options
.cookies
.data
)) {
893 php_http_arrkey_t cookie_key
;
896 ZEND_HASH_FOREACH_KEY_VAL(ht
, cookie_key
.h
, cookie_key
.key
, cookie_val
)
898 zend_string
*zs
= zval_get_string(cookie_val
);
900 php_http_arrkey_stringify(&cookie_key
, NULL
);
901 php_http_buffer_appendf(&curl
->options
.cookies
, "%s=%s; ", cookie_key
.key
->val
, zs
->val
);
902 php_http_arrkey_dtor(&cookie_key
);
904 zend_string_release(zs
);
906 ZEND_HASH_FOREACH_END();
908 php_http_buffer_fix(&curl
->options
.cookies
);
909 if (curl
->options
.cookies
.used
) {
910 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIE
, curl
->options
.cookies
.data
)) {
916 php_http_buffer_reset(&curl
->options
.cookies
);
917 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIE
, NULL
)) {
924 static ZEND_RESULT_CODE
php_http_curle_option_set_encodecookies(php_http_option_t
*opt
, zval
*val
, void *userdata
)
926 php_http_client_curl_handler_t
*curl
= userdata
;
928 curl
->options
.encode_cookies
= Z_TYPE_P(val
) == IS_TRUE
;
932 static ZEND_RESULT_CODE
php_http_curle_option_set_lastmodified(php_http_option_t
*opt
, zval
*val
, void *userdata
)
934 php_http_client_curl_handler_t
*curl
= userdata
;
935 CURL
*ch
= curl
->handle
;
938 if (Z_LVAL_P(val
) > 0) {
939 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, Z_LVAL_P(val
))) {
943 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, (long) sapi_get_request_time() + Z_LVAL_P(val
))) {
947 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, (long) (curl
->options
.range_request
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
))) {
951 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, 0)
952 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, 0)
960 static ZEND_RESULT_CODE
php_http_curle_option_set_compress(php_http_option_t
*opt
, zval
*val
, void *userdata
)
962 php_http_client_curl_handler_t
*curl
= userdata
;
963 CURL
*ch
= curl
->handle
;
965 #if !PHP_HTTP_CURL_VERSION(7,21,6)
966 # define CURLOPT_ACCEPT_ENCODING CURLOPT_ENCODING
968 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_ACCEPT_ENCODING
, Z_TYPE_P(val
) == IS_TRUE
? "" : NULL
)) {
974 static ZEND_RESULT_CODE
php_http_curle_option_set_etag(php_http_option_t
*opt
, zval
*val
, void *userdata
)
976 php_http_client_curl_handler_t
*curl
= userdata
;
977 php_http_buffer_t header
;
979 if (val
&& Z_TYPE_P(val
) == IS_STRING
&& Z_STRLEN_P(val
)) {
980 zend_bool is_quoted
= !((Z_STRVAL_P(val
)[0] != '"') || (Z_STRVAL_P(val
)[Z_STRLEN_P(val
)-1] != '"'));
981 php_http_buffer_init(&header
);
982 php_http_buffer_appendf(&header
, is_quoted
?"%s: %s":"%s: \"%s\"", curl
->options
.range_request
?"If-Match":"If-None-Match", Z_STRVAL_P(val
));
983 php_http_buffer_fix(&header
);
984 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, header
.data
);
985 php_http_buffer_dtor(&header
);
990 static ZEND_RESULT_CODE
php_http_curle_option_set_range(php_http_option_t
*opt
, zval
*val
, void *userdata
)
992 php_http_client_curl_handler_t
*curl
= userdata
;
993 CURL
*ch
= curl
->handle
;
995 php_http_buffer_reset(&curl
->options
.ranges
);
997 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
1000 HashTable
*ht
= HASH_OF(val
);
1002 ZEND_HASH_FOREACH_VAL(ht
, rr
)
1004 if (Z_TYPE_P(rr
) == IS_ARRAY
) {
1005 if (2 == php_http_array_list(Z_ARRVAL_P(rr
), 2, &rb
, &re
)) {
1006 if ( ((Z_TYPE_P(rb
) == IS_LONG
) || ((Z_TYPE_P(rb
) == IS_STRING
) && is_numeric_string(Z_STRVAL_P(rb
), Z_STRLEN_P(rb
), &rbl
, NULL
, 1))) &&
1007 ((Z_TYPE_P(re
) == IS_LONG
) || ((Z_TYPE_P(re
) == IS_STRING
) && is_numeric_string(Z_STRVAL_P(re
), Z_STRLEN_P(re
), &rel
, NULL
, 1)))) {
1008 if ((rbl
>= 0) && (rel
>= 0)) {
1009 php_http_buffer_appendf(&curl
->options
.ranges
, "%ld-%ld,", rbl
, rel
);
1016 ZEND_HASH_FOREACH_END();
1018 if (curl
->options
.ranges
.used
) {
1019 curl
->options
.range_request
= 1;
1020 /* ditch last comma */
1021 curl
->options
.ranges
.data
[curl
->options
.ranges
.used
- 1] = '\0';
1025 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RANGE
, curl
->options
.ranges
.data
)) {
1031 static ZEND_RESULT_CODE
php_http_curle_option_set_resume(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1033 php_http_client_curl_handler_t
*curl
= userdata
;
1034 CURL
*ch
= curl
->handle
;
1036 if (Z_LVAL_P(val
) > 0) {
1037 curl
->options
.range_request
= 1;
1039 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RESUME_FROM
, Z_LVAL_P(val
))) {
1045 static ZEND_RESULT_CODE
php_http_curle_option_set_retrydelay(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1047 php_http_client_curl_handler_t
*curl
= userdata
;
1049 curl
->options
.retry
.delay
= Z_DVAL_P(val
);
1053 static ZEND_RESULT_CODE
php_http_curle_option_set_retrycount(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1055 php_http_client_curl_handler_t
*curl
= userdata
;
1057 curl
->options
.retry
.count
= Z_LVAL_P(val
);
1061 static ZEND_RESULT_CODE
php_http_curle_option_set_redirect(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1063 php_http_client_curl_handler_t
*curl
= userdata
;
1064 CURL
*ch
= curl
->handle
;
1066 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(val
) ? 1L : 0L)
1067 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, curl
->options
.redirects
= Z_LVAL_P(val
))
1074 static ZEND_RESULT_CODE
php_http_curle_option_set_portrange(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1076 php_http_client_curl_handler_t
*curl
= userdata
;
1077 CURL
*ch
= curl
->handle
;
1078 long localport
= 0, localportrange
= 0;
1080 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
1083 switch (php_http_array_list(Z_ARRVAL_P(val
), 2, &zps
, &zpe
)) {
1085 localportrange
= labs(zval_get_long(zps
)-zval_get_long(zpe
))+1L;
1088 localport
= (zval_get_long(zpe
) > 0) ? MIN(zval_get_long(zps
), zval_get_long(zpe
)) : zval_get_long(zps
);
1094 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_LOCALPORT
, localport
)
1095 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_LOCALPORTRANGE
, localportrange
)
1102 #if PHP_HTTP_CURL_VERSION(7,37,0)
1103 static ZEND_RESULT_CODE
php_http_curle_option_set_proxyheader(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1105 php_http_client_curl_handler_t
*curl
= userdata
;
1107 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
1108 php_http_arrkey_t header_key
;
1110 php_http_buffer_t header
;
1112 php_http_buffer_init(&header
);
1113 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(val
), header_key
.h
, header_key
.key
, header_val
)
1115 if (header_key
.key
) {
1116 zend_string
*zs
= zval_get_string(header_val
);
1118 php_http_buffer_appendf(&header
, "%s: %s", header_key
.key
->val
, zs
->val
);
1119 zend_string_release(zs
);
1121 php_http_buffer_fix(&header
);
1122 curl
->options
.proxyheaders
= curl_slist_append(curl
->options
.proxyheaders
, header
.data
);
1123 php_http_buffer_reset(&header
);
1127 ZEND_HASH_FOREACH_END();
1128 php_http_buffer_dtor(&header
);
1130 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_PROXYHEADER
, curl
->options
.proxyheaders
)) {
1133 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_HEADEROPT
, CURLHEADER_SEPARATE
)) {
1134 curl_easy_setopt(curl
->handle
, CURLOPT_PROXYHEADER
, NULL
);
1141 #if PHP_HTTP_CURL_VERSION(7,21,3)
1142 static ZEND_RESULT_CODE
php_http_curle_option_set_resolve(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1144 php_http_client_curl_handler_t
*curl
= userdata
;
1145 CURL
*ch
= curl
->handle
;
1147 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
1148 HashTable
*ht
= HASH_OF(val
);
1151 ZEND_HASH_FOREACH_VAL(ht
, data
)
1153 zend_string
*zs
= zval_get_string(data
);
1154 curl
->options
.resolve
= curl_slist_append(curl
->options
.resolve
, zs
->val
);
1155 zend_string_release(zs
);
1157 ZEND_HASH_FOREACH_END();
1159 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RESOLVE
, curl
->options
.resolve
)) {
1163 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RESOLVE
, NULL
)) {
1171 #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
1172 static ZEND_RESULT_CODE
php_http_curle_option_set_ssl_tlsauthtype(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1174 php_http_client_curl_handler_t
*curl
= userdata
;
1175 CURL
*ch
= curl
->handle
;
1177 if (val
&& Z_LVAL_P(val
)) {
1178 switch (Z_LVAL_P(val
)) {
1179 case CURL_TLSAUTH_SRP
:
1180 if (CURLE_OK
== curl_easy_setopt(ch
, CURLOPT_TLSAUTH_TYPE
, PHP_HTTP_CURL_TLSAUTH_SRP
)) {
1188 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TLSAUTH_TYPE
, PHP_HTTP_CURL_TLSAUTH_DEF
)) {
1195 static void php_http_curle_options_init(php_http_options_t
*registry
)
1197 php_http_option_t
*opt
;
1200 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyhost"), CURLOPT_PROXY
, IS_STRING
))) {
1201 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1203 php_http_option_register(registry
, ZEND_STRL("proxytype"), CURLOPT_PROXYTYPE
, IS_LONG
);
1204 php_http_option_register(registry
, ZEND_STRL("proxyport"), CURLOPT_PROXYPORT
, IS_LONG
);
1205 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyauth"), CURLOPT_PROXYUSERPWD
, IS_STRING
))) {
1206 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1208 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyauthtype"), CURLOPT_PROXYAUTH
, IS_LONG
))) {
1209 Z_LVAL(opt
->defval
) = CURLAUTH_ANYSAFE
;
1211 php_http_option_register(registry
, ZEND_STRL("proxytunnel"), CURLOPT_HTTPPROXYTUNNEL
, _IS_BOOL
);
1212 #if PHP_HTTP_CURL_VERSION(7,19,4)
1213 php_http_option_register(registry
, ZEND_STRL("noproxy"), CURLOPT_NOPROXY
, IS_STRING
);
1216 #if PHP_HTTP_CURL_VERSION(7,37,0)
1217 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyheader"), CURLOPT_PROXYHEADER
, IS_ARRAY
))) {
1218 opt
->setter
= php_http_curle_option_set_proxyheader
;
1222 #if PHP_HTTP_CURL_VERSION(7,40,0)
1223 if ((opt
= php_http_option_register(registry
, ZEND_STRL("unix_socket_path"), CURLOPT_UNIX_SOCKET_PATH
, IS_STRING
))) {
1224 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1225 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1230 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_cache_timeout"), CURLOPT_DNS_CACHE_TIMEOUT
, IS_LONG
))) {
1231 Z_LVAL(opt
->defval
) = 60;
1233 php_http_option_register(registry
, ZEND_STRL("ipresolve"), CURLOPT_IPRESOLVE
, IS_LONG
);
1234 #if PHP_HTTP_CURL_VERSION(7,21,3)
1235 if ((opt
= php_http_option_register(registry
, ZEND_STRL("resolve"), CURLOPT_RESOLVE
, IS_ARRAY
))) {
1236 opt
->setter
= php_http_curle_option_set_resolve
;
1239 #if PHP_HTTP_HAVE_ARES
1240 # if PHP_HTTP_CURL_VERSION(7,24,0)
1241 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_servers"), CURLOPT_DNS_SERVERS
, IS_STRING
))) {
1242 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1245 # if PHP_HTTP_CURL_VERSION(7,33,0)
1246 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_interface"), CURLOPT_DNS_INTERFACE
, IS_STRING
))) {
1247 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1249 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_local_ip4"), CURLOPT_DNS_LOCAL_IP4
, IS_STRING
))) {
1250 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1252 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_local_ip6"), CURLOPT_DNS_LOCAL_IP6
, IS_STRING
))) {
1253 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1259 php_http_option_register(registry
, ZEND_STRL("low_speed_limit"), CURLOPT_LOW_SPEED_LIMIT
, IS_LONG
);
1260 php_http_option_register(registry
, ZEND_STRL("low_speed_time"), CURLOPT_LOW_SPEED_TIME
, IS_LONG
);
1263 php_http_option_register(registry, ZEND_STRL("max_send_speed"), CURLOPT_MAX_SEND_SPEED_LARGE, IS_LONG);
1264 php_http_option_register(registry, ZEND_STRL("max_recv_speed"), CURLOPT_MAX_RECV_SPEED_LARGE, IS_LONG);
1267 /* connection handling */
1269 if ((opt = php_http_option_register(registry, ZEND_STRL("maxconnects"), CURLOPT_MAXCONNECTS, IS_LONG))) {
1270 Z_LVAL(opt->defval) = 5;
1273 php_http_option_register(registry
, ZEND_STRL("fresh_connect"), CURLOPT_FRESH_CONNECT
, _IS_BOOL
);
1274 php_http_option_register(registry
, ZEND_STRL("forbid_reuse"), CURLOPT_FORBID_REUSE
, _IS_BOOL
);
1276 /* outgoing interface */
1277 php_http_option_register(registry
, ZEND_STRL("interface"), CURLOPT_INTERFACE
, IS_STRING
);
1278 if ((opt
= php_http_option_register(registry
, ZEND_STRL("portrange"), CURLOPT_LOCALPORT
, IS_ARRAY
))) {
1279 opt
->setter
= php_http_curle_option_set_portrange
;
1282 /* another endpoint port */
1283 php_http_option_register(registry
, ZEND_STRL("port"), CURLOPT_PORT
, IS_LONG
);
1285 /* RFC4007 zone_id */
1286 #if PHP_HTTP_CURL_VERSION(7,19,0)
1287 php_http_option_register(registry
, ZEND_STRL("address_scope"), CURLOPT_ADDRESS_SCOPE
, IS_LONG
);
1291 if ((opt
= php_http_option_register(registry
, ZEND_STRL("httpauth"), CURLOPT_USERPWD
, IS_STRING
))) {
1292 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1294 if ((opt
= php_http_option_register(registry
, ZEND_STRL("httpauthtype"), CURLOPT_HTTPAUTH
, IS_LONG
))) {
1295 Z_LVAL(opt
->defval
) = CURLAUTH_ANYSAFE
;
1299 if ((opt
= php_http_option_register(registry
, ZEND_STRL("redirect"), CURLOPT_FOLLOWLOCATION
, IS_LONG
))) {
1300 opt
->setter
= php_http_curle_option_set_redirect
;
1302 php_http_option_register(registry
, ZEND_STRL("unrestricted_auth"), CURLOPT_UNRESTRICTED_AUTH
, _IS_BOOL
);
1303 #if PHP_HTTP_CURL_VERSION(7,19,1)
1304 php_http_option_register(registry
, ZEND_STRL("postredir"), CURLOPT_POSTREDIR
, IS_LONG
);
1308 if ((opt
= php_http_option_register(registry
, ZEND_STRL("retrycount"), 0, IS_LONG
))) {
1309 opt
->setter
= php_http_curle_option_set_retrycount
;
1311 if ((opt
= php_http_option_register(registry
, ZEND_STRL("retrydelay"), 0, IS_DOUBLE
))) {
1312 opt
->setter
= php_http_curle_option_set_retrydelay
;
1316 if ((opt
= php_http_option_register(registry
, ZEND_STRL("referer"), CURLOPT_REFERER
, IS_STRING
))) {
1317 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1319 if ((opt
= php_http_option_register(registry
, ZEND_STRL("autoreferer"), CURLOPT_AUTOREFERER
, _IS_BOOL
))) {
1320 ZVAL_BOOL(&opt
->defval
, 1);
1324 if ((opt
= php_http_option_register(registry
, ZEND_STRL("useragent"), CURLOPT_USERAGENT
, IS_STRING
))) {
1325 /* don't check strlen, to allow sending no useragent at all */
1326 ZVAL_PSTRING(&opt
->defval
,
1327 "PECL_HTTP/" PHP_PECL_HTTP_VERSION
" "
1328 "PHP/" PHP_VERSION
" "
1329 "libcurl/" LIBCURL_VERSION
);
1333 if ((opt
= php_http_option_register(registry
, ZEND_STRL("resume"), CURLOPT_RESUME_FROM
, IS_LONG
))) {
1334 opt
->setter
= php_http_curle_option_set_resume
;
1337 if ((opt
= php_http_option_register(registry
, ZEND_STRL("range"), CURLOPT_RANGE
, IS_ARRAY
))) {
1338 opt
->setter
= php_http_curle_option_set_range
;
1342 if ((opt
= php_http_option_register(registry
, ZEND_STRL("etag"), 0, IS_STRING
))) {
1343 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1344 opt
->setter
= php_http_curle_option_set_etag
;
1348 if ((opt
= php_http_option_register(registry
, ZEND_STRL("compress"), 0, _IS_BOOL
))) {
1349 opt
->setter
= php_http_curle_option_set_compress
;
1353 if ((opt
= php_http_option_register(registry
, ZEND_STRL("lastmodified"), 0, IS_LONG
))) {
1354 opt
->setter
= php_http_curle_option_set_lastmodified
;
1358 if ((opt
= php_http_option_register(registry
, ZEND_STRL("encodecookies"), 0, _IS_BOOL
))) {
1359 opt
->setter
= php_http_curle_option_set_encodecookies
;
1360 ZVAL_BOOL(&opt
->defval
, 1);
1362 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cookies"), 0, IS_ARRAY
))) {
1363 opt
->setter
= php_http_curle_option_set_cookies
;
1366 /* cookiesession, don't load session cookies from cookiestore */
1367 php_http_option_register(registry
, ZEND_STRL("cookiesession"), CURLOPT_COOKIESESSION
, _IS_BOOL
);
1368 /* cookiestore, read initial cookies from that file and store cookies back into that file */
1369 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cookiestore"), 0, IS_STRING
))) {
1370 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1371 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1372 opt
->setter
= php_http_curle_option_set_cookiestore
;
1376 php_http_option_register(registry
, ZEND_STRL("maxfilesize"), CURLOPT_MAXFILESIZE
, IS_LONG
);
1378 /* http protocol version */
1379 php_http_option_register(registry
, ZEND_STRL("protocol"), CURLOPT_HTTP_VERSION
, IS_LONG
);
1382 if ((opt
= php_http_option_register(registry
, ZEND_STRL("timeout"), CURLOPT_TIMEOUT_MS
, IS_DOUBLE
))) {
1383 opt
->flags
|= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS
;
1385 if ((opt
= php_http_option_register(registry
, ZEND_STRL("connecttimeout"), CURLOPT_CONNECTTIMEOUT_MS
, IS_DOUBLE
))) {
1386 opt
->flags
|= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS
;
1387 Z_DVAL(opt
->defval
) = 3;
1389 #if PHP_HTTP_CURL_VERSION(7,36,0)
1390 if ((opt
= php_http_option_register(registry
, ZEND_STRL("expect_100_timeout"), CURLOPT_EXPECT_100_TIMEOUT_MS
, IS_DOUBLE
))) {
1391 opt
->flags
|= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS
;
1392 Z_DVAL(opt
->defval
) = 1;
1397 php_http_option_register(registry
, ZEND_STRL("tcp_nodelay"), CURLOPT_TCP_NODELAY
, _IS_BOOL
);
1398 #if PHP_HTTP_CURL_VERSION(7,25,0)
1399 php_http_option_register(registry
, ZEND_STRL("tcp_keepalive"), CURLOPT_TCP_KEEPALIVE
, _IS_BOOL
);
1400 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tcp_keepidle"), CURLOPT_TCP_KEEPIDLE
, IS_LONG
))) {
1401 Z_LVAL(opt
->defval
) = 60;
1403 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tcp_keepintvl"), CURLOPT_TCP_KEEPINTVL
, IS_LONG
))) {
1404 Z_LVAL(opt
->defval
) = 60;
1409 if ((opt
= php_http_option_register(registry
, ZEND_STRL("ssl"), 0, IS_ARRAY
))) {
1410 registry
= &opt
->suboptions
;
1412 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cert"), CURLOPT_SSLCERT
, IS_STRING
))) {
1413 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1414 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1416 if ((opt
= php_http_option_register(registry
, ZEND_STRL("certtype"), CURLOPT_SSLCERTTYPE
, IS_STRING
))) {
1417 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1418 ZVAL_PSTRING(&opt
->defval
, "PEM");
1420 if ((opt
= php_http_option_register(registry
, ZEND_STRL("key"), CURLOPT_SSLKEY
, IS_STRING
))) {
1421 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1422 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1424 if ((opt
= php_http_option_register(registry
, ZEND_STRL("keytype"), CURLOPT_SSLKEYTYPE
, IS_STRING
))) {
1425 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1426 ZVAL_PSTRING(&opt
->defval
, "PEM");
1428 if ((opt
= php_http_option_register(registry
, ZEND_STRL("keypasswd"), CURLOPT_SSLKEYPASSWD
, IS_STRING
))) {
1429 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1431 php_http_option_register(registry
, ZEND_STRL("engine"), CURLOPT_SSLENGINE
, IS_STRING
);
1432 php_http_option_register(registry
, ZEND_STRL("version"), CURLOPT_SSLVERSION
, IS_LONG
);
1433 if ((opt
= php_http_option_register(registry
, ZEND_STRL("verifypeer"), CURLOPT_SSL_VERIFYPEER
, _IS_BOOL
))) {
1434 ZVAL_BOOL(&opt
->defval
, 1);
1436 if ((opt
= php_http_option_register(registry
, ZEND_STRL("verifyhost"), CURLOPT_SSL_VERIFYHOST
, _IS_BOOL
))) {
1437 ZVAL_BOOL(&opt
->defval
, 1);
1438 opt
->setter
= php_http_curle_option_set_ssl_verifyhost
;
1440 #if PHP_HTTP_CURL_VERSION(7,41,0)
1441 php_http_option_register(registry
, ZEND_STRL("verifystatus"), CURLOPT_SSL_VERIFYSTATUS
, IS_BOOL
);
1443 php_http_option_register(registry
, ZEND_STRL("cipher_list"), CURLOPT_SSL_CIPHER_LIST
, IS_STRING
);
1444 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cainfo"), CURLOPT_CAINFO
, IS_STRING
))) {
1445 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1446 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1447 #ifdef PHP_HTTP_CURL_CAINFO
1448 ZVAL_PSTRING(&opt
->defval
, PHP_HTTP_CURL_CAINFO
);
1451 if ((opt
= php_http_option_register(registry
, ZEND_STRL("capath"), CURLOPT_CAPATH
, IS_STRING
))) {
1452 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1453 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1455 if ((opt
= php_http_option_register(registry
, ZEND_STRL("random_file"), CURLOPT_RANDOM_FILE
, IS_STRING
))) {
1456 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1457 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1459 if ((opt
= php_http_option_register(registry
, ZEND_STRL("egdsocket"), CURLOPT_EGDSOCKET
, IS_STRING
))) {
1460 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1461 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1463 #if PHP_HTTP_CURL_VERSION(7,19,0)
1464 if ((opt
= php_http_option_register(registry
, ZEND_STRL("issuercert"), CURLOPT_ISSUERCERT
, IS_STRING
))) {
1465 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1466 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1468 # ifdef PHP_HTTP_HAVE_OPENSSL
1469 if ((opt
= php_http_option_register(registry
, ZEND_STRL("crlfile"), CURLOPT_CRLFILE
, IS_STRING
))) {
1470 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1471 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1475 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
1476 php_http_option_register(registry
, ZEND_STRL("certinfo"), CURLOPT_CERTINFO
, _IS_BOOL
);
1478 #if PHP_HTTP_CURL_VERSION(7,36,0)
1479 if ((opt
= php_http_option_register(registry
, ZEND_STRL("enable_npn"), CURLOPT_SSL_ENABLE_NPN
, _IS_BOOL
))) {
1480 ZVAL_BOOL(&opt
->defval
, 1);
1482 if ((opt
= php_http_option_register(registry
, ZEND_STRL("enable_alpn"), CURLOPT_SSL_ENABLE_ALPN
, _IS_BOOL
))) {
1483 ZVAL_BOOL(&opt
->defval
, 1);
1486 #if PHP_HTTP_CURL_VERSION(7,39,0)
1487 if ((opt
= php_http_option_register(registry
, ZEND_STRL("pinned_publickey"), CURLOPT_PINNEDPUBLICKEY
, IS_STRING
))) {
1488 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1489 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1492 #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
1493 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tlsauthtype"), CURLOPT_TLSAUTH_TYPE
, IS_LONG
))) {
1494 opt
->setter
= php_http_curle_option_set_ssl_tlsauthtype
;
1496 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tlsauthuser"), CURLOPT_TLSAUTH_USERNAME
, IS_STRING
))) {
1497 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1499 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tlsauthpass"), CURLOPT_TLSAUTH_PASSWORD
, IS_STRING
))) {
1500 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1506 static zval
*php_http_curle_get_option(php_http_option_t
*opt
, HashTable
*options
, void *userdata
)
1508 php_http_client_curl_handler_t
*curl
= userdata
;
1511 if ((option
= php_http_option_get(opt
, options
, NULL
))) {
1514 ZVAL_DUP(&zopt
, option
);
1515 convert_to_explicit_type(&zopt
, opt
->type
);
1516 zend_hash_update(&curl
->options
.cache
, opt
->name
, &zopt
);
1517 return zend_hash_find(&curl
->options
.cache
, opt
->name
);
1522 static ZEND_RESULT_CODE
php_http_curle_set_option(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1524 php_http_client_curl_handler_t
*curl
= userdata
;
1525 CURL
*ch
= curl
->handle
;
1527 CURLcode rc
= CURLE_OK
;
1528 ZEND_RESULT_CODE rv
= SUCCESS
;
1534 switch (opt
->type
) {
1537 rv
= opt
->setter(opt
, val
, curl
);
1538 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, (long) (Z_TYPE_P(val
) == IS_TRUE
))) {
1545 rv
= opt
->setter(opt
, val
, curl
);
1546 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, Z_LVAL_P(val
))) {
1553 rv
= opt
->setter(opt
, val
, curl
);
1554 } else if (!val
|| Z_TYPE_P(val
) == IS_NULL
) {
1555 if (CURLE_OK
!= (rc
= curl_easy_setopt(ch
, opt
->option
, NULL
))) {
1558 } else if ((opt
->flags
& PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
) && !Z_STRLEN_P(val
)) {
1559 if (CURLE_OK
!= (rc
= curl_easy_setopt(ch
, opt
->option
, NULL
))) {
1562 } else if ((opt
->flags
& PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
) && Z_STRVAL_P(val
) && SUCCESS
!= php_check_open_basedir(Z_STRVAL_P(val
))) {
1563 if (CURLE_OK
!= (rc
= curl_easy_setopt(ch
, opt
->option
, NULL
))) {
1566 } else if (CURLE_OK
!= (rc
= curl_easy_setopt(ch
, opt
->option
, Z_STRVAL_P(val
)))) {
1572 if (opt
->flags
& PHP_HTTP_CURLE_OPTION_TRANSFORM_MS
) {
1574 Z_DVAL(tmp
) *= 1000;
1578 rv
= opt
->setter(opt
, val
, curl
);
1579 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, (long) Z_DVAL_P(val
))) {
1586 rv
= opt
->setter(opt
, val
, curl
);
1587 } else if (Z_TYPE_P(val
) != IS_NULL
) {
1588 rv
= php_http_options_apply(&opt
->suboptions
, Z_ARRVAL_P(val
), curl
);
1594 rv
= opt
->setter(opt
, val
, curl
);
1600 if (rv
!= SUCCESS
) {
1601 php_error_docref(NULL
, E_NOTICE
, "Could not set option %s (%s)", opt
->name
->val
, curl_easy_strerror(rc
));
1606 #if PHP_HTTP_CURL_VERSION(7,30,0)
1607 static ZEND_RESULT_CODE
php_http_curlm_option_set_pipelining_bl(php_http_option_t
*opt
, zval
*value
, void *userdata
)
1609 php_http_client_t
*client
= userdata
;
1610 php_http_client_curl_t
*curl
= client
->ctx
;
1611 CURLM
*ch
= curl
->handle
;
1614 TSRMLS_FETCH_FROM_CTX(client
->ts
);
1616 /* array of char *, ending with a NULL */
1617 if (value
&& Z_TYPE_P(value
) != IS_NULL
) {
1619 HashTable
*ht
= HASH_OF(value
);
1620 int c
= zend_hash_num_elements(ht
);
1621 char **ptr
= ecalloc(c
+ 1, sizeof(char *));
1625 zend_hash_init(&tmp_ht
, c
, NULL
, ZVAL_PTR_DTOR
, 0);
1626 array_join(ht
, &tmp_ht
, 0, ARRAY_JOIN_STRINGIFY
);
1628 ZEND_HASH_FOREACH_VAL(&tmp_ht
, entry
)
1630 *ptr
++ = Z_STRVAL_P(entry
);
1632 ZEND_HASH_FOREACH_END();
1635 if (CURLM_OK
!= curl_multi_setopt(ch
, opt
->option
, bl
)) {
1638 zend_hash_destroy(&tmp_ht
);
1645 zend_hash_destroy(&tmp_ht
);
1651 #if PHP_HTTP_HAVE_EVENT
1652 static inline ZEND_RESULT_CODE
php_http_curlm_use_eventloop(php_http_client_t
*h
, zend_bool enable
)
1654 php_http_client_curl_t
*curl
= h
->ctx
;
1656 if ((curl
->useevents
= enable
)) {
1657 if (!curl
->evbase
) {
1658 curl
->evbase
= event_base_new();
1660 if (!curl
->timeout
) {
1661 curl
->timeout
= ecalloc(1, sizeof(struct event
));
1663 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETDATA
, h
);
1664 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETFUNCTION
, php_http_curlm_socket_callback
);
1665 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERDATA
, h
);
1666 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERFUNCTION
, php_http_curlm_timer_callback
);
1668 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETDATA
, NULL
);
1669 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETFUNCTION
, NULL
);
1670 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERDATA
, NULL
);
1671 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERFUNCTION
, NULL
);
1677 static ZEND_RESULT_CODE
php_http_curlm_option_set_use_eventloop(php_http_option_t
*opt
, zval
*value
, void *userdata
)
1679 php_http_client_t
*client
= userdata
;
1681 return php_http_curlm_use_eventloop(client
, value
&& Z_TYPE_P(value
) == IS_TRUE
);
1685 static void php_http_curlm_options_init(php_http_options_t
*registry TSRMLS_DC
)
1687 php_http_option_t
*opt
;
1689 /* set size of connection cache */
1690 if ((opt
= php_http_option_register(registry
, ZEND_STRL("maxconnects"), CURLMOPT_MAXCONNECTS
, IS_LONG
))) {
1691 /* -1 == default, 0 == unlimited */
1692 ZVAL_LONG(&opt
->defval
, -1);
1694 /* set max number of connections to a single host */
1695 #if PHP_HTTP_CURL_VERSION(7,30,0)
1696 php_http_option_register(registry
, ZEND_STRL("max_host_connections"), CURLMOPT_MAX_HOST_CONNECTIONS
, IS_LONG
);
1698 /* maximum number of requests in a pipeline */
1699 #if PHP_HTTP_CURL_VERSION(7,30,0)
1700 if ((opt
= php_http_option_register(registry
, ZEND_STRL("max_pipeline_length"), CURLMOPT_MAX_PIPELINE_LENGTH
, IS_LONG
))) {
1701 ZVAL_LONG(&opt
->defval
, 5);
1704 /* max simultaneously open connections */
1705 #if PHP_HTTP_CURL_VERSION(7,30,0)
1706 php_http_option_register(registry
, ZEND_STRL("max_total_connections"), CURLMOPT_MAX_TOTAL_CONNECTIONS
, IS_LONG
);
1708 /* enable/disable HTTP pipelining */
1709 php_http_option_register(registry
, ZEND_STRL("pipelining"), CURLMOPT_PIPELINING
, _IS_BOOL
);
1710 /* chunk length threshold for pipelining */
1711 #if PHP_HTTP_CURL_VERSION(7,30,0)
1712 php_http_option_register(registry
, ZEND_STRL("chunk_length_penalty_size"), CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE
, IS_LONG
);
1714 /* size threshold for pipelining penalty */
1715 #if PHP_HTTP_CURL_VERSION(7,30,0)
1716 php_http_option_register(registry
, ZEND_STRL("content_length_penalty_size"), CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE
, IS_LONG
);
1718 /* pipelining server blacklist */
1719 #if PHP_HTTP_CURL_VERSION(7,30,0)
1720 if ((opt
= php_http_option_register(registry
, ZEND_STRL("pipelining_server_bl"), CURLMOPT_PIPELINING_SERVER_BL
, IS_ARRAY
))) {
1721 opt
->setter
= php_http_curlm_option_set_pipelining_bl
;
1724 /* pipelining host blacklist */
1725 #if PHP_HTTP_CURL_VERSION(7,30,0)
1726 if ((opt
= php_http_option_register(registry
, ZEND_STRL("pipelining_site_bl"), CURLMOPT_PIPELINING_SITE_BL
, IS_ARRAY
))) {
1727 opt
->setter
= php_http_curlm_option_set_pipelining_bl
;
1731 #if PHP_HTTP_HAVE_EVENT
1732 if ((opt
= php_http_option_register(registry
, ZEND_STRL("use_eventloop"), 0, _IS_BOOL
))) {
1733 opt
->setter
= php_http_curlm_option_set_use_eventloop
;
1738 static ZEND_RESULT_CODE
php_http_curlm_set_option(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1740 php_http_client_t
*client
= userdata
;
1741 php_http_client_curl_t
*curl
= client
->ctx
;
1742 CURLM
*ch
= curl
->handle
;
1744 CURLMcode rc
= CURLM_UNKNOWN_OPTION
;
1745 ZEND_RESULT_CODE rv
= SUCCESS
;
1749 } else if (opt
->type
&& Z_TYPE_P(val
) != opt
->type
&& !(Z_TYPE_P(val
) == IS_NULL
&& opt
->type
== IS_ARRAY
)) {
1752 ZVAL_DUP(&zopt
, val
);
1753 convert_to_explicit_type(&zopt
, opt
->type
);
1759 rv
= opt
->setter(opt
, val
, client
);
1761 switch (opt
->type
) {
1763 if (CURLM_OK
!= (rc
= curl_multi_setopt(ch
, opt
->option
, (long) zend_is_true(val
)))) {
1768 if (CURLM_OK
!= (rc
= curl_multi_setopt(ch
, opt
->option
, Z_LVAL_P(val
)))) {
1778 if (val
&& val
!= orig
&& val
!= &opt
->defval
) {
1782 if (rv
!= SUCCESS
) {
1783 php_error_docref(NULL TSRMLS_CC
, E_NOTICE
, "Could not set option %s (%s)", opt
->name
->val
, curl_easy_strerror(rc
));
1790 static ZEND_RESULT_CODE
php_http_client_curl_handler_reset(php_http_client_curl_handler_t
*curl
)
1792 CURL
*ch
= curl
->handle
;
1793 php_http_curle_storage_t
*st
;
1795 if ((st
= php_http_curle_get_storage(ch
))) {
1800 if (st
->cookiestore
) {
1801 pefree(st
->cookiestore
, 1);
1802 st
->cookiestore
= NULL
;
1804 st
->errorbuffer
[0] = '\0';
1807 curl_easy_setopt(ch
, CURLOPT_URL
, NULL
);
1808 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, NULL
);
1809 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1L);
1810 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 0L);
1811 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1812 #if PHP_HTTP_CURL_VERSION(7,19,1)
1813 curl_easy_setopt(ch
, CURLOPT_PROXYUSERNAME
, NULL
);
1814 curl_easy_setopt(ch
, CURLOPT_PROXYPASSWORD
, NULL
);
1815 curl_easy_setopt(ch
, CURLOPT_USERNAME
, NULL
);
1816 curl_easy_setopt(ch
, CURLOPT_PASSWORD
, NULL
);
1819 #if PHP_HTTP_CURL_VERSION(7,21,3)
1820 if (curl
->options
.resolve
) {
1821 curl_slist_free_all(curl
->options
.resolve
);
1822 curl
->options
.resolve
= NULL
;
1825 curl
->options
.retry
.count
= 0;
1826 curl
->options
.retry
.delay
= 0;
1827 curl
->options
.redirects
= 0;
1828 curl
->options
.encode_cookies
= 1;
1830 if (curl
->options
.headers
) {
1831 curl_slist_free_all(curl
->options
.headers
);
1832 curl
->options
.headers
= NULL
;
1834 if (curl
->options
.proxyheaders
) {
1835 curl_slist_free_all(curl
->options
.proxyheaders
);
1836 curl
->options
.proxyheaders
= NULL
;
1839 php_http_buffer_reset(&curl
->options
.cookies
);
1840 php_http_buffer_reset(&curl
->options
.ranges
);
1845 static php_http_client_curl_handler_t
*php_http_client_curl_handler_init(php_http_client_t
*h
, php_resource_factory_t
*rf
)
1848 php_http_client_curl_handler_t
*handler
;
1850 if (!(handle
= php_resource_factory_handle_ctor(rf
, NULL
))) {
1851 php_error_docref(NULL
, E_WARNING
, "Failed to initialize curl handle");
1855 handler
= ecalloc(1, sizeof(*handler
));
1857 handler
->client
= h
;
1858 handler
->handle
= handle
;
1859 handler
->response
.body
= php_http_message_body_init(NULL
, NULL
);
1860 php_http_buffer_init(&handler
->response
.headers
);
1861 php_http_buffer_init(&handler
->options
.cookies
);
1862 php_http_buffer_init(&handler
->options
.ranges
);
1863 zend_hash_init(&handler
->options
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
1866 curl_easy_setopt(handle
, CURLOPT_NOSIGNAL
, 1L);
1868 curl_easy_setopt(handle
, CURLOPT_HEADER
, 0L);
1869 curl_easy_setopt(handle
, CURLOPT_FILETIME
, 1L);
1870 curl_easy_setopt(handle
, CURLOPT_AUTOREFERER
, 1L);
1871 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
1872 curl_easy_setopt(handle
, CURLOPT_NOPROGRESS
, 0L);
1873 curl_easy_setopt(handle
, CURLOPT_HEADERFUNCTION
, php_http_curle_header_callback
);
1874 curl_easy_setopt(handle
, CURLOPT_WRITEFUNCTION
, php_http_curle_body_callback
);
1875 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, php_http_curle_raw_callback
);
1876 curl_easy_setopt(handle
, CURLOPT_READFUNCTION
, php_http_curle_read_callback
);
1877 curl_easy_setopt(handle
, CURLOPT_SEEKFUNCTION
, php_http_curle_seek_callback
);
1878 #if PHP_HTTP_CURL_VERSION(7,32,0)
1879 curl_easy_setopt(handle
, CURLOPT_XFERINFOFUNCTION
, php_http_curle_xferinfo_callback
);
1880 curl_easy_setopt(handle
, CURLOPT_XFERINFODATA
, handler
);
1882 curl_easy_setopt(handle
, CURLOPT_PROGRESSFUNCTION
, php_http_curle_progress_callback
);
1883 curl_easy_setopt(handle
, CURLOPT_PROGRESSDATA
, handler
);
1885 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, handler
);
1886 curl_easy_setopt(handle
, CURLOPT_WRITEDATA
, handler
);
1887 curl_easy_setopt(handle
, CURLOPT_HEADERDATA
, handler
);
1889 php_http_client_curl_handler_reset(handler
);
1895 static ZEND_RESULT_CODE
php_http_client_curl_handler_prepare(php_http_client_curl_handler_t
*curl
, php_http_client_enqueue_t
*enqueue
)
1898 php_http_message_t
*msg
= enqueue
->request
;
1899 php_http_curle_storage_t
*storage
= php_http_curle_get_storage(curl
->handle
);
1902 if (!PHP_HTTP_INFO(msg
).request
.url
) {
1903 php_error_docref(NULL
, E_WARNING
, "Cannot request empty URL");
1906 storage
->errorbuffer
[0] = '\0';
1908 pefree(storage
->url
, 1);
1910 php_http_url_to_string(PHP_HTTP_INFO(msg
).request
.url
, &storage
->url
, NULL
, 1);
1911 curl_easy_setopt(curl
->handle
, CURLOPT_URL
, storage
->url
);
1913 /* request method */
1914 switch (php_http_select_str(PHP_HTTP_INFO(msg
).request
.method
, 4, "GET", "HEAD", "POST", "PUT")) {
1916 curl_easy_setopt(curl
->handle
, CURLOPT_HTTPGET
, 1L);
1920 curl_easy_setopt(curl
->handle
, CURLOPT_NOBODY
, 1L);
1924 curl_easy_setopt(curl
->handle
, CURLOPT_POST
, 1L);
1928 curl_easy_setopt(curl
->handle
, CURLOPT_UPLOAD
, 1L);
1932 if (PHP_HTTP_INFO(msg
).request
.method
) {
1933 curl_easy_setopt(curl
->handle
, CURLOPT_CUSTOMREQUEST
, PHP_HTTP_INFO(msg
).request
.method
);
1935 php_error_docref(NULL
, E_WARNING
, "Cannot use empty request method");
1943 php_http_options_apply(&php_http_curle_options
, enqueue
->options
, curl
);
1945 /* request headers */
1946 php_http_message_update_headers(msg
);
1947 if (zend_hash_num_elements(&msg
->hdrs
)) {
1948 php_http_arrkey_t header_key
;
1950 zend_string
*header_str
;
1951 php_http_buffer_t header
;
1952 #if !PHP_HTTP_CURL_VERSION(7,23,0)
1953 zval
*ct
= zend_hash_str_find(&msg
->hdrs
, ZEND_STRL("Content-Length"));
1956 php_http_buffer_init(&header
);
1957 ZEND_HASH_FOREACH_KEY_VAL(&msg
->hdrs
, header_key
.h
, header_key
.key
, header_val
)
1959 if (header_key
.key
) {
1960 #if !PHP_HTTP_CURL_VERSION(7,23,0)
1961 /* avoid duplicate content-length header */
1962 if (ct
&& ct
== header_val
) {
1966 header_str
= zval_get_string(header_val
);
1967 php_http_buffer_appendf(&header
, "%s: %s", header_key
.key
->val
, header_str
->val
);
1968 php_http_buffer_fix(&header
);
1969 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, header
.data
);
1970 php_http_buffer_reset(&header
);
1971 zend_string_release(header_str
);
1974 ZEND_HASH_FOREACH_END();
1975 php_http_buffer_dtor(&header
);
1977 curl_easy_setopt(curl
->handle
, CURLOPT_HTTPHEADER
, curl
->options
.headers
);
1979 /* attach request body */
1980 if ((body_size
= php_http_message_body_size(msg
->body
))) {
1981 /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
1982 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
1983 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
1984 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
1985 * does not allow a request body.
1987 php_stream_rewind(php_http_message_body_stream(msg
->body
));
1988 curl_easy_setopt(curl
->handle
, CURLOPT_SEEKDATA
, msg
->body
);
1989 curl_easy_setopt(curl
->handle
, CURLOPT_READDATA
, msg
->body
);
1990 curl_easy_setopt(curl
->handle
, CURLOPT_INFILESIZE
, body_size
);
1991 curl_easy_setopt(curl
->handle
, CURLOPT_POSTFIELDSIZE
, body_size
);
1993 curl_easy_setopt(curl
->handle
, CURLOPT_SEEKDATA
, NULL
);
1994 curl_easy_setopt(curl
->handle
, CURLOPT_READDATA
, NULL
);
1995 curl_easy_setopt(curl
->handle
, CURLOPT_INFILESIZE
, 0L);
1996 curl_easy_setopt(curl
->handle
, CURLOPT_POSTFIELDSIZE
, 0L);
2002 static void php_http_client_curl_handler_clear(php_http_client_curl_handler_t
*handler
)
2004 curl_easy_setopt(handler
->handle
, CURLOPT_NOPROGRESS
, 1L);
2005 #if PHP_HTTP_CURL_VERSION(7,32,0)
2006 curl_easy_setopt(handler
->handle
, CURLOPT_XFERINFOFUNCTION
, NULL
);
2008 curl_easy_setopt(handler
->handle
, CURLOPT_PROGRESSFUNCTION
, NULL
);
2010 curl_easy_setopt(handler
->handle
, CURLOPT_VERBOSE
, 0L);
2011 curl_easy_setopt(handler
->handle
, CURLOPT_DEBUGFUNCTION
, NULL
);
2014 static void php_http_client_curl_handler_dtor(php_http_client_curl_handler_t
*handler
)
2016 php_http_client_curl_handler_clear(handler
);
2018 php_resource_factory_handle_dtor(handler
->rf
, handler
->handle
);
2019 php_resource_factory_free(&handler
->rf
);
2021 php_http_message_body_free(&handler
->response
.body
);
2022 php_http_buffer_dtor(&handler
->response
.headers
);
2023 php_http_buffer_dtor(&handler
->options
.ranges
);
2024 php_http_buffer_dtor(&handler
->options
.cookies
);
2025 zend_hash_destroy(&handler
->options
.cache
);
2027 #if PHP_HTTP_CURL_VERSION(7,21,3)
2028 if (handler
->options
.resolve
) {
2029 curl_slist_free_all(handler
->options
.resolve
);
2030 handler
->options
.resolve
= NULL
;
2034 if (handler
->options
.headers
) {
2035 curl_slist_free_all(handler
->options
.headers
);
2036 handler
->options
.headers
= NULL
;
2039 if (handler
->options
.proxyheaders
) {
2040 curl_slist_free_all(handler
->options
.proxyheaders
);
2041 handler
->options
.proxyheaders
= NULL
;
2047 static php_http_client_t
*php_http_client_curl_init(php_http_client_t
*h
, void *handle
)
2049 php_http_client_curl_t
*curl
;
2051 if (!handle
&& !(handle
= php_resource_factory_handle_ctor(h
->rf
, NULL
))) {
2052 php_error_docref(NULL
, E_WARNING
, "Failed to initialize curl handle");
2056 curl
= ecalloc(1, sizeof(*curl
));
2057 curl
->handle
= handle
;
2058 curl
->unfinished
= 0;
2064 static void php_http_client_curl_dtor(php_http_client_t
*h
)
2066 php_http_client_curl_t
*curl
= h
->ctx
;
2068 #if PHP_HTTP_HAVE_EVENT
2069 if (curl
->timeout
) {
2070 if (event_initialized(curl
->timeout
) && event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
2071 event_del(curl
->timeout
);
2073 efree(curl
->timeout
);
2074 curl
->timeout
= NULL
;
2077 event_base_free(curl
->evbase
);
2078 curl
->evbase
= NULL
;
2081 curl
->unfinished
= 0;
2083 php_resource_factory_handle_dtor(h
->rf
, curl
->handle
);
2089 static void queue_dtor(php_http_client_enqueue_t
*e
)
2091 php_http_client_curl_handler_t
*handler
= e
->opaque
;
2093 if (handler
->queue
.dtor
) {
2094 e
->opaque
= handler
->queue
.opaque
;
2095 handler
->queue
.dtor(e
);
2097 php_http_client_curl_handler_dtor(handler
);
2100 static php_resource_factory_t
*create_rf(php_http_client_t
*h
, php_http_client_enqueue_t
*enqueue
)
2102 php_persistent_handle_factory_t
*pf
= NULL
;
2103 php_resource_factory_t
*rf
= NULL
;
2104 php_http_url_t
*url
= enqueue
->request
->http
.info
.request
.url
;
2106 if (!url
|| (!url
->host
&& !url
->path
)) {
2107 php_error_docref(NULL
, E_WARNING
, "Cannot request empty URL");
2111 /* only if the client itself is setup for persistence */
2112 if (h
->rf
->dtor
== (void (*)(void*)) php_persistent_handle_abandon
) {
2114 char *id_str
= NULL
;
2116 int port
= url
->port
? url
->port
: 80;
2119 id_len
= spprintf(&id_str
, 0, "%s:%d", STR_PTR(url
->host
), url
->port
? url
->port
: 80);
2120 id
= php_http_cs2zs(id_str
, id_len
);
2122 if ((zport
= zend_hash_str_find(enqueue
->options
, ZEND_STRL("port")))) {
2123 zend_long lport
= zval_get_long(zport
);
2130 id_len
= spprintf(&id_str
, 0, "%s:%d", STR_PTR(url
->host
), port
);
2131 pf
= php_persistent_handle_concede(NULL
, PHP_HTTP_G
->client
.curl
.driver
.request_name
, id
, NULL
, NULL
);
2132 zend_string_release(id
);
2136 rf
= php_resource_factory_init(NULL
, php_persistent_handle_get_resource_factory_ops(), pf
, (void (*)(void*)) php_persistent_handle_abandon
);
2138 rf
= php_resource_factory_init(NULL
, &php_http_curle_resource_factory_ops
, NULL
, NULL
);
2144 static ZEND_RESULT_CODE
php_http_client_curl_enqueue(php_http_client_t
*h
, php_http_client_enqueue_t
*enqueue
)
2147 php_http_client_curl_t
*curl
= h
->ctx
;
2148 php_http_client_curl_handler_t
*handler
;
2149 php_http_client_progress_state_t
*progress
;
2150 php_resource_factory_t
*rf
;
2152 rf
= create_rf(h
, enqueue
);
2157 handler
= php_http_client_curl_handler_init(h
, rf
);
2162 if (SUCCESS
!= php_http_client_curl_handler_prepare(handler
, enqueue
)) {
2163 php_http_client_curl_handler_dtor(handler
);
2167 handler
->queue
= *enqueue
;
2168 enqueue
->opaque
= handler
;
2169 enqueue
->dtor
= queue_dtor
;
2171 if (CURLM_OK
== (rs
= curl_multi_add_handle(curl
->handle
, handler
->handle
))) {
2172 zend_llist_add_element(&h
->requests
, enqueue
);
2175 if (h
->callback
.progress
.func
&& SUCCESS
== php_http_client_getopt(h
, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
, enqueue
->request
, &progress
)) {
2176 progress
->info
= "start";
2177 h
->callback
.progress
.func(h
->callback
.progress
.arg
, h
, &handler
->queue
, progress
);
2178 progress
->started
= 1;
2183 php_error_docref(NULL
, E_WARNING
, "Could not enqueue request: %s", curl_multi_strerror(rs
));
2188 static ZEND_RESULT_CODE
php_http_client_curl_dequeue(php_http_client_t
*h
, php_http_client_enqueue_t
*enqueue
)
2191 php_http_client_curl_t
*curl
= h
->ctx
;
2192 php_http_client_curl_handler_t
*handler
= enqueue
->opaque
;
2194 php_http_client_curl_handler_clear(handler
);
2195 if (CURLM_OK
== (rs
= curl_multi_remove_handle(curl
->handle
, handler
->handle
))) {
2196 zend_llist_del_element(&h
->requests
, handler
->handle
, (int (*)(void *, void *)) compare_queue
);
2199 php_error_docref(NULL
, E_WARNING
, "Could not dequeue request: %s", curl_multi_strerror(rs
));
2205 static void php_http_client_curl_reset(php_http_client_t
*h
)
2207 zend_llist_element
*next_el
, *this_el
;
2209 for (this_el
= h
->requests
.head
; this_el
; this_el
= next_el
) {
2210 next_el
= this_el
->next
;
2211 php_http_client_curl_dequeue(h
, (void *) this_el
->data
);
2215 static inline void php_http_client_curl_get_timeout(php_http_client_curl_t
*curl
, long max_tout
, struct timeval
*timeout
)
2217 if ((CURLM_OK
== curl_multi_timeout(curl
->handle
, &max_tout
)) && (max_tout
> 0)) {
2218 timeout
->tv_sec
= max_tout
/ 1000;
2219 timeout
->tv_usec
= (max_tout
% 1000) * 1000;
2221 timeout
->tv_sec
= 0;
2222 timeout
->tv_usec
= 1000;
2227 # define SELECT_ERROR SOCKET_ERROR
2229 # define SELECT_ERROR -1
2232 static ZEND_RESULT_CODE
php_http_client_curl_wait(php_http_client_t
*h
, struct timeval
*custom_timeout
)
2236 struct timeval timeout
;
2237 php_http_client_curl_t
*curl
= h
->ctx
;
2239 #if PHP_HTTP_HAVE_EVENT
2240 if (curl
->useevents
) {
2241 if (!event_initialized(curl
->timeout
)) {
2242 event_assign(curl
->timeout
, curl
->evbase
, CURL_SOCKET_TIMEOUT
, 0, php_http_curlm_timeout_callback
, h
);
2243 } else if (custom_timeout
&& timerisset(custom_timeout
)) {
2244 event_add(curl
->timeout
, custom_timeout
);
2245 } else if (!event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
2246 php_http_client_curl_get_timeout(curl
, 1000, &timeout
);
2247 event_add(curl
->timeout
, &timeout
);
2250 event_base_loop(curl
->evbase
, EVLOOP_ONCE
);
2260 if (CURLM_OK
== curl_multi_fdset(curl
->handle
, &R
, &W
, &E
, &MAX
)) {
2261 if (custom_timeout
&& timerisset(custom_timeout
)) {
2262 timeout
= *custom_timeout
;
2264 php_http_client_curl_get_timeout(curl
, 1000, &timeout
);
2268 php_http_sleep((double) timeout
.tv_sec
+ (double) (timeout
.tv_usec
/ PHP_HTTP_MCROSEC
));
2270 } else if (SELECT_ERROR
!= select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) {
2277 static int php_http_client_curl_once(php_http_client_t
*h
)
2279 php_http_client_curl_t
*curl
= h
->ctx
;
2281 #if PHP_HTTP_HAVE_EVENT
2282 if (curl
->useevents
) {
2283 event_base_loop(curl
->evbase
, EVLOOP_NONBLOCK
);
2286 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(curl
->handle
, &curl
->unfinished
));
2288 php_http_curlm_responsehandler(h
);
2290 return curl
->unfinished
;
2294 static ZEND_RESULT_CODE
php_http_client_curl_exec(php_http_client_t
*h
)
2296 #if PHP_HTTP_HAVE_EVENT
2297 php_http_client_curl_t
*curl
= h
->ctx
;
2299 if (curl
->useevents
) {
2300 php_http_curlm_timeout_callback(CURL_SOCKET_TIMEOUT
, /*EV_READ|EV_WRITE*/0, h
);
2302 int ev_rc
= event_base_dispatch(curl
->evbase
);
2305 fprintf(stderr
, "%c", "X.0"[ev_rc
+1]);
2309 php_error_docref(NULL
, E_ERROR
, "Error in event_base_dispatch()");
2312 } while (curl
->unfinished
);
2316 while (php_http_client_curl_once(h
)) {
2317 if (SUCCESS
!= php_http_client_curl_wait(h
, NULL
)) {
2319 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
2320 php_error_docref(NULL
, E_WARNING
, "WinSock error: %d", WSAGetLastError());
2322 php_error_docref(NULL
, E_WARNING
, "%s", strerror(errno
));
2332 static ZEND_RESULT_CODE
php_http_client_curl_setopt(php_http_client_t
*h
, php_http_client_setopt_opt_t opt
, void *arg
)
2334 php_http_client_curl_t
*curl
= h
->ctx
;
2337 case PHP_HTTP_CLIENT_OPT_CONFIGURATION
:
2338 return php_http_options_apply(&php_http_curlm_options
, (HashTable
*) arg
, h
);
2341 case PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING
:
2342 if (CURLM_OK
!= curl_multi_setopt(curl
->handle
, CURLMOPT_PIPELINING
, (long) *((zend_bool
*) arg
))) {
2347 case PHP_HTTP_CLIENT_OPT_USE_EVENTS
:
2348 #if PHP_HTTP_HAVE_EVENT
2349 return php_http_curlm_use_eventloop(h
, *(zend_bool
*) arg
);
2359 static int apply_available_options(zval
*pDest
, int num_args
, va_list args
, zend_hash_key
*hash_key
)
2361 php_http_option_t
*opt
= Z_PTR_P(pDest
);
2366 ht
= va_arg(args
, HashTable
*);
2368 if ((c
= zend_hash_num_elements(&opt
->suboptions
.options
))) {
2369 array_init_size(&entry
, c
);
2370 zend_hash_apply_with_arguments(&opt
->suboptions
.options
, apply_available_options
, 1, Z_ARRVAL(entry
));
2372 /* catch deliberate NULL options */
2373 if (Z_TYPE(opt
->defval
) == IS_STRING
&& !Z_STRVAL(opt
->defval
)) {
2376 ZVAL_ZVAL(&entry
, &opt
->defval
, 1, 0);
2380 if (hash_key
->key
) {
2381 zend_hash_update(ht
, hash_key
->key
, &entry
);
2383 zend_hash_index_update(ht
, hash_key
->h
, &entry
);
2386 return ZEND_HASH_APPLY_KEEP
;
2389 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
)
2391 php_http_client_enqueue_t
*enqueue
;
2392 TSRMLS_FETCH_FROM_CTX(h
->ts
);
2395 case PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
:
2396 if ((enqueue
= php_http_client_enqueued(h
, arg
, NULL
))) {
2397 php_http_client_curl_handler_t
*handler
= enqueue
->opaque
;
2399 *((php_http_client_progress_state_t
**) res
) = &handler
->progress
;
2404 case PHP_HTTP_CLIENT_OPT_TRANSFER_INFO
:
2405 if ((enqueue
= php_http_client_enqueued(h
, arg
, NULL
))) {
2406 php_http_client_curl_handler_t
*handler
= enqueue
->opaque
;
2408 php_http_curle_get_info(handler
->handle
, *(HashTable
**) res
);
2413 case PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS
:
2414 zend_hash_apply_with_arguments(&php_http_curle_options
.options TSRMLS_CC
, apply_available_options
, 1, *(HashTable
**) res
);
2417 case PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION
:
2418 zend_hash_apply_with_arguments(&php_http_curlm_options
.options TSRMLS_CC
, apply_available_options
, 1, *(HashTable
**) res
);
2428 static php_http_client_ops_t php_http_client_curl_ops
= {
2429 &php_http_curlm_resource_factory_ops
,
2430 php_http_client_curl_init
,
2432 php_http_client_curl_dtor
,
2433 php_http_client_curl_reset
,
2434 php_http_client_curl_exec
,
2435 php_http_client_curl_wait
,
2436 php_http_client_curl_once
,
2437 php_http_client_curl_enqueue
,
2438 php_http_client_curl_dequeue
,
2439 php_http_client_curl_setopt
,
2440 php_http_client_curl_getopt
2443 php_http_client_ops_t
*php_http_client_curl_get_ops(void)
2445 return &php_http_client_curl_ops
;
2448 PHP_MINIT_FUNCTION(http_client_curl
)
2450 php_http_options_t
*options
;
2452 PHP_HTTP_G
->client
.curl
.driver
.driver_name
= zend_string_init(ZEND_STRL("curl"), 1);
2453 PHP_HTTP_G
->client
.curl
.driver
.client_name
= zend_string_init(ZEND_STRL("http\\Client\\Curl"), 1);
2454 PHP_HTTP_G
->client
.curl
.driver
.request_name
= zend_string_init(ZEND_STRL("http\\Client\\Curl\\Request"), 1);
2455 PHP_HTTP_G
->client
.curl
.driver
.client_ops
= &php_http_client_curl_ops
;
2457 if (SUCCESS
!= php_http_client_driver_add(&PHP_HTTP_G
->client
.curl
.driver
)) {
2461 if (SUCCESS
!= php_persistent_handle_provide(PHP_HTTP_G
->client
.curl
.driver
.client_name
, &php_http_curlm_resource_factory_ops
, NULL
, NULL
)) {
2464 if (SUCCESS
!= php_persistent_handle_provide(PHP_HTTP_G
->client
.curl
.driver
.request_name
, &php_http_curle_resource_factory_ops
, NULL
, NULL
)) {
2468 if ((options
= php_http_options_init(&php_http_curle_options
, 1))) {
2469 options
->getter
= php_http_curle_get_option
;
2470 options
->setter
= php_http_curle_set_option
;
2472 php_http_curle_options_init(options
);
2474 if ((options
= php_http_options_init(&php_http_curlm_options
, 1))) {
2475 options
->getter
= php_http_option_get
;
2476 options
->setter
= php_http_curlm_set_option
;
2478 php_http_curlm_options_init(options
);
2482 * HTTP Protocol Version Constants
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
);
2489 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_ANY", CURL_HTTP_VERSION_NONE
, CONST_CS
|CONST_PERSISTENT
);
2492 * SSL Version Constants
2494 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1
, CONST_CS
|CONST_PERSISTENT
);
2495 #if PHP_HTTP_CURL_VERSION(7,34,0)
2496 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_0", CURL_SSLVERSION_TLSv1_0
, CONST_CS
|CONST_PERSISTENT
);
2497 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_1", CURL_SSLVERSION_TLSv1_1
, CONST_CS
|CONST_PERSISTENT
);
2498 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_2", CURL_SSLVERSION_TLSv1_2
, CONST_CS
|CONST_PERSISTENT
);
2500 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2
, CONST_CS
|CONST_PERSISTENT
);
2501 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3
, CONST_CS
|CONST_PERSISTENT
);
2502 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT
, CONST_CS
|CONST_PERSISTENT
);
2503 #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
2504 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "TLSAUTH_SRP", CURL_TLSAUTH_SRP
, CONST_CS
|CONST_PERSISTENT
);
2508 * DNS IPvX resolving
2510 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_V4", CURL_IPRESOLVE_V4
, CONST_CS
|CONST_PERSISTENT
);
2511 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_V6", CURL_IPRESOLVE_V6
, CONST_CS
|CONST_PERSISTENT
);
2512 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER
, CONST_CS
|CONST_PERSISTENT
);
2517 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_BASIC", CURLAUTH_BASIC
, CONST_CS
|CONST_PERSISTENT
);
2518 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_DIGEST", CURLAUTH_DIGEST
, CONST_CS
|CONST_PERSISTENT
);
2519 #if PHP_HTTP_CURL_VERSION(7,19,3)
2520 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_DIGEST_IE", CURLAUTH_DIGEST_IE
, CONST_CS
|CONST_PERSISTENT
);
2522 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_NTLM", CURLAUTH_NTLM
, CONST_CS
|CONST_PERSISTENT
);
2523 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE
, CONST_CS
|CONST_PERSISTENT
);
2524 #if PHP_HTTP_CURL_VERSION(7,38,0)
2525 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_SPNEGO", CURLAUTH_NEGOTIATE
, CONST_CS
|CONST_PERSISTENT
);
2527 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_ANY", CURLAUTH_ANY
, CONST_CS
|CONST_PERSISTENT
);
2530 * Proxy Type Constants
2532 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS4", CURLPROXY_SOCKS4
, CONST_CS
|CONST_PERSISTENT
);
2533 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS4A", CURLPROXY_SOCKS5
, CONST_CS
|CONST_PERSISTENT
);
2534 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS5_HOSTNAME", CURLPROXY_SOCKS5
, CONST_CS
|CONST_PERSISTENT
);
2535 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS5", CURLPROXY_SOCKS5
, CONST_CS
|CONST_PERSISTENT
);
2536 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_HTTP", CURLPROXY_HTTP
, CONST_CS
|CONST_PERSISTENT
);
2537 #if PHP_HTTP_CURL_VERSION(7,19,4)
2538 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_HTTP_1_0", CURLPROXY_HTTP_1_0
, CONST_CS
|CONST_PERSISTENT
);
2542 * Post Redirection Constants
2544 #if PHP_HTTP_CURL_VERSION(7,19,1)
2545 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_301", CURL_REDIR_POST_301
, CONST_CS
|CONST_PERSISTENT
);
2546 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_302", CURL_REDIR_POST_302
, CONST_CS
|CONST_PERSISTENT
);
2547 #if PHP_HTTP_CURL_VERSION(7,26,0)
2548 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_303", CURL_REDIR_POST_303
, CONST_CS
|CONST_PERSISTENT
);
2550 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_ALL", CURL_REDIR_POST_ALL
, CONST_CS
|CONST_PERSISTENT
);
2556 PHP_MSHUTDOWN_FUNCTION(http_client_curl
)
2558 php_persistent_handle_cleanup(PHP_HTTP_G
->client
.curl
.driver
.client_name
, NULL
);
2559 php_persistent_handle_cleanup(PHP_HTTP_G
->client
.curl
.driver
.request_name
, NULL
);
2560 zend_string_release(PHP_HTTP_G
->client
.curl
.driver
.client_name
);
2561 zend_string_release(PHP_HTTP_G
->client
.curl
.driver
.request_name
);
2562 zend_string_release(PHP_HTTP_G
->client
.curl
.driver
.driver_name
);
2564 php_http_options_dtor(&php_http_curle_options
);
2565 php_http_options_dtor(&php_http_curlm_options
);
2570 #endif /* PHP_HTTP_HAVE_CURL */
2577 * vim600: noet sw=4 ts=4 fdm=marker
2578 * vim<600: noet sw=4 ts=4