- allow to disable zlib support
[m6w6/ext-http] / http_request_api.c
index 3408812f179373b9fcb929e28bb9080cbdf2851d..f7d308997f653d71275938f494cbb57bbd758d23 100644 (file)
@@ -29,8 +29,6 @@
 #      include "php_http_request_object.h"
 #endif
 
-ZEND_EXTERN_MODULE_GLOBALS(http);
-
 /* {{{ cruft for thread safe SSL crypto locks */
 #if defined(ZTS) && defined(HTTP_HAVE_SSL)
 #      ifdef PHP_WIN32
@@ -167,13 +165,16 @@ PHP_MSHUTDOWN_FUNCTION(http_request)
        }
 
 #define HTTP_CURL_OPT(OPTION, p) curl_easy_setopt(request->ch, CURLOPT_##OPTION, (p))
-#define HTTP_CURL_OPT_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, keyname)
-#define HTTP_CURL_OPT_SSL_STRING(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname)
-#define HTTP_CURL_OPT_SSL_STRING_(keyname) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname)
-#define HTTP_CURL_OPT_STRING_EX(keyname, optname) \
+#define HTTP_CURL_OPT_STRING(keyname, obdc) HTTP_CURL_OPT_STRING_EX(keyname, keyname, obdc)
+#define HTTP_CURL_OPT_SSL_STRING(keyname, obdc) HTTP_CURL_OPT_STRING_EX(keyname, SSL##keyname, obdc)
+#define HTTP_CURL_OPT_SSL_STRING_(keyname,obdc ) HTTP_CURL_OPT_STRING_EX(keyname, SSL_##keyname, obdc)
+#define HTTP_CURL_OPT_STRING_EX(keyname, optname, obdc) \
        if (!strcasecmp(key, #keyname)) { \
-               convert_to_string(*param); \
-               HTTP_CURL_OPT(optname, Z_STRVAL_PP(param)); \
+               zval *copy = http_request_option_cache(request, #keyname, zval_copy(IS_STRING, *param)); \
+               if (obdc) { \
+                       HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(copy), return FAILURE); \
+               } \
+               HTTP_CURL_OPT(optname, Z_STRVAL_P(copy)); \
                key = NULL; \
                continue; \
        }
@@ -182,8 +183,8 @@ PHP_MSHUTDOWN_FUNCTION(http_request)
 #define HTTP_CURL_OPT_SSL_LONG_(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL_##keyname)
 #define HTTP_CURL_OPT_LONG_EX(keyname, optname) \
        if (!strcasecmp(key, #keyname)) { \
-               convert_to_long(*param); \
-               HTTP_CURL_OPT(optname, Z_LVAL_PP(param)); \
+               zval *copy = http_request_option_cache(request, #keyname, zval_copy(IS_LONG, *param)); \
+               HTTP_CURL_OPT(optname, Z_LVAL_P(copy)); \
                key = NULL; \
                continue; \
        }
@@ -193,6 +194,9 @@ PHP_MSHUTDOWN_FUNCTION(http_request)
 #define http_request_option(r, o, k, t) _http_request_option_ex((r), (o), (k), sizeof(k), (t) TSRMLS_CC)
 #define http_request_option_ex(r, o, k, l, t) _http_request_option_ex((r), (o), (k), (l), (t) TSRMLS_CC)
 static inline zval *_http_request_option_ex(http_request *request, HashTable *options, char *key, size_t keylen, int type TSRMLS_DC);
+#define http_request_option_cache(r, k, z) _http_request_option_cache_ex((r), (k), sizeof(k), 0, (z) TSRMLS_CC)
+#define http_request_option_cache_ex(r, k, kl, h, z) _http_request_option_cache_ex((r), (k), (kl), (h), (z) TSRMLS_CC)
+static inline zval *_http_request_option_cache_ex(http_request *r, char *key, size_t keylen, ulong h, zval *opt TSRMLS_DC);
 
 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
 static int http_curl_progress_callback(void *, double, double, double, double);
@@ -214,7 +218,7 @@ PHP_HTTP_API http_request *_http_request_init_ex(http_request *request, CURL *ch
        memset(r, 0, sizeof(http_request));
        
        r->ch = ch;
-       r->url = (url && *url) ? estrdup(url) : NULL;
+       r->url = (url) ? http_absolute_url(url) : NULL;
        r->meth = (meth > 0) ? meth : HTTP_GET;
        
        phpstr_init(&r->conv.request);
@@ -315,7 +319,7 @@ PHP_HTTP_API void _http_request_defaults(http_request *request)
                HTTP_CURL_OPT(PORT, 0);
                HTTP_CURL_OPT(USERPWD, NULL);
                HTTP_CURL_OPT(HTTPAUTH, 0);
-               HTTP_CURL_OPT(ENCODING, 0);
+               HTTP_CURL_OPT(ENCODING, NULL);
                HTTP_CURL_OPT(FOLLOWLOCATION, 0);
                HTTP_CURL_OPT(UNRESTRICTED_AUTH, 0);
                HTTP_CURL_OPT(REFERER, NULL);
@@ -351,9 +355,6 @@ PHP_HTTP_API void _http_request_defaults(http_request *request)
                HTTP_CURL_OPT(IOCTLDATA, NULL);
                HTTP_CURL_OPT(READDATA, NULL);
                HTTP_CURL_OPT(INFILESIZE, 0);
-#if 0
-               HTTP_CURL_OPT(IGNORE_CONTENT_ENCODING, 1);
-#endif
        }
 }
 /* }}} */
@@ -430,15 +431,6 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
        }
 
-       /* compress, empty string enables all supported if libcurl was build with zlib support */
-       if ((zoption = http_request_option(request, options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {
-#ifdef HTTP_HAVE_CURL_ZLIB
-               HTTP_CURL_OPT(ENCODING, "gzip, deflate");
-#else
-               HTTP_CURL_OPT(ENCODING, "gzip;q=1.0, deflate;q=0.5, *;q=0.1");
-#endif
-       }
-
        /* redirects, defaults to 0 */
        if ((zoption = http_request_option(request, options, "redirect", IS_LONG))) {
                HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
@@ -459,29 +451,26 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
        }
 
        /* additional headers, array('name' => 'value') */
+       if (request->_cache.headers) {
+               curl_slist_free_all(request->_cache.headers);
+               request->_cache.headers = NULL;
+       }
        if ((zoption = http_request_option(request, options, "headers", IS_ARRAY))) {
                char *header_key;
                ulong header_idx;
                HashPosition pos;
 
-               if (request->_cache.headers) {
-                       curl_slist_free_all(request->_cache.headers);
-                       request->_cache.headers = NULL;
-               }
-               
                FOREACH_KEY(pos, zoption, header_key, header_idx) {
                        if (header_key) {
                                zval **header_val;
                                if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &header_val, &pos)) {
                                        char header[1024] = {0};
-                                       zval *cpy, *val = convert_to_type_ex(IS_STRING, *header_val, &cpy);
                                        
-                                       snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_P(val));
+                                       ZVAL_ADDREF(*header_val);
+                                       convert_to_string_ex(header_val);
+                                       snprintf(header, 1023, "%s: %s", header_key, Z_STRVAL_PP(header_val));
                                        request->_cache.headers = curl_slist_append(request->_cache.headers, header);
-                                       
-                                       if (cpy) {
-                                               zval_ptr_dtor(&cpy);
-                                       }
+                                       zval_ptr_dtor(header_val);
                                }
 
                                /* reset */
@@ -489,39 +478,27 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                        }
                }
        }
+       if ((zoption = http_request_option(request, options, "compress", IS_BOOL)) && Z_LVAL_P(zoption)) {
+               request->_cache.headers = curl_slist_append(request->_cache.headers, "Accept-Encoding: gzip;q=1.0,deflate;q=0.5,*;q=0.1");
+       }
+       HTTP_CURL_OPT(HTTPHEADER, request->_cache.headers);
 
        /* cookies, array('name' => 'value') */
        if ((zoption = http_request_option(request, options, "cookies", IS_ARRAY))) {
-               char *cookie_key = NULL;
-               ulong cookie_idx = 0;
-               HashPosition pos;
-               
                phpstr_dtor(&request->_cache.cookies);
-
-               FOREACH_KEY(pos, zoption, cookie_key, cookie_idx) {
-                       if (cookie_key) {
-                               zval **cookie_val;
-                               if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_P(zoption), (void **) &cookie_val, &pos)) {
-                                       zval *cpy, *val = convert_to_type_ex(IS_STRING, *cookie_val, &cpy);
-                                       
-                                       phpstr_appendf(&request->_cache.cookies, "%s=%s; ", cookie_key, Z_STRVAL_P(val));
-                                       
-                                       if (cpy) {
-                                               zval_ptr_dtor(&cpy);
-                                       }
-                               }
-
-                               /* reset */
-                               cookie_key = NULL;
-                       }
-               }
-
-               if (request->_cache.cookies.used) {
+               if (SUCCESS == http_urlencode_hash_recursive(HASH_OF(zoption), &request->_cache.cookies, "; ", sizeof("; ")-1, NULL, 0)) {
                        phpstr_fix(&request->_cache.cookies);
                        HTTP_CURL_OPT(COOKIE, request->_cache.cookies.data);
                }
        }
 
+#if LIBCURL_VERSION_NUM >= 0x070f01
+       /* reset cookies */
+       if ((zoption = http_request_option(request, options, "resetcookies", IS_BOOL)) && Z_LVAL_P(zoption)) {
+               HTTP_CURL_OPT(COOKIELIST, "ALL");
+       }
+#endif
+       
        /* session cookies */
        if ((zoption = http_request_option(request, options, "cookiesession", IS_BOOL))) {
                if (Z_LVAL_P(zoption)) {
@@ -535,6 +512,7 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
 
        /* cookiestore, read initial cookies from that file and store cookies back into that file */
        if ((zoption = http_request_option(request, options, "cookiestore", IS_STRING)) && Z_STRLEN_P(zoption)) {
+               HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption), return FAILURE);
                HTTP_CURL_OPT(COOKIEFILE, Z_STRVAL_P(zoption));
                HTTP_CURL_OPT(COOKIEJAR, Z_STRVAL_P(zoption));
        }
@@ -583,25 +561,25 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
 
                FOREACH_KEYVAL(pos, zoption, key, idx, param) {
                        if (key) {
-                               HTTP_CURL_OPT_SSL_STRING(CERT);
-                               HTTP_CURL_OPT_SSL_STRING(CERTTYPE);
-                               HTTP_CURL_OPT_SSL_STRING(CERTPASSWD);
+                               HTTP_CURL_OPT_SSL_STRING(CERT, 1);
+                               HTTP_CURL_OPT_SSL_STRING(CERTTYPE, 0);
+                               HTTP_CURL_OPT_SSL_STRING(CERTPASSWD, 0);
 
-                               HTTP_CURL_OPT_SSL_STRING(KEY);
-                               HTTP_CURL_OPT_SSL_STRING(KEYTYPE);
-                               HTTP_CURL_OPT_SSL_STRING(KEYPASSWD);
+                               HTTP_CURL_OPT_SSL_STRING(KEY, 0);
+                               HTTP_CURL_OPT_SSL_STRING(KEYTYPE, 0);
+                               HTTP_CURL_OPT_SSL_STRING(KEYPASSWD, 0);
 
-                               HTTP_CURL_OPT_SSL_STRING(ENGINE);
+                               HTTP_CURL_OPT_SSL_STRING(ENGINE, 0);
                                HTTP_CURL_OPT_SSL_LONG(VERSION);
 
                                HTTP_CURL_OPT_SSL_LONG_(VERIFYPEER);
                                HTTP_CURL_OPT_SSL_LONG_(VERIFYHOST);
-                               HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST);
+                               HTTP_CURL_OPT_SSL_STRING_(CIPHER_LIST, 0);
 
-                               HTTP_CURL_OPT_STRING(CAINFO);
-                               HTTP_CURL_OPT_STRING(CAPATH);
-                               HTTP_CURL_OPT_STRING(RANDOM_FILE);
-                               HTTP_CURL_OPT_STRING(EGDSOCKET);
+                               HTTP_CURL_OPT_STRING(CAINFO, 1);
+                               HTTP_CURL_OPT_STRING(CAPATH, 1);
+                               HTTP_CURL_OPT_STRING(RANDOM_FILE, 1);
+                               HTTP_CURL_OPT_STRING(EGDSOCKET, 1);
 
                                /* reset key */
                                key = NULL;
@@ -709,7 +687,6 @@ PHP_HTTP_API void _http_request_info(http_request *request, HashTable *info)
        HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
        HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
        HTTP_CURL_INFO(CONTENT_TYPE);
-       /*HTTP_CURL_INFO(PRIVATE);*/
        HTTP_CURL_INFO(HTTPAUTH_AVAIL);
        HTTP_CURL_INFO(PROXYAUTH_AVAIL);
        /*HTTP_CURL_INFO(OS_ERRNO);*/
@@ -837,6 +814,8 @@ static inline zval *_http_request_option_ex(http_request *r, HashTable *options,
        zval **zoption;
 #ifdef ZEND_ENGINE_2
        ulong h = zend_get_hash_value(key, keylen);
+#else
+       ulong h = 0;
 #endif
        
        if (!options || 
@@ -848,31 +827,32 @@ static inline zval *_http_request_option_ex(http_request *r, HashTable *options,
        ) {
                return NULL;
        }
+       
+       return http_request_option_cache_ex(r, key, keylen, h, zval_copy(type, *zoption));
+}
+/* }}} */
 
-       if (Z_TYPE_PP(zoption) != type) {
-               switch (type)
-               {
-                       case IS_BOOL:   convert_to_boolean_ex(zoption); break;
-                       case IS_LONG:   convert_to_long_ex(zoption);    break;
-                       case IS_DOUBLE: convert_to_double_ex(zoption);  break;
-                       case IS_STRING: convert_to_string_ex(zoption);  break;
-                       case IS_ARRAY:  convert_to_array_ex(zoption);   break;
-                       case IS_OBJECT: convert_to_object_ex(zoption);  break;
-                       default:
-                       break;
-               }
-       }
+/* {{{ static inline zval *http_request_option_cache(http_request *, char *key, zval *) */
+static inline zval *_http_request_option_cache_ex(http_request *r, char *key, size_t keylen, ulong h, zval *opt TSRMLS_DC)
+{
+       ZVAL_ADDREF(opt);
        
-       ZVAL_ADDREF(*zoption);
 #ifdef ZEND_ENGINE_2
-       _zend_hash_quick_add_or_update(&r->_cache.options, key, keylen, h, zoption, sizeof(zval *), NULL, 
-               zend_hash_quick_exists(&r->_cache.options, key, keylen, h)?HASH_UPDATE:HASH_ADD ZEND_FILE_LINE_CC);
-#else
-       zend_hash_add_or_update(&r->_cache.options, key, keylen, zoption, sizeof(zval *), NULL,
-               zend_hash_exists(&r->_cache.options, key, keylen)?HASH_UPDATE:HASH_ADD);
+       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);
+               }
+       }
        
-       return *zoption;
+       return opt;
 }
 /* }}} */