- Fix build
[m6w6/ext-http] / http_request_api.c
index 84acf1c37b7eb97ac8781e459f82ccc55e2af534..2a5f68e7ab1c5e564ba4d0200b38972aa4167ccd 100644 (file)
@@ -6,22 +6,20 @@
     | 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>            |
     +--------------------------------------------------------------------+
 */
 
 /* $Id$ */
 
-#ifdef HAVE_CONFIG_H
-#      include "config.h"
-#endif
-
+#define HTTP_WANT_SAPI
 #define HTTP_WANT_CURL
 #include "php_http.h"
 
 #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"
 
 #      include "php_http_request_object.h"
 #endif
 
+#include "php_http_request_int.h"
+
 /* {{{ cruft for thread safe SSL crypto locks */
-#if defined(ZTS) && defined(HTTP_HAVE_SSL)
-#      ifdef PHP_WIN32
-#              define HTTP_NEED_SSL_TSL
-#              define HTTP_NEED_OPENSSL_TSL
-#              include <openssl/crypto.h>
-#      else /* !PHP_WIN32 */
-#              if defined(HTTP_HAVE_OPENSSL)
-#                      if defined(HAVE_OPENSSL_CRYPTO_H)
-#                              define HTTP_NEED_SSL_TSL
-#                              define HTTP_NEED_OPENSSL_TSL
-#                              include <openssl/crypto.h>
-#                      else
-#                              warning \
-                                       "libcurl was compiled with OpenSSL support, but configure could not find " \
-                                       "openssl/crypto.h; thus no SSL crypto locking callbacks will be set, which may " \
-                                       "cause random crashes on SSL requests"
-#                      endif
-#              elif defined(HTTP_HAVE_GNUTLS)
-#                      if defined(HAVE_GCRYPT_H)
-#                              define HTTP_NEED_SSL_TSL
-#                              define HTTP_NEED_GNUTLS_TSL
-#                              include <gcrypt.h>
-#                      else
-#                              warning \
-                                       "libcurl was compiled with GnuTLS support, but configure could not find " \
-                                       "gcrypt.h; thus no SSL crypto locking callbacks will be set, which may " \
-                                       "cause random crashes on SSL requests"
-#                      endif
-#              else
-#                      warning \
-                               "libcurl was compiled with SSL support, but configure could not determine which" \
-                               "library was used; thus no SSL crypto locking callbacks will be set, which may " \
-                               "cause random crashes on SSL requests"
-#              endif /* HTTP_HAVE_OPENSSL || HTTP_HAVE_GNUTLS */
-#      endif /* PHP_WIN32 */
-#endif /* ZTS && HTTP_HAVE_SSL */
-
-#ifdef HTTP_NEED_SSL_TSL
-static inline void http_ssl_init(void);
-static inline void http_ssl_cleanup(void);
+#ifdef HTTP_NEED_OPENSSL_TSL
+static MUTEX_T *http_openssl_tsl = NULL;
+
+static void http_openssl_thread_lock(int mode, int n, const char * file, int line)
+{
+       if (mode & CRYPTO_LOCK) {
+               tsrm_mutex_lock(http_openssl_tsl[n]);
+       } else {
+               tsrm_mutex_unlock(http_openssl_tsl[n]);
+       }
+}
+
+static ulong http_openssl_thread_id(void)
+{
+       return (ulong) tsrm_thread_id();
+}
+#endif
+#ifdef HTTP_NEED_GNUTLS_TSL
+static int http_gnutls_mutex_create(void **m)
+{
+       if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
+               return SUCCESS;
+       } else {
+               return FAILURE;
+       }
+}
+
+static int http_gnutls_mutex_destroy(void **m)
+{
+       tsrm_mutex_free(*((MUTEX_T *) m));
+       return SUCCESS;
+}
+
+static int http_gnutls_mutex_lock(void **m)
+{
+       return tsrm_mutex_lock(*((MUTEX_T *) m));
+}
+
+static int http_gnutls_mutex_unlock(void **m)
+{
+       return tsrm_mutex_unlock(*((MUTEX_T *) m));
+}
+
+static struct gcry_thread_cbs http_gnutls_tsl = {
+       GCRY_THREAD_OPTION_USER,
+       NULL,
+       http_gnutls_mutex_create,
+       http_gnutls_mutex_destroy,
+       http_gnutls_mutex_lock,
+       http_gnutls_mutex_unlock
+};
 #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)
 {
-#ifdef HTTP_NEED_SSL_TSL
-       http_ssl_init();
+#ifdef HTTP_NEED_OPENSSL_TSL
+       /* mod_ssl, libpq or ext/curl might already have set thread lock callbacks */
+       if (!CRYPTO_get_id_callback()) {
+               int i, c = CRYPTO_num_locks();
+               
+               http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
+               
+               for (i = 0; i < c; ++i) {
+                       http_openssl_tsl[i] = tsrm_mutex_alloc();
+               }
+               
+               CRYPTO_set_id_callback(http_openssl_thread_id);
+               CRYPTO_set_locking_callback(http_openssl_thread_lock);
+       }
+#endif
+#ifdef HTTP_NEED_GNUTLS_TSL
+       gcry_control(GCRYCTL_SET_THREAD_CBS, &http_gnutls_tsl);
 #endif
 
        if (CURLE_OK != curl_global_init(CURL_GLOBAL_ALL)) {
                return FAILURE;
        }
        
+       if (SUCCESS != http_persistent_handle_provide("http_request", safe_curl_init, safe_curl_dtor, safe_curl_copy)) {
+               return FAILURE;
+       }
+       
        HTTP_LONG_CONSTANT("HTTP_AUTH_BASIC", CURLAUTH_BASIC);
        HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST", CURLAUTH_DIGEST);
+#if HTTP_CURL_VERSION(7,19,3)
+       HTTP_LONG_CONSTANT("HTTP_AUTH_DIGEST_IE", CURLAUTH_DIGEST_IE);
+#endif
        HTTP_LONG_CONSTANT("HTTP_AUTH_NTLM", CURLAUTH_NTLM);
+       HTTP_LONG_CONSTANT("HTTP_AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE);
        HTTP_LONG_CONSTANT("HTTP_AUTH_ANY", CURLAUTH_ANY);
        
-       HTTP_LONG_CONSTANT("HTTP_VERSION_NONE", CURL_HTTP_VERSION_NONE);
+       HTTP_LONG_CONSTANT("HTTP_VERSION_NONE", CURL_HTTP_VERSION_NONE); /* to be removed */
        HTTP_LONG_CONSTANT("HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0);
        HTTP_LONG_CONSTANT("HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1);
+       HTTP_LONG_CONSTANT("HTTP_VERSION_ANY", CURL_HTTP_VERSION_NONE);
+       
+       HTTP_LONG_CONSTANT("HTTP_SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1);
+       HTTP_LONG_CONSTANT("HTTP_SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2);
+       HTTP_LONG_CONSTANT("HTTP_SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3);
+       HTTP_LONG_CONSTANT("HTTP_SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT);
+       
+       HTTP_LONG_CONSTANT("HTTP_IPRESOLVE_V4", CURL_IPRESOLVE_V4);
+       HTTP_LONG_CONSTANT("HTTP_IPRESOLVE_V6", CURL_IPRESOLVE_V6);
+       HTTP_LONG_CONSTANT("HTTP_IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER);
 
+#if HTTP_CURL_VERSION(7,15,2)
+       HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS4", CURLPROXY_SOCKS4);
+#endif
+#if HTTP_CURL_VERSION(7,18,0)
+       HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS4A", CURLPROXY_SOCKS4A);
+       HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS5_HOSTNAME", CURLPROXY_SOCKS5_HOSTNAME);
+#endif
+       HTTP_LONG_CONSTANT("HTTP_PROXY_SOCKS5", CURLPROXY_SOCKS5);
+       HTTP_LONG_CONSTANT("HTTP_PROXY_HTTP", CURLPROXY_HTTP);
+#if HTTP_CURL_VERSION(7,19,4)
+       HTTP_LONG_CONSTANT("HTTP_PROXY_HTTP_1_0", CURLPROXY_HTTP_1_0);
+#endif
+
+#if HTTP_CURL_VERSION(7,19,1)
+       HTTP_LONG_CONSTANT("HTTP_POSTREDIR_301", CURL_REDIR_POST_301);
+       HTTP_LONG_CONSTANT("HTTP_POSTREDIR_302", CURL_REDIR_POST_302);
+       HTTP_LONG_CONSTANT("HTTP_POSTREDIR_ALL", CURL_REDIR_POST_ALL);
+#endif
        return SUCCESS;
 }
 /* }}} */
@@ -101,100 +208,25 @@ PHP_MINIT_FUNCTION(http_request)
 PHP_MSHUTDOWN_FUNCTION(http_request)
 {
        curl_global_cleanup();
-#ifdef HTTP_NEED_SSL_TSL
-       http_ssl_cleanup();
+#ifdef HTTP_NEED_OPENSSL_TSL
+       if (http_openssl_tsl) {
+               int i, c = CRYPTO_num_locks();
+                       
+               CRYPTO_set_id_callback(NULL);
+               CRYPTO_set_locking_callback(NULL);
+                       
+               for (i = 0; i < c; ++i) {
+                       tsrm_mutex_free(http_openssl_tsl[i]);
+               }
+                       
+               free(http_openssl_tsl);
+               http_openssl_tsl = NULL;
+       }
 #endif
        return SUCCESS;
 }
 /* }}} */
 
-/* {{{ MACROS */
-#ifndef HAVE_CURL_EASY_STRERROR
-#      define curl_easy_strerror(dummy) "unkown error"
-#endif
-
-#define HTTP_CURL_INFO(I) HTTP_CURL_INFO_EX(I, I)
-#define HTTP_CURL_INFO_EX(I, X) \
-       switch (CURLINFO_ ##I & ~CURLINFO_MASK) \
-       { \
-               case CURLINFO_STRING: \
-               { \
-                       char *c; \
-                       if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &c)) { \
-                               char *key = estrndup(#X, lenof(#X)); \
-                               add_assoc_string(&array, pretty_key(key, lenof(#X), 0, 0), c ? c : "", 1); \
-                               efree(key); \
-                       } \
-               } \
-               break; \
-\
-               case CURLINFO_DOUBLE: \
-               { \
-                       double d; \
-                       if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &d)) { \
-                               char *key = estrndup(#X, lenof(#X)); \
-                               add_assoc_double(&array, pretty_key(key, lenof(#X), 0, 0), d); \
-                               efree(key); \
-                       } \
-               } \
-               break; \
-\
-               case CURLINFO_LONG: \
-               { \
-                       long l; \
-                       if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &l)) { \
-                               char *key = estrndup(#X, lenof(#X)); \
-                               add_assoc_long(&array, pretty_key(key, lenof(#X), 0, 0), l); \
-                               efree(key); \
-                       } \
-               } \
-               break; \
-\
-               case CURLINFO_SLIST: \
-               { \
-                       struct curl_slist *l, *p; \
-                       if (CURLE_OK == curl_easy_getinfo(request->ch, CURLINFO_ ##I, &l)) { \
-                               zval *subarray; \
-                               char *key = estrndup(#X, lenof(#X)); \
-                               MAKE_STD_ZVAL(subarray); \
-                               array_init(subarray); \
-                               for (p = l; p; p = p->next) { \
-                                       add_next_index_string(subarray, p->data, 1); \
-                               } \
-                               add_assoc_zval(&array, pretty_key(key, lenof(#X), 0, 0), subarray); \
-                               curl_slist_free_all(l); \
-                               efree(key); \
-                       } \
-               } \
-       }
-
-#define HTTP_CURL_OPT(OPTION, p) HTTP_CURL_OPT_EX(request->ch, OPTION, (p))
-#define HTTP_CURL_OPT_EX(ch, OPTION, p) curl_easy_setopt((ch), CURLOPT_##OPTION, (p))
-#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)) { \
-               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; \
-       }
-#define HTTP_CURL_OPT_LONG(keyname) HTTP_OPT_SSL_LONG_EX(keyname, keyname)
-#define HTTP_CURL_OPT_SSL_LONG(keyname) HTTP_CURL_OPT_LONG_EX(keyname, SSL##keyname)
-#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)) { \
-               zval *copy = http_request_option_cache(request, #keyname, zval_copy(IS_LONG, *param)); \
-               HTTP_CURL_OPT(optname, Z_LVAL_P(copy)); \
-               key = NULL; \
-               continue; \
-       }
-/* }}} */
-
 /* {{{ forward declarations */
 #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)
@@ -203,6 +235,9 @@ static inline zval *_http_request_option_ex(http_request *request, HashTable *op
 #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);
 
+#define http_request_cookies_enabled(r) _http_request_cookies_enabled((r))
+static inline int _http_request_cookies_enabled(http_request *r);
+
 static size_t http_curl_read_callback(void *, size_t, size_t, void *);
 static int http_curl_progress_callback(void *, double, double, double, double);
 static int http_curl_raw_callback(CURL *, curl_infotype, char *, size_t, void *);
@@ -211,41 +246,59 @@ static curlioerr http_curl_ioctl_callback(CURL *, curliocmd, void *);
 /* }}} */
 
 /* {{{ CURL *http_curl_init(http_request *) */
-PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, void *context, char *error_buffer)
+PHP_HTTP_API CURL * _http_curl_init_ex(CURL *ch, http_request *request TSRMLS_DC)
 {
-       if (ch || (ch = curl_easy_init())) {
+       if (ch || (SUCCESS == http_persistent_handle_acquire("http_request", &ch))) {
 #if defined(ZTS)
-               HTTP_CURL_OPT_EX(ch, NOSIGNAL, 1);
-#endif
-               HTTP_CURL_OPT_EX(ch, HEADER, 0);
-               HTTP_CURL_OPT_EX(ch, FILETIME, 1);
-               HTTP_CURL_OPT_EX(ch, AUTOREFERER, 1);
-               HTTP_CURL_OPT_EX(ch, VERBOSE, 1);
-               HTTP_CURL_OPT_EX(ch, HEADERFUNCTION, NULL);
-               HTTP_CURL_OPT_EX(ch, DEBUGFUNCTION, http_curl_raw_callback);
-               HTTP_CURL_OPT_EX(ch, READFUNCTION, http_curl_read_callback);
-               HTTP_CURL_OPT_EX(ch, IOCTLFUNCTION, http_curl_ioctl_callback);
-               HTTP_CURL_OPT_EX(ch, WRITEFUNCTION, http_curl_dummy_callback);
-               HTTP_CURL_OPT_EX(ch, DEBUGDATA, context);
-               HTTP_CURL_OPT_EX(ch, PRIVATE, context);
-               HTTP_CURL_OPT_EX(ch, ERRORBUFFER, error_buffer);
+               curl_easy_setopt(ch, CURLOPT_NOSIGNAL, 1L);
+#endif
+               curl_easy_setopt(ch, CURLOPT_HEADER, 0L);
+               curl_easy_setopt(ch, CURLOPT_FILETIME, 1L);
+               curl_easy_setopt(ch, CURLOPT_AUTOREFERER, 1L);
+               curl_easy_setopt(ch, CURLOPT_VERBOSE, 1L);
+               curl_easy_setopt(ch, CURLOPT_HEADERFUNCTION, NULL);
+               curl_easy_setopt(ch, CURLOPT_DEBUGFUNCTION, http_curl_raw_callback);
+               curl_easy_setopt(ch, CURLOPT_READFUNCTION, http_curl_read_callback);
+               curl_easy_setopt(ch, CURLOPT_IOCTLFUNCTION, http_curl_ioctl_callback);
+               curl_easy_setopt(ch, CURLOPT_WRITEFUNCTION, http_curl_dummy_callback);
+               
+               /* set context */
+               if (request) {
+                       curl_easy_setopt(ch, CURLOPT_DEBUGDATA, request);
+                       
+                       /* attach curl handle */
+                       request->ch = ch;
+                       /* set defaults (also in http_request_reset()) */
+                       http_request_defaults(request);
+               }
        }
        
        return ch;
 }
 /* }}} */
 
+/* {{{ 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) {
-               /* avoid nasty segfaults with already cleaned up callbacks */
-               HTTP_CURL_OPT_EX(*ch, NOPROGRESS, 1);
-               HTTP_CURL_OPT_EX(*ch, PROGRESSFUNCTION, NULL);
-               HTTP_CURL_OPT_EX(*ch, VERBOSE, 0);
-               HTTP_CURL_OPT_EX(*ch, DEBUGFUNCTION, NULL);
-               curl_easy_cleanup(*ch);
-               *ch = NULL;
+               curl_easy_setopt(*ch, CURLOPT_NOPROGRESS, 1L);
+               curl_easy_setopt(*ch, CURLOPT_PROGRESSFUNCTION, NULL);
+               curl_easy_setopt(*ch, CURLOPT_VERBOSE, 0L);
+               curl_easy_setopt(*ch, CURLOPT_DEBUGFUNCTION, NULL);
+               
+               http_persistent_handle_release("http_request", ch);
        }
 }
 /* }}} */
@@ -282,8 +335,8 @@ 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);
+       http_curl_free(&request->ch);
        
        phpstr_dtor(&request->_cache.cookies);
        zend_hash_destroy(&request->_cache.options);
@@ -320,88 +373,234 @@ 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';
+               }
+       }
+}
+/* }}} */
+
+/* {{{ STATUS http_request_enable_cookies(http_request *) */
+PHP_HTTP_API STATUS _http_request_enable_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 (initialized && (http_request_cookies_enabled(request) || (CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIEFILE, "")))) {
+               return SUCCESS;
+       }
+       http_error(HE_WARNING, HTTP_E_REQUEST, "Could not enable cookies for this session");
+       return FAILURE;
+}
+/* }}} */
+
+/* {{{ STATUS http_request_reset_cookies(http_request *, int) */
+PHP_HTTP_API STATUS _http_request_reset_cookies(http_request *request, int session_only)
+{
+       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 (initialized) {
+               if (!http_request_cookies_enabled(request)) {
+                       if (SUCCESS != http_request_enable_cookies(request)) {
+                               return FAILURE;
+                       }
+               }
+               if (session_only) {
+#if HTTP_CURL_VERSION(7,15,4)
+                       if (CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIELIST, "SESS")) {
+                               return SUCCESS;
+                       }
+#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 (CURLE_OK == curl_easy_setopt(request->ch, CURLOPT_COOKIELIST, "ALL")) {
+                               return SUCCESS;
+                       }
+#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 (initialized) {
+               if (!http_request_cookies_enabled(request)) {
+                       return FAILURE;
+               }
+#if HTTP_CURL_VERSION(7,17,1)
+               if (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)
 {
        if (request->ch) {
-               HTTP_CURL_OPT(PROGRESSFUNCTION, NULL);
-               HTTP_CURL_OPT(URL, NULL);
-               HTTP_CURL_OPT(NOPROGRESS, 1);
-               HTTP_CURL_OPT(PROXY, NULL);
-               HTTP_CURL_OPT(PROXYPORT, 0);
-               HTTP_CURL_OPT(PROXYUSERPWD, NULL);
-               HTTP_CURL_OPT(PROXYAUTH, 0);
-               HTTP_CURL_OPT(INTERFACE, NULL);
-               HTTP_CURL_OPT(PORT, 0);
-               HTTP_CURL_OPT(USERPWD, NULL);
-               HTTP_CURL_OPT(HTTPAUTH, 0);
-               HTTP_CURL_OPT(ENCODING, NULL);
-               HTTP_CURL_OPT(FOLLOWLOCATION, 0);
-               HTTP_CURL_OPT(UNRESTRICTED_AUTH, 0);
-               HTTP_CURL_OPT(REFERER, NULL);
-               HTTP_CURL_OPT(USERAGENT, "PECL::HTTP/" PHP_EXT_HTTP_VERSION " (PHP/" PHP_VERSION ")");
-               HTTP_CURL_OPT(HTTPHEADER, NULL);
-               HTTP_CURL_OPT(COOKIE, NULL);
-#if LIBCURL_VERSION_NUM >= 0x070e01
-               HTTP_CURL_OPT(COOKIELIST, NULL);
-#endif
-               HTTP_CURL_OPT(COOKIEFILE, NULL);
-               HTTP_CURL_OPT(COOKIEJAR, NULL);
-               HTTP_CURL_OPT(RESUME_FROM, 0);
-               HTTP_CURL_OPT(MAXFILESIZE, 0);
-               HTTP_CURL_OPT(TIMECONDITION, 0);
-               HTTP_CURL_OPT(TIMEVALUE, 0);
-               HTTP_CURL_OPT(TIMEOUT, 0);
-               HTTP_CURL_OPT(CONNECTTIMEOUT, 3);
-               HTTP_CURL_OPT(SSLCERT, NULL);
-               HTTP_CURL_OPT(SSLCERTTYPE, NULL);
-               HTTP_CURL_OPT(SSLCERTPASSWD, NULL);
-               HTTP_CURL_OPT(SSLKEY, NULL);
-               HTTP_CURL_OPT(SSLKEYTYPE, NULL);
-               HTTP_CURL_OPT(SSLKEYPASSWD, NULL);
-               HTTP_CURL_OPT(SSLENGINE, NULL);
-               HTTP_CURL_OPT(SSLVERSION, 0);
-               HTTP_CURL_OPT(SSL_VERIFYPEER, 0);
-               HTTP_CURL_OPT(SSL_VERIFYHOST, 0);
-               HTTP_CURL_OPT(SSL_CIPHER_LIST, NULL);
-               HTTP_CURL_OPT(CAINFO, NULL);
-               HTTP_CURL_OPT(CAPATH, NULL);
-               HTTP_CURL_OPT(RANDOM_FILE, NULL);
-               HTTP_CURL_OPT(EGDSOCKET, NULL);
-               HTTP_CURL_OPT(POSTFIELDS, NULL);
-               HTTP_CURL_OPT(POSTFIELDSIZE, 0);
-               HTTP_CURL_OPT(HTTPPOST, NULL);
-               HTTP_CURL_OPT(IOCTLDATA, NULL);
-               HTTP_CURL_OPT(READDATA, NULL);
-               HTTP_CURL_OPT(INFILESIZE, 0);
-               HTTP_CURL_OPT(HTTP_VERSION, CURL_HTTP_VERSION_NONE);
+               HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, NULL);
+               HTTP_CURL_OPT(CURLOPT_URL, NULL);
+               HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 1L);
+#if HTTP_CURL_VERSION(7,19,4)
+               HTTP_CURL_OPT(CURLOPT_NOPROXY, NULL);
+#endif
+               HTTP_CURL_OPT(CURLOPT_PROXY, NULL);
+               HTTP_CURL_OPT(CURLOPT_PROXYPORT, 0L);
+               HTTP_CURL_OPT(CURLOPT_PROXYTYPE, 0L);
+               /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
+#if HTTP_CURL_VERSION(7,19,1)          
+               HTTP_CURL_OPT(CURLOPT_PROXYUSERNAME, NULL);
+               HTTP_CURL_OPT(CURLOPT_PROXYPASSWORD, NULL);
+#endif
+               HTTP_CURL_OPT(CURLOPT_PROXYAUTH, 0L);
+               HTTP_CURL_OPT(CURLOPT_HTTPPROXYTUNNEL, 0L);
+               HTTP_CURL_OPT(CURLOPT_DNS_CACHE_TIMEOUT, 60L);
+               HTTP_CURL_OPT(CURLOPT_IPRESOLVE, 0);
+               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); */
+               HTTP_CURL_OPT(CURLOPT_FRESH_CONNECT, 0L);
+               HTTP_CURL_OPT(CURLOPT_FORBID_REUSE, 0L);
+               HTTP_CURL_OPT(CURLOPT_INTERFACE, NULL);
+               HTTP_CURL_OPT(CURLOPT_PORT, 0L);
+#if HTTP_CURL_VERSION(7,19,0)
+               HTTP_CURL_OPT(CURLOPT_ADDRESS_SCOPE, 0L);
+#endif
+#if HTTP_CURL_VERSION(7,15,2)
+               HTTP_CURL_OPT(CURLOPT_LOCALPORT, 0L);
+               HTTP_CURL_OPT(CURLOPT_LOCALPORTRANGE, 0L);
+#endif
+               /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
+#if HTTP_CURL_VERSION(7,19,1)
+               HTTP_CURL_OPT(CURLOPT_USERNAME, NULL);
+               HTTP_CURL_OPT(CURLOPT_PASSWORD, NULL);
+#endif
+               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);
+#if HTTP_CURL_VERSION(7,19,1)
+               HTTP_CURL_OPT(CURLOPT_POSTREDIR, 0L);
+#elif HTTP_CURL_VERSION(7,17,1)
+               HTTP_CURL_OPT(CURLOPT_POST301, 0L);
+#endif
+               HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH, 0L);
+               HTTP_CURL_OPT(CURLOPT_REFERER, NULL);
+               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);
+               HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 0L);
+               HTTP_CURL_OPT(CURLOPT_COOKIEFILE, NULL);
+               HTTP_CURL_OPT(CURLOPT_COOKIEJAR, NULL);
+#if HTTP_CURL_VERSION(7,14,1)
+               HTTP_CURL_OPT(CURLOPT_COOKIELIST, NULL);
+#endif
+               HTTP_CURL_OPT(CURLOPT_RANGE, NULL);
+               HTTP_CURL_OPT(CURLOPT_RESUME_FROM, 0L);
+               HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, 0L);
+               HTTP_CURL_OPT(CURLOPT_TIMECONDITION, 0L);
+               HTTP_CURL_OPT(CURLOPT_TIMEVALUE, 0L);
+               HTTP_CURL_OPT(CURLOPT_TIMEOUT, 0L);
+               HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT, 3);
+               HTTP_CURL_OPT(CURLOPT_SSLCERT, NULL);
+               HTTP_CURL_OPT(CURLOPT_SSLCERTTYPE, NULL);
+               HTTP_CURL_OPT(CURLOPT_SSLCERTPASSWD, NULL);
+               HTTP_CURL_OPT(CURLOPT_SSLKEY, NULL);
+               HTTP_CURL_OPT(CURLOPT_SSLKEYTYPE, NULL);
+               HTTP_CURL_OPT(CURLOPT_SSLKEYPASSWD, NULL);
+               HTTP_CURL_OPT(CURLOPT_SSLENGINE, NULL);
+               HTTP_CURL_OPT(CURLOPT_SSLVERSION, 0L);
+               HTTP_CURL_OPT(CURLOPT_SSL_VERIFYPEER, 0L);
+               HTTP_CURL_OPT(CURLOPT_SSL_VERIFYHOST, 0L);
+               HTTP_CURL_OPT(CURLOPT_SSL_CIPHER_LIST, NULL);
+#if HTTP_CURL_VERSION(7,19,0)
+               HTTP_CURL_OPT(CURLOPT_ISSUERCERT, NULL);
+       #if defined(HTTP_HAVE_OPENSSL)
+               HTTP_CURL_OPT(CURLOPT_CRLFILE, NULL);
+       #endif
+#endif
+#if HTTP_CURL_VERSION(7,19,1) && defined(HTTP_HAVE_OPENSSL)
+               HTTP_CURL_OPT(CURLOPT_CERTINFO, NULL);
+#endif
+#ifdef HTTP_CURL_CAINFO
+               HTTP_CURL_OPT(CURLOPT_CAINFO, HTTP_CURL_CAINFO);
+#else
+               HTTP_CURL_OPT(CURLOPT_CAINFO, NULL);
+#endif
+               HTTP_CURL_OPT(CURLOPT_CAPATH, NULL);
+               HTTP_CURL_OPT(CURLOPT_RANDOM_FILE, NULL);
+               HTTP_CURL_OPT(CURLOPT_EGDSOCKET, NULL);
+               HTTP_CURL_OPT(CURLOPT_POSTFIELDS, NULL);
+               HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE, 0L);
+               HTTP_CURL_OPT(CURLOPT_HTTPPOST, NULL);
+               HTTP_CURL_OPT(CURLOPT_IOCTLDATA, NULL);
+               HTTP_CURL_OPT(CURLOPT_READDATA, NULL);
+               HTTP_CURL_OPT(CURLOPT_INFILESIZE, 0L);
+               HTTP_CURL_OPT(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_NONE);
+               HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST, NULL);
+               HTTP_CURL_OPT(CURLOPT_NOBODY, 0L);
+               HTTP_CURL_OPT(CURLOPT_POST, 0L);
+               HTTP_CURL_OPT(CURLOPT_UPLOAD, 0L);
+               HTTP_CURL_OPT(CURLOPT_HTTPGET, 1L);
        }
 }
 /* }}} */
 
 PHP_HTTP_API void _http_request_set_progress_callback(http_request *request, zval *cb)
 {
-       TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
-       
        if (request->_progress_callback) {
                zval_ptr_dtor(&request->_progress_callback);
        }
        if ((request->_progress_callback = cb)) {
                ZVAL_ADDREF(cb);
-               HTTP_CURL_OPT(NOPROGRESS, 0);
-               HTTP_CURL_OPT(PROGRESSDATA, request);
-               HTTP_CURL_OPT(PROGRESSFUNCTION, http_curl_progress_callback);
+               HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 0);
+               HTTP_CURL_OPT(CURLOPT_PROGRESSDATA, request);
+               HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, http_curl_progress_callback);
        } else {
-               HTTP_CURL_OPT(NOPROGRESS, 1);
-               HTTP_CURL_OPT(PROGRESSDATA, NULL);
-               HTTP_CURL_OPT(PROGRESSFUNCTION, NULL);
+               HTTP_CURL_OPT(CURLOPT_NOPROGRESS, 1);
+               HTTP_CURL_OPT(CURLOPT_PROGRESSDATA, NULL);
+               HTTP_CURL_OPT(CURLOPT_PROGRESSFUNCTION, NULL);
        }
 }
 
@@ -410,14 +609,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);
-       http_request_defaults(request);
        
+       if (!(storage = http_request_storage_get(request->ch))) {
+               return FAILURE;
+       }
+       storage->errorbuffer[0] = '\0';
        /* set options */
-       HTTP_CURL_OPT(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))) {
@@ -426,59 +633,207 @@ 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(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));
                }
-
                /* port */
                if ((zoption = http_request_option(request, options, "proxyport", IS_LONG))) {
-                       HTTP_CURL_OPT(PROXYPORT, Z_LVAL_P(zoption));
+                       HTTP_CURL_OPT(CURLOPT_PROXYPORT, Z_LVAL_P(zoption));
                }
                /* user:pass */
                if ((zoption = http_request_option(request, options, "proxyauth", IS_STRING)) && Z_STRLEN_P(zoption)) {
-                       HTTP_CURL_OPT(PROXYUSERPWD, Z_STRVAL_P(zoption));
+                       HTTP_CURL_OPT(CURLOPT_PROXYUSERPWD, Z_STRVAL_P(zoption));
                }
                /* auth method */
                if ((zoption = http_request_option(request, options, "proxyauthtype", IS_LONG))) {
-                       HTTP_CURL_OPT(PROXYAUTH, Z_LVAL_P(zoption));
+                       HTTP_CURL_OPT(CURLOPT_PROXYAUTH, Z_LVAL_P(zoption));
                }
+               /* tunnel */
+               if ((zoption = http_request_option(request, options, "proxytunnel", IS_BOOL)) && Z_BVAL_P(zoption)) {
+                       HTTP_CURL_OPT(CURLOPT_HTTPPROXYTUNNEL, 1L);
+               }
+       }
+#if HTTP_CURL_VERSION(7,19,4)
+       if ((zoption = http_request_option(request, options, "noproxy", IS_STRING))) {
+               HTTP_CURL_OPT(CURLOPT_NOPROXY, Z_STRVAL_P(zoption));
        }
+#endif
 
+       /* dns */
+       if ((zoption = http_request_option(request, options, "dns_cache_timeout", IS_LONG))) {
+               HTTP_CURL_OPT(CURLOPT_DNS_CACHE_TIMEOUT, Z_LVAL_P(zoption));
+       }
+       if ((zoption = http_request_option(request, options, "ipresolve", IS_LONG)) && Z_LVAL_P(zoption)) {
+               HTTP_CURL_OPT(CURLOPT_IPRESOLVE, Z_LVAL_P(zoption));
+       }
+       
+       /* limits */
+       if ((zoption = http_request_option(request, options, "low_speed_limit", IS_LONG))) {
+               HTTP_CURL_OPT(CURLOPT_LOW_SPEED_LIMIT, Z_LVAL_P(zoption));
+       }
+       if ((zoption = http_request_option(request, options, "low_speed_time", IS_LONG))) {
+               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))) {
+               HTTP_CURL_OPT(CURLOPT_MAXCONNECTS, Z_LVAL_P(zoption));
+       } */
+       if ((zoption = http_request_option(request, options, "fresh_connect", IS_BOOL)) && Z_BVAL_P(zoption)) {
+               HTTP_CURL_OPT(CURLOPT_FRESH_CONNECT, 1L);
+       }
+       if ((zoption = http_request_option(request, options, "forbid_reuse", IS_BOOL)) && Z_BVAL_P(zoption)) {
+               HTTP_CURL_OPT(CURLOPT_FORBID_REUSE, 1L);
+       }
+       
        /* outgoing interface */
        if ((zoption = http_request_option(request, options, "interface", IS_STRING))) {
-               HTTP_CURL_OPT(INTERFACE, Z_STRVAL_P(zoption));
+               HTTP_CURL_OPT(CURLOPT_INTERFACE, Z_STRVAL_P(zoption));
+               
+#if HTTP_CURL_VERSION(7,15,2)
+               if ((zoption = http_request_option(request, options, "portrange", IS_ARRAY))) {
+                       zval **prs, **pre;
+                       
+                       zend_hash_internal_pointer_reset(Z_ARRVAL_P(zoption));
+                       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 = http_zsep(IS_LONG, *prs);
+                                       zval *pre_cpy = http_zsep(IS_LONG, *pre);
+                                       
+                                       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_ptr_dtor(&prs_cpy);
+                                       zval_ptr_dtor(&pre_cpy);
+                               }
+                       }
+               }
+#endif
        }
 
        /* another port */
        if ((zoption = http_request_option(request, options, "port", IS_LONG))) {
-               HTTP_CURL_OPT(PORT, Z_LVAL_P(zoption));
+               HTTP_CURL_OPT(CURLOPT_PORT, Z_LVAL_P(zoption));
+       }
+       
+       /* RFC4007 zone_id */
+#if HTTP_CURL_VERSION(7,19,0)
+       if ((zoption = http_request_option(request, options, "address_scope", IS_LONG))) {
+               HTTP_CURL_OPT(CURLOPT_ADDRESS_SCOPE, Z_LVAL_P(zoption));
        }
+#endif
 
        /* auth */
        if ((zoption = http_request_option(request, options, "httpauth", IS_STRING)) && Z_STRLEN_P(zoption)) {
-               HTTP_CURL_OPT(USERPWD, Z_STRVAL_P(zoption));
+               HTTP_CURL_OPT(CURLOPT_USERPWD, Z_STRVAL_P(zoption));
        }
        if ((zoption = http_request_option(request, options, "httpauthtype", IS_LONG))) {
-               HTTP_CURL_OPT(HTTPAUTH, Z_LVAL_P(zoption));
+               HTTP_CURL_OPT(CURLOPT_HTTPAUTH, Z_LVAL_P(zoption));
        }
 
        /* redirects, defaults to 0 */
        if ((zoption = http_request_option(request, options, "redirect", IS_LONG))) {
-               HTTP_CURL_OPT(FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1 : 0);
-               HTTP_CURL_OPT(MAXREDIRS, Z_LVAL_P(zoption));
+               HTTP_CURL_OPT(CURLOPT_FOLLOWLOCATION, Z_LVAL_P(zoption) ? 1L : 0L);
+               HTTP_CURL_OPT(CURLOPT_MAXREDIRS, Z_LVAL_P(zoption));
                if ((zoption = http_request_option(request, options, "unrestrictedauth", IS_BOOL))) {
-                       HTTP_CURL_OPT(UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
+                       HTTP_CURL_OPT(CURLOPT_UNRESTRICTED_AUTH, Z_LVAL_P(zoption));
+               }
+#if HTTP_CURL_VERSION(7,17,1)
+               if ((zoption = http_request_option(request, options, "postredir", IS_BOOL))) {
+#      if HTTP_CURL_VERSION(7,19,1)
+                       HTTP_CURL_OPT(CURLOPT_POSTREDIR, Z_BVAL_P(zoption) ? 1L : 0L);
+#      else
+                       HTTP_CURL_OPT(CURLOPT_POST301, Z_BVAL_P(zoption) ? 1L : 0L);
+#      endif
                }
+#endif
+       }
+       
+       /* 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)) {
-               HTTP_CURL_OPT(REFERER, Z_STRVAL_P(zoption));
+               HTTP_CURL_OPT(CURLOPT_REFERER, Z_STRVAL_P(zoption));
        }
 
        /* useragent, default "PECL::HTTP/version (PHP/version)" */
-       if ((zoption = http_request_option(request, options, "useragent", IS_STRING)) && Z_STRLEN_P(zoption)) {
-               HTTP_CURL_OPT(USERAGENT, Z_STRVAL_P(zoption));
+       if ((zoption = http_request_option(request, options, "useragent", IS_STRING))) {
+               /* allow to send no user agent, not even default one */
+               if (Z_STRLEN_P(zoption)) {
+                       HTTP_CURL_OPT(CURLOPT_USERAGENT, Z_STRVAL_P(zoption));
+               } else {
+                       HTTP_CURL_OPT(CURLOPT_USERAGENT, NULL);
+               }
+       }
+
+       /* resume */
+       if ((zoption = http_request_option(request, options, "resume", IS_LONG)) && (Z_LVAL_P(zoption) > 0)) {
+               range_req = 1;
+               HTTP_CURL_OPT(CURLOPT_RESUME_FROM, Z_LVAL_P(zoption));
+       }
+       /* or range of kind array(array(0,499), array(100,1499)) */
+       else if ((zoption = http_request_option(request, options, "range", IS_ARRAY)) && zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
+               HashPosition pos1, pos2;
+               zval **rr, **rb, **re;
+               phpstr rs;
+               
+               phpstr_init(&rs);
+               FOREACH_VAL(pos1, zoption, rr) {
+                       if (Z_TYPE_PP(rr) == IS_ARRAY) {
+                               zend_hash_internal_pointer_reset_ex(Z_ARRVAL_PP(rr), &pos2);
+                               if (SUCCESS == zend_hash_get_current_data_ex(Z_ARRVAL_PP(rr), (void *) &rb, &pos2)) {
+                                       zend_hash_move_forward_ex(Z_ARRVAL_PP(rr), &pos2);
+                                       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 = http_zsep(IS_LONG, *rb);
+                                                       zval *rel = http_zsep(IS_LONG, *re);
+                                                       
+                                                       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_ptr_dtor(&rbl);
+                                                       zval_ptr_dtor(&rel);
+                                               }
+                                       }
+                               }
+                       }
+               }
+               
+               if (PHPSTR_LEN(&rs)) {
+                       zval *cached_range;
+                       
+                       /* ditch last comma */
+                       PHPSTR_VAL(&rs)[PHPSTR_LEN(&rs)-- -1] = '\0';
+                       /* cache string */
+                       MAKE_STD_ZVAL(cached_range);
+                       ZVAL_STRINGL(cached_range, PHPSTR_VAL(&rs), PHPSTR_LEN(&rs), 0);
+                       HTTP_CURL_OPT(CURLOPT_RANGE, Z_STRVAL_P(http_request_option_cache(request, "range", cached_range)));
+                       zval_ptr_dtor(&cached_range);
+               }
        }
 
        /* additional headers, array('name' => 'value') */
@@ -487,201 +842,231 @@ PHP_HTTP_API STATUS _http_request_prepare(http_request *request, HashTable *opti
                request->_cache.headers = NULL;
        }
        if ((zoption = http_request_option(request, options, "headers", IS_ARRAY))) {
-               char *header_key;
-               ulong header_idx;
+               HashKey header_key = initHashKey(0);
+               zval **header_val;
                HashPosition pos;
 
-               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_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);
-                                       zval_ptr_dtor(header_val);
+               FOREACH_KEYVAL(pos, zoption, header_key, header_val) {
+                       if (header_key.type == HASH_KEY_IS_STRING) {
+                               char header[1024];
+                               zval *header_cpy = http_zsep(IS_STRING, *header_val);
+                               
+                               if (!strcasecmp(header_key.str, "range")) {
+                                       range_req = 1;
                                }
-
-                               /* reset */
-                               header_key = NULL;
+                               snprintf(header, sizeof(header), "%s: %s", header_key.str, Z_STRVAL_P(header_cpy));
+                               request->_cache.headers = curl_slist_append(request->_cache.headers, header);
+                               zval_ptr_dtor(&header_cpy);
                        }
                }
        }
+       /* etag */
+       if ((zoption = http_request_option(request, options, "etag", IS_STRING)) && Z_STRLEN_P(zoption)) {
+               char match_header[1024], *quoted_etag = NULL;
+               
+               if ((Z_STRVAL_P(zoption)[0] != '"') || (Z_STRVAL_P(zoption)[Z_STRLEN_P(zoption)-1] != '"')) {
+                       spprintf(&quoted_etag, 0, "\"%s\"", Z_STRVAL_P(zoption));
+               }
+               snprintf(match_header, sizeof(match_header), "%s: %s", range_req?"If-Match":"If-None-Match", quoted_etag?quoted_etag:Z_STRVAL_P(zoption));
+               request->_cache.headers = curl_slist_append(request->_cache.headers, match_header);
+               STR_FREE(quoted_etag);
+       }
+       /* compression */
        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");
        }
-       HTTP_CURL_OPT(HTTPHEADER, request->_cache.headers);
+       HTTP_CURL_OPT(CURLOPT_HTTPHEADER, request->_cache.headers);
+
+       /* lastmodified */
+       if ((zoption = http_request_option(request, options, "lastmodified", IS_LONG))) {
+               if (Z_LVAL_P(zoption)) {
+                       if (Z_LVAL_P(zoption) > 0) {
+                               HTTP_CURL_OPT(CURLOPT_TIMEVALUE, Z_LVAL_P(zoption));
+                       } else {
+                               HTTP_CURL_OPT(CURLOPT_TIMEVALUE, (long) HTTP_G->request.time + Z_LVAL_P(zoption));
+                       }
+                       HTTP_CURL_OPT(CURLOPT_TIMECONDITION, (long) (range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE));
+               } else {
+                       HTTP_CURL_OPT(CURLOPT_TIMECONDITION, CURL_TIMECOND_NONE);
+               }
+       }
 
        /* cookies, array('name' => 'value') */
        if ((zoption = http_request_option(request, options, "cookies", IS_ARRAY))) {
                phpstr_dtor(&request->_cache.cookies);
                if (zend_hash_num_elements(Z_ARRVAL_P(zoption))) {
-                       if (SUCCESS == http_urlencode_hash_recursive(HASH_OF(zoption), &request->_cache.cookies, "; ", sizeof("; ")-1, NULL, 0)) {
+                       zval *urlenc_cookies = NULL;
+                       /* check whether cookies should not be urlencoded; default is to urlencode them */
+                       if ((!(urlenc_cookies = http_request_option(request, options, "encodecookies", IS_BOOL))) || Z_BVAL_P(urlenc_cookies)) {
+                               if (SUCCESS == http_urlencode_hash_recursive(HASH_OF(zoption), &request->_cache.cookies, "; ", lenof("; "), NULL, 0)) {
+                                       phpstr_fix(&request->_cache.cookies);
+                                       HTTP_CURL_OPT(CURLOPT_COOKIE, request->_cache.cookies.data);
+                               }
+                       } else {
+                               HashPosition pos;
+                               HashKey cookie_key = initHashKey(0);
+                               zval **cookie_val;
+                               
+                               FOREACH_KEYVAL(pos, zoption, cookie_key, cookie_val) {
+                                       if (cookie_key.type == HASH_KEY_IS_STRING) {
+                                               zval *val = http_zsep(IS_STRING, *cookie_val);
+                                               phpstr_appendf(&request->_cache.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(val));
+                                               zval_ptr_dtor(&val);
+                                       }
+                               }
+                               
                                phpstr_fix(&request->_cache.cookies);
-                               HTTP_CURL_OPT(COOKIE, request->_cache.cookies.data);
+                               if (PHPSTR_LEN(&request->_cache.cookies)) {
+                                       HTTP_CURL_OPT(CURLOPT_COOKIE, PHPSTR_VAL(&request->_cache.cookies));
+                               }
                        }
                }
        }
 
-#if LIBCURL_VERSION_NUM >= 0x070e01
-       /* 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)) {
-                       /* accept cookies for this session */
-                       HTTP_CURL_OPT(COOKIEFILE, "");
-               } else {
-                       /* reset session cookies */
-                       HTTP_CURL_OPT(COOKIESESSION, 1);
-               }
+       /* don't load session cookies from cookiestore */
+       if ((zoption = http_request_option(request, options, "cookiesession", IS_BOOL)) && Z_BVAL_P(zoption)) {
+               HTTP_CURL_OPT(CURLOPT_COOKIESESSION, 1L);
        }
 
        /* 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));
-       }
-
-       /* resume */
-       if ((zoption = http_request_option(request, options, "resume", IS_LONG)) && (Z_LVAL_P(zoption) != 0)) {
-               range_req = 1;
-               HTTP_CURL_OPT(RESUME_FROM, Z_LVAL_P(zoption));
+       if ((zoption = http_request_option(request, options, "cookiestore", IS_STRING))) {
+               if (Z_STRLEN_P(zoption)) {
+                       HTTP_CHECK_OPEN_BASEDIR(Z_STRVAL_P(zoption), return FAILURE);
+               }
+               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 */
        if ((zoption = http_request_option(request, options, "maxfilesize", IS_LONG))) {
-               HTTP_CURL_OPT(MAXFILESIZE, Z_LVAL_P(zoption));
+               HTTP_CURL_OPT(CURLOPT_MAXFILESIZE, Z_LVAL_P(zoption));
        }
 
        /* http protocol */
        if ((zoption = http_request_option(request, options, "protocol", IS_LONG))) {
-               HTTP_CURL_OPT(HTTP_VERSION, Z_LVAL_P(zoption));
+               HTTP_CURL_OPT(CURLOPT_HTTP_VERSION, Z_LVAL_P(zoption));
        }
 
-       /* lastmodified */
-       if ((zoption = http_request_option(request, options, "lastmodified", IS_LONG))) {
-               if (Z_LVAL_P(zoption)) {
-                       if (Z_LVAL_P(zoption) > 0) {
-                               HTTP_CURL_OPT(TIMEVALUE, Z_LVAL_P(zoption));
-                       } else {
-                               HTTP_CURL_OPT(TIMEVALUE, time(NULL) + Z_LVAL_P(zoption));
-                       }
-                       HTTP_CURL_OPT(TIMECONDITION, range_req ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE);
-               } else {
-                       HTTP_CURL_OPT(TIMECONDITION, CURL_TIMECOND_NONE);
-               }
+#if HTTP_CURL_VERSION(7,16,2)
+       /* timeout, defaults to 0 */
+       if ((zoption = http_request_option(request, options, "timeout", IS_DOUBLE))) {
+               HTTP_CURL_OPT(CURLOPT_TIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
        }
-
+       /* connecttimeout, defaults to 0 */
+       if ((zoption = http_request_option(request, options, "connecttimeout", IS_DOUBLE))) {
+               HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT_MS, (long)(Z_DVAL_P(zoption)*1000));
+       }
+#else
        /* timeout, defaults to 0 */
        if ((zoption = http_request_option(request, options, "timeout", IS_LONG))) {
-               HTTP_CURL_OPT(TIMEOUT, Z_LVAL_P(zoption));
+               HTTP_CURL_OPT(CURLOPT_TIMEOUT, Z_LVAL_P(zoption));
        }
-
-       /* connecttimeout, defaults to 3 */
+       /* connecttimeout, defaults to 0 */
        if ((zoption = http_request_option(request, options, "connecttimeout", IS_LONG))) {
-               HTTP_CURL_OPT(CONNECTTIMEOUT, Z_LVAL_P(zoption));
+               HTTP_CURL_OPT(CURLOPT_CONNECTTIMEOUT, Z_LVAL_P(zoption));
        }
+#endif
 
        /* ssl */
        if ((zoption = http_request_option(request, options, "ssl", IS_ARRAY))) {
-               ulong idx;
-               char *key = NULL;
+               HashKey key = initHashKey(0);
                zval **param;
                HashPosition pos;
 
-               FOREACH_KEYVAL(pos, zoption, key, idx, param) {
-                       if (key) {
-                               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, 0);
-                               HTTP_CURL_OPT_SSL_STRING(KEYTYPE, 0);
-                               HTTP_CURL_OPT_SSL_STRING(KEYPASSWD, 0);
-
-                               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, 0);
-
-                               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;
+               FOREACH_KEYVAL(pos, zoption, key, param) {
+                       if (key.type == HASH_KEY_IS_STRING) {
+                               HTTP_CURL_OPT_STRING(CURLOPT_SSLCERT, 0, 1);
+                               HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTTYPE, 0, 0);
+                               HTTP_CURL_OPT_STRING(CURLOPT_SSLCERTPASSWD, 0, 0);
+
+                               HTTP_CURL_OPT_STRING(CURLOPT_SSLKEY, 0, 0);
+                               HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYTYPE, 0, 0);
+                               HTTP_CURL_OPT_STRING(CURLOPT_SSLKEYPASSWD, 0, 0);
+
+                               HTTP_CURL_OPT_STRING(CURLOPT_SSLENGINE, 0, 0);
+                               HTTP_CURL_OPT_LONG(CURLOPT_SSLVERSION, 0);
+
+                               HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYPEER, 1);
+                               HTTP_CURL_OPT_LONG(CURLOPT_SSL_VERIFYHOST, 1);
+                               HTTP_CURL_OPT_STRING(CURLOPT_SSL_CIPHER_LIST, 1, 0);
+
+                               HTTP_CURL_OPT_STRING(CURLOPT_CAINFO, -3, 1);
+                               HTTP_CURL_OPT_STRING(CURLOPT_CAPATH, -3, 1);
+                               HTTP_CURL_OPT_STRING(CURLOPT_RANDOM_FILE, -3, 1);
+                               HTTP_CURL_OPT_STRING(CURLOPT_EGDSOCKET, -3, 1);
+#if HTTP_CURL_VERSION(7,19,0)
+                               HTTP_CURL_OPT_STRING(CURLOPT_ISSUERCERT, -3, 1);
+       #if defined(HTTP_HAVE_OPENSSL)
+                               HTTP_CURL_OPT_STRING(CURLOPT_CRLFILE, -3, 1);
+       #endif
+#endif
+#if HTTP_CURL_VERSION(7,19,1) && defined(HTTP_HAVE_OPENSSL)
+                               HTTP_CURL_OPT_LONG(CURLOPT_CERTINFO, -3);
+#endif
                        }
                }
        }
 
        /* request method */
-       switch (request->meth)
-       {
+       switch (request->meth) {
                case HTTP_GET:
-                       HTTP_CURL_OPT(HTTPGET, 1);
-               break;
+                       HTTP_CURL_OPT(CURLOPT_HTTPGET, 1L);
+                       break;
 
                case HTTP_HEAD:
-                       HTTP_CURL_OPT(NOBODY, 1);
-               break;
+                       HTTP_CURL_OPT(CURLOPT_NOBODY, 1L);
+                       break;
 
                case HTTP_POST:
-                       HTTP_CURL_OPT(POST, 1);
-               break;
+                       HTTP_CURL_OPT(CURLOPT_POST, 1L);
+                       break;
 
                case HTTP_PUT:
-                       HTTP_CURL_OPT(UPLOAD, 1);
-               break;
+                       HTTP_CURL_OPT(CURLOPT_UPLOAD, 1L);
+                       break;
 
                default:
                        if (http_request_method_exists(0, request->meth, NULL)) {
-                               HTTP_CURL_OPT(CUSTOMREQUEST, http_request_method_name(request->meth));
+                               HTTP_CURL_OPT(CURLOPT_CUSTOMREQUEST, http_request_method_name(request->meth));
                        } else {
                                http_error_ex(HE_WARNING, HTTP_E_REQUEST_METHOD, "Unsupported request method: %d (%s)", request->meth, request->url);
                                return FAILURE;
                        }
-               break;
+                       break;
        }
 
        /* attach request body */
        if (request->body && (request->meth != HTTP_GET) && (request->meth != HTTP_HEAD) && (request->meth != HTTP_OPTIONS)) {
-               switch (request->body->type)
-               {
+               switch (request->body->type) {
                        case HTTP_REQUEST_BODY_EMPTY:
                                /* nothing */
-                       break;
+                               break;
                        
-                       case HTTP_REQUEST_BODY_CSTRING:
-                               HTTP_CURL_OPT(POSTFIELDS, request->body->data);
-                               HTTP_CURL_OPT(POSTFIELDSIZE, request->body->size);
-                       break;
-
                        case HTTP_REQUEST_BODY_CURLPOST:
-                               HTTP_CURL_OPT(HTTPPOST, (struct curl_httppost *) request->body->data);
-                       break;
+                               HTTP_CURL_OPT(CURLOPT_HTTPPOST, (struct curl_httppost *) request->body->data);
+                               break;
 
+                       case HTTP_REQUEST_BODY_CSTRING:
+                               if (request->meth != HTTP_PUT) {
+                                       HTTP_CURL_OPT(CURLOPT_POSTFIELDS, request->body->data);
+                                       HTTP_CURL_OPT(CURLOPT_POSTFIELDSIZE, request->body->size);
+                                       break;
+                               }
+                               /* fallthrough, PUT/UPLOAD _needs_ READDATA */
                        case HTTP_REQUEST_BODY_UPLOADFILE:
-                               HTTP_CURL_OPT(IOCTLDATA, request);
-                               HTTP_CURL_OPT(READDATA, request);
-                               HTTP_CURL_OPT(INFILESIZE, request->body->size);
-                       break;
+                               HTTP_CURL_OPT(CURLOPT_IOCTLDATA, request);
+                               HTTP_CURL_OPT(CURLOPT_READDATA, request);
+                               HTTP_CURL_OPT(CURLOPT_INFILESIZE, request->body->size);
+                               break;
 
                        default:
                                /* shouldn't ever happen */
                                http_error_ex(HE_ERROR, 0, "Unknown request body type: %d (%s)", request->body->type, request->url);
                                return FAILURE;
-                       break;
                }
        }
 
@@ -692,63 +1077,66 @@ 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;
+                       }
+               }
        }
 }
 /* }}} */
 
-/* {{{ void http_request_info(http_request *, HashTable *) */
-PHP_HTTP_API void _http_request_info(http_request *request, HashTable *info)
-{
-       zval array;
-       INIT_ZARR(array, info);
-
-       HTTP_CURL_INFO(EFFECTIVE_URL);
-       HTTP_CURL_INFO(RESPONSE_CODE);
-       HTTP_CURL_INFO_EX(HTTP_CONNECTCODE, connect_code);
-       HTTP_CURL_INFO(FILETIME);
-       HTTP_CURL_INFO(TOTAL_TIME);
-       HTTP_CURL_INFO(NAMELOOKUP_TIME);
-       HTTP_CURL_INFO(CONNECT_TIME);
-       HTTP_CURL_INFO(PRETRANSFER_TIME);
-       HTTP_CURL_INFO(STARTTRANSFER_TIME);
-       HTTP_CURL_INFO(REDIRECT_TIME);
-       HTTP_CURL_INFO(REDIRECT_COUNT);
-       HTTP_CURL_INFO(SIZE_UPLOAD);
-       HTTP_CURL_INFO(SIZE_DOWNLOAD);
-       HTTP_CURL_INFO(SPEED_DOWNLOAD);
-       HTTP_CURL_INFO(SPEED_UPLOAD);
-       HTTP_CURL_INFO(HEADER_SIZE);
-       HTTP_CURL_INFO(REQUEST_SIZE);
-       HTTP_CURL_INFO(SSL_VERIFYRESULT);
-       HTTP_CURL_INFO(SSL_ENGINES);
-       HTTP_CURL_INFO(CONTENT_LENGTH_DOWNLOAD);
-       HTTP_CURL_INFO(CONTENT_LENGTH_UPLOAD);
-       HTTP_CURL_INFO(CONTENT_TYPE);
-       HTTP_CURL_INFO(HTTPAUTH_AVAIL);
-       HTTP_CURL_INFO(PROXYAUTH_AVAIL);
-       /*HTTP_CURL_INFO(OS_ERRNO);*/
-       HTTP_CURL_INFO(NUM_CONNECTS);
-#if LIBCURL_VERSION_NUM >= 0x070e01
-       HTTP_CURL_INFO_EX(COOKIELIST, cookies);
-#endif
-}
-/* }}} */
-
 /* {{{ static size_t http_curl_read_callback(void *, size_t, size_t, void *) */
 static size_t http_curl_read_callback(void *data, size_t len, size_t n, void *ctx)
 {
        http_request *request = (http_request *) ctx;
        TSRMLS_FETCH_FROM_CTX(request->tsrm_ls);
-
-       if (request->body == NULL || request->body->type != HTTP_REQUEST_BODY_UPLOADFILE) {
-               return 0;
+       
+       if (request->body) {
+               switch (request->body->type) {
+                       case HTTP_REQUEST_BODY_CSTRING:
+                       {
+                               size_t out = MIN(len * n, request->body->size - request->body->priv);
+                               
+                               if (out) {
+                                       memcpy(data, ((char *) request->body->data) + request->body->priv, out);
+                                       request->body->priv += out;
+                                       return out;
+                               }
+                               break;
+                       }
+                       
+                       case HTTP_REQUEST_BODY_UPLOADFILE:
+                               return php_stream_read((php_stream *) request->body->data, data, len * n);
+               }
        }
-       return php_stream_read((php_stream *) request->body->data, data, len * n);
+       return 0;
 }
 /* }}} */
 
@@ -769,7 +1157,11 @@ static int http_curl_progress_callback(void *ctx, double dltotal, double dlnow,
        add_assoc_double(param, "ultotal", ultotal);
        add_assoc_double(param, "ulnow", ulnow);
        
-       call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, &param TSRMLS_CC);
+       with_error_handling(EH_NORMAL, NULL) {
+               request->_in_progress_cb = 1;
+               call_user_function(EG(function_table), NULL, request->_progress_callback, &retval, 1, &param TSRMLS_CC);
+               request->_in_progress_cb = 0;
+       } end_error_handling();
        
        zval_ptr_dtor(&param);
        zval_dtor(&retval);
@@ -787,12 +1179,23 @@ static curlioerr http_curl_ioctl_callback(CURL *ch, curliocmd cmd, void *ctx)
        if (cmd != CURLIOCMD_RESTARTREAD) {
                return CURLIOE_UNKNOWNCMD;
        }
-       if (    request->body == NULL || 
-                       request->body->type != HTTP_REQUEST_BODY_UPLOADFILE ||
-                       SUCCESS != php_stream_rewind((php_stream *) request->body->data)) {
-               return CURLIOE_FAILRESTART;
+       
+       if (request->body) {
+               switch (request->body->type) {
+                       case HTTP_REQUEST_BODY_CSTRING:
+                               request->body->priv = 0;
+                               return CURLIOE_OK;
+                               break;
+                               
+                       case HTTP_REQUEST_BODY_UPLOADFILE:
+                               if (SUCCESS == php_stream_rewind((php_stream *) request->body->data)) {
+                                       return CURLIOE_OK;
+                               }
+                               break;
+               }
        }
-       return CURLIOE_OK;
+       
+       return CURLIOE_FAILRESTART;
 }
 /* }}} */
 
@@ -801,44 +1204,44 @@ static int http_curl_raw_callback(CURL *ch, curl_infotype type, char *data, size
 {
        http_request *request = (http_request *) ctx;
 
-       switch (type)
-       {
+#define EMPTY_HEADER(d, l) (!l || (l == 1 && d[0] == '\n') || (l == 2 && d[0] == '\r' && d[1] == '\n'))
+       switch (type) {
                case CURLINFO_DATA_IN:
                        if (request->conv.last_type == CURLINFO_HEADER_IN) {
                                phpstr_appends(&request->conv.response, HTTP_CRLF);
                        }
-               case CURLINFO_HEADER_IN:
                        phpstr_append(&request->conv.response, data, length);
-               break;
-               case CURLINFO_DATA_OUT:
-                       if (request->conv.last_type == CURLINFO_HEADER_OUT) {
-                               phpstr_appends(&request->conv.request, HTTP_CRLF);
+                       break;
+               case CURLINFO_HEADER_IN:
+                       if (!EMPTY_HEADER(data, length)) {
+                               phpstr_append(&request->conv.response, data, length);
                        }
+                       break;
+               case CURLINFO_DATA_OUT:
                case CURLINFO_HEADER_OUT:
                        phpstr_append(&request->conv.request, data, length);
-               break;
+                       break;
                default:
+                       break;
+       }
+
 #if 0
-                       fprintf(stderr, "## ", type);
-                       if (!type) {
-                               fprintf(stderr, "%s", data);
-                       } else {
-                               ulong i;
-                               for (i = 1; i <= length; ++i) {
-                                       fprintf(stderr, "%02X ", data[i-1] & 0xFF);
-                                       if (!(i % 20)) {
-                                               fprintf(stderr, "\n## ");
-                                       }
+       {
+               const char _sym[] = "><><><";
+               if (type) {
+                       for (fprintf(stderr, "%c ", _sym[type-1]); length--; data++) {
+                               fprintf(stderr, HTTP_IS_CTYPE(print, *data)?"%c":"\\x%02X", (int) *data);
+                               if (*data == '\n' && length) {
+                                       fprintf(stderr, "\n%c ", _sym[type-1]);
                                }
-                               fprintf(stderr, "\n");
                        }
-                       if (data[length-1] != 0xa) {
-                               fprintf(stderr, "\n");
-                       }
-#endif
-               break;
+                       fprintf(stderr, "\n");
+               } else {
+                       fprintf(stderr, "# %s", data);
+               }
        }
-
+#endif
+       
        if (type) {
                request->conv.last_type = type;
        }
@@ -849,24 +1252,22 @@ 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 *option, *cached;
+                       
+                       option = http_zsep(type, *zoption);
+                       cached = http_request_option_cache_ex(r, key, keylen, h, option);
+                       
+                       zval_ptr_dtor(&option);
+                       return cached;
+               }
        }
        
-       return http_request_option_cache_ex(r, key, keylen, h, zval_copy(type, *zoption));
+       return NULL;
 }
 /* }}} */
 
@@ -875,122 +1276,26 @@ 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;
 }
 /* }}} */
 
-#ifdef HTTP_NEED_OPENSSL_TSL
-/* {{{ */
-static MUTEX_T *http_openssl_tsl = NULL;
-
-static void http_ssl_lock(int mode, int n, const char * file, int line)
-{
-       if (mode & CRYPTO_LOCK) {
-               tsrm_mutex_lock(http_openssl_tsl[n]);
-       } else {
-               tsrm_mutex_unlock(http_openssl_tsl[n]);
-       }
-}
-
-static ulong http_ssl_id(void)
-{
-       return (ulong) tsrm_thread_id();
-}
-
-static inline void http_ssl_init(void)
-{
-       int i, c = CRYPTO_num_locks();
-       
-       http_openssl_tsl = malloc(c * sizeof(MUTEX_T));
+/* {{{ static inline int http_request_cookies_enabled(http_request *) */
+static inline int _http_request_cookies_enabled(http_request *request) {
+       http_request_storage *st;
        
-       for (i = 0; i < c; ++i) {
-               http_openssl_tsl[i] = tsrm_mutex_alloc();
+       if (request->ch && (st = http_request_storage_get(request->ch)) && st->cookiestore) {
+               /* cookies are enabled */
+               return 1;
        }
-       
-       CRYPTO_set_id_callback(http_ssl_id);
-       CRYPTO_set_locking_callback(http_ssl_lock);
-}
-
-static inline void http_ssl_cleanup(void)
-{
-       if (http_openssl_tsl) {
-               int i, c = CRYPTO_num_locks();
-               
-               CRYPTO_set_id_callback(NULL);
-               CRYPTO_set_locking_callback(NULL);
-               
-               for (i = 0; i < c; ++i) {
-                       tsrm_mutex_free(http_openssl_tsl[i]);
-               }
-               
-               free(http_openssl_tsl);
-               http_openssl_tsl = NULL;
-       }
-}
-#endif /* HTTP_NEED_OPENSSL_TSL */
-/* }}} */
-
-#ifdef HTTP_NEED_GNUTLS_TSL
-/* {{{ */
-static int http_ssl_mutex_create(void **m)
-{
-       if (*((MUTEX_T *) m) = tsrm_mutex_alloc()) {
-               return SUCCESS;
-       } else {
-               return FAILURE;
-       }
-}
-
-static int http_ssl_mutex_destroy(void **m)
-{
-       tsrm_mutex_free(*((MUTEX_T *) m));
-       return SUCCESS;
-}
-
-static int http_ssl_mutex_lock(void **m)
-{
-       return tsrm_mutex_lock(*((MUTEX_T *) m));
-}
-
-static int http_ssl_mutex_unlock(void **m)
-{
-       return tsrm_mutex_unlock(*((MUTEX_T *) m));
-}
-
-static struct gcry_thread_cbs http_gnutls_tsl = {
-       GCRY_THREAD_OPTION_USER,
-       NULL,
-       http_ssl_mutex_create,
-       http_ssl_mutex_destroy,
-       http_ssl_mutex_lock,
-       http_ssl_mutex_unlock
-};
-
-static inline void http_ssl_init(void)
-{
-       gcry_control(GCRYCTL_SET_THREAD_CBS, &http_gnutls_tsl);
-}
-
-static inline void http_ssl_cleanup(void)
-{
-       return;
+       return 0;
 }
-#endif /* HTTP_NEED_GNUTLS_TSL */
 /* }}} */
 
 #endif /* HTTP_HAVE_CURL */