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-2011, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
13 #include "php_http_api.h"
15 #if PHP_HTTP_HAVE_CURL
17 #include <curl/curl.h>
18 #define PHP_HTTP_CURL_VERSION(x, y, z) (LIBCURL_VERSION_NUM >= (((x)<<16) + ((y)<<8) + (z)))
20 #if PHP_HTTP_HAVE_EVENT
24 typedef struct php_http_curl_request
{
30 struct curl_slist
*headers
;
31 php_http_buffer_t cookies
;
42 php_http_request_progress_t progress
;
44 } php_http_curl_request_t
;
46 typedef struct php_http_curl_request_storage
{
49 char errorbuffer
[0x100];
50 } php_http_curl_request_storage_t
;
52 typedef struct php_http_curl_request_pool
{
55 int unfinished
; /* int because of curl_multi_perform() */
57 #if PHP_HTTP_HAVE_EVENT
58 struct event
*timeout
;
62 } php_http_curl_request_pool_t
;
64 typedef struct php_http_curl_request_datashare
{
66 } php_http_curl_request_datashare_t
;
68 #define PHP_HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \
71 PHP_HTTP_CURL_OPT_STRING_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION, obdc); \
73 #define PHP_HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
74 if (!strcasecmp(key.str, keyname)) { \
75 zval *copy = cache_option(&curl->options.cache, keyname, strlen(keyname)+1, 0, php_http_ztyp(IS_STRING, *param)); \
77 if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(copy) TSRMLS_CC)) { \
81 curl_easy_setopt(ch, optname, Z_STRVAL_P(copy)); \
82 zval_ptr_dtor(©); \
85 #define PHP_HTTP_CURL_OPT_LONG(OPTION, ldiff) \
88 PHP_HTTP_CURL_OPT_LONG_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION); \
90 #define PHP_HTTP_CURL_OPT_LONG_EX(keyname, optname) \
91 if (!strcasecmp(key.str, keyname)) { \
92 zval *copy = php_http_ztyp(IS_LONG, *param); \
93 curl_easy_setopt(ch, optname, Z_LVAL_P(copy)); \
94 zval_ptr_dtor(©); \
98 static inline php_http_curl_request_storage_t
*get_storage(CURL
*ch
) {
99 php_http_curl_request_storage_t
*st
= NULL
;
101 curl_easy_getinfo(ch
, CURLINFO_PRIVATE
, &st
);
104 st
= pecalloc(1, sizeof(*st
), 1);
105 curl_easy_setopt(ch
, CURLOPT_PRIVATE
, st
);
106 curl_easy_setopt(ch
, CURLOPT_ERRORBUFFER
, st
->errorbuffer
);
112 /* resource_factory ops */
114 static void *php_http_curl_ctor(void *opaque TSRMLS_DC
)
118 if ((ch
= curl_easy_init())) {
125 static void *php_http_curl_copy(void *opaque
, void *handle TSRMLS_DC
)
129 if ((ch
= curl_easy_duphandle(handle
))) {
137 static void php_http_curl_dtor(void *opaque
, void *handle TSRMLS_DC
)
139 php_http_curl_request_storage_t
*st
= get_storage(handle
);
141 curl_easy_cleanup(handle
);
147 if (st
->cookiestore
) {
148 pefree(st
->cookiestore
, 1);
154 static void *php_http_curlm_ctor(void *opaque TSRMLS_DC
)
156 return curl_multi_init();
159 static void php_http_curlm_dtor(void *opaque
, void *handle TSRMLS_DC
)
161 curl_multi_cleanup(handle
);
164 static void *php_http_curlsh_ctor(void *opaque TSRMLS_DC
)
166 return curl_share_init();
169 static void php_http_curlsh_dtor(void *opaque
, void *handle TSRMLS_DC
)
171 curl_share_cleanup(handle
);
176 static size_t php_http_curl_read_callback(void *data
, size_t len
, size_t n
, void *ctx
)
178 php_http_message_body_t
*body
= ctx
;
181 TSRMLS_FETCH_FROM_CTX(body
->ts
);
182 return php_stream_read(php_http_message_body_stream(body
), data
, len
* n
);
187 static int php_http_curl_progress_callback(void *ctx
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
189 php_http_request_t
*h
= ctx
;
190 php_http_curl_request_t
*curl
= h
->ctx
;
191 TSRMLS_FETCH_FROM_CTX(h
->ts
);
193 curl
->progress
.state
.dl
.total
= dltotal
;
194 curl
->progress
.state
.dl
.now
= dlnow
;
195 curl
->progress
.state
.ul
.total
= ultotal
;
196 curl
->progress
.state
.ul
.now
= ulnow
;
198 php_http_request_progress_notify(&curl
->progress TSRMLS_CC
);
203 static curlioerr
php_http_curl_ioctl_callback(CURL
*ch
, curliocmd cmd
, void *ctx
)
205 php_http_message_body_t
*body
= ctx
;
207 if (cmd
!= CURLIOCMD_RESTARTREAD
) {
208 return CURLIOE_UNKNOWNCMD
;
212 TSRMLS_FETCH_FROM_CTX(body
->ts
);
214 if (SUCCESS
== php_stream_rewind(php_http_message_body_stream(body
))) {
219 return CURLIOE_FAILRESTART
;
222 static int php_http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
224 php_http_request_t
*h
= ctx
;
225 php_http_curl_request_t
*curl
= h
->ctx
;
227 TSRMLS_FETCH_FROM_CTX(h
->ts
);
232 if (php_memnstr(data
, ZEND_STRL("About to connect"), data
+ length
)) {
233 curl
->progress
.state
.info
= "resolve";
234 } else if (php_memnstr(data
, ZEND_STRL("Trying"), data
+ length
)) {
235 curl
->progress
.state
.info
= "connect";
236 } else if (php_memnstr(data
, ZEND_STRL("Connected"), data
+ length
)) {
237 curl
->progress
.state
.info
= "connected";
238 } else if (php_memnstr(data
, ZEND_STRL("left intact"), data
+ length
)) {
239 curl
->progress
.state
.info
= "not disconnected";
240 } else if (php_memnstr(data
, ZEND_STRL("closed"), data
+ length
)) {
241 curl
->progress
.state
.info
= "disconnected";
243 php_http_request_progress_notify(&curl
->progress TSRMLS_CC
);
245 case CURLINFO_HEADER_OUT
:
246 case CURLINFO_DATA_OUT
:
247 case CURLINFO_SSL_DATA_OUT
:
248 curl
->progress
.state
.info
= "send";
250 case CURLINFO_HEADER_IN
:
251 case CURLINFO_DATA_IN
:
252 case CURLINFO_SSL_DATA_IN
:
253 curl
->progress
.state
.info
= "receive";
260 case CURLINFO_HEADER_IN
:
261 case CURLINFO_DATA_IN
:
262 case CURLINFO_HEADER_OUT
:
263 case CURLINFO_DATA_OUT
:
264 php_http_buffer_append(h
->buffer
, data
, length
);
266 if (curl
->options
.redirects
) {
267 flags
|= PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS
;
270 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
== php_http_message_parser_parse(h
->parser
, h
->buffer
, flags
, &h
->message
)) {
280 _dpf(type
, data
, length
);
286 static int php_http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
)
291 static STATUS
php_http_curl_request_prepare(php_http_request_t
*h
, php_http_request_method_t meth
, const char *url
, php_http_message_body_t
*body
)
293 php_http_curl_request_t
*curl
= h
->ctx
;
294 php_http_curl_request_storage_t
*storage
= get_storage(curl
->handle
);
295 TSRMLS_FETCH_FROM_CTX(h
->ts
);
297 storage
->errorbuffer
[0] = '\0';
299 pefree(storage
->url
, 1);
301 storage
->url
= pestrdup(url
, 1);
302 curl_easy_setopt(curl
->handle
, CURLOPT_URL
, storage
->url
);
307 curl_easy_setopt(curl
->handle
, CURLOPT_HTTPGET
, 1L);
311 curl_easy_setopt(curl
->handle
, CURLOPT_NOBODY
, 1L);
315 curl_easy_setopt(curl
->handle
, CURLOPT_POST
, 1L);
319 curl_easy_setopt(curl
->handle
, CURLOPT_UPLOAD
, 1L);
323 const char *name
= php_http_request_method_name(meth TSRMLS_CC
);
326 curl_easy_setopt(curl
->handle
, CURLOPT_CUSTOMREQUEST
, name
);
328 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_METHOD
, "Unsupported request method: %d (%s)", meth
, url
);
335 /* attach request body */
337 /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
338 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
339 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
340 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
341 * does not allow a request body.
345 size_t body_size
= php_http_message_body_size(body
);
347 curl_easy_setopt(curl
->handle
, CURLOPT_IOCTLDATA
, body
);
348 curl_easy_setopt(curl
->handle
, CURLOPT_READDATA
, body
);
349 curl_easy_setopt(curl
->handle
, CURLOPT_INFILESIZE
, body_size
);
350 curl_easy_setopt(curl
->handle
, CURLOPT_POSTFIELDSIZE
, body_size
);
359 static void php_http_curl_request_pool_responsehandler(php_http_request_pool_t
*pool
)
363 php_http_curl_request_pool_t
*curl
= pool
->ctx
;
364 TSRMLS_FETCH_FROM_CTX(pool
->ts
);
367 CURLMsg
*msg
= curl_multi_info_read(curl
->handle
, &remaining
);
369 if (msg
&& CURLMSG_DONE
== msg
->msg
) {
372 if (CURLE_OK
!= msg
->data
.result
) {
373 php_http_curl_request_storage_t
*st
= get_storage(msg
->easy_handle
);
374 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST
, "%s; %s (%s)", curl_easy_strerror(msg
->data
.result
), STR_PTR(st
->errorbuffer
), STR_PTR(st
->url
));
377 php_http_request_pool_requests(pool
, &requests
, NULL
);
378 for (request
= requests
; *request
; ++request
) {
379 php_http_request_object_t
*obj
= zend_object_store_get_object(*request TSRMLS_CC
);
381 if (msg
->easy_handle
== ((php_http_curl_request_t
*) (obj
->request
->ctx
))->handle
) {
382 Z_ADDREF_PP(request
);
383 zend_llist_add_element(&pool
->requests
.finished
, request
);
384 php_http_request_object_responsehandler(obj
, *request TSRMLS_CC
);
387 zval_ptr_dtor(request
);
395 #if PHP_HTTP_HAVE_EVENT
397 typedef struct php_http_request_pool_event
{
399 php_http_request_pool_t
*pool
;
400 } php_http_request_pool_event_t
;
402 static inline int etoca(short action
) {
403 switch (action
& (EV_READ
|EV_WRITE
)) {
405 return CURL_CSELECT_IN
;
408 return CURL_CSELECT_OUT
;
410 case EV_READ
|EV_WRITE
:
411 return CURL_CSELECT_IN
|CURL_CSELECT_OUT
;
418 static void php_http_curl_request_pool_timeout_callback(int socket
, short action
, void *event_data
)
420 php_http_request_pool_t
*pool
= event_data
;
421 php_http_curl_request_pool_t
*curl
= pool
->ctx
;
424 fprintf(stderr
, "T");
426 if (curl
->useevents
) {
428 TSRMLS_FETCH_FROM_CTX(pool
->ts
);
430 while (CURLM_CALL_MULTI_PERFORM
== (rc
= curl_multi_socket_action(curl
->handle
, socket
, etoca(action
), &curl
->unfinished
)));
432 if (CURLM_OK
!= rc
) {
433 php_http_error(HE_WARNING
, PHP_HTTP_E_SOCKET
, "%s", curl_multi_strerror(rc
));
436 php_http_curl_request_pool_responsehandler(pool
);
440 static void php_http_curl_request_pool_event_callback(int socket
, short action
, void *event_data
)
442 php_http_request_pool_t
*pool
= event_data
;
443 php_http_curl_request_pool_t
*curl
= pool
->ctx
;
446 fprintf(stderr
, "E");
448 if (curl
->useevents
) {
449 CURLMcode rc
= CURLE_OK
;
450 TSRMLS_FETCH_FROM_CTX(pool
->ts
);
452 while (CURLM_CALL_MULTI_PERFORM
== (rc
= curl_multi_socket_action(curl
->handle
, socket
, etoca(action
), &curl
->unfinished
)));
454 if (CURLM_OK
!= rc
) {
455 php_http_error(HE_WARNING
, PHP_HTTP_E_SOCKET
, "%s", curl_multi_strerror(rc
));
458 php_http_curl_request_pool_responsehandler(pool
);
460 /* remove timeout if there are no transfers left */
461 if (!curl
->unfinished
&& event_initialized(curl
->timeout
) && event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
462 event_del(curl
->timeout
);
467 static int php_http_curl_request_pool_socket_callback(CURL
*easy
, curl_socket_t sock
, int action
, void *socket_data
, void *assign_data
)
469 php_http_request_pool_t
*pool
= socket_data
;
470 php_http_curl_request_pool_t
*curl
= pool
->ctx
;
473 fprintf(stderr
, "S");
475 if (curl
->useevents
) {
476 int events
= EV_PERSIST
;
477 php_http_request_pool_event_t
*ev
= assign_data
;
478 TSRMLS_FETCH_FROM_CTX(pool
->ts
);
481 ev
= ecalloc(1, sizeof(php_http_request_pool_event_t
));
483 curl_multi_assign(curl
->handle
, sock
, ev
);
484 event_base_set(PHP_HTTP_G
->curl
.event_base
, &ev
->evnt
);
486 event_del(&ev
->evnt
);
496 case CURL_POLL_INOUT
:
497 events
|= EV_READ
|EV_WRITE
;
500 case CURL_POLL_REMOVE
:
507 php_http_error(HE_WARNING
, PHP_HTTP_E_SOCKET
, "Unknown socket action %d", action
);
511 event_set(&ev
->evnt
, sock
, events
, php_http_curl_request_pool_event_callback
, pool
);
512 event_add(&ev
->evnt
, NULL
);
518 static void php_http_curl_request_pool_timer_callback(CURLM
*multi
, long timeout_ms
, void *timer_data
)
520 php_http_request_pool_t
*pool
= timer_data
;
521 php_http_curl_request_pool_t
*curl
= pool
->ctx
;
524 fprintf(stderr
, "%ld", timeout_ms
);
526 if (curl
->useevents
) {
528 if (timeout_ms
< 0) {
529 php_http_curl_request_pool_timeout_callback(CURL_SOCKET_TIMEOUT
, CURL_CSELECT_IN
|CURL_CSELECT_OUT
, pool
);
530 } else if (timeout_ms
> 0 || !event_initialized(curl
->timeout
) || !event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
531 struct timeval timeout
;
532 TSRMLS_FETCH_FROM_CTX(pool
->ts
);
534 if (!event_initialized(curl
->timeout
)) {
535 event_set(curl
->timeout
, -1, 0, php_http_curl_request_pool_timeout_callback
, pool
);
536 event_base_set(PHP_HTTP_G
->curl
.event_base
, curl
->timeout
);
537 } else if (event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
538 event_del(curl
->timeout
);
541 timeout
.tv_sec
= timeout_ms
/ 1000;
542 timeout
.tv_usec
= (timeout_ms
% 1000) * 1000;
544 event_add(curl
->timeout
, &timeout
);
549 #endif /* HAVE_EVENT */
552 static inline zval
*cache_option(HashTable
*cache
, char *key
, size_t keylen
, ulong h
, zval
*opt
)
557 zend_hash_quick_update(cache
, key
, keylen
, h
, &opt
, sizeof(zval
*), NULL
);
559 zend_hash_update(cache
, key
, keylen
, &opt
, sizeof(zval
*), NULL
);
565 static inline zval
*get_option(HashTable
*cache
, HashTable
*options
, char *key
, size_t keylen
, int type
)
569 ulong h
= zend_hash_func(key
, keylen
);
571 if (SUCCESS
== zend_hash_quick_find(options
, key
, keylen
, h
, (void *) &zoption
)) {
572 zval
*option
= php_http_ztyp(type
, *zoption
);
575 zval
*cached
= cache_option(cache
, key
, keylen
, h
, option
);
577 zval_ptr_dtor(&option
);
587 static STATUS
set_options(php_http_request_t
*h
, HashTable
*options
)
591 php_http_curl_request_t
*curl
= h
->ctx
;
592 CURL
*ch
= curl
->handle
;
593 TSRMLS_FETCH_FROM_CTX(h
->ts
);
596 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxyhost"), IS_STRING
))) {
597 curl_easy_setopt(ch
, CURLOPT_PROXY
, Z_STRVAL_P(zoption
));
599 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxytype"), IS_LONG
))) {
600 curl_easy_setopt(ch
, CURLOPT_PROXYTYPE
, Z_LVAL_P(zoption
));
603 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxyport"), IS_LONG
))) {
604 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
607 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxyauth"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
608 curl_easy_setopt(ch
, CURLOPT_PROXYUSERPWD
, Z_STRVAL_P(zoption
));
611 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxyauthtype"), IS_LONG
))) {
612 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
615 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxytunnel"), IS_BOOL
)) && Z_BVAL_P(zoption
)) {
616 curl_easy_setopt(ch
, CURLOPT_HTTPPROXYTUNNEL
, 1L);
619 #if PHP_HTTP_CURL_VERSION(7,19,4)
620 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("noproxy"), IS_STRING
))) {
621 curl_easy_setopt(ch
, CURLOPT_NOPROXY
, Z_STRVAL_P(zoption
));
626 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("dns_cache_timeout"), IS_LONG
))) {
627 curl_easy_setopt(ch
, CURLOPT_DNS_CACHE_TIMEOUT
, Z_LVAL_P(zoption
));
629 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("ipresolve"), IS_LONG
)) && Z_LVAL_P(zoption
)) {
630 curl_easy_setopt(ch
, CURLOPT_IPRESOLVE
, Z_LVAL_P(zoption
));
634 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("low_speed_limit"), IS_LONG
))) {
635 curl_easy_setopt(ch
, CURLOPT_LOW_SPEED_LIMIT
, Z_LVAL_P(zoption
));
637 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("low_speed_time"), IS_LONG
))) {
638 curl_easy_setopt(ch
, CURLOPT_LOW_SPEED_TIME
, Z_LVAL_P(zoption
));
641 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_send_speed"), IS_LONG))) {
642 curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
644 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_recv_speed"), IS_LONG))) {
645 curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
649 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("maxconnects"), IS_LONG))) {
650 curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption));
652 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("fresh_connect"), IS_BOOL
)) && Z_BVAL_P(zoption
)) {
653 curl_easy_setopt(ch
, CURLOPT_FRESH_CONNECT
, 1L);
655 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("forbid_reuse"), IS_BOOL
)) && Z_BVAL_P(zoption
)) {
656 curl_easy_setopt(ch
, CURLOPT_FORBID_REUSE
, 1L);
659 /* outgoing interface */
660 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("interface"), IS_STRING
))) {
661 curl_easy_setopt(ch
, CURLOPT_INTERFACE
, Z_STRVAL_P(zoption
));
663 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("portrange"), IS_ARRAY
))) {
666 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
667 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void *) &prs
)) {
668 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
669 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void *) &pre
)) {
670 zval
*prs_cpy
= php_http_ztyp(IS_LONG
, *prs
);
671 zval
*pre_cpy
= php_http_ztyp(IS_LONG
, *pre
);
673 if (Z_LVAL_P(prs_cpy
) && Z_LVAL_P(pre_cpy
)) {
674 curl_easy_setopt(ch
, CURLOPT_LOCALPORT
, MIN(Z_LVAL_P(prs_cpy
), Z_LVAL_P(pre_cpy
)));
675 curl_easy_setopt(ch
, CURLOPT_LOCALPORTRANGE
, labs(Z_LVAL_P(prs_cpy
)-Z_LVAL_P(pre_cpy
))+1L);
677 zval_ptr_dtor(&prs_cpy
);
678 zval_ptr_dtor(&pre_cpy
);
685 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("port"), IS_LONG
))) {
686 curl_easy_setopt(ch
, CURLOPT_PORT
, Z_LVAL_P(zoption
));
689 /* RFC4007 zone_id */
690 #if PHP_HTTP_CURL_VERSION(7,19,0)
691 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("address_scope"), IS_LONG
))) {
692 curl_easy_setopt(ch
, CURLOPT_ADDRESS_SCOPE
, Z_LVAL_P(zoption
));
697 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("httpauth"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
698 curl_easy_setopt(ch
, CURLOPT_USERPWD
, Z_STRVAL_P(zoption
));
700 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("httpauthtype"), IS_LONG
))) {
701 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
704 /* redirects, defaults to 0 */
705 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("redirect"), IS_LONG
))) {
706 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1L : 0L);
707 curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, curl
->options
.redirects
= Z_LVAL_P(zoption
));
708 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("unrestrictedauth"), IS_BOOL
))) {
709 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
711 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("postredir"), IS_BOOL
))) {
712 #if PHP_HTTP_CURL_VERSION(7,19,1)
713 curl_easy_setopt(ch
, CURLOPT_POSTREDIR
, Z_BVAL_P(zoption
) ? 1L : 0L);
715 curl_easy_setopt(ch
, CURLOPT_POST301
, Z_BVAL_P(zoption
) ? 1L : 0L);
719 curl
->options
.redirects
= 0;
722 /* retries, defaults to 0 */
723 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("retrycount"), IS_LONG
))) {
724 curl
->options
.retry
.count
= Z_LVAL_P(zoption
);
725 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("retrydelay"), IS_DOUBLE
))) {
726 curl
->options
.retry
.delay
= Z_DVAL_P(zoption
);
728 curl
->options
.retry
.delay
= 0;
731 curl
->options
.retry
.count
= 0;
735 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("referer"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
736 curl_easy_setopt(ch
, CURLOPT_REFERER
, Z_STRVAL_P(zoption
));
739 /* useragent, default "PECL::HTTP/version (PHP/version)" */
740 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("useragent"), IS_STRING
))) {
741 /* allow to send no user agent, not even default one */
742 if (Z_STRLEN_P(zoption
)) {
743 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, Z_STRVAL_P(zoption
));
745 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, NULL
);
750 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("resume"), IS_LONG
)) && (Z_LVAL_P(zoption
) > 0)) {
752 curl_easy_setopt(ch
, CURLOPT_RESUME_FROM
, Z_LVAL_P(zoption
));
754 /* or range of kind array(array(0,499), array(100,1499)) */
755 else if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("range"), IS_ARRAY
)) && zend_hash_num_elements(Z_ARRVAL_P(zoption
))) {
756 HashPosition pos1
, pos2
;
757 zval
**rr
, **rb
, **re
;
758 php_http_buffer_t rs
;
760 php_http_buffer_init(&rs
);
761 FOREACH_VAL(pos1
, zoption
, rr
) {
762 if (Z_TYPE_PP(rr
) == IS_ARRAY
) {
763 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr
), &pos2
);
764 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr
), (void *) &rb
, &pos2
)) {
765 zend_hash_move_forward_ex(Z_ARRVAL_PP(rr
), &pos2
);
766 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr
), (void *) &re
, &pos2
)) {
767 if ( ((Z_TYPE_PP(rb
) == IS_LONG
) || ((Z_TYPE_PP(rb
) == IS_STRING
) && is_numeric_string(Z_STRVAL_PP(rb
), Z_STRLEN_PP(rb
), NULL
, NULL
, 1))) &&
768 ((Z_TYPE_PP(re
) == IS_LONG
) || ((Z_TYPE_PP(re
) == IS_STRING
) && is_numeric_string(Z_STRVAL_PP(re
), Z_STRLEN_PP(re
), NULL
, NULL
, 1)))) {
769 zval
*rbl
= php_http_ztyp(IS_LONG
, *rb
);
770 zval
*rel
= php_http_ztyp(IS_LONG
, *re
);
772 if ((Z_LVAL_P(rbl
) >= 0) && (Z_LVAL_P(rel
) >= 0)) {
773 php_http_buffer_appendf(&rs
, "%ld-%ld,", Z_LVAL_P(rbl
), Z_LVAL_P(rel
));
783 if (PHP_HTTP_BUFFER_LEN(&rs
)) {
786 /* ditch last comma */
787 PHP_HTTP_BUFFER_VAL(&rs
)[PHP_HTTP_BUFFER_LEN(&rs
)-- -1] = '\0';
789 MAKE_STD_ZVAL(cached_range
);
790 ZVAL_STRINGL(cached_range
, PHP_HTTP_BUFFER_VAL(&rs
), PHP_HTTP_BUFFER_LEN(&rs
), 0);
791 curl_easy_setopt(ch
, CURLOPT_RANGE
, Z_STRVAL_P(cache_option(&curl
->options
.cache
, ZEND_STRS("range"), 0, cached_range
)));
792 zval_ptr_dtor(&cached_range
);
796 /* additional headers, array('name' => 'value') */
797 if (curl
->options
.headers
) {
798 curl_slist_free_all(curl
->options
.headers
);
799 curl
->options
.headers
= NULL
;
801 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("headers"), IS_ARRAY
))) {
802 php_http_array_hashkey_t header_key
= php_http_array_hashkey_init(0);
805 php_http_buffer_t header
;
807 php_http_buffer_init(&header
);
808 FOREACH_KEYVAL(pos
, zoption
, header_key
, header_val
) {
809 if (header_key
.type
== HASH_KEY_IS_STRING
) {
810 zval
*header_cpy
= php_http_ztyp(IS_STRING
, *header_val
);
812 if (!strcasecmp(header_key
.str
, "range")) {
816 php_http_buffer_appendf(&header
, "%s: %s", header_key
.str
, Z_STRVAL_P(header_cpy
));
817 php_http_buffer_fix(&header
);
818 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, PHP_HTTP_BUFFER_VAL(&header
));
819 php_http_buffer_reset(&header
);
821 zval_ptr_dtor(&header_cpy
);
824 php_http_buffer_dtor(&header
);
827 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("etag"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
828 zend_bool is_quoted
= !((Z_STRVAL_P(zoption
)[0] != '"') || (Z_STRVAL_P(zoption
)[Z_STRLEN_P(zoption
)-1] != '"'));
829 php_http_buffer_t header
;
831 php_http_buffer_init(&header
);
832 php_http_buffer_appendf(&header
, is_quoted
?"%s: %s":"%s: \"%s\"", range_req
?"If-Match":"If-None-Match", Z_STRVAL_P(zoption
));
833 php_http_buffer_fix(&header
);
834 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, PHP_HTTP_BUFFER_VAL(&header
));
835 php_http_buffer_dtor(&header
);
838 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("compress"), IS_BOOL
)) && Z_LVAL_P(zoption
)) {
839 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
841 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, curl
->options
.headers
);
844 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("lastmodified"), IS_LONG
))) {
845 if (Z_LVAL_P(zoption
)) {
846 if (Z_LVAL_P(zoption
) > 0) {
847 curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, Z_LVAL_P(zoption
));
849 curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, (long) PHP_HTTP_G
->env
.request
.time
+ Z_LVAL_P(zoption
));
851 curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, (long) (range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
));
853 curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, CURL_TIMECOND_NONE
);
857 /* cookies, array('name' => 'value') */
858 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("cookies"), IS_ARRAY
))) {
859 php_http_buffer_dtor(&curl
->options
.cookies
);
860 if (zend_hash_num_elements(Z_ARRVAL_P(zoption
))) {
861 zval
*urlenc_cookies
= NULL
;
862 /* check whether cookies should not be urlencoded; default is to urlencode them */
863 if ((!(urlenc_cookies
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("encodecookies"), IS_BOOL
))) || Z_BVAL_P(urlenc_cookies
)) {
864 if (SUCCESS
== php_http_url_encode_hash_ex(HASH_OF(zoption
), &curl
->options
.cookies
, ZEND_STRS(";"), ZEND_STRS("="), NULL
, 0 TSRMLS_CC
)) {
865 php_http_buffer_fix(&curl
->options
.cookies
);
866 curl_easy_setopt(ch
, CURLOPT_COOKIE
, curl
->options
.cookies
.data
);
870 php_http_array_hashkey_t cookie_key
= php_http_array_hashkey_init(0);
873 FOREACH_KEYVAL(pos
, zoption
, cookie_key
, cookie_val
) {
874 if (cookie_key
.type
== HASH_KEY_IS_STRING
) {
875 zval
*val
= php_http_ztyp(IS_STRING
, *cookie_val
);
876 php_http_buffer_appendf(&curl
->options
.cookies
, "%s=%s; ", cookie_key
.str
, Z_STRVAL_P(val
));
881 php_http_buffer_fix(&curl
->options
.cookies
);
882 if (PHP_HTTP_BUFFER_LEN(&curl
->options
.cookies
)) {
883 curl_easy_setopt(ch
, CURLOPT_COOKIE
, PHP_HTTP_BUFFER_VAL(&curl
->options
.cookies
));
889 /* don't load session cookies from cookiestore */
890 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("cookiesession"), IS_BOOL
)) && Z_BVAL_P(zoption
)) {
891 curl_easy_setopt(ch
, CURLOPT_COOKIESESSION
, 1L);
894 /* cookiestore, read initial cookies from that file and store cookies back into that file */
895 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("cookiestore"), IS_STRING
))) {
896 php_http_curl_request_storage_t
*storage
= get_storage(curl
->handle
);
898 if (Z_STRLEN_P(zoption
)) {
899 if (SUCCESS
!= php_check_open_basedir(Z_STRVAL_P(zoption
) TSRMLS_CC
)) {
903 if (storage
->cookiestore
) {
904 pefree(storage
->cookiestore
, 1);
906 storage
->cookiestore
= pestrndup(Z_STRVAL_P(zoption
), Z_STRLEN_P(zoption
), 1);
907 curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, storage
->cookiestore
);
908 curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, storage
->cookiestore
);
912 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("maxfilesize"), IS_LONG
))) {
913 curl_easy_setopt(ch
, CURLOPT_MAXFILESIZE
, Z_LVAL_P(zoption
));
917 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("protocol"), IS_LONG
))) {
918 curl_easy_setopt(ch
, CURLOPT_HTTP_VERSION
, Z_LVAL_P(zoption
));
921 /* timeout, defaults to 0 */
922 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("timeout"), IS_DOUBLE
))) {
923 curl_easy_setopt(ch
, CURLOPT_TIMEOUT_MS
, (long)(Z_DVAL_P(zoption
)*1000));
925 /* connecttimeout, defaults to 0 */
926 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("connecttimeout"), IS_DOUBLE
))) {
927 curl_easy_setopt(ch
, CURLOPT_CONNECTTIMEOUT_MS
, (long)(Z_DVAL_P(zoption
)*1000));
931 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("ssl"), IS_ARRAY
))) {
932 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
936 FOREACH_KEYVAL(pos
, zoption
, key
, param
) {
937 if (key
.type
== HASH_KEY_IS_STRING
) {
938 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT
, 0, 1);
939 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE
, 0, 0);
940 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD
, 0, 0);
942 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY
, 0, 0);
943 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE
, 0, 0);
944 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD
, 0, 0);
946 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE
, 0, 0);
947 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION
, 0);
949 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER
, 1);
950 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST
, 1);
951 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST
, 1, 0);
953 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAINFO
, -3, 1);
954 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAPATH
, -3, 1);
955 PHP_HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE
, -3, 1);
956 PHP_HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET
, -3, 1);
957 #if PHP_HTTP_CURL_VERSION(7,19,0)
958 PHP_HTTP_CURL_OPT_STRING(CURLOPT_ISSUERCERT
, -3, 1);
959 #if defined(PHP_HTTP_HAVE_OPENSSL)
960 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CRLFILE
, -3, 1);
963 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
964 PHP_HTTP_CURL_OPT_LONG(CURLOPT_CERTINFO
, -3);
972 static STATUS
get_info(CURL
*ch
, HashTable
*info
)
977 struct curl_slist
*s
, *p
;
978 zval
*subarray
, array
;
979 INIT_PZVAL_ARRAY(&array
, info
);
981 /* BEGIN::CURLINFO */
982 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_EFFECTIVE_URL
, &c
)) {
983 add_assoc_string_ex(&array
, "effective_url", sizeof("effective_url"), c
? c
: "", 1);
985 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_RESPONSE_CODE
, &l
)) {
986 add_assoc_long_ex(&array
, "response_code", sizeof("response_code"), l
);
988 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_TOTAL_TIME
, &d
)) {
989 add_assoc_double_ex(&array
, "total_time", sizeof("total_time"), d
);
991 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NAMELOOKUP_TIME
, &d
)) {
992 add_assoc_double_ex(&array
, "namelookup_time", sizeof("namelookup_time"), d
);
994 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONNECT_TIME
, &d
)) {
995 add_assoc_double_ex(&array
, "connect_time", sizeof("connect_time"), d
);
997 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRETRANSFER_TIME
, &d
)) {
998 add_assoc_double_ex(&array
, "pretransfer_time", sizeof("pretransfer_time"), d
);
1000 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_UPLOAD
, &d
)) {
1001 add_assoc_double_ex(&array
, "size_upload", sizeof("size_upload"), d
);
1003 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_DOWNLOAD
, &d
)) {
1004 add_assoc_double_ex(&array
, "size_download", sizeof("size_download"), d
);
1006 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_DOWNLOAD
, &d
)) {
1007 add_assoc_double_ex(&array
, "speed_download", sizeof("speed_download"), d
);
1009 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_UPLOAD
, &d
)) {
1010 add_assoc_double_ex(&array
, "speed_upload", sizeof("speed_upload"), d
);
1012 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HEADER_SIZE
, &l
)) {
1013 add_assoc_long_ex(&array
, "header_size", sizeof("header_size"), l
);
1015 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REQUEST_SIZE
, &l
)) {
1016 add_assoc_long_ex(&array
, "request_size", sizeof("request_size"), l
);
1018 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_VERIFYRESULT
, &l
)) {
1019 add_assoc_long_ex(&array
, "ssl_verifyresult", sizeof("ssl_verifyresult"), l
);
1021 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_FILETIME
, &l
)) {
1022 add_assoc_long_ex(&array
, "filetime", sizeof("filetime"), l
);
1024 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
1025 add_assoc_double_ex(&array
, "content_length_download", sizeof("content_length_download"), d
);
1027 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_UPLOAD
, &d
)) {
1028 add_assoc_double_ex(&array
, "content_length_upload", sizeof("content_length_upload"), d
);
1030 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_STARTTRANSFER_TIME
, &d
)) {
1031 add_assoc_double_ex(&array
, "starttransfer_time", sizeof("starttransfer_time"), d
);
1033 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_TYPE
, &c
)) {
1034 add_assoc_string_ex(&array
, "content_type", sizeof("content_type"), c
? c
: "", 1);
1036 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_TIME
, &d
)) {
1037 add_assoc_double_ex(&array
, "redirect_time", sizeof("redirect_time"), d
);
1039 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_COUNT
, &l
)) {
1040 add_assoc_long_ex(&array
, "redirect_count", sizeof("redirect_count"), l
);
1042 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTP_CONNECTCODE
, &l
)) {
1043 add_assoc_long_ex(&array
, "connect_code", sizeof("connect_code"), l
);
1045 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTPAUTH_AVAIL
, &l
)) {
1046 add_assoc_long_ex(&array
, "httpauth_avail", sizeof("httpauth_avail"), l
);
1048 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PROXYAUTH_AVAIL
, &l
)) {
1049 add_assoc_long_ex(&array
, "proxyauth_avail", sizeof("proxyauth_avail"), l
);
1051 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_OS_ERRNO
, &l
)) {
1052 add_assoc_long_ex(&array
, "os_errno", sizeof("os_errno"), l
);
1054 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NUM_CONNECTS
, &l
)) {
1055 add_assoc_long_ex(&array
, "num_connects", sizeof("num_connects"), l
);
1057 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_ENGINES
, &s
)) {
1058 MAKE_STD_ZVAL(subarray
);
1059 array_init(subarray
);
1060 for (p
= s
; p
; p
= p
->next
) {
1062 add_next_index_string(subarray
, p
->data
, 1);
1065 add_assoc_zval_ex(&array
, "ssl_engines", sizeof("ssl_engines"), subarray
);
1066 curl_slist_free_all(s
);
1068 #if PHP_HTTP_CURL_VERSION(7,14,1)
1069 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_COOKIELIST
, &s
)) {
1070 MAKE_STD_ZVAL(subarray
);
1071 array_init(subarray
);
1072 for (p
= s
; p
; p
= p
->next
) {
1074 add_next_index_string(subarray
, p
->data
, 1);
1077 add_assoc_zval_ex(&array
, "cookies", sizeof("cookies"), subarray
);
1078 curl_slist_free_all(s
);
1081 #if PHP_HTTP_CURL_VERSION(7,18,2)
1082 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_URL
, &c
)) {
1083 add_assoc_string_ex(&array
, "redirect_url", sizeof("redirect_url"), c
? c
: "", 1);
1086 #if PHP_HTTP_CURL_VERSION(7,19,0)
1087 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRIMARY_IP
, &c
)) {
1088 add_assoc_string_ex(&array
, "primary_ip", sizeof("primary_ip"), c
? c
: "", 1);
1091 #if PHP_HTTP_CURL_VERSION(7,19,0)
1092 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_APPCONNECT_TIME
, &d
)) {
1093 add_assoc_double_ex(&array
, "appconnect_time", sizeof("appconnect_time"), d
);
1096 #if PHP_HTTP_CURL_VERSION(7,19,4)
1097 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONDITION_UNMET
, &l
)) {
1098 add_assoc_long_ex(&array
, "condition_unmet", sizeof("condition_unmet"), l
);
1102 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
1106 struct curl_certinfo
*ci
;
1107 char *colon
, *keyname
;
1109 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CERTINFO
, &ci
)) {
1110 MAKE_STD_ZVAL(ci_array
);
1111 array_init(ci_array
);
1113 for (i
= 0; i
< ci
->num_of_certs
; ++i
) {
1114 s
= ci
->certinfo
[i
];
1116 MAKE_STD_ZVAL(subarray
);
1117 array_init(subarray
);
1118 for (p
= s
; p
; p
= p
->next
) {
1120 if ((colon
= strchr(p
->data
, ':'))) {
1121 keyname
= estrndup(p
->data
, colon
- p
->data
);
1122 add_assoc_string_ex(subarray
, keyname
, colon
- p
->data
+ 1, colon
+ 1, 1);
1125 add_next_index_string(subarray
, p
->data
, 1);
1129 add_next_index_zval(ci_array
, subarray
);
1131 add_assoc_zval_ex(&array
, "certinfo", sizeof("certinfo"), ci_array
);
1135 add_assoc_string_ex(&array
, "error", sizeof("error"), get_storage(ch
)->errorbuffer
, 1);
1141 /* request datashare handler ops */
1143 static php_http_request_datashare_t
*php_http_curl_request_datashare_init(php_http_request_datashare_t
*h
, void *handle
)
1145 php_http_curl_request_datashare_t
*curl
;
1146 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1148 if (!handle
&& !(handle
= php_http_resource_factory_handle_ctor(h
->rf TSRMLS_CC
))) {
1149 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_DATASHARE
, "could not initialize curl share handle");
1153 curl
= ecalloc(1, sizeof(*curl
));
1154 curl
->handle
= handle
;
1160 static void php_http_curl_request_datashare_dtor(php_http_request_datashare_t
*h
)
1162 php_http_curl_request_datashare_t
*curl
= h
->ctx
;
1163 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1165 php_http_resource_factory_handle_dtor(h
->rf
, curl
->handle TSRMLS_CC
);
1171 static STATUS
php_http_curl_request_datashare_attach(php_http_request_datashare_t
*h
, php_http_request_t
*r
)
1174 php_http_curl_request_datashare_t
*curl
= h
->ctx
;
1175 php_http_curl_request_t
*recurl
= r
->ctx
;
1176 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1178 if (CURLE_OK
!= (rc
= curl_easy_setopt(recurl
->handle
, CURLOPT_SHARE
, curl
->handle
))) {
1179 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_DATASHARE
, "Could not attach request to the datashare: %s", curl_easy_strerror(rc
));
1185 static STATUS
php_http_curl_request_datashare_detach(php_http_request_datashare_t
*h
, php_http_request_t
*r
)
1188 php_http_curl_request_t
*recurl
= r
->ctx
;
1189 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1192 if (CURLE_OK
!= (rc
= curl_easy_setopt(recurl
->handle
, CURLOPT_SHARE
, NULL
))) {
1193 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_DATASHARE
, "Could not detach request from the datashare: %s", curl_share_strerror(rc
));
1199 static STATUS
php_http_curl_request_datashare_setopt(php_http_request_datashare_t
*h
, php_http_request_datashare_setopt_opt_t opt
, void *arg
)
1202 php_http_curl_request_datashare_t
*curl
= h
->ctx
;
1205 case PHP_HTTP_REQUEST_DATASHARE_OPT_COOKIES
:
1206 if (CURLSHE_OK
!= (rc
= curl_share_setopt(curl
->handle
, *((zend_bool
*) arg
) ? CURLSHOPT_SHARE
: CURLSHOPT_UNSHARE
, CURL_LOCK_DATA_COOKIE
))) {
1207 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1209 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_DATASHARE
, "Could not %s sharing of cookie data: %s", *((zend_bool
*) arg
) ? "enable" : "disable", curl_share_strerror(rc
));
1214 case PHP_HTTP_REQUEST_DATASHARE_OPT_RESOLVER
:
1215 if (CURLSHE_OK
!= (rc
= curl_share_setopt(curl
->handle
, *((zend_bool
*) arg
) ? CURLSHOPT_SHARE
: CURLSHOPT_UNSHARE
, CURL_LOCK_DATA_DNS
))) {
1216 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1218 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_DATASHARE
, "Could not %s sharing of resolver data: %s", *((zend_bool
*) arg
) ? "enable" : "disable", curl_share_strerror(rc
));
1230 static php_http_resource_factory_ops_t php_http_curlsh_resource_factory_ops
= {
1231 php_http_curlsh_ctor
,
1233 php_http_curlsh_dtor
1236 static php_http_request_datashare_ops_t php_http_curl_request_datashare_ops
= {
1237 &php_http_curlsh_resource_factory_ops
,
1238 php_http_curl_request_datashare_init
,
1240 php_http_curl_request_datashare_dtor
,
1242 php_http_curl_request_datashare_attach
,
1243 php_http_curl_request_datashare_detach
,
1244 php_http_curl_request_datashare_setopt
,
1247 PHP_HTTP_API php_http_request_datashare_ops_t
*php_http_curl_get_request_datashare_ops(void)
1249 return &php_http_curl_request_datashare_ops
;
1253 /* request pool handler ops */
1255 static php_http_request_pool_t
*php_http_curl_request_pool_init(php_http_request_pool_t
*h
, void *handle
)
1257 php_http_curl_request_pool_t
*curl
;
1258 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1260 if (!handle
&& !(handle
= php_http_resource_factory_handle_ctor(h
->rf TSRMLS_CC
))) {
1261 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_POOL
, "could not initialize curl pool handle");
1265 curl
= ecalloc(1, sizeof(*curl
));
1266 curl
->handle
= handle
;
1267 curl
->unfinished
= 0;
1273 static void php_http_curl_request_pool_dtor(php_http_request_pool_t
*h
)
1275 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1276 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1278 #if PHP_HTTP_HAVE_EVENT
1279 if (curl
->timeout
) {
1280 efree(curl
->timeout
);
1281 curl
->timeout
= NULL
;
1284 curl
->unfinished
= 0;
1285 php_http_request_pool_reset(h
);
1287 php_http_resource_factory_handle_dtor(h
->rf
, curl
->handle TSRMLS_CC
);
1293 static STATUS
php_http_curl_request_pool_attach(php_http_request_pool_t
*h
, php_http_request_t
*r
, php_http_request_method_t m
, const char *url
, php_http_message_body_t
*body
)
1295 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1296 php_http_curl_request_t
*recurl
= r
->ctx
;
1298 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1300 if (SUCCESS
!= php_http_curl_request_prepare(r
, m
, url
, body
)) {
1304 if (CURLM_OK
== (rs
= curl_multi_add_handle(curl
->handle
, recurl
->handle
))) {
1308 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_POOL
, "Could not attach request to pool: %s", curl_multi_strerror(rs
));
1313 static STATUS
php_http_curl_request_pool_detach(php_http_request_pool_t
*h
, php_http_request_t
*r
)
1315 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1316 php_http_curl_request_t
*recurl
= r
->ctx
;
1317 CURLMcode rs
= curl_multi_remove_handle(curl
->handle
, recurl
->handle
);
1318 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1320 if (CURLM_OK
== rs
) {
1323 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_POOL
, "Could not detach request from pool: %s", curl_multi_strerror(rs
));
1329 # define SELECT_ERROR SOCKET_ERROR
1331 # define SELECT_ERROR -1
1334 static STATUS
php_http_curl_request_pool_wait(php_http_request_pool_t
*h
, struct timeval
*custom_timeout
)
1338 struct timeval timeout
;
1339 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1341 #if PHP_HTTP_HAVE_EVENT
1342 if (curl
->useevents
) {
1343 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1345 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "not implemented");
1350 if (custom_timeout
&& timerisset(custom_timeout
)) {
1351 timeout
= *custom_timeout
;
1353 long max_tout
= 1000;
1355 if ((CURLM_OK
== curl_multi_timeout(curl
->handle
, &max_tout
)) && (max_tout
> 0)) {
1356 timeout
.tv_sec
= max_tout
/ 1000;
1357 timeout
.tv_usec
= (max_tout
% 1000) * 1000;
1360 timeout
.tv_usec
= 1000;
1368 if (CURLM_OK
== curl_multi_fdset(curl
->handle
, &R
, &W
, &E
, &MAX
)) {
1370 php_http_sleep((double) timeout
.tv_sec
+ (double) (timeout
.tv_usec
/ PHP_HTTP_MCROSEC
));
1372 } else if (SELECT_ERROR
!= select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) {
1379 static int php_http_curl_request_pool_once(php_http_request_pool_t
*h
)
1381 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1383 #if PHP_HTTP_HAVE_EVENT
1384 if (curl
->useevents
) {
1385 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1386 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "not implemented");
1391 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(curl
->handle
, &curl
->unfinished
));
1393 php_http_curl_request_pool_responsehandler(h
);
1395 return curl
->unfinished
;
1398 #if PHP_HTTP_HAVE_EVENT
1399 static void dolog(int i
, const char *m
) {
1400 fprintf(stderr
, "%d: %s\n", i
, m
);
1403 static STATUS
php_http_curl_request_pool_exec(php_http_request_pool_t
*h
)
1405 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1407 #if PHP_HTTP_HAVE_EVENT
1408 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1410 if (curl
->useevents
) {
1411 event_set_log_callback(dolog
);
1414 fprintf(stderr
, "X");
1416 event_base_dispatch(PHP_HTTP_G
->curl
.event_base
);
1417 } while (curl
->unfinished
);
1421 while (php_http_curl_request_pool_once(h
)) {
1422 if (SUCCESS
!= php_http_curl_request_pool_wait(h
, NULL
)) {
1424 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
1425 php_http_error(HE_WARNING
, PHP_HTTP_E_SOCKET
, "WinSock error: %d", WSAGetLastError());
1427 php_http_error(HE_WARNING
, PHP_HTTP_E_SOCKET
, strerror(errno
));
1437 static STATUS
php_http_curl_request_pool_setopt(php_http_request_pool_t
*h
, php_http_request_pool_setopt_opt_t opt
, void *arg
)
1439 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1442 case PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING
:
1443 if (CURLM_OK
!= curl_multi_setopt(curl
->handle
, CURLMOPT_PIPELINING
, (long) *((zend_bool
*) arg
))) {
1448 case PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS
:
1449 #if PHP_HTTP_HAVE_EVENT
1450 if ((curl
->useevents
= *((zend_bool
*) arg
))) {
1451 if (!curl
->timeout
) {
1452 curl
->timeout
= ecalloc(1, sizeof(struct event
));
1454 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETDATA
, h
);
1455 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETFUNCTION
, php_http_curl_request_pool_socket_callback
);
1456 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERDATA
, h
);
1457 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERFUNCTION
, php_http_curl_request_pool_timer_callback
);
1459 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETDATA
, NULL
);
1460 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETFUNCTION
, NULL
);
1461 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERDATA
, NULL
);
1462 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERFUNCTION
, NULL
);
1473 static php_http_resource_factory_ops_t php_http_curlm_resource_factory_ops
= {
1474 php_http_curlm_ctor
,
1479 static php_http_request_pool_ops_t php_http_curl_request_pool_ops
= {
1480 &php_http_curlm_resource_factory_ops
,
1481 php_http_curl_request_pool_init
,
1483 php_http_curl_request_pool_dtor
,
1485 php_http_curl_request_pool_exec
,
1486 php_http_curl_request_pool_wait
,
1487 php_http_curl_request_pool_once
,
1488 php_http_curl_request_pool_attach
,
1489 php_http_curl_request_pool_detach
,
1490 php_http_curl_request_pool_setopt
,
1493 PHP_HTTP_API php_http_request_pool_ops_t
*php_http_curl_get_request_pool_ops(void)
1495 return &php_http_curl_request_pool_ops
;
1498 /* request handler ops */
1500 static STATUS
php_http_curl_request_reset(php_http_request_t
*h
);
1502 static php_http_request_t
*php_http_curl_request_init(php_http_request_t
*h
, void *handle
)
1504 php_http_curl_request_t
*ctx
;
1505 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1507 if (!handle
&& !(handle
= php_http_resource_factory_handle_ctor(h
->rf TSRMLS_CC
))) {
1508 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST
, "could not initialize curl handle");
1512 ctx
= ecalloc(1, sizeof(*ctx
));
1513 ctx
->handle
= handle
;
1514 php_http_buffer_init(&ctx
->options
.cookies
);
1515 zend_hash_init(&ctx
->options
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
1519 curl_easy_setopt(handle
, CURLOPT_NOSIGNAL
, 1L);
1521 curl_easy_setopt(handle
, CURLOPT_HEADER
, 0L);
1522 curl_easy_setopt(handle
, CURLOPT_FILETIME
, 1L);
1523 curl_easy_setopt(handle
, CURLOPT_AUTOREFERER
, 1L);
1524 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
1525 curl_easy_setopt(handle
, CURLOPT_NOPROGRESS
, 0L);
1526 curl_easy_setopt(handle
, CURLOPT_HEADERFUNCTION
, NULL
);
1527 curl_easy_setopt(handle
, CURLOPT_WRITEFUNCTION
, php_http_curl_dummy_callback
);
1528 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, php_http_curl_raw_callback
);
1529 curl_easy_setopt(handle
, CURLOPT_READFUNCTION
, php_http_curl_read_callback
);
1530 curl_easy_setopt(handle
, CURLOPT_IOCTLFUNCTION
, php_http_curl_ioctl_callback
);
1531 curl_easy_setopt(handle
, CURLOPT_PROGRESSFUNCTION
, php_http_curl_progress_callback
);
1532 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, h
);
1533 curl_easy_setopt(handle
, CURLOPT_PROGRESSDATA
, h
);
1535 php_http_curl_request_reset(h
);
1540 static php_http_request_t
*php_http_curl_request_copy(php_http_request_t
*from
, php_http_request_t
*to
)
1542 php_http_curl_request_t
*ctx
= from
->ctx
;
1544 TSRMLS_FETCH_FROM_CTX(from
->ts
);
1546 if (!(copy
= php_http_resource_factory_handle_copy(from
->rf
, ctx
->handle TSRMLS_CC
))) {
1551 return php_http_curl_request_init(to
, copy
);
1553 return php_http_request_init(NULL
, from
->ops
, from
->rf
, copy TSRMLS_CC
);
1557 static void php_http_curl_request_dtor(php_http_request_t
*h
)
1559 php_http_curl_request_t
*ctx
= h
->ctx
;
1560 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1562 curl_easy_setopt(ctx
->handle
, CURLOPT_NOPROGRESS
, 1L);
1563 curl_easy_setopt(ctx
->handle
, CURLOPT_PROGRESSFUNCTION
, NULL
);
1564 curl_easy_setopt(ctx
->handle
, CURLOPT_VERBOSE
, 0L);
1565 curl_easy_setopt(ctx
->handle
, CURLOPT_DEBUGFUNCTION
, NULL
);
1567 php_http_resource_factory_handle_dtor(h
->rf
, ctx
->handle TSRMLS_CC
);
1569 php_http_buffer_dtor(&ctx
->options
.cookies
);
1570 zend_hash_destroy(&ctx
->options
.cache
);
1572 if (ctx
->options
.headers
) {
1573 curl_slist_free_all(ctx
->options
.headers
);
1574 ctx
->options
.headers
= NULL
;
1576 php_http_request_progress_dtor(&ctx
->progress TSRMLS_CC
);
1581 static STATUS
php_http_curl_request_reset(php_http_request_t
*h
)
1583 CURL
*ch
= ((php_http_curl_request_t
*) h
->ctx
)->handle
;
1584 php_http_curl_request_storage_t
*st
;
1586 if ((st
= get_storage(ch
))) {
1591 if (st
->cookiestore
) {
1592 pefree(st
->cookiestore
, 1);
1593 st
->cookiestore
= NULL
;
1595 st
->errorbuffer
[0] = '\0';
1598 curl_easy_setopt(ch
, CURLOPT_URL
, NULL
);
1599 #if PHP_HTTP_CURL_VERSION(7,19,4)
1600 curl_easy_setopt(ch
, CURLOPT_NOPROXY
, NULL
);
1602 curl_easy_setopt(ch
, CURLOPT_PROXY
, NULL
);
1603 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, 0L);
1604 curl_easy_setopt(ch
, CURLOPT_PROXYTYPE
, 0L);
1605 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1606 #if PHP_HTTP_CURL_VERSION(7,19,1)
1607 curl_easy_setopt(ch
, CURLOPT_PROXYUSERNAME
, NULL
);
1608 curl_easy_setopt(ch
, CURLOPT_PROXYPASSWORD
, NULL
);
1610 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, 0L);
1611 curl_easy_setopt(ch
, CURLOPT_HTTPPROXYTUNNEL
, 0L);
1612 curl_easy_setopt(ch
, CURLOPT_DNS_CACHE_TIMEOUT
, 60L);
1613 curl_easy_setopt(ch
, CURLOPT_IPRESOLVE
, 0);
1614 curl_easy_setopt(ch
, CURLOPT_LOW_SPEED_LIMIT
, 0L);
1615 curl_easy_setopt(ch
, CURLOPT_LOW_SPEED_TIME
, 0L);
1617 curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) 0);
1618 curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) 0);
1621 curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, 5L); */
1622 curl_easy_setopt(ch
, CURLOPT_FRESH_CONNECT
, 0L);
1623 curl_easy_setopt(ch
, CURLOPT_FORBID_REUSE
, 0L);
1624 curl_easy_setopt(ch
, CURLOPT_INTERFACE
, NULL
);
1625 curl_easy_setopt(ch
, CURLOPT_PORT
, 0L);
1626 #if PHP_HTTP_CURL_VERSION(7,19,0)
1627 curl_easy_setopt(ch
, CURLOPT_ADDRESS_SCOPE
, 0L);
1629 curl_easy_setopt(ch
, CURLOPT_LOCALPORT
, 0L);
1630 curl_easy_setopt(ch
, CURLOPT_LOCALPORTRANGE
, 0L);
1631 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1632 #if PHP_HTTP_CURL_VERSION(7,19,1)
1633 curl_easy_setopt(ch
, CURLOPT_USERNAME
, NULL
);
1634 curl_easy_setopt(ch
, CURLOPT_PASSWORD
, NULL
);
1636 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, 0L);
1637 curl_easy_setopt(ch
, CURLOPT_ENCODING
, NULL
);
1638 /* we do this ourself anyway */
1639 curl_easy_setopt(ch
, CURLOPT_HTTP_CONTENT_DECODING
, 0L);
1640 curl_easy_setopt(ch
, CURLOPT_HTTP_TRANSFER_DECODING
, 0L);
1641 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, 0L);
1642 #if PHP_HTTP_CURL_VERSION(7,19,1)
1643 curl_easy_setopt(ch
, CURLOPT_POSTREDIR
, 0L);
1645 curl_easy_setopt(ch
, CURLOPT_POST301
, 0L);
1647 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, 0L);
1648 curl_easy_setopt(ch
, CURLOPT_REFERER
, NULL
);
1649 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, "PECL::HTTP/" PHP_HTTP_EXT_VERSION
" (PHP/" PHP_VERSION
")");
1650 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, NULL
);
1651 curl_easy_setopt(ch
, CURLOPT_COOKIE
, NULL
);
1652 curl_easy_setopt(ch
, CURLOPT_COOKIESESSION
, 0L);
1653 /* these options would enable curl's cookie engine by default which we don't want
1654 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, NULL);
1655 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, NULL); */
1656 curl_easy_setopt(ch
, CURLOPT_COOKIELIST
, NULL
);
1657 curl_easy_setopt(ch
, CURLOPT_RANGE
, NULL
);
1658 curl_easy_setopt(ch
, CURLOPT_RESUME_FROM
, 0L);
1659 curl_easy_setopt(ch
, CURLOPT_MAXFILESIZE
, 0L);
1660 curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, 0L);
1661 curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, 0L);
1662 curl_easy_setopt(ch
, CURLOPT_TIMEOUT
, 0L);
1663 curl_easy_setopt(ch
, CURLOPT_CONNECTTIMEOUT
, 3);
1664 curl_easy_setopt(ch
, CURLOPT_SSLCERT
, NULL
);
1665 curl_easy_setopt(ch
, CURLOPT_SSLCERTTYPE
, NULL
);
1666 curl_easy_setopt(ch
, CURLOPT_SSLCERTPASSWD
, NULL
);
1667 curl_easy_setopt(ch
, CURLOPT_SSLKEY
, NULL
);
1668 curl_easy_setopt(ch
, CURLOPT_SSLKEYTYPE
, NULL
);
1669 curl_easy_setopt(ch
, CURLOPT_SSLKEYPASSWD
, NULL
);
1670 curl_easy_setopt(ch
, CURLOPT_SSLENGINE
, NULL
);
1671 curl_easy_setopt(ch
, CURLOPT_SSLVERSION
, 0L);
1672 curl_easy_setopt(ch
, CURLOPT_SSL_VERIFYPEER
, 0L);
1673 curl_easy_setopt(ch
, CURLOPT_SSL_VERIFYHOST
, 0L);
1674 curl_easy_setopt(ch
, CURLOPT_SSL_CIPHER_LIST
, NULL
);
1675 #if PHP_HTTP_CURL_VERSION(7,19,0)
1676 curl_easy_setopt(ch
, CURLOPT_ISSUERCERT
, NULL
);
1677 #if defined(PHP_HTTP_HAVE_OPENSSL)
1678 curl_easy_setopt(ch
, CURLOPT_CRLFILE
, NULL
);
1681 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
1682 curl_easy_setopt(ch
, CURLOPT_CERTINFO
, NULL
);
1684 #ifdef PHP_HTTP_CURL_CAINFO
1685 curl_easy_setopt(ch
, CURLOPT_CAINFO
, PHP_HTTP_CURL_CAINFO
);
1687 curl_easy_setopt(ch
, CURLOPT_CAINFO
, NULL
);
1689 curl_easy_setopt(ch
, CURLOPT_CAPATH
, NULL
);
1690 curl_easy_setopt(ch
, CURLOPT_RANDOM_FILE
, NULL
);
1691 curl_easy_setopt(ch
, CURLOPT_EGDSOCKET
, NULL
);
1692 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, NULL
);
1693 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, 0L);
1694 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, NULL
);
1695 curl_easy_setopt(ch
, CURLOPT_IOCTLDATA
, NULL
);
1696 curl_easy_setopt(ch
, CURLOPT_READDATA
, NULL
);
1697 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, 0L);
1698 curl_easy_setopt(ch
, CURLOPT_HTTP_VERSION
, CURL_HTTP_VERSION_NONE
);
1699 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, NULL
);
1700 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 0L);
1701 curl_easy_setopt(ch
, CURLOPT_POST
, 0L);
1702 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 0L);
1703 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1L);
1708 static STATUS
php_http_curl_request_exec(php_http_request_t
*h
, php_http_request_method_t meth
, const char *url
, php_http_message_body_t
*body
)
1712 php_http_curl_request_t
*curl
= h
->ctx
;
1713 php_http_curl_request_storage_t
*storage
= get_storage(curl
->handle
);
1714 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1716 if (SUCCESS
!= php_http_curl_request_prepare(h
, meth
, url
, body
)) {
1721 if (CURLE_OK
!= (result
= curl_easy_perform(curl
->handle
))) {
1722 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST
, "%s; %s (%s)", curl_easy_strerror(result
), storage
->errorbuffer
, storage
->url
);
1724 if (EG(exception
)) {
1725 add_property_long(EG(exception
), "curlCode", result
);
1728 if (curl
->options
.retry
.count
> tries
++) {
1730 case CURLE_COULDNT_RESOLVE_PROXY
:
1731 case CURLE_COULDNT_RESOLVE_HOST
:
1732 case CURLE_COULDNT_CONNECT
:
1733 case CURLE_WRITE_ERROR
:
1734 case CURLE_READ_ERROR
:
1735 case CURLE_OPERATION_TIMEDOUT
:
1736 case CURLE_SSL_CONNECT_ERROR
:
1737 case CURLE_GOT_NOTHING
:
1738 case CURLE_SSL_ENGINE_SETFAILED
:
1739 case CURLE_SEND_ERROR
:
1740 case CURLE_RECV_ERROR
:
1741 case CURLE_SSL_ENGINE_INITFAILED
:
1742 case CURLE_LOGIN_DENIED
:
1743 if (curl
->options
.retry
.delay
>= PHP_HTTP_DIFFSEC
) {
1744 php_http_sleep(curl
->options
.retry
.delay
);
1758 static STATUS
php_http_curl_request_setopt(php_http_request_t
*h
, php_http_request_setopt_opt_t opt
, void *arg
)
1760 php_http_curl_request_t
*curl
= h
->ctx
;
1761 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1764 case PHP_HTTP_REQUEST_OPT_SETTINGS
:
1765 return set_options(h
, arg
);
1768 case PHP_HTTP_REQUEST_OPT_PROGRESS_CALLBACK
:
1769 if (curl
->progress
.in_cb
) {
1770 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST
, "Cannot change progress callback while executing it");
1773 if (curl
->progress
.callback
) {
1774 php_http_request_progress_dtor(&curl
->progress TSRMLS_CC
);
1776 curl
->progress
.callback
= arg
;
1779 case PHP_HTTP_REQUEST_OPT_COOKIES_ENABLE
:
1780 /* are cookies already enabled anyway? */
1781 if (!get_storage(curl
->handle
)->cookiestore
) {
1782 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIEFILE
, "")) {
1788 case PHP_HTTP_REQUEST_OPT_COOKIES_RESET
:
1789 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIELIST
, "ALL")) {
1794 case PHP_HTTP_REQUEST_OPT_COOKIES_RESET_SESSION
:
1795 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIELIST
, "SESS")) {
1800 case PHP_HTTP_REQUEST_OPT_COOKIES_FLUSH
:
1801 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIELIST
, "FLUSH")) {
1813 static STATUS
php_http_curl_request_getopt(php_http_request_t
*h
, php_http_request_getopt_opt_t opt
, void *arg
)
1815 php_http_curl_request_t
*curl
= h
->ctx
;
1818 case PHP_HTTP_REQUEST_OPT_PROGRESS_INFO
:
1819 *((php_http_request_progress_t
**) arg
) = &curl
->progress
;
1822 case PHP_HTTP_REQUEST_OPT_TRANSFER_INFO
:
1823 get_info(curl
->handle
, arg
);
1833 static php_http_resource_factory_ops_t php_http_curl_resource_factory_ops
= {
1839 static php_http_request_ops_t php_http_curl_request_ops
= {
1840 &php_http_curl_resource_factory_ops
,
1841 php_http_curl_request_init
,
1842 php_http_curl_request_copy
,
1843 php_http_curl_request_dtor
,
1844 php_http_curl_request_reset
,
1845 php_http_curl_request_exec
,
1846 php_http_curl_request_setopt
,
1847 php_http_curl_request_getopt
1850 PHP_HTTP_API php_http_request_ops_t
*php_http_curl_get_request_ops(void)
1852 return &php_http_curl_request_ops
;
1855 #if defined(ZTS) && defined(PHP_HTTP_HAVE_SSL)
1857 # define PHP_HTTP_NEED_OPENSSL_TSL
1858 # include <openssl/crypto.h>
1859 # else /* !PHP_WIN32 */
1860 # if defined(PHP_HTTP_HAVE_OPENSSL)
1861 # define PHP_HTTP_NEED_OPENSSL_TSL
1862 # include <openssl/crypto.h>
1863 # elif defined(PHP_HTTP_HAVE_GNUTLS)
1864 # define PHP_HTTP_NEED_GNUTLS_TSL
1865 # include <gcrypt.h>
1868 "libcurl was compiled with SSL support, but configure could not determine which" \
1869 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
1870 "cause random crashes on SSL requests"
1871 # endif /* PHP_HTTP_HAVE_OPENSSL || PHP_HTTP_HAVE_GNUTLS */
1872 # endif /* PHP_WIN32 */
1873 #endif /* ZTS && PHP_HTTP_HAVE_SSL */
1876 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
1877 static MUTEX_T
*php_http_openssl_tsl
= NULL
;
1879 static void php_http_openssl_thread_lock(int mode
, int n
, const char * file
, int line
)
1881 if (mode
& CRYPTO_LOCK
) {
1882 tsrm_mutex_lock(php_http_openssl_tsl
[n
]);
1884 tsrm_mutex_unlock(php_http_openssl_tsl
[n
]);
1888 static ulong
php_http_openssl_thread_id(void)
1890 return (ulong
) tsrm_thread_id();
1893 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
1894 static int php_http_gnutls_mutex_create(void **m
)
1896 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
1903 static int php_http_gnutls_mutex_destroy(void **m
)
1905 tsrm_mutex_free(*((MUTEX_T
*) m
));
1909 static int php_http_gnutls_mutex_lock(void **m
)
1911 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
1914 static int php_http_gnutls_mutex_unlock(void **m
)
1916 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
1919 static struct gcry_thread_cbs php_http_gnutls_tsl
= {
1920 GCRY_THREAD_OPTION_USER
,
1922 php_http_gnutls_mutex_create
,
1923 php_http_gnutls_mutex_destroy
,
1924 php_http_gnutls_mutex_lock
,
1925 php_http_gnutls_mutex_unlock
1929 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpCURL, method, 0, req_args)
1930 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpCURL, method, 0)
1931 #define PHP_HTTP_CURL_ME(method, visibility) PHP_ME(HttpCURL, method, PHP_HTTP_ARGS(HttpCURL, method), visibility)
1932 #define PHP_HTTP_CURL_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpCURL, method))
1933 #define PHP_HTTP_CURL_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpCURL_##al), PHP_HTTP_ARGS(HttpCURL, al), vis)
1935 PHP_HTTP_EMPTY_ARGS(__construct
);
1937 zend_class_entry
*php_http_curl_class_entry
;
1938 zend_function_entry php_http_curl_method_entry
[] = {
1939 PHP_HTTP_CURL_ME(__construct
, ZEND_ACC_PRIVATE
|ZEND_ACC_CTOR
)
1941 EMPTY_FUNCTION_ENTRY
1944 PHP_METHOD(HttpCURL
, __construct
) {
1947 PHP_MINIT_FUNCTION(http_curl
)
1949 php_http_request_factory_driver_t driver
= {
1950 &php_http_curl_request_ops
,
1951 &php_http_curl_request_pool_ops
,
1952 &php_http_curl_request_datashare_ops
1955 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
1956 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
1957 if (!CRYPTO_get_id_callback()) {
1958 int i
, c
= CRYPTO_num_locks();
1960 php_http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
1962 for (i
= 0; i
< c
; ++i
) {
1963 php_http_openssl_tsl
[i
] = tsrm_mutex_alloc();
1966 CRYPTO_set_id_callback(php_http_openssl_thread_id
);
1967 CRYPTO_set_locking_callback(php_http_openssl_thread_lock
);
1970 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
1971 gcry_control(GCRYCTL_SET_THREAD_CBS
, &php_http_gnutls_tsl
);
1974 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
1978 if (SUCCESS
!= php_http_persistent_handle_provide(ZEND_STRL("http_request_datashare.curl"), &php_http_curlsh_resource_factory_ops
, NULL
, NULL
)) {
1982 if (SUCCESS
!= php_http_persistent_handle_provide(ZEND_STRL("http_request_pool.curl"), &php_http_curlm_resource_factory_ops
, NULL
, NULL
)) {
1986 if (SUCCESS
!= php_http_persistent_handle_provide(ZEND_STRL("http_request.curl"), &php_http_curl_resource_factory_ops
, NULL
, NULL
)) {
1990 if (SUCCESS
!= php_http_request_factory_add_driver(ZEND_STRL("curl"), &driver
)) {
1994 PHP_HTTP_REGISTER_CLASS(http
, CURL
, http_curl
, php_http_curl_class_entry
, 0);
1997 * HTTP Protocol Version Constants
1999 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("HTTP_VERSION_1_0"), CURL_HTTP_VERSION_1_0 TSRMLS_CC
);
2000 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("HTTP_VERSION_1_1"), CURL_HTTP_VERSION_1_1 TSRMLS_CC
);
2001 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("HTTP_VERSION_NONE"), CURL_HTTP_VERSION_NONE TSRMLS_CC
); /* to be removed */
2002 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("HTTP_VERSION_ANY"), CURL_HTTP_VERSION_NONE TSRMLS_CC
);
2005 * SSL Version Constants
2007 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("SSL_VERSION_TLSv1"), CURL_SSLVERSION_TLSv1 TSRMLS_CC
);
2008 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("SSL_VERSION_SSLv2"), CURL_SSLVERSION_SSLv2 TSRMLS_CC
);
2009 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("SSL_VERSION_SSLv3"), CURL_SSLVERSION_SSLv3 TSRMLS_CC
);
2010 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("SSL_VERSION_ANY"), CURL_SSLVERSION_DEFAULT TSRMLS_CC
);
2013 * DNS IPvX resolving
2015 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("IPRESOLVE_V4"), CURL_IPRESOLVE_V4 TSRMLS_CC
);
2016 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("IPRESOLVE_V6"), CURL_IPRESOLVE_V6 TSRMLS_CC
);
2017 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("IPRESOLVE_ANY"), CURL_IPRESOLVE_WHATEVER TSRMLS_CC
);
2022 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("AUTH_BASIC"), CURLAUTH_BASIC TSRMLS_CC
);
2023 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("AUTH_DIGEST"), CURLAUTH_DIGEST TSRMLS_CC
);
2024 #if PHP_HTTP_CURL_VERSION(7,19,3)
2025 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("AUTH_DIGEST_IE"), CURLAUTH_DIGEST_IE TSRMLS_CC
);
2027 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("AUTH_NTLM"), CURLAUTH_NTLM TSRMLS_CC
);
2028 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("AUTH_GSSNEG"), CURLAUTH_GSSNEGOTIATE TSRMLS_CC
);
2029 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("AUTH_ANY"), CURLAUTH_ANY TSRMLS_CC
);
2032 * Proxy Type Constants
2034 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("PROXY_SOCKS4"), CURLPROXY_SOCKS4 TSRMLS_CC
);
2035 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("PROXY_SOCKS4A"), CURLPROXY_SOCKS5 TSRMLS_CC
);
2036 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("PROXY_SOCKS5_HOSTNAME"), CURLPROXY_SOCKS5 TSRMLS_CC
);
2037 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("PROXY_SOCKS5"), CURLPROXY_SOCKS5 TSRMLS_CC
);
2038 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("PROXY_HTTP"), CURLPROXY_HTTP TSRMLS_CC
);
2039 # if PHP_HTTP_CURL_VERSION(7,19,4)
2040 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("PROXY_HTTP_1_0"), CURLPROXY_HTTP_1_0 TSRMLS_CC
);
2044 * Post Redirection Constants
2046 #if PHP_HTTP_CURL_VERSION(7,19,1)
2047 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("POSTREDIR_301"), CURL_REDIR_POST_301 TSRMLS_CC
);
2048 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("POSTREDIR_302"), CURL_REDIR_POST_302 TSRMLS_CC
);
2049 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("POSTREDIR_ALL"), CURL_REDIR_POST_ALL TSRMLS_CC
);
2055 PHP_MSHUTDOWN_FUNCTION(http_curl
)
2057 curl_global_cleanup();
2058 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
2059 if (php_http_openssl_tsl
) {
2060 int i
, c
= CRYPTO_num_locks();
2062 CRYPTO_set_id_callback(NULL
);
2063 CRYPTO_set_locking_callback(NULL
);
2065 for (i
= 0; i
< c
; ++i
) {
2066 tsrm_mutex_free(php_http_openssl_tsl
[i
]);
2069 free(php_http_openssl_tsl
);
2070 php_http_openssl_tsl
= NULL
;
2076 PHP_RINIT_FUNCTION(http_curl
)
2078 #if PHP_HTTP_HAVE_EVENT
2079 if (!PHP_HTTP_G
->curl
.event_base
&& !(PHP_HTTP_G
->curl
.event_base
= event_init())) {
2087 #endif /* PHP_HTTP_HAVE_CURL */
2094 * vim600: noet sw=4 ts=4 fdm=marker
2095 * vim<600: noet sw=4 ts=4