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 struct curl_slist
*resolve
;
32 php_http_buffer_t cookies
;
43 php_http_request_progress_t progress
;
45 } php_http_curl_request_t
;
47 typedef struct php_http_curl_request_storage
{
50 char errorbuffer
[0x100];
51 } php_http_curl_request_storage_t
;
53 typedef struct php_http_curl_request_pool
{
56 int unfinished
; /* int because of curl_multi_perform() */
58 #if PHP_HTTP_HAVE_EVENT
59 struct event
*timeout
;
63 } php_http_curl_request_pool_t
;
65 typedef struct php_http_curl_request_datashare
{
67 } php_http_curl_request_datashare_t
;
69 #define PHP_HTTP_CURL_OPT_STRING(OPTION, ldiff, obdc) \
72 PHP_HTTP_CURL_OPT_STRING_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION, obdc); \
74 #define PHP_HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
75 if (!strcasecmp(key.str, keyname)) { \
76 zval *copy = cache_option(&curl->options.cache, keyname, strlen(keyname)+1, 0, php_http_ztyp(IS_STRING, *param)); \
78 if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(copy) TSRMLS_CC)) { \
82 curl_easy_setopt(ch, optname, Z_STRVAL_P(copy)); \
83 zval_ptr_dtor(©); \
86 #define PHP_HTTP_CURL_OPT_LONG(OPTION, ldiff) \
89 PHP_HTTP_CURL_OPT_LONG_EX(K+lenof("CURLOPT_KEY")+ldiff, OPTION); \
91 #define PHP_HTTP_CURL_OPT_LONG_EX(keyname, optname) \
92 if (!strcasecmp(key.str, keyname)) { \
93 zval *copy = php_http_ztyp(IS_LONG, *param); \
94 curl_easy_setopt(ch, optname, Z_LVAL_P(copy)); \
95 zval_ptr_dtor(©); \
99 static inline php_http_curl_request_storage_t
*get_storage(CURL
*ch
) {
100 php_http_curl_request_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 /* resource_factory ops */
115 static void *php_http_curl_ctor(void *opaque TSRMLS_DC
)
119 if ((ch
= curl_easy_init())) {
126 static void *php_http_curl_copy(void *opaque
, void *handle TSRMLS_DC
)
130 if ((ch
= curl_easy_duphandle(handle
))) {
138 static void php_http_curl_dtor(void *opaque
, void *handle TSRMLS_DC
)
140 php_http_curl_request_storage_t
*st
= get_storage(handle
);
142 curl_easy_cleanup(handle
);
148 if (st
->cookiestore
) {
149 pefree(st
->cookiestore
, 1);
155 static void *php_http_curlm_ctor(void *opaque TSRMLS_DC
)
157 return curl_multi_init();
160 static void php_http_curlm_dtor(void *opaque
, void *handle TSRMLS_DC
)
162 curl_multi_cleanup(handle
);
165 static void *php_http_curlsh_ctor(void *opaque TSRMLS_DC
)
167 return curl_share_init();
170 static void php_http_curlsh_dtor(void *opaque
, void *handle TSRMLS_DC
)
172 curl_share_cleanup(handle
);
177 static size_t php_http_curl_read_callback(void *data
, size_t len
, size_t n
, void *ctx
)
179 php_http_message_body_t
*body
= ctx
;
182 TSRMLS_FETCH_FROM_CTX(body
->ts
);
183 return php_stream_read(php_http_message_body_stream(body
), data
, len
* n
);
188 static int php_http_curl_progress_callback(void *ctx
, double dltotal
, double dlnow
, double ultotal
, double ulnow
)
190 php_http_request_t
*h
= ctx
;
191 php_http_curl_request_t
*curl
= h
->ctx
;
192 TSRMLS_FETCH_FROM_CTX(h
->ts
);
194 curl
->progress
.state
.dl
.total
= dltotal
;
195 curl
->progress
.state
.dl
.now
= dlnow
;
196 curl
->progress
.state
.ul
.total
= ultotal
;
197 curl
->progress
.state
.ul
.now
= ulnow
;
199 php_http_request_progress_notify(&curl
->progress TSRMLS_CC
);
204 static curlioerr
php_http_curl_ioctl_callback(CURL
*ch
, curliocmd cmd
, void *ctx
)
206 php_http_message_body_t
*body
= ctx
;
208 if (cmd
!= CURLIOCMD_RESTARTREAD
) {
209 return CURLIOE_UNKNOWNCMD
;
213 TSRMLS_FETCH_FROM_CTX(body
->ts
);
215 if (SUCCESS
== php_stream_rewind(php_http_message_body_stream(body
))) {
220 return CURLIOE_FAILRESTART
;
223 static int php_http_curl_raw_callback(CURL
*ch
, curl_infotype type
, char *data
, size_t length
, void *ctx
)
225 php_http_request_t
*h
= ctx
;
226 php_http_curl_request_t
*curl
= h
->ctx
;
228 TSRMLS_FETCH_FROM_CTX(h
->ts
);
233 if (php_memnstr(data
, ZEND_STRL("About to connect"), data
+ length
)) {
234 curl
->progress
.state
.info
= "resolve";
235 } else if (php_memnstr(data
, ZEND_STRL("Trying"), data
+ length
)) {
236 curl
->progress
.state
.info
= "connect";
237 } else if (php_memnstr(data
, ZEND_STRL("Connected"), data
+ length
)) {
238 curl
->progress
.state
.info
= "connected";
239 } else if (php_memnstr(data
, ZEND_STRL("left intact"), data
+ length
)) {
240 curl
->progress
.state
.info
= "not disconnected";
241 } else if (php_memnstr(data
, ZEND_STRL("closed"), data
+ length
)) {
242 curl
->progress
.state
.info
= "disconnected";
243 } else if (php_memnstr(data
, ZEND_STRL("Issue another request"), data
+ length
)) {
244 curl
->progress
.state
.info
= "redirect";
246 php_http_request_progress_notify(&curl
->progress TSRMLS_CC
);
248 case CURLINFO_HEADER_OUT
:
249 case CURLINFO_DATA_OUT
:
250 case CURLINFO_SSL_DATA_OUT
:
251 curl
->progress
.state
.info
= "send";
253 case CURLINFO_HEADER_IN
:
254 case CURLINFO_DATA_IN
:
255 case CURLINFO_SSL_DATA_IN
:
256 curl
->progress
.state
.info
= "receive";
263 case CURLINFO_HEADER_IN
:
264 case CURLINFO_DATA_IN
:
265 case CURLINFO_HEADER_OUT
:
266 case CURLINFO_DATA_OUT
:
267 php_http_buffer_append(h
->buffer
, data
, length
);
269 if (curl
->options
.redirects
) {
270 flags
|= PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS
;
273 if (PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE
== php_http_message_parser_parse(h
->parser
, h
->buffer
, flags
, &h
->message
)) {
283 _dpf(type
, data
, length
);
289 static int php_http_curl_dummy_callback(char *data
, size_t n
, size_t l
, void *s
)
294 static STATUS
php_http_curl_request_prepare(php_http_request_t
*h
, const char *meth
, const char *url
, php_http_message_body_t
*body
)
296 php_http_curl_request_t
*curl
= h
->ctx
;
297 php_http_curl_request_storage_t
*storage
= get_storage(curl
->handle
);
298 TSRMLS_FETCH_FROM_CTX(h
->ts
);
300 storage
->errorbuffer
[0] = '\0';
302 pefree(storage
->url
, 1);
304 storage
->url
= pestrdup(url
, 1);
305 curl_easy_setopt(curl
->handle
, CURLOPT_URL
, storage
->url
);
308 switch (php_http_select_str(meth
, 4, "GET", "HEAD", "POST", "PUT")) {
310 curl_easy_setopt(curl
->handle
, CURLOPT_HTTPGET
, 1L);
314 curl_easy_setopt(curl
->handle
, CURLOPT_NOBODY
, 1L);
318 curl_easy_setopt(curl
->handle
, CURLOPT_POST
, 1L);
322 curl_easy_setopt(curl
->handle
, CURLOPT_UPLOAD
, 1L);
327 curl_easy_setopt(curl
->handle
, CURLOPT_CUSTOMREQUEST
, meth
);
329 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_METHOD
, "Unsupported request method: '%s' (%s)", meth
, url
);
336 /* attach request body */
338 /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
339 * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
340 * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
341 * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
342 * does not allow a request body.
344 size_t body_size
= php_http_message_body_size(body
);
346 curl_easy_setopt(curl
->handle
, CURLOPT_IOCTLDATA
, body
);
347 curl_easy_setopt(curl
->handle
, CURLOPT_READDATA
, body
);
348 curl_easy_setopt(curl
->handle
, CURLOPT_INFILESIZE
, body_size
);
349 curl_easy_setopt(curl
->handle
, CURLOPT_POSTFIELDSIZE
, body_size
);
355 static void php_http_curl_request_pool_responsehandler(php_http_request_pool_t
*pool
)
359 php_http_curl_request_pool_t
*curl
= pool
->ctx
;
360 TSRMLS_FETCH_FROM_CTX(pool
->ts
);
363 CURLMsg
*msg
= curl_multi_info_read(curl
->handle
, &remaining
);
365 if (msg
&& CURLMSG_DONE
== msg
->msg
) {
368 if (CURLE_OK
!= msg
->data
.result
) {
369 php_http_curl_request_storage_t
*st
= get_storage(msg
->easy_handle
);
370 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
));
373 php_http_request_pool_requests(pool
, &requests
, NULL
);
374 for (request
= requests
; *request
; ++request
) {
375 php_http_request_object_t
*obj
= zend_object_store_get_object(*request TSRMLS_CC
);
377 if (msg
->easy_handle
== ((php_http_curl_request_t
*) (obj
->request
->ctx
))->handle
) {
378 Z_ADDREF_PP(request
);
379 zend_llist_add_element(&pool
->requests
.finished
, request
);
380 php_http_request_object_responsehandler(obj
, *request TSRMLS_CC
);
383 zval_ptr_dtor(request
);
391 #if PHP_HTTP_HAVE_EVENT
393 typedef struct php_http_request_pool_event
{
395 php_http_request_pool_t
*pool
;
396 } php_http_request_pool_event_t
;
398 static inline int etoca(short action
) {
399 switch (action
& (EV_READ
|EV_WRITE
)) {
401 return CURL_CSELECT_IN
;
404 return CURL_CSELECT_OUT
;
406 case EV_READ
|EV_WRITE
:
407 return CURL_CSELECT_IN
|CURL_CSELECT_OUT
;
414 static void php_http_curl_request_pool_timeout_callback(int socket
, short action
, void *event_data
)
416 php_http_request_pool_t
*pool
= event_data
;
417 php_http_curl_request_pool_t
*curl
= pool
->ctx
;
420 fprintf(stderr
, "T");
422 if (curl
->useevents
) {
424 TSRMLS_FETCH_FROM_CTX(pool
->ts
);
426 while (CURLM_CALL_MULTI_PERFORM
== (rc
= curl_multi_socket_action(curl
->handle
, socket
, etoca(action
), &curl
->unfinished
)));
428 if (CURLM_OK
!= rc
) {
429 php_http_error(HE_WARNING
, PHP_HTTP_E_SOCKET
, "%s", curl_multi_strerror(rc
));
432 php_http_curl_request_pool_responsehandler(pool
);
436 static void php_http_curl_request_pool_event_callback(int socket
, short action
, void *event_data
)
438 php_http_request_pool_t
*pool
= event_data
;
439 php_http_curl_request_pool_t
*curl
= pool
->ctx
;
442 fprintf(stderr
, "E");
444 if (curl
->useevents
) {
445 CURLMcode rc
= CURLE_OK
;
446 TSRMLS_FETCH_FROM_CTX(pool
->ts
);
448 while (CURLM_CALL_MULTI_PERFORM
== (rc
= curl_multi_socket_action(curl
->handle
, socket
, etoca(action
), &curl
->unfinished
)));
450 if (CURLM_OK
!= rc
) {
451 php_http_error(HE_WARNING
, PHP_HTTP_E_SOCKET
, "%s", curl_multi_strerror(rc
));
454 php_http_curl_request_pool_responsehandler(pool
);
456 /* remove timeout if there are no transfers left */
457 if (!curl
->unfinished
&& event_initialized(curl
->timeout
) && event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
458 event_del(curl
->timeout
);
463 static int php_http_curl_request_pool_socket_callback(CURL
*easy
, curl_socket_t sock
, int action
, void *socket_data
, void *assign_data
)
465 php_http_request_pool_t
*pool
= socket_data
;
466 php_http_curl_request_pool_t
*curl
= pool
->ctx
;
469 fprintf(stderr
, "S");
471 if (curl
->useevents
) {
472 int events
= EV_PERSIST
;
473 php_http_request_pool_event_t
*ev
= assign_data
;
474 TSRMLS_FETCH_FROM_CTX(pool
->ts
);
477 ev
= ecalloc(1, sizeof(php_http_request_pool_event_t
));
479 curl_multi_assign(curl
->handle
, sock
, ev
);
480 event_base_set(PHP_HTTP_G
->curl
.event_base
, &ev
->evnt
);
482 event_del(&ev
->evnt
);
492 case CURL_POLL_INOUT
:
493 events
|= EV_READ
|EV_WRITE
;
496 case CURL_POLL_REMOVE
:
503 php_http_error(HE_WARNING
, PHP_HTTP_E_SOCKET
, "Unknown socket action %d", action
);
507 event_set(&ev
->evnt
, sock
, events
, php_http_curl_request_pool_event_callback
, pool
);
508 event_add(&ev
->evnt
, NULL
);
514 static void php_http_curl_request_pool_timer_callback(CURLM
*multi
, long timeout_ms
, void *timer_data
)
516 php_http_request_pool_t
*pool
= timer_data
;
517 php_http_curl_request_pool_t
*curl
= pool
->ctx
;
520 fprintf(stderr
, "%ld", timeout_ms
);
522 if (curl
->useevents
) {
524 if (timeout_ms
< 0) {
525 php_http_curl_request_pool_timeout_callback(CURL_SOCKET_TIMEOUT
, CURL_CSELECT_IN
|CURL_CSELECT_OUT
, pool
);
526 } else if (timeout_ms
> 0 || !event_initialized(curl
->timeout
) || !event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
527 struct timeval timeout
;
528 TSRMLS_FETCH_FROM_CTX(pool
->ts
);
530 if (!event_initialized(curl
->timeout
)) {
531 event_set(curl
->timeout
, -1, 0, php_http_curl_request_pool_timeout_callback
, pool
);
532 event_base_set(PHP_HTTP_G
->curl
.event_base
, curl
->timeout
);
533 } else if (event_pending(curl
->timeout
, EV_TIMEOUT
, NULL
)) {
534 event_del(curl
->timeout
);
537 timeout
.tv_sec
= timeout_ms
/ 1000;
538 timeout
.tv_usec
= (timeout_ms
% 1000) * 1000;
540 event_add(curl
->timeout
, &timeout
);
545 #endif /* HAVE_EVENT */
548 static inline zval
*cache_option(HashTable
*cache
, char *key
, size_t keylen
, ulong h
, zval
*opt
)
553 zend_hash_quick_update(cache
, key
, keylen
, h
, &opt
, sizeof(zval
*), NULL
);
555 zend_hash_update(cache
, key
, keylen
, &opt
, sizeof(zval
*), NULL
);
561 static inline zval
*get_option(HashTable
*cache
, HashTable
*options
, char *key
, size_t keylen
, int type
)
565 ulong h
= zend_hash_func(key
, keylen
);
567 if (SUCCESS
== zend_hash_quick_find(options
, key
, keylen
, h
, (void *) &zoption
)) {
568 zval
*option
= php_http_ztyp(type
, *zoption
);
571 zval
*cached
= cache_option(cache
, key
, keylen
, h
, option
);
573 zval_ptr_dtor(&option
);
583 static STATUS
set_options(php_http_request_t
*h
, HashTable
*options
)
587 php_http_curl_request_t
*curl
= h
->ctx
;
588 CURL
*ch
= curl
->handle
;
589 TSRMLS_FETCH_FROM_CTX(h
->ts
);
592 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxyhost"), IS_STRING
))) {
593 curl_easy_setopt(ch
, CURLOPT_PROXY
, Z_STRVAL_P(zoption
));
595 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxytype"), IS_LONG
))) {
596 curl_easy_setopt(ch
, CURLOPT_PROXYTYPE
, Z_LVAL_P(zoption
));
599 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxyport"), IS_LONG
))) {
600 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, Z_LVAL_P(zoption
));
603 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxyauth"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
604 curl_easy_setopt(ch
, CURLOPT_PROXYUSERPWD
, Z_STRVAL_P(zoption
));
607 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxyauthtype"), IS_LONG
))) {
608 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, Z_LVAL_P(zoption
));
611 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("proxytunnel"), IS_BOOL
)) && Z_BVAL_P(zoption
)) {
612 curl_easy_setopt(ch
, CURLOPT_HTTPPROXYTUNNEL
, 1L);
615 #if PHP_HTTP_CURL_VERSION(7,19,4)
616 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("noproxy"), IS_STRING
))) {
617 curl_easy_setopt(ch
, CURLOPT_NOPROXY
, Z_STRVAL_P(zoption
));
622 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("dns_cache_timeout"), IS_LONG
))) {
623 curl_easy_setopt(ch
, CURLOPT_DNS_CACHE_TIMEOUT
, Z_LVAL_P(zoption
));
625 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("ipresolve"), IS_LONG
)) && Z_LVAL_P(zoption
)) {
626 curl_easy_setopt(ch
, CURLOPT_IPRESOLVE
, Z_LVAL_P(zoption
));
628 #if PHP_HTTP_CURL_VERSION(7,21,3)
629 if (curl
->options
.resolve
) {
630 curl_slist_free_all(curl
->options
.resolve
);
631 curl
->options
.resolve
= NULL
;
633 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("resolve"), IS_ARRAY
))) {
634 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
638 FOREACH_KEYVAL(pos
, zoption
, key
, data
) {
639 zval
*cpy
= php_http_ztyp(IS_STRING
, *data
);
641 curl
->options
.resolve
= curl_slist_append(curl
->options
.resolve
, Z_STRVAL_P(cpy
));
647 #if PHP_HTTP_CURL_VERSION(7,24,0)
648 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("dns_servers"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
649 curl_easy_setopt(ch
, CURLOPT_DNS_SERVERS
, Z_STRVAL_P(zoption
));
654 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("low_speed_limit"), IS_LONG
))) {
655 curl_easy_setopt(ch
, CURLOPT_LOW_SPEED_LIMIT
, Z_LVAL_P(zoption
));
657 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("low_speed_time"), IS_LONG
))) {
658 curl_easy_setopt(ch
, CURLOPT_LOW_SPEED_TIME
, Z_LVAL_P(zoption
));
661 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_send_speed"), IS_LONG))) {
662 curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
664 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("max_recv_speed"), IS_LONG))) {
665 curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
669 if ((zoption = get_option(&curl->cache.options, options, ZEND_STRS("maxconnects"), IS_LONG))) {
670 curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption));
672 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("fresh_connect"), IS_BOOL
)) && Z_BVAL_P(zoption
)) {
673 curl_easy_setopt(ch
, CURLOPT_FRESH_CONNECT
, 1L);
675 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("forbid_reuse"), IS_BOOL
)) && Z_BVAL_P(zoption
)) {
676 curl_easy_setopt(ch
, CURLOPT_FORBID_REUSE
, 1L);
679 /* outgoing interface */
680 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("interface"), IS_STRING
))) {
681 curl_easy_setopt(ch
, CURLOPT_INTERFACE
, Z_STRVAL_P(zoption
));
683 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("portrange"), IS_ARRAY
))) {
686 zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption
));
687 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void *) &prs
)) {
688 zend_hash_move_forward(Z_ARRVAL_P(zoption
));
689 if (SUCCESS
== zend_hash_get_current_data(Z_ARRVAL_P(zoption
), (void *) &pre
)) {
690 zval
*prs_cpy
= php_http_ztyp(IS_LONG
, *prs
);
691 zval
*pre_cpy
= php_http_ztyp(IS_LONG
, *pre
);
693 if (Z_LVAL_P(prs_cpy
) && Z_LVAL_P(pre_cpy
)) {
694 curl_easy_setopt(ch
, CURLOPT_LOCALPORT
, MIN(Z_LVAL_P(prs_cpy
), Z_LVAL_P(pre_cpy
)));
695 curl_easy_setopt(ch
, CURLOPT_LOCALPORTRANGE
, labs(Z_LVAL_P(prs_cpy
)-Z_LVAL_P(pre_cpy
))+1L);
697 zval_ptr_dtor(&prs_cpy
);
698 zval_ptr_dtor(&pre_cpy
);
705 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("port"), IS_LONG
))) {
706 curl_easy_setopt(ch
, CURLOPT_PORT
, Z_LVAL_P(zoption
));
709 /* RFC4007 zone_id */
710 #if PHP_HTTP_CURL_VERSION(7,19,0)
711 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("address_scope"), IS_LONG
))) {
712 curl_easy_setopt(ch
, CURLOPT_ADDRESS_SCOPE
, Z_LVAL_P(zoption
));
717 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("httpauth"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
718 curl_easy_setopt(ch
, CURLOPT_USERPWD
, Z_STRVAL_P(zoption
));
720 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("httpauthtype"), IS_LONG
))) {
721 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, Z_LVAL_P(zoption
));
724 /* redirects, defaults to 0 */
725 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("redirect"), IS_LONG
))) {
726 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, Z_LVAL_P(zoption
) ? 1L : 0L);
727 curl_easy_setopt(ch
, CURLOPT_MAXREDIRS
, curl
->options
.redirects
= Z_LVAL_P(zoption
));
728 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("unrestrictedauth"), IS_BOOL
))) {
729 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, Z_LVAL_P(zoption
));
731 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("postredir"), IS_BOOL
))) {
732 #if PHP_HTTP_CURL_VERSION(7,19,1)
733 curl_easy_setopt(ch
, CURLOPT_POSTREDIR
, Z_BVAL_P(zoption
) ? 1L : 0L);
735 curl_easy_setopt(ch
, CURLOPT_POST301
, Z_BVAL_P(zoption
) ? 1L : 0L);
739 curl
->options
.redirects
= 0;
742 /* retries, defaults to 0 */
743 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("retrycount"), IS_LONG
))) {
744 curl
->options
.retry
.count
= Z_LVAL_P(zoption
);
745 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("retrydelay"), IS_DOUBLE
))) {
746 curl
->options
.retry
.delay
= Z_DVAL_P(zoption
);
748 curl
->options
.retry
.delay
= 0;
751 curl
->options
.retry
.count
= 0;
755 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("referer"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
756 curl_easy_setopt(ch
, CURLOPT_REFERER
, Z_STRVAL_P(zoption
));
759 /* useragent, default "PECL::HTTP/version (PHP/version)" */
760 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("useragent"), IS_STRING
))) {
761 /* allow to send no user agent, not even default one */
762 if (Z_STRLEN_P(zoption
)) {
763 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, Z_STRVAL_P(zoption
));
765 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, NULL
);
770 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("resume"), IS_LONG
)) && (Z_LVAL_P(zoption
) > 0)) {
772 curl_easy_setopt(ch
, CURLOPT_RESUME_FROM
, Z_LVAL_P(zoption
));
774 /* or range of kind array(array(0,499), array(100,1499)) */
775 else if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("range"), IS_ARRAY
)) && zend_hash_num_elements(Z_ARRVAL_P(zoption
))) {
776 HashPosition pos1
, pos2
;
777 zval
**rr
, **rb
, **re
;
778 php_http_buffer_t rs
;
780 php_http_buffer_init(&rs
);
781 FOREACH_VAL(pos1
, zoption
, rr
) {
782 if (Z_TYPE_PP(rr
) == IS_ARRAY
) {
783 zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr
), &pos2
);
784 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr
), (void *) &rb
, &pos2
)) {
785 zend_hash_move_forward_ex(Z_ARRVAL_PP(rr
), &pos2
);
786 if (SUCCESS
== zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr
), (void *) &re
, &pos2
)) {
787 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))) &&
788 ((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)))) {
789 zval
*rbl
= php_http_ztyp(IS_LONG
, *rb
);
790 zval
*rel
= php_http_ztyp(IS_LONG
, *re
);
792 if ((Z_LVAL_P(rbl
) >= 0) && (Z_LVAL_P(rel
) >= 0)) {
793 php_http_buffer_appendf(&rs
, "%ld-%ld,", Z_LVAL_P(rbl
), Z_LVAL_P(rel
));
803 if (PHP_HTTP_BUFFER_LEN(&rs
)) {
806 /* ditch last comma */
807 PHP_HTTP_BUFFER_VAL(&rs
)[PHP_HTTP_BUFFER_LEN(&rs
)-- -1] = '\0';
809 MAKE_STD_ZVAL(cached_range
);
810 ZVAL_STRINGL(cached_range
, PHP_HTTP_BUFFER_VAL(&rs
), PHP_HTTP_BUFFER_LEN(&rs
), 0);
811 curl_easy_setopt(ch
, CURLOPT_RANGE
, Z_STRVAL_P(cache_option(&curl
->options
.cache
, ZEND_STRS("range"), 0, cached_range
)));
812 zval_ptr_dtor(&cached_range
);
816 /* additional headers, array('name' => 'value') */
817 if (curl
->options
.headers
) {
818 curl_slist_free_all(curl
->options
.headers
);
819 curl
->options
.headers
= NULL
;
821 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("headers"), IS_ARRAY
))) {
822 php_http_array_hashkey_t header_key
= php_http_array_hashkey_init(0);
825 php_http_buffer_t header
;
827 php_http_buffer_init(&header
);
828 FOREACH_KEYVAL(pos
, zoption
, header_key
, header_val
) {
829 if (header_key
.type
== HASH_KEY_IS_STRING
) {
830 zval
*header_cpy
= php_http_ztyp(IS_STRING
, *header_val
);
832 if (!strcasecmp(header_key
.str
, "range")) {
836 php_http_buffer_appendf(&header
, "%s: %s", header_key
.str
, Z_STRVAL_P(header_cpy
));
837 php_http_buffer_fix(&header
);
838 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, PHP_HTTP_BUFFER_VAL(&header
));
839 php_http_buffer_reset(&header
);
841 zval_ptr_dtor(&header_cpy
);
844 php_http_buffer_dtor(&header
);
847 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("etag"), IS_STRING
)) && Z_STRLEN_P(zoption
)) {
848 zend_bool is_quoted
= !((Z_STRVAL_P(zoption
)[0] != '"') || (Z_STRVAL_P(zoption
)[Z_STRLEN_P(zoption
)-1] != '"'));
849 php_http_buffer_t header
;
851 php_http_buffer_init(&header
);
852 php_http_buffer_appendf(&header
, is_quoted
?"%s: %s":"%s: \"%s\"", range_req
?"If-Match":"If-None-Match", Z_STRVAL_P(zoption
));
853 php_http_buffer_fix(&header
);
854 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, PHP_HTTP_BUFFER_VAL(&header
));
855 php_http_buffer_dtor(&header
);
858 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("compress"), IS_BOOL
)) && Z_LVAL_P(zoption
)) {
859 curl
->options
.headers
= curl_slist_append(curl
->options
.headers
, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5");
861 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, curl
->options
.headers
);
864 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("lastmodified"), IS_LONG
))) {
865 if (Z_LVAL_P(zoption
)) {
866 if (Z_LVAL_P(zoption
) > 0) {
867 curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, Z_LVAL_P(zoption
));
869 curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, (long) PHP_HTTP_G
->env
.request
.time
+ Z_LVAL_P(zoption
));
871 curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, (long) (range_req
? CURL_TIMECOND_IFUNMODSINCE
: CURL_TIMECOND_IFMODSINCE
));
873 curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, CURL_TIMECOND_NONE
);
877 /* cookies, array('name' => 'value') */
878 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("cookies"), IS_ARRAY
))) {
879 php_http_buffer_dtor(&curl
->options
.cookies
);
880 if (zend_hash_num_elements(Z_ARRVAL_P(zoption
))) {
881 zval
*urlenc_cookies
= NULL
;
882 /* check whether cookies should not be urlencoded; default is to urlencode them */
883 if ((!(urlenc_cookies
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("encodecookies"), IS_BOOL
))) || Z_BVAL_P(urlenc_cookies
)) {
884 if (SUCCESS
== php_http_url_encode_hash_ex(HASH_OF(zoption
), &curl
->options
.cookies
, ZEND_STRS(";"), ZEND_STRS("="), NULL
, 0 TSRMLS_CC
)) {
885 php_http_buffer_fix(&curl
->options
.cookies
);
886 curl_easy_setopt(ch
, CURLOPT_COOKIE
, curl
->options
.cookies
.data
);
890 php_http_array_hashkey_t cookie_key
= php_http_array_hashkey_init(0);
893 FOREACH_KEYVAL(pos
, zoption
, cookie_key
, cookie_val
) {
894 if (cookie_key
.type
== HASH_KEY_IS_STRING
) {
895 zval
*val
= php_http_ztyp(IS_STRING
, *cookie_val
);
896 php_http_buffer_appendf(&curl
->options
.cookies
, "%s=%s; ", cookie_key
.str
, Z_STRVAL_P(val
));
901 php_http_buffer_fix(&curl
->options
.cookies
);
902 if (PHP_HTTP_BUFFER_LEN(&curl
->options
.cookies
)) {
903 curl_easy_setopt(ch
, CURLOPT_COOKIE
, PHP_HTTP_BUFFER_VAL(&curl
->options
.cookies
));
909 /* don't load session cookies from cookiestore */
910 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("cookiesession"), IS_BOOL
)) && Z_BVAL_P(zoption
)) {
911 curl_easy_setopt(ch
, CURLOPT_COOKIESESSION
, 1L);
914 /* cookiestore, read initial cookies from that file and store cookies back into that file */
915 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("cookiestore"), IS_STRING
))) {
916 php_http_curl_request_storage_t
*storage
= get_storage(curl
->handle
);
918 if (Z_STRLEN_P(zoption
)) {
919 if (SUCCESS
!= php_check_open_basedir(Z_STRVAL_P(zoption
) TSRMLS_CC
)) {
923 if (storage
->cookiestore
) {
924 pefree(storage
->cookiestore
, 1);
926 storage
->cookiestore
= pestrndup(Z_STRVAL_P(zoption
), Z_STRLEN_P(zoption
), 1);
927 curl_easy_setopt(ch
, CURLOPT_COOKIEFILE
, storage
->cookiestore
);
928 curl_easy_setopt(ch
, CURLOPT_COOKIEJAR
, storage
->cookiestore
);
932 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("maxfilesize"), IS_LONG
))) {
933 curl_easy_setopt(ch
, CURLOPT_MAXFILESIZE
, Z_LVAL_P(zoption
));
937 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("protocol"), IS_LONG
))) {
938 curl_easy_setopt(ch
, CURLOPT_HTTP_VERSION
, Z_LVAL_P(zoption
));
941 /* timeout, defaults to 0 */
942 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("timeout"), IS_DOUBLE
))) {
943 curl_easy_setopt(ch
, CURLOPT_TIMEOUT_MS
, (long)(Z_DVAL_P(zoption
)*1000));
945 /* connecttimeout, defaults to 0 */
946 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("connecttimeout"), IS_DOUBLE
))) {
947 curl_easy_setopt(ch
, CURLOPT_CONNECTTIMEOUT_MS
, (long)(Z_DVAL_P(zoption
)*1000));
951 if ((zoption
= get_option(&curl
->options
.cache
, options
, ZEND_STRS("ssl"), IS_ARRAY
))) {
952 php_http_array_hashkey_t key
= php_http_array_hashkey_init(0);
956 FOREACH_KEYVAL(pos
, zoption
, key
, param
) {
957 if (key
.type
== HASH_KEY_IS_STRING
) {
958 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT
, 0, 1);
959 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE
, 0, 0);
960 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD
, 0, 0);
962 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY
, 0, 0);
963 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE
, 0, 0);
964 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD
, 0, 0);
966 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE
, 0, 0);
967 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION
, 0);
969 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER
, 1);
970 PHP_HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST
, 1);
971 PHP_HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST
, 1, 0);
973 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAINFO
, -3, 1);
974 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CAPATH
, -3, 1);
975 PHP_HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE
, -3, 1);
976 PHP_HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET
, -3, 1);
977 #if PHP_HTTP_CURL_VERSION(7,19,0)
978 PHP_HTTP_CURL_OPT_STRING(CURLOPT_ISSUERCERT
, -3, 1);
979 #if defined(PHP_HTTP_HAVE_OPENSSL)
980 PHP_HTTP_CURL_OPT_STRING(CURLOPT_CRLFILE
, -3, 1);
983 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
984 PHP_HTTP_CURL_OPT_LONG(CURLOPT_CERTINFO
, -3);
992 static STATUS
get_info(CURL
*ch
, HashTable
*info
)
997 struct curl_slist
*s
, *p
;
998 zval
*subarray
, array
;
999 INIT_PZVAL_ARRAY(&array
, info
);
1001 /* BEGIN::CURLINFO */
1002 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_EFFECTIVE_URL
, &c
)) {
1003 add_assoc_string_ex(&array
, "effective_url", sizeof("effective_url"), c
? c
: "", 1);
1005 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_RESPONSE_CODE
, &l
)) {
1006 add_assoc_long_ex(&array
, "response_code", sizeof("response_code"), l
);
1008 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_TOTAL_TIME
, &d
)) {
1009 add_assoc_double_ex(&array
, "total_time", sizeof("total_time"), d
);
1011 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NAMELOOKUP_TIME
, &d
)) {
1012 add_assoc_double_ex(&array
, "namelookup_time", sizeof("namelookup_time"), d
);
1014 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONNECT_TIME
, &d
)) {
1015 add_assoc_double_ex(&array
, "connect_time", sizeof("connect_time"), d
);
1017 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRETRANSFER_TIME
, &d
)) {
1018 add_assoc_double_ex(&array
, "pretransfer_time", sizeof("pretransfer_time"), d
);
1020 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_UPLOAD
, &d
)) {
1021 add_assoc_double_ex(&array
, "size_upload", sizeof("size_upload"), d
);
1023 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SIZE_DOWNLOAD
, &d
)) {
1024 add_assoc_double_ex(&array
, "size_download", sizeof("size_download"), d
);
1026 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_DOWNLOAD
, &d
)) {
1027 add_assoc_double_ex(&array
, "speed_download", sizeof("speed_download"), d
);
1029 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SPEED_UPLOAD
, &d
)) {
1030 add_assoc_double_ex(&array
, "speed_upload", sizeof("speed_upload"), d
);
1032 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HEADER_SIZE
, &l
)) {
1033 add_assoc_long_ex(&array
, "header_size", sizeof("header_size"), l
);
1035 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REQUEST_SIZE
, &l
)) {
1036 add_assoc_long_ex(&array
, "request_size", sizeof("request_size"), l
);
1038 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_VERIFYRESULT
, &l
)) {
1039 add_assoc_long_ex(&array
, "ssl_verifyresult", sizeof("ssl_verifyresult"), l
);
1041 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_FILETIME
, &l
)) {
1042 add_assoc_long_ex(&array
, "filetime", sizeof("filetime"), l
);
1044 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_DOWNLOAD
, &d
)) {
1045 add_assoc_double_ex(&array
, "content_length_download", sizeof("content_length_download"), d
);
1047 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_LENGTH_UPLOAD
, &d
)) {
1048 add_assoc_double_ex(&array
, "content_length_upload", sizeof("content_length_upload"), d
);
1050 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_STARTTRANSFER_TIME
, &d
)) {
1051 add_assoc_double_ex(&array
, "starttransfer_time", sizeof("starttransfer_time"), d
);
1053 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONTENT_TYPE
, &c
)) {
1054 add_assoc_string_ex(&array
, "content_type", sizeof("content_type"), c
? c
: "", 1);
1056 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_TIME
, &d
)) {
1057 add_assoc_double_ex(&array
, "redirect_time", sizeof("redirect_time"), d
);
1059 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_COUNT
, &l
)) {
1060 add_assoc_long_ex(&array
, "redirect_count", sizeof("redirect_count"), l
);
1062 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTP_CONNECTCODE
, &l
)) {
1063 add_assoc_long_ex(&array
, "connect_code", sizeof("connect_code"), l
);
1065 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_HTTPAUTH_AVAIL
, &l
)) {
1066 add_assoc_long_ex(&array
, "httpauth_avail", sizeof("httpauth_avail"), l
);
1068 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PROXYAUTH_AVAIL
, &l
)) {
1069 add_assoc_long_ex(&array
, "proxyauth_avail", sizeof("proxyauth_avail"), l
);
1071 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_OS_ERRNO
, &l
)) {
1072 add_assoc_long_ex(&array
, "os_errno", sizeof("os_errno"), l
);
1074 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_NUM_CONNECTS
, &l
)) {
1075 add_assoc_long_ex(&array
, "num_connects", sizeof("num_connects"), l
);
1077 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_SSL_ENGINES
, &s
)) {
1078 MAKE_STD_ZVAL(subarray
);
1079 array_init(subarray
);
1080 for (p
= s
; p
; p
= p
->next
) {
1082 add_next_index_string(subarray
, p
->data
, 1);
1085 add_assoc_zval_ex(&array
, "ssl_engines", sizeof("ssl_engines"), subarray
);
1086 curl_slist_free_all(s
);
1088 #if PHP_HTTP_CURL_VERSION(7,14,1)
1089 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_COOKIELIST
, &s
)) {
1090 MAKE_STD_ZVAL(subarray
);
1091 array_init(subarray
);
1092 for (p
= s
; p
; p
= p
->next
) {
1094 add_next_index_string(subarray
, p
->data
, 1);
1097 add_assoc_zval_ex(&array
, "cookies", sizeof("cookies"), subarray
);
1098 curl_slist_free_all(s
);
1101 #if PHP_HTTP_CURL_VERSION(7,18,2)
1102 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_REDIRECT_URL
, &c
)) {
1103 add_assoc_string_ex(&array
, "redirect_url", sizeof("redirect_url"), c
? c
: "", 1);
1106 #if PHP_HTTP_CURL_VERSION(7,19,0)
1107 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_PRIMARY_IP
, &c
)) {
1108 add_assoc_string_ex(&array
, "primary_ip", sizeof("primary_ip"), c
? c
: "", 1);
1111 #if PHP_HTTP_CURL_VERSION(7,19,0)
1112 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_APPCONNECT_TIME
, &d
)) {
1113 add_assoc_double_ex(&array
, "appconnect_time", sizeof("appconnect_time"), d
);
1116 #if PHP_HTTP_CURL_VERSION(7,19,4)
1117 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CONDITION_UNMET
, &l
)) {
1118 add_assoc_long_ex(&array
, "condition_unmet", sizeof("condition_unmet"), l
);
1122 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
1126 struct curl_certinfo
*ci
;
1127 char *colon
, *keyname
;
1129 if (CURLE_OK
== curl_easy_getinfo(ch
, CURLINFO_CERTINFO
, &ci
)) {
1130 MAKE_STD_ZVAL(ci_array
);
1131 array_init(ci_array
);
1133 for (i
= 0; i
< ci
->num_of_certs
; ++i
) {
1134 s
= ci
->certinfo
[i
];
1136 MAKE_STD_ZVAL(subarray
);
1137 array_init(subarray
);
1138 for (p
= s
; p
; p
= p
->next
) {
1140 if ((colon
= strchr(p
->data
, ':'))) {
1141 keyname
= estrndup(p
->data
, colon
- p
->data
);
1142 add_assoc_string_ex(subarray
, keyname
, colon
- p
->data
+ 1, colon
+ 1, 1);
1145 add_next_index_string(subarray
, p
->data
, 1);
1149 add_next_index_zval(ci_array
, subarray
);
1151 add_assoc_zval_ex(&array
, "certinfo", sizeof("certinfo"), ci_array
);
1155 add_assoc_string_ex(&array
, "error", sizeof("error"), get_storage(ch
)->errorbuffer
, 1);
1161 /* request datashare handler ops */
1163 static php_http_request_datashare_t
*php_http_curl_request_datashare_init(php_http_request_datashare_t
*h
, void *handle
)
1165 php_http_curl_request_datashare_t
*curl
;
1166 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1168 if (!handle
&& !(handle
= php_http_resource_factory_handle_ctor(h
->rf TSRMLS_CC
))) {
1169 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_DATASHARE
, "could not initialize curl share handle");
1173 curl
= ecalloc(1, sizeof(*curl
));
1174 curl
->handle
= handle
;
1180 static void php_http_curl_request_datashare_dtor(php_http_request_datashare_t
*h
)
1182 php_http_curl_request_datashare_t
*curl
= h
->ctx
;
1183 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1185 php_http_resource_factory_handle_dtor(h
->rf
, curl
->handle TSRMLS_CC
);
1191 static STATUS
php_http_curl_request_datashare_attach(php_http_request_datashare_t
*h
, php_http_request_t
*r
)
1194 php_http_curl_request_datashare_t
*curl
= h
->ctx
;
1195 php_http_curl_request_t
*recurl
= r
->ctx
;
1196 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1198 if (CURLE_OK
!= (rc
= curl_easy_setopt(recurl
->handle
, CURLOPT_SHARE
, curl
->handle
))) {
1199 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_DATASHARE
, "Could not attach request to the datashare: %s", curl_easy_strerror(rc
));
1205 static STATUS
php_http_curl_request_datashare_detach(php_http_request_datashare_t
*h
, php_http_request_t
*r
)
1208 php_http_curl_request_t
*recurl
= r
->ctx
;
1209 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1212 if (CURLE_OK
!= (rc
= curl_easy_setopt(recurl
->handle
, CURLOPT_SHARE
, NULL
))) {
1213 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_DATASHARE
, "Could not detach request from the datashare: %s", curl_share_strerror(rc
));
1219 static STATUS
php_http_curl_request_datashare_setopt(php_http_request_datashare_t
*h
, php_http_request_datashare_setopt_opt_t opt
, void *arg
)
1222 php_http_curl_request_datashare_t
*curl
= h
->ctx
;
1225 case PHP_HTTP_REQUEST_DATASHARE_OPT_COOKIES
:
1226 if (CURLSHE_OK
!= (rc
= curl_share_setopt(curl
->handle
, *((zend_bool
*) arg
) ? CURLSHOPT_SHARE
: CURLSHOPT_UNSHARE
, CURL_LOCK_DATA_COOKIE
))) {
1227 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1229 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
));
1234 case PHP_HTTP_REQUEST_DATASHARE_OPT_RESOLVER
:
1235 if (CURLSHE_OK
!= (rc
= curl_share_setopt(curl
->handle
, *((zend_bool
*) arg
) ? CURLSHOPT_SHARE
: CURLSHOPT_UNSHARE
, CURL_LOCK_DATA_DNS
))) {
1236 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1238 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
));
1243 #if PHP_HTTP_CURL_VERSION(7,23,0)
1244 case PHP_HTTP_REQUEST_DATASHARE_OPT_SSLSESSIONS
:
1245 if (CURLSHE_OK
!= (rc
= curl_share_setopt(curl
->handle
, *((zend_bool
*) arg
) ? CURLSHOPT_SHARE
: CURLSHOPT_UNSHARE
, CURL_LOCK_DATA_SSL_SESSION
))) {
1246 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1248 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_DATASHARE
, "Could not %s sharing of SSL session data: %s", *((zend_bool
*) arg
) ? "enable" : "disable", curl_share_strerror(rc
));
1261 static php_http_resource_factory_ops_t php_http_curlsh_resource_factory_ops
= {
1262 php_http_curlsh_ctor
,
1264 php_http_curlsh_dtor
1267 static php_http_request_datashare_ops_t php_http_curl_request_datashare_ops
= {
1268 &php_http_curlsh_resource_factory_ops
,
1269 php_http_curl_request_datashare_init
,
1271 php_http_curl_request_datashare_dtor
,
1273 php_http_curl_request_datashare_attach
,
1274 php_http_curl_request_datashare_detach
,
1275 php_http_curl_request_datashare_setopt
,
1278 PHP_HTTP_API php_http_request_datashare_ops_t
*php_http_curl_get_request_datashare_ops(void)
1280 return &php_http_curl_request_datashare_ops
;
1284 /* request pool handler ops */
1286 static php_http_request_pool_t
*php_http_curl_request_pool_init(php_http_request_pool_t
*h
, void *handle
)
1288 php_http_curl_request_pool_t
*curl
;
1289 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1291 if (!handle
&& !(handle
= php_http_resource_factory_handle_ctor(h
->rf TSRMLS_CC
))) {
1292 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_POOL
, "could not initialize curl pool handle");
1296 curl
= ecalloc(1, sizeof(*curl
));
1297 curl
->handle
= handle
;
1298 curl
->unfinished
= 0;
1304 static void php_http_curl_request_pool_dtor(php_http_request_pool_t
*h
)
1306 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1307 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1309 #if PHP_HTTP_HAVE_EVENT
1310 if (curl
->timeout
) {
1311 efree(curl
->timeout
);
1312 curl
->timeout
= NULL
;
1315 curl
->unfinished
= 0;
1316 php_http_request_pool_reset(h
);
1318 php_http_resource_factory_handle_dtor(h
->rf
, curl
->handle TSRMLS_CC
);
1324 static STATUS
php_http_curl_request_pool_attach(php_http_request_pool_t
*h
, php_http_request_t
*r
, const char *m
, const char *url
, php_http_message_body_t
*body
)
1326 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1327 php_http_curl_request_t
*recurl
= r
->ctx
;
1329 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1331 if (SUCCESS
!= php_http_curl_request_prepare(r
, m
, url
, body
)) {
1335 if (CURLM_OK
== (rs
= curl_multi_add_handle(curl
->handle
, recurl
->handle
))) {
1339 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_POOL
, "Could not attach request to pool: %s", curl_multi_strerror(rs
));
1344 static STATUS
php_http_curl_request_pool_detach(php_http_request_pool_t
*h
, php_http_request_t
*r
)
1346 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1347 php_http_curl_request_t
*recurl
= r
->ctx
;
1348 CURLMcode rs
= curl_multi_remove_handle(curl
->handle
, recurl
->handle
);
1349 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1351 if (CURLM_OK
== rs
) {
1354 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST_POOL
, "Could not detach request from pool: %s", curl_multi_strerror(rs
));
1360 # define SELECT_ERROR SOCKET_ERROR
1362 # define SELECT_ERROR -1
1365 static STATUS
php_http_curl_request_pool_wait(php_http_request_pool_t
*h
, struct timeval
*custom_timeout
)
1369 struct timeval timeout
;
1370 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1372 #if PHP_HTTP_HAVE_EVENT
1373 if (curl
->useevents
) {
1374 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1376 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "not implemented");
1381 if (custom_timeout
&& timerisset(custom_timeout
)) {
1382 timeout
= *custom_timeout
;
1384 long max_tout
= 1000;
1386 if ((CURLM_OK
== curl_multi_timeout(curl
->handle
, &max_tout
)) && (max_tout
> 0)) {
1387 timeout
.tv_sec
= max_tout
/ 1000;
1388 timeout
.tv_usec
= (max_tout
% 1000) * 1000;
1391 timeout
.tv_usec
= 1000;
1399 if (CURLM_OK
== curl_multi_fdset(curl
->handle
, &R
, &W
, &E
, &MAX
)) {
1401 php_http_sleep((double) timeout
.tv_sec
+ (double) (timeout
.tv_usec
/ PHP_HTTP_MCROSEC
));
1403 } else if (SELECT_ERROR
!= select(MAX
+ 1, &R
, &W
, &E
, &timeout
)) {
1410 static int php_http_curl_request_pool_once(php_http_request_pool_t
*h
)
1412 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1414 #if PHP_HTTP_HAVE_EVENT
1415 if (curl
->useevents
) {
1416 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1417 php_http_error(HE_WARNING
, PHP_HTTP_E_RUNTIME
, "not implemented");
1422 while (CURLM_CALL_MULTI_PERFORM
== curl_multi_perform(curl
->handle
, &curl
->unfinished
));
1424 php_http_curl_request_pool_responsehandler(h
);
1426 return curl
->unfinished
;
1429 #if PHP_HTTP_HAVE_EVENT
1430 static void dolog(int i
, const char *m
) {
1431 fprintf(stderr
, "%d: %s\n", i
, m
);
1434 static STATUS
php_http_curl_request_pool_exec(php_http_request_pool_t
*h
)
1436 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1438 #if PHP_HTTP_HAVE_EVENT
1439 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1441 if (curl
->useevents
) {
1442 event_set_log_callback(dolog
);
1445 fprintf(stderr
, "X");
1447 event_base_dispatch(PHP_HTTP_G
->curl
.event_base
);
1448 } while (curl
->unfinished
);
1452 while (php_http_curl_request_pool_once(h
)) {
1453 if (SUCCESS
!= php_http_curl_request_pool_wait(h
, NULL
)) {
1455 /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
1456 php_http_error(HE_WARNING
, PHP_HTTP_E_SOCKET
, "WinSock error: %d", WSAGetLastError());
1458 php_http_error(HE_WARNING
, PHP_HTTP_E_SOCKET
, strerror(errno
));
1468 static STATUS
php_http_curl_request_pool_setopt(php_http_request_pool_t
*h
, php_http_request_pool_setopt_opt_t opt
, void *arg
)
1470 php_http_curl_request_pool_t
*curl
= h
->ctx
;
1473 case PHP_HTTP_REQUEST_POOL_OPT_ENABLE_PIPELINING
:
1474 if (CURLM_OK
!= curl_multi_setopt(curl
->handle
, CURLMOPT_PIPELINING
, (long) *((zend_bool
*) arg
))) {
1479 case PHP_HTTP_REQUEST_POOL_OPT_USE_EVENTS
:
1480 #if PHP_HTTP_HAVE_EVENT
1481 if ((curl
->useevents
= *((zend_bool
*) arg
))) {
1482 if (!curl
->timeout
) {
1483 curl
->timeout
= ecalloc(1, sizeof(struct event
));
1485 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETDATA
, h
);
1486 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETFUNCTION
, php_http_curl_request_pool_socket_callback
);
1487 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERDATA
, h
);
1488 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERFUNCTION
, php_http_curl_request_pool_timer_callback
);
1490 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETDATA
, NULL
);
1491 curl_multi_setopt(curl
->handle
, CURLMOPT_SOCKETFUNCTION
, NULL
);
1492 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERDATA
, NULL
);
1493 curl_multi_setopt(curl
->handle
, CURLMOPT_TIMERFUNCTION
, NULL
);
1504 static php_http_resource_factory_ops_t php_http_curlm_resource_factory_ops
= {
1505 php_http_curlm_ctor
,
1510 static php_http_request_pool_ops_t php_http_curl_request_pool_ops
= {
1511 &php_http_curlm_resource_factory_ops
,
1512 php_http_curl_request_pool_init
,
1514 php_http_curl_request_pool_dtor
,
1516 php_http_curl_request_pool_exec
,
1517 php_http_curl_request_pool_wait
,
1518 php_http_curl_request_pool_once
,
1519 php_http_curl_request_pool_attach
,
1520 php_http_curl_request_pool_detach
,
1521 php_http_curl_request_pool_setopt
,
1524 PHP_HTTP_API php_http_request_pool_ops_t
*php_http_curl_get_request_pool_ops(void)
1526 return &php_http_curl_request_pool_ops
;
1529 /* request handler ops */
1531 static STATUS
php_http_curl_request_reset(php_http_request_t
*h
);
1533 static php_http_request_t
*php_http_curl_request_init(php_http_request_t
*h
, void *handle
)
1535 php_http_curl_request_t
*ctx
;
1536 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1538 if (!handle
&& !(handle
= php_http_resource_factory_handle_ctor(h
->rf TSRMLS_CC
))) {
1539 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST
, "could not initialize curl handle");
1543 ctx
= ecalloc(1, sizeof(*ctx
));
1544 ctx
->handle
= handle
;
1545 php_http_buffer_init(&ctx
->options
.cookies
);
1546 zend_hash_init(&ctx
->options
.cache
, 0, NULL
, ZVAL_PTR_DTOR
, 0);
1550 curl_easy_setopt(handle
, CURLOPT_NOSIGNAL
, 1L);
1552 curl_easy_setopt(handle
, CURLOPT_HEADER
, 0L);
1553 curl_easy_setopt(handle
, CURLOPT_FILETIME
, 1L);
1554 curl_easy_setopt(handle
, CURLOPT_AUTOREFERER
, 1L);
1555 curl_easy_setopt(handle
, CURLOPT_VERBOSE
, 1L);
1556 curl_easy_setopt(handle
, CURLOPT_NOPROGRESS
, 0L);
1557 curl_easy_setopt(handle
, CURLOPT_HEADERFUNCTION
, NULL
);
1558 curl_easy_setopt(handle
, CURLOPT_WRITEFUNCTION
, php_http_curl_dummy_callback
);
1559 curl_easy_setopt(handle
, CURLOPT_DEBUGFUNCTION
, php_http_curl_raw_callback
);
1560 curl_easy_setopt(handle
, CURLOPT_READFUNCTION
, php_http_curl_read_callback
);
1561 curl_easy_setopt(handle
, CURLOPT_IOCTLFUNCTION
, php_http_curl_ioctl_callback
);
1562 curl_easy_setopt(handle
, CURLOPT_PROGRESSFUNCTION
, php_http_curl_progress_callback
);
1563 curl_easy_setopt(handle
, CURLOPT_DEBUGDATA
, h
);
1564 curl_easy_setopt(handle
, CURLOPT_PROGRESSDATA
, h
);
1566 php_http_curl_request_reset(h
);
1571 static php_http_request_t
*php_http_curl_request_copy(php_http_request_t
*from
, php_http_request_t
*to
)
1573 php_http_curl_request_t
*ctx
= from
->ctx
;
1575 TSRMLS_FETCH_FROM_CTX(from
->ts
);
1577 if (!(copy
= php_http_resource_factory_handle_copy(from
->rf
, ctx
->handle TSRMLS_CC
))) {
1582 return php_http_curl_request_init(to
, copy
);
1584 return php_http_request_init(NULL
, from
->ops
, from
->rf
, copy TSRMLS_CC
);
1588 static void php_http_curl_request_dtor(php_http_request_t
*h
)
1590 php_http_curl_request_t
*ctx
= h
->ctx
;
1591 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1593 curl_easy_setopt(ctx
->handle
, CURLOPT_NOPROGRESS
, 1L);
1594 curl_easy_setopt(ctx
->handle
, CURLOPT_PROGRESSFUNCTION
, NULL
);
1595 curl_easy_setopt(ctx
->handle
, CURLOPT_VERBOSE
, 0L);
1596 curl_easy_setopt(ctx
->handle
, CURLOPT_DEBUGFUNCTION
, NULL
);
1598 php_http_resource_factory_handle_dtor(h
->rf
, ctx
->handle TSRMLS_CC
);
1600 php_http_buffer_dtor(&ctx
->options
.cookies
);
1601 zend_hash_destroy(&ctx
->options
.cache
);
1603 if (ctx
->options
.headers
) {
1604 curl_slist_free_all(ctx
->options
.headers
);
1605 ctx
->options
.headers
= NULL
;
1607 php_http_request_progress_dtor(&ctx
->progress TSRMLS_CC
);
1612 static STATUS
php_http_curl_request_reset(php_http_request_t
*h
)
1614 CURL
*ch
= ((php_http_curl_request_t
*) h
->ctx
)->handle
;
1615 php_http_curl_request_storage_t
*st
;
1617 if ((st
= get_storage(ch
))) {
1622 if (st
->cookiestore
) {
1623 pefree(st
->cookiestore
, 1);
1624 st
->cookiestore
= NULL
;
1626 st
->errorbuffer
[0] = '\0';
1629 curl_easy_setopt(ch
, CURLOPT_URL
, NULL
);
1630 #if PHP_HTTP_CURL_VERSION(7,19,4)
1631 curl_easy_setopt(ch
, CURLOPT_NOPROXY
, NULL
);
1633 curl_easy_setopt(ch
, CURLOPT_PROXY
, NULL
);
1634 curl_easy_setopt(ch
, CURLOPT_PROXYPORT
, 0L);
1635 curl_easy_setopt(ch
, CURLOPT_PROXYTYPE
, 0L);
1636 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1637 #if PHP_HTTP_CURL_VERSION(7,19,1)
1638 curl_easy_setopt(ch
, CURLOPT_PROXYUSERNAME
, NULL
);
1639 curl_easy_setopt(ch
, CURLOPT_PROXYPASSWORD
, NULL
);
1641 curl_easy_setopt(ch
, CURLOPT_PROXYAUTH
, 0L);
1642 curl_easy_setopt(ch
, CURLOPT_HTTPPROXYTUNNEL
, 0L);
1643 curl_easy_setopt(ch
, CURLOPT_DNS_CACHE_TIMEOUT
, 60L);
1644 curl_easy_setopt(ch
, CURLOPT_IPRESOLVE
, 0);
1645 #if PHP_HTTP_CURL_VERSION(7,21,3)
1646 curl_easy_setopt(ch
, CURLOPT_RESOLVE
, NULL
);
1648 #if PHP_HTTP_CURL_VERSION(7,24,0)
1649 curl_easy_setopt(ch
, CURLOPT_DNS_SERVERS
, NULL
);
1651 curl_easy_setopt(ch
, CURLOPT_LOW_SPEED_LIMIT
, 0L);
1652 curl_easy_setopt(ch
, CURLOPT_LOW_SPEED_TIME
, 0L);
1654 curl_easy_setopt(ch, CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) 0);
1655 curl_easy_setopt(ch, CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) 0);
1658 curl_easy_setopt(ch, CURLOPT_MAXCONNECTS, 5L); */
1659 curl_easy_setopt(ch
, CURLOPT_FRESH_CONNECT
, 0L);
1660 curl_easy_setopt(ch
, CURLOPT_FORBID_REUSE
, 0L);
1661 curl_easy_setopt(ch
, CURLOPT_INTERFACE
, NULL
);
1662 curl_easy_setopt(ch
, CURLOPT_PORT
, 0L);
1663 #if PHP_HTTP_CURL_VERSION(7,19,0)
1664 curl_easy_setopt(ch
, CURLOPT_ADDRESS_SCOPE
, 0L);
1666 curl_easy_setopt(ch
, CURLOPT_LOCALPORT
, 0L);
1667 curl_easy_setopt(ch
, CURLOPT_LOCALPORTRANGE
, 0L);
1668 /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
1669 #if PHP_HTTP_CURL_VERSION(7,19,1)
1670 curl_easy_setopt(ch
, CURLOPT_USERNAME
, NULL
);
1671 curl_easy_setopt(ch
, CURLOPT_PASSWORD
, NULL
);
1673 curl_easy_setopt(ch
, CURLOPT_HTTPAUTH
, 0L);
1674 curl_easy_setopt(ch
, CURLOPT_ENCODING
, NULL
);
1675 /* we do this ourself anyway */
1676 curl_easy_setopt(ch
, CURLOPT_HTTP_CONTENT_DECODING
, 0L);
1677 curl_easy_setopt(ch
, CURLOPT_HTTP_TRANSFER_DECODING
, 0L);
1678 curl_easy_setopt(ch
, CURLOPT_FOLLOWLOCATION
, 0L);
1679 #if PHP_HTTP_CURL_VERSION(7,19,1)
1680 curl_easy_setopt(ch
, CURLOPT_POSTREDIR
, 0L);
1682 curl_easy_setopt(ch
, CURLOPT_POST301
, 0L);
1684 curl_easy_setopt(ch
, CURLOPT_UNRESTRICTED_AUTH
, 0L);
1685 curl_easy_setopt(ch
, CURLOPT_REFERER
, NULL
);
1686 curl_easy_setopt(ch
, CURLOPT_USERAGENT
, "PECL::HTTP/" PHP_HTTP_EXT_VERSION
" (PHP/" PHP_VERSION
")");
1687 curl_easy_setopt(ch
, CURLOPT_HTTPHEADER
, NULL
);
1688 curl_easy_setopt(ch
, CURLOPT_COOKIE
, NULL
);
1689 curl_easy_setopt(ch
, CURLOPT_COOKIESESSION
, 0L);
1690 /* these options would enable curl's cookie engine by default which we don't want
1691 curl_easy_setopt(ch, CURLOPT_COOKIEFILE, NULL);
1692 curl_easy_setopt(ch, CURLOPT_COOKIEJAR, NULL); */
1693 curl_easy_setopt(ch
, CURLOPT_COOKIELIST
, NULL
);
1694 curl_easy_setopt(ch
, CURLOPT_RANGE
, NULL
);
1695 curl_easy_setopt(ch
, CURLOPT_RESUME_FROM
, 0L);
1696 curl_easy_setopt(ch
, CURLOPT_MAXFILESIZE
, 0L);
1697 curl_easy_setopt(ch
, CURLOPT_TIMECONDITION
, 0L);
1698 curl_easy_setopt(ch
, CURLOPT_TIMEVALUE
, 0L);
1699 curl_easy_setopt(ch
, CURLOPT_TIMEOUT
, 0L);
1700 curl_easy_setopt(ch
, CURLOPT_CONNECTTIMEOUT
, 3);
1701 curl_easy_setopt(ch
, CURLOPT_SSLCERT
, NULL
);
1702 curl_easy_setopt(ch
, CURLOPT_SSLCERTTYPE
, NULL
);
1703 curl_easy_setopt(ch
, CURLOPT_SSLCERTPASSWD
, NULL
);
1704 curl_easy_setopt(ch
, CURLOPT_SSLKEY
, NULL
);
1705 curl_easy_setopt(ch
, CURLOPT_SSLKEYTYPE
, NULL
);
1706 curl_easy_setopt(ch
, CURLOPT_SSLKEYPASSWD
, NULL
);
1707 curl_easy_setopt(ch
, CURLOPT_SSLENGINE
, NULL
);
1708 curl_easy_setopt(ch
, CURLOPT_SSLVERSION
, 0L);
1709 curl_easy_setopt(ch
, CURLOPT_SSL_VERIFYPEER
, 0L);
1710 curl_easy_setopt(ch
, CURLOPT_SSL_VERIFYHOST
, 0L);
1711 curl_easy_setopt(ch
, CURLOPT_SSL_CIPHER_LIST
, NULL
);
1712 #if PHP_HTTP_CURL_VERSION(7,19,0)
1713 curl_easy_setopt(ch
, CURLOPT_ISSUERCERT
, NULL
);
1714 #if defined(PHP_HTTP_HAVE_OPENSSL)
1715 curl_easy_setopt(ch
, CURLOPT_CRLFILE
, NULL
);
1718 #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
1719 curl_easy_setopt(ch
, CURLOPT_CERTINFO
, NULL
);
1721 #ifdef PHP_HTTP_CURL_CAINFO
1722 curl_easy_setopt(ch
, CURLOPT_CAINFO
, PHP_HTTP_CURL_CAINFO
);
1724 curl_easy_setopt(ch
, CURLOPT_CAINFO
, NULL
);
1726 curl_easy_setopt(ch
, CURLOPT_CAPATH
, NULL
);
1727 curl_easy_setopt(ch
, CURLOPT_RANDOM_FILE
, NULL
);
1728 curl_easy_setopt(ch
, CURLOPT_EGDSOCKET
, NULL
);
1729 curl_easy_setopt(ch
, CURLOPT_POSTFIELDS
, NULL
);
1730 curl_easy_setopt(ch
, CURLOPT_POSTFIELDSIZE
, 0L);
1731 curl_easy_setopt(ch
, CURLOPT_HTTPPOST
, NULL
);
1732 curl_easy_setopt(ch
, CURLOPT_IOCTLDATA
, NULL
);
1733 curl_easy_setopt(ch
, CURLOPT_READDATA
, NULL
);
1734 curl_easy_setopt(ch
, CURLOPT_INFILESIZE
, 0L);
1735 curl_easy_setopt(ch
, CURLOPT_HTTP_VERSION
, CURL_HTTP_VERSION_NONE
);
1736 curl_easy_setopt(ch
, CURLOPT_CUSTOMREQUEST
, NULL
);
1737 curl_easy_setopt(ch
, CURLOPT_NOBODY
, 0L);
1738 curl_easy_setopt(ch
, CURLOPT_POST
, 0L);
1739 curl_easy_setopt(ch
, CURLOPT_UPLOAD
, 0L);
1740 curl_easy_setopt(ch
, CURLOPT_HTTPGET
, 1L);
1745 static STATUS
php_http_curl_request_exec(php_http_request_t
*h
, const char *meth
, const char *url
, php_http_message_body_t
*body
)
1749 php_http_curl_request_t
*curl
= h
->ctx
;
1750 php_http_curl_request_storage_t
*storage
= get_storage(curl
->handle
);
1751 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1753 if (SUCCESS
!= php_http_curl_request_prepare(h
, meth
, url
, body
)) {
1758 if (CURLE_OK
!= (result
= curl_easy_perform(curl
->handle
))) {
1759 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST
, "%s; %s (%s)", curl_easy_strerror(result
), storage
->errorbuffer
, storage
->url
);
1761 if (EG(exception
)) {
1762 add_property_long(EG(exception
), "curlCode", result
);
1765 if (curl
->options
.retry
.count
> tries
++) {
1767 case CURLE_COULDNT_RESOLVE_PROXY
:
1768 case CURLE_COULDNT_RESOLVE_HOST
:
1769 case CURLE_COULDNT_CONNECT
:
1770 case CURLE_WRITE_ERROR
:
1771 case CURLE_READ_ERROR
:
1772 case CURLE_OPERATION_TIMEDOUT
:
1773 case CURLE_SSL_CONNECT_ERROR
:
1774 case CURLE_GOT_NOTHING
:
1775 case CURLE_SSL_ENGINE_SETFAILED
:
1776 case CURLE_SEND_ERROR
:
1777 case CURLE_RECV_ERROR
:
1778 case CURLE_SSL_ENGINE_INITFAILED
:
1779 case CURLE_LOGIN_DENIED
:
1780 if (curl
->options
.retry
.delay
>= PHP_HTTP_DIFFSEC
) {
1781 php_http_sleep(curl
->options
.retry
.delay
);
1795 static STATUS
php_http_curl_request_setopt(php_http_request_t
*h
, php_http_request_setopt_opt_t opt
, void *arg
)
1797 php_http_curl_request_t
*curl
= h
->ctx
;
1798 TSRMLS_FETCH_FROM_CTX(h
->ts
);
1801 case PHP_HTTP_REQUEST_OPT_SETTINGS
:
1802 return set_options(h
, arg
);
1805 case PHP_HTTP_REQUEST_OPT_PROGRESS_CALLBACK
:
1806 if (curl
->progress
.in_cb
) {
1807 php_http_error(HE_WARNING
, PHP_HTTP_E_REQUEST
, "Cannot change progress callback while executing it");
1810 if (curl
->progress
.callback
) {
1811 php_http_request_progress_dtor(&curl
->progress TSRMLS_CC
);
1813 curl
->progress
.callback
= arg
;
1816 case PHP_HTTP_REQUEST_OPT_COOKIES_ENABLE
:
1817 /* are cookies already enabled anyway? */
1818 if (!get_storage(curl
->handle
)->cookiestore
) {
1819 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIEFILE
, "")) {
1825 case PHP_HTTP_REQUEST_OPT_COOKIES_RESET
:
1826 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIELIST
, "ALL")) {
1831 case PHP_HTTP_REQUEST_OPT_COOKIES_RESET_SESSION
:
1832 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIELIST
, "SESS")) {
1837 case PHP_HTTP_REQUEST_OPT_COOKIES_FLUSH
:
1838 if (CURLE_OK
!= curl_easy_setopt(curl
->handle
, CURLOPT_COOKIELIST
, "FLUSH")) {
1850 static STATUS
php_http_curl_request_getopt(php_http_request_t
*h
, php_http_request_getopt_opt_t opt
, void *arg
)
1852 php_http_curl_request_t
*curl
= h
->ctx
;
1855 case PHP_HTTP_REQUEST_OPT_PROGRESS_INFO
:
1856 *((php_http_request_progress_t
**) arg
) = &curl
->progress
;
1859 case PHP_HTTP_REQUEST_OPT_TRANSFER_INFO
:
1860 get_info(curl
->handle
, arg
);
1870 static php_http_resource_factory_ops_t php_http_curl_resource_factory_ops
= {
1876 static php_http_request_ops_t php_http_curl_request_ops
= {
1877 &php_http_curl_resource_factory_ops
,
1878 php_http_curl_request_init
,
1879 php_http_curl_request_copy
,
1880 php_http_curl_request_dtor
,
1881 php_http_curl_request_reset
,
1882 php_http_curl_request_exec
,
1883 php_http_curl_request_setopt
,
1884 php_http_curl_request_getopt
1887 PHP_HTTP_API php_http_request_ops_t
*php_http_curl_get_request_ops(void)
1889 return &php_http_curl_request_ops
;
1892 #if defined(ZTS) && defined(PHP_HTTP_HAVE_SSL)
1894 # define PHP_HTTP_NEED_OPENSSL_TSL
1895 # include <openssl/crypto.h>
1896 # else /* !PHP_WIN32 */
1897 # if defined(PHP_HTTP_HAVE_OPENSSL)
1898 # define PHP_HTTP_NEED_OPENSSL_TSL
1899 # include <openssl/crypto.h>
1900 # elif defined(PHP_HTTP_HAVE_GNUTLS)
1901 # define PHP_HTTP_NEED_GNUTLS_TSL
1902 # include <gcrypt.h>
1905 "libcurl was compiled with SSL support, but configure could not determine which" \
1906 "library was used; thus no SSL crypto locking callbacks will be set, which may " \
1907 "cause random crashes on SSL requests"
1908 # endif /* PHP_HTTP_HAVE_OPENSSL || PHP_HTTP_HAVE_GNUTLS */
1909 # endif /* PHP_WIN32 */
1910 #endif /* ZTS && PHP_HTTP_HAVE_SSL */
1913 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
1914 static MUTEX_T
*php_http_openssl_tsl
= NULL
;
1916 static void php_http_openssl_thread_lock(int mode
, int n
, const char * file
, int line
)
1918 if (mode
& CRYPTO_LOCK
) {
1919 tsrm_mutex_lock(php_http_openssl_tsl
[n
]);
1921 tsrm_mutex_unlock(php_http_openssl_tsl
[n
]);
1925 static ulong
php_http_openssl_thread_id(void)
1927 return (ulong
) tsrm_thread_id();
1930 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
1931 static int php_http_gnutls_mutex_create(void **m
)
1933 if (*((MUTEX_T
*) m
) = tsrm_mutex_alloc()) {
1940 static int php_http_gnutls_mutex_destroy(void **m
)
1942 tsrm_mutex_free(*((MUTEX_T
*) m
));
1946 static int php_http_gnutls_mutex_lock(void **m
)
1948 return tsrm_mutex_lock(*((MUTEX_T
*) m
));
1951 static int php_http_gnutls_mutex_unlock(void **m
)
1953 return tsrm_mutex_unlock(*((MUTEX_T
*) m
));
1956 static struct gcry_thread_cbs php_http_gnutls_tsl
= {
1957 GCRY_THREAD_OPTION_USER
,
1959 php_http_gnutls_mutex_create
,
1960 php_http_gnutls_mutex_destroy
,
1961 php_http_gnutls_mutex_lock
,
1962 php_http_gnutls_mutex_unlock
1966 #define PHP_HTTP_BEGIN_ARGS(method, req_args) PHP_HTTP_BEGIN_ARGS_EX(HttpCURL, method, 0, req_args)
1967 #define PHP_HTTP_EMPTY_ARGS(method) PHP_HTTP_EMPTY_ARGS_EX(HttpCURL, method, 0)
1968 #define PHP_HTTP_CURL_ME(method, visibility) PHP_ME(HttpCURL, method, PHP_HTTP_ARGS(HttpCURL, method), visibility)
1969 #define PHP_HTTP_CURL_ALIAS(method, func) PHP_HTTP_STATIC_ME_ALIAS(method, func, PHP_HTTP_ARGS(HttpCURL, method))
1970 #define PHP_HTTP_CURL_MALIAS(me, al, vis) ZEND_FENTRY(me, ZEND_MN(HttpCURL_##al), PHP_HTTP_ARGS(HttpCURL, al), vis)
1972 PHP_HTTP_EMPTY_ARGS(__construct
);
1974 zend_class_entry
*php_http_curl_class_entry
;
1975 zend_function_entry php_http_curl_method_entry
[] = {
1976 PHP_HTTP_CURL_ME(__construct
, ZEND_ACC_PRIVATE
|ZEND_ACC_CTOR
)
1978 EMPTY_FUNCTION_ENTRY
1981 PHP_METHOD(HttpCURL
, __construct
) {
1984 PHP_MINIT_FUNCTION(http_curl
)
1986 php_http_request_factory_driver_t driver
= {
1987 &php_http_curl_request_ops
,
1988 &php_http_curl_request_pool_ops
,
1989 &php_http_curl_request_datashare_ops
1992 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
1993 /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
1994 if (!CRYPTO_get_id_callback()) {
1995 int i
, c
= CRYPTO_num_locks();
1997 php_http_openssl_tsl
= malloc(c
* sizeof(MUTEX_T
));
1999 for (i
= 0; i
< c
; ++i
) {
2000 php_http_openssl_tsl
[i
] = tsrm_mutex_alloc();
2003 CRYPTO_set_id_callback(php_http_openssl_thread_id
);
2004 CRYPTO_set_locking_callback(php_http_openssl_thread_lock
);
2007 #ifdef PHP_HTTP_NEED_GNUTLS_TSL
2008 gcry_control(GCRYCTL_SET_THREAD_CBS
, &php_http_gnutls_tsl
);
2011 if (CURLE_OK
!= curl_global_init(CURL_GLOBAL_ALL
)) {
2015 if (SUCCESS
!= php_http_persistent_handle_provide(ZEND_STRL("http_request_datashare.curl"), &php_http_curlsh_resource_factory_ops
, NULL
, NULL
)) {
2019 if (SUCCESS
!= php_http_persistent_handle_provide(ZEND_STRL("http_request_pool.curl"), &php_http_curlm_resource_factory_ops
, NULL
, NULL
)) {
2023 if (SUCCESS
!= php_http_persistent_handle_provide(ZEND_STRL("http_request.curl"), &php_http_curl_resource_factory_ops
, NULL
, NULL
)) {
2027 if (SUCCESS
!= php_http_request_factory_add_driver(ZEND_STRL("curl"), &driver
)) {
2031 PHP_HTTP_REGISTER_CLASS(http
, CURL
, http_curl
, php_http_curl_class_entry
, 0);
2034 * HTTP Protocol Version Constants
2036 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("HTTP_VERSION_1_0"), CURL_HTTP_VERSION_1_0 TSRMLS_CC
);
2037 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("HTTP_VERSION_1_1"), CURL_HTTP_VERSION_1_1 TSRMLS_CC
);
2038 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("HTTP_VERSION_NONE"), CURL_HTTP_VERSION_NONE TSRMLS_CC
); /* to be removed */
2039 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("HTTP_VERSION_ANY"), CURL_HTTP_VERSION_NONE TSRMLS_CC
);
2042 * SSL Version Constants
2044 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("SSL_VERSION_TLSv1"), CURL_SSLVERSION_TLSv1 TSRMLS_CC
);
2045 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("SSL_VERSION_SSLv2"), CURL_SSLVERSION_SSLv2 TSRMLS_CC
);
2046 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("SSL_VERSION_SSLv3"), CURL_SSLVERSION_SSLv3 TSRMLS_CC
);
2047 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("SSL_VERSION_ANY"), CURL_SSLVERSION_DEFAULT TSRMLS_CC
);
2050 * DNS IPvX resolving
2052 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("IPRESOLVE_V4"), CURL_IPRESOLVE_V4 TSRMLS_CC
);
2053 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("IPRESOLVE_V6"), CURL_IPRESOLVE_V6 TSRMLS_CC
);
2054 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("IPRESOLVE_ANY"), CURL_IPRESOLVE_WHATEVER TSRMLS_CC
);
2059 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("AUTH_BASIC"), CURLAUTH_BASIC TSRMLS_CC
);
2060 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("AUTH_DIGEST"), CURLAUTH_DIGEST TSRMLS_CC
);
2061 #if PHP_HTTP_CURL_VERSION(7,19,3)
2062 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("AUTH_DIGEST_IE"), CURLAUTH_DIGEST_IE TSRMLS_CC
);
2064 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("AUTH_NTLM"), CURLAUTH_NTLM TSRMLS_CC
);
2065 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("AUTH_GSSNEG"), CURLAUTH_GSSNEGOTIATE TSRMLS_CC
);
2066 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("AUTH_ANY"), CURLAUTH_ANY TSRMLS_CC
);
2069 * Proxy Type Constants
2071 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("PROXY_SOCKS4"), CURLPROXY_SOCKS4 TSRMLS_CC
);
2072 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("PROXY_SOCKS4A"), CURLPROXY_SOCKS5 TSRMLS_CC
);
2073 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("PROXY_SOCKS5_HOSTNAME"), CURLPROXY_SOCKS5 TSRMLS_CC
);
2074 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("PROXY_SOCKS5"), CURLPROXY_SOCKS5 TSRMLS_CC
);
2075 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("PROXY_HTTP"), CURLPROXY_HTTP TSRMLS_CC
);
2076 # if PHP_HTTP_CURL_VERSION(7,19,4)
2077 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("PROXY_HTTP_1_0"), CURLPROXY_HTTP_1_0 TSRMLS_CC
);
2081 * Post Redirection Constants
2083 #if PHP_HTTP_CURL_VERSION(7,19,1)
2084 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("POSTREDIR_301"), CURL_REDIR_POST_301 TSRMLS_CC
);
2085 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("POSTREDIR_302"), CURL_REDIR_POST_302 TSRMLS_CC
);
2086 zend_declare_class_constant_long(php_http_curl_class_entry
, ZEND_STRL("POSTREDIR_ALL"), CURL_REDIR_POST_ALL TSRMLS_CC
);
2092 PHP_MSHUTDOWN_FUNCTION(http_curl
)
2094 curl_global_cleanup();
2095 #ifdef PHP_HTTP_NEED_OPENSSL_TSL
2096 if (php_http_openssl_tsl
) {
2097 int i
, c
= CRYPTO_num_locks();
2099 CRYPTO_set_id_callback(NULL
);
2100 CRYPTO_set_locking_callback(NULL
);
2102 for (i
= 0; i
< c
; ++i
) {
2103 tsrm_mutex_free(php_http_openssl_tsl
[i
]);
2106 free(php_http_openssl_tsl
);
2107 php_http_openssl_tsl
= NULL
;
2113 PHP_RINIT_FUNCTION(http_curl
)
2115 #if PHP_HTTP_HAVE_EVENT
2116 if (!PHP_HTTP_G
->curl
.event_base
&& !(PHP_HTTP_G
->curl
.event_base
= event_init())) {
2124 #endif /* PHP_HTTP_HAVE_CURL */
2131 * vim600: noet sw=4 ts=4 fdm=marker
2132 * vim<600: noet sw=4 ts=4