release 1.6.1
[m6w6/ext-http] / http_request_api.c
index 289cac491d4ea003b0b082728dd960fd348a23b2..2d853a2b91265a135f767dbb261625f08556a254 100644 (file)
@@ -6,7 +6,7 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2006, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2007, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
 #ifdef HTTP_HAVE_CURL
 
 #include "php_http_api.h"
+#include "php_http_persistent_handle_api.h"
 #include "php_http_request_api.h"
 #include "php_http_url_api.h"
-#ifdef HTTP_HAVE_PERSISTENT_HANDLES
-#      include "php_http_persistent_handle_api.h"
-#endif
 
 #ifdef ZEND_ENGINE_2
 #      include "php_http_request_object.h"
@@ -86,6 +84,51 @@ static struct gcry_thread_cbs http_gnutls_tsl = {
 #endif
 /* }}} */
 
+/* safe curl wrappers */
+#define init_curl_storage(ch) \
+       {\
+               http_request_storage *st = pecalloc(1, sizeof(http_request_storage), 1); \
+               curl_easy_setopt(ch, CURLOPT_PRIVATE, st); \
+               curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, st->errorbuffer); \
+       }
+
+static void *safe_curl_init(void)
+{
+       CURL *ch;
+       
+       if ((ch = curl_easy_init())) {
+               init_curl_storage(ch);
+               return ch;
+       }
+       return NULL;
+}
+static void *safe_curl_copy(void *p)
+{
+       CURL *ch;
+       
+       if ((ch = curl_easy_duphandle(p))) {
+               init_curl_storage(ch);
+               return ch;
+       }
+       return NULL;
+}
+static void safe_curl_dtor(void *p) {
+       http_request_storage *st = http_request_storage_get(p);
+       
+       curl_easy_cleanup(p);
+       
+       if (st) {
+               if (st->url) {
+                       pefree(st->url, 1);
+               }
+               if (st->cookiestore) {
+                       pefree(st->cookiestore, 1);
+               }
+               pefree(st, 1);
+       }
+}
+/* }}} */
+
 /* {{{ MINIT */
 PHP_MINIT_FUNCTION(http_request)
 {
@@ -112,11 +155,9 @@ PHP_MINIT_FUNCTION(http_request)
                return FAILURE;
        }
        
-#ifdef HTTP_HAVE_PERSISTENT_HANDLES
-       if (SUCCESS != http_persistent_handle_provide("http_request", curl_easy_init, curl_easy_cleanup)) {
+       if (SUCCESS != http_persistent_handle_provide("http_request", safe_curl_init, safe_curl_dtor, safe_curl_copy)) {
                return FAILURE;
        }
-#endif
        
        HTTP_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC);
        HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST);
@@ -185,18 +226,10 @@ static int http_curl_dummy_callback(char *data, size_t n, size_t l, void *s) { r
 static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *);
 /* }}} */
 
-#ifdef HTTP_HAVE_PERSISTENT_HANDLES
-#      define HTTP_CURL_HANDLE_CTOR(ch) (SUCCESS == http_persistent_handle_acquire("http_request", &(ch)))
-#      define HTTP_CURL_HANDLE_DTOR(chp) http_persistent_handle_release("http_request", (chp))
-#else
-#      define HTTP_CURL_HANDLE_CTOR(ch) ((ch) = curl_easy_init())
-#      define HTTP_CURL_HANDLE_DTOR(chp) curl_easy_cleanup(*(chp)); *(chp) = NULL
-#endif
-
 /* {{{ CURL *http_curl_init(http_request *) */
-PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, http_request *request)
+PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, http_request *request TSRMLS_DC)
 {
-       if (ch || HTTP_CURL_HANDLE_CTOR(ch)) {
+       if (ch || (SUCCESS == http_persistent_handle_acquire("http_request", &ch))) {
 #if defined(ZTS)
                curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
 #endif
@@ -212,9 +245,7 @@ PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, http_request *request)
                
                /* set context */
                if (request) {
-                       curl_easy_setopt(ch, CURLOPT_PRIVATE, request);
                        curl_easy_setopt(ch, CURLOPT_DEBUGDATA, request);
-                       curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, request->_error);
                        
                        /* attach curl handle */
                        request->ch = ch;
@@ -227,8 +258,20 @@ PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, http_request *request)
 }
 /* }}} */
 
+/* {{{ CURL *http_curl_copy(CURL *) */
+PHP_HTTP_API CURL *_http_curl_copy(CURL *ch TSRMLS_DC)
+{
+       CURL *copy;
+       
+       if (SUCCESS == http_persistent_handle_accrete("http_request", ch, &copy)) {
+               return copy;
+       }
+       return NULL;
+}
+/* }}} */
+
 /* {{{ void http_curl_free(CURL **) */
-PHP_HTTP_API void _http_curl_free(CURL **ch)
+PHP_HTTP_API void _http_curl_free(CURL **ch TSRMLS_DC)
 {
        if (*ch) {
                curl_easy_setopt(*ch, CURLOPT_NOPROGRESS, 1L);
@@ -236,7 +279,7 @@ PHP_HTTP_API void _http_curl_free(CURL **ch)
                curl_easy_setopt(*ch, CURLOPT_VERBOSE, 0L);
                curl_easy_setopt(*ch, CURLOPT_DEBUGFUNCTION, NULL);
                
-               HTTP_CURL_HANDLE_DTOR(ch);
+               http_persistent_handle_release("http_request", ch);
        }
 }
 /* }}} */
@@ -271,6 +314,8 @@ PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch
 /* {{{ void http_request_dtor(http_request *) */
 PHP_HTTP_API void _http_request_dtor(http_request *request)
 {
+       TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
+       
        http_curl_free(&request->ch);
        http_request_reset(request);
        
@@ -309,11 +354,23 @@ PHP_HTTP_API void _http_request_reset(http_request *request)
        phpstr_dtor(&request->conv.request);
        phpstr_dtor(&request->conv.response);
        http_request_body_dtor(request->body);
+       http_request_defaults(request);
        
        if (request->ch) {
-               http_request_defaults(request);
+               http_request_storage *st = http_request_storage_get(request->ch);
+               
+               if (st) {
+                       if (st->url) {
+                               pefree(st->url, 1);
+                               st->url = NULL;
+                       }
+                       if (st->cookiestore) {
+                               pefree(st->cookiestore, 1);
+                               st->cookiestore = NULL;
+                       }
+                       st->errorbuffer[0] = '\0';
+               }
        }
-       request->_error[0] = '\0';
 }
 /* }}} */
 
@@ -344,20 +401,38 @@ PHP_HTTP_API STATUS _http_request_reset_cookies(http_request *request, int sessi
                if (initialized && CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIELIST, "SESS")) {
                        return SUCCESS;
                }
-#endif
+#else
                http_error(HE_WARNING, HTTP_E_REQUEST, "Could not reset session cookies (need libcurl >= v7.15.4)");
+#endif
        } else {
 #if HTTP_CURL_VERSION(7,14,1)
                if (initialized && CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIELIST, "ALL")) {
                        return SUCCESS;
                }
-#endif
+#else
                http_error(HE_WARNING, HTTP_E_REQUEST, "Could not reset cookies (need libcurl >= v7.14.1)");
+#endif
        }
        return FAILURE;
 }
 /* }}} */
 
+PHP_HTTP_API STATUS _http_request_flush_cookies(http_request *request)
+{
+       int initialized = 1;
+       TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
+       
+       HTTP_CHECK_CURL_INIT(request->ch, http_curl_init_ex(request->ch, request), initialized = 0);
+#if HTTP_CURL_VERSION(7,17,1)
+       if (initialized && CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIELIST, "FLUSH")) {
+               return SUCCESS;
+       }
+#else
+       http_error(HE_WARNING, HTTP_E_REQUEST, "Could not flush cookies (need libcurl >= v7.17.1)");
+#endif
+       return FAILURE;
+}
+
 /* {{{ void http_request_defaults(http_request *) */
 PHP_HTTP_API void _http_request_defaults(http_request *request)
 {
@@ -375,8 +450,10 @@ PHP_HTTP_API void _http_request_defaults(http_request *request)
                HTTP_CURL_OPT(CURLOPT_LOW_SPEED_LIMIT, 0L);
                HTTP_CURL_OPT(CURLOPT_LOW_SPEED_TIME, 0L);
 #if HTTP_CURL_VERSION(7,15,5)
+               /* LFS weirdance
                HTTP_CURL_OPT(CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) 0);
                HTTP_CURL_OPT(CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) 0);
+               */
 #endif
                /* crashes
                HTTP_CURL_OPT(CURLOPT_MAXCONNECTS, 5L); */
@@ -391,10 +468,15 @@ PHP_HTTP_API void _http_request_defaults(http_request *request)
                HTTP_CURL_OPT(CURLOPT_USERPWD, NULL);
                HTTP_CURL_OPT(CURLOPT_HTTPAUTH, 0L);
                HTTP_CURL_OPT(CURLOPT_ENCODING, NULL);
+#if HTTP_CURL_VERSION(7,16,2)
+               /* we do this ourself anyway */
+               HTTP_CURL_OPT(CURLOPT_HTTP_CONTENT_DECODING, 0L);
+               HTTP_CURL_OPT(CURLOPT_HTTP_TRANSFER_DECODING, 0L);
+#endif
                HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION, 0L);
                HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH, 0L);
                HTTP_CURL_OPT(CURLOPT_REFERER, NULL);
-               HTTP_CURL_OPT(CURLOPT_USERAGENT, "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
+               HTTP_CURL_OPT(CURLOPT_USERAGENT, "PECL::HTTP/" PHP_HTTP_VERSION " (PHP/" PHP_VERSION ")");
                HTTP_CURL_OPT(CURLOPT_HTTPHEADER, NULL);
                HTTP_CURL_OPT(CURLOPT_COOKIE, NULL);
 #if HTTP_CURL_VERSION(7,14,1)
@@ -464,13 +546,22 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
 {
        zval *zoption;
        zend_bool range_req = 0;
+       http_request_storage *storage;
 
        TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
        
        HTTP_CHECK_CURL_INIT(request->ch, http_curl_init(request), return FAILURE);
        
+       if (!(storage = http_request_storage_get(request->ch))) {
+               return FAILURE;
+       }
+       storage->errorbuffer[0] = '\0';
        /* set options */
-       HTTP_CURL_OPT(CURLOPT_URL, request->url);
+       if (storage->url) {
+               pefree(storage->url, 1);
+       }
+       storage->url = pestrdup(request->url, 1);
+       HTTP_CURL_OPT(CURLOPT_URL, storage->url);
 
        /* progress callback */
        if ((zoption = http_request_option(request, options, "onprogress", -1))) {
@@ -479,9 +570,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
 
        /* proxy */
        if ((zoption = http_request_option(request, options, "proxyhost", IS_STRING))) {
-               if (Z_STRLEN_P(zoption)) {
-                       HTTP_CURL_OPT(CURLOPT_PROXY, Z_STRVAL_P(zoption));
-               }
+               HTTP_CURL_OPT(CURLOPT_PROXY, Z_STRVAL_P(zoption));
                /* type */
                if ((zoption = http_request_option(request, options, "proxytype", IS_LONG))) {
                        HTTP_CURL_OPT(CURLOPT_PROXYTYPE, Z_LVAL_P(zoption));
@@ -516,12 +605,14 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                HTTP_CURL_OPT(CURLOPT_LOW_SPEED_TIME, Z_LVAL_P(zoption));
        }
 #if HTTP_CURL_VERSION(7,15,5)
+       /* LSF weirdance
        if ((zoption = http_request_option(request, options, "max_send_speed", IS_LONG))) {
                HTTP_CURL_OPT(CURLOPT_MAX_SEND_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
        }
        if ((zoption = http_request_option(request, options, "max_recv_speed", IS_LONG))) {
                HTTP_CURL_OPT(CURLOPT_MAX_RECV_SPEED_LARGE, (curl_off_t) Z_LVAL_P(zoption));
        }
+       */
 #endif
        /* crashes
        if ((zoption = http_request_option(request, options, "maxconnects", IS_LONG))) {
@@ -546,14 +637,20 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                        if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &prs)) {
                                zend_hash_move_forward(Z_ARRVAL_P(zoption));
                                if (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(zoption), (void *) &pre)) {
-                                       zval *prs_cpy = zval_copy(IS_LONG, *prs), *pre_cpy = zval_copy(IS_LONG, *pre);
+                                       zval *prs_cpy = *prs, *pre_cpy = *pre;
                                        
+                                       convert_to_long_ex(&prs_cpy);
+                                       convert_to_long_ex(&pre_cpy);
                                        if (Z_LVAL_P(prs_cpy) && Z_LVAL_P(pre_cpy)) {
                                                HTTP_CURL_OPT(CURLOPT_LOCALPORT, MIN(Z_LVAL_P(prs_cpy), Z_LVAL_P(pre_cpy)));
                                                HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE, labs(Z_LVAL_P(prs_cpy)-Z_LVAL_P(pre_cpy))+1L);
                                        }
-                                       zval_free(&prs_cpy);
-                                       zval_free(&pre_cpy);
+                                       if (prs_cpy != *prs) {
+                                               zval_ptr_dtor(&prs_cpy);
+                                       }
+                                       if (pre_cpy != *pre) {
+                                               zval_ptr_dtor(&pre_cpy);
+                                       }
                                }
                        }
                }
@@ -581,6 +678,18 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                        HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
                }
        }
+       
+       /* retries, defaults to 0 */
+       if ((zoption = http_request_option(request, options, "retrycount", IS_LONG))) {
+               request->_retry.count = Z_LVAL_P(zoption);
+               if ((zoption = http_request_option(request, options, "retrydelay", IS_DOUBLE))) {
+                       request->_retry.delay = Z_DVAL_P(zoption);
+               } else {
+                       request->_retry.delay = 0;
+               }
+       } else {
+               request->_retry.count = 0;
+       }
 
        /* referer */
        if ((zoption = http_request_option(request, options, "referer", IS_STRING)) && Z_STRLEN_P(zoption)) {
@@ -617,13 +726,19 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                                        if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &re, &pos2)) {
                                                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))) &&
                                                                ((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)))) {
-                                                       zval *rbl = zval_copy(IS_LONG, *rb), *rel = zval_copy(IS_LONG, *re);
+                                                       zval *rbl = *rb, *rel = *re;
                                                        
+                                                       convert_to_long_ex(&rbl);
+                                                       convert_to_long_ex(&rel);
                                                        if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) {
                                                                phpstr_appendf(&rs, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel));
                                                        }
-                                                       zval_free(&rbl);
-                                                       zval_free(&rel);
+                                                       if (rbl != *rb) {
+                                                               zval_ptr_dtor(&rbl);
+                                                       }
+                                                       if (rel != *re) {
+                                                               zval_ptr_dtor(&rel);
+                                                       }
                                                }
                                        }
                                }
@@ -717,9 +832,12 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                                
                                FOREACH_KEYVAL(pos, zoption, cookie_key, cookie_val) {
                                        if (cookie_key.type == HASH_KEY_IS_STRING) {
-                                               zval *val = zval_copy(IS_STRING, *cookie_val);
+                                               zval *val = *cookie_val;
+                                               convert_to_string_ex(&val);
                                                phpstr_appendf(&request->_cache.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(val));
-                                               zval_free(&val);
+                                               if (val != *cookie_val) {
+                                                       zval_ptr_dtor(&val);
+                                               }
                                        }
                                }
                                
@@ -741,8 +859,12 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                if (Z_STRLEN_P(zoption)) {
                        HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption), return FAILURE);
                }
-               HTTP_CURL_OPT(CURLOPT_COOKIEFILE, Z_STRVAL_P(zoption));
-               HTTP_CURL_OPT(CURLOPT_COOKIEJAR, Z_STRVAL_P(zoption));
+               if (storage->cookiestore) {
+                       pefree(storage->cookiestore, 1);
+               }
+               storage->cookiestore = pestrndup(Z_STRVAL_P(zoption), Z_STRLEN_P(zoption), 1);
+               HTTP_CURL_OPT(CURLOPT_COOKIEFILE, storage->cookiestore);
+               HTTP_CURL_OPT(CURLOPT_COOKIEJAR, storage->cookiestore);
        }
 
        /* maxfilesize */
@@ -872,11 +994,37 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
 /* {{{ void http_request_exec(http_request *) */
 PHP_HTTP_API void _http_request_exec(http_request *request)
 {
+       uint tries = 0;
        CURLcode result;
        TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
        
+retry:
        if (CURLE_OK != (result = curl_easy_perform(request->ch))) {
-               http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), request->_error, request->url);
+               http_error_ex(HE_WARNING, HTTP_E_REQUEST, "%s; %s (%s)", curl_easy_strerror(result), http_request_storage_get(request->ch)->errorbuffer, request->url);
+               
+               if (request->_retry.count > tries++) {
+                       switch (result) {
+                               case CURLE_COULDNT_RESOLVE_PROXY:
+                               case CURLE_COULDNT_RESOLVE_HOST:
+                               case CURLE_COULDNT_CONNECT:
+                               case CURLE_WRITE_ERROR:
+                               case CURLE_READ_ERROR:
+                               case CURLE_OPERATION_TIMEDOUT:
+                               case CURLE_SSL_CONNECT_ERROR:
+                               case CURLE_GOT_NOTHING:
+                               case CURLE_SSL_ENGINE_SETFAILED:
+                               case CURLE_SEND_ERROR:
+                               case CURLE_RECV_ERROR:
+                               case CURLE_SSL_ENGINE_INITFAILED:
+                               case CURLE_LOGIN_DENIED:
+                                       if (request->_retry.delay >= HTTP_DIFFSEC) {
+                                               http_sleep(request->_retry.delay);
+                                       }
+                                       goto retry;
+                               default:
+                                       break;
+                       }
+               }
        }
 }
 /* }}} */
@@ -1021,24 +1169,24 @@ static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size
 /* {{{ static inline zval *http_request_option(http_request *, HashTable *, char *, size_t, int) */
 static inline zval *_http_request_option_ex(http_request *r, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC)
 {
-       zval **zoption;
-#ifdef ZEND_ENGINE_2
-       ulong h = zend_get_hash_value(key, keylen);
-#else
-       ulong h = 0;
-#endif
-       
-       if (!options || 
-#ifdef ZEND_ENGINE_2
-                       (SUCCESS != zend_hash_quick_find(options, key, keylen, h, (void *) &zoption))
-#else
-                       (SUCCESS != zend_hash_find(options, key, keylen, (void *) &zoption))
-#endif
-       ) {
-               return NULL;
+       if (options) {
+               zval **zoption;
+               ulong h = zend_hash_func(key, keylen);
+               
+               if (SUCCESS == zend_hash_quick_find(options, key, keylen, h, (void *) &zoption)) {
+                       zval *copy;
+                       
+                       MAKE_STD_ZVAL(copy);
+                       ZVAL_ZVAL(copy, *zoption, 1, 0);
+                       
+                       convert_to_type(type, copy);
+                       http_request_option_cache_ex(r, key, keylen, h, copy);
+                       zval_ptr_dtor(&copy);
+                       return copy;
+               }
        }
        
-       return http_request_option_cache_ex(r, key, keylen, h, zval_copy(type, *zoption));
+       return NULL;
 }
 /* }}} */
 
@@ -1047,19 +1195,10 @@ static inline zval *_http_request_option_cache_ex(http_request *r, char *key, si
 {
        ZVAL_ADDREF(opt);
        
-#ifdef ZEND_ENGINE_2
        if (h) {
-               _zend_hash_quick_add_or_update(&r->_cache.options, key, keylen, h, &opt, sizeof(zval *), NULL, 
-                       zend_hash_quick_exists(&r->_cache.options, key, keylen, h)?HASH_UPDATE:HASH_ADD ZEND_FILE_LINE_CC);
-       }
-       else
-#endif
-       {
-               if (zend_hash_exists(&r->_cache.options, key, keylen)) {
-                       zend_hash_update(&r->_cache.options, key, keylen, &opt, sizeof(zval *), NULL);
-               } else {
-                       zend_hash_add(&r->_cache.options, key, keylen, &opt, sizeof(zval *), NULL);
-               }
+               zend_hash_quick_update(&r->_cache.options, key, keylen, h, &opt, sizeof(zval *), NULL);
+       } else {
+               zend_hash_update(&r->_cache.options, key, keylen, &opt, sizeof(zval *), NULL);
        }
        
        return opt;