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
;
227 if (0 == php_stream_seek(php_http_message_body_stream(body
), offset
, origin
)) {
233 static int php_http_curle_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
235 php_http_client_curl_handler_t
*h
= ctx
;
240 if (data
[0] == '-') {
241 } else if (php_memnstr(data
, ZEND_STRL("Adding handle:"), data
+ length
)) {
242 h
->progress
.info
= "setup";
243 } else if (php_memnstr(data
, ZEND_STRL("addHandle"), data
+ length
)) {
244 h
->progress
.info
= "setup";
245 } else if (php_memnstr(data
, ZEND_STRL("About to connect"), data
+ length
)) {
246 h
->progress
.info
= "resolve";
247 } else if (php_memnstr(data
, ZEND_STRL("Trying"), data
+ length
)) {
248 h
->progress
.info
= "connect";
249 } else if (php_memnstr(data
, ZEND_STRL("Found bundle for host"), data
+ length
)) {
250 h
->progress
.info
= "connect";
251 } else if (php_memnstr(data
, ZEND_STRL("Connected"), data
+ length
)) {
252 h
->progress
.info
= "connected";
253 } else if (php_memnstr(data
, ZEND_STRL("Re-using existing connection!"), data
+ length
)) {
254 h
->progress
.info
= "connected";
255 } else if (php_memnstr(data
, ZEND_STRL("blacklisted"), data
+ length
)) {
256 h
->progress
.info
= "blacklist check";
257 } else if (php_memnstr(data
, ZEND_STRL("SSL"), data
+ length
)) {
258 h
->progress
.info
= "ssl negotiation";
259 } else if (php_memnstr(data
, ZEND_STRL("upload"), data
+ length
)) {
260 h
->progress
.info
= "uploaded";
261 } else if (php_memnstr(data
, ZEND_STRL("left intact"), data
+ length
)) {
262 h
->progress
.info
= "not disconnected";
263 } else if (php_memnstr(data
, ZEND_STRL("closed"), data
+ length
)) {
264 h
->progress
.info
= "disconnected";
265 } else if (php_memnstr(data
, ZEND_STRL("Issue another request"), data
+ length
)) {
266 h
->progress
.info
= "redirect";
267 } else if (php_memnstr(data
, ZEND_STRL("Operation timed out"), data
+ length
)) {
268 h
->progress
.info
= "timeout";
271 h
->progress
.info
= data
;
272 data
[length
- 1] = '\0';
275 if (h
->client
->callback
.progress
.func
) {
276 h
->client
->callback
.progress
.func(h
->client
->callback
.progress
.arg
, h
->client
, &h
->queue
, &h
->progress
);
279 case CURLINFO_HEADER_OUT
:
280 case CURLINFO_DATA_OUT
:
281 case CURLINFO_SSL_DATA_OUT
:
282 h
->progress
.info
= "send";
284 case CURLINFO_HEADER_IN
:
285 case CURLINFO_DATA_IN
:
286 case CURLINFO_SSL_DATA_IN
:
287 h
->progress
.info
= "receive";
295 _dpf(type
, data
, length
);
301 static int php_http_curle_header_callback(char *data
, size_t n
, size_t l
, void *arg
)
303 php_http_client_curl_handler_t
*h
= arg
;
305 return php_http_buffer_append(&h
->response
.headers
, data
, n
* l
);
308 static int php_http_curle_body_callback(char *data
, size_t n
, size_t l
, void *arg
)
310 php_http_client_curl_handler_t
*h
= arg
;
312 return php_http_message_body_append(h
->response
.body
, data
, n
*l
);
315 static ZEND_RESULT_CODE
php_http_curle_get_info(CURL
*ch
, HashTable
*info
)
320 struct curl_slist
*s
= NULL
, *p
= NULL
;
323 /* BEGIN::CURLINFO */
324 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_EFFECTIVE_URL
, &c
)) {
325 ZVAL_STRING(&tmp
, STR_PTR(c
));
326 zend_hash_str_update(info
, "effective_url", lenof("effective_url"), &tmp
);
328 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_RESPONSE_CODE
, &l
)) {
330 zend_hash_str_update(info
, "response_code", lenof("response_code"), &tmp
);
332 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_TOTAL_TIME
, &d
)) {
333 ZVAL_DOUBLE(&tmp
, d
);
334 zend_hash_str_update(info
, "total_time", lenof("total_time"), &tmp
);
336 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NAMELOOKUP_TIME
, &d
)) {
337 ZVAL_DOUBLE(&tmp
, d
);
338 zend_hash_str_update(info
, "namelookup_time", lenof("namelookup_time"), &tmp
);
340 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONNECT_TIME
, &d
)) {
341 ZVAL_DOUBLE(&tmp
, d
);
342 zend_hash_str_update(info
, "connect_time", lenof("connect_time"), &tmp
);
344 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRETRANSFER_TIME
, &d
)) {
345 ZVAL_DOUBLE(&tmp
, d
);
346 zend_hash_str_update(info
, "pretransfer_time", lenof("pretransfer_time"), &tmp
);
348 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_UPLOAD
, &d
)) {
349 ZVAL_DOUBLE(&tmp
, d
);
350 zend_hash_str_update(info
, "size_upload", lenof("size_upload"), &tmp
);
352 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_DOWNLOAD
, &d
)) {
353 ZVAL_DOUBLE(&tmp
, d
);
354 zend_hash_str_update(info
, "size_download", lenof("size_download"), &tmp
);
356 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_DOWNLOAD
, &d
)) {
357 ZVAL_DOUBLE(&tmp
, d
);
358 zend_hash_str_update(info
, "speed_download", lenof("speed_download"), &tmp
);
360 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_UPLOAD
, &d
)) {
361 ZVAL_DOUBLE(&tmp
, d
);
362 zend_hash_str_update(info
, "speed_upload", lenof("speed_upload"), &tmp
);
364 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HEADER_SIZE
, &l
)) {
366 zend_hash_str_update(info
, "header_size", lenof("header_size"), &tmp
);
368 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REQUEST_SIZE
, &l
)) {
370 zend_hash_str_update(info
, "request_size", lenof("request_size"), &tmp
);
372 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_VERIFYRESULT
, &l
)) {
374 zend_hash_str_update(info
, "ssl_verifyresult", lenof("ssl_verifyresult"), &tmp
);
376 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_FILETIME
, &l
)) {
378 zend_hash_str_update(info
, "filetime", lenof("filetime"), &tmp
);
380 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
381 ZVAL_DOUBLE(&tmp
, d
);
382 zend_hash_str_update(info
, "content_length_download", lenof("content_length_download"), &tmp
);
384 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_UPLOAD
, &d
)) {
385 ZVAL_DOUBLE(&tmp
, d
);
386 zend_hash_str_update(info
, "content_length_upload", lenof("content_length_upload"), &tmp
);
388 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_STARTTRANSFER_TIME
, &d
)) {
389 ZVAL_DOUBLE(&tmp
, d
);
390 zend_hash_str_update(info
, "starttransfer_time", lenof("starttransfer_time"), &tmp
);
392 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_TYPE
, &c
)) {
393 ZVAL_STRING(&tmp
, STR_PTR(c
));
394 zend_hash_str_update(info
, "content_type", lenof("content_type"), &tmp
);
396 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_TIME
, &d
)) {
397 ZVAL_DOUBLE(&tmp
, d
);
398 zend_hash_str_update(info
, "redirect_time", lenof("redirect_time"), &tmp
);
400 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_COUNT
, &l
)) {
402 zend_hash_str_update(info
, "redirect_count", lenof("redirect_count"), &tmp
);
404 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTP_CONNECTCODE
, &l
)) {
406 zend_hash_str_update(info
, "connect_code", lenof("connect_code"), &tmp
);
408 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTPAUTH_AVAIL
, &l
)) {
410 zend_hash_str_update(info
, "httpauth_avail", lenof("httpauth_avail"), &tmp
);
412 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PROXYAUTH_AVAIL
, &l
)) {
414 zend_hash_str_update(info
, "proxyauth_avail", lenof("proxyauth_avail"), &tmp
);
416 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_OS_ERRNO
, &l
)) {
418 zend_hash_str_update(info
, "os_errno", lenof("os_errno"), &tmp
);
420 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NUM_CONNECTS
, &l
)) {
422 zend_hash_str_update(info
, "num_connects", lenof("num_connects"), &tmp
);
424 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_ENGINES
, &s
)) {
426 for (p
= s
; p
; p
= p
->next
) {
428 add_next_index_string(&tmp
, p
->data
);
431 zend_hash_str_update(info
, "ssl_engines", lenof("ssl_engines"), &tmp
);
432 curl_slist_free_all(s
);
434 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_URL
, &c
)) {
435 ZVAL_STRING(&tmp
, STR_PTR(c
));
436 zend_hash_str_update(info
, "redirect_url", lenof("redirect_url"), &tmp
);
438 #if PHP_HTTP_CURL_VERSION(7,19,0)
439 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRIMARY_IP
, &c
)) {
440 ZVAL_STRING(&tmp
, STR_PTR(c
));
441 zend_hash_str_update(info
, "primary_ip", lenof("primary_ip"), &tmp
);
444 #if PHP_HTTP_CURL_VERSION(7,19,0)
445 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_APPCONNECT_TIME
, &d
)) {
446 ZVAL_DOUBLE(&tmp
, d
);
447 zend_hash_str_update(info
, "appconnect_time", lenof("appconnect_time"), &tmp
);
450 #if PHP_HTTP_CURL_VERSION(7,19,4)
451 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONDITION_UNMET
, &l
)) {
453 zend_hash_str_update(info
, "condition_unmet", lenof("condition_unmet"), &tmp
);
456 #if PHP_HTTP_CURL_VERSION(7,21,0)
457 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRIMARY_PORT
, &l
)) {
459 zend_hash_str_update(info
, "primary_port", lenof("primary_port"), &tmp
);
462 #if PHP_HTTP_CURL_VERSION(7,21,0)
463 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_LOCAL_IP
, &c
)) {
464 ZVAL_STRING(&tmp
, STR_PTR(c
));
465 zend_hash_str_update(info
, "local_ip", lenof("local_ip"), &tmp
);
468 #if PHP_HTTP_CURL_VERSION(7,21,0)
469 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_LOCAL_PORT
, &l
)) {
471 zend_hash_str_update(info
, "local_port", lenof("local_port"), &tmp
);
477 #if PHP_HTTP_CURL_VERSION(7,34,0)
479 zval ti_array
, subarray
;
480 struct curl_tlssessioninfo
*ti
;
482 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_TLS_SESSION
, &ti
)) {
485 ZVAL_NULL(&subarray
);
486 array_init(&ti_array
);
488 switch (ti
->backend
) {
489 case CURLSSLBACKEND_NONE
:
492 case CURLSSLBACKEND_OPENSSL
:
494 #ifdef PHP_HTTP_HAVE_OPENSSL
496 SSL_CTX
*ctx
= ti
->internals
;
498 array_init(&subarray
);
499 add_assoc_long_ex(&subarray
, ZEND_STRL("number"), SSL_CTX_sess_number(ctx
));
500 add_assoc_long_ex(&subarray
, ZEND_STRL("connect"), SSL_CTX_sess_connect(ctx
));
501 add_assoc_long_ex(&subarray
, ZEND_STRL("connect_good"), SSL_CTX_sess_connect_good(ctx
));
502 add_assoc_long_ex(&subarray
, ZEND_STRL("connect_renegotiate"), SSL_CTX_sess_connect_renegotiate(ctx
));
503 add_assoc_long_ex(&subarray
, ZEND_STRL("hits"), SSL_CTX_sess_hits(ctx
));
504 add_assoc_long_ex(&subarray
, ZEND_STRL("cache_full"), SSL_CTX_sess_cache_full(ctx
));
508 case CURLSSLBACKEND_GNUTLS
:
510 #ifdef PHP_HTTP_HAVE_GNUTLS
512 gnutls_session_t sess
= ti
->internals
;
515 array_init(&subarray
);
516 if ((desc
= gnutls_session_get_desc(sess
))) {
517 add_assoc_string_ex(&subarray
, ZEND_STRL("desc"), desc
);
520 add_assoc_bool_ex(&subarray
, ZEND_STRL("resumed"), gnutls_session_is_resumed(sess
));
524 case CURLSSLBACKEND_NSS
:
527 #if !PHP_HTTP_CURL_VERSION(7,39,0)
528 case CURLSSLBACKEND_QSOSSL
:
532 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)) || (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))
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
)
608 php_http_message_t
*response
;
609 php_http_header_parser_t parser
;
612 response
= php_http_message_init(NULL
, 0, h
->response
.body
);
613 php_http_header_parser_init(&parser
);
614 while (h
->response
.headers
.used
) {
615 php_http_header_parser_state_t st
= php_http_header_parser_parse(&parser
,
616 &h
->response
.headers
, PHP_HTTP_HEADER_PARSER_CLEANUP
, &response
->hdrs
,
617 (php_http_info_callback_t
) php_http_message_info_callback
, (void *) &response
);
618 if (PHP_HTTP_HEADER_PARSER_STATE_FAILURE
== st
) {
622 php_http_header_parser_dtor(&parser
);
624 /* move body to right message */
625 if (response
->body
!= h
->response
.body
) {
626 php_http_message_t
*ptr
= response
;
628 while (ptr
->parent
) {
631 php_http_message_body_free(&response
->body
);
632 response
->body
= ptr
->body
;
635 php_http_message_body_addref(h
->response
.body
);
637 /* let's update the response headers */
638 if ((zh
= php_http_message_header(response
, ZEND_STRL("Content-Length")))) {
640 zend_hash_str_update(&response
->hdrs
, "X-Original-Content-Length", lenof("X-Original-Content-Length"), zh
);
642 if ((zh
= php_http_message_header(response
, ZEND_STRL("Transfer-Encoding")))) {
644 zend_hash_str_update(&response
->hdrs
, "X-Original-Transfer-Encoding", lenof("X-Original-Transfer-Encoding"), zh
);
645 zend_hash_str_del(&response
->hdrs
, "Transfer-Encoding", lenof("Transfer-Encoding"));
647 if ((zh
= php_http_message_header(response
, ZEND_STRL("Content-Range")))) {
649 zend_hash_str_update(&response
->hdrs
, "X-Original-Content-Range", lenof("X-Original-Content-Range"), zh
);
650 zend_hash_str_del(&response
->hdrs
, "Content-Range", lenof("Content-Range"));
652 if ((zh
= php_http_message_header(response
, ZEND_STRL("Content-Encoding")))) {
654 zend_hash_str_update(&response
->hdrs
, "X-Original-Content-Encoding", lenof("X-Original-Content-Encoding"), zh
);
655 zend_hash_str_del(&response
->hdrs
, "Content-Encoding", lenof("Content-Encoding"));
657 php_http_message_update_headers(response
);
662 static void php_http_curlm_responsehandler(php_http_client_t
*context
)
664 int err_count
= 0, remaining
= 0;
665 php_http_curle_storage_t
*st
, *err
= NULL
;
666 php_http_client_enqueue_t
*enqueue
;
667 php_http_client_curl_t
*curl
= context
->ctx
;
670 CURLMsg
*msg
= curl_multi_info_read(curl
->handle
, &remaining
);
672 if (msg
&& CURLMSG_DONE
== msg
->msg
) {
673 if (CURLE_OK
!= msg
->data
.result
) {
674 st
= php_http_curle_get_storage(msg
->easy_handle
);
675 st
->errorcode
= msg
->data
.result
;
677 /* defer the warnings/exceptions, so the callback is still called for this request */
679 err
= ecalloc(remaining
+ 1, sizeof(*err
));
681 memcpy(&err
[err_count
], st
, sizeof(*st
));
683 err
[err_count
].url
= estrdup(st
->url
);
688 if ((enqueue
= php_http_client_enqueued(context
, msg
->easy_handle
, compare_queue
))) {
689 php_http_client_curl_handler_t
*handler
= enqueue
->opaque
;
690 php_http_message_t
*response
= php_http_curlm_responseparser(handler
);
693 context
->callback
.response
.func(context
->callback
.response
.arg
, context
, &handler
->queue
, &response
);
694 php_http_message_free(&response
);
704 php_error_docref(NULL
, E_WARNING
, "%s; %s (%s)", curl_easy_strerror(err
[i
].errorcode
), err
[i
].errorbuffer
, STR_PTR(err
[i
].url
));
708 } while (++i
< err_count
);
714 #if PHP_HTTP_HAVE_EVENT
716 typedef struct php_http_curlm_event
{
718 php_http_client_t
*context
;
719 } php_http_curlm_event_t
;
721 static inline int etoca(short action
) {
722 switch (action
& (EV_READ
|EV_WRITE
)) {
724 return CURL_CSELECT_IN
;
727 return CURL_CSELECT_OUT
;
729 case EV_READ
|EV_WRITE
:
730 return CURL_CSELECT_IN
|CURL_CSELECT_OUT
;
737 static void php_http_curlm_timeout_callback(int socket
, short action
, void *event_data
)
739 php_http_client_t
*context
= event_data
;
740 php_http_client_curl_t
*curl
= context
->ctx
;
743 fprintf(stderr
, "T");
745 if (curl
->useevents
) {
748 /* ignore and use -1,0 on timeout */
752 while (CURLM_CALL_MULTI_PERFORM
== (rc
= curl_multi_socket_action(curl
->handle
, CURL_SOCKET_TIMEOUT
, 0, &curl
->unfinished
)));
754 if (CURLM_OK
!= rc
) {
755 php_error_docref(NULL
, E_WARNING
, "%s", curl_multi_strerror(rc
));
758 php_http_curlm_responsehandler(context
);
762 static void php_http_curlm_event_callback(int socket
, short action
, void *event_data
)
764 php_http_client_t
*context
= event_data
;
765 php_http_client_curl_t
*curl
= context
->ctx
;
768 fprintf(stderr
, "E");
770 if (curl
->useevents
) {
771 CURLMcode rc
= CURLM_OK
;
773 while (CURLM_CALL_MULTI_PERFORM
== (rc
= curl_multi_socket_action(curl
->handle
, socket
, etoca(action
), &curl
->unfinished
)));
775 if (CURLM_OK
!= rc
) {
776 php_error_docref(NULL
, E_WARNING
, "%s", curl_multi_strerror(rc
));
779 php_http_curlm_responsehandler(context
);
781 /* remove timeout if there are no transfers left */
782 if (!curl
->unfinished
&& event_initialized(curl
->timeout
) && event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
783 event_del(curl
->timeout
);
788 static int php_http_curlm_socket_callback(CURL
*easy
, curl_socket_t sock
, int action
, void *socket_data
, void *assign_data
)
790 php_http_client_t
*context
= socket_data
;
791 php_http_client_curl_t
*curl
= context
->ctx
;
794 fprintf(stderr
, "S");
796 if (curl
->useevents
) {
797 int events
= EV_PERSIST
;
798 php_http_curlm_event_t
*ev
= assign_data
;
801 ev
= ecalloc(1, sizeof(php_http_curlm_event_t
));
802 ev
->context
= context
;
803 curl_multi_assign(curl
->handle
, sock
, ev
);
805 event_del(&ev
->evnt
);
815 case CURL_POLL_INOUT
:
816 events
|= EV_READ
|EV_WRITE
;
819 case CURL_POLL_REMOVE
:
826 php_error_docref(NULL
, E_WARNING
, "Unknown socket action %d", action
);
830 event_assign(&ev
->evnt
, curl
->evbase
, sock
, events
, php_http_curlm_event_callback
, context
);
831 event_add(&ev
->evnt
, NULL
);
837 static void php_http_curlm_timer_callback(CURLM
*multi
, long timeout_ms
, void *timer_data
)
839 php_http_client_t
*context
= timer_data
;
840 php_http_client_curl_t
*curl
= context
->ctx
;
843 fprintf(stderr
, "\ntimer <- timeout_ms: %ld\n", timeout_ms
);
845 if (curl
->useevents
) {
847 if (timeout_ms
< 0) {
848 php_http_curlm_timeout_callback(CURL_SOCKET_TIMEOUT
, /*EV_READ|EV_WRITE*/0, context
);
849 } else if (timeout_ms
> 0 || !event_initialized(curl
->timeout
) || !event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
850 struct timeval timeout
;
852 if (!event_initialized(curl
->timeout
)) {
853 event_assign(curl
->timeout
, curl
->evbase
, CURL_SOCKET_TIMEOUT
, 0, php_http_curlm_timeout_callback
, context
);
856 timeout
.tv_sec
= timeout_ms
/ 1000;
857 timeout
.tv_usec
= (timeout_ms
% 1000) * 1000;
859 event_add(curl
->timeout
, &timeout
);
864 #endif /* HAVE_EVENT */
868 static php_http_options_t php_http_curle_options
, php_http_curlm_options
;
870 #define PHP_HTTP_CURLE_OPTION_CHECK_STRLEN 0x0001
871 #define PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR 0x0002
872 #define PHP_HTTP_CURLE_OPTION_TRANSFORM_MS 0x0004
874 static ZEND_RESULT_CODE
php_http_curle_option_set_ssl_verifyhost(php_http_option_t
*opt
, zval
*val
, void *userdata
)
876 php_http_client_curl_handler_t
*curl
= userdata
;
877 CURL
*ch
= curl
->handle
;
879 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_SSL_VERIFYHOST
, Z_TYPE_P(val
) == IS_TRUE
? 2 : 0)) {
885 static ZEND_RESULT_CODE
php_http_curle_option_set_cookiestore(php_http_option_t
*opt
, zval
*val
, void *userdata
)
887 php_http_client_curl_handler_t
*curl
= userdata
;
888 CURL
*ch
= curl
->handle
;
889 php_http_curle_storage_t
*storage
= php_http_curle_get_storage(curl
->handle
);
891 if (storage
->cookiestore
) {
892 pefree(storage
->cookiestore
, 1);
894 if (val
&& Z_TYPE_P(val
) == IS_STRING
&& Z_STRLEN_P(val
)) {
895 storage
->cookiestore
= pestrndup(Z_STRVAL_P(val
), Z_STRLEN_P(val
), 1);
897 storage
->cookiestore
= NULL
;
899 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, storage
->cookiestore
)
900 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, storage
->cookiestore
)
907 static ZEND_RESULT_CODE
php_http_curle_option_set_cookies(php_http_option_t
*opt
, zval
*val
, void *userdata
)
909 php_http_client_curl_handler_t
*curl
= userdata
;
910 CURL
*ch
= curl
->handle
;
912 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
913 HashTable
*ht
= HASH_OF(val
);
915 if (curl
->options
.encode_cookies
) {
916 if (SUCCESS
== php_http_url_encode_hash_ex(ht
, &curl
->options
.cookies
, ZEND_STRL(";"), ZEND_STRL("="), NULL
, 0)) {
917 php_http_buffer_fix(&curl
->options
.cookies
);
918 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIE
, curl
->options
.cookies
.data
)) {
925 php_http_arrkey_t cookie_key
;
928 ZEND_HASH_FOREACH_KEY_VAL(ht
, cookie_key
.h
, cookie_key
.key
, cookie_val
)
930 zend_string
*zs
= zval_get_string(cookie_val
);
932 php_http_arrkey_stringify(&cookie_key
, NULL
);
933 php_http_buffer_appendf(&curl
->options
.cookies
, "%s=%s; ", cookie_key
.key
->val
, zs
->val
);
934 php_http_arrkey_dtor(&cookie_key
);
936 zend_string_release(zs
);
938 ZEND_HASH_FOREACH_END();
940 php_http_buffer_fix(&curl
->options
.cookies
);
941 if (curl
->options
.cookies
.used
) {
942 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIE
, curl
->options
.cookies
.data
)) {
948 php_http_buffer_reset(&curl
->options
.cookies
);
949 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_COOKIE
, NULL
)) {
956 static ZEND_RESULT_CODE
php_http_curle_option_set_encodecookies(php_http_option_t
*opt
, zval
*val
, void *userdata
)
958 php_http_client_curl_handler_t
*curl
= userdata
;
960 curl
->options
.encode_cookies
= Z_TYPE_P(val
) == IS_TRUE
;
964 static ZEND_RESULT_CODE
php_http_curle_option_set_lastmodified(php_http_option_t
*opt
, zval
*val
, void *userdata
)
966 php_http_client_curl_handler_t
*curl
= userdata
;
967 CURL
*ch
= curl
->handle
;
970 if (Z_LVAL_P(val
) > 0) {
971 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, Z_LVAL_P(val
))) {
975 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, (long) sapi_get_request_time() + Z_LVAL_P(val
))) {
979 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, (long) (curl
->options
.range_request
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
))) {
983 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, 0)
984 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, 0)
992 static ZEND_RESULT_CODE
php_http_curle_option_set_compress(php_http_option_t
*opt
, zval
*val
, void *userdata
)
994 php_http_client_curl_handler_t
*curl
= userdata
;
995 CURL
*ch
= curl
->handle
;
997 #if !PHP_HTTP_CURL_VERSION(7,21,6)
998 # define CURLOPT_ACCEPT_ENCODING CURLOPT_ENCODING
1000 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_ACCEPT_ENCODING
, Z_TYPE_P(val
) == IS_TRUE
? "" : NULL
)) {
1006 static ZEND_RESULT_CODE
php_http_curle_option_set_etag(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1008 php_http_client_curl_handler_t
*curl
= userdata
;
1009 php_http_buffer_t header
;
1011 if (val
&& Z_TYPE_P(val
) == IS_STRING
&& Z_STRLEN_P(val
)) {
1012 zend_bool is_quoted
= !((Z_STRVAL_P(val
)[0] != '"') || (Z_STRVAL_P(val
)[Z_STRLEN_P(val
)-1] != '"'));
1013 php_http_buffer_init(&header
);
1014 php_http_buffer_appendf(&header
, is_quoted
?"%s: %s":"%s: \"%s\"", curl
->options
.range_request
?"If-Match":"If-None-Match", Z_STRVAL_P(val
));
1015 php_http_buffer_fix(&header
);
1016 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, header
.data
);
1017 php_http_buffer_dtor(&header
);
1022 static ZEND_RESULT_CODE
php_http_curle_option_set_range(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1024 php_http_client_curl_handler_t
*curl
= userdata
;
1025 CURL
*ch
= curl
->handle
;
1027 php_http_buffer_reset(&curl
->options
.ranges
);
1029 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
1032 HashTable
*ht
= HASH_OF(val
);
1034 ZEND_HASH_FOREACH_VAL(ht
, rr
)
1036 if (Z_TYPE_P(rr
) == IS_ARRAY
) {
1037 if (2 == php_http_array_list(Z_ARRVAL_P(rr
), 2, &rb
, &re
)) {
1038 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))) &&
1039 ((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)))) {
1040 if ((rbl
>= 0) && (rel
>= 0)) {
1041 php_http_buffer_appendf(&curl
->options
.ranges
, "%ld-%ld,", rbl
, rel
);
1048 ZEND_HASH_FOREACH_END();
1050 if (curl
->options
.ranges
.used
) {
1051 curl
->options
.range_request
= 1;
1052 /* ditch last comma */
1053 curl
->options
.ranges
.data
[curl
->options
.ranges
.used
- 1] = '\0';
1057 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RANGE
, curl
->options
.ranges
.data
)) {
1063 static ZEND_RESULT_CODE
php_http_curle_option_set_resume(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1065 php_http_client_curl_handler_t
*curl
= userdata
;
1066 CURL
*ch
= curl
->handle
;
1068 if (Z_LVAL_P(val
) > 0) {
1069 curl
->options
.range_request
= 1;
1071 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RESUME_FROM
, Z_LVAL_P(val
))) {
1077 static ZEND_RESULT_CODE
php_http_curle_option_set_retrydelay(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1079 php_http_client_curl_handler_t
*curl
= userdata
;
1081 curl
->options
.retry
.delay
= Z_DVAL_P(val
);
1085 static ZEND_RESULT_CODE
php_http_curle_option_set_retrycount(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1087 php_http_client_curl_handler_t
*curl
= userdata
;
1089 curl
->options
.retry
.count
= Z_LVAL_P(val
);
1093 static ZEND_RESULT_CODE
php_http_curle_option_set_redirect(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1095 php_http_client_curl_handler_t
*curl
= userdata
;
1096 CURL
*ch
= curl
->handle
;
1098 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(val
) ? 1L : 0L)
1099 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, curl
->options
.redirects
= Z_LVAL_P(val
))
1106 static ZEND_RESULT_CODE
php_http_curle_option_set_portrange(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1108 php_http_client_curl_handler_t
*curl
= userdata
;
1109 CURL
*ch
= curl
->handle
;
1110 long localport
= 0, localportrange
= 0;
1112 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
1115 switch (php_http_array_list(Z_ARRVAL_P(val
), 2, &zps
, &zpe
)) {
1117 localportrange
= labs(zval_get_long(zps
)-zval_get_long(zpe
))+1L;
1120 localport
= (zval_get_long(zpe
) > 0) ? MIN(zval_get_long(zps
), zval_get_long(zpe
)) : zval_get_long(zps
);
1126 if ( CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_LOCALPORT
, localport
)
1127 || CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_LOCALPORTRANGE
, localportrange
)
1134 #if PHP_HTTP_CURL_VERSION(7,37,0)
1135 static ZEND_RESULT_CODE
php_http_curle_option_set_proxyheader(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1137 php_http_client_curl_handler_t
*curl
= userdata
;
1139 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
1140 php_http_arrkey_t header_key
;
1142 php_http_buffer_t header
;
1144 php_http_buffer_init(&header
);
1145 ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(val
), header_key
.h
, header_key
.key
, header_val
)
1147 if (header_key
.key
) {
1148 zend_string
*zs
= zval_get_string(header_val
);
1150 php_http_buffer_appendf(&header
, "%s: %s", header_key
.key
->val
, zs
->val
);
1151 zend_string_release(zs
);
1153 php_http_buffer_fix(&header
);
1154 curl
->options
.proxyheaders
= curl_slist_append(curl
->options
.proxyheaders
, header
.data
);
1155 php_http_buffer_reset(&header
);
1159 ZEND_HASH_FOREACH_END();
1160 php_http_buffer_dtor(&header
);
1162 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_PROXYHEADER
, curl
->options
.proxyheaders
)) {
1165 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_HEADEROPT
, CURLHEADER_SEPARATE
)) {
1166 curl_easy_setopt(curl
->handle
, CURLOPT_PROXYHEADER
, NULL
);
1173 #if PHP_HTTP_CURL_VERSION(7,21,3)
1174 static ZEND_RESULT_CODE
php_http_curle_option_set_resolve(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1176 php_http_client_curl_handler_t
*curl
= userdata
;
1177 CURL
*ch
= curl
->handle
;
1179 if (val
&& Z_TYPE_P(val
) != IS_NULL
) {
1180 HashTable
*ht
= HASH_OF(val
);
1183 ZEND_HASH_FOREACH_VAL(ht
, data
)
1185 zend_string
*zs
= zval_get_string(data
);
1186 curl
->options
.resolve
= curl_slist_append(curl
->options
.resolve
, zs
->val
);
1187 zend_string_release(zs
);
1189 ZEND_HASH_FOREACH_END();
1191 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RESOLVE
, curl
->options
.resolve
)) {
1195 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_RESOLVE
, NULL
)) {
1203 #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
1204 static ZEND_RESULT_CODE
php_http_curle_option_set_ssl_tlsauthtype(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1206 php_http_client_curl_handler_t
*curl
= userdata
;
1207 CURL
*ch
= curl
->handle
;
1209 if (val
&& Z_LVAL_P(val
)) {
1210 switch (Z_LVAL_P(val
)) {
1211 case CURL_TLSAUTH_SRP
:
1212 if (CURLE_OK
== curl_easy_setopt(ch
, CURLOPT_TLSAUTH_TYPE
, PHP_HTTP_CURL_TLSAUTH_SRP
)) {
1220 if (CURLE_OK
!= curl_easy_setopt(ch
, CURLOPT_TLSAUTH_TYPE
, PHP_HTTP_CURL_TLSAUTH_DEF
)) {
1227 static void php_http_curle_options_init(php_http_options_t
*registry
)
1229 php_http_option_t
*opt
;
1232 #if PHP_HTTP_CURL_VERSION(7,42,0)
1233 php_http_option_register(registry
, ZEND_STRL("path_as_is"), CURLOPT_PATH_AS_IS
, _IS_BOOL
);
1237 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyhost"), CURLOPT_PROXY
, IS_STRING
))) {
1238 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1240 php_http_option_register(registry
, ZEND_STRL("proxytype"), CURLOPT_PROXYTYPE
, IS_LONG
);
1241 php_http_option_register(registry
, ZEND_STRL("proxyport"), CURLOPT_PROXYPORT
, IS_LONG
);
1242 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyauth"), CURLOPT_PROXYUSERPWD
, IS_STRING
))) {
1243 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1245 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyauthtype"), CURLOPT_PROXYAUTH
, IS_LONG
))) {
1246 Z_LVAL(opt
->defval
) = CURLAUTH_ANYSAFE
;
1248 php_http_option_register(registry
, ZEND_STRL("proxytunnel"), CURLOPT_HTTPPROXYTUNNEL
, _IS_BOOL
);
1249 #if PHP_HTTP_CURL_VERSION(7,19,4)
1250 php_http_option_register(registry
, ZEND_STRL("noproxy"), CURLOPT_NOPROXY
, IS_STRING
);
1253 #if PHP_HTTP_CURL_VERSION(7,37,0)
1254 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxyheader"), CURLOPT_PROXYHEADER
, IS_ARRAY
))) {
1255 opt
->setter
= php_http_curle_option_set_proxyheader
;
1258 #if PHP_HTTP_CURL_VERSION(7,43,0)
1259 if ((opt
= php_http_option_register(registry
, ZEND_STRL("proxy_service_name"), CURLOPT_PROXY_SERVICE_NAME
, IS_STRING
))) {
1260 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1264 #if PHP_HTTP_CURL_VERSION(7,40,0)
1265 if ((opt
= php_http_option_register(registry
, ZEND_STRL("unix_socket_path"), CURLOPT_UNIX_SOCKET_PATH
, IS_STRING
))) {
1266 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1267 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1272 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_cache_timeout"), CURLOPT_DNS_CACHE_TIMEOUT
, IS_LONG
))) {
1273 Z_LVAL(opt
->defval
) = 60;
1275 php_http_option_register(registry
, ZEND_STRL("ipresolve"), CURLOPT_IPRESOLVE
, IS_LONG
);
1276 #if PHP_HTTP_CURL_VERSION(7,21,3)
1277 if ((opt
= php_http_option_register(registry
, ZEND_STRL("resolve"), CURLOPT_RESOLVE
, IS_ARRAY
))) {
1278 opt
->setter
= php_http_curle_option_set_resolve
;
1281 #if PHP_HTTP_HAVE_ARES
1282 # if PHP_HTTP_CURL_VERSION(7,24,0)
1283 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_servers"), CURLOPT_DNS_SERVERS
, IS_STRING
))) {
1284 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1287 # if PHP_HTTP_CURL_VERSION(7,33,0)
1288 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_interface"), CURLOPT_DNS_INTERFACE
, IS_STRING
))) {
1289 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1291 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_local_ip4"), CURLOPT_DNS_LOCAL_IP4
, IS_STRING
))) {
1292 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1294 if ((opt
= php_http_option_register(registry
, ZEND_STRL("dns_local_ip6"), CURLOPT_DNS_LOCAL_IP6
, IS_STRING
))) {
1295 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1301 php_http_option_register(registry
, ZEND_STRL("low_speed_limit"), CURLOPT_LOW_SPEED_LIMIT
, IS_LONG
);
1302 php_http_option_register(registry
, ZEND_STRL("low_speed_time"), CURLOPT_LOW_SPEED_TIME
, IS_LONG
);
1305 php_http_option_register(registry, ZEND_STRL("max_send_speed"), CURLOPT_MAX_SEND_SPEED_LARGE, IS_LONG);
1306 php_http_option_register(registry, ZEND_STRL("max_recv_speed"), CURLOPT_MAX_RECV_SPEED_LARGE, IS_LONG);
1309 /* connection handling */
1311 if ((opt = php_http_option_register(registry, ZEND_STRL("maxconnects"), CURLOPT_MAXCONNECTS, IS_LONG))) {
1312 Z_LVAL(opt->defval) = 5;
1315 php_http_option_register(registry
, ZEND_STRL("fresh_connect"), CURLOPT_FRESH_CONNECT
, _IS_BOOL
);
1316 php_http_option_register(registry
, ZEND_STRL("forbid_reuse"), CURLOPT_FORBID_REUSE
, _IS_BOOL
);
1318 /* outgoing interface */
1319 php_http_option_register(registry
, ZEND_STRL("interface"), CURLOPT_INTERFACE
, IS_STRING
);
1320 if ((opt
= php_http_option_register(registry
, ZEND_STRL("portrange"), CURLOPT_LOCALPORT
, IS_ARRAY
))) {
1321 opt
->setter
= php_http_curle_option_set_portrange
;
1324 /* another endpoint port */
1325 php_http_option_register(registry
, ZEND_STRL("port"), CURLOPT_PORT
, IS_LONG
);
1327 /* RFC4007 zone_id */
1328 #if PHP_HTTP_CURL_VERSION(7,19,0)
1329 php_http_option_register(registry
, ZEND_STRL("address_scope"), CURLOPT_ADDRESS_SCOPE
, IS_LONG
);
1333 if ((opt
= php_http_option_register(registry
, ZEND_STRL("httpauth"), CURLOPT_USERPWD
, IS_STRING
))) {
1334 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1336 if ((opt
= php_http_option_register(registry
, ZEND_STRL("httpauthtype"), CURLOPT_HTTPAUTH
, IS_LONG
))) {
1337 Z_LVAL(opt
->defval
) = CURLAUTH_ANYSAFE
;
1339 #if PHP_HTTP_CURL_VERSION(7,43,0)
1340 if ((opt
= php_http_option_register(registry
, ZEND_STRL("service_name"), CURLOPT_SERVICE_NAME
, IS_STRING
))) {
1341 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1346 if ((opt
= php_http_option_register(registry
, ZEND_STRL("redirect"), CURLOPT_FOLLOWLOCATION
, IS_LONG
))) {
1347 opt
->setter
= php_http_curle_option_set_redirect
;
1349 php_http_option_register(registry
, ZEND_STRL("unrestricted_auth"), CURLOPT_UNRESTRICTED_AUTH
, _IS_BOOL
);
1350 #if PHP_HTTP_CURL_VERSION(7,19,1)
1351 php_http_option_register(registry
, ZEND_STRL("postredir"), CURLOPT_POSTREDIR
, IS_LONG
);
1355 if ((opt
= php_http_option_register(registry
, ZEND_STRL("retrycount"), 0, IS_LONG
))) {
1356 opt
->setter
= php_http_curle_option_set_retrycount
;
1358 if ((opt
= php_http_option_register(registry
, ZEND_STRL("retrydelay"), 0, IS_DOUBLE
))) {
1359 opt
->setter
= php_http_curle_option_set_retrydelay
;
1363 if ((opt
= php_http_option_register(registry
, ZEND_STRL("referer"), CURLOPT_REFERER
, IS_STRING
))) {
1364 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1366 if ((opt
= php_http_option_register(registry
, ZEND_STRL("autoreferer"), CURLOPT_AUTOREFERER
, _IS_BOOL
))) {
1367 ZVAL_BOOL(&opt
->defval
, 1);
1371 if ((opt
= php_http_option_register(registry
, ZEND_STRL("useragent"), CURLOPT_USERAGENT
, IS_STRING
))) {
1372 /* don't check strlen, to allow sending no useragent at all */
1373 ZVAL_PSTRING(&opt
->defval
,
1374 "PECL_HTTP/" PHP_PECL_HTTP_VERSION
" "
1375 "PHP/" PHP_VERSION
" "
1376 "libcurl/" LIBCURL_VERSION
);
1380 if ((opt
= php_http_option_register(registry
, ZEND_STRL("resume"), CURLOPT_RESUME_FROM
, IS_LONG
))) {
1381 opt
->setter
= php_http_curle_option_set_resume
;
1384 if ((opt
= php_http_option_register(registry
, ZEND_STRL("range"), CURLOPT_RANGE
, IS_ARRAY
))) {
1385 opt
->setter
= php_http_curle_option_set_range
;
1389 if ((opt
= php_http_option_register(registry
, ZEND_STRL("etag"), 0, IS_STRING
))) {
1390 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1391 opt
->setter
= php_http_curle_option_set_etag
;
1395 if ((opt
= php_http_option_register(registry
, ZEND_STRL("compress"), 0, _IS_BOOL
))) {
1396 opt
->setter
= php_http_curle_option_set_compress
;
1400 if ((opt
= php_http_option_register(registry
, ZEND_STRL("lastmodified"), 0, IS_LONG
))) {
1401 opt
->setter
= php_http_curle_option_set_lastmodified
;
1405 if ((opt
= php_http_option_register(registry
, ZEND_STRL("encodecookies"), 0, _IS_BOOL
))) {
1406 opt
->setter
= php_http_curle_option_set_encodecookies
;
1407 ZVAL_BOOL(&opt
->defval
, 1);
1409 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cookies"), 0, IS_ARRAY
))) {
1410 opt
->setter
= php_http_curle_option_set_cookies
;
1413 /* cookiesession, don't load session cookies from cookiestore */
1414 php_http_option_register(registry
, ZEND_STRL("cookiesession"), CURLOPT_COOKIESESSION
, _IS_BOOL
);
1415 /* cookiestore, read initial cookies from that file and store cookies back into that file */
1416 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cookiestore"), 0, IS_STRING
))) {
1417 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1418 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1419 opt
->setter
= php_http_curle_option_set_cookiestore
;
1423 php_http_option_register(registry
, ZEND_STRL("maxfilesize"), CURLOPT_MAXFILESIZE
, IS_LONG
);
1425 /* http protocol version */
1426 php_http_option_register(registry
, ZEND_STRL("protocol"), CURLOPT_HTTP_VERSION
, IS_LONG
);
1429 if ((opt
= php_http_option_register(registry
, ZEND_STRL("timeout"), CURLOPT_TIMEOUT_MS
, IS_DOUBLE
))) {
1430 opt
->flags
|= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS
;
1432 if ((opt
= php_http_option_register(registry
, ZEND_STRL("connecttimeout"), CURLOPT_CONNECTTIMEOUT_MS
, IS_DOUBLE
))) {
1433 opt
->flags
|= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS
;
1434 Z_DVAL(opt
->defval
) = 3;
1436 #if PHP_HTTP_CURL_VERSION(7,36,0)
1437 if ((opt
= php_http_option_register(registry
, ZEND_STRL("expect_100_timeout"), CURLOPT_EXPECT_100_TIMEOUT_MS
, IS_DOUBLE
))) {
1438 opt
->flags
|= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS
;
1439 Z_DVAL(opt
->defval
) = 1;
1444 php_http_option_register(registry
, ZEND_STRL("tcp_nodelay"), CURLOPT_TCP_NODELAY
, _IS_BOOL
);
1445 #if PHP_HTTP_CURL_VERSION(7,25,0)
1446 php_http_option_register(registry
, ZEND_STRL("tcp_keepalive"), CURLOPT_TCP_KEEPALIVE
, _IS_BOOL
);
1447 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tcp_keepidle"), CURLOPT_TCP_KEEPIDLE
, IS_LONG
))) {
1448 Z_LVAL(opt
->defval
) = 60;
1450 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tcp_keepintvl"), CURLOPT_TCP_KEEPINTVL
, IS_LONG
))) {
1451 Z_LVAL(opt
->defval
) = 60;
1456 if ((opt
= php_http_option_register(registry
, ZEND_STRL("ssl"), 0, IS_ARRAY
))) {
1457 registry
= &opt
->suboptions
;
1459 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cert"), CURLOPT_SSLCERT
, IS_STRING
))) {
1460 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1461 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1463 if ((opt
= php_http_option_register(registry
, ZEND_STRL("certtype"), CURLOPT_SSLCERTTYPE
, IS_STRING
))) {
1464 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1465 ZVAL_PSTRING(&opt
->defval
, "PEM");
1467 if ((opt
= php_http_option_register(registry
, ZEND_STRL("key"), CURLOPT_SSLKEY
, IS_STRING
))) {
1468 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1469 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1471 if ((opt
= php_http_option_register(registry
, ZEND_STRL("keytype"), CURLOPT_SSLKEYTYPE
, IS_STRING
))) {
1472 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1473 ZVAL_PSTRING(&opt
->defval
, "PEM");
1475 if ((opt
= php_http_option_register(registry
, ZEND_STRL("keypasswd"), CURLOPT_SSLKEYPASSWD
, IS_STRING
))) {
1476 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1478 php_http_option_register(registry
, ZEND_STRL("engine"), CURLOPT_SSLENGINE
, IS_STRING
);
1479 php_http_option_register(registry
, ZEND_STRL("version"), CURLOPT_SSLVERSION
, IS_LONG
);
1480 if ((opt
= php_http_option_register(registry
, ZEND_STRL("verifypeer"), CURLOPT_SSL_VERIFYPEER
, _IS_BOOL
))) {
1481 ZVAL_BOOL(&opt
->defval
, 1);
1483 if ((opt
= php_http_option_register(registry
, ZEND_STRL("verifyhost"), CURLOPT_SSL_VERIFYHOST
, _IS_BOOL
))) {
1484 ZVAL_BOOL(&opt
->defval
, 1);
1485 opt
->setter
= php_http_curle_option_set_ssl_verifyhost
;
1487 #if PHP_HTTP_CURL_VERSION(7,41,0)
1488 php_http_option_register(registry
, ZEND_STRL("verifystatus"), CURLOPT_SSL_VERIFYSTATUS
, _IS_BOOL
);
1490 php_http_option_register(registry
, ZEND_STRL("cipher_list"), CURLOPT_SSL_CIPHER_LIST
, IS_STRING
);
1491 if ((opt
= php_http_option_register(registry
, ZEND_STRL("cainfo"), CURLOPT_CAINFO
, IS_STRING
))) {
1492 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1493 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1494 #ifdef PHP_HTTP_CURL_CAINFO
1495 ZVAL_PSTRING(&opt
->defval
, PHP_HTTP_CURL_CAINFO
);
1498 if ((opt
= php_http_option_register(registry
, ZEND_STRL("capath"), CURLOPT_CAPATH
, IS_STRING
))) {
1499 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1500 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1502 if ((opt
= php_http_option_register(registry
, ZEND_STRL("random_file"), CURLOPT_RANDOM_FILE
, IS_STRING
))) {
1503 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1504 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1506 if ((opt
= php_http_option_register(registry
, ZEND_STRL("egdsocket"), CURLOPT_EGDSOCKET
, IS_STRING
))) {
1507 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1508 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1510 #if PHP_HTTP_CURL_VERSION(7,19,0)
1511 if ((opt
= php_http_option_register(registry
, ZEND_STRL("issuercert"), CURLOPT_ISSUERCERT
, IS_STRING
))) {
1512 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1513 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1515 # ifdef PHP_HTTP_HAVE_OPENSSL
1516 if ((opt
= php_http_option_register(registry
, ZEND_STRL("crlfile"), CURLOPT_CRLFILE
, IS_STRING
))) {
1517 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1518 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1522 #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))
1523 if ((opt
= php_http_option_register(registry
, ZEND_STRL("certinfo"), CURLOPT_CERTINFO
, _IS_BOOL
))) {
1524 ZVAL_FALSE(&opt
->defval
);
1527 #if PHP_HTTP_CURL_VERSION(7,36,0)
1528 if ((opt
= php_http_option_register(registry
, ZEND_STRL("enable_npn"), CURLOPT_SSL_ENABLE_NPN
, _IS_BOOL
))) {
1529 ZVAL_BOOL(&opt
->defval
, 1);
1531 if ((opt
= php_http_option_register(registry
, ZEND_STRL("enable_alpn"), CURLOPT_SSL_ENABLE_ALPN
, _IS_BOOL
))) {
1532 ZVAL_BOOL(&opt
->defval
, 1);
1535 #if PHP_HTTP_CURL_VERSION(7,39,0)
1536 /* FIXME: see http://curl.haxx.se/libcurl/c/CURLOPT_PINNEDPUBLICKEY.html#AVAILABILITY */
1537 if ((opt
= php_http_option_register(registry
, ZEND_STRL("pinned_publickey"), CURLOPT_PINNEDPUBLICKEY
, IS_STRING
))) {
1538 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1539 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
;
1542 #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
1543 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tlsauthtype"), CURLOPT_TLSAUTH_TYPE
, IS_LONG
))) {
1544 opt
->setter
= php_http_curle_option_set_ssl_tlsauthtype
;
1546 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tlsauthuser"), CURLOPT_TLSAUTH_USERNAME
, IS_STRING
))) {
1547 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1549 if ((opt
= php_http_option_register(registry
, ZEND_STRL("tlsauthpass"), CURLOPT_TLSAUTH_PASSWORD
, IS_STRING
))) {
1550 opt
->flags
|= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
;
1553 #if PHP_HTTP_CURL_VERSION(7,42,0) && (defined(PHP_HTTP_HAVE_NSS) || defined(PHP_HTTP_HAVE_DARWINSSL))
1554 php_http_option_register(registry
, ZEND_STRL("falsestart"), CURLOPT_SSL_FALSESTART
, _IS_BOOL
);
1559 static zval
*php_http_curle_get_option(php_http_option_t
*opt
, HashTable
*options
, void *userdata
)
1561 php_http_client_curl_handler_t
*curl
= userdata
;
1564 if ((option
= php_http_option_get(opt
, options
, NULL
))) {
1567 ZVAL_DUP(&zopt
, option
);
1568 convert_to_explicit_type(&zopt
, opt
->type
);
1569 zend_hash_update(&curl
->options
.cache
, opt
->name
, &zopt
);
1570 return zend_hash_find(&curl
->options
.cache
, opt
->name
);
1575 static ZEND_RESULT_CODE
php_http_curle_set_option(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1577 php_http_client_curl_handler_t
*curl
= userdata
;
1578 CURL
*ch
= curl
->handle
;
1580 CURLcode rc
= CURLE_OK
;
1581 ZEND_RESULT_CODE rv
= SUCCESS
;
1587 switch (opt
->type
) {
1590 rv
= opt
->setter(opt
, val
, curl
);
1591 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, (long) (Z_TYPE_P(val
) == IS_TRUE
))) {
1598 rv
= opt
->setter(opt
, val
, curl
);
1599 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, Z_LVAL_P(val
))) {
1606 rv
= opt
->setter(opt
, val
, curl
);
1607 } else if (!val
|| Z_TYPE_P(val
) == IS_NULL
) {
1608 if (CURLE_OK
!= (rc
= curl_easy_setopt(ch
, opt
->option
, NULL
))) {
1611 } else if ((opt
->flags
& PHP_HTTP_CURLE_OPTION_CHECK_STRLEN
) && !Z_STRLEN_P(val
)) {
1612 if (CURLE_OK
!= (rc
= curl_easy_setopt(ch
, opt
->option
, NULL
))) {
1615 } else if ((opt
->flags
& PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR
) && Z_STRVAL_P(val
) && SUCCESS
!= php_check_open_basedir(Z_STRVAL_P(val
))) {
1616 if (CURLE_OK
!= (rc
= curl_easy_setopt(ch
, opt
->option
, NULL
))) {
1619 } else if (CURLE_OK
!= (rc
= curl_easy_setopt(ch
, opt
->option
, Z_STRVAL_P(val
)))) {
1625 if (opt
->flags
& PHP_HTTP_CURLE_OPTION_TRANSFORM_MS
) {
1627 Z_DVAL(tmp
) *= 1000;
1631 rv
= opt
->setter(opt
, val
, curl
);
1632 } else if (CURLE_OK
!= curl_easy_setopt(ch
, opt
->option
, (long) Z_DVAL_P(val
))) {
1639 rv
= opt
->setter(opt
, val
, curl
);
1640 } else if (Z_TYPE_P(val
) != IS_NULL
) {
1641 rv
= php_http_options_apply(&opt
->suboptions
, Z_ARRVAL_P(val
), curl
);
1647 rv
= opt
->setter(opt
, val
, curl
);
1653 if (rv
!= SUCCESS
) {
1654 php_error_docref(NULL
, E_NOTICE
, "Could not set option %s (%s)", opt
->name
->val
, curl_easy_strerror(rc
));
1659 #if PHP_HTTP_CURL_VERSION(7,30,0)
1660 static ZEND_RESULT_CODE
php_http_curlm_option_set_pipelining_bl(php_http_option_t
*opt
, zval
*value
, void *userdata
)
1662 php_http_client_t
*client
= userdata
;
1663 php_http_client_curl_t
*curl
= client
->ctx
;
1664 CURLM
*ch
= curl
->handle
;
1668 /* array of char *, ending with a NULL */
1669 if (value
&& Z_TYPE_P(value
) != IS_NULL
) {
1671 HashTable
*ht
= HASH_OF(value
);
1672 int c
= zend_hash_num_elements(ht
);
1673 char **ptr
= ecalloc(c
+ 1, sizeof(char *));
1677 zend_hash_init(&tmp_ht
, c
, NULL
, ZVAL_PTR_DTOR
, 0);
1678 array_join(ht
, &tmp_ht
, 0, ARRAY_JOIN_STRINGIFY
);
1680 ZEND_HASH_FOREACH_VAL(&tmp_ht
, entry
)
1682 *ptr
++ = Z_STRVAL_P(entry
);
1684 ZEND_HASH_FOREACH_END();
1687 if (CURLM_OK
!= curl_multi_setopt(ch
, opt
->option
, bl
)) {
1690 zend_hash_destroy(&tmp_ht
);
1697 zend_hash_destroy(&tmp_ht
);
1703 #if PHP_HTTP_HAVE_EVENT
1704 static inline ZEND_RESULT_CODE
php_http_curlm_use_eventloop(php_http_client_t
*h
, zend_bool enable
)
1706 php_http_client_curl_t
*curl
= h
->ctx
;
1708 if ((curl
->useevents
= enable
)) {
1709 if (!curl
->evbase
) {
1710 curl
->evbase
= event_base_new();
1712 if (!curl
->timeout
) {
1713 curl
->timeout
= ecalloc(1, sizeof(struct event
));
1715 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETDATA
, h
);
1716 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETFUNCTION
, php_http_curlm_socket_callback
);
1717 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERDATA
, h
);
1718 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERFUNCTION
, php_http_curlm_timer_callback
);
1720 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETDATA
, NULL
);
1721 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETFUNCTION
, NULL
);
1722 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERDATA
, NULL
);
1723 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERFUNCTION
, NULL
);
1729 static ZEND_RESULT_CODE
php_http_curlm_option_set_use_eventloop(php_http_option_t
*opt
, zval
*value
, void *userdata
)
1731 php_http_client_t
*client
= userdata
;
1733 return php_http_curlm_use_eventloop(client
, value
&& Z_TYPE_P(value
) == IS_TRUE
);
1737 static void php_http_curlm_options_init(php_http_options_t
*registry
)
1739 php_http_option_t
*opt
;
1741 /* set size of connection cache */
1742 if ((opt
= php_http_option_register(registry
, ZEND_STRL("maxconnects"), CURLMOPT_MAXCONNECTS
, IS_LONG
))) {
1743 /* -1 == default, 0 == unlimited */
1744 ZVAL_LONG(&opt
->defval
, -1);
1746 /* set max number of connections to a single host */
1747 #if PHP_HTTP_CURL_VERSION(7,30,0)
1748 php_http_option_register(registry
, ZEND_STRL("max_host_connections"), CURLMOPT_MAX_HOST_CONNECTIONS
, IS_LONG
);
1750 /* maximum number of requests in a pipeline */
1751 #if PHP_HTTP_CURL_VERSION(7,30,0)
1752 if ((opt
= php_http_option_register(registry
, ZEND_STRL("max_pipeline_length"), CURLMOPT_MAX_PIPELINE_LENGTH
, IS_LONG
))) {
1753 ZVAL_LONG(&opt
->defval
, 5);
1756 /* max simultaneously open connections */
1757 #if PHP_HTTP_CURL_VERSION(7,30,0)
1758 php_http_option_register(registry
, ZEND_STRL("max_total_connections"), CURLMOPT_MAX_TOTAL_CONNECTIONS
, IS_LONG
);
1760 /* enable/disable HTTP pipelining */
1761 php_http_option_register(registry
, ZEND_STRL("pipelining"), CURLMOPT_PIPELINING
, _IS_BOOL
);
1762 /* chunk length threshold for pipelining */
1763 #if PHP_HTTP_CURL_VERSION(7,30,0)
1764 php_http_option_register(registry
, ZEND_STRL("chunk_length_penalty_size"), CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE
, IS_LONG
);
1766 /* size threshold for pipelining penalty */
1767 #if PHP_HTTP_CURL_VERSION(7,30,0)
1768 php_http_option_register(registry
, ZEND_STRL("content_length_penalty_size"), CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE
, IS_LONG
);
1770 /* pipelining server blacklist */
1771 #if PHP_HTTP_CURL_VERSION(7,30,0)
1772 if ((opt
= php_http_option_register(registry
, ZEND_STRL("pipelining_server_bl"), CURLMOPT_PIPELINING_SERVER_BL
, IS_ARRAY
))) {
1773 opt
->setter
= php_http_curlm_option_set_pipelining_bl
;
1776 /* pipelining host blacklist */
1777 #if PHP_HTTP_CURL_VERSION(7,30,0)
1778 if ((opt
= php_http_option_register(registry
, ZEND_STRL("pipelining_site_bl"), CURLMOPT_PIPELINING_SITE_BL
, IS_ARRAY
))) {
1779 opt
->setter
= php_http_curlm_option_set_pipelining_bl
;
1783 #if PHP_HTTP_HAVE_EVENT
1784 if ((opt
= php_http_option_register(registry
, ZEND_STRL("use_eventloop"), 0, _IS_BOOL
))) {
1785 opt
->setter
= php_http_curlm_option_set_use_eventloop
;
1790 static ZEND_RESULT_CODE
php_http_curlm_set_option(php_http_option_t
*opt
, zval
*val
, void *userdata
)
1792 php_http_client_t
*client
= userdata
;
1793 php_http_client_curl_t
*curl
= client
->ctx
;
1794 CURLM
*ch
= curl
->handle
;
1796 CURLMcode rc
= CURLM_UNKNOWN_OPTION
;
1797 ZEND_RESULT_CODE rv
= SUCCESS
;
1801 } else if (opt
->type
&& Z_TYPE_P(val
) != opt
->type
&& !(Z_TYPE_P(val
) == IS_NULL
&& opt
->type
== IS_ARRAY
)) {
1804 ZVAL_DUP(&zopt
, val
);
1805 convert_to_explicit_type(&zopt
, opt
->type
);
1811 rv
= opt
->setter(opt
, val
, client
);
1813 switch (opt
->type
) {
1815 if (CURLM_OK
!= (rc
= curl_multi_setopt(ch
, opt
->option
, (long) zend_is_true(val
)))) {
1820 if (CURLM_OK
!= (rc
= curl_multi_setopt(ch
, opt
->option
, Z_LVAL_P(val
)))) {
1830 if (val
&& val
!= orig
&& val
!= &opt
->defval
) {
1834 if (rv
!= SUCCESS
) {
1835 php_error_docref(NULL
, E_NOTICE
, "Could not set option %s (%s)", opt
->name
->val
, curl_easy_strerror(rc
));
1842 static ZEND_RESULT_CODE
php_http_client_curl_handler_reset(php_http_client_curl_handler_t
*curl
)
1844 CURL
*ch
= curl
->handle
;
1845 php_http_curle_storage_t
*st
;
1847 if ((st
= php_http_curle_get_storage(ch
))) {
1852 if (st
->cookiestore
) {
1853 pefree(st
->cookiestore
, 1);
1854 st
->cookiestore
= NULL
;
1856 st
->errorbuffer
[0] = '\0';
1859 curl_easy_setopt(ch
, CURLOPT_URL
, NULL
);
1860 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, NULL
);
1861 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1L);
1862 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 0L);
1863 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1864 #if PHP_HTTP_CURL_VERSION(7,19,1)
1865 curl_easy_setopt(ch
, CURLOPT_PROXYUSERNAME
, NULL
);
1866 curl_easy_setopt(ch
, CURLOPT_PROXYPASSWORD
, NULL
);
1867 curl_easy_setopt(ch
, CURLOPT_USERNAME
, NULL
);
1868 curl_easy_setopt(ch
, CURLOPT_PASSWORD
, NULL
);
1871 #if PHP_HTTP_CURL_VERSION(7,21,3)
1872 if (curl
->options
.resolve
) {
1873 curl_slist_free_all(curl
->options
.resolve
);
1874 curl
->options
.resolve
= NULL
;
1877 curl
->options
.retry
.count
= 0;
1878 curl
->options
.retry
.delay
= 0;
1879 curl
->options
.redirects
= 0;
1880 curl
->options
.encode_cookies
= 1;
1882 if (curl
->options
.headers
) {
1883 curl_slist_free_all(curl
->options
.headers
);
1884 curl
->options
.headers
= NULL
;
1886 if (curl
->options
.proxyheaders
) {
1887 curl_slist_free_all(curl
->options
.proxyheaders
);
1888 curl
->options
.proxyheaders
= NULL
;
1891 php_http_buffer_reset(&curl
->options
.cookies
);
1892 php_http_buffer_reset(&curl
->options
.ranges
);
1897 static php_http_client_curl_handler_t
*php_http_client_curl_handler_init(php_http_client_t
*h
, php_resource_factory_t
*rf
)
1900 php_http_client_curl_handler_t
*handler
;
1902 if (!(handle
= php_resource_factory_handle_ctor(rf
, NULL
))) {
1903 php_error_docref(NULL
, E_WARNING
, "Failed to initialize curl handle");
1907 handler
= ecalloc(1, sizeof(*handler
));
1909 handler
->client
= h
;
1910 handler
->handle
= handle
;
1911 handler
->response
.body
= php_http_message_body_init(NULL
, NULL
);
1912 php_http_buffer_init(&handler
->response
.headers
);
1913 php_http_buffer_init(&handler
->options
.cookies
);
1914 php_http_buffer_init(&handler
->options
.ranges
);
1915 zend_hash_init(&handler
->options
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
1918 curl_easy_setopt(handle
, CURLOPT_NOSIGNAL
, 1L);
1920 curl_easy_setopt(handle
, CURLOPT_HEADER
, 0L);
1921 curl_easy_setopt(handle
, CURLOPT_FILETIME
, 1L);
1922 curl_easy_setopt(handle
, CURLOPT_AUTOREFERER
, 1L);
1923 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
1924 curl_easy_setopt(handle
, CURLOPT_NOPROGRESS
, 0L);
1925 curl_easy_setopt(handle
, CURLOPT_HEADERFUNCTION
, php_http_curle_header_callback
);
1926 curl_easy_setopt(handle
, CURLOPT_WRITEFUNCTION
, php_http_curle_body_callback
);
1927 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, php_http_curle_raw_callback
);
1928 curl_easy_setopt(handle
, CURLOPT_READFUNCTION
, php_http_curle_read_callback
);
1929 curl_easy_setopt(handle
, CURLOPT_SEEKFUNCTION
, php_http_curle_seek_callback
);
1930 #if PHP_HTTP_CURL_VERSION(7,32,0)
1931 curl_easy_setopt(handle
, CURLOPT_XFERINFOFUNCTION
, php_http_curle_xferinfo_callback
);
1932 curl_easy_setopt(handle
, CURLOPT_XFERINFODATA
, handler
);
1934 curl_easy_setopt(handle
, CURLOPT_PROGRESSFUNCTION
, php_http_curle_progress_callback
);
1935 curl_easy_setopt(handle
, CURLOPT_PROGRESSDATA
, handler
);
1937 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, handler
);
1938 curl_easy_setopt(handle
, CURLOPT_WRITEDATA
, handler
);
1939 curl_easy_setopt(handle
, CURLOPT_HEADERDATA
, handler
);
1941 php_http_client_curl_handler_reset(handler
);
1947 static ZEND_RESULT_CODE
php_http_client_curl_handler_prepare(php_http_client_curl_handler_t
*curl
, php_http_client_enqueue_t
*enqueue
)
1950 php_http_message_t
*msg
= enqueue
->request
;
1951 php_http_curle_storage_t
*storage
= php_http_curle_get_storage(curl
->handle
);
1954 if (!PHP_HTTP_INFO(msg
).request
.url
) {
1955 php_error_docref(NULL
, E_WARNING
, "Cannot request empty URL");
1958 storage
->errorbuffer
[0] = '\0';
1960 pefree(storage
->url
, 1);
1962 php_http_url_to_string(PHP_HTTP_INFO(msg
).request
.url
, &storage
->url
, NULL
, 1);
1963 curl_easy_setopt(curl
->handle
, CURLOPT_URL
, storage
->url
);
1966 php_http_options_apply(&php_http_curle_options
, enqueue
->options
, curl
);
1968 /* request headers */
1969 php_http_message_update_headers(msg
);
1970 if (zend_hash_num_elements(&msg
->hdrs
)) {
1971 php_http_arrkey_t header_key
;
1973 zend_string
*header_str
;
1974 php_http_buffer_t header
;
1975 #if !PHP_HTTP_CURL_VERSION(7,23,0)
1976 zval
*ct
= zend_hash_str_find(&msg
->hdrs
, ZEND_STRL("Content-Length"));
1979 php_http_buffer_init(&header
);
1980 ZEND_HASH_FOREACH_KEY_VAL(&msg
->hdrs
, header_key
.h
, header_key
.key
, header_val
)
1982 if (header_key
.key
) {
1983 #if !PHP_HTTP_CURL_VERSION(7,23,0)
1984 /* avoid duplicate content-length header */
1985 if (ct
&& ct
== header_val
) {
1989 header_str
= zval_get_string(header_val
);
1990 php_http_buffer_appendf(&header
, "%s: %s", header_key
.key
->val
, header_str
->val
);
1991 php_http_buffer_fix(&header
);
1992 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, header
.data
);
1993 php_http_buffer_reset(&header
);
1994 zend_string_release(header_str
);
1997 ZEND_HASH_FOREACH_END();
1998 php_http_buffer_dtor(&header
);
2000 curl_easy_setopt(curl
->handle
, CURLOPT_HTTPHEADER
, curl
->options
.headers
);
2002 /* attach request body */
2003 if ((body_size
= php_http_message_body_size(msg
->body
))) {
2004 /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
2005 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
2006 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
2007 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
2008 * does not allow a request body.
2010 php_stream_rewind(php_http_message_body_stream(msg
->body
));
2011 curl_easy_setopt(curl
->handle
, CURLOPT_SEEKDATA
, msg
->body
);
2012 curl_easy_setopt(curl
->handle
, CURLOPT_READDATA
, msg
->body
);
2013 curl_easy_setopt(curl
->handle
, CURLOPT_INFILESIZE
, body_size
);
2014 curl_easy_setopt(curl
->handle
, CURLOPT_POSTFIELDSIZE
, body_size
);
2015 curl_easy_setopt(curl
->handle
, CURLOPT_POST
, 1L);
2017 curl_easy_setopt(curl
->handle
, CURLOPT_SEEKDATA
, NULL
);
2018 curl_easy_setopt(curl
->handle
, CURLOPT_READDATA
, NULL
);
2019 curl_easy_setopt(curl
->handle
, CURLOPT_INFILESIZE
, 0L);
2020 curl_easy_setopt(curl
->handle
, CURLOPT_POSTFIELDSIZE
, 0L);
2024 * Always use CUSTOMREQUEST, else curl won't send any request body for GET etc.
2025 * See e.g. bug #69313.
2027 * Here's what curl does:
2028 * - CURLOPT_HTTPGET: ignore request body
2029 * - CURLOPT_UPLOAD: set "Expect: 100-continue" header
2030 * - CURLOPT_POST: set "Content-Type: application/x-www-form-urlencoded" header
2031 * Now select the least bad.
2033 * See also https://tools.ietf.org/html/rfc7231#section-5.1.1
2035 if (PHP_HTTP_INFO(msg
).request
.method
) {
2036 switch(php_http_select_str(PHP_HTTP_INFO(msg
).request
.method
, 2, "HEAD", "PUT")) {
2038 curl_easy_setopt(curl
->handle
, CURLOPT_NOBODY
, 1L);
2041 curl_easy_setopt(curl
->handle
, CURLOPT_UPLOAD
, 1L);
2044 curl_easy_setopt(curl
->handle
, CURLOPT_CUSTOMREQUEST
, PHP_HTTP_INFO(msg
).request
.method
);
2047 php_error_docref(NULL
, E_WARNING
, "Cannot use empty request method");
2054 static void php_http_client_curl_handler_clear(php_http_client_curl_handler_t
*handler
)
2056 curl_easy_setopt(handler
->handle
, CURLOPT_NOPROGRESS
, 1L);
2057 #if PHP_HTTP_CURL_VERSION(7,32,0)
2058 curl_easy_setopt(handler
->handle
, CURLOPT_XFERINFOFUNCTION
, NULL
);
2060 curl_easy_setopt(handler
->handle
, CURLOPT_PROGRESSFUNCTION
, NULL
);
2062 curl_easy_setopt(handler
->handle
, CURLOPT_VERBOSE
, 0L);
2063 curl_easy_setopt(handler
->handle
, CURLOPT_DEBUGFUNCTION
, NULL
);
2066 static void php_http_client_curl_handler_dtor(php_http_client_curl_handler_t
*handler
)
2068 php_http_client_curl_handler_clear(handler
);
2070 php_resource_factory_handle_dtor(handler
->rf
, handler
->handle
);
2071 php_resource_factory_free(&handler
->rf
);
2073 php_http_message_body_free(&handler
->response
.body
);
2074 php_http_buffer_dtor(&handler
->response
.headers
);
2075 php_http_buffer_dtor(&handler
->options
.ranges
);
2076 php_http_buffer_dtor(&handler
->options
.cookies
);
2077 zend_hash_destroy(&handler
->options
.cache
);
2079 #if PHP_HTTP_CURL_VERSION(7,21,3)
2080 if (handler
->options
.resolve
) {
2081 curl_slist_free_all(handler
->options
.resolve
);
2082 handler
->options
.resolve
= NULL
;
2086 if (handler
->options
.headers
) {
2087 curl_slist_free_all(handler
->options
.headers
);
2088 handler
->options
.headers
= NULL
;
2091 if (handler
->options
.proxyheaders
) {
2092 curl_slist_free_all(handler
->options
.proxyheaders
);
2093 handler
->options
.proxyheaders
= NULL
;
2099 static php_http_client_t
*php_http_client_curl_init(php_http_client_t
*h
, void *handle
)
2101 php_http_client_curl_t
*curl
;
2103 if (!handle
&& !(handle
= php_resource_factory_handle_ctor(h
->rf
, NULL
))) {
2104 php_error_docref(NULL
, E_WARNING
, "Failed to initialize curl handle");
2108 curl
= ecalloc(1, sizeof(*curl
));
2109 curl
->handle
= handle
;
2110 curl
->unfinished
= 0;
2116 static void php_http_client_curl_dtor(php_http_client_t
*h
)
2118 php_http_client_curl_t
*curl
= h
->ctx
;
2120 #if PHP_HTTP_HAVE_EVENT
2121 if (curl
->timeout
) {
2122 if (event_initialized(curl
->timeout
) && event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
2123 event_del(curl
->timeout
);
2125 efree(curl
->timeout
);
2126 curl
->timeout
= NULL
;
2129 event_base_free(curl
->evbase
);
2130 curl
->evbase
= NULL
;
2133 curl
->unfinished
= 0;
2135 php_resource_factory_handle_dtor(h
->rf
, curl
->handle
);
2141 static void queue_dtor(php_http_client_enqueue_t
*e
)
2143 php_http_client_curl_handler_t
*handler
= e
->opaque
;
2145 if (handler
->queue
.dtor
) {
2146 e
->opaque
= handler
->queue
.opaque
;
2147 handler
->queue
.dtor(e
);
2149 php_http_client_curl_handler_dtor(handler
);
2152 static php_resource_factory_t
*create_rf(php_http_client_t
*h
, php_http_client_enqueue_t
*enqueue
)
2154 php_persistent_handle_factory_t
*pf
= NULL
;
2155 php_resource_factory_t
*rf
= NULL
;
2156 php_http_url_t
*url
= enqueue
->request
->http
.info
.request
.url
;
2158 if (!url
|| (!url
->host
&& !url
->path
)) {
2159 php_error_docref(NULL
, E_WARNING
, "Cannot request empty URL");
2163 /* only if the client itself is setup for persistence */
2164 if (php_resource_factory_is_persistent(h
->rf
)) {
2166 char *id_str
= NULL
;
2168 int port
= url
->port
? url
->port
: 80;
2171 if ((zport
= zend_hash_str_find(enqueue
->options
, ZEND_STRL("port")))) {
2172 zend_long lport
= zval_get_long(zport
);
2179 id_len
= spprintf(&id_str
, 0, "%s:%d", STR_PTR(url
->host
), port
);
2180 id
= php_http_cs2zs(id_str
, id_len
);
2181 pf
= php_persistent_handle_concede(NULL
, PHP_HTTP_G
->client
.curl
.driver
.request_name
, id
, NULL
, NULL
);
2182 zend_string_release(id
);
2186 rf
= php_persistent_handle_resource_factory_init(NULL
, pf
);
2188 rf
= php_resource_factory_init(NULL
, &php_http_curle_resource_factory_ops
, NULL
, NULL
);
2194 static ZEND_RESULT_CODE
php_http_client_curl_enqueue(php_http_client_t
*h
, php_http_client_enqueue_t
*enqueue
)
2197 php_http_client_curl_t
*curl
= h
->ctx
;
2198 php_http_client_curl_handler_t
*handler
;
2199 php_http_client_progress_state_t
*progress
;
2200 php_resource_factory_t
*rf
;
2202 rf
= create_rf(h
, enqueue
);
2207 handler
= php_http_client_curl_handler_init(h
, rf
);
2212 if (SUCCESS
!= php_http_client_curl_handler_prepare(handler
, enqueue
)) {
2213 php_http_client_curl_handler_dtor(handler
);
2217 handler
->queue
= *enqueue
;
2218 enqueue
->opaque
= handler
;
2219 enqueue
->dtor
= queue_dtor
;
2221 if (CURLM_OK
== (rs
= curl_multi_add_handle(curl
->handle
, handler
->handle
))) {
2222 zend_llist_add_element(&h
->requests
, enqueue
);
2225 if (h
->callback
.progress
.func
&& SUCCESS
== php_http_client_getopt(h
, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
, enqueue
->request
, &progress
)) {
2226 progress
->info
= "start";
2227 h
->callback
.progress
.func(h
->callback
.progress
.arg
, h
, &handler
->queue
, progress
);
2228 progress
->started
= 1;
2233 php_error_docref(NULL
, E_WARNING
, "Could not enqueue request: %s", curl_multi_strerror(rs
));
2238 static ZEND_RESULT_CODE
php_http_client_curl_dequeue(php_http_client_t
*h
, php_http_client_enqueue_t
*enqueue
)
2241 php_http_client_curl_t
*curl
= h
->ctx
;
2242 php_http_client_curl_handler_t
*handler
= enqueue
->opaque
;
2244 php_http_client_curl_handler_clear(handler
);
2245 if (CURLM_OK
== (rs
= curl_multi_remove_handle(curl
->handle
, handler
->handle
))) {
2246 zend_llist_del_element(&h
->requests
, handler
->handle
, (int (*)(void *, void *)) compare_queue
);
2249 php_error_docref(NULL
, E_WARNING
, "Could not dequeue request: %s", curl_multi_strerror(rs
));
2255 static void php_http_client_curl_reset(php_http_client_t
*h
)
2257 zend_llist_element
*next_el
, *this_el
;
2259 for (this_el
= h
->requests
.head
; this_el
; this_el
= next_el
) {
2260 next_el
= this_el
->next
;
2261 php_http_client_curl_dequeue(h
, (void *) this_el
->data
);
2265 static inline void php_http_client_curl_get_timeout(php_http_client_curl_t
*curl
, long max_tout
, struct timeval
*timeout
)
2267 if ((CURLM_OK
== curl_multi_timeout(curl
->handle
, &max_tout
)) && (max_tout
> 0)) {
2268 timeout
->tv_sec
= max_tout
/ 1000;
2269 timeout
->tv_usec
= (max_tout
% 1000) * 1000;
2271 timeout
->tv_sec
= 0;
2272 timeout
->tv_usec
= 1000;
2277 # define SELECT_ERROR SOCKET_ERROR
2279 # define SELECT_ERROR -1
2282 static ZEND_RESULT_CODE
php_http_client_curl_wait(php_http_client_t
*h
, struct timeval
*custom_timeout
)
2286 struct timeval timeout
;
2287 php_http_client_curl_t
*curl
= h
->ctx
;
2289 #if PHP_HTTP_HAVE_EVENT
2290 if (curl
->useevents
) {
2291 if (!event_initialized(curl
->timeout
)) {
2292 event_assign(curl
->timeout
, curl
->evbase
, CURL_SOCKET_TIMEOUT
, 0, php_http_curlm_timeout_callback
, h
);
2293 } else if (custom_timeout
&& timerisset(custom_timeout
)) {
2294 event_add(curl
->timeout
, custom_timeout
);
2295 } else if (!event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
2296 php_http_client_curl_get_timeout(curl
, 1000, &timeout
);
2297 event_add(curl
->timeout
, &timeout
);
2300 event_base_loop(curl
->evbase
, EVLOOP_ONCE
);
2310 if (CURLM_OK
== curl_multi_fdset(curl
->handle
, &R
, &W
, &E
, &MAX
)) {
2311 if (custom_timeout
&& timerisset(custom_timeout
)) {
2312 timeout
= *custom_timeout
;
2314 php_http_client_curl_get_timeout(curl
, 1000, &timeout
);
2318 php_http_sleep((double) timeout
.tv_sec
+ (double) (timeout
.tv_usec
/ PHP_HTTP_MCROSEC
));
2320 } else if (SELECT_ERROR
!= select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) {
2327 static int php_http_client_curl_once(php_http_client_t
*h
)
2329 php_http_client_curl_t
*curl
= h
->ctx
;
2331 #if PHP_HTTP_HAVE_EVENT
2332 if (curl
->useevents
) {
2333 event_base_loop(curl
->evbase
, EVLOOP_NONBLOCK
);
2336 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(curl
->handle
, &curl
->unfinished
));
2338 php_http_curlm_responsehandler(h
);
2340 return curl
->unfinished
;
2344 static ZEND_RESULT_CODE
php_http_client_curl_exec(php_http_client_t
*h
)
2346 #if PHP_HTTP_HAVE_EVENT
2347 php_http_client_curl_t
*curl
= h
->ctx
;
2349 if (curl
->useevents
) {
2350 php_http_curlm_timeout_callback(CURL_SOCKET_TIMEOUT
, /*EV_READ|EV_WRITE*/0, h
);
2352 int ev_rc
= event_base_dispatch(curl
->evbase
);
2355 fprintf(stderr
, "%c", "X.0"[ev_rc
+1]);
2359 php_error_docref(NULL
, E_ERROR
, "Error in event_base_dispatch()");
2362 } while (curl
->unfinished
&& !EG(exception
));
2366 while (php_http_client_curl_once(h
) && !EG(exception
)) {
2367 if (SUCCESS
!= php_http_client_curl_wait(h
, NULL
)) {
2369 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
2370 php_error_docref(NULL
, E_WARNING
, "WinSock error: %d", WSAGetLastError());
2372 php_error_docref(NULL
, E_WARNING
, "%s", strerror(errno
));
2382 static ZEND_RESULT_CODE
php_http_client_curl_setopt(php_http_client_t
*h
, php_http_client_setopt_opt_t opt
, void *arg
)
2384 php_http_client_curl_t
*curl
= h
->ctx
;
2387 case PHP_HTTP_CLIENT_OPT_CONFIGURATION
:
2388 return php_http_options_apply(&php_http_curlm_options
, (HashTable
*) arg
, h
);
2391 case PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING
:
2392 if (CURLM_OK
!= curl_multi_setopt(curl
->handle
, CURLMOPT_PIPELINING
, (long) *((zend_bool
*) arg
))) {
2397 case PHP_HTTP_CLIENT_OPT_USE_EVENTS
:
2398 #if PHP_HTTP_HAVE_EVENT
2399 return php_http_curlm_use_eventloop(h
, *(zend_bool
*) arg
);
2409 static int apply_available_options(zval
*pDest
, int num_args
, va_list args
, zend_hash_key
*hash_key
)
2411 php_http_option_t
*opt
= Z_PTR_P(pDest
);
2416 ht
= va_arg(args
, HashTable
*);
2418 if ((c
= zend_hash_num_elements(&opt
->suboptions
.options
))) {
2419 array_init_size(&entry
, c
);
2420 zend_hash_apply_with_arguments(&opt
->suboptions
.options
, apply_available_options
, 1, Z_ARRVAL(entry
));
2422 /* catch deliberate NULL options */
2423 if (Z_TYPE(opt
->defval
) == IS_STRING
&& !Z_STRVAL(opt
->defval
)) {
2426 ZVAL_ZVAL(&entry
, &opt
->defval
, 1, 0);
2430 if (hash_key
->key
) {
2431 zend_hash_update(ht
, hash_key
->key
, &entry
);
2433 zend_hash_index_update(ht
, hash_key
->h
, &entry
);
2436 return ZEND_HASH_APPLY_KEEP
;
2439 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
)
2441 php_http_client_enqueue_t
*enqueue
;
2444 case PHP_HTTP_CLIENT_OPT_PROGRESS_INFO
:
2445 if ((enqueue
= php_http_client_enqueued(h
, arg
, NULL
))) {
2446 php_http_client_curl_handler_t
*handler
= enqueue
->opaque
;
2448 *((php_http_client_progress_state_t
**) res
) = &handler
->progress
;
2453 case PHP_HTTP_CLIENT_OPT_TRANSFER_INFO
:
2454 if ((enqueue
= php_http_client_enqueued(h
, arg
, NULL
))) {
2455 php_http_client_curl_handler_t
*handler
= enqueue
->opaque
;
2457 php_http_curle_get_info(handler
->handle
, *(HashTable
**) res
);
2462 case PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS
:
2463 zend_hash_apply_with_arguments(&php_http_curle_options
.options
, apply_available_options
, 1, *(HashTable
**) res
);
2466 case PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION
:
2467 zend_hash_apply_with_arguments(&php_http_curlm_options
.options
, apply_available_options
, 1, *(HashTable
**) res
);
2477 static php_http_client_ops_t php_http_client_curl_ops
= {
2478 &php_http_curlm_resource_factory_ops
,
2479 php_http_client_curl_init
,
2481 php_http_client_curl_dtor
,
2482 php_http_client_curl_reset
,
2483 php_http_client_curl_exec
,
2484 php_http_client_curl_wait
,
2485 php_http_client_curl_once
,
2486 php_http_client_curl_enqueue
,
2487 php_http_client_curl_dequeue
,
2488 php_http_client_curl_setopt
,
2489 php_http_client_curl_getopt
2492 php_http_client_ops_t
*php_http_client_curl_get_ops(void)
2494 return &php_http_client_curl_ops
;
2497 PHP_MINIT_FUNCTION(http_client_curl
)
2499 php_http_options_t
*options
;
2501 PHP_HTTP_G
->client
.curl
.driver
.driver_name
= zend_string_init(ZEND_STRL("curl"), 1);
2502 PHP_HTTP_G
->client
.curl
.driver
.client_name
= zend_string_init(ZEND_STRL("http\\Client\\Curl"), 1);
2503 PHP_HTTP_G
->client
.curl
.driver
.request_name
= zend_string_init(ZEND_STRL("http\\Client\\Curl\\Request"), 1);
2504 PHP_HTTP_G
->client
.curl
.driver
.client_ops
= &php_http_client_curl_ops
;
2506 if (SUCCESS
!= php_http_client_driver_add(&PHP_HTTP_G
->client
.curl
.driver
)) {
2510 if (SUCCESS
!= php_persistent_handle_provide(PHP_HTTP_G
->client
.curl
.driver
.client_name
, &php_http_curlm_resource_factory_ops
, NULL
, NULL
)) {
2513 if (SUCCESS
!= php_persistent_handle_provide(PHP_HTTP_G
->client
.curl
.driver
.request_name
, &php_http_curle_resource_factory_ops
, NULL
, NULL
)) {
2517 if ((options
= php_http_options_init(&php_http_curle_options
, 1))) {
2518 options
->getter
= php_http_curle_get_option
;
2519 options
->setter
= php_http_curle_set_option
;
2521 php_http_curle_options_init(options
);
2523 if ((options
= php_http_options_init(&php_http_curlm_options
, 1))) {
2524 options
->getter
= php_http_option_get
;
2525 options
->setter
= php_http_curlm_set_option
;
2527 php_http_curlm_options_init(options
);
2531 * HTTP Protocol Version Constants
2533 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0
, CONST_CS
|CONST_PERSISTENT
);
2534 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1
, CONST_CS
|CONST_PERSISTENT
);
2535 #if PHP_HTTP_CURL_VERSION(7,33,0)
2536 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_2_0", CURL_HTTP_VERSION_2_0
, CONST_CS
|CONST_PERSISTENT
);
2538 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_ANY", CURL_HTTP_VERSION_NONE
, CONST_CS
|CONST_PERSISTENT
);
2541 * SSL Version Constants
2543 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1
, CONST_CS
|CONST_PERSISTENT
);
2544 #if PHP_HTTP_CURL_VERSION(7,34,0)
2545 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_0", CURL_SSLVERSION_TLSv1_0
, CONST_CS
|CONST_PERSISTENT
);
2546 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_1", CURL_SSLVERSION_TLSv1_1
, CONST_CS
|CONST_PERSISTENT
);
2547 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_2", CURL_SSLVERSION_TLSv1_2
, CONST_CS
|CONST_PERSISTENT
);
2549 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2
, CONST_CS
|CONST_PERSISTENT
);
2550 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3
, CONST_CS
|CONST_PERSISTENT
);
2551 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT
, CONST_CS
|CONST_PERSISTENT
);
2552 #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
2553 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "TLSAUTH_SRP", CURL_TLSAUTH_SRP
, CONST_CS
|CONST_PERSISTENT
);
2557 * DNS IPvX resolving
2559 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_V4", CURL_IPRESOLVE_V4
, CONST_CS
|CONST_PERSISTENT
);
2560 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_V6", CURL_IPRESOLVE_V6
, CONST_CS
|CONST_PERSISTENT
);
2561 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER
, CONST_CS
|CONST_PERSISTENT
);
2566 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_BASIC", CURLAUTH_BASIC
, CONST_CS
|CONST_PERSISTENT
);
2567 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_DIGEST", CURLAUTH_DIGEST
, CONST_CS
|CONST_PERSISTENT
);
2568 #if PHP_HTTP_CURL_VERSION(7,19,3)
2569 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_DIGEST_IE", CURLAUTH_DIGEST_IE
, CONST_CS
|CONST_PERSISTENT
);
2571 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_NTLM", CURLAUTH_NTLM
, CONST_CS
|CONST_PERSISTENT
);
2572 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE
, CONST_CS
|CONST_PERSISTENT
);
2573 #if PHP_HTTP_CURL_VERSION(7,38,0)
2574 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_SPNEGO", CURLAUTH_NEGOTIATE
, CONST_CS
|CONST_PERSISTENT
);
2576 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_ANY", CURLAUTH_ANY
, CONST_CS
|CONST_PERSISTENT
);
2579 * Proxy Type Constants
2581 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS4", CURLPROXY_SOCKS4
, CONST_CS
|CONST_PERSISTENT
);
2582 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS4A", CURLPROXY_SOCKS5
, CONST_CS
|CONST_PERSISTENT
);
2583 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS5_HOSTNAME", CURLPROXY_SOCKS5
, CONST_CS
|CONST_PERSISTENT
);
2584 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS5", CURLPROXY_SOCKS5
, CONST_CS
|CONST_PERSISTENT
);
2585 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_HTTP", CURLPROXY_HTTP
, CONST_CS
|CONST_PERSISTENT
);
2586 #if PHP_HTTP_CURL_VERSION(7,19,4)
2587 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_HTTP_1_0", CURLPROXY_HTTP_1_0
, CONST_CS
|CONST_PERSISTENT
);
2591 * Post Redirection Constants
2593 #if PHP_HTTP_CURL_VERSION(7,19,1)
2594 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_301", CURL_REDIR_POST_301
, CONST_CS
|CONST_PERSISTENT
);
2595 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_302", CURL_REDIR_POST_302
, CONST_CS
|CONST_PERSISTENT
);
2596 #if PHP_HTTP_CURL_VERSION(7,26,0)
2597 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_303", CURL_REDIR_POST_303
, CONST_CS
|CONST_PERSISTENT
);
2599 REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_ALL", CURL_REDIR_POST_ALL
, CONST_CS
|CONST_PERSISTENT
);
2605 PHP_MSHUTDOWN_FUNCTION(http_client_curl
)
2607 php_persistent_handle_cleanup(PHP_HTTP_G
->client
.curl
.driver
.client_name
, NULL
);
2608 php_persistent_handle_cleanup(PHP_HTTP_G
->client
.curl
.driver
.request_name
, NULL
);
2609 zend_string_release(PHP_HTTP_G
->client
.curl
.driver
.client_name
);
2610 zend_string_release(PHP_HTTP_G
->client
.curl
.driver
.request_name
);
2611 zend_string_release(PHP_HTTP_G
->client
.curl
.driver
.driver_name
);
2613 php_http_options_dtor(&php_http_curle_options
);
2614 php_http_options_dtor(&php_http_curlm_options
);
2619 #endif /* PHP_HTTP_HAVE_CURL */
2626 * vim600: noet sw=4 ts=4 fdm=marker
2627 * vim<600: noet sw=4 ts=4