- improve internal array handling
[m6w6/ext-http] / http_request_object.c
index c5fd2936b76688d74f0e1ed7e3e3bd2b969b8e88..c56562a0af9cdcf9c41f0cce33ff6394ea829eba 100644 (file)
@@ -72,6 +72,13 @@ HTTP_BEGIN_ARGS(addCookies, 1)
        HTTP_ARG_VAL(cookies, 0)
 HTTP_END_ARGS;
 
+HTTP_EMPTY_ARGS(enableCookies);
+#if HTTP_CURL_VERSION(7,14,1)
+HTTP_BEGIN_ARGS(resetCookies, 0)
+       HTTP_ARG_VAL(session_only, 0)
+HTTP_END_ARGS;
+#endif
+
 HTTP_EMPTY_ARGS(getUrl);
 HTTP_BEGIN_ARGS(setUrl, 1)
        HTTP_ARG_VAL(url, 0)
@@ -190,6 +197,13 @@ HTTP_BEGIN_ARGS(postFields, 2)
        HTTP_ARG_VAL(info, 1)
 HTTP_END_ARGS;
 
+HTTP_BEGIN_ARGS(putData, 2)
+       HTTP_ARG_VAL(url, 0)
+       HTTP_ARG_VAL(data, 0)
+       HTTP_ARG_VAL(options, 0)
+       HTTP_ARG_VAL(info, 1)
+HTTP_END_ARGS;
+
 HTTP_BEGIN_ARGS(putFile, 2)
        HTTP_ARG_VAL(url, 0)
        HTTP_ARG_VAL(file, 0)
@@ -220,8 +234,12 @@ HTTP_BEGIN_ARGS(methodExists, 1)
        HTTP_ARG_VAL(method, 0)
 HTTP_END_ARGS;
 
-#define http_request_object_declare_default_properties() _http_request_object_declare_default_properties(TSRMLS_C)
-static inline void _http_request_object_declare_default_properties(TSRMLS_D);
+#ifdef HAVE_CURL_GETFORMDATA
+HTTP_BEGIN_ARGS(encodeBody, 2)
+       HTTP_ARG_VAL(fields, 0)
+       HTTP_ARG_VAL(files, 0)
+HTTP_END_ARGS;
+#endif
 
 #define OBJ_PROP_CE http_request_object_ce
 zend_class_entry *http_request_object_ce;
@@ -242,6 +260,11 @@ zend_function_entry http_request_object_fe[] = {
        HTTP_REQUEST_ME(getCookies, ZEND_ACC_PUBLIC)
        HTTP_REQUEST_ME(setCookies, ZEND_ACC_PUBLIC)
 
+       HTTP_REQUEST_ME(enableCookies, ZEND_ACC_PUBLIC)
+#if HTTP_CURL_VERSION(7,14,1)
+       HTTP_REQUEST_ME(resetCookies, ZEND_ACC_PUBLIC)
+#endif
+
        HTTP_REQUEST_ME(setMethod, ZEND_ACC_PUBLIC)
        HTTP_REQUEST_ME(getMethod, ZEND_ACC_PUBLIC)
 
@@ -294,6 +317,7 @@ zend_function_entry http_request_object_fe[] = {
        HTTP_REQUEST_ALIAS(head, http_head)
        HTTP_REQUEST_ALIAS(postData, http_post_data)
        HTTP_REQUEST_ALIAS(postFields, http_post_fields)
+       HTTP_REQUEST_ALIAS(putData, http_put_data)
        HTTP_REQUEST_ALIAS(putFile, http_put_file)
        HTTP_REQUEST_ALIAS(putStream, http_put_stream)
 
@@ -301,7 +325,9 @@ zend_function_entry http_request_object_fe[] = {
        HTTP_REQUEST_ALIAS(methodUnregister, http_request_method_unregister)
        HTTP_REQUEST_ALIAS(methodName, http_request_method_name)
        HTTP_REQUEST_ALIAS(methodExists, http_request_method_exists)
-
+#ifdef HAVE_CURL_GETFORMDATA
+       HTTP_REQUEST_ALIAS(encodeBody, http_request_body_encode)
+#endif
        EMPTY_FUNCTION_ENTRY
 };
 static zend_object_handlers http_request_object_handlers;
@@ -310,66 +336,11 @@ PHP_MINIT_FUNCTION(http_request_object)
 {
        HTTP_REGISTER_CLASS_EX(HttpRequest, http_request_object, NULL, 0);
        http_request_object_handlers.clone_obj = _http_request_object_clone_obj;
-       return SUCCESS;
-}
-
-zend_object_value _http_request_object_new(zend_class_entry *ce TSRMLS_DC)
-{
-       return http_request_object_new_ex(ce, NULL, NULL);
-}
-
-zend_object_value _http_request_object_new_ex(zend_class_entry *ce, CURL *ch, http_request_object **ptr TSRMLS_DC)
-{
-       zend_object_value ov;
-       http_request_object *o;
-
-       o = ecalloc(1, sizeof(http_request_object));
-       o->zo.ce = ce;
-       o->request = http_request_init_ex(NULL, ch, 0, NULL);
-       
-       if (ptr) {
-               *ptr = o;
-       }
-
-       ALLOC_HASHTABLE(OBJ_PROP(o));
-       zend_hash_init(OBJ_PROP(o), 0, NULL, ZVAL_PTR_DTOR, 0);
-       zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
-
-       ov.handle = putObject(http_request_object, o);
-       ov.handlers = &http_request_object_handlers;
-
-       return ov;
-}
-
-zend_object_value _http_request_object_clone_obj(zval *this_ptr TSRMLS_DC)
-{
-       zend_object *old_zo;
-       zend_object_value new_ov;
-       http_request_object *new_obj;
-       getObject(http_request_object, old_obj);
-       
-       old_zo = zend_objects_get_address(this_ptr TSRMLS_CC);
-       new_ov = http_request_object_new_ex(old_zo->ce, NULL, &new_obj);
-       if (old_obj->request->ch) {
-               http_curl_init_ex(curl_easy_duphandle(old_obj->request->ch), new_obj->request);
-       }
-       
-       zend_objects_clone_members(&new_obj->zo, new_ov, old_zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
-       phpstr_append(&new_obj->request->conv.request, old_obj->request->conv.request.data, old_obj->request->conv.request.used);
-       phpstr_append(&new_obj->request->conv.response, old_obj->request->conv.response.data, old_obj->request->conv.response.used);
-       
-       return new_ov;
-}
-
-static inline void _http_request_object_declare_default_properties(TSRMLS_D)
-{
-       zend_class_entry *ce = http_request_object_ce;
 
        DCL_PROP_N(PRIVATE, options);
        DCL_PROP_N(PRIVATE, postFields);
        DCL_PROP_N(PRIVATE, postFiles);
        DCL_PROP_N(PRIVATE, responseInfo);
-       DCL_PROP_N(PRIVATE, responseData);
        DCL_PROP_N(PRIVATE, responseMessage);
        DCL_PROP(PRIVATE, long, responseCode, 0);
        DCL_PROP(PRIVATE, string, responseStatus, "");
@@ -386,7 +357,7 @@ static inline void _http_request_object_declare_default_properties(TSRMLS_D)
 #ifndef WONKY
        /*
         * Request Method Constants
-        */
+       */
        /* HTTP/1.1 */
        DCL_CONST(long, "METH_GET", HTTP_GET);
        DCL_CONST(long, "METH_HEAD", HTTP_HEAD);
@@ -419,31 +390,103 @@ static inline void _http_request_object_declare_default_properties(TSRMLS_D)
        /* WebDAV Access Control - RFC 3744 */
        DCL_CONST(long, "METH_ACL", HTTP_ACL);
 
-       /* cURL HTTP protocol versions */
+       /*
+       * HTTP Protocol Version Constants
+       */
        DCL_CONST(long, "VERSION_1_0", CURL_HTTP_VERSION_1_0);
        DCL_CONST(long, "VERSION_1_1", CURL_HTTP_VERSION_1_1);
-       DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE);
+       DCL_CONST(long, "VERSION_NONE", CURL_HTTP_VERSION_NONE); /* to be removed */
+       DCL_CONST(long, "VERSION_ANY", CURL_HTTP_VERSION_NONE);
+
+       /*
+       * SSL Version Constants
+       */
+       DCL_CONST(long, "SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1);
+       DCL_CONST(long, "SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2);
+       DCL_CONST(long, "SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3);
+       DCL_CONST(long, "SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT);
 
        /*
-        * Auth Constants
-        */
+       * DNS IPvX resolving
+       */
+       DCL_CONST(long, "IPRESOLVE_V4", CURL_IPRESOLVE_V4);
+       DCL_CONST(long, "IPRESOLVE_V6", CURL_IPRESOLVE_V6);
+       DCL_CONST(long, "IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER);
+
+       /*
+       * Auth Constants
+       */
        DCL_CONST(long, "AUTH_BASIC", CURLAUTH_BASIC);
        DCL_CONST(long, "AUTH_DIGEST", CURLAUTH_DIGEST);
        DCL_CONST(long, "AUTH_NTLM", CURLAUTH_NTLM);
+       DCL_CONST(long, "AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE);
        DCL_CONST(long, "AUTH_ANY", CURLAUTH_ANY);
+       
+       /*
+       * Proxy Type Constants
+       */
+#      if HTTP_CURL_VERSION(7,15,2)
+       DCL_CONST(long, "PROXY_SOCKS4", CURLPROXY_SOCKS4);
+#      endif
+       DCL_CONST(long, "PROXY_SOCKS5", CURLPROXY_SOCKS5);
+       DCL_CONST(long, "PROXY_HTTP", CURLPROXY_HTTP);
 #endif /* WONKY */
+       
+       return SUCCESS;
+}
+
+zend_object_value _http_request_object_new(zend_class_entry *ce TSRMLS_DC)
+{
+       return http_request_object_new_ex(ce, NULL, NULL);
+}
+
+zend_object_value _http_request_object_new_ex(zend_class_entry *ce, CURL *ch, http_request_object **ptr TSRMLS_DC)
+{
+       zend_object_value ov;
+       http_request_object *o;
+
+       o = ecalloc(1, sizeof(http_request_object));
+       o->zo.ce = ce;
+       o->request = http_request_init_ex(NULL, ch, 0, NULL);
+       
+       if (ptr) {
+               *ptr = o;
+       }
+
+       ALLOC_HASHTABLE(OBJ_PROP(o));
+       zend_hash_init(OBJ_PROP(o), zend_hash_num_elements(&ce->default_properties), NULL, ZVAL_PTR_DTOR, 0);
+       zend_hash_copy(OBJ_PROP(o), &ce->default_properties, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+
+       ov.handle = putObject(http_request_object, o);
+       ov.handlers = &http_request_object_handlers;
+
+       return ov;
+}
+
+zend_object_value _http_request_object_clone_obj(zval *this_ptr TSRMLS_DC)
+{
+       zend_object_value new_ov;
+       http_request_object *new_obj;
+       getObject(http_request_object, old_obj);
+       
+       new_ov = http_request_object_new_ex(old_obj->zo.ce, NULL, &new_obj);
+       if (old_obj->request->ch) {
+               http_curl_init_ex(curl_easy_duphandle(old_obj->request->ch), new_obj->request);
+       }
+       
+       zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
+       phpstr_append(&new_obj->request->conv.request, old_obj->request->conv.request.data, old_obj->request->conv.request.used);
+       phpstr_append(&new_obj->request->conv.response, old_obj->request->conv.response.data, old_obj->request->conv.response.used);
+       
+       return new_ov;
 }
 
 void _http_request_object_free(zend_object *object TSRMLS_DC)
 {
        http_request_object *o = (http_request_object *) object;
 
-       if (OBJ_PROP(o)) {
-               zend_hash_destroy(OBJ_PROP(o));
-               FREE_HASHTABLE(OBJ_PROP(o));
-       }
        http_request_free(&o->request);
-       efree(o);
+       freeObject(o);
 }
 
 #define http_request_object_check_request_content_type(t) _http_request_object_check_request_content_type((t) TSRMLS_CC)
@@ -455,12 +498,12 @@ static inline void _http_request_object_check_request_content_type(zval *this_pt
                zval **headers, *opts = GET_PROP(options);
                
                if (    (Z_TYPE_P(opts) == IS_ARRAY) &&
-                               (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void **) &headers)) && 
+                               (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), "headers", sizeof("headers"), (void *) &headers)) && 
                                (Z_TYPE_PP(headers) == IS_ARRAY)) {
                        zval **ct_header;
                        
                        /* only override if not already set */
-                       if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), "Content-Type", sizeof("Content-Type"), (void **) &ct_header))) {
+                       if ((SUCCESS != zend_hash_find(Z_ARRVAL_PP(headers), "Content-Type", sizeof("Content-Type"), (void *) &ct_header))) {
                                add_assoc_stringl(*headers, "Content-Type", Z_STRVAL_P(ctype), Z_STRLEN_P(ctype), 1);
                        } else
                        /* or not a string, zero length string or a string of spaces */
@@ -471,7 +514,7 @@ static inline void _http_request_object_check_request_content_type(zval *this_pt
                                
                                /* check for spaces only */
                                for (i = 0; i < Z_STRLEN_PP(ct_header); ++i) {
-                                       if (!isspace(Z_STRVAL_PP(ct_header)[i])) {
+                                       if (!HTTP_IS_CTYPE(space, Z_STRVAL_PP(ct_header)[i])) {
                                                only_space = 0;
                                                break;
                                        }
@@ -505,28 +548,30 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
        {
                case HTTP_GET:
                case HTTP_HEAD:
-               break;
+                       break;
 
                case HTTP_PUT:
                {
-                       zval *put_data = GET_PROP(putData);
+                       zval *put_file = GET_PROP(putFile);
                        
                        http_request_object_check_request_content_type(getThis());
-                       if (Z_STRLEN_P(put_data)) {
-                               obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING,
-                                       estrndup(Z_STRVAL_P(put_data), Z_STRLEN_P(put_data)), Z_STRLEN_P(put_data), 1);
-                       } else {
+                       
+                       if (Z_STRLEN_P(put_file)) {
                                php_stream_statbuf ssb;
-                               php_stream *stream = php_stream_open_wrapper_ex(Z_STRVAL_P(GET_PROP(putFile)), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT);
+                               php_stream *stream = php_stream_open_wrapper_ex(Z_STRVAL_P(put_file), "rb", REPORT_ERRORS|ENFORCE_SAFE_MODE, NULL, HTTP_DEFAULT_STREAM_CONTEXT);
                                
-                               if (stream && !php_stream_stat(stream, &ssb)) {
+                               if (stream && SUCCESS == php_stream_stat(stream, &ssb)) {
                                        obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_UPLOADFILE, stream, ssb.sb.st_size, 1);
                                } else {
                                        status = FAILURE;
                                }
+                       } else {
+                               zval *put_data = GET_PROP(putData);
+                               obj->request->body = http_request_body_init_ex(obj->request->body, HTTP_REQUEST_BODY_CSTRING,
+                                       estrndup(Z_STRVAL_P(put_data), Z_STRLEN_P(put_data)), Z_STRLEN_P(put_data), 1);
                        }
+                       break;
                }
-               break;
 
                case HTTP_POST:
                default:
@@ -552,8 +597,8 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                                        }
                                }
                        }
+                       break;
                }
-               break;
        }
 
        if (status == SUCCESS) {
@@ -576,7 +621,7 @@ STATUS _http_request_object_requesthandler(http_request_object *obj, zval *this_
                        zval **entry, *pcb;
                        
                        if (    (Z_TYPE_P(options) != IS_ARRAY)
-                               ||      (SUCCESS != zend_hash_find(Z_ARRVAL_P(options), "onprogress", sizeof("onprogress"), (void **) &entry)
+                               ||      (SUCCESS != zend_hash_find(Z_ARRVAL_P(options), "onprogress", sizeof("onprogress"), (void *) &entry)
                                ||      (!zval_is_true(*entry)))) {
                                MAKE_STD_ZVAL(pcb);
                                array_init(pcb);
@@ -610,9 +655,7 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
        phpstr_fix(&obj->request->conv.response);
        
        if ((msg = http_message_parse(PHPSTR_VAL(&obj->request->conv.response), PHPSTR_LEN(&obj->request->conv.response)))) {
-               char *body;
-               size_t body_len;
-               zval *headers, *message, *resp;
+               zval *message;
 
                if (zval_is_true(GET_PROP(recordHistory))) {
                        zval *hist, *history = GET_PROP(history);
@@ -629,18 +672,7 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
                }
 
                UPD_PROP(long, responseCode, msg->http.info.response.code);
-               UPD_PROP(string, responseStatus, msg->http.info.response.status);
-
-               MAKE_STD_ZVAL(resp);
-               array_init(resp);
-               MAKE_STD_ZVAL(headers);
-               array_init(headers);
-               zend_hash_copy(Z_ARRVAL_P(headers), &msg->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
-               add_assoc_zval(resp, "headers", headers);
-               phpstr_data(PHPSTR(msg), &body, &body_len);
-               add_assoc_stringl(resp, "body", body, body_len, 0);
-               SET_PROP(responseData, resp);
-               zval_ptr_dtor(&resp);
+               UPD_PROP(string, responseStatus, STR_PTR(msg->http.info.response.status));
 
                MAKE_STD_ZVAL(message);
                ZVAL_OBJVAL(message, http_message_object_new_ex(http_message_object_ce, msg, NULL), 0);
@@ -650,17 +682,13 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
                ret = SUCCESS;
        } else {
                /* update properties with empty values*/
-               zval *resp = GET_PROP(responseData), *znull;
+               zval *znull;
                
                MAKE_STD_ZVAL(znull);
                ZVAL_NULL(znull);
                SET_PROP(responseMessage, znull);
                zval_ptr_dtor(&znull);
                
-               if (Z_TYPE_P(resp) == IS_ARRAY) {
-                       zend_hash_clean(Z_ARRVAL_P(resp));
-               }
-               
                UPD_PROP(long, responseCode, 0);
                UPD_PROP(string, responseStatus, "");
                
@@ -684,12 +712,14 @@ STATUS _http_request_object_responsehandler(http_request_object *obj, zval *this
                ret = FAILURE;
        }
        
-       if (zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) {
+       if (!EG(exception) && zend_hash_exists(&Z_OBJCE_P(getThis())->function_table, "onfinish", sizeof("onfinish"))) {
                zval *param;
                
                MAKE_STD_ZVAL(param);
                ZVAL_BOOL(param, ret == SUCCESS);
-               zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL, param);
+               with_error_handling(EH_NORMAL, NULL) {
+                       zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "onfinish", NULL, param);
+               } end_error_handling();
                zval_ptr_dtor(&param);
        }
        
@@ -718,26 +748,26 @@ static inline void _http_request_object_set_options_subr(INTERNAL_FUNCTION_PARAM
        array_init(new_opts);
        old_opts = GET_PROP(options);
        if (Z_TYPE_P(old_opts) == IS_ARRAY) {
-               array_copy(old_opts, new_opts);
+               array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
        }
 
-       if (prettify_keys && opts) {
-               zend_hash_apply_with_arguments(Z_ARRVAL_P(opts), apply_pretty_key, 0);
-       }
-       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void **) &entry)) {
+       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) {
                if (overwrite) {
                        zend_hash_clean(Z_ARRVAL_PP(entry));
                }
                if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
                        if (overwrite) {
-                               array_copy(opts, *entry);
+                               array_copy(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry));
                        } else {
-                               array_merge(opts, *entry);
+                               array_join(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry), 0, prettify_keys ? ARRAY_JOIN_PRETTIFY : 0);
                        }
                }
        } else if (opts) {
+               if (prettify_keys) {
+                       zend_hash_apply_with_arguments(Z_ARRVAL_P(opts), apply_pretty_key, 0);
+               }
                ZVAL_ADDREF(opts);
-               add_assoc_zval(new_opts, key, opts);
+               add_assoc_zval_ex(new_opts, key, len, opts);
        }
        SET_PROP(options, new_opts);
        zval_ptr_dtor(&new_opts);
@@ -751,16 +781,16 @@ static inline void _http_request_get_options_subr(INTERNAL_FUNCTION_PARAMETERS,
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                zval *opts, **options;
 
                opts = GET_PROP(options);
                array_init(return_value);
 
                if (    (Z_TYPE_P(opts) == IS_ARRAY) && 
-                               (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void **) &options))) {
+                               (SUCCESS == zend_hash_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
                        convert_to_array(*options);
-                       array_copy(*options, return_value);
+                       array_copy(Z_ARRVAL_PP(options), Z_ARRVAL_P(return_value));
                }
        }
 }
@@ -787,7 +817,7 @@ PHP_METHOD(HttpRequest, __construct)
        zval *options = NULL;
 
        SET_EH_THROW_HTTP();
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla", &URL, &URL_len, &meth, &options)) {
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sla!", &URL, &URL_len, &meth, &options)) {
                if (URL) {
                        UPD_STRL(url, URL, URL_len);
                }
@@ -814,8 +844,7 @@ PHP_METHOD(HttpRequest, __construct)
  */
 PHP_METHOD(HttpRequest, setOptions)
 {
-       char *key = NULL;
-       ulong idx = 0;
+       HashKey key = initHashKey(0);
        HashPosition pos;
        zval *opts = NULL, *old_opts, *new_opts, *add_opts, **opt;
 
@@ -835,32 +864,40 @@ PHP_METHOD(HttpRequest, setOptions)
        MAKE_STD_ZVAL(add_opts);
        array_init(add_opts);
        /* some options need extra attention -- thus cannot use array_merge() directly */
-       FOREACH_KEYVAL(pos, opts, key, idx, opt) {
-               if (key) {
-                       if (!strcmp(key, "headers")) {
+       FOREACH_KEYVAL(pos, opts, key, opt) {
+               if (key.type == HASH_KEY_IS_STRING) {
+                       if (!strcmp(key.str, "headers")) {
                                zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addheaders", NULL, *opt);
-                       } else if (!strcmp(key, "cookies")) {
+                       } else if (!strcmp(key.str, "cookies")) {
                                zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addcookies", NULL, *opt);
-                       } else if (!strcmp(key, "ssl")) {
+                       } else if (!strcmp(key.str, "ssl")) {
                                zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "addssloptions", NULL, *opt);
-                       } else if ((!strcasecmp(key, "url")) || (!strcasecmp(key, "uri"))) {
+                       } else if ((!strcasecmp(key.str, "url")) || (!strcasecmp(key.str, "uri"))) {
                                zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "seturl", NULL, *opt);
-                       } else if (!strcmp(key, "method")) {
+                       } else if (!strcmp(key.str, "method")) {
                                zend_call_method_with_1_params(&getThis(), Z_OBJCE_P(getThis()), NULL, "setmethod", NULL, *opt);
+#if HTTP_CURL_VERSION(7,14,1)
+                       } else if (!strcmp(key.str, "resetcookies")) {
+                               getObject(http_request_object, obj);
+                               http_request_reset_cookies(obj->request, 0);
+#endif
+                       } else if (!strcmp(key.str, "enablecookies")) {
+                               getObject(http_request_object, obj);
+                               http_request_enable_cookies(obj->request);
+                       } else if (!strcasecmp(key.str, "recordHistory")) {
+                               UPD_PROP(bool, recordHistory, 1);
                        } else {
                                ZVAL_ADDREF(*opt);
-                               add_assoc_zval(add_opts, key, *opt);
+                               add_assoc_zval_ex(add_opts, key.str, key.len, *opt);
                        }
-                       /* reset */
-                       key = NULL;
                }
        }
        
        old_opts = GET_PROP(options);
        if (Z_TYPE_P(old_opts) == IS_ARRAY) {
-               array_copy(old_opts, new_opts);
+               array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
        }
-       array_merge(add_opts, new_opts);
+       array_join(Z_ARRVAL_P(add_opts), Z_ARRVAL_P(new_opts), 0, 0);
        SET_PROP(options, new_opts);
        zval_ptr_dtor(&new_opts);
        zval_ptr_dtor(&add_opts);
@@ -879,7 +916,7 @@ PHP_METHOD(HttpRequest, getOptions)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(options);
        }
 }
@@ -1009,6 +1046,44 @@ PHP_METHOD(HttpRequest, getCookies)
 }
 /* }}} */
 
+/* {{{ proto bool HttpRequest::enableCookies()
+ *
+ * Enable automatic sending of received cookies.
+ * Note that cuutomly set cookies will be sent anyway.
+ */
+PHP_METHOD(HttpRequest, enableCookies)
+{
+       NO_ARGS {
+               getObject(http_request_object, obj);
+               RETURN_SUCCESS(http_request_enable_cookies(obj->request));
+       }
+       
+}
+/* }}} */
+
+/* {{{ proto bool HttpRequest::resetCookies([bool session_only = FALSE])
+ *
+ * Reset all automatically received/sent cookies.
+ * Note that customly set cookies are not affected.
+ *
+ * Accepts an optional bool parameter specifying
+ * whether only session cookies should be reset
+ * (needs libcurl >= v7.15.4, else libcurl >= v7.14.1).
+ *
+ * Returns TRUE on success, or FALSE on failure.
+ */
+PHP_METHOD(HttpRequest, resetCookies)
+{
+       zend_bool session_only = 0;
+       getObject(http_request_object, obj);
+       
+       if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &session_only)) {
+               RETURN_FALSE;
+       }
+       RETURN_SUCCESS(http_request_reset_cookies(obj->request, session_only));
+}
+/* }}} */
+
 /* {{{ proto bool HttpRequest::setUrl(string url)
  *
  * Set the request URL.
@@ -1041,7 +1116,7 @@ PHP_METHOD(HttpRequest, getUrl)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(url);
        }
 }
@@ -1079,7 +1154,7 @@ PHP_METHOD(HttpRequest, getMethod)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(method);
        }
 }
@@ -1122,7 +1197,7 @@ PHP_METHOD(HttpRequest, getContentType)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(contentType);
        }
 }
@@ -1181,7 +1256,7 @@ PHP_METHOD(HttpRequest, getQueryData)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(queryData);
        }
 }
@@ -1242,9 +1317,9 @@ PHP_METHOD(HttpRequest, addPostFields)
                array_init(new_post);
                old_post = GET_PROP(postFields);
                if (Z_TYPE_P(old_post) == IS_ARRAY) {
-                       array_copy(old_post, new_post);
+                       array_copy(Z_ARRVAL_P(old_post), Z_ARRVAL_P(new_post));
                }
-               array_merge(post_data, new_post);
+               array_join(Z_ARRVAL_P(post_data), Z_ARRVAL_P(new_post), 0, 0);
                SET_PROP(postFields, new_post);
                zval_ptr_dtor(&new_post);
        }
@@ -1274,7 +1349,7 @@ PHP_METHOD(HttpRequest, setPostFields)
        MAKE_STD_ZVAL(post);
        array_init(post);
        if (post_data && zend_hash_num_elements(Z_ARRVAL_P(post_data))) {
-               array_copy(post_data, post);
+               array_copy(Z_ARRVAL_P(post_data), Z_ARRVAL_P(post));
        }
        SET_PROP(postFields, post);
        zval_ptr_dtor(&post);
@@ -1293,7 +1368,7 @@ PHP_METHOD(HttpRequest, getPostFields)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(postFields);
        }
 }
@@ -1373,7 +1448,7 @@ PHP_METHOD(HttpRequest, getRawPostData)
 {
        NO_ARGS;
        
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(rawPostData);
        }
 }
@@ -1420,7 +1495,7 @@ PHP_METHOD(HttpRequest, addPostFile)
        array_init(new_post);
        old_post = GET_PROP(postFiles);
        if (Z_TYPE_P(old_post) == IS_ARRAY) {
-               array_copy(old_post, new_post);
+               array_copy(Z_ARRVAL_P(old_post), Z_ARRVAL_P(new_post));
        }
        add_next_index_zval(new_post, entry);
        SET_PROP(postFiles, new_post);
@@ -1452,7 +1527,7 @@ PHP_METHOD(HttpRequest, setPostFiles)
        MAKE_STD_ZVAL(post);
        array_init(post);
        if (files && (Z_TYPE_P(files) == IS_ARRAY)) {
-               array_copy(files, post);
+               array_copy(Z_ARRVAL_P(files), Z_ARRVAL_P(post));
        }
        SET_PROP(postFiles, post);
        zval_ptr_dtor(&post);
@@ -1471,7 +1546,7 @@ PHP_METHOD(HttpRequest, getPostFiles)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(postFiles);
        }
 }
@@ -1510,7 +1585,7 @@ PHP_METHOD(HttpRequest, getPutFile)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(putFile);
        }
 }
@@ -1590,7 +1665,7 @@ PHP_METHOD(HttpRequest, getPutData)
 {
        NO_ARGS;
        
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(putData);
        }
 }
@@ -1611,8 +1686,24 @@ PHP_METHOD(HttpRequest, getResponseData)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
-               RETURN_PROP(responseData);
+       if (return_value_used) {
+               char *body;
+               size_t body_len;
+               zval *headers, *message = GET_PROP(responseMessage);
+               
+               if (Z_TYPE_P(message) == IS_OBJECT) {
+                       getObjectEx(http_message_object, msg, message);
+                       
+                       array_init(return_value);
+                       
+                       MAKE_STD_ZVAL(headers);
+                       array_init(headers);
+                       zend_hash_copy(Z_ARRVAL_P(headers), &msg->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+                       add_assoc_zval(return_value, "headers", headers);
+                       
+                       phpstr_data(PHPSTR(msg->message), &body, &body_len);
+                       add_assoc_stringl(return_value, "body", body, body_len, 0);
+               }
        }
 }
 /* }}} */
@@ -1632,29 +1723,29 @@ PHP_METHOD(HttpRequest, getResponseData)
  */
 PHP_METHOD(HttpRequest, getResponseHeader)
 {
-       IF_RETVAL_USED {
-               zval *data, **headers, **header;
+       if (return_value_used) {
+               zval *header;
                char *header_name = NULL;
                int header_len = 0;
 
-               if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
-                       RETURN_FALSE;
-               }
-
-               data = GET_PROP(responseData);
-               if (    (Z_TYPE_P(data) == IS_ARRAY) && 
-                               (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) &&
-                               (Z_TYPE_PP(headers) == IS_ARRAY)) {
-                       if (!header_len || !header_name) {
-                               RETVAL_ZVAL(*headers, 1, 0);
-                       } else if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(headers), pretty_key(header_name, header_len, 1, 1), header_len + 1, (void **) &header)) {
-                               RETVAL_ZVAL(*header, 1, 0);
-                       } else {
-                               RETVAL_FALSE;
+               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &header_name, &header_len)) {
+                       zval *message = GET_PROP(responseMessage);
+                       
+                       if (Z_TYPE_P(message) == IS_OBJECT) {
+                               getObjectEx(http_message_object, msg, message);
+                               
+                               if (header_len) {
+                                       if ((header = http_message_header_ex(msg->message, pretty_key(header_name, header_len, 1, 1), header_len + 1, 0))) {
+                                               RETURN_ZVAL(header, 1, 1);
+                                       }
+                               } else {
+                                       array_init(return_value);
+                                       zend_hash_copy(Z_ARRVAL_P(return_value), &msg->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
+                                       return;
+                               }
                        }
-               } else {
-                       RETVAL_FALSE;
                }
+               RETURN_FALSE;
        }
 }
 /* }}} */
@@ -1670,47 +1761,57 @@ PHP_METHOD(HttpRequest, getResponseHeader)
  */
 PHP_METHOD(HttpRequest, getResponseCookies)
 {
-       IF_RETVAL_USED {
+       if (return_value_used) {
                long flags = 0;
-               zval *allowed_extras_array = NULL, *data, **headers;
-
-               if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la", &flags, &allowed_extras_array)) {
-                       RETURN_FALSE;
-               }
-
-               data = GET_PROP(responseData);
-               if (    (Z_TYPE_P(data) == IS_ARRAY) &&
-                               (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "headers", sizeof("headers"), (void **) &headers)) &&
-                               (Z_TYPE_PP(headers) == IS_ARRAY)) {
+               zval *allowed_extras_array = NULL;
+               
+               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la!", &flags, &allowed_extras_array)) {
                        int i = 0;
-                       ulong idx = 0;
-                       char *key = NULL, **allowed_extras = NULL;
-                       zval **header = NULL, **entry = NULL;
+                       HashKey key = initHashKey(0);
+                       char **allowed_extras = NULL;
+                       zval **header = NULL, **entry = NULL, *message = GET_PROP(responseMessage);
                        HashPosition pos, pos1, pos2;
                        
-                       array_init(return_value);
-
-                       if (allowed_extras_array) {
-                               allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
-                               FOREACH_VAL(pos, allowed_extras_array, entry) {
-                                       ZVAL_ADDREF(*entry);
-                                       convert_to_string_ex(entry);
-                                       allowed_extras[i++] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
-                                       zval_ptr_dtor(entry);
+                       if (Z_TYPE_P(message) == IS_OBJECT) {
+                               getObjectEx(http_message_object, msg, message);
+                               
+                               array_init(return_value);
+                               
+                               if (allowed_extras_array) {
+                                       allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
+                                       FOREACH_VAL(pos, allowed_extras_array, entry) {
+                                               ZVAL_ADDREF(*entry);
+                                               convert_to_string_ex(entry);
+                                               allowed_extras[i++] = estrndup(Z_STRVAL_PP(entry), Z_STRLEN_PP(entry));
+                                               zval_ptr_dtor(entry);
+                                       }
                                }
-                       }
-                       
-                       FOREACH_HASH_KEYVAL(pos1, Z_ARRVAL_PP(headers), key, idx, header) {
-                               if (key && !strcasecmp(key, "Set-Cookie")) {
-                                       http_cookie_list list;
-                                       
-                                       if (Z_TYPE_PP(header) == IS_ARRAY) {
-                                               zval **single_header;
+                               
+                               FOREACH_HASH_KEYVAL(pos1, &msg->message->hdrs, key, header) {
+                                       if (key.type == HASH_KEY_IS_STRING && !strcasecmp(key.str, "Set-Cookie")) {
+                                               http_cookie_list list;
                                                
-                                               FOREACH_VAL(pos2, *header, single_header) {
-                                                       ZVAL_ADDREF(*single_header);
-                                                       convert_to_string_ex(single_header);
-                                                       if (http_parse_cookie_ex(&list, Z_STRVAL_PP(single_header), flags, allowed_extras)) {
+                                               if (Z_TYPE_PP(header) == IS_ARRAY) {
+                                                       zval **single_header;
+                                                       
+                                                       FOREACH_VAL(pos2, *header, single_header) {
+                                                               ZVAL_ADDREF(*single_header);
+                                                               convert_to_string_ex(single_header);
+                                                               if (http_parse_cookie_ex(&list, Z_STRVAL_PP(single_header), flags, allowed_extras)) {
+                                                                       zval *cookie;
+                                                                       
+                                                                       MAKE_STD_ZVAL(cookie);
+                                                                       object_init(cookie);
+                                                                       http_cookie_list_tostruct(&list, cookie);
+                                                                       add_next_index_zval(return_value, cookie);
+                                                                       http_cookie_list_dtor(&list);
+                                                               }
+                                                               zval_ptr_dtor(single_header);
+                                                       }
+                                               } else {
+                                                       ZVAL_ADDREF(*header);
+                                                       convert_to_string_ex(header);
+                                                       if (http_parse_cookie_ex(&list, Z_STRVAL_PP(header), flags, allowed_extras)) {
                                                                zval *cookie;
                                                                
                                                                MAKE_STD_ZVAL(cookie);
@@ -1719,36 +1820,22 @@ PHP_METHOD(HttpRequest, getResponseCookies)
                                                                add_next_index_zval(return_value, cookie);
                                                                http_cookie_list_dtor(&list);
                                                        }
-                                                       zval_ptr_dtor(single_header);
-                                               }
-                                       } else {
-                                               ZVAL_ADDREF(*header);
-                                               convert_to_string_ex(header);
-                                               if (http_parse_cookie_ex(&list, Z_STRVAL_PP(header), flags, allowed_extras)) {
-                                                       zval *cookie;
-                                                               
-                                                       MAKE_STD_ZVAL(cookie);
-                                                       object_init(cookie);
-                                                       http_cookie_list_tostruct(&list, cookie);
-                                                       add_next_index_zval(return_value, cookie);
-                                                       http_cookie_list_dtor(&list);
+                                                       zval_ptr_dtor(header);
                                                }
-                                               zval_ptr_dtor(header);
                                        }
                                }
-                               /* reset key */
-                               key = NULL;
-                       }
-       
-                       if (allowed_extras) {
-                               for (i = 0; allowed_extras[i]; ++i) {
-                                       efree(allowed_extras[i]);
+                               
+                               if (allowed_extras) {
+                                       for (i = 0; allowed_extras[i]; ++i) {
+                                               efree(allowed_extras[i]);
+                                       }
+                                       efree(allowed_extras);
                                }
-                               efree(allowed_extras);
+                               
+                               return;
                        }
-               } else {
-                       RETURN_FALSE;
                }
+               RETURN_FALSE;
        }
 }
 /* }}} */
@@ -1766,13 +1853,12 @@ PHP_METHOD(HttpRequest, getResponseBody)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
-               zval **body;
-               zval *data = GET_PROP(responseData);
+       if (return_value_used) {
+               zval *message = GET_PROP(responseMessage);
                
-               if (    (Z_TYPE_P(data) == IS_ARRAY) && 
-                               (SUCCESS == zend_hash_find(Z_ARRVAL_P(data), "body", sizeof("body"), (void **) &body))) {
-                       RETURN_ZVAL(*body, 1, 0);
+               if (Z_TYPE_P(message) == IS_OBJECT) {
+                       getObjectEx(http_message_object, msg, message);
+                       RETURN_PHPSTR_DUP(&msg->message->body);
                } else {
                        RETURN_FALSE;
                }
@@ -1793,7 +1879,7 @@ PHP_METHOD(HttpRequest, getResponseCode)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(responseCode);
        }
 }
@@ -1809,7 +1895,7 @@ PHP_METHOD(HttpRequest, getResponseStatus)
 {
        NO_ARGS;
        
-       IF_RETVAL_USED {
+       if (return_value_used) {
                RETURN_PROP(responseStatus);
        }
 }
@@ -1833,7 +1919,7 @@ PHP_METHOD(HttpRequest, getResponseStatus)
  */
 PHP_METHOD(HttpRequest, getResponseInfo)
 {
-       IF_RETVAL_USED {
+       if (return_value_used) {
                zval *info, **infop;
                char *info_name = NULL;
                int info_len = 0;
@@ -1849,7 +1935,7 @@ PHP_METHOD(HttpRequest, getResponseInfo)
                }
 
                if (info_len && info_name) {
-                       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void **) &infop)) {
+                       if (SUCCESS == zend_hash_find(Z_ARRVAL_P(info), pretty_key(info_name, info_len, 0, 0), info_len + 1, (void *) &infop)) {
                                RETURN_ZVAL(*infop, 1, 0);
                        } else {
                                http_error_ex(HE_NOTICE, HTTP_E_INVALID_PARAM, "Could not find response info named %s", info_name);
@@ -1873,13 +1959,11 @@ PHP_METHOD(HttpRequest, getResponseInfo)
  * to access the data of previously received responses within this request
  * cycle.
  * 
- * Throws HttpException.
+ * Throws HttpException, HttpRuntimeException.
  */
 PHP_METHOD(HttpRequest, getResponseMessage)
 {
-       NO_ARGS;
-
-       IF_RETVAL_USED {
+       NO_ARGS {
                zval *message;
 
                SET_EH_THROW_HTTP();
@@ -1887,7 +1971,7 @@ PHP_METHOD(HttpRequest, getResponseMessage)
                if (Z_TYPE_P(message) == IS_OBJECT) {
                        RETVAL_OBJECT(message, 1);
                } else {
-                       RETVAL_NULL();
+                       http_error(HE_WARNING, HTTP_E_RUNTIME, "HttpRequest does not contain a response message");
                }
                SET_EH_NORMAL();
        }
@@ -1916,7 +2000,7 @@ PHP_METHOD(HttpRequest, getRequestMessage)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                http_message *msg;
                getObject(http_request_object, obj);
 
@@ -1934,13 +2018,12 @@ PHP_METHOD(HttpRequest, getRequestMessage)
  * Get sent HTTP message.
  * 
  * Returns an HttpMessage in a form of a string 
- * 
  */
 PHP_METHOD(HttpRequest, getRawRequestMessage)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                getObject(http_request_object, obj);
 
                RETURN_PHPSTR_DUP(&obj->request->conv.request);
@@ -1953,13 +2036,12 @@ PHP_METHOD(HttpRequest, getRawRequestMessage)
  * Get the entire HTTP response.
  * 
  * Returns the complete web server response, including the headers in a form of a string.
- * 
  */
 PHP_METHOD(HttpRequest, getRawResponseMessage)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                getObject(http_request_object, obj);
 
                RETURN_PHPSTR_DUP(&obj->request->conv.response);
@@ -1986,7 +2068,7 @@ PHP_METHOD(HttpRequest, getHistory)
 {
        NO_ARGS;
 
-       IF_RETVAL_USED {
+       if (return_value_used) {
                zval *hist;
                
                SET_EH_THROW_HTTP();