Merge branch 'R_2_5'
authorMichael Wallner <mike@php.net>
Mon, 28 Sep 2015 14:12:37 +0000 (16:12 +0200)
committerMichael Wallner <mike@php.net>
Mon, 28 Sep 2015 14:12:37 +0000 (16:12 +0200)
Conflicts:
.gitignore
config.w32

56 files changed:
1  2 
.gitignore
config9.m4
scripts/gen_curlinfo.php
scripts/gen_travis_yml.php
src/php_http.c
src/php_http_api.h
src/php_http_buffer.c
src/php_http_buffer.h
src/php_http_client.c
src/php_http_client.h
src/php_http_client_curl.c
src/php_http_client_curl.h
src/php_http_client_request.c
src/php_http_client_response.c
src/php_http_cookie.c
src/php_http_cookie.h
src/php_http_encoding.c
src/php_http_encoding.h
src/php_http_env.c
src/php_http_env.h
src/php_http_env_request.c
src/php_http_env_response.c
src/php_http_env_response.h
src/php_http_etag.c
src/php_http_etag.h
src/php_http_exception.c
src/php_http_exception.h
src/php_http_filter.c
src/php_http_header.c
src/php_http_header.h
src/php_http_header_parser.c
src/php_http_header_parser.h
src/php_http_info.c
src/php_http_info.h
src/php_http_message.c
src/php_http_message.h
src/php_http_message_body.c
src/php_http_message_body.h
src/php_http_message_parser.c
src/php_http_message_parser.h
src/php_http_misc.c
src/php_http_misc.h
src/php_http_negotiate.c
src/php_http_negotiate.h
src/php_http_object.c
src/php_http_object.h
src/php_http_options.c
src/php_http_options.h
src/php_http_params.c
src/php_http_params.h
src/php_http_querystring.c
src/php_http_querystring.h
src/php_http_url.c
src/php_http_url.h
src/php_http_version.c
src/php_http_version.h

diff --cc .gitignore
@@@ -40,4 -40,4 +40,5 @@@ tests/*.s
  lcov_data
  *~
  *.phar
 +!travis/*.phar
+ vendor/
diff --cc config9.m4
@@@ -613,53 -615,17 +615,20 @@@ dnl ---
        dnl extension deps
        PHP_ADD_EXTENSION_DEP([http], [raphf], true)
        PHP_ADD_EXTENSION_DEP([http], [propro], true)
-       
        PHP_SUBST([HTTP_SHARED_LIBADD])
  
-       PHP_HTTP_HEADERS="
-               php_http_api.h \
-               php_http_buffer.h \
-               php_http_curl_client.h \
-               php_http_curl_client_datashare.h \
-               php_http_client_datashare.h \
-               php_http_client_factory.h \
-               php_http_client.h \
-               php_http_client_interface.h \
-               php_http_curl_client_pool.h \
-               php_http_client_pool.h \
-               php_http_client_request.h \
-               php_http_client_response.h \
-               php_http_cookie.h \
-               php_http_curl.h \
-               php_http_encoding.h \
-               php_http_env.h \
-               php_http_env_request.h \
-               php_http_env_response.h \
-               php_http_etag.h \
-               php_http_exception.h \
-               php_http_filter.h \
-               php_http.h \
-               php_http_header_parser.h \
-               php_http_header.h \
-               php_http_info.h \
-               php_http_message_body.h \
-               php_http_message.h \
-               php_http_message_parser.h \
-               php_http_misc.h \
-               php_http_negotiate.h \
-               php_http_object.h \
-               php_http_options.h \
-               php_http_params.h \
-               php_http_querystring.h \
-               php_http_response_codes.h \
-               php_http_url.h \
-               php_http_utf8.h \
-               php_http_version.h \
-       "
-       PHP_INSTALL_HEADERS(ext/http, $PHP_HTTP_HEADERS)
+       PHP_HTTP_HEADERS=`(cd $ext_srcdir/src && echo *.h)`
+       PHP_INSTALL_HEADERS(ext/http, php_http.h $PHP_HTTP_HEADERS)
+       PHP_SUBST([PHP_HTTP_HEADERS])
+       PHP_HTTP_SRCDIR=$ext_srcdir
+       PHP_SUBST([PHP_HTTP_SRCDIR])
+       PHP_HTTP_BUILDDIR=$ext_builddir
+       PHP_SUBST([PHP_HTTP_BUILDDIR])
+       PHP_ADD_MAKEFILE_FRAGMENT
  
        AC_DEFINE([HAVE_HTTP], [1], [Have extended HTTP support])
 +      if $HTTP_HAVE_A_REQUEST_LIB; then
 +              AC_DEFINE([PHP_HTTP_HAVE_CLIENT], [1], [Have HTTP client support])
 +      fi
  fi
index 0000000,076a10d..2e2100f
mode 000000,100755..100755
--- /dev/null
@@@ -1,0 -1,99 +1,100 @@@
 -// $Id: gen_curlinfo.php 323304 2012-02-17 21:13:24Z mike $
+ #!/usr/bin/env php
+ <?php
 -              add_assoc_string_ex(&array, "%s", sizeof("%2$s"), c ? c : "", 1);
+ error_reporting(0);
+ function failure() {
+       // this is why error_get_last() should return a stdClass object
+       $error = error_get_last();
+       fprintf(STDERR, "FAILURE: %s\n", $error["message"]);
+       exit(-1);
+ }
+ function file_re($file, $pattern, $all = true) {
+       static $path;
+       
+       $path or $path = isset($_SERVER['argv'][1]) ? $_SERVER['argv'][1].'/include/curl/' : "/usr/local/include/curl/";
+       
+       if ($content = file_get_contents($path . $file)) {
+               if ($all) {
+                       if (preg_match_all($pattern, $content, $matches, PREG_SET_ORDER)) {
+                               return $matches;
+                       }
+               } else {
+                       if (preg_match($pattern, $content, $matches)) {
+                               return $matches;
+                       }
+               }
+               trigger_error("no match in $file for $pattern");
+       }
+       failure();
+ }
+ $ifdefs = array(
+       'PRIMARY_IP' => 'PHP_HTTP_CURL_VERSION(7,19,0)',
+       'APPCONNECT_TIME' => 'PHP_HTTP_CURL_VERSION(7,19,0)',
+     'CONDITION_UNMET' => 'PHP_HTTP_CURL_VERSION(7,19,4)',
+     'PRIMARY_PORT' => 'PHP_HTTP_CURL_VERSION(7,21,0)',
+     'LOCAL_PORT' => 'PHP_HTTP_CURL_VERSION(7,21,0)',
+     'LOCAL_IP' => 'PHP_HTTP_CURL_VERSION(7,21,0)',
+ );
+ $exclude = array(
+     'PRIVATE', 'LASTSOCKET', 'FTP_ENTRY_PATH', 'CERTINFO', 'TLS_SESSION',
+     'RTSP_SESSION_ID', 'RTSP_CLIENT_CSEQ', 'RTSP_SERVER_CSEQ', 'RTSP_CSEQ_RECV',
+       'COOKIELIST'
+ );
+ $translate = array(
+       'HTTP_CONNECTCODE' => "connect_code",
+       'COOKIELIST' => 'cookies',
+ );
+ $templates = array(
+ 'STRING' => 
+ '     if (CURLE_OK == curl_easy_getinfo(ch, %s, &c)) {
 -              add_assoc_double_ex(&array, "%s", sizeof("%2$s"), d);
++              ZVAL_STRING(&tmp, STR_PTR(c));
++              zend_hash_str_update(info, "%s", lenof("%2$s"), &tmp);
+       }
+ ',
+ 'DOUBLE' => 
+ '     if (CURLE_OK == curl_easy_getinfo(ch, %s, &d)) {
 -              add_assoc_long_ex(&array, "%s", sizeof("%2$s"), l);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "%s", lenof("%2$s"), &tmp);
+       }
+ ',
+ 'LONG' => 
+ '     if (CURLE_OK == curl_easy_getinfo(ch, %s, &l)) {
 -              MAKE_STD_ZVAL(subarray);
 -              array_init(subarray);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "%s", lenof("%2$s"), &tmp);
+       }
+ ',
+ 'SLIST' =>
+ '     if (CURLE_OK == curl_easy_getinfo(ch, %s, &s)) {
 -                              add_next_index_string(subarray, p->data, 1);
++              array_init(&tmp);
+               for (p = s; p; p = p->next) {
+                       if (p->data) {
 -              add_assoc_zval_ex(&array, "%s", sizeof("%2$s"), subarray);
++                              add_next_index_string(&tmp, p->data);
+                       }
+               }
++              zend_hash_str_update(info, "%s", lenof("%2$s"), &tmp);
+               curl_slist_free_all(s);
+       }
+ ',
+ );
+ $infos = file_re('curl.h', '/^\s*(CURLINFO_(\w+))\s*=\s*CURLINFO_(STRING|LONG|DOUBLE|SLIST)\s*\+\s*\d+\s*,?\s*$/m');
+ ob_start();
+ foreach ($infos as $info) {
+       list(, $full, $short, $type) = $info;
+       if (in_array($short, $exclude)) continue;
+       if (isset($ifdefs[$short])) printf("#if %s\n", $ifdefs[$short]);
+       printf($templates[$type], $full, strtolower((isset($translate[$short])) ? $translate[$short] : $short));
+       if (isset($ifdefs[$short])) printf("#endif\n");
+ }
+ file_put_contents("php_http_client_curl.c", 
+       preg_replace('/(\/\* BEGIN::CURLINFO \*\/\n).*(\n\s*\/\* END::CURLINFO \*\/)/s', '$1'. ob_get_contents() .'$2',
+               file_get_contents("php_http_client_curl.c")));
+ ?>
index 0000000,89ab0b2..60dbc6a
mode 000000,100755..100755
--- /dev/null
@@@ -1,0 -1,45 +1,46 @@@
 -      "PHP" => ["5.4", "5.5", "5.6"],
+ #!/usr/bin/env php
+ # autogenerated file; do not edit
+ language: c
+ addons:
+  apt:
+   packages:
+    - php5-cli
+    - php-pear
+    - libcurl4-openssl-dev
+    - zlib1g-dev
+    - libidn11-dev
+    - libevent-dev
+ env:
+ <?php
+ $gen = include "./travis/pecl/gen-matrix.php";
+ $env = $gen([
 -      "enable_iconv" => ["yes"]
++      "PHP" => ["master"],
+       "enable_debug",
+       "enable_maintainer_zts",
+       "enable_json",
+       "enable_hash" => ["yes"],
 - - make -f travis/pecl/Makefile pecl PECL=raphf
 - - make -f travis/pecl/Makefile pecl PECL=propro
++      "enable_iconv" => ["yes"],
++      "enable_phar" => ["yes"],
++      "enable_posix" => ["yes"]
+ ]);
+ foreach ($env as $e) {
+       printf(" - %s\n", $e);
+ }
+ ?>
+ before_script:
+  - make -f travis/pecl/Makefile php
++ - make -f travis/pecl/Makefile pharext/raphf-phpng pharext/propro-phpng
+ script:
+  - make -f travis/pecl/Makefile ext PECL=http
+  - make -f travis/pecl/Makefile test
+ after_script:
+  - test -e tests/helper/server.log && cat tests/helper/server.log
+ sudo: false
diff --cc src/php_http.c
index 0000000,2ff20f1..f9bb4d0
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,252 +1,245 @@@
 -static inline void php_http_globals_init(zend_php_http_globals *G TSRMLS_DC)
+ /*
+     +--------------------------------------------------------------------+
+     | PECL :: http                                                       |
+     +--------------------------------------------------------------------+
+     | Redistribution and use in source and binary forms, with or without |
+     | modification, are permitted provided that the conditions mentioned |
+     | in the accompanying LICENSE file are met.                          |
+     +--------------------------------------------------------------------+
+     | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
+     +--------------------------------------------------------------------+
+ */
+ #include "php_http_api.h"
+ #include <php_ini.h>
+ #include <ext/standard/info.h>
+ #include <zlib.h>
+ #if PHP_HTTP_HAVE_CURL
+ #     include <curl/curl.h>
+ #     if PHP_HTTP_HAVE_EVENT
+ #             if PHP_HTTP_HAVE_EVENT2
+ #                     include <event2/event.h>
+ #                     include <event2/event_struct.h>
+ #             else
+ #                     include <event.h>
+ #             endif
+ #     endif
+ #endif
+ #if PHP_HTTP_HAVE_IDN2
+ #     include <idn2.h>
+ #elif PHP_HTTP_HAVE_IDN
+ #     include <idna.h>
+ #endif
+ ZEND_DECLARE_MODULE_GLOBALS(php_http);
+ #ifdef COMPILE_DL_HTTP
+ ZEND_GET_MODULE(http)
+ #endif
+ zend_function_entry http_functions[] = {
+       EMPTY_FUNCTION_ENTRY
+ };
+ PHP_MINIT_FUNCTION(http);
+ PHP_MSHUTDOWN_FUNCTION(http);
+ PHP_RSHUTDOWN_FUNCTION(http);
+ PHP_MINFO_FUNCTION(http);
+ static zend_module_dep http_module_deps[] = {
+       ZEND_MOD_REQUIRED("raphf")
+       ZEND_MOD_REQUIRED("propro")
+       ZEND_MOD_REQUIRED("spl")
+ #ifdef PHP_HTTP_HAVE_HASH
+       ZEND_MOD_REQUIRED("hash")
+ #endif
+ #ifdef PHP_HTTP_HAVE_ICONV
+       ZEND_MOD_REQUIRED("iconv")
+ #endif
+       {NULL, NULL, NULL, 0}
+ };
+ zend_module_entry http_module_entry = {
+       STANDARD_MODULE_HEADER_EX,
+       NULL,
+       http_module_deps,
+       "http",
+       http_functions,
+       PHP_MINIT(http),
+       PHP_MSHUTDOWN(http),
+       NULL,
+       PHP_RSHUTDOWN(http),
+       PHP_MINFO(http),
+       PHP_PECL_HTTP_VERSION,
+       STANDARD_MODULE_PROPERTIES
+ };
+ int http_module_number;
+ #if PHP_DEBUG && !HAVE_GCOV
+ void _dpf(int type, const char *data, size_t length)
+ {
+       static const char _sym[] = "><><><";
+       if (type) {
+               int nwp = 0;
+               for (fprintf(stderr, "%c ", _sym[type-1]); length--; data++) {
+                       int ip = PHP_HTTP_IS_CTYPE(print, *data);
+                       if (!ip && *data != '\r' && *data != '\n') nwp = 1;
+                       fprintf(stderr, ip?"%c":"\\x%02x", (int) (*data & 0xff));
+                       if (!nwp && *data == '\n' && length) {
+                               fprintf(stderr, "\n%c ", _sym[type-1]);
+                       }
+               }
+               fprintf(stderr, "\n");
+       } else {
+               fprintf(stderr, "# %.*s\n", (int) length, data);
+       }
+ }
+ #endif
+ static void php_http_globals_init_once(zend_php_http_globals *G)
+ {
+       memset(G, 0, sizeof(*G));
+ }
+ #if 0
 -static inline void php_http_globals_free(zend_php_http_globals *G TSRMLS_DC)
++static inline void php_http_globals_init(zend_php_http_globals *G)
+ {
+ }
 -#if ZTS && PHP_DEBUG && !HAVE_GCOV
 -zend_php_http_globals *php_http_globals(void)
 -{
 -      TSRMLS_FETCH();
 -      return PHP_HTTP_G;
 -}
 -#endif
 -
++static inline void php_http_globals_free(zend_php_http_globals *G)
+ {
+ }
+ #endif
+ PHP_INI_BEGIN()
+       STD_PHP_INI_ENTRY("http.etag.mode", "crc32b", PHP_INI_ALL, OnUpdateString, env.etag_mode, zend_php_http_globals, php_http_globals)
+ PHP_INI_END()
+ PHP_MINIT_FUNCTION(http)
+ {
+       http_module_number = module_number;
+       ZEND_INIT_MODULE_GLOBALS(php_http, php_http_globals_init_once, NULL);
+       REGISTER_INI_ENTRIES();
+       
+       if (0
++      || SUCCESS != PHP_MINIT_CALL(http_object)
+       || SUCCESS != PHP_MINIT_CALL(http_exception)
+       || SUCCESS != PHP_MINIT_CALL(http_cookie)
+       || SUCCESS != PHP_MINIT_CALL(http_encoding)
+       || SUCCESS != PHP_MINIT_CALL(http_filter)
+       || SUCCESS != PHP_MINIT_CALL(http_header)
+       || SUCCESS != PHP_MINIT_CALL(http_header_parser)
+       || SUCCESS != PHP_MINIT_CALL(http_message)
+       || SUCCESS != PHP_MINIT_CALL(http_message_parser)
+       || SUCCESS != PHP_MINIT_CALL(http_message_body)
+       || SUCCESS != PHP_MINIT_CALL(http_querystring)
+       || SUCCESS != PHP_MINIT_CALL(http_client)
+       || SUCCESS != PHP_MINIT_CALL(http_client_request)
+       || SUCCESS != PHP_MINIT_CALL(http_client_response)
+ #if PHP_HTTP_HAVE_CURL
+       || SUCCESS != PHP_MINIT_CALL(http_curl)
+       || SUCCESS != PHP_MINIT_CALL(http_client_curl)
+ #endif
+       || SUCCESS != PHP_MINIT_CALL(http_url)
+       || SUCCESS != PHP_MINIT_CALL(http_env)
+       || SUCCESS != PHP_MINIT_CALL(http_env_request)
+       || SUCCESS != PHP_MINIT_CALL(http_env_response)
+       || SUCCESS != PHP_MINIT_CALL(http_params)
+       ) {
+               return FAILURE;
+       }
+       
+       return SUCCESS;
+ }
+ PHP_MSHUTDOWN_FUNCTION(http)
+ {
+       UNREGISTER_INI_ENTRIES();
+       
+       if (0
+       || SUCCESS != PHP_MSHUTDOWN_CALL(http_message)
+ #if PHP_HTTP_HAVE_CURL
+       || SUCCESS != PHP_MSHUTDOWN_CALL(http_client_curl)
+       || SUCCESS != PHP_MSHUTDOWN_CALL(http_curl)
+ #endif
+       || SUCCESS != PHP_MSHUTDOWN_CALL(http_client)
+       ) {
+               return FAILURE;
+       }
+       
+       return SUCCESS;
+ }
+ PHP_RSHUTDOWN_FUNCTION(http)
+ {
+       if (0
+       || SUCCESS != PHP_RSHUTDOWN_CALL(http_env)
+       ) {
+               return FAILURE;
+       }
+       
+       return SUCCESS;
+ }
+ PHP_MINFO_FUNCTION(http)
+ {
+       php_http_buffer_t buf;
+       php_http_buffer_init(&buf);
+       php_info_print_table_start();
+       php_info_print_table_header(2, "HTTP Support", "enabled");
+       php_info_print_table_row(2, "Extension Version", PHP_PECL_HTTP_VERSION);
+       php_info_print_table_end();
+       
+       php_info_print_table_start();
+       php_info_print_table_header(3, "Used Library", "Compiled", "Linked");
+       php_info_print_table_row(3, "libz", ZLIB_VERSION, zlibVersion());
+ #if PHP_HTTP_HAVE_CURL
+       {
+               curl_version_info_data *cv = curl_version_info(CURLVERSION_NOW);
+               php_info_print_table_row(3, "libcurl", LIBCURL_VERSION, cv->version);
+       }
+ #else
+       php_info_print_table_row(3, "libcurl", "disabled", "disabled");
+ #endif
+ #if PHP_HTTP_HAVE_EVENT
+       php_info_print_table_row(3, "libevent",
+ #     ifdef LIBEVENT_VERSION
+                       LIBEVENT_VERSION,
+ #     else
+                       PHP_HTTP_EVENT_VERSION,
+ #     endif
+                       event_get_version());
+ #else
+       php_info_print_table_row(3, "libevent", "disabled", "disabled");
+ #endif
+ #if PHP_HTTP_HAVE_IDN2
+       php_info_print_table_row(3, "libidn2 (IDNA2008)", IDN2_VERSION, idn2_check_version(NULL));
+ #elif PHP_HTTP_HAVE_IDN
+       php_info_print_table_row(3, "libidn (IDNA2003)", PHP_HTTP_LIBIDN_VERSION, "unknown");
+ #endif
+       php_info_print_table_end();
+       
+       DISPLAY_INI_ENTRIES();
+ }
+ /*
+  * Local variables:
+  * tab-width: 4
+  * c-basic-offset: 4
+  * End:
+  * vim600: noet sw=4 ts=4 fdm=marker
+  * vim<600: noet sw=4 ts=4
+  */
index 0000000,08b6ba8..2313413
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,141 +1,148 @@@
 -#     define PHP_HTTP_G ((zend_php_http_globals *) (*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(php_http_globals_id)])
+ /*
+     +--------------------------------------------------------------------+
+     | PECL :: http                                                       |
+     +--------------------------------------------------------------------+
+     | Redistribution and use in source and binary forms, with or without |
+     | modification, are permitted provided that the conditions mentioned |
+     | in the accompanying LICENSE file are met.                          |
+     +--------------------------------------------------------------------+
+     | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
+     +--------------------------------------------------------------------+
+ */
+ #ifndef PHP_HTTP_API_H
+ #define PHP_HTTP_API_H
+ #ifdef HAVE_CONFIG_H
+ #include "config.h"
+ #endif
+ #ifndef PHP_WIN32
+ #include <php_config.h>
+ #endif
+ #include <php.h>
+ #include <SAPI.h>
+ #include <ext/raphf/php_raphf.h>
+ #include <ext/propro/php_propro.h>
+ #include <ext/standard/php_string.h>
+ #include <ext/spl/spl_iterators.h>
+ #include <ext/date/php_date.h>
+ #include <zend_interfaces.h>
+ #include <zend_exceptions.h>
+ #ifdef PHP_WIN32
+ # define PHP_HTTP_API __declspec(dllexport)
+ #elif defined(__GNUC__) && __GNUC__ >= 4
+ # define PHP_HTTP_API extern __attribute__ ((visibility("default")))
+ #else
+ # define PHP_HTTP_API extern
+ #endif
+ #if (defined(HAVE_ICONV) || defined(PHP_HTTP_HAVE_EXT_ICONV)) && (PHP_HTTP_SHARED_DEPS || !defined(COMPILE_DL_ICONV))
+ #     define PHP_HTTP_HAVE_ICONV
+ #endif
+ #if (defined(HAVE_HASH_EXT) || defined(PHP_HTTP_HAVE_EXT_HASH)) && (PHP_HTTP_SHARED_DEPS || !defined(COMPILE_DL_HASH)) && defined(PHP_HTTP_HAVE_PHP_HASH_H)
+ #     define PHP_HTTP_HAVE_HASH
+ #endif
+ #include <stddef.h>
+ #ifdef PHP_WIN32
+ #     define CURL_STATICLIB
+ #     include <winsock2.h>
+ #else
+ #     ifdef HAVE_NETDB_H
+ #             include <netdb.h>
+ #     endif
+ #     ifdef HAVE_UNISTD_H
+ #             include <unistd.h>
+ #     endif
+ #endif
+ #if defined(HAVE_WCHAR_H) && defined(HAVE_WCTYPE_H) && defined(HAVE_ISWALNUM) && (defined(HAVE_MBRTOWC) || defined(HAVE_MBTOWC))
+ #     define PHP_HTTP_HAVE_WCHAR 1
+ #endif
+ #include <ctype.h>
+ #define PHP_HTTP_IS_CTYPE(type, c) is##type((int) (unsigned char) (c))
+ #define PHP_HTTP_TO_CTYPE(type, c) to##type((int) (unsigned char) (c))
+ #include "php_http.h"
+ #include "php_http_buffer.h"
+ #include "php_http_misc.h"
+ #include "php_http_options.h"
+ #include "php_http.h"
+ #include "php_http_cookie.h"
+ #include "php_http_encoding.h"
+ #include "php_http_info.h"
+ #include "php_http_message.h"
+ #include "php_http_env.h"
+ #include "php_http_env_request.h"
+ #include "php_http_env_response.h"
+ #include "php_http_etag.h"
+ #include "php_http_exception.h"
+ #include "php_http_filter.h"
+ #include "php_http_header_parser.h"
+ #include "php_http_header.h"
+ #include "php_http_message_body.h"
+ #include "php_http_message_parser.h"
+ #include "php_http_negotiate.h"
+ #include "php_http_object.h"
+ #include "php_http_params.h"
+ #include "php_http_querystring.h"
+ #include "php_http_client.h"
+ #include "php_http_curl.h"
+ #include "php_http_client_request.h"
+ #include "php_http_client_response.h"
+ #include "php_http_client_curl.h"
+ #include "php_http_url.h"
+ #include "php_http_version.h"
+ ZEND_BEGIN_MODULE_GLOBALS(php_http)
+       struct php_http_env_globals env;
++#ifdef PHP_HTTP_HAVE_CLIENT
++      struct {
++#ifdef PHP_HTTP_HAVE_CURL
++              struct php_http_client_curl_globals curl;
++#endif
++      } client;
++#endif
+ ZEND_END_MODULE_GLOBALS(php_http)
+ ZEND_EXTERN_MODULE_GLOBALS(php_http);
+ #ifdef ZTS
+ #     include "TSRM/TSRM.h"
 -#     define TSRMLS_FETCH_FROM_CTX(ctx) void ***tsrm_ls = ((ctx)?(ctx):ts_resource_ex(0, NULL))
++#     define PHP_HTTP_G ((zend_php_http_globals *) (*((void ***) tsrm_get_ls_cache()))[TSRM_UNSHUFFLE_RSRC_ID(php_http_globals_id)])
+ #     undef TSRMLS_FETCH_FROM_CTX
++#     define TSRMLS_FETCH_FROM_CTX(ctx) ERROR
+ #else
+ #     define PHP_HTTP_G (&php_http_globals)
+ #endif
+ #if PHP_DEBUG
+ #     define _DPF_STR 0
+ #     define _DPF_IN  1
+ #     define _DPF_OUT 2
+ extern void _dpf(int type, const char *data, size_t length);
+ #else
+ #     define _dpf(t,s,l);
+ #endif
+ #endif /* PHP_HTTP_API_H */
+ /*
+  * Local variables:
+  * tab-width: 4
+  * c-basic-offset: 4
+  * End:
+  * vim600: noet sw=4 ts=4 fdm=marker
+  * vim<600: noet sw=4 ts=4
+  */
index 0000000,e24a4e1..b84bcd0
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,459 +1,502 @@@
 -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_init_ex(php_http_buffer_t *buf, size_t chunk_size, int flags)
+ /*
+     +--------------------------------------------------------------------+
+     | PECL :: http                                                       |
+     +--------------------------------------------------------------------+
+     | Redistribution and use in source and binary forms, with or without |
+     | modification, are permitted provided that the conditions mentioned |
+     | in the accompanying LICENSE file are met.                          |
+     +--------------------------------------------------------------------+
+     | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
+     +--------------------------------------------------------------------+
+ */
+ #include <php.h>
+ #include "php_http_buffer.h"
 -              buf->data = (flags & PHP_HTTP_BUFFER_INIT_PREALLOC) ? pemalloc(buf->size, buf->pmem) : NULL;
++PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_init_ex(
++              php_http_buffer_t *buf, size_t chunk_size, unsigned flags)
+ {
+       if (!buf) {
+               buf = pemalloc(sizeof(*buf), flags & PHP_HTTP_BUFFER_INIT_PERSISTENT);
+       }
+       if (buf) {
+               buf->size = (chunk_size) ? chunk_size : PHP_HTTP_BUFFER_DEFAULT_SIZE;
+               buf->pmem = (flags & PHP_HTTP_BUFFER_INIT_PERSISTENT) ? 1 : 0;
 -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_from_string_ex(php_http_buffer_t *buf, const char *string, size_t length)
++              buf->data = (flags & PHP_HTTP_BUFFER_INIT_PREALLOC) ?
++                              pemalloc(buf->size, buf->pmem) : NULL;
+               buf->free = (flags & PHP_HTTP_BUFFER_INIT_PREALLOC) ? buf->size : 0;
+               buf->used = 0;
+       }
+       
+       return buf;
+ }
 -              if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(buf, string, length)) {
++PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_from_string_ex(
++              php_http_buffer_t *buf, const char *str, size_t len)
+ {
+       if ((buf = php_http_buffer_init(buf))) {
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_resize_ex(php_http_buffer_t *buf, size_t len, size_t override_size, int allow_error)
++              if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(buf, str, len)) {
+                       pefree(buf, buf->pmem);
+                       buf = NULL;
+               }
+       }
+       return buf;
+ }
 -                      ptr = perealloc_recoverable(buf->data, buf->used + buf->free + size, buf->pmem);
++PHP_HTTP_BUFFER_API size_t php_http_buffer_resize_ex(
++              php_http_buffer_t *buf, size_t len, size_t override_size,
++              zend_bool allow_error)
+ {
+       char *ptr = NULL;
+ #if 0
+       fprintf(stderr, "RESIZE: len=%lu, size=%lu, used=%lu, free=%lu, total=%lu\n", len, buf->size, buf->used, buf->free, buf->free+buf->used);
+ #endif
+       if (buf->free < len) {
+               size_t size = override_size ? override_size : buf->size;
+               
+               while ((size + buf->free) < len) {
+                       size <<= 1;
+               }
+               
+               if (allow_error) {
 -PHP_HTTP_BUFFER_API char *php_http_buffer_account(php_http_buffer_t *buf, size_t to_account)
++                      ptr = perealloc_recoverable(buf->data,
++                                      buf->used + buf->free + size, buf->pmem);
+               } else {
+                       ptr = perealloc(buf->data, buf->used + buf->free + size, buf->pmem);
+               }
+               
+               if (ptr) {
+                       buf->data = ptr;
+               } else {
+                       return PHP_HTTP_BUFFER_NOMEM;
+               }
+               
+               buf->free += size;
+               return size;
+       }
+       return 0;
+ }
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_append(php_http_buffer_t *buf, const char *append, size_t append_len)
++PHP_HTTP_BUFFER_API char *php_http_buffer_account(
++              php_http_buffer_t *buf, size_t to_account)
+ {
+       assert(to_account <= buf->free);
+       buf->free -= to_account;
+       buf->used += to_account;
+       return buf->data + buf->used;
+ }
+ PHP_HTTP_BUFFER_API size_t php_http_buffer_shrink(php_http_buffer_t *buf)
+ {
+       /* avoid another realloc on fixation */
+       if (buf->free > 1) {
+               char *ptr = perealloc(buf->data, buf->used + 1, buf->pmem);
+               
+               if (ptr) {
+                       buf->data = ptr;
+               } else {
+                       return PHP_HTTP_BUFFER_NOMEM;
+               }
+               buf->free = 1;
+       }
+       return buf->used;
+ }
 -      if (buf->free < append_len && PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize(buf, append_len)) {
++PHP_HTTP_BUFFER_API size_t php_http_buffer_append(php_http_buffer_t *buf,
++              const char *append, size_t append_len)
+ {
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_appendf(php_http_buffer_t *buf, const char *format, ...)
++      if (    buf->free < append_len &&
++                      PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize(buf, append_len)
++      ) {
+               return PHP_HTTP_BUFFER_NOMEM;
+       }
+       memcpy(buf->data + buf->used, append, append_len);
+       buf->used += append_len;
+       buf->free -= append_len;
+       return append_len;
+ }
 -PHP_HTTP_BUFFER_API char *php_http_buffer_data(const php_http_buffer_t *buf, char **into, size_t *len)
++PHP_HTTP_BUFFER_API size_t php_http_buffer_appendf(php_http_buffer_t *buf,
++              const char *format, ...)
+ {
+       va_list argv;
+       char *append;
+       size_t append_len, alloc;
+       va_start(argv, format);
+       append_len = vspprintf(&append, 0, format, argv);
+       va_end(argv);
+       alloc = php_http_buffer_append(buf, append, append_len);
+       efree(append);
+       if (PHP_HTTP_BUFFER_NOMEM == alloc) {
+               return PHP_HTTP_BUFFER_NOMEM;
+       }
+       return append_len;
+ }
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_cut(php_http_buffer_t *buf, size_t offset, size_t length)
++PHP_HTTP_BUFFER_API char *php_http_buffer_data(const php_http_buffer_t *buf,
++              char **into, size_t *len)
+ {
+       char *copy = ecalloc(1, buf->used + 1);
+       memcpy(copy, buf->data, buf->used);
+       if (into) {
+               *into = copy;
+       }
+       if (len) {
+               *len = buf->used;
+       }
+       return copy;
+ }
 -      memmove(buf->data + offset, buf->data + offset + length, buf->used - length - offset);
++PHP_HTTP_BUFFER_API size_t php_http_buffer_cut(php_http_buffer_t *buf,
++              size_t offset, size_t length)
+ {
+       if (offset > buf->used) {
+               return 0;
+       }
+       if (offset + length > buf->used) {
+               length = buf->used - offset;
+       }
 -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_fix(php_http_buffer_t *buf)
++      memmove(buf->data + offset, buf->data + offset + length,
++                      buf->used - length - offset);
+       buf->used -= length;
+       buf->free += length;
+       return length;
+ }
 -      if (buf->free < 1 && PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize_ex(buf, 1, 1, 0)) {
++PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_fix(
++              php_http_buffer_t *buf)
+ {
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_chunk_buffer(php_http_buffer_t **s, const char *data, size_t data_len, char **chunk, size_t chunk_size)
++      if (    buf->free < 1 &&
++                      PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize_ex(buf, 1, 1, 0)
++      ) {
+               return NULL;
+       }
+       buf->data[buf->used] = '\0';
+       return buf;
+ }
+ PHP_HTTP_BUFFER_API void php_http_buffer_reset(php_http_buffer_t *buf)
+ {
+       buf->free += buf->used;
+       buf->used = 0;
+ }
+ PHP_HTTP_BUFFER_API void php_http_buffer_dtor(php_http_buffer_t *buf)
+ {
+       if (buf->data) {
+               pefree(buf->data, buf->pmem);
+               buf->data = NULL;
+       }
+       buf->used = 0;
+       buf->free = 0;
+ }
+ PHP_HTTP_BUFFER_API void php_http_buffer_free(php_http_buffer_t **buf)
+ {
+       if (*buf) {
+               php_http_buffer_dtor(*buf);
+               pefree(*buf, (*buf)->pmem);
+               *buf = NULL;
+       }
+ }
 -              *s = php_http_buffer_init_ex(NULL, chunk_size << 1, chunk_size ? PHP_HTTP_BUFFER_INIT_PREALLOC : 0);
++PHP_HTTP_BUFFER_API size_t php_http_buffer_chunk_buffer(php_http_buffer_t **s,
++              const char *data, size_t data_len, char **chunk, size_t chunk_size)
+ {
+       php_http_buffer_t *storage;
+       
+       *chunk = NULL;
+       
+       if (!*s) {
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_chunked_output(php_http_buffer_t **s, const char *data, size_t data_len, size_t chunk_len, php_http_buffer_pass_func_t passout, void *opaque TSRMLS_DC)
++              *s = php_http_buffer_init_ex(NULL, chunk_size << 1,
++                              chunk_size ? PHP_HTTP_BUFFER_INIT_PREALLOC : 0);
+       }
+       storage = *s;
+       
+       if (data_len) {
+               php_http_buffer_append(storage, data, data_len);
+       }
+       
+       if (!chunk_size) {
+               php_http_buffer_data(storage, chunk, &chunk_size);
+               php_http_buffer_free(s);
+               return chunk_size;
+       }
+       
+       if (storage->used >= chunk_size) {
+               *chunk = estrndup(storage->data, chunk_size);
+               php_http_buffer_cut(storage, 0, chunk_size);
+               return chunk_size;
+       }
+       
+       return 0;
+ }
 -              if (PHP_HTTP_BUFFER_PASS0 == passout(opaque, chunk, got TSRMLS_CC)) {
++PHP_HTTP_BUFFER_API size_t php_http_buffer_chunked_output(php_http_buffer_t **s,
++              const char *data, size_t data_len, size_t chunk_len,
++              php_http_buffer_pass_func_t passout, void *opaque)
+ {
+       char *chunk = NULL;
+       size_t passed = 0, got = 0;
+       while ((got = php_http_buffer_chunk_buffer(s, data, data_len, &chunk, chunk_len))) {
 -PHP_HTTP_BUFFER_API ssize_t php_http_buffer_passthru(php_http_buffer_t **s, size_t chunk_size, php_http_buffer_pass_func_t passin, void *passin_arg, php_http_buffer_pass_func_t passon, void *passon_arg TSRMLS_DC)
++              if (PHP_HTTP_BUFFER_PASS0 == passout(opaque, chunk, got)) {
+                       PTR_SET(chunk, NULL);
+                       return PHP_HTTP_BUFFER_PASS0;
+               }
+               ++passed;
+               if (!chunk_len) {
+                       /*      we already got the last chunk,
+                               and freed all resources */
+                       break;
+               }
+               data = NULL;
+               data_len = 0;
+               PTR_SET(chunk, NULL);
+       }
+       PTR_FREE(chunk);
+       return passed;
+ }
 -      size_t passed_on = 0, passed_in = php_http_buffer_chunked_input(s, chunk_size, passin, passin_arg TSRMLS_CC);
++PHP_HTTP_BUFFER_API ssize_t php_http_buffer_passthru(php_http_buffer_t **s, size_t chunk_size,
++              php_http_buffer_pass_func_t passin, void *passin_arg,
++              php_http_buffer_pass_func_t passon, void *passon_arg)
+ {
 -              passed_on = passon(passon_arg, (*s)->data, (*s)->used TSRMLS_CC);
++      size_t passed_on = 0, passed_in;
++
++      passed_in = php_http_buffer_chunked_input(s, chunk_size, passin, passin_arg);
+       if (passed_in == PHP_HTTP_BUFFER_PASS0) {
+               return passed_in;
+       }
+       if (passed_in || (*s)->used) {
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_chunked_input(php_http_buffer_t **s, size_t chunk_size, php_http_buffer_pass_func_t passin, void *opaque TSRMLS_DC)
++              passed_on = passon(passon_arg, (*s)->data, (*s)->used);
+               if (passed_on == PHP_HTTP_BUFFER_PASS0) {
+                       return passed_on;
+               }
+               if (passed_on) {
+                       php_http_buffer_cut(*s, 0, passed_on);
+               }
+       }
+       return passed_on - passed_in;
+ }
 -              *s = php_http_buffer_init_ex(NULL, chunk_size, chunk_size ? PHP_HTTP_BUFFER_INIT_PREALLOC : 0);
++PHP_HTTP_BUFFER_API size_t php_http_buffer_chunked_input(php_http_buffer_t **s,
++              size_t chunk_size, php_http_buffer_pass_func_t passin, void *opaque)
+ {
+       php_http_buffer_t *str;
+       size_t passed;
+       if (!*s) {
 -      passed = passin(opaque, str->data + str->used, chunk_size TSRMLS_CC);
++              *s = php_http_buffer_init_ex(NULL, chunk_size,
++                              chunk_size ? PHP_HTTP_BUFFER_INIT_PREALLOC : 0);
+       }
+       str = *s;
+       php_http_buffer_resize(str, chunk_size);
 -PHP_HTTP_BUFFER_API int php_http_buffer_cmp(php_http_buffer_t *left, php_http_buffer_t *right)
++      passed = passin(opaque, str->data + str->used, chunk_size);
+       if (passed != PHP_HTTP_BUFFER_PASS0) {
+               str->used += passed;
+               str->free -= passed;
+       }
+       php_http_buffer_fix(str);
+       return passed;
+ }
+ #ifdef PHP_HTTP_BUFFER_EXTENDED
 -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_copy(const php_http_buffer_t *from, php_http_buffer_t *to)
++PHP_HTTP_BUFFER_API int php_http_buffer_cmp(php_http_buffer_t *left,
++              php_http_buffer_t *right)
+ {
+       if (left->used > right->used) {
+               return -1;
+       } else if (right->used > left->used) {
+               return 1;
+       } else {
+               return memcmp(left->data, right->data, left->used);
+       }
+ }
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_insert(php_http_buffer_t *buf, const char *insert, size_t insert_len, size_t offset)
++PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_copy(
++              const php_http_buffer_t *from, php_http_buffer_t *to)
+ {
+       int free_to = !to;
+       to = php_http_buffer_clone(from, to);
+       if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(to, from->data, from->used)) {
+               if (free_to) {
+                       php_http_buffer_free(&to);
+               } else {
+                       php_http_buffer_dtor(to);
+               }
+       }
+       return to;
+ }
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_insertf(php_http_buffer_t *buf, size_t offset, const char *format, ...)
++PHP_HTTP_BUFFER_API size_t php_http_buffer_insert(php_http_buffer_t *buf,
++              const char *insert, size_t insert_len, size_t offset)
+ {
+       if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize(buf, insert_len)) {
+               return PHP_HTTP_BUFFER_NOMEM;
+       }
+       memmove(buf->data + offset + insert_len, buf->data + offset, insert_len);
+       memcpy(buf->data + offset, insert, insert_len);
+       buf->used += insert_len;
+       buf->free -= insert_len;
+       return insert_len;
+ }
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_prepend(php_http_buffer_t *buf, const char *prepend, size_t prepend_len)
++PHP_HTTP_BUFFER_API size_t php_http_buffer_insertf(php_http_buffer_t *buf,
++              size_t offset, const char *format, ...)
+ {
+       va_list argv;
+       char *insert;
+       size_t insert_len, alloc;
+       va_start(argv, format);
+       insert_len = vspprintf(&insert, 0, format, argv);
+       va_end(argv);
+       alloc = php_http_buffer_insert(buf, insert, insert_len, offset);
+       efree(insert);
+       if (PHP_HTTP_BUFFER_NOMEM == alloc) {
+               return PHP_HTTP_BUFFER_NOMEM;
+       }
+       return insert_len;
+ }
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_prependf(php_http_buffer_t *buf, const char *format, ...)
++PHP_HTTP_BUFFER_API size_t php_http_buffer_prepend(php_http_buffer_t *buf,
++              const char *prepend, size_t prepend_len)
+ {
+       if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_resize(buf, prepend_len)) {
+               return PHP_HTTP_BUFFER_NOMEM;
+       }
+       memmove(buf->data + prepend_len, buf->data, buf->used);
+       memcpy(buf->data, prepend, prepend_len);
+       buf->used += prepend_len;
+       buf->free -= prepend_len;
+       return prepend_len;
+ }
 -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_sub(const php_http_buffer_t *buf, size_t offset, size_t length)
++PHP_HTTP_BUFFER_API size_t php_http_buffer_prependf(php_http_buffer_t *buf,
++              const char *format, ...)
+ {
+       va_list argv;
+       char *prepend;
+       size_t prepend_len, alloc;
+       va_start(argv, format);
+       prepend_len = vspprintf(&prepend, 0, format, argv);
+       va_end(argv);
+       alloc = php_http_buffer_prepend(buf, prepend, prepend_len);
+       efree(prepend);
+       if (PHP_HTTP_BUFFER_NOMEM == alloc) {
+               return PHP_HTTP_BUFFER_NOMEM;
+       }
+       return prepend_len;
+ }
 -              size_t need = 1 + ((length + offset) > buf->used ? (buf->used - offset) : (length - offset));
 -              php_http_buffer_t *sub = php_http_buffer_init_ex(NULL, need, PHP_HTTP_BUFFER_INIT_PREALLOC | (buf->pmem ? PHP_HTTP_BUFFER_INIT_PERSISTENT:0));
++PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_sub(
++              const php_http_buffer_t *buf, size_t offset, size_t length)
+ {
+       if (offset >= buf->used) {
+               return NULL;
+       } else {
 -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_right(const php_http_buffer_t *buf, size_t length)
++              php_http_buffer_t *sub;
++              size_t need = 1 + ((length + offset) > buf->used ?
++                              (buf->used - offset) : (length - offset));
++              unsigned flags = buf->pmem ? PHP_HTTP_BUFFER_INIT_PERSISTENT : 0;
++
++              sub = php_http_buffer_init_ex(NULL, need,
++                              PHP_HTTP_BUFFER_INIT_PREALLOC | flags);
++
+               if (sub) {
+                       if (PHP_HTTP_BUFFER_NOMEM == php_http_buffer_append(sub, buf->data + offset, need)) {
+                               php_http_buffer_free(&sub);
+                       } else {
+                               sub->size = buf->size;
+                       }
+               }
+               return sub;
+       }
+ }
 -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_va(php_http_buffer_t *buf, unsigned argc, va_list argv)
++PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_right(
++              const php_http_buffer_t *buf, size_t length)
+ {
+       if (length < buf->used) {
+               return php_http_buffer_sub(buf, buf->used - length, length);
+       } else {
+               return php_http_buffer_sub(buf, 0, buf->used);
+       }
+ }
 -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_ex(php_http_buffer_t *buf, unsigned argc, ...)
++PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_va(
++              php_http_buffer_t *buf, unsigned argc, va_list argv)
+ {
+       unsigned i = 0;
+       buf = php_http_buffer_init(buf);
+       if (buf) {
+               while (argc > i++) {
+                       php_http_buffer_free_t f = va_arg(argv, php_http_buffer_free_t);
+                       php_http_buffer_t *current = va_arg(argv, php_http_buffer_t *);
+                       php_http_buffer_append(buf, current->data, current->used);
+                       FREE_PHP_HTTP_BUFFER(f, current);
+               }
+       }
+       return buf;
+ }
++PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_ex(
++              php_http_buffer_t *buf, unsigned argc, ...)
+ {
+       va_list argv;
+       php_http_buffer_t *ret;
+       va_start(argv, argc);
+       ret = php_http_buffer_merge_va(buf, argc, argv);
+       va_end(argv);
+       return ret;
+ }
+ PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge(unsigned argc, ...)
+ {
+       va_list argv;
+       php_http_buffer_t *ret;
+       va_start(argv, argc);
+       ret = php_http_buffer_merge_va(NULL, argc, argv);
+       va_end(argv);
+       return ret;
+ }
+ #endif
+ /*
+  * Local variables:
+  * tab-width: 4
+  * c-basic-offset: 4
+  * End:
+  * vim600: sw=4 ts=4 fdm=marker
+  * vim<600: sw=4 ts=4
+  */
index 0000000,faf8992..818c443
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,250 +1,245 @@@
 -#ifndef TSRMLS_D
 -#     define TSRMLS_D
 -#     define TSRMLS_DC
 -#     define TSRMLS_CC
 -#     define TSRMLS_C
 -#endif
+ /*
+     +--------------------------------------------------------------------+
+     | PECL :: http                                                       |
+     +--------------------------------------------------------------------+
+     | Redistribution and use in source and binary forms, with or without |
+     | modification, are permitted provided that the conditions mentioned |
+     | in the accompanying LICENSE file are met.                          |
+     +--------------------------------------------------------------------+
+     | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
+     +--------------------------------------------------------------------+
+ */
+ #ifndef PHP_HTTP_BUFFER_H
+ #define PHP_HTTP_BUFFER_H
+ #ifndef PHP_HTTP_BUFFER_DEFAULT_SIZE
+ #     define PHP_HTTP_BUFFER_DEFAULT_SIZE 256
+ #endif
+ #define PHP_HTTP_BUFFER_ERROR ((size_t) -1)
+ #define PHP_HTTP_BUFFER_NOMEM PHP_HTTP_BUFFER_ERROR
+ #define PHP_HTTP_BUFFER_PASS0 PHP_HTTP_BUFFER_ERROR
+ #ifndef PTR_FREE
+ #     define PTR_FREE(PTR) \
+       { \
+               if (PTR) { \
+                       efree(PTR); \
+               } \
+       }
+ #endif
+ #ifndef PTR_SET
+ #     define PTR_SET(PTR, SET) \
+       { \
+               PTR_FREE(PTR); \
+               PTR = SET; \
+       }
+ #endif
 -              pefree(STR, STR->pmem); break; \
+ #ifdef PHP_ATTRIBUTE_FORMAT
+ #     define PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(f, a, b) PHP_ATTRIBUTE_FORMAT(f, a, b)
+ #else
+ #     define PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(f, a, b)
+ #endif
+ #ifndef pemalloc
+ #     define pemalloc(s,p)    malloc(s)
+ #     define pefree(x,p)              free(x)
+ #     define perealloc(x,s,p) realloc(x,s)
+ #     define perealloc_recoverable perealloc
+ #     define ecalloc calloc
+ static inline void *estrndup(void *p, size_t s)
+ {
+       char *r = (char *) malloc(s+1);
+       if (r) memcpy((void *) r, p, s), r[s] = '\0';
+       return (void *) r;
+ }
+ #endif
+ #if defined(PHP_WIN32)
+ #     if defined(PHP_HTTP_BUFFER_EXPORTS)
+ #             define PHP_HTTP_BUFFER_API __declspec(dllexport)
+ #     elif defined(COMPILE_DL_PHP_HTTP_BUFFER)
+ #             define PHP_HTTP_BUFFER_API __declspec(dllimport)
+ #     else
+ #             define PHP_HTTP_BUFFER_API
+ #     endif
+ #else
+ #     define PHP_HTTP_BUFFER_API
+ #endif
+ #define PHP_HTTP_BUFFER(p) ((php_http_buffer_t *) (p))
+ #define FREE_PHP_HTTP_BUFFER_PTR(STR) pefree(STR, STR->pmem)
+ #define FREE_PHP_HTTP_BUFFER_VAL(STR) php_http_buffer_dtor(STR)
+ #define FREE_PHP_HTTP_BUFFER_ALL(STR) php_http_buffer_free(&(STR))
+ #define FREE_PHP_HTTP_BUFFER(free, STR) \
+       switch (free) \
+       { \
+       case PHP_HTTP_BUFFER_FREE_NOT: \
+               break; \
+       case PHP_HTTP_BUFFER_FREE_PTR: \
 -PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_init_ex(php_http_buffer_t *buf, size_t chunk_size, int flags);
++              pefree(STR, STR->pmem); \
+               break; \
+       case PHP_HTTP_BUFFER_FREE_VAL: \
+               php_http_buffer_dtor(STR); \
+               break; \
+       case PHP_HTTP_BUFFER_FREE_ALL: { \
+               php_http_buffer_t *PTR = (STR); \
+               php_http_buffer_free(&PTR); \
+               break; \
+       } \
+       default:\
+               break; \
+       }
+ #define RETURN_PHP_HTTP_BUFFER_PTR(STR) RETURN_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_PTR, 0)
+ #define RETURN_PHP_HTTP_BUFFER_VAL(STR) RETURN_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_NOT, 0)
+ #define RETURN_PHP_HTTP_BUFFER_DUP(STR) RETURN_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_NOT, 1)
+ #define RETVAL_PHP_HTTP_BUFFER_PTR(STR) RETVAL_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_PTR, 0)
+ #define RETVAL_PHP_HTTP_BUFFER_VAL(STR) RETVAL_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_NOT, 0)
+ #define RETVAL_PHP_HTTP_BUFFER_DUP(STR) RETVAL_PHP_HTTP_BUFFER((STR), PHP_HTTP_BUFFER_FREE_NOT, 1)
+ /* RETURN_PHP_HTTP_BUFFER(buf, PHP_HTTP_BUFFER_FREE_PTR, 0) */
+ #define RETURN_PHP_HTTP_BUFFER(STR, free, dup) \
+       RETVAL_PHP_HTTP_BUFFER((STR), (free), (dup)); \
+       return;
+ #define RETVAL_PHP_HTTP_BUFFER(STR, free, dup) \
+       php_http_buffer_fix(STR); \
+       RETVAL_STRINGL((STR)->data, (STR)->used, (dup)); \
+       FREE_PHP_HTTP_BUFFER((free), (STR));
+ typedef struct php_http_buffer {
+       char  *data;
+       size_t used;
+       size_t free;
+       size_t size;
+       unsigned pmem:1;
+       unsigned reserved:31;
+ } php_http_buffer_t;
+ typedef enum php_http_buffer_free {
+       PHP_HTTP_BUFFER_FREE_NOT = 0,
+       PHP_HTTP_BUFFER_FREE_PTR,       /* pefree() */
+       PHP_HTTP_BUFFER_FREE_VAL,       /* php_http_buffer_dtor() */
+       PHP_HTTP_BUFFER_FREE_ALL        /* php_http_buffer_free() */
+ } php_http_buffer_free_t;
+ #define PHP_HTTP_BUFFER_ALL_FREE(STR) PHP_HTTP_BUFFER_FREE_ALL,(STR)
+ #define PHP_HTTP_BUFFER_PTR_FREE(STR) PHP_HTTP_BUFFER_FREE_PTR,(STR)
+ #define PHP_HTTP_BUFFER_VAL_FREE(STR) PHP_HTTP_BUFFER_FREE_VAL,(STR)
+ #define PHP_HTTP_BUFFER_NOT_FREE(STR) PHP_HTTP_BUFFER_FREE_NOT,(STR)
+ #define PHP_HTTP_BUFFER_INIT_PREALLOC 0x01
+ #define PHP_HTTP_BUFFER_INIT_PERSISTENT       0x02
+ /* create a new php_http_buffer_t */
+ #define php_http_buffer_new() php_http_buffer_init(NULL)
+ #define php_http_buffer_init(b) php_http_buffer_init_ex(b, PHP_HTTP_BUFFER_DEFAULT_SIZE, 0)
+ #define php_http_buffer_clone(from, to) php_http_buffer_init_ex((to), (from)->size, (from)->pmem ? PHP_HTTP_BUFFER_INIT_PERSISTENT:0)
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_resize_ex(php_http_buffer_t *buf, size_t len, size_t override_size, int allow_error);
++PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_init_ex(php_http_buffer_t *buf, size_t chunk_size, unsigned flags);
+ /* create a php_http_buffer_t from a zval or c-string */
+ #define php_http_buffer_from_zval(z) php_http_buffer_from_string(Z_STRVAL(z), Z_STRLEN(z))
+ #define php_http_buffer_from_zval_ex(b, z) php_http_buffer_from_string_ex(b, Z_STRVAL(z), Z_STRLEN(z))
+ #define php_http_buffer_from_string(s, l) php_http_buffer_from_string_ex(NULL, (s), (l))
+ PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_from_string_ex(php_http_buffer_t *buf, const char *string, size_t length);
+ /* usually only called from within the internal functions */
+ #define php_http_buffer_resize(b, s) php_http_buffer_resize_ex((b), (s), 0, 0)
 -typedef size_t (*php_http_buffer_pass_func_t)(void *opaque, char *, size_t TSRMLS_DC);
++PHP_HTTP_BUFFER_API size_t php_http_buffer_resize_ex(php_http_buffer_t *buf, size_t len, size_t override_size, zend_bool allow_error);
+ PHP_HTTP_BUFFER_API char *php_http_buffer_account(php_http_buffer_t *buf, size_t to_account);
+ /* shrink memory chunk to actually used size (+1) */
+ PHP_HTTP_BUFFER_API size_t php_http_buffer_shrink(php_http_buffer_t *buf);
+ /* append data to the php_http_buffer_t */
+ #define php_http_buffer_appends(b, a) php_http_buffer_append((b), (a), sizeof(a)-1)
+ #define php_http_buffer_appendl(b, a) php_http_buffer_append((b), (a), strlen(a))
++#define php_http_buffer_appendz(b, z) php_http_buffer_append((b), (z)->val, (z)->len)
+ PHP_HTTP_BUFFER_API size_t php_http_buffer_append(php_http_buffer_t *buf, const char *append, size_t append_len);
+ PHP_HTTP_BUFFER_API size_t php_http_buffer_appendf(php_http_buffer_t *buf, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 2, 3);
+ /* get a zero-terminated string */
+ PHP_HTTP_BUFFER_API char *php_http_buffer_data(const php_http_buffer_t *buf, char **into, size_t *len);
+ /* remove a substring */
+ PHP_HTTP_BUFFER_API size_t php_http_buffer_cut(php_http_buffer_t *buf, size_t offset, size_t length);
+ /* sets a trailing NUL byte */
+ PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_fix(php_http_buffer_t *buf);
+ /* reset php_http_buffer_t object */
+ PHP_HTTP_BUFFER_API void php_http_buffer_reset(php_http_buffer_t *buf);
+ /* free a php_http_buffer_t objects contents */
+ PHP_HTTP_BUFFER_API void php_http_buffer_dtor(php_http_buffer_t *buf);
+ /* free a php_http_buffer_t object completely */
+ PHP_HTTP_BUFFER_API void php_http_buffer_free(php_http_buffer_t **buf);
+ /* stores data in a php_http_buffer_t until it reaches chunk_size */
+ PHP_HTTP_BUFFER_API size_t php_http_buffer_chunk_buffer(php_http_buffer_t **s, const char *data, size_t data_len, char **chunk, size_t chunk_size);
 -PHP_HTTP_BUFFER_API ssize_t php_http_buffer_passthru(php_http_buffer_t **s, size_t chunk_size, php_http_buffer_pass_func_t passin, void *passin_arg, php_http_buffer_pass_func_t passon, void *passon_arg TSRMLS_DC);
++typedef size_t (*php_http_buffer_pass_func_t)(void *opaque, char *, size_t);
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_chunked_output(php_http_buffer_t **s, const char *data, size_t data_len, size_t chunk_size, php_http_buffer_pass_func_t passout, void *opaque TSRMLS_DC);
++PHP_HTTP_BUFFER_API ssize_t php_http_buffer_passthru(php_http_buffer_t **s, size_t chunk_size, php_http_buffer_pass_func_t passin, void *passin_arg, php_http_buffer_pass_func_t passon, void *passon_arg);
+ /* wrapper around php_http_buffer_chunk_buffer, which passes available chunks to passthru() */
 -PHP_HTTP_BUFFER_API size_t php_http_buffer_chunked_input(php_http_buffer_t **s, size_t chunk_size, php_http_buffer_pass_func_t passin, void *opaque TSRMLS_DC);
++PHP_HTTP_BUFFER_API size_t php_http_buffer_chunked_output(php_http_buffer_t **s, const char *data, size_t data_len, size_t chunk_size, php_http_buffer_pass_func_t passout, void *opaque);
+ /* write chunks directly into php_http_buffer_t buffer */
++PHP_HTTP_BUFFER_API size_t php_http_buffer_chunked_input(php_http_buffer_t **s, size_t chunk_size, php_http_buffer_pass_func_t passin, void *opaque);
+ #     ifdef PHP_HTTP_BUFFER_EXTENDED
+ /* memcmp for php_http_buffer_t objects */
+ PHP_HTTP_BUFFER_API int php_http_buffer_cmp(php_http_buffer_t *left, php_http_buffer_t *right);
+ /* get a complete php_http_buffer_t duplicate */
+ PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_copy(const php_http_buffer_t *from, php_http_buffer_t *to);
+ /* merge several php_http_buffer_t objects
+    use like:
+       php_http_buffer_t *final = php_http_buffer_merge(3,
+               PHP_HTTP_BUFFER_NOT_FREE(&keep),
+               PHP_HTTP_BUFFER_ALL_FREE(middle_ptr),
+               PHP_HTTP_BUFFER_VAL_FREE(&local);
+ */
+ PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge(unsigned argc, ...);
+ PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_ex(php_http_buffer_t *buf, unsigned argc, ...);
+ PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_merge_va(php_http_buffer_t *buf, unsigned argc, va_list argv);
+ /* insert data at a specific position of the php_http_buffer_t */
+ #define php_http_buffer_inserts(b, i, o) php_http_buffer_insert((b), (i), sizeof(i)-1, (o))
+ #define php_http_buffer_insertl(b, i, o) php_http_buffer_insert((b), (i), strlen(i), (o))
+ PHP_HTTP_BUFFER_API size_t php_http_buffer_insert(php_http_buffer_t *buf, const char *insert, size_t insert_len, size_t offset);
+ PHP_HTTP_BUFFER_API size_t php_http_buffer_insertf(php_http_buffer_t *buf, size_t offset, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 3, 4);
+ /* prepend data */
+ #define php_http_buffer_prepends(b, p) php_http_buffer_prepend((b), (p), sizeof(p)-1)
+ #define php_http_buffer_prependl(b, p) php_http_buffer_prepend((b), (p), strlen(p))
+ PHP_HTTP_BUFFER_API size_t php_http_buffer_prepend(php_http_buffer_t *buf, const char *prepend, size_t prepend_len);
+ PHP_HTTP_BUFFER_API size_t php_http_buffer_prependf(php_http_buffer_t *buf, const char *format, ...) PHP_HTTP_BUFFER_ATTRIBUTE_FORMAT(printf, 2, 3);
+ /* get a part of the php_http_buffer_t */
+ #define php_http_buffer_mid(b, o, l) php_http_buffer_sub((b), (o), (l))
+ #define php_http_buffer_left(b, l) php_http_buffer_sub((b), 0, (l))
+ PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_right(const php_http_buffer_t *buf, size_t length);
+ PHP_HTTP_BUFFER_API php_http_buffer_t *php_http_buffer_sub(const php_http_buffer_t *buf, size_t offset, size_t len);
+ #     endif /* PHP_HTTP_BUFFER_EXTENDED */
+ #endif /* PHP_HTTP_BUFFER_H */
+ /*
+  * Local variables:
+  * tab-width: 4
+  * c-basic-offset: 4
+  * End:
+  * vim600: sw=4 ts=4 fdm=marker
+  * vim<600: sw=4 ts=4
+  */
index 0000000,160e8bb..5d78803
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,1284 +1,1252 @@@
 -      return zend_hash_add(&php_http_client_drivers, driver->name_str, driver->name_len + 1, (void *) driver, sizeof(php_http_client_driver_t), NULL);
+ /*
+     +--------------------------------------------------------------------+
+     | PECL :: http                                                       |
+     +--------------------------------------------------------------------+
+     | Redistribution and use in source and binary forms, with or without |
+     | modification, are permitted provided that the conditions mentioned |
+     | in the accompanying LICENSE file are met.                          |
+     +--------------------------------------------------------------------+
+     | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
+     +--------------------------------------------------------------------+
+ */
+ #include "php_http_api.h"
+ #include "php_http_client.h"
+ #include <ext/spl/spl_observer.h>
+ /*
+  * array of name => php_http_client_driver_t*
+  */
+ static HashTable php_http_client_drivers;
++static void php_http_client_driver_hash_dtor(zval *pData)
++{
++      pefree(Z_PTR_P(pData), 1);
++}
++
+ ZEND_RESULT_CODE php_http_client_driver_add(php_http_client_driver_t *driver)
+ {
 -ZEND_RESULT_CODE php_http_client_driver_get(const char *name_str, size_t name_len, php_http_client_driver_t *driver)
++      return zend_hash_add_mem(&php_http_client_drivers, driver->driver_name, (void *) driver, sizeof(php_http_client_driver_t))
++                      ? SUCCESS : FAILURE;
+ }
 -      if ((name_str && SUCCESS == zend_hash_find(&php_http_client_drivers, name_str, name_len + 1, (void *) &tmp))
 -      ||      (SUCCESS == zend_hash_get_current_data(&php_http_client_drivers, (void *) &tmp))) {
 -              *driver = *tmp;
 -              return SUCCESS;
++php_http_client_driver_t *php_http_client_driver_get(zend_string *name)
+ {
++      zval *ztmp;
+       php_http_client_driver_t *tmp;
 -      return FAILURE;
++      if (name && (tmp = zend_hash_find_ptr(&php_http_client_drivers, name))) {
++              return tmp;
+       }
 -static int apply_driver_list(void *p, void *arg TSRMLS_DC)
++      if ((ztmp = zend_hash_get_current_data(&php_http_client_drivers))) {
++              return Z_PTR_P(ztmp);
++      }
++      return NULL;
+ }
 -      php_http_client_driver_t *d = p;
 -      zval *zname;
++static int apply_driver_list(zval *p, void *arg)
+ {
 -      MAKE_STD_ZVAL(zname);
 -      ZVAL_STRINGL(zname, d->name_str, d->name_len, 1);
++      php_http_client_driver_t *d = Z_PTR_P(p);
++      zval zname;
 -      zend_hash_next_index_insert(arg, &zname, sizeof(zval *), NULL);
++      ZVAL_STR_COPY(&zname, d->driver_name);
 -void php_http_client_driver_list(HashTable *ht TSRMLS_DC)
++      zend_hash_next_index_insert(arg, &zname);
+       return ZEND_HASH_APPLY_KEEP;
+ }
 -      zend_hash_apply_with_argument(&php_http_client_drivers, apply_driver_list, ht TSRMLS_CC);
++void php_http_client_driver_list(HashTable *ht)
+ {
 -void php_http_client_options_set_subr(zval *this_ptr, char *key, size_t len, zval *opts, int overwrite TSRMLS_DC)
++      zend_hash_apply_with_argument(&php_http_client_drivers, apply_driver_list, ht);
+ }
 -              zend_class_entry *this_ce = Z_OBJCE_P(getThis());
 -              zval *old_opts, *new_opts, **entry = NULL;
++void php_http_client_options_set_subr(zval *instance, char *key, size_t len, zval *opts, int overwrite)
+ {
+       if (overwrite || (opts && zend_hash_num_elements(Z_ARRVAL_P(opts)))) {
 -              MAKE_STD_ZVAL(new_opts);
 -              array_init(new_opts);
 -              old_opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
++              zend_class_entry *this_ce = Z_OBJCE_P(instance);
++              zval old_opts_tmp, *old_opts, new_opts, *entry = NULL;
 -                      array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
++              array_init(&new_opts);
++              old_opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &old_opts_tmp);
+               if (Z_TYPE_P(old_opts) == IS_ARRAY) {
 -                              zend_symtable_update(Z_ARRVAL_P(new_opts), key, len, (void *) &opts, sizeof(zval *), NULL);
++                      array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL(new_opts));
+               }
+               if (overwrite) {
+                       if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
+                               Z_ADDREF_P(opts);
 -                              zend_symtable_del(Z_ARRVAL_P(new_opts), key, len);
++                              zend_symtable_str_update(Z_ARRVAL(new_opts), key, len, opts);
+                       } else {
 -                      if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(new_opts), key, len, (void *) &entry)) {
 -                              array_join(Z_ARRVAL_P(opts), Z_ARRVAL_PP(entry), 0, 0);
++                              zend_symtable_str_del(Z_ARRVAL(new_opts), key, len);
+                       }
+               } else if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
 -                              zend_symtable_update(Z_ARRVAL_P(new_opts), key, len, (void *) &opts, sizeof(zval *), NULL);
++                      if ((entry = zend_symtable_str_find(Z_ARRVAL(new_opts), key, len))) {
++                              array_join(Z_ARRVAL_P(opts), Z_ARRVAL_P(entry), 0, 0);
+                       } else {
+                               Z_ADDREF_P(opts);
 -              zend_update_property(this_ce, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC);
++                              zend_symtable_str_update(Z_ARRVAL(new_opts), key, len, opts);
+                       }
+               }
 -void php_http_client_options_set(zval *this_ptr, zval *opts TSRMLS_DC)
++              zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts);
+               zval_ptr_dtor(&new_opts);
+       }
+ }
 -      php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 -      HashPosition pos;
 -      zval *new_opts;
 -      zend_class_entry *this_ce = Z_OBJCE_P(getThis());
 -      zend_bool is_client = instanceof_function(this_ce, php_http_client_class_entry TSRMLS_CC);
++void php_http_client_options_set(zval *instance, zval *opts)
+ {
 -      MAKE_STD_ZVAL(new_opts);
 -      array_init(new_opts);
++      php_http_arrkey_t key;
++      zval new_opts;
++      zend_class_entry *this_ce = Z_OBJCE_P(instance);
++      zend_bool is_client = instanceof_function(this_ce, php_http_client_class_entry);
 -              zend_update_property(this_ce, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC);
++      array_init(&new_opts);
+       if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
 -              zval *old_opts, *add_opts, **opt;
++              zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts);
+               zval_ptr_dtor(&new_opts);
+       } else {
 -              MAKE_STD_ZVAL(add_opts);
 -              array_init(add_opts);
++              zval old_opts_tmp, *old_opts, add_opts, *opt;
 -              FOREACH_KEYVAL(pos, opts, key, opt) {
 -                      if (key.type == HASH_KEY_IS_STRING) {
 -#define KEYMATCH(k, s) ((sizeof(s)==k.len) && !strcasecmp(k.str, s))
 -                              if (Z_TYPE_PP(opt) == IS_ARRAY && (KEYMATCH(key, "ssl") || KEYMATCH(key, "cookies"))) {
 -                                      php_http_client_options_set_subr(getThis(), key.str, key.len, *opt, 0 TSRMLS_CC);
 -                              } else if (is_client && (KEYMATCH(key, "recordHistory") || KEYMATCH(key, "responseMessageClass"))) {
 -                                      zend_update_property(this_ce, getThis(), key.str, key.len-1, *opt TSRMLS_CC);
 -                              } else if (Z_TYPE_PP(opt) == IS_NULL) {
 -                                      old_opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
++              array_init(&add_opts);
+               /* some options need extra attention -- thus cannot use array_merge() directly */
 -                                              zend_symtable_del(Z_ARRVAL_P(old_opts), key.str, key.len);
++              ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(opts), key.h, key.key, opt)
++              {
++                      if (key.key) {
++                              if (Z_TYPE_P(opt) == IS_ARRAY && (zend_string_equals_literal(key.key, "ssl") || zend_string_equals_literal(key.key, "cookies"))) {
++                                      php_http_client_options_set_subr(instance, key.key->val, key.key->len, opt, 0);
++                              } else if (is_client && (zend_string_equals_literal(key.key, "recordHistory") || zend_string_equals_literal(key.key, "responseMessageClass"))) {
++                                      zend_update_property(this_ce, instance, key.key->val, key.key->len, opt);
++                              } else if (Z_TYPE_P(opt) == IS_NULL) {
++                                      old_opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &old_opts_tmp);
+                                       if (Z_TYPE_P(old_opts) == IS_ARRAY) {
 -                                      Z_ADDREF_P(*opt);
 -                                      add_assoc_zval_ex(add_opts, key.str, key.len, *opt);
++                                              zend_symtable_del(Z_ARRVAL_P(old_opts), key.key);
+                                       }
+                               } else {
 -              old_opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
++                                      Z_TRY_ADDREF_P(opt);
++                                      add_assoc_zval_ex(&add_opts, key.key->val, key.key->len, opt);
+                               }
+                       }
+               }
++              ZEND_HASH_FOREACH_END();
 -                      array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
++              old_opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &old_opts_tmp);
+               if (Z_TYPE_P(old_opts) == IS_ARRAY) {
 -              array_join(Z_ARRVAL_P(add_opts), Z_ARRVAL_P(new_opts), 0, 0);
 -              zend_update_property(this_ce, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC);
++                      array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL(new_opts));
+               }
 -void php_http_client_options_get_subr(zval *this_ptr, char *key, size_t len, zval *return_value TSRMLS_DC)
++              array_join(Z_ARRVAL(add_opts), Z_ARRVAL(new_opts), 0, 0);
++              zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts);
+               zval_ptr_dtor(&new_opts);
+               zval_ptr_dtor(&add_opts);
+       }
+ }
 -      zend_class_entry *this_ce = Z_OBJCE_P(getThis());
 -      zval **options, *opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
++void php_http_client_options_get_subr(zval *instance, char *key, size_t len, zval *return_value)
+ {
 -      if ((Z_TYPE_P(opts) == IS_ARRAY) && (SUCCESS == zend_symtable_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
 -              RETVAL_ZVAL(*options, 1, 0);
++      zend_class_entry *this_ce = Z_OBJCE_P(instance);
++      zval *options, opts_tmp, *opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &opts_tmp);
 -php_http_client_t *php_http_client_init(php_http_client_t *h, php_http_client_ops_t *ops, php_resource_factory_t *rf, void *init_arg TSRMLS_DC)
++      if ((Z_TYPE_P(opts) == IS_ARRAY) && (options = zend_symtable_str_find(Z_ARRVAL_P(opts), key, len))) {
++              RETVAL_ZVAL(options, 1, 0);
+       }
+ }
+ static void queue_dtor(void *enqueued)
+ {
+       php_http_client_enqueue_t *e = enqueued;
+       if (e->dtor) {
+               e->dtor(e);
+       }
+ }
 -      TSRMLS_SET_CTX(h->ts);
++php_http_client_t *php_http_client_init(php_http_client_t *h, php_http_client_ops_t *ops, php_resource_factory_t *rf, void *init_arg)
+ {
+       php_http_client_t *free_h = NULL;
+       if (!h) {
+               free_h = h = emalloc(sizeof(*h));
+       }
+       memset(h, 0, sizeof(*h));
+       h->ops = ops;
+       if (rf) {
+               h->rf = rf;
+       } else if (ops->rsrc) {
+               h->rf = php_resource_factory_init(NULL, h->ops->rsrc, h, NULL);
+       }
+       zend_llist_init(&h->requests, sizeof(php_http_client_enqueue_t), queue_dtor, 0);
+       zend_llist_init(&h->responses, sizeof(void *), NULL, 0);
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize client");
 -                      if (free_h) {
 -                              efree(free_h);
 -                      }
+       if (h->ops->init) {
+               if (!(h = h->ops->init(h, init_arg))) {
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
 -
++                      php_error_docref(NULL, E_WARNING, "Could not initialize client");
++                      PTR_FREE(free_h);
+               }
+       }
+       return h;
+ }
+ php_http_client_t *php_http_client_copy(php_http_client_t *from, php_http_client_t *to)
+ {
+       if (from->ops->copy) {
+               return from->ops->copy(from, to);
+       }
+       return NULL;
+ }
+ void php_http_client_dtor(php_http_client_t *h)
+ {
+       php_http_client_reset(h);
+       if (h->ops->dtor) {
+               h->ops->dtor(h);
+       }
+       php_resource_factory_free(&h->rf);
+ }
+ void php_http_client_free(php_http_client_t **h) {
+       if (*h) {
+               php_http_client_dtor(*h);
+               efree(*h);
+               *h = NULL;
+       }
+ }
+ ZEND_RESULT_CODE php_http_client_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
+ {
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to enqueue request; request already in queue");
+       if (h->ops->enqueue) {
+               if (php_http_client_enqueued(h, enqueue->request, NULL)) {
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
 -
++                      php_error_docref(NULL, E_WARNING, "Failed to enqueue request; request already in queue");
+                       return FAILURE;
+               }
+               return h->ops->enqueue(h, enqueue);
+       }
+       return FAILURE;
+ }
+ ZEND_RESULT_CODE php_http_client_dequeue(php_http_client_t *h, php_http_message_t *request)
+ {
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to dequeue request; request not in queue");
+       if (h->ops->dequeue) {
+               php_http_client_enqueue_t *enqueue = php_http_client_enqueued(h, request, NULL);
+               if (!enqueue) {
 -void php_http_client_object_free(void *object TSRMLS_DC)
++                      php_error_docref(NULL, E_WARNING, "Failed to dequeue request; request not in queue");
+                       return FAILURE;
+               }
+               return h->ops->dequeue(h, enqueue);
+       }
+       return FAILURE;
+ }
+ php_http_client_enqueue_t *php_http_client_enqueued(php_http_client_t *h, void *compare_arg, php_http_client_enqueue_cmp_func_t compare_func)
+ {
+       zend_llist_element *el = NULL;
+       if (compare_func) {
+               for (el = h->requests.head; el; el = el->next) {
+                       if (compare_func((php_http_client_enqueue_t *) el->data, compare_arg)) {
+                               break;
+                       }
+               }
+       } else {
+               for (el = h->requests.head; el; el = el->next) {
+                       if (((php_http_client_enqueue_t *) el->data)->request == compare_arg) {
+                               break;
+                       }
+               }
+       }
+       return el ? (php_http_client_enqueue_t *) el->data : NULL;
+ }
+ ZEND_RESULT_CODE php_http_client_wait(php_http_client_t *h, struct timeval *custom_timeout)
+ {
+       if (h->ops->wait) {
+               return h->ops->wait(h, custom_timeout);
+       }
+       return FAILURE;
+ }
+ int php_http_client_once(php_http_client_t *h)
+ {
+       if (h->ops->once) {
+               return h->ops->once(h);
+       }
+       return FAILURE;
+ }
+ ZEND_RESULT_CODE php_http_client_exec(php_http_client_t *h)
+ {
+       if (h->ops->exec) {
+               return h->ops->exec(h);
+       }
+       return FAILURE;
+ }
+ void php_http_client_reset(php_http_client_t *h)
+ {
+       if (h->ops->reset) {
+               h->ops->reset(h);
+       }
+       zend_llist_clean(&h->requests);
+       zend_llist_clean(&h->responses);
+ }
+ ZEND_RESULT_CODE php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
+ {
+       if (h->ops->setopt) {
+               return h->ops->setopt(h, opt, arg);
+       }
+       return FAILURE;
+ }
+ ZEND_RESULT_CODE php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void *res_ptr)
+ {
+       if (h->ops->getopt) {
+               return h->ops->getopt(h, opt, arg, res_ptr);
+       }
+       return FAILURE;
+ }
+ zend_class_entry *php_http_client_class_entry;
+ static zend_object_handlers php_http_client_object_handlers;
 -      php_http_client_object_t *o = (php_http_client_object_t *) object;
++void php_http_client_object_free(zend_object *object)
+ {
 -      zend_object_std_dtor((zend_object *) o TSRMLS_CC);
 -      efree(o);
++      php_http_client_object_t *o = PHP_HTTP_OBJ(object, NULL);
+       php_http_client_free(&o->client);
+       php_http_object_method_dtor(&o->notify);
+       php_http_object_method_free(&o->update);
 -zend_object_value php_http_client_object_new_ex(zend_class_entry *ce, php_http_client_t *client, php_http_client_object_t **ptr TSRMLS_DC)
++      zend_object_std_dtor(object);
+ }
 -      o = ecalloc(1, sizeof(php_http_client_object_t));
 -      zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
 -      object_properties_init((zend_object *) o, ce);
++php_http_client_object_t *php_http_client_object_new_ex(zend_class_entry *ce, php_http_client_t *client)
+ {
+       php_http_client_object_t *o;
 -      if (ptr) {
 -              *ptr = o;
 -      }
++      o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
++      zend_object_std_init(&o->zo, ce);
++      object_properties_init(&o->zo, ce);
+       o->client = client;
 -      o->zv.handle = zend_objects_store_put(o, NULL, php_http_client_object_free, NULL TSRMLS_CC);
 -      o->zv.handlers = &php_http_client_object_handlers;
 -
 -      return o->zv;
++      o->zo.handlers = &php_http_client_object_handlers;
 -zend_object_value php_http_client_object_new(zend_class_entry *ce TSRMLS_DC)
++      return o;
+ }
 -      return php_http_client_object_new_ex(ce, NULL, NULL TSRMLS_CC);
++zend_object *php_http_client_object_new(zend_class_entry *ce)
+ {
 -static void handle_history(zval *zclient, php_http_message_t *request, php_http_message_t *response TSRMLS_DC)
++      return &php_http_client_object_new_ex(ce, NULL)->zo;
+ }
 -      zval *new_hist, *old_hist = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), 0 TSRMLS_CC);
 -      php_http_message_t *zipped = php_http_message_zip(response, request);
 -      zend_object_value ov = php_http_message_object_new_ex(php_http_message_class_entry, zipped, NULL TSRMLS_CC);
++static void handle_history(zval *zclient, php_http_message_t *request, php_http_message_t *response)
+ {
 -      MAKE_STD_ZVAL(new_hist);
 -      ZVAL_OBJVAL(new_hist, ov, 0);
++      zval new_hist, old_hist_tmp, *old_hist = zend_read_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), 0, &old_hist_tmp);
++      php_http_message_t *req_copy = php_http_message_copy(request, NULL);
++      php_http_message_t *res_copy = php_http_message_copy(response, NULL);
++      php_http_message_t *zipped = php_http_message_zip(res_copy, req_copy);
++      php_http_message_object_t *obj = php_http_message_object_new_ex(php_http_message_class_entry, zipped);
 -              php_http_message_object_prepend(new_hist, old_hist, 1 TSRMLS_CC);
++      ZVAL_OBJ(&new_hist, &obj->zo);
+       if (Z_TYPE_P(old_hist) == IS_OBJECT) {
 -      zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), new_hist TSRMLS_CC);
++              php_http_message_object_prepend(&new_hist, old_hist, 1);
+       }
 -      TSRMLS_FETCH_FROM_CTX(client->ts);
++      zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), &new_hist);
+       zval_ptr_dtor(&new_hist);
+ }
+ static ZEND_RESULT_CODE handle_response(void *arg, php_http_client_t *client, php_http_client_enqueue_t *e, php_http_message_t **response)
+ {
+       zend_bool dequeue = 0;
+       zval zclient;
+       php_http_message_t *msg;
+       php_http_client_progress_state_t *progress;
 -      INIT_PZVAL(&zclient);
 -      ZVAL_OBJVAL(&zclient, ((php_http_client_object_t*) arg)->zv, 0);
 -              zval *info, *zresponse, *zrequest;
++      ZVAL_OBJ(&zclient, &((php_http_client_object_t*) arg)->zo);
+       if ((msg = *response)) {
+               php_http_message_object_t *msg_obj;
 -              if (z_is_true(zend_read_property(php_http_client_class_entry, &zclient, ZEND_STRL("recordHistory"), 0 TSRMLS_CC))) {
 -                      handle_history(&zclient, e->request, *response TSRMLS_CC);
++              zval info, zresponse, zrequest, rec_hist_tmp;
+               HashTable *info_ht;
+               /* ensure the message is of type response (could be uninitialized in case of early error, like DNS) */
+               php_http_message_set_type(msg, PHP_HTTP_RESPONSE);
 -              MAKE_STD_ZVAL(zresponse);
 -              ZVAL_OBJVAL(zresponse, php_http_message_object_new_ex(php_http_client_response_class_entry, msg, &msg_obj TSRMLS_CC), 0);
 -
 -              MAKE_STD_ZVAL(zrequest);
 -              ZVAL_OBJVAL(zrequest, ((php_http_message_object_t *) e->opaque)->zv, 1);
++              if (zend_is_true(zend_read_property(php_http_client_class_entry, &zclient, ZEND_STRL("recordHistory"), 0, &rec_hist_tmp))) {
++                      handle_history(&zclient, e->request, *response);
+               }
+               /* hard detach, redirects etc. are in the history */
+               php_http_message_free(&msg->parent);
+               *response = NULL;
 -              php_http_message_object_prepend(zresponse, zrequest, 1 TSRMLS_CC);
++              msg_obj = php_http_message_object_new_ex(php_http_client_response_class_entry, msg);
++              ZVAL_OBJ(&zresponse, &msg_obj->zo);
++              ZVAL_OBJECT(&zrequest, &((php_http_message_object_t *) e->opaque)->zo, 1);
 -              MAKE_STD_ZVAL(info);
 -              object_init(info);
 -              info_ht = HASH_OF(info);
++              php_http_message_object_prepend(&zresponse, &zrequest, 1);
 -              zend_update_property(php_http_client_response_class_entry, zresponse, ZEND_STRL("transferInfo"), info TSRMLS_CC);
++              object_init(&info);
++              info_ht = HASH_OF(&info);
+               php_http_client_getopt(client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, e->request, &info_ht);
 -              zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
++              zend_update_property(php_http_client_response_class_entry, &zresponse, ZEND_STRL("transferInfo"), &info);
+               zval_ptr_dtor(&info);
 -                      zval *retval = NULL;
++              Z_ADDREF(zresponse);
+               zend_llist_add_element(&client->responses, &msg_obj);
+               if (e->closure.fci.size) {
 -                      zend_fcall_info_argn(&e->closure.fci TSRMLS_CC, 1, &zresponse);
 -                      zend_replace_error_handling(EH_NORMAL, NULL, &zeh TSRMLS_CC);
 -                      zend_fcall_info_call(&e->closure.fci, &e->closure.fcc, &retval, NULL TSRMLS_CC);
 -                      zend_restore_error_handling(&zeh TSRMLS_CC);
 -                      zend_fcall_info_argn(&e->closure.fci TSRMLS_CC, 0);
++                      zval retval;
+                       zend_error_handling zeh;
 -                      if (retval) {
 -                              if (Z_TYPE_P(retval) == IS_BOOL) {
 -                                      dequeue = Z_BVAL_P(retval);
 -                              }
 -                              zval_ptr_dtor(&retval);
++                      ZVAL_UNDEF(&retval);
++                      zend_fcall_info_argn(&e->closure.fci, 1, &zresponse);
++                      zend_replace_error_handling(EH_NORMAL, NULL, &zeh);
++                      zend_fcall_info_call(&e->closure.fci, &e->closure.fcc, &retval, NULL);
++                      zend_restore_error_handling(&zeh);
++                      zend_fcall_info_argn(&e->closure.fci, 0);
 -      zval *zrequest, *zprogress, *zclient, **args[2];
++                      if (Z_TYPE(retval) == IS_TRUE) {
++                              dequeue = 1;
+                       }
++                      zval_ptr_dtor(&retval);
+               }
+               zval_ptr_dtor(&zresponse);
+               zval_ptr_dtor(&zrequest);
+       }
+       if (SUCCESS == php_http_client_getopt(client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, e->request, &progress)) {
+               progress->info = "finished";
+               progress->finished = 1;
+               client->callback.progress.func(client->callback.progress.arg, client, e, progress);
+       }
+       if (dequeue) {
+               php_http_client_dequeue(client, e->request);
+       }
+       return SUCCESS;
+ }
+ static void handle_progress(void *arg, php_http_client_t *client, php_http_client_enqueue_t *e, php_http_client_progress_state_t *progress)
+ {
 -      TSRMLS_FETCH_FROM_CTX(client->ts);
 -
 -      MAKE_STD_ZVAL(zclient);
 -      ZVAL_OBJVAL(zclient, client_obj->zv, 1);
 -
 -      MAKE_STD_ZVAL(zrequest);
 -      ZVAL_OBJVAL(zrequest, ((php_http_message_object_t *) e->opaque)->zv, 1);
 -      args[0] = &zrequest;
 -
 -      MAKE_STD_ZVAL(zprogress);
 -      object_init(zprogress);
 -      add_property_bool(zprogress, "started", progress->started);
 -      add_property_bool(zprogress, "finished", progress->finished);
 -      add_property_string(zprogress, "info", STR_PTR(progress->info), 1);
 -      add_property_double(zprogress, "dltotal", progress->dl.total);
 -      add_property_double(zprogress, "dlnow", progress->dl.now);
 -      add_property_double(zprogress, "ultotal", progress->ul.total);
 -      add_property_double(zprogress, "ulnow", progress->ul.now);
 -      args[1] = &zprogress;
 -
 -      zend_replace_error_handling(EH_NORMAL, NULL, &zeh TSRMLS_CC);
 -      php_http_object_method_call(&client_obj->notify, zclient, NULL, 2, args TSRMLS_CC);
 -      zend_restore_error_handling(&zeh TSRMLS_CC);
++      zval zclient, args[2];
+       php_http_client_object_t *client_obj = arg;
+       zend_error_handling zeh;
 -      zval_ptr_dtor(&zrequest);
 -      zval_ptr_dtor(&zprogress);
++
++      ZVAL_OBJECT(&zclient, &client_obj->zo, 1);
++      ZVAL_OBJECT(&args[0], &((php_http_message_object_t *) e->opaque)->zo, 1);
++      object_init(&args[1]);
++      add_property_bool(&args[1], "started", progress->started);
++      add_property_bool(&args[1], "finished", progress->finished);
++      add_property_string(&args[1], "info", STR_PTR(progress->info));
++      add_property_double(&args[1], "dltotal", progress->dl.total);
++      add_property_double(&args[1], "dlnow", progress->dl.now);
++      add_property_double(&args[1], "ultotal", progress->ul.total);
++      add_property_double(&args[1], "ulnow", progress->ul.now);
++
++      zend_replace_error_handling(EH_NORMAL, NULL, &zeh);
++      php_http_object_method_call(&client_obj->notify, &zclient, NULL, 2, args);
++      zend_restore_error_handling(&zeh);
+       zval_ptr_dtor(&zclient);
 -      TSRMLS_FETCH_FROM_CTX(msg_obj->message->ts);
++      zval_ptr_dtor(&args[0]);
++      zval_ptr_dtor(&args[1]);
+ }
+ static void response_dtor(void *data)
+ {
+       php_http_message_object_t *msg_obj = *(php_http_message_object_t **) data;
 -      zend_objects_store_del_ref_by_handle_ex(msg_obj->zv.handle, msg_obj->zv.handlers TSRMLS_CC);
 -              char *driver_str = NULL, *persistent_handle_str = NULL;
 -              int driver_len = 0, persistent_handle_len = 0;
 -              php_http_client_driver_t driver;
++      zend_objects_store_del(&msg_obj->zo);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_construct, 0, 0, 0)
+       ZEND_ARG_INFO(0, driver)
+       ZEND_ARG_INFO(0, persistent_handle_id)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, __construct)
+ {
 -              zval *os;
++              zend_string *driver_name = NULL, *persistent_handle_name = NULL;
++              php_http_client_driver_t *driver;
+               php_resource_factory_t *rf = NULL;
+               php_http_client_object_t *obj;
 -              php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|ss", &driver_str, &driver_len, &persistent_handle_str, &persistent_handle_len), invalid_arg, return);
++              zval os;
 -              if (SUCCESS != php_http_client_driver_get(driver_str, driver_len, &driver)) {
 -                      php_http_throw(unexpected_val, "Failed to locate \"%s\" client request handler", driver_str);
++              php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|S!S!", &driver_name, &persistent_handle_name), invalid_arg, return);
 -              MAKE_STD_ZVAL(os);
 -              object_init_ex(os, spl_ce_SplObjectStorage);
 -              zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), os TSRMLS_CC);
++              if (!zend_hash_num_elements(&php_http_client_drivers)) {
++                      php_http_throw(unexpected_val, "No http\\Client drivers available", NULL);
++                      return;
++              }
++              if (!(driver = php_http_client_driver_get(driver_name))) {
++                      php_http_throw(unexpected_val, "Failed to locate \"%s\" client request handler", driver_name ? driver_name->val : "default");
+                       return;
+               }
 -              if (persistent_handle_len) {
 -                      char *name_str;
 -                      size_t name_len;
++              object_init_ex(&os, spl_ce_SplObjectStorage);
++              zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), &os);
+               zval_ptr_dtor(&os);
 -                      name_len = spprintf(&name_str, 0, "http\\Client\\%s", driver.name_str);
 -                      php_http_pretty_key(name_str + sizeof("http\\Client"), driver.name_len, 1, 1);
 -
 -                      if ((pf = php_persistent_handle_concede(NULL , name_str, name_len, persistent_handle_str, persistent_handle_len, NULL, NULL TSRMLS_CC))) {
++              if (persistent_handle_name) {
+                       php_persistent_handle_factory_t *pf;
 -
 -                      efree(name_str);
++                      if ((pf = php_persistent_handle_concede(NULL, driver->client_name, persistent_handle_name, NULL, NULL))) {
+                               rf = php_persistent_handle_resource_factory_init(NULL, pf);
+                       }
 -              obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               }
 -              php_http_expect(obj->client = php_http_client_init(NULL, driver.client_ops, rf, NULL TSRMLS_CC), runtime, return);
++              obj = PHP_HTTP_OBJ(NULL, getThis());
 -              php_http_object_method_init(&obj->notify, getThis(), ZEND_STRL("notify") TSRMLS_CC);
++              php_http_expect(obj->client = php_http_client_init(NULL, driver->client_ops, rf, NULL), runtime, return);
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++              php_http_object_method_init(&obj->notify, getThis(), ZEND_STRL("notify"));
+               obj->client->callback.response.func = handle_response;
+               obj->client->callback.response.arg = obj;
+               obj->client->callback.progress.func = handle_progress;
+               obj->client->callback.progress.arg = obj;
+               obj->client->responses.dtor = response_dtor;
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_reset, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, reset)
+ {
+       php_http_client_object_t *obj;
+       php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
 -static HashTable *combined_options(zval *client, zval *request TSRMLS_DC)
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       obj->iterator = 0;
+       php_http_client_reset(obj->client);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
 -      int num_options = 0;
 -      zval *z_roptions = NULL, *z_coptions = zend_read_property(php_http_client_class_entry, client, ZEND_STRL("options"), 0 TSRMLS_CC);
++static HashTable *combined_options(zval *client, zval *request)
+ {
+       HashTable *options;
 -      zend_call_method_with_0_params(&request, NULL, NULL, "getOptions", &z_roptions);
 -      if (z_roptions && Z_TYPE_P(z_roptions) == IS_ARRAY) {
 -              int num = zend_hash_num_elements(Z_ARRVAL_P(z_roptions));
++      unsigned num_options = 0;
++      zval z_roptions, z_options_tmp, *z_coptions = zend_read_property(php_http_client_class_entry, client, ZEND_STRL("options"), 0, &z_options_tmp);
+       if (Z_TYPE_P(z_coptions) == IS_ARRAY) {
+               num_options = zend_hash_num_elements(Z_ARRVAL_P(z_coptions));
+       }
 -      if (z_roptions) {
 -              if (Z_TYPE_P(z_roptions) == IS_ARRAY) {
 -                      array_join(Z_ARRVAL_P(z_roptions), options, 0, 0);
 -              }
 -              zval_ptr_dtor(&z_roptions);
++      ZVAL_UNDEF(&z_roptions);
++      zend_call_method_with_0_params(request, NULL, NULL, "getOptions", &z_roptions);
++      if (Z_TYPE(z_roptions) == IS_ARRAY) {
++              unsigned num = zend_hash_num_elements(Z_ARRVAL(z_roptions));
+               if (num > num_options) {
+                       num_options = num;
+               }
+       }
+       ALLOC_HASHTABLE(options);
+       ZEND_INIT_SYMTABLE_EX(options, num_options, 0);
+       if (Z_TYPE_P(z_coptions) == IS_ARRAY) {
+               array_copy(Z_ARRVAL_P(z_coptions), options);
+       }
 -      TSRMLS_FETCH_FROM_CTX(msg_obj->message->ts);
++      if (Z_TYPE(z_roptions) == IS_ARRAY) {
++              array_join(Z_ARRVAL(z_roptions), options, 0, 0);
+       }
++      zval_ptr_dtor(&z_roptions);
++
+       return options;
+ }
+ static void msg_queue_dtor(php_http_client_enqueue_t *e)
+ {
+       php_http_message_object_t *msg_obj = e->opaque;
 -      zend_objects_store_del_ref_by_handle_ex(msg_obj->zv.handle, msg_obj->zv.handlers TSRMLS_CC);
 -              if (e->closure.fci.object_ptr) {
 -                      zval_ptr_dtor(&e->closure.fci.object_ptr);
++      zend_objects_store_del(&msg_obj->zo);
+       zend_hash_destroy(e->options);
+       FREE_HASHTABLE(e->options);
+       if (e->closure.fci.size) {
+               zval_ptr_dtor(&e->closure.fci.function_name);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|f", &request, php_http_client_request_class_entry, &fci, &fcc), invalid_arg, return);
++              if (e->closure.fci.object) {
++                      zend_objects_store_del(e->closure.fci.object);
+               }
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enqueue, 0, 0, 1)
+       ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
+       ZEND_ARG_INFO(0, callable)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, enqueue)
+ {
+       zval *request;
+       zend_fcall_info fci = empty_fcall_info;
+       zend_fcall_info_cache fcc = empty_fcall_info_cache;
+       php_http_client_object_t *obj;
+       php_http_message_object_t *msg_obj;
+       php_http_client_enqueue_t q;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      msg_obj = zend_object_store_get_object(request TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O|f", &request, php_http_client_request_class_entry, &fci, &fcc), invalid_arg, return);
 -      q.options = combined_options(getThis(), request TSRMLS_CC);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
++      msg_obj = PHP_HTTP_OBJ(NULL, request);
+       if (php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
+               php_http_throw(bad_method_call, "Failed to enqueue request; request already in queue", NULL);
+               return;
+       }
+       q.request = msg_obj->message;
 -              Z_ADDREF_P(fci.function_name);
 -              if (fci.object_ptr) {
 -                      Z_ADDREF_P(fci.object_ptr);
++      q.options = combined_options(getThis(), request);
+       q.dtor = msg_queue_dtor;
+       q.opaque = msg_obj;
+       q.closure.fci = fci;
+       q.closure.fcc = fcc;
+       if (fci.size) {
 -      zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
++              Z_TRY_ADDREF(fci.function_name);
++              if (fci.object) {
++                      ++GC_REFCOUNT(fci.object);
+               }
+       }
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_class_entry), invalid_arg, return);
++      Z_ADDREF_P(request);
+       php_http_expect(SUCCESS == php_http_client_enqueue(obj->client, &q), runtime,
+                       msg_queue_dtor(&q);
+                       return;
+       );
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_dequeue, 0, 0, 1)
+       ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, dequeue)
+ {
+       zval *request;
+       php_http_client_object_t *obj;
+       php_http_message_object_t *msg_obj;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      msg_obj = zend_object_store_get_object(request TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, php_http_client_request_class_entry), invalid_arg, return);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|f", &request, php_http_client_request_class_entry, &fci, &fcc), invalid_arg, return);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
++      msg_obj = PHP_HTTP_OBJ(NULL, request);
+       if (!php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
+               php_http_throw(bad_method_call, "Failed to dequeue request; request not in queue", NULL);
+               return;
+       }
+       php_http_expect(SUCCESS == php_http_client_dequeue(obj->client, msg_obj->message), runtime, return);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_requeue, 0, 0, 1)
+       ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
+       ZEND_ARG_INFO(0, callable)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, requeue)
+ {
+       zval *request;
+       zend_fcall_info fci = empty_fcall_info;
+       zend_fcall_info_cache fcc = empty_fcall_info_cache;
+       php_http_client_object_t *obj;
+       php_http_message_object_t *msg_obj;
+       php_http_client_enqueue_t q;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      msg_obj = zend_object_store_get_object(request TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O|f", &request, php_http_client_request_class_entry, &fci, &fcc), invalid_arg, return);
 -      q.options = combined_options(getThis(), request TSRMLS_CC);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
++      msg_obj = PHP_HTTP_OBJ(NULL, request);
+       if (php_http_client_enqueued(obj->client, msg_obj->message, NULL)) {
+               php_http_expect(SUCCESS == php_http_client_dequeue(obj->client, msg_obj->message), runtime, return);
+       }
+       q.request = msg_obj->message;
 -              Z_ADDREF_P(fci.function_name);
 -              if (fci.object_ptr) {
 -                      Z_ADDREF_P(fci.object_ptr);
++      q.options = combined_options(getThis(), request);
+       q.dtor = msg_queue_dtor;
+       q.opaque = msg_obj;
+       q.closure.fci = fci;
+       q.closure.fcc = fcc;
+       if (fci.size) {
 -      zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
++              Z_TRY_ADDREF(fci.function_name);
++              if (fci.object) {
++                      ++GC_REFCOUNT(fci.object);
+               }
+       }
 -      long count_mode = -1;
++      Z_ADDREF_P(request);
+       php_http_expect(SUCCESS == php_http_client_enqueue(obj->client, &q), runtime,
+                       msg_queue_dtor(&q);
+                       return;
+       );
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_count, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, count)
+ {
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &count_mode)) {
 -              php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      zend_long count_mode = -1;
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &zrequest, php_http_client_request_class_entry), invalid_arg, return);
++      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|l", &count_mode)) {
++              php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
+               RETVAL_LONG(zend_llist_count(&obj->client->requests));
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getResponse, 0, 0, 0)
+       ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, getResponse)
+ {
+       zval *zrequest = NULL;
+       php_http_client_object_t *obj;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|O", &zrequest, php_http_client_request_class_entry), invalid_arg, return);
 -              php_http_message_object_t *req_obj = zend_object_store_get_object(zrequest TSRMLS_CC);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       if (zrequest) {
+               /* lookup the response with the request */
+               zend_llist_element *el = NULL;
 -                              RETURN_OBJVAL(response_obj->zv, 1);
++              php_http_message_object_t *req_obj = PHP_HTTP_OBJ(NULL, zrequest);
+               for (el = obj->client->responses.head; el; el = el->next) {
+                       php_http_message_object_t *response_obj = *(php_http_message_object_t **) el->data;
+                       if (response_obj->message->parent == req_obj->message) {
 -                      RETVAL_OBJVAL(response_obj->zv, 1);
++                              RETURN_OBJECT(&response_obj->zo, 1);
+                       }
+               }
+               /* not found for the request! */
+               php_http_throw(unexpected_val, "Could not find response for the request", NULL);
+               return;
+       }
+       /* pop off the last response */
+       if (obj->client->responses.tail) {
+               php_http_message_object_t *response_obj = *(php_http_message_object_t **) obj->client->responses.tail->data;
+               /* pop off and go */
+               if (response_obj) {
 -      zval *zhistory;
++                      RETVAL_OBJECT(&response_obj->zo, 1);
+                       zend_llist_remove_tail(&obj->client->responses);
+               }
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getHistory, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, getHistory)
+ {
 -      zhistory = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC);
++      zval zhistory_tmp, *zhistory;
+       php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      zhistory = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0, &zhistory_tmp);
+       RETVAL_ZVAL(zhistory, 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_send, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, send)
+ {
+       php_http_client_object_t *obj;
+       php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
 -              php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       php_http_expect(SUCCESS == php_http_client_exec(obj->client), runtime, return);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_once, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, once)
+ {
+       if (SUCCESS == zend_parse_parameters_none()) {
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|d", &timeout)) {
++              php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
+               RETURN_BOOL(0 < php_http_client_once(obj->client));
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_wait, 0, 0, 0)
+       ZEND_ARG_INFO(0, timeout)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, wait)
+ {
+       double timeout = 0;
 -              php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|d", &timeout)) {
+               struct timeval timeout_val;
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|H!", &settings), invalid_arg, return);
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++              php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
+               timeout_val.tv_sec = (time_t) timeout;
+               timeout_val.tv_usec = PHP_HTTP_USEC(timeout) % PHP_HTTP_MCROSEC;
+               RETURN_BOOL(SUCCESS == php_http_client_wait(obj->client, timeout > 0 ? &timeout_val : NULL));
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_configure, 0, 0, 1)
+       ZEND_ARG_ARRAY_INFO(0, settings, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, configure)
+ {
+       HashTable *settings = NULL;
+       php_http_client_object_t *obj;
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable), invalid_arg, return);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|H!", &settings), invalid_arg, return);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       php_http_expect(SUCCESS == php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_CONFIGURATION, settings), unexpected_val, return);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enablePipelining, 0, 0, 0)
+       ZEND_ARG_INFO(0, enable)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, enablePipelining)
+ {
+       zend_bool enable = 1;
+       php_http_client_object_t *obj;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &enable), invalid_arg, return);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &enable), invalid_arg, return);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       php_http_expect(SUCCESS == php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING, &enable), unexpected_val, return);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enableEvents, 0, 0, 0)
+       ZEND_ARG_INFO(0, enable)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, enableEvents)
+ {
+       zend_bool enable = 1;
+       php_http_client_object_t *obj;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &enable), invalid_arg, return);
 -      zval **args[3];
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       php_http_expect(SUCCESS == php_http_client_setopt(obj->client, PHP_HTTP_CLIENT_OPT_USE_EVENTS, &enable), unexpected_val, return);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ struct notify_arg {
+       php_http_object_method_t *cb;
 -static int notify(zend_object_iterator *iter, void *puser TSRMLS_DC)
++      zval args[3];
+       int argc;
+ };
 -      zval **observer = NULL;
++static int notify(zend_object_iterator *iter, void *puser)
+ {
 -      iter->funcs->get_current_data(iter, &observer TSRMLS_CC);
 -      if (observer) {
 -              return php_http_object_method_call(arg->cb, *observer, NULL, arg->argc, arg->args TSRMLS_CC);
++      zval *observer;
+       struct notify_arg *arg = puser;
 -      return FAILURE;
++      if ((observer = iter->funcs->get_current_data(iter))) {
++              if (SUCCESS == php_http_object_method_call(arg->cb, observer, NULL, arg->argc, arg->args)) {
++                      return ZEND_HASH_APPLY_KEEP;
++              }
+       }
 -      zval *request = NULL, *zprogress = NULL, *observers;
++      return ZEND_HASH_APPLY_STOP;
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_notify, 0, 0, 0)
+       ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, notify)
+ {
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O!o!", &request, php_http_client_request_class_entry, &zprogress), invalid_arg, return);
++      zval *request = NULL, *zprogress = NULL, observers_tmp, *observers;
+       php_http_client_object_t *client_obj;
+       struct notify_arg arg = {NULL};
 -      client_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|O!o!", &request, php_http_client_request_class_entry, &zprogress), invalid_arg, return);
 -
 -              Z_ADDREF_P(getThis());
 -              arg.args[0] = &getThis();
++      client_obj = PHP_HTTP_OBJ(NULL, getThis());
++      observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0, &observers_tmp);
+       if (Z_TYPE_P(observers) != IS_OBJECT) {
+               php_http_throw(unexpected_val, "Observer storage is corrupted", NULL);
+               return;
+       }
+       if (client_obj->update) {
+               arg.cb = client_obj->update;
 -                      Z_ADDREF_P(request);
 -                      arg.args[1] = &request;
++              ZVAL_COPY(&arg.args[0], getThis());
+               arg.argc = 1;
+               if (request) {
 -
++                      ZVAL_COPY(&arg.args[1], request);
+                       arg.argc += 1;
+               }
 -                      Z_ADDREF_P(zprogress);
 -                      arg.args[2] = &zprogress;
+               if (zprogress) {
 -              spl_iterator_apply(observers, notify, &arg TSRMLS_CC);
++                      ZVAL_COPY(&arg.args[2], zprogress);
+                       arg.argc += 1;
+               }
 -              zval_ptr_dtor(&getThis());
++              spl_iterator_apply(observers, notify, &arg);
 -                      zval_ptr_dtor(&request);
++              zval_ptr_dtor(getThis());
+               if (request) {
 -                      zval_ptr_dtor(&zprogress);
++                      zval_ptr_dtor(request);
+               }
+               if (zprogress) {
 -      zval *observers, *observer, *retval = NULL;
++                      zval_ptr_dtor(zprogress);
+               }
+       }
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_attach, 0, 0, 1)
+       ZEND_ARG_OBJ_INFO(0, observer, SplObserver, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, attach)
+ {
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver), invalid_arg, return);
++      zval observers_tmp, *observers, *observer, retval;
+       php_http_client_object_t *client_obj;
 -      client_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &observer, spl_ce_SplObserver), invalid_arg, return);
 -              client_obj->update = php_http_object_method_init(NULL, observer, ZEND_STRL("update") TSRMLS_CC);
++      client_obj = PHP_HTTP_OBJ(NULL, getThis());
++      observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0, &observers_tmp);
+       if (Z_TYPE_P(observers) != IS_OBJECT) {
+               php_http_throw(unexpected_val, "Observer storage is corrupted", NULL);
+               return;
+       }
+       if (!client_obj->update) {
 -      zend_call_method_with_1_params(&observers, NULL, NULL, "attach", &retval, observer);
 -      if (retval) {
 -              zval_ptr_dtor(&retval);
 -      }
++              client_obj->update = php_http_object_method_init(NULL, observer, ZEND_STRL("update"));
+       }
 -      zval *observers, *observer, *retval = NULL;
++      ZVAL_UNDEF(&retval);
++      zend_call_method_with_1_params(observers, NULL, NULL, "attach", &retval, observer);
++      zval_ptr_dtor(&retval);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_detach, 0, 0, 1)
+       ZEND_ARG_OBJ_INFO(0, observer, SplObserver, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, detach)
+ {
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver), invalid_arg, return);
++      zval observers_tmp, *observers, *observer, retval;
 -      observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &observer, spl_ce_SplObserver), invalid_arg, return);
 -      zend_call_method_with_1_params(&observers, NULL, NULL, "detach", &retval, observer);
 -      if (retval) {
 -              zval_ptr_dtor(&retval);
 -      }
++      observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0, &observers_tmp);
+       if (Z_TYPE_P(observers) != IS_OBJECT) {
+               php_http_throw(unexpected_val, "Observer storage is corrupted", NULL);
+               return;
+       }
 -      zval *observers;
++      ZVAL_UNDEF(&retval);
++      zend_call_method_with_1_params(observers, NULL, NULL, "detach", &retval, observer);
++      zval_ptr_dtor(&retval);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getObservers, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, getObservers)
+ {
 -      observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
++      zval observers_tmp, *observers;
+       php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_class_entry), invalid_arg, return);
++      observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0, &observers_tmp);
+       if (Z_TYPE_P(observers) != IS_OBJECT) {
+               php_http_throw(unexpected_val, "Observer storage is corrupted", NULL);
+               return;
+       }
+       RETVAL_ZVAL(observers, 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getProgressInfo, 0, 0, 1)
+       ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, getProgressInfo)
+ {
+       zval *request;
+       php_http_client_object_t *obj;
+       php_http_message_object_t *req_obj;
+       php_http_client_progress_state_t *progress;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      req_obj = zend_object_store_get_object(request TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, php_http_client_request_class_entry), invalid_arg, return);
 -      add_property_string(return_value, "info", STR_PTR(progress->info), 1);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
++      req_obj = PHP_HTTP_OBJ(NULL, request);
+       php_http_expect(SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, req_obj->message, &progress), unexpected_val, return);
+       object_init(return_value);
+       add_property_bool(return_value, "started", progress->started);
+       add_property_bool(return_value, "finished", progress->finished);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_class_entry), invalid_arg, return);
++      add_property_string(return_value, "info", STR_PTR(progress->info));
+       add_property_double(return_value, "dltotal", progress->dl.total);
+       add_property_double(return_value, "dlnow", progress->dl.now);
+       add_property_double(return_value, "ultotal", progress->ul.total);
+       add_property_double(return_value, "ulnow", progress->ul.now);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getTransferInfo, 0, 0, 1)
+       ZEND_ARG_OBJ_INFO(0, request, http\\Client\\Request, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, getTransferInfo)
+ {
+       zval *request;
+       HashTable *info;
+       php_http_client_object_t *obj;
+       php_http_message_object_t *req_obj;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      req_obj = zend_object_store_get_object(request TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, php_http_client_request_class_entry), invalid_arg, return);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
++      req_obj = PHP_HTTP_OBJ(NULL, request);
+       object_init(return_value);
+       info = HASH_OF(return_value);
+       php_http_expect(SUCCESS == php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, req_obj->message, &info), unexpected_val, return);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setOptions, 0, 0, 0)
+       ZEND_ARG_ARRAY_INFO(0, options, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, setOptions)
+ {
+       zval *opts = NULL;
 -      php_http_client_options_set(getThis(), opts TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
 -              zval *options = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
++      php_http_client_options_set(getThis(), opts);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getOptions, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, getOptions)
+ {
+       if (SUCCESS == zend_parse_parameters_none()) {
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
++              zval options_tmp, *options = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0, &options_tmp);
+               RETVAL_ZVAL(options, 1, 0);
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setSslOptions, 0, 0, 0)
+       ZEND_ARG_ARRAY_INFO(0, ssl_option, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, setSslOptions)
+ {
+       zval *opts = NULL;
 -      php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 1 TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
++      php_http_client_options_set_subr(getThis(), ZEND_STRL("ssl"), opts, 1);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_addSslOptions, 0, 0, 0)
+       ZEND_ARG_ARRAY_INFO(0, ssl_options, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, addSslOptions)
+ {
+       zval *opts = NULL;
 -      php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 0 TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
 -              php_http_client_options_get_subr(getThis(), ZEND_STRS("ssl"), return_value TSRMLS_CC);
++      php_http_client_options_set_subr(getThis(), ZEND_STRL("ssl"), opts, 0);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getSslOptions, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, getSslOptions)
+ {
+       if (SUCCESS == zend_parse_parameters_none()) {
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
++              php_http_client_options_get_subr(getThis(), ZEND_STRL("ssl"), return_value);
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_setCookies, 0, 0, 0)
+       ZEND_ARG_ARRAY_INFO(0, cookies, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, setCookies)
+ {
+       zval *opts = NULL;
 -      php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 1 TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
++      php_http_client_options_set_subr(getThis(), ZEND_STRL("cookies"), opts, 1);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_addCookies, 0, 0, 0)
+       ZEND_ARG_ARRAY_INFO(0, cookies, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, addCookies)
+ {
+       zval *opts = NULL;
 -      php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 0 TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
 -              php_http_client_options_get_subr(getThis(), ZEND_STRS("cookies"), return_value TSRMLS_CC);
++      php_http_client_options_set_subr(getThis(), ZEND_STRL("cookies"), opts, 0);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getCookies, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, getCookies)
+ {
+       if (SUCCESS == zend_parse_parameters_none()) {
 -static PHP_METHOD(HttpClient, getAvailableDrivers) {
++              php_http_client_options_get_subr(getThis(), ZEND_STRL("cookies"), return_value);
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableDrivers, 0, 0, 0)
+ ZEND_END_ARG_INFO();
 -              php_http_client_driver_list(Z_ARRVAL_P(return_value) TSRMLS_CC);
++static PHP_METHOD(HttpClient, getAvailableDrivers)
++{
+       if (SUCCESS == zend_parse_parameters_none()) {
+               array_init(return_value);
 -              php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++              php_http_client_driver_list(Z_ARRVAL_P(return_value));
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableOptions, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, getAvailableOptions)
+ {
+       if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++              php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
+               array_init(return_value);
+               php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS, NULL, &Z_ARRVAL_P(return_value));
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getAvailableConfiguration, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClient, getAvailableConfiguration)
+ {
+       if (SUCCESS == zend_parse_parameters_none()) {
 -      php_http_client_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
++              php_http_client_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
+               array_init(return_value);
+               php_http_client_getopt(obj->client, PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION, NULL, &Z_ARRVAL_P(return_value));
+       }
+ }
+ static zend_function_entry php_http_client_methods[] = {
+       PHP_ME(HttpClient, __construct,          ai_HttpClient_construct,            ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
+       PHP_ME(HttpClient, reset,                ai_HttpClient_reset,                ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, enqueue,              ai_HttpClient_enqueue,              ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, dequeue,              ai_HttpClient_dequeue,              ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, requeue,              ai_HttpClient_requeue,              ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, count,                ai_HttpClient_count,                ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, send,                 ai_HttpClient_send,                 ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, once,                 ai_HttpClient_once,                 ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, wait,                 ai_HttpClient_wait,                 ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getResponse,          ai_HttpClient_getResponse,          ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getHistory,           ai_HttpClient_getHistory,           ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, configure,            ai_HttpClient_configure,            ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, enablePipelining,     ai_HttpClient_enablePipelining,     ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
+       PHP_ME(HttpClient, enableEvents,         ai_HttpClient_enableEvents,         ZEND_ACC_PUBLIC|ZEND_ACC_DEPRECATED)
+       PHP_ME(HttpClient, notify,               ai_HttpClient_notify,               ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, attach,               ai_HttpClient_attach,               ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, detach,               ai_HttpClient_detach,               ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getObservers,         ai_HttpClient_getObservers,         ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getProgressInfo,      ai_HttpClient_getProgressInfo,      ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getTransferInfo,      ai_HttpClient_getTransferInfo,      ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, setOptions,           ai_HttpClient_setOptions,           ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getOptions,           ai_HttpClient_getOptions,           ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, setSslOptions,        ai_HttpClient_setSslOptions,        ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, addSslOptions,        ai_HttpClient_addSslOptions,        ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getSslOptions,        ai_HttpClient_getSslOptions,        ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, setCookies,           ai_HttpClient_setCookies,           ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, addCookies,           ai_HttpClient_addCookies,           ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getCookies,           ai_HttpClient_getCookies,           ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getAvailableDrivers,  ai_HttpClient_getAvailableDrivers,  ZEND_ACC_PUBLIC|ZEND_ACC_STATIC)
+       PHP_ME(HttpClient, getAvailableOptions,  ai_HttpClient_getAvailableOptions,  ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClient, getAvailableConfiguration, ai_HttpClient_getAvailableConfiguration, ZEND_ACC_PUBLIC)
+       EMPTY_FUNCTION_ENTRY
+ };
+ PHP_MINIT_FUNCTION(http_client)
+ {
+       zend_class_entry ce = {0};
+       INIT_NS_CLASS_ENTRY(ce, "http", "Client", php_http_client_methods);
 -      zend_class_implements(php_http_client_class_entry TSRMLS_CC, 2, spl_ce_SplSubject, spl_ce_Countable);
++      php_http_client_class_entry = zend_register_internal_class_ex(&ce, NULL);
+       php_http_client_class_entry->create_object = php_http_client_object_new;
 -      zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("observers"), ZEND_ACC_PRIVATE TSRMLS_CC);
 -      zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("options"), ZEND_ACC_PROTECTED TSRMLS_CC);
 -      zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("history"), ZEND_ACC_PROTECTED TSRMLS_CC);
 -      zend_declare_property_bool(php_http_client_class_entry, ZEND_STRL("recordHistory"), 0, ZEND_ACC_PUBLIC TSRMLS_CC);
++      zend_class_implements(php_http_client_class_entry, 2, spl_ce_SplSubject, spl_ce_Countable);
+       memcpy(&php_http_client_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
++      php_http_client_object_handlers.offset = XtOffsetOf(php_http_client_object_t, zo);
++      php_http_client_object_handlers.free_obj = php_http_client_object_free;
+       php_http_client_object_handlers.clone_obj = NULL;
 -      zend_hash_init(&php_http_client_drivers, 2, NULL, NULL, 1);
++      zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("observers"), ZEND_ACC_PRIVATE);
++      zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("options"), ZEND_ACC_PROTECTED);
++      zend_declare_property_null(php_http_client_class_entry, ZEND_STRL("history"), ZEND_ACC_PROTECTED);
++      zend_declare_property_bool(php_http_client_class_entry, ZEND_STRL("recordHistory"), 0, ZEND_ACC_PUBLIC);
++      zend_hash_init(&php_http_client_drivers, 2, NULL, php_http_client_driver_hash_dtor, 1);
+       return SUCCESS;
+ }
+ PHP_MSHUTDOWN_FUNCTION(http_client)
+ {
+       zend_hash_destroy(&php_http_client_drivers);
+       return SUCCESS;
+ }
+ /*
+  * Local variables:
+  * tab-width: 4
+  * c-basic-offset: 4
+  * End:
+  * vim600: noet sw=4 ts=4 fdm=marker
+  * vim<600: noet sw=4 ts=4
+  */
index 0000000,f4a5b59..792581a
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,161 +1,157 @@@
 -      const char *name_str;
 -      size_t name_len;
+ /*
+     +--------------------------------------------------------------------+
+     | PECL :: http                                                       |
+     +--------------------------------------------------------------------+
+     | Redistribution and use in source and binary forms, with or without |
+     | modification, are permitted provided that the conditions mentioned |
+     | in the accompanying LICENSE file are met.                          |
+     +--------------------------------------------------------------------+
+     | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
+     +--------------------------------------------------------------------+
+ */
+ #ifndef PHP_HTTP_CLIENT_H
+ #define PHP_HTTP_CLIENT_H
+ typedef enum php_http_client_setopt_opt {
+       PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING,
+       PHP_HTTP_CLIENT_OPT_USE_EVENTS,
+       PHP_HTTP_CLIENT_OPT_CONFIGURATION,
+ } php_http_client_setopt_opt_t;
+ typedef enum php_http_client_getopt_opt {
+       PHP_HTTP_CLIENT_OPT_PROGRESS_INFO,              /* php_http_client_enqueue_t*, php_http_client_progress_state_t** */
+       PHP_HTTP_CLIENT_OPT_TRANSFER_INFO,              /* php_http_client_enqueue_t*, HashTable* */
+       PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS,          /* NULL, HashTable* */
+       PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION,/* NULL, HashTable */
+ } php_http_client_getopt_opt_t;
+ typedef struct php_http_client_enqueue {
+       php_http_message_t *request; /* unique */
+       HashTable *options;
+       void (*dtor)(struct php_http_client_enqueue *);
+       void *opaque;
+       struct {
+               zend_fcall_info fci;
+               zend_fcall_info_cache fcc;
+       } closure;
+ } php_http_client_enqueue_t;
+ typedef struct php_http_client *(*php_http_client_init_func_t)(struct php_http_client *p, void *init_arg);
+ typedef struct php_http_client *(*php_http_client_copy_func_t)(struct php_http_client *from, struct php_http_client *to);
+ typedef void (*php_http_client_dtor_func_t)(struct php_http_client *p);
+ typedef void (*php_http_client_reset_func_t)(struct php_http_client *p);
+ typedef ZEND_RESULT_CODE (*php_http_client_exec_func_t)(struct php_http_client *p);
+ typedef int (*php_http_client_once_func_t)(struct php_http_client *p);
+ typedef ZEND_RESULT_CODE (*php_http_client_wait_func_t)(struct php_http_client *p, struct timeval *custom_timeout);
+ typedef ZEND_RESULT_CODE (*php_http_client_enqueue_func_t)(struct php_http_client *p, php_http_client_enqueue_t *enqueue);
+ typedef ZEND_RESULT_CODE (*php_http_client_dequeue_func_t)(struct php_http_client *p, php_http_client_enqueue_t *enqueue);
+ typedef ZEND_RESULT_CODE (*php_http_client_setopt_func_t)(struct php_http_client *p, php_http_client_setopt_opt_t opt, void *arg);
+ typedef ZEND_RESULT_CODE (*php_http_client_getopt_func_t)(struct php_http_client *h, php_http_client_getopt_opt_t opt, void *arg, void **res);
+ typedef struct php_http_client_ops {
+       php_resource_factory_ops_t *rsrc;
+       php_http_client_init_func_t init;
+       php_http_client_copy_func_t copy;
+       php_http_client_dtor_func_t dtor;
+       php_http_client_reset_func_t reset;
+       php_http_client_exec_func_t exec;
+       php_http_client_wait_func_t wait;
+       php_http_client_once_func_t once;
+       php_http_client_enqueue_func_t enqueue;
+       php_http_client_dequeue_func_t dequeue;
+       php_http_client_setopt_func_t setopt;
+       php_http_client_getopt_func_t getopt;
+ } php_http_client_ops_t;
+ typedef struct php_http_client_driver {
 -PHP_HTTP_API ZEND_RESULT_CODE php_http_client_driver_get(const char *name_str, size_t name_len, php_http_client_driver_t *driver);
++      zend_string *driver_name;
++      zend_string *client_name;
++      zend_string *request_name;
+       php_http_client_ops_t *client_ops;
+ } php_http_client_driver_t;
+ PHP_HTTP_API ZEND_RESULT_CODE php_http_client_driver_add(php_http_client_driver_t *driver);
 -
 -#ifdef ZTS
 -      void ***ts;
 -#endif
++PHP_HTTP_API php_http_client_driver_t *php_http_client_driver_get(zend_string *name);
+ typedef struct php_http_client_progress_state {
+       struct {
+               double now;
+               double total;
+       } ul;
+       struct {
+               double now;
+               double total;
+       } dl;
+       const char *info;
+       unsigned started:1;
+       unsigned finished:1;
+ } php_http_client_progress_state_t;
+ typedef ZEND_RESULT_CODE (*php_http_client_response_callback_t)(void *arg, struct php_http_client *client, php_http_client_enqueue_t *e, php_http_message_t **response);
+ typedef void (*php_http_client_progress_callback_t)(void *arg, struct php_http_client *client, php_http_client_enqueue_t *e, php_http_client_progress_state_t *state);
+ typedef struct php_http_client {
+       void *ctx;
+       php_resource_factory_t *rf;
+       php_http_client_ops_t *ops;
+       struct {
+               struct {
+                       php_http_client_response_callback_t func;
+                       void *arg;
+               } response;
+               struct {
+                       php_http_client_progress_callback_t func;
+                       void *arg;
+               } progress;
+       } callback;
+       zend_llist requests;
+       zend_llist responses;
 -      zend_object zo;
 -      zend_object_value zv;
+ } php_http_client_t;
+ PHP_HTTP_API zend_class_entry *php_http_client_class_entry;
+ typedef struct php_http_client_object {
 -      long iterator;
+       php_http_client_t *client;
 -PHP_HTTP_API php_http_client_t *php_http_client_init(php_http_client_t *h, php_http_client_ops_t *ops, php_resource_factory_t *rf, void *init_arg TSRMLS_DC);
+       php_http_object_method_t *update;
+       php_http_object_method_t notify;
++      long iterator;
++      zend_object zo;
+ } php_http_client_object_t;
++PHP_HTTP_API php_http_client_t *php_http_client_init(php_http_client_t *h, php_http_client_ops_t *ops, php_resource_factory_t *rf, void *init_arg);
+ PHP_HTTP_API php_http_client_t *php_http_client_copy(php_http_client_t *from, php_http_client_t *to);
+ PHP_HTTP_API void php_http_client_dtor(php_http_client_t *h);
+ PHP_HTTP_API void php_http_client_free(php_http_client_t **h);
+ PHP_HTTP_API ZEND_RESULT_CODE php_http_client_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue);
+ PHP_HTTP_API ZEND_RESULT_CODE php_http_client_dequeue(php_http_client_t *h, php_http_message_t *request);
+ PHP_HTTP_API ZEND_RESULT_CODE php_http_client_wait(php_http_client_t *h, struct timeval *custom_timeout);
+ PHP_HTTP_API int php_http_client_once(php_http_client_t *h);
+ PHP_HTTP_API ZEND_RESULT_CODE php_http_client_exec(php_http_client_t *h);
+ PHP_HTTP_API void php_http_client_reset(php_http_client_t *h);
+ PHP_HTTP_API ZEND_RESULT_CODE php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg);
+ PHP_HTTP_API ZEND_RESULT_CODE php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void *res_ptr);
+ typedef int (*php_http_client_enqueue_cmp_func_t)(php_http_client_enqueue_t *cmp, void *arg);
+ /* compare with request message pointer if compare_func is NULL */
+ PHP_HTTP_API php_http_client_enqueue_t *php_http_client_enqueued(php_http_client_t *h, void *compare_arg, php_http_client_enqueue_cmp_func_t compare_func);
+ PHP_MINIT_FUNCTION(http_client);
+ PHP_MSHUTDOWN_FUNCTION(http_client);
+ #endif
+ /*
+  * Local variables:
+  * tab-width: 4
+  * c-basic-offset: 4
+  * End:
+  * vim600: noet sw=4 ts=4 fdm=marker
+  * vim<600: noet sw=4 ts=4
+  */
index 0000000,fed92fc..85f12b7
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,2599 +1,2628 @@@
 -              long redirects;
 -              unsigned range_request:1;
 -              unsigned encode_cookies:1;
 -
+ /*
+     +--------------------------------------------------------------------+
+     | PECL :: http                                                       |
+     +--------------------------------------------------------------------+
+     | Redistribution and use in source and binary forms, with or without |
+     | modification, are permitted provided that the conditions mentioned |
+     | in the accompanying LICENSE file are met.                          |
+     +--------------------------------------------------------------------+
+     | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
+     +--------------------------------------------------------------------+
+ */
+ #include "php_http_api.h"
+ #include "php_http_client.h"
+ #if PHP_HTTP_HAVE_CURL
+ #if PHP_HTTP_HAVE_EVENT
+ #     if !PHP_HTTP_HAVE_EVENT2 && /* just be really sure */ !(LIBEVENT_VERSION_NUMBER >= 0x02000000)
+ #             include <event.h>
+ #             define event_base_new event_init
+ #             define event_assign(e, b, s, a, cb, d) do {\
+                       event_set(e, s, a, cb, d); \
+                       event_base_set(b, e); \
+               } while(0)
+ #     else
+ #             if PHP_HTTP_HAVE_EVENT2
+ #                     include <event2/event.h>
+ #                     include <event2/event_struct.h>
+ #             else
+ #                     error "libevent presence is unknown"
+ #             endif
+ #     endif
+ #     ifndef DBG_EVENTS
+ #             define DBG_EVENTS 0
+ #     endif
+ #endif
+ #ifdef PHP_HTTP_HAVE_OPENSSL
+ #     include <openssl/ssl.h>
+ #endif
+ #ifdef PHP_HTTP_HAVE_GNUTLS
+ #     include <gnutls.h>
+ #endif
+ typedef struct php_http_client_curl {
+       CURLM *handle;
+       int unfinished;  /* int because of curl_multi_perform() */
+ #if PHP_HTTP_HAVE_EVENT
+       struct event_base *evbase;
+       struct event *timeout;
+       unsigned useevents:1;
+ #endif
+ } php_http_client_curl_t;
+ typedef struct php_http_client_curl_handler {
+       CURL *handle;
+       php_resource_factory_t *rf;
+       php_http_client_t *client;
+       php_http_client_progress_state_t progress;
+       php_http_client_enqueue_t queue;
+       struct {
+               php_http_buffer_t headers;
+               php_http_message_body_t *body;
+       } response;
+       struct {
+               HashTable cache;
+               struct curl_slist *proxyheaders;
+               struct curl_slist *headers;
+               struct curl_slist *resolve;
+               php_http_buffer_t cookies;
+               php_http_buffer_t ranges;
 -static void *php_http_curle_ctor(void *opaque, void *init_arg TSRMLS_DC)
+               struct {
+                       uint count;
+                       double delay;
+               } retry;
++              long redirects;
++              unsigned range_request:1;
++              unsigned encode_cookies:1;
++
+       } options;
+ } php_http_client_curl_handler_t;
+ typedef struct php_http_curle_storage {
+       char *url;
+       char *cookiestore;
+       CURLcode errorcode;
+       char errorbuffer[0x100];
+ } php_http_curle_storage_t;
+ static inline php_http_curle_storage_t *php_http_curle_get_storage(CURL *ch) {
+       php_http_curle_storage_t *st = NULL;
+       curl_easy_getinfo(ch, CURLINFO_PRIVATE, &st);
+       if (!st) {
+               st = pecalloc(1, sizeof(*st), 1);
+               curl_easy_setopt(ch, CURLOPT_PRIVATE, st);
+               curl_easy_setopt(ch, CURLOPT_ERRORBUFFER, st->errorbuffer);
+       }
+       return st;
+ }
 -static void *php_http_curle_copy(void *opaque, void *handle TSRMLS_DC)
++static void *php_http_curle_ctor(void *opaque, void *init_arg)
+ {
+       void *ch;
+       if ((ch = curl_easy_init())) {
+               php_http_curle_get_storage(ch);
+               return ch;
+       }
+       return NULL;
+ }
 -static void php_http_curle_dtor(void *opaque, void *handle TSRMLS_DC)
++static void *php_http_curle_copy(void *opaque, void *handle)
+ {
+       void *ch;
+       if ((ch = curl_easy_duphandle(handle))) {
+               curl_easy_reset(ch);
+               php_http_curle_get_storage(ch);
+               return ch;
+       }
+       return NULL;
+ }
 -static void *php_http_curlm_ctor(void *opaque, void *init_arg TSRMLS_DC)
++static void php_http_curle_dtor(void *opaque, void *handle)
+ {
+       php_http_curle_storage_t *st = php_http_curle_get_storage(handle);
+       curl_easy_cleanup(handle);
+       if (st) {
+               if (st->url) {
+                       pefree(st->url, 1);
+               }
+               if (st->cookiestore) {
+                       pefree(st->cookiestore, 1);
+               }
+               pefree(st, 1);
+       }
+ }
+ static php_resource_factory_ops_t php_http_curle_resource_factory_ops = {
+       php_http_curle_ctor,
+       php_http_curle_copy,
+       php_http_curle_dtor
+ };
 -static void php_http_curlm_dtor(void *opaque, void *handle TSRMLS_DC)
++static void *php_http_curlm_ctor(void *opaque, void *init_arg)
+ {
+       return curl_multi_init();
+ }
 -      if (body && body->stream_id) {
++static void php_http_curlm_dtor(void *opaque, void *handle)
+ {
+       curl_multi_cleanup(handle);
+ }
+ static php_resource_factory_ops_t php_http_curlm_resource_factory_ops = {
+       php_http_curlm_ctor,
+       NULL,
+       php_http_curlm_dtor
+ };
+ /* curl callbacks */
+ static size_t php_http_curle_read_callback(void *data, size_t len, size_t n, void *ctx)
+ {
+       php_http_message_body_t *body = ctx;
 -                      TSRMLS_FETCH_FROM_CTX(body->ts);
++      if (body && body->res) {
+               php_stream *s = php_http_message_body_stream(body);
+               if (s) {
 -      TSRMLS_FETCH_FROM_CTX(body->ts);
+                       return php_stream_read(s, data, len * n);
+               } else abort();
+       }
+       return 0;
+ }
+ #if PHP_HTTP_CURL_VERSION(7,32,0)
+ static int php_http_curle_xferinfo_callback(void *ctx, curl_off_t dltotal, curl_off_t dlnow, curl_off_t ultotal, curl_off_t ulnow)
+ #else
+ static int php_http_curle_progress_callback(void *ctx, double dltotal, double dlnow, double ultotal, double ulnow)
+ #endif
+ {
+       php_http_client_curl_handler_t *h = ctx;
+       zend_bool update = 0;
+       if (h->progress.dl.total != dltotal
+       ||      h->progress.dl.now != dlnow
+       ||      h->progress.ul.total != ultotal
+       ||      h->progress.ul.now != ulnow
+       ) {
+               update = 1;
+               h->progress.dl.total = dltotal;
+               h->progress.dl.now = dlnow;
+               h->progress.ul.total = ultotal;
+               h->progress.ul.now = ulnow;
+       }
+       if (update && h->client->callback.progress.func) {
+               h->client->callback.progress.func(h->client->callback.progress.arg, h->client, &h->queue, &h->progress);
+       }
+       return 0;
+ }
+ static int php_http_curle_seek_callback(void *userdata, curl_off_t offset, int origin)
+ {
+       php_http_message_body_t *body = userdata;
 -      char *c;
 -      long l;
 -      double d;
 -      struct curl_slist *s, *p;
 -      zval *subarray, array;
 -      INIT_PZVAL_ARRAY(&array, info);
+       if (!body) {
+               return 1;
+       }
+       if (0 == php_stream_seek(php_http_message_body_stream(body), offset, origin)) {
+               return 0;
+       }
+       return 2;
+ }
+ static int php_http_curle_raw_callback(CURL *ch, curl_infotype type, char *data, size_t length, void *ctx)
+ {
+       php_http_client_curl_handler_t *h = ctx;
+       /* catch progress */
+       switch (type) {
+               case CURLINFO_TEXT:
+                       if (data[0] == '-') {
+                       } else if (php_memnstr(data, ZEND_STRL("Adding handle:"), data + length)) {
+                               h->progress.info = "setup";
+                       } else if (php_memnstr(data, ZEND_STRL("addHandle"), data + length)) {
+                               h->progress.info = "setup";
+                       } else if (php_memnstr(data, ZEND_STRL("About to connect"), data + length)) {
+                               h->progress.info = "resolve";
+                       } else if (php_memnstr(data, ZEND_STRL("Trying"), data + length)) {
+                               h->progress.info = "connect";
+                       } else if (php_memnstr(data, ZEND_STRL("Found bundle for host"), data + length)) {
+                               h->progress.info = "connect";
+                       } else if (php_memnstr(data, ZEND_STRL("Connected"), data + length)) {
+                               h->progress.info = "connected";
+                       } else if (php_memnstr(data, ZEND_STRL("Re-using existing connection!"), data + length)) {
+                               h->progress.info = "connected";
+                       } else if (php_memnstr(data, ZEND_STRL("blacklisted"), data + length)) {
+                               h->progress.info = "blacklist check";
+                       } else if (php_memnstr(data, ZEND_STRL("SSL"), data + length)) {
+                               h->progress.info = "ssl negotiation";
+                       } else if (php_memnstr(data, ZEND_STRL("upload"), data + length)) {
+                               h->progress.info = "uploaded";
+                       } else if (php_memnstr(data, ZEND_STRL("left intact"), data + length)) {
+                               h->progress.info = "not disconnected";
+                       } else if (php_memnstr(data, ZEND_STRL("closed"), data + length)) {
+                               h->progress.info = "disconnected";
+                       } else if (php_memnstr(data, ZEND_STRL("Issue another request"), data + length)) {
+                               h->progress.info = "redirect";
+                       } else if (php_memnstr(data, ZEND_STRL("Operation timed out"), data + length)) {
+                               h->progress.info = "timeout";
+                       } else {
+ #if 0
+                               h->progress.info = data;
+                               data[length - 1] = '\0';
+ #endif
+                       }
+                       if (h->client->callback.progress.func) {
+                               h->client->callback.progress.func(h->client->callback.progress.arg, h->client, &h->queue, &h->progress);
+                       }
+                       break;
+               case CURLINFO_HEADER_OUT:
+               case CURLINFO_DATA_OUT:
+               case CURLINFO_SSL_DATA_OUT:
+                       h->progress.info = "send";
+                       break;
+               case CURLINFO_HEADER_IN:
+               case CURLINFO_DATA_IN:
+               case CURLINFO_SSL_DATA_IN:
+                       h->progress.info = "receive";
+                       break;
+               default:
+                       break;
+       }
+ #if 0
+       /* debug */
+       _dpf(type, data, length);
+ #endif
+       return 0;
+ }
+ static int php_http_curle_header_callback(char *data, size_t n, size_t l, void *arg)
+ {
+       php_http_client_curl_handler_t *h = arg;
+       return php_http_buffer_append(&h->response.headers, data, n * l);
+ }
+ static int php_http_curle_body_callback(char *data, size_t n, size_t l, void *arg)
+ {
+       php_http_client_curl_handler_t *h = arg;
+       return php_http_message_body_append(h->response.body, data, n*l);
+ }
+ static ZEND_RESULT_CODE php_http_curle_get_info(CURL *ch, HashTable *info)
+ {
 -              add_assoc_string_ex(&array, "effective_url", sizeof("effective_url"), c ? c : "", 1);
++      char *c = NULL;
++      long l = 0;
++      double d = 0;
++      struct curl_slist *s = NULL, *p = NULL;
++      zval tmp = {{0}};
+       /* BEGIN::CURLINFO */
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_EFFECTIVE_URL, &c)) {
 -              add_assoc_long_ex(&array, "response_code", sizeof("response_code"), l);
++              ZVAL_STRING(&tmp, STR_PTR(c));
++              zend_hash_str_update(info, "effective_url", lenof("effective_url"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_RESPONSE_CODE, &l)) {
 -              add_assoc_double_ex(&array, "total_time", sizeof("total_time"), d);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "response_code", lenof("response_code"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TOTAL_TIME, &d)) {
 -              add_assoc_double_ex(&array, "namelookup_time", sizeof("namelookup_time"), d);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "total_time", lenof("total_time"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NAMELOOKUP_TIME, &d)) {
 -              add_assoc_double_ex(&array, "connect_time", sizeof("connect_time"), d);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "namelookup_time", lenof("namelookup_time"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONNECT_TIME, &d)) {
 -              add_assoc_double_ex(&array, "pretransfer_time", sizeof("pretransfer_time"), d);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "connect_time", lenof("connect_time"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRETRANSFER_TIME, &d)) {
 -              add_assoc_double_ex(&array, "size_upload", sizeof("size_upload"), d);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "pretransfer_time", lenof("pretransfer_time"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_UPLOAD, &d)) {
 -              add_assoc_double_ex(&array, "size_download", sizeof("size_download"), d);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "size_upload", lenof("size_upload"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SIZE_DOWNLOAD, &d)) {
 -              add_assoc_double_ex(&array, "speed_download", sizeof("speed_download"), d);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "size_download", lenof("size_download"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_DOWNLOAD, &d)) {
 -              add_assoc_double_ex(&array, "speed_upload", sizeof("speed_upload"), d);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "speed_download", lenof("speed_download"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SPEED_UPLOAD, &d)) {
 -              add_assoc_long_ex(&array, "header_size", sizeof("header_size"), l);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "speed_upload", lenof("speed_upload"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HEADER_SIZE, &l)) {
 -              add_assoc_long_ex(&array, "request_size", sizeof("request_size"), l);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "header_size", lenof("header_size"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REQUEST_SIZE, &l)) {
 -              add_assoc_long_ex(&array, "ssl_verifyresult", sizeof("ssl_verifyresult"), l);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "request_size", lenof("request_size"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_VERIFYRESULT, &l)) {
 -              add_assoc_long_ex(&array, "filetime", sizeof("filetime"), l);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "ssl_verifyresult", lenof("ssl_verifyresult"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_FILETIME, &l)) {
 -              add_assoc_double_ex(&array, "content_length_download", sizeof("content_length_download"), d);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "filetime", lenof("filetime"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &d)) {
 -              add_assoc_double_ex(&array, "content_length_upload", sizeof("content_length_upload"), d);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "content_length_download", lenof("content_length_download"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_LENGTH_UPLOAD, &d)) {
 -              add_assoc_double_ex(&array, "starttransfer_time", sizeof("starttransfer_time"), d);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "content_length_upload", lenof("content_length_upload"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_STARTTRANSFER_TIME, &d)) {
 -              add_assoc_string_ex(&array, "content_type", sizeof("content_type"), c ? c : "", 1);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "starttransfer_time", lenof("starttransfer_time"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONTENT_TYPE, &c)) {
 -              add_assoc_double_ex(&array, "redirect_time", sizeof("redirect_time"), d);
++              ZVAL_STRING(&tmp, STR_PTR(c));
++              zend_hash_str_update(info, "content_type", lenof("content_type"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_TIME, &d)) {
 -              add_assoc_long_ex(&array, "redirect_count", sizeof("redirect_count"), l);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "redirect_time", lenof("redirect_time"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_COUNT, &l)) {
 -              add_assoc_long_ex(&array, "connect_code", sizeof("connect_code"), l);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "redirect_count", lenof("redirect_count"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTP_CONNECTCODE, &l)) {
 -              add_assoc_long_ex(&array, "httpauth_avail", sizeof("httpauth_avail"), l);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "connect_code", lenof("connect_code"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_HTTPAUTH_AVAIL, &l)) {
 -              add_assoc_long_ex(&array, "proxyauth_avail", sizeof("proxyauth_avail"), l);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "httpauth_avail", lenof("httpauth_avail"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PROXYAUTH_AVAIL, &l)) {
 -              add_assoc_long_ex(&array, "os_errno", sizeof("os_errno"), l);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "proxyauth_avail", lenof("proxyauth_avail"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_OS_ERRNO, &l)) {
 -              add_assoc_long_ex(&array, "num_connects", sizeof("num_connects"), l);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "os_errno", lenof("os_errno"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_NUM_CONNECTS, &l)) {
 -              MAKE_STD_ZVAL(subarray);
 -              array_init(subarray);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "num_connects", lenof("num_connects"), &tmp);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_SSL_ENGINES, &s)) {
 -                              add_next_index_string(subarray, p->data, 1);
++              array_init(&tmp);
+               for (p = s; p; p = p->next) {
+                       if (p->data) {
 -              add_assoc_zval_ex(&array, "ssl_engines", sizeof("ssl_engines"), subarray);
++                              add_next_index_string(&tmp, p->data);
+                       }
+               }
 -              add_assoc_string_ex(&array, "redirect_url", sizeof("redirect_url"), c ? c : "", 1);
++              zend_hash_str_update(info, "ssl_engines", lenof("ssl_engines"), &tmp);
+               curl_slist_free_all(s);
+       }
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_REDIRECT_URL, &c)) {
 -              add_assoc_string_ex(&array, "primary_ip", sizeof("primary_ip"), c ? c : "", 1);
++              ZVAL_STRING(&tmp, STR_PTR(c));
++              zend_hash_str_update(info, "redirect_url", lenof("redirect_url"), &tmp);
+       }
+ #if PHP_HTTP_CURL_VERSION(7,19,0)
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_IP, &c)) {
 -              add_assoc_double_ex(&array, "appconnect_time", sizeof("appconnect_time"), d);
++              ZVAL_STRING(&tmp, STR_PTR(c));
++              zend_hash_str_update(info, "primary_ip", lenof("primary_ip"), &tmp);
+       }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,19,0)
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_APPCONNECT_TIME, &d)) {
 -              add_assoc_long_ex(&array, "condition_unmet", sizeof("condition_unmet"), l);
++              ZVAL_DOUBLE(&tmp, d);
++              zend_hash_str_update(info, "appconnect_time", lenof("appconnect_time"), &tmp);
+       }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,19,4)
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CONDITION_UNMET, &l)) {
 -              add_assoc_long_ex(&array, "primary_port", sizeof("primary_port"), l);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "condition_unmet", lenof("condition_unmet"), &tmp);
+       }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,21,0)
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_PRIMARY_PORT, &l)) {
 -              add_assoc_string_ex(&array, "local_ip", sizeof("local_ip"), c ? c : "", 1);
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "primary_port", lenof("primary_port"), &tmp);
+       }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,21,0)
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_IP, &c)) {
 -              add_assoc_long_ex(&array, "local_port", sizeof("local_port"), l);
++              ZVAL_STRING(&tmp, STR_PTR(c));
++              zend_hash_str_update(info, "local_ip", lenof("local_ip"), &tmp);
+       }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,21,0)
+       if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_LOCAL_PORT, &l)) {
 -              zval *ti_array;
++              ZVAL_LONG(&tmp, l);
++              zend_hash_str_update(info, "local_port", lenof("local_port"), &tmp);
+       }
+ #endif
+       /* END::CURLINFO */
+ #if PHP_HTTP_CURL_VERSION(7,34,0)
+       {
 -                      const char *backend;
++              zval ti_array, subarray;
+               struct curl_tlssessioninfo *ti;
+               if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TLS_SESSION, &ti)) {
 -                      MAKE_STD_ZVAL(subarray);
 -                      ZVAL_NULL(subarray);
 -                      MAKE_STD_ZVAL(ti_array);
 -                      array_init(ti_array);
++                      char *backend;
 -                                      array_init(subarray);
 -                                      add_assoc_long_ex(subarray, ZEND_STRS("number"), SSL_CTX_sess_number(ctx));
 -                                      add_assoc_long_ex(subarray, ZEND_STRS("connect"), SSL_CTX_sess_connect(ctx));
 -                                      add_assoc_long_ex(subarray, ZEND_STRS("connect_good"), SSL_CTX_sess_connect_good(ctx));
 -                                      add_assoc_long_ex(subarray, ZEND_STRS("connect_renegotiate"), SSL_CTX_sess_connect_renegotiate(ctx));
 -                                      add_assoc_long_ex(subarray, ZEND_STRS("hits"), SSL_CTX_sess_hits(ctx));
 -                                      add_assoc_long_ex(subarray, ZEND_STRS("cache_full"), SSL_CTX_sess_cache_full(ctx));
++                      ZVAL_NULL(&subarray);
++                      array_init(&ti_array);
+                       switch (ti->backend) {
+                       case CURLSSLBACKEND_NONE:
+                               backend = "none";
+                               break;
+                       case CURLSSLBACKEND_OPENSSL:
+                               backend = "openssl";
+ #ifdef PHP_HTTP_HAVE_OPENSSL
+                               {
+                                       SSL_CTX *ctx = ti->internals;
 -                                      array_init(subarray);
++                                      array_init(&subarray);
++                                      add_assoc_long_ex(&subarray, ZEND_STRL("number"), SSL_CTX_sess_number(ctx));
++                                      add_assoc_long_ex(&subarray, ZEND_STRL("connect"), SSL_CTX_sess_connect(ctx));
++                                      add_assoc_long_ex(&subarray, ZEND_STRL("connect_good"), SSL_CTX_sess_connect_good(ctx));
++                                      add_assoc_long_ex(&subarray, ZEND_STRL("connect_renegotiate"), SSL_CTX_sess_connect_renegotiate(ctx));
++                                      add_assoc_long_ex(&subarray, ZEND_STRL("hits"), SSL_CTX_sess_hits(ctx));
++                                      add_assoc_long_ex(&subarray, ZEND_STRL("cache_full"), SSL_CTX_sess_cache_full(ctx));
+                               }
+ #endif
+                               break;
+                       case CURLSSLBACKEND_GNUTLS:
+                               backend = "gnutls";
+ #ifdef PHP_HTTP_HAVE_GNUTLS
+                               {
+                                       gnutls_session_t sess = ti->internals;
+                                       char *desc;
 -                                              add_assoc_string_ex(subarray, ZEND_STRS("desc"), desc, 1);
++                                      array_init(&subarray);
+                                       if ((desc = gnutls_session_get_desc(sess))) {
 -                                      add_assoc_bool_ex(subarray, ZEND_STRS("resumed"), gnutls_session_is_resumed(sess));
++                                              add_assoc_string_ex(&subarray, ZEND_STRL("desc"), desc);
+                                               gnutls_free(desc);
+                                       }
 -                      add_assoc_string_ex(ti_array, ZEND_STRS("backend"), estrdup(backend), 0);
 -                      add_assoc_zval_ex(ti_array, ZEND_STRS("internals"), subarray);
 -                      add_assoc_zval_ex(&array, "tls_session", sizeof("tls_session"), ti_array);
++                                      add_assoc_bool_ex(&subarray, ZEND_STRL("resumed"), gnutls_session_is_resumed(sess));
+                               }
+ #endif
+                               break;
+                       case CURLSSLBACKEND_NSS:
+                               backend = "nss";
+                               break;
+ #if !PHP_HTTP_CURL_VERSION(7,39,0)
+                       case CURLSSLBACKEND_QSOSSL:
+                               backend = "qsossl";
+                               break;
+ #else
+                       case CURLSSLBACKEND_GSKIT:
+                               backend = "gskit";
+                               break;
+ #endif
+                       case CURLSSLBACKEND_POLARSSL:
+                               backend = "polarssl";
+                               break;
+                       case CURLSSLBACKEND_CYASSL:
+                               backend = "cyassl";
+                               break;
+                       case CURLSSLBACKEND_SCHANNEL:
+                               backend = "schannel";
+                               break;
+                       case CURLSSLBACKEND_DARWINSSL:
+                               backend = "darwinssl";
+                               break;
+                       default:
+                               backend = "unknown";
+                       }
 -              zval *ci_array;
++                      add_assoc_string_ex(&ti_array, ZEND_STRL("backend"), backend);
++                      add_assoc_zval_ex(&ti_array, ZEND_STRL("internals"), &subarray);
++                      zend_hash_str_update(info, "tls_session", lenof("tls_session"), &ti_array);
+               }
+       }
+ #endif
+ #if (PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)) || (PHP_HTTP_CURL_VERSION(7,34,0) && defined(PHP_HTTP_HAVE_NSS)) || (PHP_HTTP_CURL_VERSION(7,42,0) && defined(PHP_HTTP_HAVE_GNUTLS)) || (PHP_HTTP_CURL_VERSION(7,39,0) && defined(PHP_HTTP_HAVE_GSKIT))
+       {
+               int i;
 -                      MAKE_STD_ZVAL(ci_array);
 -                      array_init(ci_array);
++              zval ci_array, subarray;
+               struct curl_certinfo *ci;
+               char *colon, *keyname;
+               if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_CERTINFO, &ci)) {
 -                              MAKE_STD_ZVAL(subarray);
 -                              array_init(subarray);
++                      array_init(&ci_array);
+                       for (i = 0; i < ci->num_of_certs; ++i) {
+                               s = ci->certinfo[i];
 -                                                      add_assoc_string_ex(subarray, keyname, colon - p->data + 1, colon + 1, 1);
++                              array_init(&subarray);
+                               for (p = s; p; p = p->next) {
+                                       if (p->data) {
+                                               if ((colon = strchr(p->data, ':'))) {
+                                                       keyname = estrndup(p->data, colon - p->data);
 -                                                      add_next_index_string(subarray, p->data, 1);
++                                                      add_assoc_string_ex(&subarray, keyname, colon - p->data, colon + 1);
+                                                       efree(keyname);
+                                               } else {
 -                              add_next_index_zval(ci_array, subarray);
++                                                      add_next_index_string(&subarray, p->data);
+                                               }
+                                       }
+                               }
 -                      add_assoc_zval_ex(&array, "certinfo", sizeof("certinfo"), ci_array);
++                              add_next_index_zval(&ci_array, &subarray);
+                       }
 -              add_assoc_long_ex(&array, "curlcode", sizeof("curlcode"), st->errorcode);
 -              add_assoc_string_ex(&array, "error", sizeof("error"), st->errorbuffer, 1);
++                      zend_hash_str_update(info, "certinfo", lenof("certinfo"), &ci_array);
+               }
+       }
+ #endif
+       {
+               php_http_curle_storage_t *st = php_http_curle_get_storage(ch);
 -static php_http_message_t *php_http_curlm_responseparser(php_http_client_curl_handler_t *h TSRMLS_DC)
++              ZVAL_LONG(&tmp, st->errorcode);
++              zend_hash_str_update(info, "curlcode", lenof("curlcode"), &tmp);
++              ZVAL_STRING(&tmp, st->errorbuffer);
++              zend_hash_str_update(info, "error", lenof("error"), &tmp);
+       }
+       return SUCCESS;
+ }
+ static int compare_queue(php_http_client_enqueue_t *e, void *handle)
+ {
+       return handle == ((php_http_client_curl_handler_t *) e->opaque)->handle;
+ }
 -      response = php_http_message_init(NULL, 0, h->response.body TSRMLS_CC);
 -      php_http_header_parser_init(&parser TSRMLS_CC);
++static php_http_message_t *php_http_curlm_responseparser(php_http_client_curl_handler_t *h)
+ {
+       php_http_message_t *response;
+       php_http_header_parser_t parser;
+       zval *zh;
 -      if ((zh = php_http_message_header(response, ZEND_STRL("Content-Length"), 1))) {
 -              zend_hash_update(&response->hdrs, "X-Original-Content-Length", sizeof("X-Original-Content-Length"), &zh, sizeof(zval *), NULL);
++      response = php_http_message_init(NULL, 0, h->response.body);
++      php_http_header_parser_init(&parser);
+       while (h->response.headers.used) {
+               php_http_header_parser_state_t st = php_http_header_parser_parse(&parser,
+                               &h->response.headers, PHP_HTTP_HEADER_PARSER_CLEANUP, &response->hdrs,
+                               (php_http_info_callback_t) php_http_message_info_callback, (void *) &response);
+               if (PHP_HTTP_HEADER_PARSER_STATE_FAILURE == st) {
+                       break;
+               }
+       }
+       php_http_header_parser_dtor(&parser);
+       /* move body to right message */
+       if (response->body != h->response.body) {
+               php_http_message_t *ptr = response;
+               while (ptr->parent) {
+                       ptr = ptr->parent;
+               }
+               php_http_message_body_free(&response->body);
+               response->body = ptr->body;
+               ptr->body = NULL;
+       }
+       php_http_message_body_addref(h->response.body);
+       /* let's update the response headers */
 -      if ((zh = php_http_message_header(response, ZEND_STRL("Transfer-Encoding"), 0))) {
 -              zend_hash_update(&response->hdrs, "X-Original-Transfer-Encoding", sizeof("X-Original-Transfer-Encoding"), (void *) &zh, sizeof(zval *), NULL);
 -              zend_hash_del(&response->hdrs, "Transfer-Encoding", sizeof("Transfer-Encoding"));
++      if ((zh = php_http_message_header(response, ZEND_STRL("Content-Length")))) {
++              Z_TRY_ADDREF_P(zh);
++              zend_hash_str_update(&response->hdrs, "X-Original-Content-Length", lenof("X-Original-Content-Length"), zh);
+       }
 -      if ((zh = php_http_message_header(response, ZEND_STRL("Content-Range"), 0))) {
 -              zend_hash_update(&response->hdrs, "X-Original-Content-Range", sizeof("X-Original-Content-Range"), &zh, sizeof(zval *), NULL);
 -              zend_hash_del(&response->hdrs, "Content-Range", sizeof("Content-Range"));
++      if ((zh = php_http_message_header(response, ZEND_STRL("Transfer-Encoding")))) {
++              Z_TRY_ADDREF_P(zh);
++              zend_hash_str_update(&response->hdrs, "X-Original-Transfer-Encoding", lenof("X-Original-Transfer-Encoding"), zh);
++              zend_hash_str_del(&response->hdrs, "Transfer-Encoding", lenof("Transfer-Encoding"));
+       }
 -      if ((zh = php_http_message_header(response, ZEND_STRL("Content-Encoding"), 0))) {
 -              zend_hash_update(&response->hdrs, "X-Original-Content-Encoding", sizeof("X-Original-Content-Encoding"), &zh, sizeof(zval *), NULL);
 -              zend_hash_del(&response->hdrs, "Content-Encoding", sizeof("Content-Encoding"));
++      if ((zh = php_http_message_header(response, ZEND_STRL("Content-Range")))) {
++              Z_TRY_ADDREF_P(zh);
++              zend_hash_str_update(&response->hdrs, "X-Original-Content-Range", lenof("X-Original-Content-Range"), zh);
++              zend_hash_str_del(&response->hdrs, "Content-Range", lenof("Content-Range"));
+       }
 -      TSRMLS_FETCH_FROM_CTX(context->ts);
++      if ((zh = php_http_message_header(response, ZEND_STRL("Content-Encoding")))) {
++              Z_TRY_ADDREF_P(zh);
++              zend_hash_str_update(&response->hdrs, "X-Original-Content-Encoding", lenof("X-Original-Content-Encoding"), zh);
++              zend_hash_str_del(&response->hdrs, "Content-Encoding", lenof("Content-Encoding"));
+       }
+       php_http_message_update_headers(response);
+       return response;
+ }
+ static void php_http_curlm_responsehandler(php_http_client_t *context)
+ {
+       int err_count = 0, remaining = 0;
+       php_http_curle_storage_t *st, *err = NULL;
+       php_http_client_enqueue_t *enqueue;
+       php_http_client_curl_t *curl = context->ctx;
 -                              php_http_message_t *response = php_http_curlm_responseparser(handler TSRMLS_CC);
+       do {
+               CURLMsg *msg = curl_multi_info_read(curl->handle, &remaining);
+               if (msg && CURLMSG_DONE == msg->msg) {
+                       if (CURLE_OK != msg->data.result) {
+                               st = php_http_curle_get_storage(msg->easy_handle);
+                               st->errorcode = msg->data.result;
+                               /* defer the warnings/exceptions, so the callback is still called for this request */
+                               if (!err) {
+                                       err = ecalloc(remaining + 1, sizeof(*err));
+                               }
+                               memcpy(&err[err_count], st, sizeof(*st));
+                               if (st->url) {
+                                       err[err_count].url = estrdup(st->url);
+                               }
+                               err_count++;
+                       }
+                       if ((enqueue = php_http_client_enqueued(context, msg->easy_handle, compare_queue))) {
+                               php_http_client_curl_handler_t *handler = enqueue->opaque;
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s; %s (%s)", curl_easy_strerror(err[i].errorcode), err[i].errorbuffer, STR_PTR(err[i].url));
++                              php_http_message_t *response = php_http_curlm_responseparser(handler);
+                               if (response) {
+                                       context->callback.response.func(context->callback.response.arg, context, &handler->queue, &response);
+                                       php_http_message_free(&response);
+                               }
+                       }
+               }
+       } while (remaining);
+       if (err_count) {
+               int i = 0;
+               do {
 -              TSRMLS_FETCH_FROM_CTX(context->ts);
++                      php_error_docref(NULL, E_WARNING, "%s; %s (%s)", curl_easy_strerror(err[i].errorcode), err[i].errorbuffer, STR_PTR(err[i].url));
+                       if (err[i].url) {
+                               efree(err[i].url);
+                       }
+               } while (++i < err_count);
+               efree(err);
+       }
+ }
+ #if PHP_HTTP_HAVE_EVENT
+ typedef struct php_http_curlm_event {
+       struct event evnt;
+       php_http_client_t *context;
+ } php_http_curlm_event_t;
+ static inline int etoca(short action) {
+       switch (action & (EV_READ|EV_WRITE)) {
+               case EV_READ:
+                       return CURL_CSELECT_IN;
+                       break;
+               case EV_WRITE:
+                       return CURL_CSELECT_OUT;
+                       break;
+               case EV_READ|EV_WRITE:
+                       return CURL_CSELECT_IN|CURL_CSELECT_OUT;
+                       break;
+               default:
+                       return 0;
+       }
+ }
+ static void php_http_curlm_timeout_callback(int socket, short action, void *event_data)
+ {
+       php_http_client_t *context = event_data;
+       php_http_client_curl_t *curl = context->ctx;
+ #if DBG_EVENTS
+       fprintf(stderr, "T");
+ #endif
+       if (curl->useevents) {
+               CURLMcode rc;
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s",  curl_multi_strerror(rc));
+               /* ignore and use -1,0 on timeout */
+               (void) socket;
+               (void) action;
+               while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, CURL_SOCKET_TIMEOUT, 0, &curl->unfinished)));
+               if (CURLM_OK != rc) {
 -              TSRMLS_FETCH_FROM_CTX(context->ts);
++                      php_error_docref(NULL, E_WARNING, "%s",  curl_multi_strerror(rc));
+               }
+               php_http_curlm_responsehandler(context);
+       }
+ }
+ static void php_http_curlm_event_callback(int socket, short action, void *event_data)
+ {
+       php_http_client_t *context = event_data;
+       php_http_client_curl_t *curl = context->ctx;
+ #if DBG_EVENTS
+       fprintf(stderr, "E");
+ #endif
+       if (curl->useevents) {
+               CURLMcode rc = CURLM_OK;
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", curl_multi_strerror(rc));
+               while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished)));
+               if (CURLM_OK != rc) {
 -              TSRMLS_FETCH_FROM_CTX(context->ts);
++                      php_error_docref(NULL, E_WARNING, "%s", curl_multi_strerror(rc));
+               }
+               php_http_curlm_responsehandler(context);
+               /* remove timeout if there are no transfers left */
+               if (!curl->unfinished && event_initialized(curl->timeout) && event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
+                       event_del(curl->timeout);
+               }
+       }
+ }
+ static int php_http_curlm_socket_callback(CURL *easy, curl_socket_t sock, int action, void *socket_data, void *assign_data)
+ {
+       php_http_client_t *context = socket_data;
+       php_http_client_curl_t *curl = context->ctx;
+ #if DBG_EVENTS
+       fprintf(stderr, "S");
+ #endif
+       if (curl->useevents) {
+               int events = EV_PERSIST;
+               php_http_curlm_event_t *ev = assign_data;
 -                              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown socket action %d", action);
+               if (!ev) {
+                       ev = ecalloc(1, sizeof(php_http_curlm_event_t));
+                       ev->context = context;
+                       curl_multi_assign(curl->handle, sock, ev);
+               } else {
+                       event_del(&ev->evnt);
+               }
+               switch (action) {
+                       case CURL_POLL_IN:
+                               events |= EV_READ;
+                               break;
+                       case CURL_POLL_OUT:
+                               events |= EV_WRITE;
+                               break;
+                       case CURL_POLL_INOUT:
+                               events |= EV_READ|EV_WRITE;
+                               break;
+                       case CURL_POLL_REMOVE:
+                               efree(ev);
+                               /* no break */
+                       case CURL_POLL_NONE:
+                               return 0;
+                       default:
 -      if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, Z_BVAL_P(val) ? 2 : 0)) {
++                              php_error_docref(NULL, E_WARNING, "Unknown socket action %d", action);
+                               return -1;
+               }
+               event_assign(&ev->evnt, curl->evbase, sock, events, php_http_curlm_event_callback, context);
+               event_add(&ev->evnt, NULL);
+       }
+       return 0;
+ }
+ static void php_http_curlm_timer_callback(CURLM *multi, long timeout_ms, void *timer_data)
+ {
+       php_http_client_t *context = timer_data;
+       php_http_client_curl_t *curl = context->ctx;
+ #if DBG_EVENTS
+       fprintf(stderr, "\ntimer <- timeout_ms: %ld\n", timeout_ms);
+ #endif
+       if (curl->useevents) {
+               if (timeout_ms < 0) {
+                       php_http_curlm_timeout_callback(CURL_SOCKET_TIMEOUT, /*EV_READ|EV_WRITE*/0, context);
+               } else if (timeout_ms > 0 || !event_initialized(curl->timeout) || !event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
+                       struct timeval timeout;
+                       if (!event_initialized(curl->timeout)) {
+                               event_assign(curl->timeout, curl->evbase, CURL_SOCKET_TIMEOUT, 0, php_http_curlm_timeout_callback, context);
+                       }
+                       timeout.tv_sec = timeout_ms / 1000;
+                       timeout.tv_usec = (timeout_ms % 1000) * 1000;
+                       event_add(curl->timeout, &timeout);
+               }
+       }
+ }
+ #endif /* HAVE_EVENT */
+ /* curl options */
+ static php_http_options_t php_http_curle_options, php_http_curlm_options;
+ #define PHP_HTTP_CURLE_OPTION_CHECK_STRLEN            0x0001
+ #define PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR           0x0002
+ #define PHP_HTTP_CURLE_OPTION_TRANSFORM_MS            0x0004
+ static ZEND_RESULT_CODE php_http_curle_option_set_ssl_verifyhost(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       CURL *ch = curl->handle;
 -      if (val && Z_STRLEN_P(val)) {
++      if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, Z_TYPE_P(val) == IS_TRUE ? 2 : 0)) {
+               return FAILURE;
+       }
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curle_option_set_cookiestore(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       CURL *ch = curl->handle;
+       php_http_curle_storage_t *storage = php_http_curle_get_storage(curl->handle);
+       if (storage->cookiestore) {
+               pefree(storage->cookiestore, 1);
+       }
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
++      if (val && Z_TYPE_P(val) == IS_STRING && Z_STRLEN_P(val)) {
+               storage->cookiestore = pestrndup(Z_STRVAL_P(val), Z_STRLEN_P(val), 1);
+       } else {
+               storage->cookiestore = NULL;
+       }
+       if (    CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIEFILE, storage->cookiestore)
+               ||      CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIEJAR, storage->cookiestore)
+       ) {
+               return FAILURE;
+       }
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curle_option_set_cookies(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       CURL *ch = curl->handle;
 -                      if (SUCCESS == php_http_url_encode_hash_ex(HASH_OF(val), &curl->options.cookies, ZEND_STRL(";"), ZEND_STRL("="), NULL, 0 TSRMLS_CC)) {
+       if (val && Z_TYPE_P(val) != IS_NULL) {
++              HashTable *ht = HASH_OF(val);
++
+               if (curl->options.encode_cookies) {
 -                      HashPosition pos;
 -                      php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0);
 -                      zval **cookie_val;
++                      if (SUCCESS == php_http_url_encode_hash_ex(ht, &curl->options.cookies, ZEND_STRL(";"), ZEND_STRL("="), NULL, 0)) {
+                               php_http_buffer_fix(&curl->options.cookies);
+                               if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data)) {
+                                       return FAILURE;
+                               }
+                       } else {
+                               return FAILURE;
+                       }
+               } else {
 -                      FOREACH_KEYVAL(pos, val, cookie_key, cookie_val) {
 -                              zval *zv = php_http_ztyp(IS_STRING, *cookie_val);
++                      php_http_arrkey_t cookie_key;
++                      zval *cookie_val;
 -                              php_http_array_hashkey_stringify(&cookie_key);
 -                              php_http_buffer_appendf(&curl->options.cookies, "%s=%s; ", cookie_key.str, Z_STRVAL_P(zv));
 -                              php_http_array_hashkey_stringfree(&cookie_key);
++                      ZEND_HASH_FOREACH_KEY_VAL(ht, cookie_key.h, cookie_key.key, cookie_val)
++                      {
++                              zend_string *zs = zval_get_string(cookie_val);
 -                              zval_ptr_dtor(&zv);
++                              php_http_arrkey_stringify(&cookie_key, NULL);
++                              php_http_buffer_appendf(&curl->options.cookies, "%s=%s; ", cookie_key.key->val, zs->val);
++                              php_http_arrkey_dtor(&cookie_key);
 -      curl->options.encode_cookies = Z_BVAL_P(val);
++                              zend_string_release(zs);
+                       }
++                      ZEND_HASH_FOREACH_END();
+                       php_http_buffer_fix(&curl->options.cookies);
+                       if (curl->options.cookies.used) {
+                               if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIE, curl->options.cookies.data)) {
+                                       return FAILURE;
+                               }
+                       }
+               }
+       } else {
+               php_http_buffer_reset(&curl->options.cookies);
+               if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_COOKIE, NULL)) {
+                       return FAILURE;
+               }
+       }
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curle_option_set_encodecookies(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
++      curl->options.encode_cookies = Z_TYPE_P(val) == IS_TRUE;
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curle_option_set_lastmodified(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       CURL *ch = curl->handle;
 -                      if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) sapi_get_request_time(TSRMLS_C) + Z_LVAL_P(val))) {
+       if (Z_LVAL_P(val)) {
+               if (Z_LVAL_P(val) > 0) {
+                       if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, Z_LVAL_P(val))) {
+                               return FAILURE;
+                       }
+               } else {
 -      if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_ACCEPT_ENCODING, Z_BVAL_P(val) ? "" : NULL)) {
++                      if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) sapi_get_request_time() + Z_LVAL_P(val))) {
+                               return FAILURE;
+                       }
+               }
+               if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMECONDITION, (long) (curl->options.range_request ? CURL_TIMECOND_IFUNMODSINCE : CURL_TIMECOND_IFMODSINCE))) {
+                       return FAILURE;
+               }
+       } else {
+               if (    CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, 0)
+                       ||      CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMECONDITION, 0)
+               ) {
+                       return FAILURE;
+               }
+       }
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curle_option_set_compress(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       CURL *ch = curl->handle;
+ #if !PHP_HTTP_CURL_VERSION(7,21,6)
+ #     define CURLOPT_ACCEPT_ENCODING CURLOPT_ENCODING
+ #endif
 -      if (Z_STRLEN_P(val)) {
++      if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_ACCEPT_ENCODING, Z_TYPE_P(val) == IS_TRUE ? "" : NULL)) {
+               return FAILURE;
+       }
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curle_option_set_etag(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       php_http_buffer_t header;
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
++      if (val && Z_TYPE_P(val) == IS_STRING && Z_STRLEN_P(val)) {
+               zend_bool is_quoted = !((Z_STRVAL_P(val)[0] != '"') || (Z_STRVAL_P(val)[Z_STRLEN_P(val)-1] != '"'));
+               php_http_buffer_init(&header);
+               php_http_buffer_appendf(&header, is_quoted?"%s: %s":"%s: \"%s\"", curl->options.range_request?"If-Match":"If-None-Match", Z_STRVAL_P(val));
+               php_http_buffer_fix(&header);
+               curl->options.headers = curl_slist_append(curl->options.headers, header.data);
+               php_http_buffer_dtor(&header);
+       }
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curle_option_set_range(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       CURL *ch = curl->handle;
 -              HashPosition pos;
 -              zval **rr, **rb, **re;
 -
 -              FOREACH_VAL(pos, val, rr) {
 -                      if (Z_TYPE_PP(rr) == IS_ARRAY) {
 -                              if (2 == php_http_array_list(Z_ARRVAL_PP(rr) TSRMLS_CC, 2, &rb, &re)) {
 -                                      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 = php_http_ztyp(IS_LONG, *rb);
 -                                              zval *rel = php_http_ztyp(IS_LONG, *re);
 -
 -                                              if ((Z_LVAL_P(rbl) >= 0) && (Z_LVAL_P(rel) >= 0)) {
 -                                                      php_http_buffer_appendf(&curl->options.ranges, "%ld-%ld,", Z_LVAL_P(rbl), Z_LVAL_P(rel));
+       php_http_buffer_reset(&curl->options.ranges);
+       if (val && Z_TYPE_P(val) != IS_NULL) {
 -                                              zval_ptr_dtor(&rbl);
 -                                              zval_ptr_dtor(&rel);
++              zval *rr, *rb, *re;
++              zend_long rbl, rel;
++              HashTable *ht = HASH_OF(val);
++
++              ZEND_HASH_FOREACH_VAL(ht, rr)
++              {
++                      if (Z_TYPE_P(rr) == IS_ARRAY) {
++                              if (2 == php_http_array_list(Z_ARRVAL_P(rr), 2, &rb, &re)) {
++                                      if (    ((Z_TYPE_P(rb) == IS_LONG) || ((Z_TYPE_P(rb) == IS_STRING) && is_numeric_string(Z_STRVAL_P(rb), Z_STRLEN_P(rb), &rbl, NULL, 1))) &&
++                                                      ((Z_TYPE_P(re) == IS_LONG) || ((Z_TYPE_P(re) == IS_STRING) && is_numeric_string(Z_STRVAL_P(re), Z_STRLEN_P(re), &rel, NULL, 1)))) {
++                                              if ((rbl >= 0) && (rel >= 0)) {
++                                                      php_http_buffer_appendf(&curl->options.ranges, "%ld-%ld,", rbl, rel);
+                                               }
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
+                                       }
+                               }
+                       }
+               }
++              ZEND_HASH_FOREACH_END();
+               if (curl->options.ranges.used) {
+                       curl->options.range_request = 1;
+                       /* ditch last comma */
+                       curl->options.ranges.data[curl->options.ranges.used - 1] = '\0';
+               }
+       }
+       if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RANGE, curl->options.ranges.data)) {
+               return FAILURE;
+       }
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curle_option_set_resume(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       CURL *ch = curl->handle;
+       if (Z_LVAL_P(val) > 0) {
+               curl->options.range_request = 1;
+       }
+       if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESUME_FROM, Z_LVAL_P(val))) {
+               return FAILURE;
+       }
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curle_option_set_retrydelay(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       curl->options.retry.delay = Z_DVAL_P(val);
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curle_option_set_retrycount(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       curl->options.retry.count = Z_LVAL_P(val);
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curle_option_set_redirect(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       CURL *ch = curl->handle;
+       if (    CURLE_OK != curl_easy_setopt(ch, CURLOPT_FOLLOWLOCATION, Z_LVAL_P(val) ? 1L : 0L)
+               ||      CURLE_OK != curl_easy_setopt(ch, CURLOPT_MAXREDIRS, curl->options.redirects = Z_LVAL_P(val))
+       ) {
+               return FAILURE;
+       }
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curle_option_set_portrange(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       CURL *ch = curl->handle;
+       long localport = 0, localportrange = 0;
 -              zval **z_port_start, *zps_copy = NULL, **z_port_end, *zpe_copy = NULL;
+       if (val && Z_TYPE_P(val) != IS_NULL) {
 -              switch (php_http_array_list(Z_ARRVAL_P(val) TSRMLS_CC, 2, &z_port_start, &z_port_end)) {
++              zval *zps, *zpe;
 -                      zps_copy = php_http_ztyp(IS_LONG, *z_port_start);
 -                      zpe_copy = php_http_ztyp(IS_LONG, *z_port_end);
 -                      localportrange = labs(Z_LVAL_P(zps_copy)-Z_LVAL_P(zpe_copy))+1L;
++              switch (php_http_array_list(Z_ARRVAL_P(val), 2, &zps, &zpe)) {
+               case 2:
 -                      if (!zps_copy) {
 -                              zps_copy = php_http_ztyp(IS_LONG, *z_port_start);
 -                      }
 -                      localport = (zpe_copy && Z_LVAL_P(zpe_copy) > 0) ? MIN(Z_LVAL_P(zps_copy), Z_LVAL_P(zpe_copy)) : Z_LVAL_P(zps_copy);
 -                      zval_ptr_dtor(&zps_copy);
 -                      if (zpe_copy) {
 -                              zval_ptr_dtor(&zpe_copy);
 -                      }
++                      localportrange = labs(zval_get_long(zps)-zval_get_long(zpe))+1L;
+                       /* no break */
+               case 1:
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
++                      localport = (zval_get_long(zpe) > 0) ? MIN(zval_get_long(zps), zval_get_long(zpe)) : zval_get_long(zps);
+                       break;
+               default:
+                       break;
+               }
+       }
+       if (    CURLE_OK != curl_easy_setopt(ch, CURLOPT_LOCALPORT, localport)
+               ||      CURLE_OK != curl_easy_setopt(ch, CURLOPT_LOCALPORTRANGE, localportrange)
+       ) {
+               return FAILURE;
+       }
+       return SUCCESS;
+ }
+ #if PHP_HTTP_CURL_VERSION(7,37,0)
+ static ZEND_RESULT_CODE php_http_curle_option_set_proxyheader(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
 -              php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0);
 -              zval **header_val, *header_cpy;
 -              HashPosition pos;
+       if (val && Z_TYPE_P(val) != IS_NULL) {
 -              FOREACH_KEYVAL(pos, val, header_key, header_val) {
 -                      if (header_key.type == HASH_KEY_IS_STRING) {
 -                              header_cpy = php_http_ztyp(IS_STRING, *header_val);
 -                              php_http_buffer_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy));
++              php_http_arrkey_t header_key;
++              zval *header_val;
+               php_http_buffer_t header;
+               php_http_buffer_init(&header);
 -                              zval_ptr_dtor(&header_cpy);
++              ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(val), header_key.h, header_key.key, header_val)
++              {
++                      if (header_key.key) {
++                              zend_string *zs = zval_get_string(header_val);
++
++                              php_http_buffer_appendf(&header, "%s: %s", header_key.key->val, zs->val);
++                              zend_string_release(zs);
++
+                               php_http_buffer_fix(&header);
+                               curl->options.proxyheaders = curl_slist_append(curl->options.proxyheaders, header.data);
+                               php_http_buffer_reset(&header);
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
+                       }
+               }
++              ZEND_HASH_FOREACH_END();
+               php_http_buffer_dtor(&header);
+       }
+       if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_PROXYHEADER, curl->options.proxyheaders)) {
+               return FAILURE;
+       }
+       if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_HEADEROPT, CURLHEADER_SEPARATE)) {
+               curl_easy_setopt(curl->handle, CURLOPT_PROXYHEADER, NULL);
+               return FAILURE;
+       }
+       return SUCCESS;
+ }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,21,3)
+ static ZEND_RESULT_CODE php_http_curle_option_set_resolve(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       CURL *ch = curl->handle;
 -              php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 -              HashPosition pos;
 -              zval **data;
+       if (val && Z_TYPE_P(val) != IS_NULL) {
 -              FOREACH_KEYVAL(pos, val, key, data) {
 -                      zval *cpy = php_http_ztyp(IS_STRING, *data);
 -                      curl->options.resolve = curl_slist_append(curl->options.resolve, Z_STRVAL_P(cpy));
 -                      zval_ptr_dtor(&cpy);
++              HashTable *ht = HASH_OF(val);
++              zval *data;
 -static void php_http_curle_options_init(php_http_options_t *registry TSRMLS_DC)
++              ZEND_HASH_FOREACH_VAL(ht, data)
++              {
++                      zend_string *zs = zval_get_string(data);
++                      curl->options.resolve = curl_slist_append(curl->options.resolve, zs->val);
++                      zend_string_release(zs);
+               }
++              ZEND_HASH_FOREACH_END();
+               if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESOLVE, curl->options.resolve)) {
+                       return FAILURE;
+               }
+       } else {
+               if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_RESOLVE, NULL)) {
+                       return FAILURE;
+               }
+       }
+       return SUCCESS;
+ }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
+ static ZEND_RESULT_CODE php_http_curle_option_set_ssl_tlsauthtype(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       CURL *ch = curl->handle;
+       if (val && Z_LVAL_P(val)) {
+               switch (Z_LVAL_P(val)) {
+               case CURL_TLSAUTH_SRP:
+                       if (CURLE_OK == curl_easy_setopt(ch, CURLOPT_TLSAUTH_TYPE, PHP_HTTP_CURL_TLSAUTH_SRP)) {
+                               return SUCCESS;
+                       }
+                       /* no break */
+               default:
+                       return FAILURE;
+               }
+       }
+       if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TLSAUTH_TYPE, PHP_HTTP_CURL_TLSAUTH_DEF)) {
+               return FAILURE;
+       }
+       return SUCCESS;
+ }
+ #endif
 -      php_http_option_register(registry, ZEND_STRL("proxytunnel"), CURLOPT_HTTPPROXYTUNNEL, IS_BOOL);
++static void php_http_curle_options_init(php_http_options_t *registry)
+ {
+       php_http_option_t *opt;
+       /* url options */
+ #if PHP_HTTP_CURL_VERSION(7,42,0)
+       php_http_option_register(registry, ZEND_STRL("path_as_is"), CURLOPT_PATH_AS_IS, IS_BOOL);
+ #endif
+       /* proxy */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("proxyhost"), CURLOPT_PROXY, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+       }
+       php_http_option_register(registry, ZEND_STRL("proxytype"), CURLOPT_PROXYTYPE, IS_LONG);
+       php_http_option_register(registry, ZEND_STRL("proxyport"), CURLOPT_PROXYPORT, IS_LONG);
+       if ((opt = php_http_option_register(registry, ZEND_STRL("proxyauth"), CURLOPT_PROXYUSERPWD, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+       }
+       if ((opt = php_http_option_register(registry, ZEND_STRL("proxyauthtype"), CURLOPT_PROXYAUTH, IS_LONG))) {
+               Z_LVAL(opt->defval) = CURLAUTH_ANYSAFE;
+       }
 -      php_http_option_register(registry, ZEND_STRL("fresh_connect"), CURLOPT_FRESH_CONNECT, IS_BOOL);
 -      php_http_option_register(registry, ZEND_STRL("forbid_reuse"), CURLOPT_FORBID_REUSE, IS_BOOL);
++      php_http_option_register(registry, ZEND_STRL("proxytunnel"), CURLOPT_HTTPPROXYTUNNEL, _IS_BOOL);
+ #if PHP_HTTP_CURL_VERSION(7,19,4)
+       php_http_option_register(registry, ZEND_STRL("noproxy"), CURLOPT_NOPROXY, IS_STRING);
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,37,0)
+       if ((opt = php_http_option_register(registry, ZEND_STRL("proxyheader"), CURLOPT_PROXYHEADER, IS_ARRAY))) {
+               opt->setter = php_http_curle_option_set_proxyheader;
+       }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,43,0)
+       if ((opt = php_http_option_register(registry, ZEND_STRL("proxy_service_name"), CURLOPT_PROXY_SERVICE_NAME, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+       }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,40,0)
+       if ((opt = php_http_option_register(registry, ZEND_STRL("unix_socket_path"), CURLOPT_UNIX_SOCKET_PATH, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
+       }
+ #endif
+       /* dns */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("dns_cache_timeout"), CURLOPT_DNS_CACHE_TIMEOUT, IS_LONG))) {
+               Z_LVAL(opt->defval) = 60;
+       }
+       php_http_option_register(registry, ZEND_STRL("ipresolve"), CURLOPT_IPRESOLVE, IS_LONG);
+ #if PHP_HTTP_CURL_VERSION(7,21,3)
+       if ((opt = php_http_option_register(registry, ZEND_STRL("resolve"), CURLOPT_RESOLVE, IS_ARRAY))) {
+               opt->setter = php_http_curle_option_set_resolve;
+       }
+ #endif
+ #if PHP_HTTP_HAVE_ARES
+ # if PHP_HTTP_CURL_VERSION(7,24,0)
+       if ((opt = php_http_option_register(registry, ZEND_STRL("dns_servers"), CURLOPT_DNS_SERVERS, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+       }
+ # endif
+ # if PHP_HTTP_CURL_VERSION(7,33,0)
+       if ((opt = php_http_option_register(registry, ZEND_STRL("dns_interface"), CURLOPT_DNS_INTERFACE, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+       }
+       if ((opt = php_http_option_register(registry, ZEND_STRL("dns_local_ip4"), CURLOPT_DNS_LOCAL_IP4, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+       }
+       if ((opt = php_http_option_register(registry, ZEND_STRL("dns_local_ip6"), CURLOPT_DNS_LOCAL_IP6, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+       }
+ # endif
+ #endif
+       /* limits */
+       php_http_option_register(registry, ZEND_STRL("low_speed_limit"), CURLOPT_LOW_SPEED_LIMIT, IS_LONG);
+       php_http_option_register(registry, ZEND_STRL("low_speed_time"), CURLOPT_LOW_SPEED_TIME, IS_LONG);
+       /* LSF weirdance
+       php_http_option_register(registry, ZEND_STRL("max_send_speed"), CURLOPT_MAX_SEND_SPEED_LARGE, IS_LONG);
+       php_http_option_register(registry, ZEND_STRL("max_recv_speed"), CURLOPT_MAX_RECV_SPEED_LARGE, IS_LONG);
+       */
+       /* connection handling */
+       /* crashes
+       if ((opt = php_http_option_register(registry, ZEND_STRL("maxconnects"), CURLOPT_MAXCONNECTS, IS_LONG))) {
+               Z_LVAL(opt->defval) = 5;
+       }
+       */
 -      php_http_option_register(registry, ZEND_STRL("unrestricted_auth"), CURLOPT_UNRESTRICTED_AUTH, IS_BOOL);
++      php_http_option_register(registry, ZEND_STRL("fresh_connect"), CURLOPT_FRESH_CONNECT, _IS_BOOL);
++      php_http_option_register(registry, ZEND_STRL("forbid_reuse"), CURLOPT_FORBID_REUSE, _IS_BOOL);
+       /* outgoing interface */
+       php_http_option_register(registry, ZEND_STRL("interface"), CURLOPT_INTERFACE, IS_STRING);
+       if ((opt = php_http_option_register(registry, ZEND_STRL("portrange"), CURLOPT_LOCALPORT, IS_ARRAY))) {
+               opt->setter = php_http_curle_option_set_portrange;
+       }
+       /* another endpoint port */
+       php_http_option_register(registry, ZEND_STRL("port"), CURLOPT_PORT, IS_LONG);
+       /* RFC4007 zone_id */
+ #if PHP_HTTP_CURL_VERSION(7,19,0)
+       php_http_option_register(registry, ZEND_STRL("address_scope"), CURLOPT_ADDRESS_SCOPE, IS_LONG);
+ #endif
+       /* auth */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("httpauth"), CURLOPT_USERPWD, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+       }
+       if ((opt = php_http_option_register(registry, ZEND_STRL("httpauthtype"), CURLOPT_HTTPAUTH, IS_LONG))) {
+               Z_LVAL(opt->defval) = CURLAUTH_ANYSAFE;
+       }
+ #if PHP_HTTP_CURL_VERSION(7,43,0)
+       if ((opt = php_http_option_register(registry, ZEND_STRL("service_name"), CURLOPT_SERVICE_NAME, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+       }
+ #endif
+       /* redirects */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("redirect"), CURLOPT_FOLLOWLOCATION, IS_LONG))) {
+               opt->setter = php_http_curle_option_set_redirect;
+       }
 -      if ((opt = php_http_option_register(registry, ZEND_STRL("autoreferer"), CURLOPT_AUTOREFERER, IS_BOOL))) {
++      php_http_option_register(registry, ZEND_STRL("unrestricted_auth"), CURLOPT_UNRESTRICTED_AUTH, _IS_BOOL);
+ #if PHP_HTTP_CURL_VERSION(7,19,1)
+       php_http_option_register(registry, ZEND_STRL("postredir"), CURLOPT_POSTREDIR, IS_LONG);
+ #endif
+       /* retries */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("retrycount"), 0, IS_LONG))) {
+               opt->setter = php_http_curle_option_set_retrycount;
+       }
+       if ((opt = php_http_option_register(registry, ZEND_STRL("retrydelay"), 0, IS_DOUBLE))) {
+               opt->setter = php_http_curle_option_set_retrydelay;
+       }
+       /* referer */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("referer"), CURLOPT_REFERER, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+       }
 -              ZVAL_STRING(&opt->defval,
++      if ((opt = php_http_option_register(registry, ZEND_STRL("autoreferer"), CURLOPT_AUTOREFERER, _IS_BOOL))) {
+               ZVAL_BOOL(&opt->defval, 1);
+       }
+       /* useragent */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("useragent"), CURLOPT_USERAGENT, IS_STRING))) {
+               /* don't check strlen, to allow sending no useragent at all */
 -                              "libcurl/" LIBCURL_VERSION
 -                      , 0);
++              ZVAL_PSTRING(&opt->defval,
+                               "PECL_HTTP/" PHP_PECL_HTTP_VERSION " "
+                               "PHP/" PHP_VERSION " "
 -      if ((opt = php_http_option_register(registry, ZEND_STRL("compress"), 0, IS_BOOL))) {
++                              "libcurl/" LIBCURL_VERSION);
+       }
+       /* resume */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("resume"), CURLOPT_RESUME_FROM, IS_LONG))) {
+               opt->setter = php_http_curle_option_set_resume;
+       }
+       /* ranges */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("range"), CURLOPT_RANGE, IS_ARRAY))) {
+               opt->setter = php_http_curle_option_set_range;
+       }
+       /* etag */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("etag"), 0, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+               opt->setter = php_http_curle_option_set_etag;
+       }
+       /* compression */
 -      if ((opt = php_http_option_register(registry, ZEND_STRL("encodecookies"), 0, IS_BOOL))) {
++      if ((opt = php_http_option_register(registry, ZEND_STRL("compress"), 0, _IS_BOOL))) {
+               opt->setter = php_http_curle_option_set_compress;
+       }
+       /* lastmodified */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("lastmodified"), 0, IS_LONG))) {
+               opt->setter = php_http_curle_option_set_lastmodified;
+       }
+       /* cookies */
 -      php_http_option_register(registry, ZEND_STRL("cookiesession"), CURLOPT_COOKIESESSION, IS_BOOL);
++      if ((opt = php_http_option_register(registry, ZEND_STRL("encodecookies"), 0, _IS_BOOL))) {
+               opt->setter = php_http_curle_option_set_encodecookies;
+               ZVAL_BOOL(&opt->defval, 1);
+       }
+       if ((opt = php_http_option_register(registry, ZEND_STRL("cookies"), 0, IS_ARRAY))) {
+               opt->setter = php_http_curle_option_set_cookies;
+       }
+       /* cookiesession, don't load session cookies from cookiestore */
 -      php_http_option_register(registry, ZEND_STRL("tcp_nodelay"), CURLOPT_TCP_NODELAY, IS_BOOL);
++      php_http_option_register(registry, ZEND_STRL("cookiesession"), CURLOPT_COOKIESESSION, _IS_BOOL);
+       /* cookiestore, read initial cookies from that file and store cookies back into that file */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("cookiestore"), 0, IS_STRING))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+               opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
+               opt->setter = php_http_curle_option_set_cookiestore;
+       }
+       /* maxfilesize */
+       php_http_option_register(registry, ZEND_STRL("maxfilesize"), CURLOPT_MAXFILESIZE, IS_LONG);
+       /* http protocol version */
+       php_http_option_register(registry, ZEND_STRL("protocol"), CURLOPT_HTTP_VERSION, IS_LONG);
+       /* timeouts */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("timeout"), CURLOPT_TIMEOUT_MS, IS_DOUBLE))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS;
+       }
+       if ((opt = php_http_option_register(registry, ZEND_STRL("connecttimeout"), CURLOPT_CONNECTTIMEOUT_MS, IS_DOUBLE))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS;
+               Z_DVAL(opt->defval) = 3;
+       }
+ #if PHP_HTTP_CURL_VERSION(7,36,0)
+       if ((opt = php_http_option_register(registry, ZEND_STRL("expect_100_timeout"), CURLOPT_EXPECT_100_TIMEOUT_MS, IS_DOUBLE))) {
+               opt->flags |= PHP_HTTP_CURLE_OPTION_TRANSFORM_MS;
+               Z_DVAL(opt->defval) = 1;
+       }
+ #endif
+       /* tcp */
 -      php_http_option_register(registry, ZEND_STRL("tcp_keepalive"), CURLOPT_TCP_KEEPALIVE, IS_BOOL);
++      php_http_option_register(registry, ZEND_STRL("tcp_nodelay"), CURLOPT_TCP_NODELAY, _IS_BOOL);
+ #if PHP_HTTP_CURL_VERSION(7,25,0)
 -                      ZVAL_STRING(&opt->defval, "PEM", 0);
++      php_http_option_register(registry, ZEND_STRL("tcp_keepalive"), CURLOPT_TCP_KEEPALIVE, _IS_BOOL);
+       if ((opt = php_http_option_register(registry, ZEND_STRL("tcp_keepidle"), CURLOPT_TCP_KEEPIDLE, IS_LONG))) {
+               Z_LVAL(opt->defval) = 60;
+       }
+       if ((opt = php_http_option_register(registry, ZEND_STRL("tcp_keepintvl"), CURLOPT_TCP_KEEPINTVL, IS_LONG))) {
+               Z_LVAL(opt->defval) = 60;
+       }
+ #endif
+       /* ssl */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("ssl"), 0, IS_ARRAY))) {
+               registry = &opt->suboptions;
+               if ((opt = php_http_option_register(registry, ZEND_STRL("cert"), CURLOPT_SSLCERT, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
+               }
+               if ((opt = php_http_option_register(registry, ZEND_STRL("certtype"), CURLOPT_SSLCERTTYPE, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
 -                      ZVAL_STRING(&opt->defval, "PEM", 0);
++                      ZVAL_PSTRING(&opt->defval, "PEM");
+               }
+               if ((opt = php_http_option_register(registry, ZEND_STRL("key"), CURLOPT_SSLKEY, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
+               }
+               if ((opt = php_http_option_register(registry, ZEND_STRL("keytype"), CURLOPT_SSLKEYTYPE, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
 -              if ((opt = php_http_option_register(registry, ZEND_STRL("verifypeer"), CURLOPT_SSL_VERIFYPEER, IS_BOOL))) {
++                      ZVAL_PSTRING(&opt->defval, "PEM");
+               }
+               if ((opt = php_http_option_register(registry, ZEND_STRL("keypasswd"), CURLOPT_SSLKEYPASSWD, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+               }
+               php_http_option_register(registry, ZEND_STRL("engine"), CURLOPT_SSLENGINE, IS_STRING);
+               php_http_option_register(registry, ZEND_STRL("version"), CURLOPT_SSLVERSION, IS_LONG);
 -              if ((opt = php_http_option_register(registry, ZEND_STRL("verifyhost"), CURLOPT_SSL_VERIFYHOST, IS_BOOL))) {
++              if ((opt = php_http_option_register(registry, ZEND_STRL("verifypeer"), CURLOPT_SSL_VERIFYPEER, _IS_BOOL))) {
+                       ZVAL_BOOL(&opt->defval, 1);
+               }
 -              php_http_option_register(registry, ZEND_STRL("verifystatus"), CURLOPT_SSL_VERIFYSTATUS, IS_BOOL);
++              if ((opt = php_http_option_register(registry, ZEND_STRL("verifyhost"), CURLOPT_SSL_VERIFYHOST, _IS_BOOL))) {
+                       ZVAL_BOOL(&opt->defval, 1);
+                       opt->setter = php_http_curle_option_set_ssl_verifyhost;
+               }
+ #if PHP_HTTP_CURL_VERSION(7,41,0)
 -                      ZVAL_STRING(&opt->defval, PHP_HTTP_CURL_CAINFO, 0);
++              php_http_option_register(registry, ZEND_STRL("verifystatus"), CURLOPT_SSL_VERIFYSTATUS, _IS_BOOL);
+ #endif
+               php_http_option_register(registry, ZEND_STRL("cipher_list"), CURLOPT_SSL_CIPHER_LIST, IS_STRING);
+               if ((opt = php_http_option_register(registry, ZEND_STRL("cainfo"), CURLOPT_CAINFO, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
+ #ifdef PHP_HTTP_CURL_CAINFO
 -              php_http_option_register(registry, ZEND_STRL("certinfo"), CURLOPT_CERTINFO, IS_BOOL);
++                      ZVAL_PSTRING(&opt->defval, PHP_HTTP_CURL_CAINFO);
+ #endif
+               }
+               if ((opt = php_http_option_register(registry, ZEND_STRL("capath"), CURLOPT_CAPATH, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
+               }
+               if ((opt = php_http_option_register(registry, ZEND_STRL("random_file"), CURLOPT_RANDOM_FILE, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
+               }
+               if ((opt = php_http_option_register(registry, ZEND_STRL("egdsocket"), CURLOPT_EGDSOCKET, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
+               }
+ #if PHP_HTTP_CURL_VERSION(7,19,0)
+               if ((opt = php_http_option_register(registry, ZEND_STRL("issuercert"), CURLOPT_ISSUERCERT, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
+               }
+ #     ifdef PHP_HTTP_HAVE_OPENSSL
+               if ((opt = php_http_option_register(registry, ZEND_STRL("crlfile"), CURLOPT_CRLFILE, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
+               }
+ #     endif
+ #endif
+ #if (PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)) || (PHP_HTTP_CURL_VERSION(7,34,0) && defined(PHP_HTTP_HAVE_NSS)) || (PHP_HTTP_CURL_VERSION(7,42,0) && defined(PHP_HTTP_HAVE_GNUTLS)) || (PHP_HTTP_CURL_VERSION(7,39,0) && defined(PHP_HTTP_HAVE_GSKIT))
 -              if ((opt = php_http_option_register(registry, ZEND_STRL("enable_npn"), CURLOPT_SSL_ENABLE_NPN, IS_BOOL))) {
++              if ((opt = php_http_option_register(registry, ZEND_STRL("certinfo"), CURLOPT_CERTINFO, _IS_BOOL))) {
++                      ZVAL_FALSE(&opt->defval);
++              }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,36,0)
 -              if ((opt = php_http_option_register(registry, ZEND_STRL("enable_alpn"), CURLOPT_SSL_ENABLE_ALPN, IS_BOOL))) {
++              if ((opt = php_http_option_register(registry, ZEND_STRL("enable_npn"), CURLOPT_SSL_ENABLE_NPN, _IS_BOOL))) {
+                       ZVAL_BOOL(&opt->defval, 1);
+               }
 -              php_http_option_register(registry, ZEND_STRL("falsestart"), CURLOPT_SSL_FALSESTART, IS_BOOL);
++              if ((opt = php_http_option_register(registry, ZEND_STRL("enable_alpn"), CURLOPT_SSL_ENABLE_ALPN, _IS_BOOL))) {
+                       ZVAL_BOOL(&opt->defval, 1);
+               }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,39,0)
+               /* FIXME: see http://curl.haxx.se/libcurl/c/CURLOPT_PINNEDPUBLICKEY.html#AVAILABILITY */
+               if ((opt = php_http_option_register(registry, ZEND_STRL("pinned_publickey"), CURLOPT_PINNEDPUBLICKEY, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
+               }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
+               if ((opt = php_http_option_register(registry, ZEND_STRL("tlsauthtype"), CURLOPT_TLSAUTH_TYPE, IS_LONG))) {
+                       opt->setter = php_http_curle_option_set_ssl_tlsauthtype;
+               }
+               if ((opt = php_http_option_register(registry, ZEND_STRL("tlsauthuser"), CURLOPT_TLSAUTH_USERNAME, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+               }
+               if ((opt = php_http_option_register(registry, ZEND_STRL("tlsauthpass"), CURLOPT_TLSAUTH_PASSWORD, IS_STRING))) {
+                       opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
+               }
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,42,0) && (defined(PHP_HTTP_HAVE_NSS) || defined(PHP_HTTP_HAVE_DARWINSSL))
 -              option = php_http_ztyp(opt->type, option);
 -              zend_hash_quick_update(&curl->options.cache, opt->name.s, opt->name.l, opt->name.h, &option, sizeof(zval *), NULL);
++              php_http_option_register(registry, ZEND_STRL("falsestart"), CURLOPT_SSL_FALSESTART, _IS_BOOL);
+ #endif
+       }
+ }
+ static zval *php_http_curle_get_option(php_http_option_t *opt, HashTable *options, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       zval *option;
+       if ((option = php_http_option_get(opt, options, NULL))) {
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
++              zval zopt;
++
++              ZVAL_DUP(&zopt, option);
++              convert_to_explicit_type(&zopt, opt->type);
++              zend_hash_update(&curl->options.cache, opt->name, &zopt);
++              return zend_hash_find(&curl->options.cache, opt->name);
+       }
+       return option;
+ }
+ static ZEND_RESULT_CODE php_http_curle_set_option(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_curl_handler_t *curl = userdata;
+       CURL *ch = curl->handle;
+       zval tmp;
+       CURLcode rc = CURLE_OK;
+       ZEND_RESULT_CODE rv = SUCCESS;
 -      case IS_BOOL:
+       if (!val) {
+               val = &opt->defval;
+       }
+       switch (opt->type) {
 -              } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) Z_BVAL_P(val))) {
++      case _IS_BOOL:
+               if (opt->setter) {
+                       rv = opt->setter(opt, val, curl);
 -              } else if ((opt->flags & PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR) && Z_STRVAL_P(val) && SUCCESS != php_check_open_basedir(Z_STRVAL_P(val) TSRMLS_CC)) {
++              } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) (Z_TYPE_P(val) == IS_TRUE))) {
+                       rv = FAILURE;
+               }
+               break;
+       case IS_LONG:
+               if (opt->setter) {
+                       rv = opt->setter(opt, val, curl);
+               } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, Z_LVAL_P(val))) {
+                       rv = FAILURE;
+               }
+               break;
+       case IS_STRING:
+               if (opt->setter) {
+                       rv = opt->setter(opt, val, curl);
++              } else if (!val || Z_TYPE_P(val) == IS_NULL) {
++                      if (CURLE_OK != (rc = curl_easy_setopt(ch, opt->option, NULL))) {
++                              rv = FAILURE;
++                      }
+               } else if ((opt->flags & PHP_HTTP_CURLE_OPTION_CHECK_STRLEN) && !Z_STRLEN_P(val)) {
+                       if (CURLE_OK != (rc = curl_easy_setopt(ch, opt->option, NULL))) {
+                               rv = FAILURE;
+                       }
 -              php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not set option %s (%s)", opt->name.s, curl_easy_strerror(rc));
++              } else if ((opt->flags & PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR) && Z_STRVAL_P(val) && SUCCESS != php_check_open_basedir(Z_STRVAL_P(val))) {
+                       if (CURLE_OK != (rc = curl_easy_setopt(ch, opt->option, NULL))) {
+                               rv = FAILURE;
+                       }
+               } else if (CURLE_OK != (rc = curl_easy_setopt(ch, opt->option, Z_STRVAL_P(val)))) {
+                       rv = FAILURE;
+               }
+               break;
+       case IS_DOUBLE:
+               if (opt->flags & PHP_HTTP_CURLE_OPTION_TRANSFORM_MS) {
+                       tmp = *val;
+                       Z_DVAL(tmp) *= 1000;
+                       val = &tmp;
+               }
+               if (opt->setter) {
+                       rv = opt->setter(opt, val, curl);
+               } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) Z_DVAL_P(val))) {
+                       rv = FAILURE;
+               }
+               break;
+       case IS_ARRAY:
+               if (opt->setter) {
+                       rv = opt->setter(opt, val, curl);
+               } else if (Z_TYPE_P(val) != IS_NULL) {
+                       rv = php_http_options_apply(&opt->suboptions, Z_ARRVAL_P(val), curl);
+               }
+               break;
+       default:
+               if (opt->setter) {
+                       rv = opt->setter(opt, val, curl);
+               } else {
+                       rv = FAILURE;
+               }
+               break;
+       }
+       if (rv != SUCCESS) {
 -      TSRMLS_FETCH_FROM_CTX(client->ts);
++              php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_easy_strerror(rc));
+       }
+       return rv;
+ }
+ #if PHP_HTTP_CURL_VERSION(7,30,0)
+ static ZEND_RESULT_CODE php_http_curlm_option_set_pipelining_bl(php_http_option_t *opt, zval *value, void *userdata)
+ {
+       php_http_client_t *client = userdata;
+       php_http_client_curl_t *curl = client->ctx;
+       CURLM *ch = curl->handle;
+       HashTable tmp_ht;
+       char **bl = NULL;
 -              zval **entry;
 -              HashPosition pos;
+       /* array of char *, ending with a NULL */
+       if (value && Z_TYPE_P(value) != IS_NULL) {
 -              FOREACH_HASH_VAL(pos, &tmp_ht, entry) {
 -                      *ptr++ = Z_STRVAL_PP(entry);
++              zval *entry;
+               HashTable *ht = HASH_OF(value);
+               int c = zend_hash_num_elements(ht);
+               char **ptr = ecalloc(c + 1, sizeof(char *));
+               bl = ptr;
+               zend_hash_init(&tmp_ht, c, NULL, ZVAL_PTR_DTOR, 0);
+               array_join(ht, &tmp_ht, 0, ARRAY_JOIN_STRINGIFY);
 -      return php_http_curlm_use_eventloop(client, value && Z_BVAL_P(value));
++              ZEND_HASH_FOREACH_VAL(&tmp_ht, entry)
++              {
++                      *ptr++ = Z_STRVAL_P(entry);
+               }
++              ZEND_HASH_FOREACH_END();
+       }
+       if (CURLM_OK != curl_multi_setopt(ch, opt->option, bl)) {
+               if (bl) {
+                       efree(bl);
+                       zend_hash_destroy(&tmp_ht);
+               }
+               return FAILURE;
+       }
+       if (bl) {
+               efree(bl);
+               zend_hash_destroy(&tmp_ht);
+       }
+       return SUCCESS;
+ }
+ #endif
+ #if PHP_HTTP_HAVE_EVENT
+ static inline ZEND_RESULT_CODE php_http_curlm_use_eventloop(php_http_client_t *h, zend_bool enable)
+ {
+       php_http_client_curl_t *curl = h->ctx;
+       if ((curl->useevents = enable)) {
+               if (!curl->evbase) {
+                       curl->evbase = event_base_new();
+               }
+               if (!curl->timeout) {
+                       curl->timeout = ecalloc(1, sizeof(struct event));
+               }
+               curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, h);
+               curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, php_http_curlm_socket_callback);
+               curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, h);
+               curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, php_http_curlm_timer_callback);
+       } else {
+               curl_multi_setopt(curl->handle, CURLMOPT_SOCKETDATA, NULL);
+               curl_multi_setopt(curl->handle, CURLMOPT_SOCKETFUNCTION, NULL);
+               curl_multi_setopt(curl->handle, CURLMOPT_TIMERDATA, NULL);
+               curl_multi_setopt(curl->handle, CURLMOPT_TIMERFUNCTION, NULL);
+       }
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_curlm_option_set_use_eventloop(php_http_option_t *opt, zval *value, void *userdata)
+ {
+       php_http_client_t *client = userdata;
 -static void php_http_curlm_options_init(php_http_options_t *registry TSRMLS_DC)
++      return php_http_curlm_use_eventloop(client, value && Z_TYPE_P(value) == IS_TRUE);
+ }
+ #endif
 -      php_http_option_register(registry, ZEND_STRL("pipelining"), CURLMOPT_PIPELINING, IS_BOOL);
++static void php_http_curlm_options_init(php_http_options_t *registry)
+ {
+       php_http_option_t *opt;
+       /* set size of connection cache */
+       if ((opt = php_http_option_register(registry, ZEND_STRL("maxconnects"), CURLMOPT_MAXCONNECTS, IS_LONG))) {
+               /* -1 == default, 0 == unlimited */
+               ZVAL_LONG(&opt->defval, -1);
+       }
+       /* set max number of connections to a single host */
+ #if PHP_HTTP_CURL_VERSION(7,30,0)
+       php_http_option_register(registry, ZEND_STRL("max_host_connections"), CURLMOPT_MAX_HOST_CONNECTIONS, IS_LONG);
+ #endif
+       /* maximum number of requests in a pipeline */
+ #if PHP_HTTP_CURL_VERSION(7,30,0)
+       if ((opt = php_http_option_register(registry, ZEND_STRL("max_pipeline_length"), CURLMOPT_MAX_PIPELINE_LENGTH, IS_LONG))) {
+               ZVAL_LONG(&opt->defval, 5);
+       }
+ #endif
+       /* max simultaneously open connections */
+ #if PHP_HTTP_CURL_VERSION(7,30,0)
+       php_http_option_register(registry, ZEND_STRL("max_total_connections"), CURLMOPT_MAX_TOTAL_CONNECTIONS, IS_LONG);
+ #endif
+       /* enable/disable HTTP pipelining */
 -      if ((opt = php_http_option_register(registry, ZEND_STRL("use_eventloop"), 0, IS_BOOL))) {
++      php_http_option_register(registry, ZEND_STRL("pipelining"), CURLMOPT_PIPELINING, _IS_BOOL);
+       /* chunk length threshold for pipelining */
+ #if PHP_HTTP_CURL_VERSION(7,30,0)
+       php_http_option_register(registry, ZEND_STRL("chunk_length_penalty_size"), CURLMOPT_CHUNK_LENGTH_PENALTY_SIZE, IS_LONG);
+ #endif
+       /* size threshold for pipelining penalty */
+ #if PHP_HTTP_CURL_VERSION(7,30,0)
+       php_http_option_register(registry, ZEND_STRL("content_length_penalty_size"), CURLMOPT_CONTENT_LENGTH_PENALTY_SIZE, IS_LONG);
+ #endif
+       /* pipelining server blacklist */
+ #if PHP_HTTP_CURL_VERSION(7,30,0)
+       if ((opt = php_http_option_register(registry, ZEND_STRL("pipelining_server_bl"), CURLMOPT_PIPELINING_SERVER_BL, IS_ARRAY))) {
+               opt->setter = php_http_curlm_option_set_pipelining_bl;
+       }
+ #endif
+       /* pipelining host blacklist */
+ #if PHP_HTTP_CURL_VERSION(7,30,0)
+       if ((opt = php_http_option_register(registry, ZEND_STRL("pipelining_site_bl"), CURLMOPT_PIPELINING_SITE_BL, IS_ARRAY))) {
+               opt->setter = php_http_curlm_option_set_pipelining_bl;
+       }
+ #endif
+       /* events */
+ #if PHP_HTTP_HAVE_EVENT
 -      TSRMLS_FETCH_FROM_CTX(client->ts);
++      if ((opt = php_http_option_register(registry, ZEND_STRL("use_eventloop"), 0, _IS_BOOL))) {
+               opt->setter = php_http_curlm_option_set_use_eventloop;
+       }
+ #endif
+ }
+ static ZEND_RESULT_CODE php_http_curlm_set_option(php_http_option_t *opt, zval *val, void *userdata)
+ {
+       php_http_client_t *client = userdata;
+       php_http_client_curl_t *curl = client->ctx;
+       CURLM *ch = curl->handle;
+       zval *orig = val;
+       CURLMcode rc = CURLM_UNKNOWN_OPTION;
+       ZEND_RESULT_CODE rv = SUCCESS;
 -              val = php_http_ztyp(opt->type, val);
+       if (!val) {
+               val = &opt->defval;
+       } else if (opt->type && Z_TYPE_P(val) != opt->type && !(Z_TYPE_P(val) == IS_NULL && opt->type == IS_ARRAY)) {
 -              case IS_BOOL:
 -                      if (CURLM_OK != (rc = curl_multi_setopt(ch, opt->option, (long) Z_BVAL_P(val)))) {
++              zval zopt;
++
++              ZVAL_DUP(&zopt, val);
++              convert_to_explicit_type(&zopt, opt->type);
++
++              val = &zopt;
+       }
+       if (opt->setter) {
+               rv = opt->setter(opt, val, client);
+       } else {
+               switch (opt->type) {
 -              zval_ptr_dtor(&val);
++              case _IS_BOOL:
++                      if (CURLM_OK != (rc = curl_multi_setopt(ch, opt->option, (long) zend_is_true(val)))) {
+                               rv = FAILURE;
+                       }
+                       break;
+               case IS_LONG:
+                       if (CURLM_OK != (rc = curl_multi_setopt(ch, opt->option, Z_LVAL_P(val)))) {
+                               rv = FAILURE;
+                       }
+                       break;
+               default:
+                       rv = FAILURE;
+                       break;
+               }
+       }
+       if (val && val != orig && val != &opt->defval) {
 -              php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not set option %s (%s)", opt->name.s, curl_easy_strerror(rc));
++              zval_ptr_dtor(val);
+       }
+       if (rv != SUCCESS) {
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
++              php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_easy_strerror(rc));
+       }
+       return rv;
+ }
+ /* client ops */
+ static ZEND_RESULT_CODE php_http_client_curl_handler_reset(php_http_client_curl_handler_t *curl)
+ {
+       CURL *ch = curl->handle;
+       php_http_curle_storage_t *st;
+       if ((st = php_http_curle_get_storage(ch))) {
+               if (st->url) {
+                       pefree(st->url, 1);
+                       st->url = NULL;
+               }
+               if (st->cookiestore) {
+                       pefree(st->cookiestore, 1);
+                       st->cookiestore = NULL;
+               }
+               st->errorbuffer[0] = '\0';
+       }
+       curl_easy_setopt(ch, CURLOPT_URL, NULL);
+       curl_easy_setopt(ch, CURLOPT_CUSTOMREQUEST, NULL);
+       curl_easy_setopt(ch, CURLOPT_HTTPGET, 1L);
+       curl_easy_setopt(ch, CURLOPT_NOBODY, 0L);
+       /* libcurl < 7.19.6 does not clear auth info with USERPWD set to NULL */
+ #if PHP_HTTP_CURL_VERSION(7,19,1)
+       curl_easy_setopt(ch, CURLOPT_PROXYUSERNAME, NULL);
+       curl_easy_setopt(ch, CURLOPT_PROXYPASSWORD, NULL);
+       curl_easy_setopt(ch, CURLOPT_USERNAME, NULL);
+       curl_easy_setopt(ch, CURLOPT_PASSWORD, NULL);
+ #endif
+ #if PHP_HTTP_CURL_VERSION(7,21,3)
+       if (curl->options.resolve) {
+               curl_slist_free_all(curl->options.resolve);
+               curl->options.resolve = NULL;
+       }
+ #endif
+       curl->options.retry.count = 0;
+       curl->options.retry.delay = 0;
+       curl->options.redirects = 0;
+       curl->options.encode_cookies = 1;
+       if (curl->options.headers) {
+               curl_slist_free_all(curl->options.headers);
+               curl->options.headers = NULL;
+       }
+       if (curl->options.proxyheaders) {
+               curl_slist_free_all(curl->options.proxyheaders);
+               curl->options.proxyheaders = NULL;
+       }
+       php_http_buffer_reset(&curl->options.cookies);
+       php_http_buffer_reset(&curl->options.ranges);
+       return SUCCESS;
+ }
+ static php_http_client_curl_handler_t *php_http_client_curl_handler_init(php_http_client_t *h, php_resource_factory_t *rf)
+ {
+       void *handle;
+       php_http_client_curl_handler_t *handler;
 -      if (!(handle = php_resource_factory_handle_ctor(rf, NULL TSRMLS_CC))) {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to initialize curl handle");
 -      handler->response.body = php_http_message_body_init(NULL, NULL TSRMLS_CC);
++      if (!(handle = php_resource_factory_handle_ctor(rf, NULL))) {
++              php_error_docref(NULL, E_WARNING, "Failed to initialize curl handle");
+               return NULL;
+       }
+       handler = ecalloc(1, sizeof(*handler));
+       handler->rf = rf;
+       handler->client = h;
+       handler->handle = handle;
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
++      handler->response.body = php_http_message_body_init(NULL, NULL);
+       php_http_buffer_init(&handler->response.headers);
+       php_http_buffer_init(&handler->options.cookies);
+       php_http_buffer_init(&handler->options.ranges);
+       zend_hash_init(&handler->options.cache, 0, NULL, ZVAL_PTR_DTOR, 0);
+ #if defined(ZTS)
+       curl_easy_setopt(handle, CURLOPT_NOSIGNAL, 1L);
+ #endif
+       curl_easy_setopt(handle, CURLOPT_HEADER, 0L);
+       curl_easy_setopt(handle, CURLOPT_FILETIME, 1L);
+       curl_easy_setopt(handle, CURLOPT_AUTOREFERER, 1L);
+       curl_easy_setopt(handle, CURLOPT_VERBOSE, 1L);
+       curl_easy_setopt(handle, CURLOPT_NOPROGRESS, 0L);
+       curl_easy_setopt(handle, CURLOPT_HEADERFUNCTION, php_http_curle_header_callback);
+       curl_easy_setopt(handle, CURLOPT_WRITEFUNCTION, php_http_curle_body_callback);
+       curl_easy_setopt(handle, CURLOPT_DEBUGFUNCTION, php_http_curle_raw_callback);
+       curl_easy_setopt(handle, CURLOPT_READFUNCTION, php_http_curle_read_callback);
+       curl_easy_setopt(handle, CURLOPT_SEEKFUNCTION, php_http_curle_seek_callback);
+ #if PHP_HTTP_CURL_VERSION(7,32,0)
+       curl_easy_setopt(handle, CURLOPT_XFERINFOFUNCTION, php_http_curle_xferinfo_callback);
+       curl_easy_setopt(handle, CURLOPT_XFERINFODATA, handler);
+ #else
+       curl_easy_setopt(handle, CURLOPT_PROGRESSFUNCTION, php_http_curle_progress_callback);
+       curl_easy_setopt(handle, CURLOPT_PROGRESSDATA, handler);
+ #endif
+       curl_easy_setopt(handle, CURLOPT_DEBUGDATA, handler);
+       curl_easy_setopt(handle, CURLOPT_WRITEDATA, handler);
+       curl_easy_setopt(handle, CURLOPT_HEADERDATA, handler);
+       php_http_client_curl_handler_reset(handler);
+       return handler;
+ }
+ static ZEND_RESULT_CODE php_http_client_curl_handler_prepare(php_http_client_curl_handler_t *curl, php_http_client_enqueue_t *enqueue)
+ {
+       size_t body_size;
+       php_http_message_t *msg = enqueue->request;
+       php_http_curle_storage_t *storage = php_http_curle_get_storage(curl->handle);
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot request empty URL");
+       /* request url */
+       if (!PHP_HTTP_INFO(msg).request.url) {
 -              php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0);
 -              zval **header_val, *header_cpy;
 -              HashPosition pos;
++              php_error_docref(NULL, E_WARNING, "Cannot request empty URL");
+               return FAILURE;
+       }
+       storage->errorbuffer[0] = '\0';
+       if (storage->url) {
+               pefree(storage->url, 1);
+       }
+       php_http_url_to_string(PHP_HTTP_INFO(msg).request.url, &storage->url, NULL, 1);
+       curl_easy_setopt(curl->handle, CURLOPT_URL, storage->url);
+       /* apply options */
+       php_http_options_apply(&php_http_curle_options, enqueue->options, curl);
+       /* request headers */
+       php_http_message_update_headers(msg);
+       if (zend_hash_num_elements(&msg->hdrs)) {
 -              zval **ct = NULL;
 -
 -              zend_hash_find(&msg->hdrs, ZEND_STRS("Content-Length"), (void *) &ct);
++              php_http_arrkey_t header_key;
++              zval *header_val;
++              zend_string *header_str;
+               php_http_buffer_t header;
+ #if !PHP_HTTP_CURL_VERSION(7,23,0)
 -              FOREACH_HASH_KEYVAL(pos, &msg->hdrs, header_key, header_val) {
 -                      if (header_key.type == HASH_KEY_IS_STRING) {
++              zval *ct = zend_hash_str_find(&msg->hdrs, ZEND_STRL("Content-Length"));
+ #endif
+               php_http_buffer_init(&header);
 -                              if (ct && *ct == *header_val) {
++              ZEND_HASH_FOREACH_KEY_VAL(&msg->hdrs, header_key.h, header_key.key, header_val)
++              {
++                      if (header_key.key) {
+ #if !PHP_HTTP_CURL_VERSION(7,23,0)
+                               /* avoid duplicate content-length header */
 -                              header_cpy = php_http_ztyp(IS_STRING, *header_val);
 -                              php_http_buffer_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(header_cpy));
++                              if (ct && ct == header_val) {
+                                       continue;
+                               }
+ #endif
 -
 -                              zval_ptr_dtor(&header_cpy);
++                              header_str = zval_get_string(header_val);
++                              php_http_buffer_appendf(&header, "%s: %s", header_key.key->val, header_str->val);
+                               php_http_buffer_fix(&header);
+                               curl->options.headers = curl_slist_append(curl->options.headers, header.data);
+                               php_http_buffer_reset(&header);
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot use empty request method");
++                              zend_string_release(header_str);
+                       }
+               }
++              ZEND_HASH_FOREACH_END();
+               php_http_buffer_dtor(&header);
+       }
+       curl_easy_setopt(curl->handle, CURLOPT_HTTPHEADER, curl->options.headers);
+       /* attach request body */
+       if ((body_size = php_http_message_body_size(msg->body))) {
+               /* RFC2616, section 4.3 (para. 4) states that »a message-body MUST NOT be included in a request if the
+                * specification of the request method (section 5.1.1) does not allow sending an entity-body in request.«
+                * Following the clause in section 5.1.1 (para. 2) that request methods »MUST be implemented with the
+                * same semantics as those specified in section 9« reveal that not any single defined HTTP/1.1 method
+                * does not allow a request body.
+                */
+               php_stream_rewind(php_http_message_body_stream(msg->body));
+               curl_easy_setopt(curl->handle, CURLOPT_SEEKDATA, msg->body);
+               curl_easy_setopt(curl->handle, CURLOPT_READDATA, msg->body);
+               curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, body_size);
+               curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, body_size);
+               curl_easy_setopt(curl->handle, CURLOPT_POST, 1L);
+       } else {
+               curl_easy_setopt(curl->handle, CURLOPT_SEEKDATA, NULL);
+               curl_easy_setopt(curl->handle, CURLOPT_READDATA, NULL);
+               curl_easy_setopt(curl->handle, CURLOPT_INFILESIZE, 0L);
+               curl_easy_setopt(curl->handle, CURLOPT_POSTFIELDSIZE, 0L);
+       }
+       /*
+        * Always use CUSTOMREQUEST, else curl won't send any request body for GET etc.
+        * See e.g. bug #69313.
+        *
+        * Here's what curl does:
+        * - CURLOPT_HTTPGET: ignore request body
+        * - CURLOPT_UPLOAD: set "Expect: 100-continue" header
+        * - CURLOPT_POST: set "Content-Type: application/x-www-form-urlencoded" header
+        * Now select the least bad.
+        *
+        * See also https://tools.ietf.org/html/rfc7231#section-5.1.1
+        */
+       if (PHP_HTTP_INFO(msg).request.method) {
+               switch(php_http_select_str(PHP_HTTP_INFO(msg).request.method, 2, "HEAD", "PUT")) {
+               case 0:
+                       curl_easy_setopt(curl->handle, CURLOPT_NOBODY, 1L);
+                       break;
+               case 1:
+                       curl_easy_setopt(curl->handle, CURLOPT_UPLOAD, 1L);
+                       break;
+               default:
+                       curl_easy_setopt(curl->handle, CURLOPT_CUSTOMREQUEST, PHP_HTTP_INFO(msg).request.method);
+               }
+       } else {
 -      TSRMLS_FETCH_FROM_CTX(handler->client->ts);
 -
++              php_error_docref(NULL, E_WARNING, "Cannot use empty request method");
+               return FAILURE;
+       }
+       return SUCCESS;
+ }
+ static void php_http_client_curl_handler_clear(php_http_client_curl_handler_t *handler)
+ {
+       curl_easy_setopt(handler->handle, CURLOPT_NOPROGRESS, 1L);
+ #if PHP_HTTP_CURL_VERSION(7,32,0)
+       curl_easy_setopt(handler->handle, CURLOPT_XFERINFOFUNCTION, NULL);
+ #else
+       curl_easy_setopt(handler->handle, CURLOPT_PROGRESSFUNCTION, NULL);
+ #endif
+       curl_easy_setopt(handler->handle, CURLOPT_VERBOSE, 0L);
+       curl_easy_setopt(handler->handle, CURLOPT_DEBUGFUNCTION, NULL);
+ }
+ static void php_http_client_curl_handler_dtor(php_http_client_curl_handler_t *handler)
+ {
 -      php_resource_factory_handle_dtor(handler->rf, handler->handle TSRMLS_CC);
+       php_http_client_curl_handler_clear(handler);
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
++      php_resource_factory_handle_dtor(handler->rf, handler->handle);
+       php_resource_factory_free(&handler->rf);
+       php_http_message_body_free(&handler->response.body);
+       php_http_buffer_dtor(&handler->response.headers);
+       php_http_buffer_dtor(&handler->options.ranges);
+       php_http_buffer_dtor(&handler->options.cookies);
+       zend_hash_destroy(&handler->options.cache);
++#if PHP_HTTP_CURL_VERSION(7,21,3)
++      if (handler->options.resolve) {
++              curl_slist_free_all(handler->options.resolve);
++              handler->options.resolve = NULL;
++      }
++#endif
++
+       if (handler->options.headers) {
+               curl_slist_free_all(handler->options.headers);
+               handler->options.headers = NULL;
+       }
++      if (handler->options.proxyheaders) {
++              curl_slist_free_all(handler->options.proxyheaders);
++              handler->options.proxyheaders = NULL;
++      }
++
+       efree(handler);
+ }
+ static php_http_client_t *php_http_client_curl_init(php_http_client_t *h, void *handle)
+ {
+       php_http_client_curl_t *curl;
 -      if (!handle && !(handle = php_resource_factory_handle_ctor(h->rf, NULL TSRMLS_CC))) {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to initialize curl handle");
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
++      if (!handle && !(handle = php_resource_factory_handle_ctor(h->rf, NULL))) {
++              php_error_docref(NULL, E_WARNING, "Failed to initialize curl handle");
+               return NULL;
+       }
+       curl = ecalloc(1, sizeof(*curl));
+       curl->handle = handle;
+       curl->unfinished = 0;
+       h->ctx = curl;
+       return h;
+ }
+ static void php_http_client_curl_dtor(php_http_client_t *h)
+ {
+       php_http_client_curl_t *curl = h->ctx;
 -      php_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC);
+ #if PHP_HTTP_HAVE_EVENT
+       if (curl->timeout) {
+               if (event_initialized(curl->timeout) && event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
+                       event_del(curl->timeout);
+               }
+               efree(curl->timeout);
+               curl->timeout = NULL;
+       }
+       if (curl->evbase) {
+               event_base_free(curl->evbase);
+               curl->evbase = NULL;
+       }
+ #endif
+       curl->unfinished = 0;
 -static php_resource_factory_t *create_rf(php_http_client_t *h, php_http_client_enqueue_t *enqueue TSRMLS_DC)
++      php_resource_factory_handle_dtor(h->rf, curl->handle);
+       efree(curl);
+       h->ctx = NULL;
+ }
+ static void queue_dtor(php_http_client_enqueue_t *e)
+ {
+       php_http_client_curl_handler_t *handler = e->opaque;
+       if (handler->queue.dtor) {
+               e->opaque = handler->queue.opaque;
+               handler->queue.dtor(e);
+       }
+       php_http_client_curl_handler_dtor(handler);
+ }
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot request empty URL");
++static php_resource_factory_t *create_rf(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
+ {
+       php_persistent_handle_factory_t *pf = NULL;
+       php_resource_factory_t *rf = NULL;
+       php_http_url_t *url = enqueue->request->http.info.request.url;
+       if (!url || (!url->host && !url->path)) {
 -              zval **zport;
++              php_error_docref(NULL, E_WARNING, "Cannot request empty URL");
+               return NULL;
+       }
+       /* only if the client itself is setup for persistence */
+       if (php_resource_factory_is_persistent(h->rf)) {
++              zend_string *id;
+               char *id_str = NULL;
+               size_t id_len;
+               int port = url->port ? url->port : 80;
 -              if (SUCCESS == zend_hash_find(enqueue->options, ZEND_STRS("port"), (void *) &zport)) {
 -                      zval *zcpy = php_http_ztyp(IS_LONG, *zport);
++              zval *zport;
 -                      if (Z_LVAL_P(zcpy)) {
 -                              port = Z_LVAL_P(zcpy);
++              if ((zport = zend_hash_str_find(enqueue->options, ZEND_STRL("port")))) {
++                      zend_long lport = zval_get_long(zport);
 -                      zval_ptr_dtor(&zcpy);
++                      if (lport > 0) {
++                              port = lport;
+                       }
 -              pf = php_persistent_handle_concede(NULL, ZEND_STRL("http\\Client\\Curl\\Request"), id_str, id_len, NULL, NULL TSRMLS_CC);
 -              efree(id_str);
+               }
+               id_len = spprintf(&id_str, 0, "%s:%d", STR_PTR(url->host), port);
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
++              id = php_http_cs2zs(id_str, id_len);
++              pf = php_persistent_handle_concede(NULL, PHP_HTTP_G->client.curl.driver.request_name, id, NULL, NULL);
++              zend_string_release(id);
+       }
+       if (pf) {
+               rf = php_persistent_handle_resource_factory_init(NULL, pf);
+       } else {
+               rf = php_resource_factory_init(NULL, &php_http_curle_resource_factory_ops, NULL, NULL);
+       }
+       return rf;
+ }
+ static ZEND_RESULT_CODE php_http_client_curl_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
+ {
+       CURLMcode rs;
+       php_http_client_curl_t *curl = h->ctx;
+       php_http_client_curl_handler_t *handler;
+       php_http_client_progress_state_t *progress;
+       php_resource_factory_t *rf;
 -      rf = create_rf(h, enqueue TSRMLS_CC);
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not enqueue request: %s", curl_multi_strerror(rs));
++      rf = create_rf(h, enqueue);
+       if (!rf) {
+               return FAILURE;
+       }
+       handler = php_http_client_curl_handler_init(h, rf);
+       if (!handler) {
+               return FAILURE;
+       }
+       if (SUCCESS != php_http_client_curl_handler_prepare(handler, enqueue)) {
+               php_http_client_curl_handler_dtor(handler);
+               return FAILURE;
+       }
+       handler->queue = *enqueue;
+       enqueue->opaque = handler;
+       enqueue->dtor = queue_dtor;
+       if (CURLM_OK == (rs = curl_multi_add_handle(curl->handle, handler->handle))) {
+               zend_llist_add_element(&h->requests, enqueue);
+               ++curl->unfinished;
+               if (h->callback.progress.func && SUCCESS == php_http_client_getopt(h, PHP_HTTP_CLIENT_OPT_PROGRESS_INFO, enqueue->request, &progress)) {
+                       progress->info = "start";
+                       h->callback.progress.func(h->callback.progress.arg, h, &handler->queue, progress);
+                       progress->started = 1;
+               }
+               return SUCCESS;
+       } else {
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
++              php_error_docref(NULL, E_WARNING, "Could not enqueue request: %s", curl_multi_strerror(rs));
+               return FAILURE;
+       }
+ }
+ static ZEND_RESULT_CODE php_http_client_curl_dequeue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
+ {
+       CURLMcode rs;
+       php_http_client_curl_t *curl = h->ctx;
+       php_http_client_curl_handler_t *handler = enqueue->opaque;
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not dequeue request: %s", curl_multi_strerror(rs));
+       php_http_client_curl_handler_clear(handler);
+       if (CURLM_OK == (rs = curl_multi_remove_handle(curl->handle, handler->handle))) {
+               zend_llist_del_element(&h->requests, handler->handle, (int (*)(void *, void *)) compare_queue);
+               return SUCCESS;
+       } else {
 -#endif
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
++              php_error_docref(NULL, E_WARNING, "Could not dequeue request: %s", curl_multi_strerror(rs));
+       }
+       return FAILURE;
+ }
+ static void php_http_client_curl_reset(php_http_client_t *h)
+ {
+       zend_llist_element *next_el, *this_el;
+       for (this_el = h->requests.head; this_el; this_el = next_el) {
+               next_el = this_el->next;
+               php_http_client_curl_dequeue(h, (void *) this_el->data);
+       }
+ }
+ static inline void php_http_client_curl_get_timeout(php_http_client_curl_t *curl, long max_tout, struct timeval *timeout)
+ {
+       if ((CURLM_OK == curl_multi_timeout(curl->handle, &max_tout)) && (max_tout > 0)) {
+               timeout->tv_sec = max_tout / 1000;
+               timeout->tv_usec = (max_tout % 1000) * 1000;
+       } else {
+               timeout->tv_sec = 0;
+               timeout->tv_usec = 1000;
+       }
+ }
+ #ifdef PHP_WIN32
+ #     define SELECT_ERROR SOCKET_ERROR
+ #else
+ #     define SELECT_ERROR -1
+ #endif
+ static ZEND_RESULT_CODE php_http_client_curl_wait(php_http_client_t *h, struct timeval *custom_timeout)
+ {
+       int MAX;
+       fd_set R, W, E;
+       struct timeval timeout;
+       php_http_client_curl_t *curl = h->ctx;
+ #if PHP_HTTP_HAVE_EVENT
+       if (curl->useevents) {
+               if (!event_initialized(curl->timeout)) {
+                       event_assign(curl->timeout, curl->evbase, CURL_SOCKET_TIMEOUT, 0, php_http_curlm_timeout_callback, h);
+               } else if (custom_timeout && timerisset(custom_timeout)) {
+                       event_add(curl->timeout, custom_timeout);
+               } else if (!event_pending(curl->timeout, EV_TIMEOUT, NULL)) {
+                       php_http_client_curl_get_timeout(curl, 1000, &timeout);
+                       event_add(curl->timeout, &timeout);
+               }
+               event_base_loop(curl->evbase, EVLOOP_ONCE);
+               return SUCCESS;
+       }
+ #endif
+       FD_ZERO(&R);
+       FD_ZERO(&W);
+       FD_ZERO(&E);
+       if (CURLM_OK == curl_multi_fdset(curl->handle, &R, &W, &E, &MAX)) {
+               if (custom_timeout && timerisset(custom_timeout)) {
+                       timeout = *custom_timeout;
+               } else {
+                       php_http_client_curl_get_timeout(curl, 1000, &timeout);
+               }
+               if (MAX == -1) {
+                       php_http_sleep((double) timeout.tv_sec + (double) (timeout.tv_usec / PHP_HTTP_MCROSEC));
+                       return SUCCESS;
+               } else if (SELECT_ERROR != select(MAX + 1, &R, &W, &E, &timeout)) {
+                       return SUCCESS;
+               }
+       }
+       return FAILURE;
+ }
+ static int php_http_client_curl_once(php_http_client_t *h)
+ {
+       php_http_client_curl_t *curl = h->ctx;
+ #if PHP_HTTP_HAVE_EVENT
+       if (curl->useevents) {
+               event_base_loop(curl->evbase, EVLOOP_NONBLOCK);
+       } else
+ #endif
+       while (CURLM_CALL_MULTI_PERFORM == curl_multi_perform(curl->handle, &curl->unfinished));
+       php_http_curlm_responsehandler(h);
+       return curl->unfinished;
+ }
+ static ZEND_RESULT_CODE php_http_client_curl_exec(php_http_client_t *h)
+ {
+ #if PHP_HTTP_HAVE_EVENT
+       php_http_client_curl_t *curl = h->ctx;
 -#if PHP_HTTP_HAVE_EVENT
 -                              php_error_docref(NULL TSRMLS_CC, E_ERROR, "Error in event_base_dispatch()");
+       if (curl->useevents) {
+               php_http_curlm_timeout_callback(CURL_SOCKET_TIMEOUT, /*EV_READ|EV_WRITE*/0, h);
+               do {
+                       int ev_rc = event_base_dispatch(curl->evbase);
+ #if DBG_EVENTS
+                       fprintf(stderr, "%c", "X.0"[ev_rc+1]);
+ #endif
+                       if (ev_rc < 0) {
 -                              php_error_docref(NULL TSRMLS_CC, E_WARNING, "WinSock error: %d", WSAGetLastError());
++                              php_error_docref(NULL, E_ERROR, "Error in event_base_dispatch()");
+                               return FAILURE;
+                       }
+               } while (curl->unfinished && !EG(exception));
+       } else
+ #endif
+       {
+               while (php_http_client_curl_once(h) && !EG(exception)) {
+                       if (SUCCESS != php_http_client_curl_wait(h, NULL)) {
+ #ifdef PHP_WIN32
+                               /* see http://msdn.microsoft.com/library/en-us/winsock/winsock/windows_sockets_error_codes_2.asp */
 -                              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
++                              php_error_docref(NULL, E_WARNING, "WinSock error: %d", WSAGetLastError());
+ #else
 -static int apply_available_options(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
++                              php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
+ #endif
+                               return FAILURE;
+                       }
+               }
+       }
+       return SUCCESS;
+ }
+ static ZEND_RESULT_CODE php_http_client_curl_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
+ {
+       php_http_client_curl_t *curl = h->ctx;
+       switch (opt) {
+               case PHP_HTTP_CLIENT_OPT_CONFIGURATION:
+                       return php_http_options_apply(&php_http_curlm_options, (HashTable *) arg,  h);
+                       break;
+               case PHP_HTTP_CLIENT_OPT_ENABLE_PIPELINING:
+                       if (CURLM_OK != curl_multi_setopt(curl->handle, CURLMOPT_PIPELINING, (long) *((zend_bool *) arg))) {
+                               return FAILURE;
+                       }
+                       break;
+               case PHP_HTTP_CLIENT_OPT_USE_EVENTS:
+ #if PHP_HTTP_HAVE_EVENT
+                       return php_http_curlm_use_eventloop(h, *(zend_bool *) arg);
+                       break;
+ #endif
+               default:
+                       return FAILURE;
+       }
+       return SUCCESS;
+ }
 -      php_http_option_t *opt = pDest;
++static int apply_available_options(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key)
+ {
 -      zval *entry;
++      php_http_option_t *opt = Z_PTR_P(pDest);
+       HashTable *ht;
 -      MAKE_STD_ZVAL(entry);
 -
++      zval entry;
+       int c;
+       ht = va_arg(args, HashTable*);
 -              array_init_size(entry, c);
 -              zend_hash_apply_with_arguments(&opt->suboptions.options TSRMLS_CC, apply_available_options, 1, Z_ARRVAL_P(entry));
+       if ((c = zend_hash_num_elements(&opt->suboptions.options))) {
 -                      ZVAL_NULL(entry);
++              array_init_size(&entry, c);
++              zend_hash_apply_with_arguments(&opt->suboptions.options, apply_available_options, 1, Z_ARRVAL(entry));
+       } else {
+               /* catch deliberate NULL options */
+               if (Z_TYPE(opt->defval) == IS_STRING && !Z_STRVAL(opt->defval)) {
 -                      ZVAL_COPY_VALUE(entry, &opt->defval);
 -                      zval_copy_ctor(entry);
++                      ZVAL_NULL(&entry);
+               } else {
 -      if (hash_key->nKeyLength) {
 -              zend_hash_quick_update(ht, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &entry, sizeof(zval *), NULL);
++                      ZVAL_ZVAL(&entry, &opt->defval, 1, 0);
+               }
+       }
 -              zend_hash_index_update(ht, hash_key->h, (void *) &entry, sizeof(zval *), NULL);
++      if (hash_key->key) {
++              zend_hash_update(ht, hash_key->key, &entry);
+       } else {
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
++              zend_hash_index_update(ht, hash_key->h, &entry);
+       }
+       return ZEND_HASH_APPLY_KEEP;
+ }
+ static ZEND_RESULT_CODE php_http_client_curl_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void **res)
+ {
+       php_http_client_enqueue_t *enqueue;
 -              zend_hash_apply_with_arguments(&php_http_curle_options.options TSRMLS_CC, apply_available_options, 1, *(HashTable **) res);
+       switch (opt) {
+       case PHP_HTTP_CLIENT_OPT_PROGRESS_INFO:
+               if ((enqueue = php_http_client_enqueued(h, arg, NULL))) {
+                       php_http_client_curl_handler_t *handler = enqueue->opaque;
+                       *((php_http_client_progress_state_t **) res) = &handler->progress;
+                       return SUCCESS;
+               }
+               break;
+       case PHP_HTTP_CLIENT_OPT_TRANSFER_INFO:
+               if ((enqueue = php_http_client_enqueued(h, arg, NULL))) {
+                       php_http_client_curl_handler_t *handler = enqueue->opaque;
+                       php_http_curle_get_info(handler->handle, *(HashTable **) res);
+                       return SUCCESS;
+               }
+               break;
+       case PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS:
 -              zend_hash_apply_with_arguments(&php_http_curlm_options.options TSRMLS_CC, apply_available_options, 1, *(HashTable **) res);
++              zend_hash_apply_with_arguments(&php_http_curle_options.options, apply_available_options, 1, *(HashTable **) res);
+               break;
+       case PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION:
 -      php_http_client_driver_t driver = {
 -              ZEND_STRL("curl"),
 -              &php_http_client_curl_ops
 -      };
++              zend_hash_apply_with_arguments(&php_http_curlm_options.options, apply_available_options, 1, *(HashTable **) res);
+               break;
+       default:
+               break;
+       }
+       return FAILURE;
+ }
+ static php_http_client_ops_t php_http_client_curl_ops = {
+       &php_http_curlm_resource_factory_ops,
+       php_http_client_curl_init,
+       NULL /* copy */,
+       php_http_client_curl_dtor,
+       php_http_client_curl_reset,
+       php_http_client_curl_exec,
+       php_http_client_curl_wait,
+       php_http_client_curl_once,
+       php_http_client_curl_enqueue,
+       php_http_client_curl_dequeue,
+       php_http_client_curl_setopt,
+       php_http_client_curl_getopt
+ };
+ php_http_client_ops_t *php_http_client_curl_get_ops(void)
+ {
+       return &php_http_client_curl_ops;
+ }
+ PHP_MINIT_FUNCTION(http_client_curl)
+ {
+       php_http_options_t *options;
 -      if (SUCCESS != php_http_client_driver_add(&driver)) {
 -      if (SUCCESS != php_persistent_handle_provide(ZEND_STRL("http\\Client\\Curl"), &php_http_curlm_resource_factory_ops, NULL, NULL TSRMLS_CC)) {
++      PHP_HTTP_G->client.curl.driver.driver_name = zend_string_init(ZEND_STRL("curl"), 1);
++      PHP_HTTP_G->client.curl.driver.client_name = zend_string_init(ZEND_STRL("http\\Client\\Curl"), 1);
++      PHP_HTTP_G->client.curl.driver.request_name = zend_string_init(ZEND_STRL("http\\Client\\Curl\\Request"), 1);
++      PHP_HTTP_G->client.curl.driver.client_ops = &php_http_client_curl_ops;
++
++      if (SUCCESS != php_http_client_driver_add(&PHP_HTTP_G->client.curl.driver)) {
+               return FAILURE;
+       }
 -      if (SUCCESS != php_persistent_handle_provide(ZEND_STRL("http\\Client\\Curl\\Request"), &php_http_curle_resource_factory_ops, NULL, NULL TSRMLS_CC)) {
++      if (SUCCESS != php_persistent_handle_provide(PHP_HTTP_G->client.curl.driver.client_name, &php_http_curlm_resource_factory_ops, NULL, NULL)) {
+               return FAILURE;
+       }
 -              php_http_curle_options_init(options TSRMLS_CC);
++      if (SUCCESS != php_persistent_handle_provide(PHP_HTTP_G->client.curl.driver.request_name, &php_http_curle_resource_factory_ops, NULL, NULL)) {
+               return FAILURE;
+       }
+       if ((options = php_http_options_init(&php_http_curle_options, 1))) {
+               options->getter = php_http_curle_get_option;
+               options->setter = php_http_curle_set_option;
 -              php_http_curlm_options_init(options TSRMLS_CC);
++              php_http_curle_options_init(options);
+       }
+       if ((options = php_http_options_init(&php_http_curlm_options, 1))) {
+               options->getter = php_http_option_get;
+               options->setter = php_http_curlm_set_option;
 -      php_persistent_handle_cleanup(ZEND_STRL("http\\Client\\Curl"), NULL, 0 TSRMLS_CC);
 -      php_persistent_handle_cleanup(ZEND_STRL("http\\Client\\Curl\\Request"), NULL, 0 TSRMLS_CC);
++              php_http_curlm_options_init(options);
+       }
+       /*
+       * HTTP Protocol Version Constants
+       */
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_1_0", CURL_HTTP_VERSION_1_0, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_1_1", CURL_HTTP_VERSION_1_1, CONST_CS|CONST_PERSISTENT);
+ #if PHP_HTTP_CURL_VERSION(7,33,0)
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_2_0", CURL_HTTP_VERSION_2_0, CONST_CS|CONST_PERSISTENT);
+ #endif
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "HTTP_VERSION_ANY", CURL_HTTP_VERSION_NONE, CONST_CS|CONST_PERSISTENT);
+       /*
+       * SSL Version Constants
+       */
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1", CURL_SSLVERSION_TLSv1, CONST_CS|CONST_PERSISTENT);
+ #if PHP_HTTP_CURL_VERSION(7,34,0)
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_0", CURL_SSLVERSION_TLSv1_0, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_1", CURL_SSLVERSION_TLSv1_1, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_TLSv1_2", CURL_SSLVERSION_TLSv1_2, CONST_CS|CONST_PERSISTENT);
+ #endif
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_SSLv2", CURL_SSLVERSION_SSLv2, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_SSLv3", CURL_SSLVERSION_SSLv3, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "SSL_VERSION_ANY", CURL_SSLVERSION_DEFAULT, CONST_CS|CONST_PERSISTENT);
+ #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "TLSAUTH_SRP", CURL_TLSAUTH_SRP, CONST_CS|CONST_PERSISTENT);
+ #endif
+       /*
+       * DNS IPvX resolving
+       */
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_V4", CURL_IPRESOLVE_V4, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_V6", CURL_IPRESOLVE_V6, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "IPRESOLVE_ANY", CURL_IPRESOLVE_WHATEVER, CONST_CS|CONST_PERSISTENT);
+       /*
+       * Auth Constants
+       */
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_BASIC", CURLAUTH_BASIC, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_DIGEST", CURLAUTH_DIGEST, CONST_CS|CONST_PERSISTENT);
+ #if PHP_HTTP_CURL_VERSION(7,19,3)
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_DIGEST_IE", CURLAUTH_DIGEST_IE, CONST_CS|CONST_PERSISTENT);
+ #endif
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_NTLM", CURLAUTH_NTLM, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_GSSNEG", CURLAUTH_GSSNEGOTIATE, CONST_CS|CONST_PERSISTENT);
+ #if PHP_HTTP_CURL_VERSION(7,38,0)
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_SPNEGO", CURLAUTH_NEGOTIATE, CONST_CS|CONST_PERSISTENT);
+ #endif
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "AUTH_ANY", CURLAUTH_ANY, CONST_CS|CONST_PERSISTENT);
+       /*
+       * Proxy Type Constants
+       */
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS4", CURLPROXY_SOCKS4, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS4A", CURLPROXY_SOCKS5, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS5_HOSTNAME", CURLPROXY_SOCKS5, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_SOCKS5", CURLPROXY_SOCKS5, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_HTTP", CURLPROXY_HTTP, CONST_CS|CONST_PERSISTENT);
+ #if PHP_HTTP_CURL_VERSION(7,19,4)
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "PROXY_HTTP_1_0", CURLPROXY_HTTP_1_0, CONST_CS|CONST_PERSISTENT);
+ #endif
+       /*
+       * Post Redirection Constants
+       */
+ #if PHP_HTTP_CURL_VERSION(7,19,1)
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_301", CURL_REDIR_POST_301, CONST_CS|CONST_PERSISTENT);
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_302", CURL_REDIR_POST_302, CONST_CS|CONST_PERSISTENT);
+ #if PHP_HTTP_CURL_VERSION(7,26,0)
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_303", CURL_REDIR_POST_303, CONST_CS|CONST_PERSISTENT);
+ #endif
+       REGISTER_NS_LONG_CONSTANT("http\\Client\\Curl", "POSTREDIR_ALL", CURL_REDIR_POST_ALL, CONST_CS|CONST_PERSISTENT);
+ #endif
+       return SUCCESS;
+ }
+ PHP_MSHUTDOWN_FUNCTION(http_client_curl)
+ {
++      php_persistent_handle_cleanup(PHP_HTTP_G->client.curl.driver.client_name, NULL);
++      php_persistent_handle_cleanup(PHP_HTTP_G->client.curl.driver.request_name, NULL);
++      zend_string_release(PHP_HTTP_G->client.curl.driver.client_name);
++      zend_string_release(PHP_HTTP_G->client.curl.driver.request_name);
++      zend_string_release(PHP_HTTP_G->client.curl.driver.driver_name);
+       php_http_options_dtor(&php_http_curle_options);
+       php_http_options_dtor(&php_http_curlm_options);
+       return SUCCESS;
+ }
+ #endif /* PHP_HTTP_HAVE_CURL */
+ /*
+  * Local variables:
+  * tab-width: 4
+  * c-basic-offset: 4
+  * End:
+  * vim600: noet sw=4 ts=4 fdm=marker
+  * vim<600: noet sw=4 ts=4
+  */
index 0000000,c82a09c..9128647
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,31 +1,35 @@@
+ /*
+     +--------------------------------------------------------------------+
+     | PECL :: http                                                       |
+     +--------------------------------------------------------------------+
+     | Redistribution and use in source and binary forms, with or without |
+     | modification, are permitted provided that the conditions mentioned |
+     | in the accompanying LICENSE file are met.                          |
+     +--------------------------------------------------------------------+
+     | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
+     +--------------------------------------------------------------------+
+ */
+ #ifndef PHP_HTTP_CLIENT_CURL_H
+ #define PHP_HTTP_CLIENT_CURL_H
+ #if PHP_HTTP_HAVE_CURL
++struct php_http_client_curl_globals {
++      php_http_client_driver_t driver;
++};
++
+ PHP_MINIT_FUNCTION(http_client_curl);
+ PHP_MSHUTDOWN_FUNCTION(http_client_curl);
+ #endif /* PHP_HTTP_HAVE_CURL */
+ #endif /* PHP_HTTP_CLIENT_CURL_H */
+ /*
+  * Local variables:
+  * tab-width: 4
+  * c-basic-offset: 4
+  * End:
+  * vim600: noet sw=4 ts=4 fdm=marker
+  * vim<600: noet sw=4 ts=4
+  */
index 0000000,0e40cc5..05d4a81
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,314 +1,301 @@@
 -void php_http_client_options_set_subr(zval *this_ptr, char *key, size_t len, zval *opts, int overwrite TSRMLS_DC);
 -void php_http_client_options_set(zval *this_ptr, zval *opts TSRMLS_DC);
 -void php_http_client_options_get_subr(zval *this_ptr, char *key, size_t len, zval *return_value TSRMLS_DC);
+ /*
+     +--------------------------------------------------------------------+
+     | PECL :: http                                                       |
+     +--------------------------------------------------------------------+
+     | Redistribution and use in source and binary forms, with or without |
+     | modification, are permitted provided that the conditions mentioned |
+     | in the accompanying LICENSE file are met.                          |
+     +--------------------------------------------------------------------+
+     | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
+     +--------------------------------------------------------------------+
+ */
+ #include "php_http_api.h"
 -                      obj->message = php_http_message_init(NULL, PHP_HTTP_REQUEST, NULL TSRMLS_CC); \
++void php_http_client_options_set_subr(zval *this_ptr, char *key, size_t len, zval *opts, int overwrite);
++void php_http_client_options_set(zval *this_ptr, zval *opts);
++void php_http_client_options_get_subr(zval *this_ptr, char *key, size_t len, zval *return_value);
+ #define PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj) \
+       do { \
+               if (!obj->message) { \
 -      int meth_len = 0;
++                      obj->message = php_http_message_init(NULL, PHP_HTTP_REQUEST, NULL); \
+               } \
+       } while(0)
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest___construct, 0, 0, 0)
+       ZEND_ARG_INFO(0, method)
+       ZEND_ARG_INFO(0, url)
+       ZEND_ARG_ARRAY_INFO(0, headers, 1)
+       ZEND_ARG_OBJ_INFO(0, body, http\\Message\\Body, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientRequest, __construct)
+ {
+       char *meth_str = NULL;
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!z!a!O!", &meth_str, &meth_len, &zurl, &zheaders, &zbody, php_http_message_body_class_entry), invalid_arg, return);
++      size_t meth_len = 0;
+       zval *zheaders = NULL, *zbody = NULL, *zurl = NULL;
+       php_http_message_object_t *obj;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s!z!a!O!", &meth_str, &meth_len, &zurl, &zheaders, &zbody, php_http_message_body_class_entry), invalid_arg, return);
 -              obj->message = php_http_message_init(NULL, PHP_HTTP_REQUEST, NULL TSRMLS_CC);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       if (obj->message) {
+               php_http_message_set_type(obj->message, PHP_HTTP_REQUEST);
+       } else {
 -              php_http_expect(SUCCESS == php_http_message_object_set_body(obj, zbody TSRMLS_CC), unexpected_val, return);
++              obj->message = php_http_message_init(NULL, PHP_HTTP_REQUEST, NULL);
+       }
+       if (zbody) {
 -              PHP_HTTP_INFO(obj->message).request.url = php_http_url_from_zval(zurl, ~0 TSRMLS_CC);
++              php_http_expect(SUCCESS == php_http_message_object_set_body(obj, zbody), unexpected_val, return);
+       }
+       if (meth_str && meth_len) {
+               PHP_HTTP_INFO(obj->message).request.method = estrndup(meth_str, meth_len);
+       }
+       if (zurl) {
 -      char *ct_str;
 -      int ct_len;
++              PHP_HTTP_INFO(obj->message).request.url = php_http_url_from_zval(zurl, ~0);
+       }
+       if (zheaders) {
+               array_copy(Z_ARRVAL_P(zheaders), &obj->message->hdrs);
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_setContentType, 0, 0, 1)
+       ZEND_ARG_INFO(0, content_type)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientRequest, setContentType)
+ {
 -      zval *zct;
++      zend_string *ct_str;
+       php_http_message_object_t *obj;
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &ct_str, &ct_len), invalid_arg, return);
++      zval zct;
 -      if (ct_len && !strchr(ct_str, '/')) {
 -              php_http_throw(unexpected_val, "Content type \"%s\" does not seem to contain a primary and a secondary part", ct_str);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "S", &ct_str), invalid_arg, return);
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -
++      if (ct_str->len && !strchr(ct_str->val, '/')) {
++              php_http_throw(unexpected_val, "Content type \"%s\" does not seem to contain a primary and a secondary part", ct_str->val);
+               return;
+       }
 -      MAKE_STD_ZVAL(zct);
 -      ZVAL_STRINGL(zct, ct_str, ct_len, 1);
 -      zend_hash_update(&obj->message->hdrs, "Content-Type", sizeof("Content-Type"), (void *) &zct, sizeof(void *), NULL);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      ZVAL_STR_COPY(&zct, ct_str);
++      zend_hash_str_update(&obj->message->hdrs, "Content-Type", lenof("Content-Type"), &zct);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_getContentType, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientRequest, getContentType)
+ {
+       if (SUCCESS == zend_parse_parameters_none()) {
 -              zct = php_http_message_header(obj->message, ZEND_STRL("Content-Type"), 1);
++              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
+               zval *zct;
+               PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
+               php_http_message_update_headers(obj->message);
 -                      RETURN_ZVAL(zct, 0, 1);
++              zct = php_http_message_header(obj->message, ZEND_STRL("Content-Type"));
+               if (zct) {
 -      zval *qdata = NULL;
++                      RETURN_ZVAL(zct, 1, 0);
+               }
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_setQuery, 0, 0, 0)
+       ZEND_ARG_INFO(0, query_data)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientRequest, setQuery)
+ {
 -      char empty[] = "";
++      zval *qdata = NULL, arr, str;
+       php_http_message_object_t *obj;
+       php_http_url_t *old_url = NULL, new_url = {NULL};
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z!", &qdata), invalid_arg, return);
 -
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+       unsigned flags = PHP_HTTP_URL_REPLACE;
 -              zval arr, str;
 -
 -              INIT_PZVAL(&arr);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z!", &qdata), invalid_arg, return);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
++      ZVAL_NULL(&str);
+       if (qdata) {
 -              INIT_PZVAL(&str);
 -              ZVAL_NULL(&str);
+               array_init(&arr);
 -              php_http_expect(SUCCESS == php_http_querystring_update(&arr, qdata, &str TSRMLS_CC), bad_querystring,
 -      obj->message->http.info.request.url = php_http_url_mod(old_url, &new_url, flags TSRMLS_CC);
++              php_http_expect(SUCCESS == php_http_querystring_update(&arr, qdata, &str), bad_querystring,
+                               zval_dtor(&arr);
+                               return;
+               );
++
+               new_url.query = Z_STRVAL(str);
+               zval_dtor(&arr);
+       } else {
+               flags = PHP_HTTP_URL_STRIP_QUERY;
+       }
+       if (obj->message->http.info.request.url) {
+               old_url = obj->message->http.info.request.url;
+       }
 -      if (new_url.query != &empty[0]) {
 -              PTR_FREE(new_url.query);
 -      }
++      obj->message->http.info.request.url = php_http_url_mod(old_url, &new_url, flags);
+       if (old_url) {
+               php_http_url_free(&old_url);
+       }
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      zval_ptr_dtor(&str);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_getQuery, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientRequest, getQuery)
+ {
+       if (SUCCESS == zend_parse_parameters_none()) {
 -                      RETVAL_STRING(obj->message->http.info.request.url->query, 1);
++              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
+               PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
+               if (obj->message->http.info.request.url && obj->message->http.info.request.url->query) {
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &qdata), invalid_arg, return);
 -
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++                      RETVAL_STRING(obj->message->http.info.request.url->query);
+               }
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_addQuery, 0, 0, 1)
+       ZEND_ARG_INFO(0, query_data)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientRequest, addQuery)
+ {
+       zval *qdata, arr, str;
+       php_http_message_object_t *obj;
+       php_http_url_t *old_url = NULL, new_url = {NULL};
 -      INIT_PZVAL(&arr);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z", &qdata), invalid_arg, return);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       PHP_HTTP_CLIENT_REQUEST_OBJECT_INIT(obj);
 -      INIT_PZVAL(&str);
+       array_init(&arr);
 -      php_http_expect(SUCCESS == php_http_querystring_update(&arr, qdata, &str TSRMLS_CC), bad_querystring,
+       ZVAL_NULL(&str);
 -      obj->message->http.info.request.url = php_http_url_mod(old_url, &new_url, PHP_HTTP_URL_JOIN_QUERY TSRMLS_CC);
++      php_http_expect(SUCCESS == php_http_querystring_update(&arr, qdata, &str), bad_querystring,
+                       zval_dtor(&arr);
+                       return;
+       );
+       new_url.query = Z_STRVAL(str);
+       zval_dtor(&arr);
+       if (obj->message->http.info.request.url) {
+               old_url = obj->message->http.info.request.url;
+       }
 -      PTR_FREE(new_url.query);
++      obj->message->http.info.request.url = php_http_url_mod(old_url, &new_url, PHP_HTTP_URL_JOIN_QUERY);
+       if (old_url) {
+               php_http_url_free(&old_url);
+       }
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
++      zval_ptr_dtor(&str);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_setOptions, 0, 0, 0)
+       ZEND_ARG_ARRAY_INFO(0, options, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientRequest, setOptions)
+ {
+       zval *opts = NULL;
 -      php_http_client_options_set(getThis(), opts TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
 -              zval *zoptions = zend_read_property(php_http_client_request_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
++      php_http_client_options_set(getThis(), opts);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_getOptions, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientRequest, getOptions)
+ {
+       if (SUCCESS == zend_parse_parameters_none()) {
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
++              zval tmp, *zoptions = zend_read_property(php_http_client_request_class_entry, getThis(), ZEND_STRL("options"), 0, &tmp);
+               RETURN_ZVAL(zoptions, 1, 0);
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_setSslOptions, 0, 0, 0)
+       ZEND_ARG_ARRAY_INFO(0, ssl_options, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientRequest, setSslOptions)
+ {
+       zval *opts = NULL;
 -      php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 1 TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
++      php_http_client_options_set_subr(getThis(), ZEND_STRL("ssl"), opts, 1);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_addSslOptions, 0, 0, 0)
+       ZEND_ARG_ARRAY_INFO(0, ssl_options, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientRequest, addSslOptions)
+ {
+       zval *opts = NULL;
 -      php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 0 TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
 -              php_http_client_options_get_subr(getThis(), ZEND_STRS("ssl"), return_value TSRMLS_CC);
++      php_http_client_options_set_subr(getThis(), ZEND_STRL("ssl"), opts, 0);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientRequest_getSslOptions, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientRequest, getSslOptions)
+ {
+       if (SUCCESS == zend_parse_parameters_none()) {
 -      php_http_client_request_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry, NULL TSRMLS_CC);
++              php_http_client_options_get_subr(getThis(), ZEND_STRL("ssl"), return_value);
+       }
+ }
+ static zend_function_entry php_http_client_request_methods[] = {
+       PHP_ME(HttpClientRequest, __construct,    ai_HttpClientRequest___construct,    ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
+       PHP_ME(HttpClientRequest, setContentType, ai_HttpClientRequest_setContentType, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClientRequest, getContentType, ai_HttpClientRequest_getContentType, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClientRequest, setQuery,       ai_HttpClientRequest_setQuery,       ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClientRequest, getQuery,       ai_HttpClientRequest_getQuery,       ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClientRequest, addQuery,       ai_HttpClientRequest_addQuery,       ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClientRequest, setOptions,     ai_HttpClientRequest_setOptions,     ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClientRequest, getOptions,     ai_HttpClientRequest_getOptions,     ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClientRequest, setSslOptions,  ai_HttpClientRequest_setSslOptions,  ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClientRequest, getSslOptions,  ai_HttpClientRequest_getSslOptions,  ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClientRequest, addSslOptions,  ai_HttpClientRequest_addSslOptions,  ZEND_ACC_PUBLIC)
+       EMPTY_FUNCTION_ENTRY
+ };
+ zend_class_entry *php_http_client_request_class_entry;
+ PHP_MINIT_FUNCTION(http_client_request)
+ {
+       zend_class_entry ce = {0};
+       INIT_NS_CLASS_ENTRY(ce, "http\\Client", "Request", php_http_client_request_methods);
 -      zend_declare_property_null(php_http_client_request_class_entry, ZEND_STRL("options"), ZEND_ACC_PROTECTED TSRMLS_CC);
++      php_http_client_request_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry);
++      zend_declare_property_null(php_http_client_request_class_entry, ZEND_STRL("options"), ZEND_ACC_PROTECTED);
+       return SUCCESS;
+ }
+ /*
+  * Local variables:
+  * tab-width: 4
+  * c-basic-offset: 4
+  * End:
+  * vim600: noet sw=4 ts=4 fdm=marker
+  * vim<600: noet sw=4 ts=4
+  */
index 0000000,8d512ec..b1377fe
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,143 +1,146 @@@
 -      long flags = 0;
+ /*
+     +--------------------------------------------------------------------+
+     | PECL :: http                                                       |
+     +--------------------------------------------------------------------+
+     | Redistribution and use in source and binary forms, with or without |
+     | modification, are permitted provided that the conditions mentioned |
+     | in the accompanying LICENSE file are met.                          |
+     +--------------------------------------------------------------------+
+     | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
+     +--------------------------------------------------------------------+
+ */
+ #include "php_http_api.h"
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientResponse_getCookies, 0, 0, 0)
+       ZEND_ARG_INFO(0, flags)
+       ZEND_ARG_INFO(0, allowed_extras)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientResponse, getCookies)
+ {
 -      zval *header = NULL, **entry = NULL;
 -      HashPosition pos;
++      zend_long flags = 0;
+       zval *allowed_extras_array = NULL;
+       int i = 0;
+       char **allowed_extras = NULL;
 -      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|la!", &flags, &allowed_extras_array)) {
++      zval *header = NULL, *entry = NULL;
+       php_http_message_object_t *msg;
 -      msg = zend_object_store_get_object(getThis() TSRMLS_CC);
++      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|la!/", &flags, &allowed_extras_array)) {
+               return;
+       }
 -              FOREACH_VAL(pos, allowed_extras_array, entry) {
 -                      zval *data = php_http_ztyp(IS_STRING, *entry);
 -                      allowed_extras[i++] = estrndup(Z_STRVAL_P(data), Z_STRLEN_P(data));
 -                      zval_ptr_dtor(&data);
++      msg = PHP_HTTP_OBJ(NULL, getThis());
+       array_init(return_value);
+       if (allowed_extras_array) {
++              /* FIXME: use zend_string** instead of char** */
+               allowed_extras = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(allowed_extras_array)) + 1, sizeof(char *));
 -      if ((header = php_http_message_header(msg->message, ZEND_STRL("Set-Cookie"), 0))) {
++              ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(allowed_extras_array), entry)
++              {
++                      zend_string *zs = zval_get_string(entry);
++                      allowed_extras[i++] = estrndup(zs->val, zs->len);
++                      zend_string_release(zs);
+               }
++              ZEND_HASH_FOREACH_END();
+       }
 -                      zval **single_header;
++      if ((header = php_http_message_header(msg->message, ZEND_STRL("Set-Cookie")))) {
+               php_http_cookie_list_t *list;
+               if (Z_TYPE_P(header) == IS_ARRAY) {
 -                      FOREACH_VAL(pos, header, single_header) {
 -                              zval *data = php_http_ztyp(IS_STRING, *single_header);
++                      zval *single_header;
 -                              if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), Z_STRLEN_P(data), flags, allowed_extras TSRMLS_CC))) {
 -                                      zval *cookie;
++                      ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(header), single_header)
++                      {
++                              zend_string *zs = zval_get_string(single_header);
 -                                      MAKE_STD_ZVAL(cookie);
 -                                      ZVAL_OBJVAL(cookie, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0);
 -                                      add_next_index_zval(return_value, cookie);
++                              if ((list = php_http_cookie_list_parse(NULL, zs->val, zs->len, flags, allowed_extras))) {
++                                      zval cookie;
 -                              zval_ptr_dtor(&data);
++                                      ZVAL_OBJ(&cookie, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo);
++                                      add_next_index_zval(return_value, &cookie);
+                               }
 -                      zval *data = php_http_ztyp(IS_STRING, header);
 -                      if ((list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(data), Z_STRLEN_P(data), flags, allowed_extras TSRMLS_CC))) {
 -                              zval *cookie;
++                              zend_string_release(zs);
+                       }
++                      ZEND_HASH_FOREACH_END();
+               } else {
 -                              MAKE_STD_ZVAL(cookie);
 -                              ZVAL_OBJVAL(cookie, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0);
 -                              add_next_index_zval(return_value, cookie);
++                      zend_string *zs = zval_get_string(header);
 -                      zval_ptr_dtor(&data);
++                      if ((list = php_http_cookie_list_parse(NULL, zs->val, zs->len, flags, allowed_extras))) {
++                              zval cookie;
++
++                              ZVAL_OBJ(&cookie, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo);
++                              add_next_index_zval(return_value, &cookie);
+                       }
 -              zval_ptr_dtor(&header);
++                      zend_string_release(zs);
+               }
 -      int info_len = 0;
 -      zval *info;
+       }
+       if (allowed_extras) {
+               for (i = 0; allowed_extras[i]; ++i) {
+                       efree(allowed_extras[i]);
+               }
+               efree(allowed_extras);
+       }
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClientResponse_getTransferInfo, 0, 0, 0)
+       ZEND_ARG_INFO(0, element)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpClientResponse, getTransferInfo)
+ {
+       char *info_name = NULL;
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &info_name, &info_len), invalid_arg, return);
++      size_t info_len = 0;
++      zval info_tmp, info_name_tmp, *info;
 -      info = zend_read_property(php_http_client_response_class_entry, getThis(), ZEND_STRL("transferInfo"), 0 TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &info_name, &info_len), invalid_arg, return);
 -              info = zend_read_property(NULL, info, php_http_pretty_key(info_name, info_len, 0, 0), info_len, 0 TSRMLS_CC);
++      info = zend_read_property(php_http_client_response_class_entry, getThis(), ZEND_STRL("transferInfo"), 0, &info_tmp);
+       /* request completed? */
+       if (Z_TYPE_P(info) != IS_OBJECT) {
+               php_http_throw(bad_method_call, "Incomplete state", NULL);
+               return;
+       }
+       if (info_len && info_name) {
 -      php_http_client_response_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry, NULL TSRMLS_CC);
 -      zend_declare_property_null(php_http_client_response_class_entry, ZEND_STRL("transferInfo"), ZEND_ACC_PROTECTED TSRMLS_CC);
++              info = zend_read_property(NULL, info, php_http_pretty_key(info_name, info_len, 0, 0), info_len, 0, &info_name_tmp);
+               if (!info) {
+                       php_http_throw(unexpected_val, "Could not find transfer info with name '%s'", info_name);
+                       return;
+               }
+       }
+       RETURN_ZVAL(info, 1, 0);
+ }
+ static zend_function_entry php_http_client_response_methods[] = {
+       PHP_ME(HttpClientResponse, getCookies,      ai_HttpClientResponse_getCookies,      ZEND_ACC_PUBLIC)
+       PHP_ME(HttpClientResponse, getTransferInfo, ai_HttpClientResponse_getTransferInfo, ZEND_ACC_PUBLIC)
+       EMPTY_FUNCTION_ENTRY
+ };
+ zend_class_entry *php_http_client_response_class_entry;
+ PHP_MINIT_FUNCTION(http_client_response)
+ {
+       zend_class_entry ce = {0};
+       INIT_NS_CLASS_ENTRY(ce, "http\\Client", "Response", php_http_client_response_methods);
++      php_http_client_response_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry);
++
++      zend_declare_property_null(php_http_client_response_class_entry, ZEND_STRL("transferInfo"), ZEND_ACC_PROTECTED);
+       return SUCCESS;
+ }
+ /*
+  * Local variables:
+  * tab-width: 4
+  * c-basic-offset: 4
+  * End:
+  * vim600: noet sw=4 ts=4 fdm=marker
+  * vim<600: noet sw=4 ts=4
+  */
index 0000000,354dfa6..9625401
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,1038 +1,1043 @@@
 -php_http_cookie_list_t *php_http_cookie_list_init(php_http_cookie_list_t *list TSRMLS_DC)
+ /*
+     +--------------------------------------------------------------------+
+     | PECL :: http                                                       |
+     +--------------------------------------------------------------------+
+     | Redistribution and use in source and binary forms, with or without |
+     | modification, are permitted provided that the conditions mentioned |
+     | in the accompanying LICENSE file are met.                          |
+     +--------------------------------------------------------------------+
+     | Copyright (c) 2004-2014, Michael Wallner <mike@php.net>            |
+     +--------------------------------------------------------------------+
+ */
+ #include "php_http_api.h"
 -      TSRMLS_SET_CTX(list->ts);
 -
++php_http_cookie_list_t *php_http_cookie_list_init(php_http_cookie_list_t *list)
+ {
+       if (!list) {
+               list = emalloc(sizeof(*list));
+       }
+       
+       zend_hash_init(&list->cookies, 0, NULL, ZVAL_PTR_DTOR, 0);
+       zend_hash_init(&list->extras, 0, NULL, ZVAL_PTR_DTOR, 0);
+       
+       list->path = NULL;
+       list->domain = NULL;
+       list->expires = -1;
+       list->max_age = -1;
+       list->flags = 0;
+       
 -      TSRMLS_FETCH_FROM_CTX(from->ts);
 -
 -      to = php_http_cookie_list_init(to TSRMLS_CC);
+       return list;
+ }
+ php_http_cookie_list_t *php_http_cookie_list_copy(php_http_cookie_list_t *from, php_http_cookie_list_t *to)
+ {
 -const char *php_http_cookie_list_get_cookie(php_http_cookie_list_t *list, const char *name, size_t name_len, zval **zcookie)
++      to = php_http_cookie_list_init(to);
+       array_copy(&from->cookies, &to->cookies);
+       array_copy(&from->extras, &to->extras);
+       PTR_SET(to->path, from->path ? estrdup(from->path) : NULL);
+       PTR_SET(to->domain, from->domain ? estrdup(from->domain) : NULL);
+       to->expires = from->expires;
+       to->max_age = from->max_age;
+       to->flags = from->flags;
+       return to;
+ }
+ void php_http_cookie_list_dtor(php_http_cookie_list_t *list)
+ {
+       if (list) {
+               zend_hash_destroy(&list->cookies);
+               zend_hash_destroy(&list->extras);
+       
+               PTR_SET(list->path, NULL);
+               PTR_SET(list->domain, NULL);
+       }
+ }
+ void php_http_cookie_list_free(php_http_cookie_list_t **list)
+ {
+       if (*list) {
+               php_http_cookie_list_dtor(*list);
+               efree(*list);
+               *list = NULL;
+       }
+ }
 -      zval **cookie;
 -      if ((SUCCESS != zend_symtable_find(&list->cookies, name, name_len + 1, (void *) &cookie)) || (Z_TYPE_PP(cookie) != IS_STRING)) {
++const char *php_http_cookie_list_get_cookie(php_http_cookie_list_t *list, const char *name, size_t name_len, zval *zcookie)
+ {
 -      return Z_STRVAL_PP(cookie);
++      zval *cookie = zend_symtable_str_find(&list->cookies, name, name_len);
++
++      if (!cookie || (Z_TYPE_P(cookie) != IS_STRING)) {
+               return NULL;
+       }
+       if (zcookie) {
+               *zcookie = *cookie;
+       }
 -const char *php_http_cookie_list_get_extra(php_http_cookie_list_t *list, const char *name, size_t name_len, zval **zextra)
++      return Z_STRVAL_P(cookie);
+ }
 -      zval **extra;
++const char *php_http_cookie_list_get_extra(php_http_cookie_list_t *list, const char *name, size_t name_len, zval *zextra)
+ {
 -      if ((SUCCESS != zend_symtable_find(&list->extras, name, name_len + 1, (void *) &extra)) || (Z_TYPE_PP(extra) != IS_STRING)) {
++      zval *extra = zend_symtable_str_find(&list->extras, name, name_len);
 -      return Z_STRVAL_PP(extra);
++      if (!extra || (Z_TYPE_P(extra) != IS_STRING)) {
+               return NULL;
+       }
+       if (zextra) {
+               *zextra = *extra;
+       }
 -      zval *cookie_value;
++      return Z_STRVAL_P(extra);
+ }
+ void php_http_cookie_list_add_cookie(php_http_cookie_list_t *list, const char *name, size_t name_len, const char *value, size_t value_len)
+ {
 -      MAKE_STD_ZVAL(cookie_value);
 -      ZVAL_STRINGL(cookie_value, estrndup(value, value_len), value_len, 0);
 -      zend_symtable_update(&list->cookies, name, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL);
++      zval cookie_value;
 -      zval *cookie_value;
++      ZVAL_STRINGL(&cookie_value, value, value_len);
++      zend_symtable_str_update(&list->cookies, name, name_len, &cookie_value);
+ }
+ void php_http_cookie_list_add_extra(php_http_cookie_list_t *list, const char *name, size_t name_len, const char *value, size_t value_len)
+ {
 -      MAKE_STD_ZVAL(cookie_value);
 -      ZVAL_STRINGL(cookie_value, estrndup(value, value_len), value_len, 0);
 -      zend_symtable_update(&list->extras, name, name_len + 1, (void *) &cookie_value, sizeof(zval *), NULL);
++      zval extra_value;
 -#define _KEY_IS(s) (key->len == sizeof(s) && !strncasecmp(key->str, (s), key->len))
 -static void add_entry(php_http_cookie_list_t *list, char **allowed_extras, long flags, php_http_array_hashkey_t *key, zval *val)
++      ZVAL_STRINGL(&extra_value, value, value_len);
++      zend_symtable_str_update(&list->extras, name, name_len, &extra_value);
+ }
 -      zval *arg = php_http_zsep(1, IS_STRING, val);
++#define _KEY_IS(s) (key->key && key->key->len == sizeof(s)-1 && !strncasecmp(key->key->val, (s), key->key->len))
++static void add_entry(php_http_cookie_list_t *list, char **allowed_extras, long flags, zend_hash_key *key, zval *val)
+ {
 -              Z_STRLEN_P(arg) = php_raw_url_decode(Z_STRVAL_P(arg), Z_STRLEN_P(arg));
++      zval arg;
++
++      ZVAL_DUP(&arg, val);
++      convert_to_string(&arg);
+       if (!(flags & PHP_HTTP_COOKIE_PARSE_RAW)) {
 -              PTR_SET(list->path, estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg)));
++              Z_STRLEN(arg) = php_raw_url_decode(Z_STRVAL(arg), Z_STRLEN(arg));
++              zend_string_forget_hash_val(Z_STR(arg));
+       }
+       if _KEY_IS("path") {
 -              PTR_SET(list->domain, estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg)));
++              PTR_SET(list->path, estrndup(Z_STRVAL(arg), Z_STRLEN(arg)));
+       } else if _KEY_IS("domain") {
 -              char *date = estrndup(Z_STRVAL_P(arg), Z_STRLEN_P(arg));
++              PTR_SET(list->domain, estrndup(Z_STRVAL(arg), Z_STRLEN(arg)));
+       } else if _KEY_IS("expires") {
 -              list->max_age = strtol(Z_STRVAL_P(arg), NULL, 10);
++              char *date = estrndup(Z_STRVAL(arg), Z_STRLEN(arg));
+               list->expires = php_parse_date(date, NULL);
+               efree(date);
+       } else if _KEY_IS("max-age") {
 -
 -                      php_http_array_hashkey_stringify(key);
++              list->max_age = zval_get_long(val);
+       } else if _KEY_IS("secure") {
+               list->flags |= PHP_HTTP_COOKIE_SECURE;
+       } else if _KEY_IS("httpOnly") {
+               list->flags |= PHP_HTTP_COOKIE_HTTPONLY;
+       } else {
++              php_http_arrkey_t tmp = {0};
++
++              php_http_arrkey_stringify(&tmp, key);
++
+               /* check for extra */
+               if (allowed_extras) {
+                       char **ae = allowed_extras;
 -                              if (!strncasecmp(key->str, *ae, key->len)) {
 -                                      if (key->type == HASH_KEY_IS_LONG) {
 -                                              zend_hash_index_update(&list->extras, key->num, (void *) &arg, sizeof(zval *), NULL);
 -                                      } else {
 -                                              zend_hash_update(&list->extras, key->str, key->len, (void *) &arg, sizeof(zval *), NULL);
 -                                      }
 -                                      php_http_array_hashkey_stringfree(key);
+                       for (; *ae; ++ae) {
 -                      php_http_array_hashkey_stringfree(key);
++                              if (!strncasecmp(*ae, tmp.key->val, tmp.key->len)) {
++                                      zend_symtable_update(&list->extras, tmp.key, &arg);
++                                      php_http_arrkey_dtor(&tmp);
+                                       return;
+                               }
+                       }
 -              if (key->type == HASH_KEY_IS_LONG) {
 -                      zend_hash_index_update(&list->cookies, key->num, (void *) &arg, sizeof(zval *), NULL);
 -              } else {
 -                      zend_hash_update(&list->cookies, key->str, key->len, (void *) &arg, sizeof(zval *), NULL);
 -              }
+               }
+               /* cookie */
 -php_http_cookie_list_t *php_http_cookie_list_parse(php_http_cookie_list_t *list, const char *str, size_t len, long flags, char **allowed_extras TSRMLS_DC)
++              zend_symtable_update(&list->cookies, tmp.key, &arg);
++
++              php_http_arrkey_dtor(&tmp);
+               return;
+       }
++
+       zval_ptr_dtor(&arg);
+ }
 -      HashPosition pos1, pos2;
 -      php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 -      zval **param, **val, **args, **arg;
++php_http_cookie_list_t *php_http_cookie_list_parse(php_http_cookie_list_t *list, const char *str, size_t len, long flags, char **allowed_extras)
+ {
+       php_http_params_opts_t opts;
+       HashTable params;
 -      php_http_params_parse(&params, &opts TSRMLS_CC);
++      zend_hash_key k, arg_k;
++      zval *param, *val, *args, *arg;
+       php_http_params_opts_default_get(&opts);
+       opts.input.str = estrndup(str, len);
+       opts.input.len = len;
+       opts.param = NULL;
+       zend_hash_init(&params, 10, NULL, ZVAL_PTR_DTOR, 0);
 -      list = php_http_cookie_list_init(list TSRMLS_CC);
 -      FOREACH_HASH_KEYVAL(pos1, &params, key, param) {
 -              if (Z_TYPE_PP(param) == IS_ARRAY) {
 -                      if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(param), ZEND_STRS("value"), (void *) &val)) {
 -                              add_entry(list, NULL, flags, &key, *val);
++      php_http_params_parse(&params, &opts);
+       efree(opts.input.str);
 -                      if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(param), ZEND_STRS("arguments"), (void *) &args) && Z_TYPE_PP(args) == IS_ARRAY) {
 -                              FOREACH_KEYVAL(pos2, *args, key, arg) {
 -                                      add_entry(list, allowed_extras, flags, &key, *arg);
++      list = php_http_cookie_list_init(list);
++      ZEND_HASH_FOREACH_KEY_VAL(&params, k.h, k.key, param)
++      {
++              if (Z_TYPE_P(param) == IS_ARRAY) {
++                      if ((val = zend_hash_str_find(Z_ARRVAL_P(param), ZEND_STRL("value")))) {
++                              add_entry(list, NULL, flags, &k, val);
+                       }
 -      zval array, *cookies, *extras;
 -      TSRMLS_FETCH_FROM_CTX(list->ts);
 -      
 -      INIT_PZVAL_ARRAY(&array, HASH_OF(strct));
++                      if ((args = zend_hash_str_find(Z_ARRVAL_P(param), ZEND_STRL("arguments"))) && Z_TYPE_P(args) == IS_ARRAY) {
++                              ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(args), arg_k.h, arg_k.key, arg)
++                              {
++                                      add_entry(list, allowed_extras, flags, &arg_k, arg);
+                               }
++                              ZEND_HASH_FOREACH_END();
+                       }
+               }
+       }
++      ZEND_HASH_FOREACH_END();
++
+       zend_hash_destroy(&params);
+       return list;
+ }
+ void php_http_cookie_list_to_struct(php_http_cookie_list_t *list, zval *strct)
+ {
 -      MAKE_STD_ZVAL(cookies);
 -      array_init(cookies);
 -      zend_hash_copy(Z_ARRVAL_P(cookies), &list->cookies, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
 -      add_assoc_zval(&array, "cookies", cookies);
++      zval cookies, extras, tmp;
++      HashTable *ht = HASH_OF(strct);
+       
 -      MAKE_STD_ZVAL(extras);
 -      array_init(extras);
 -      zend_hash_copy(Z_ARRVAL_P(extras), &list->extras, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
 -      add_assoc_zval(&array, "extras", extras);
++      array_init_size(&cookies, zend_hash_num_elements(&list->cookies));
++      array_copy(&list->cookies, Z_ARRVAL(cookies));
++      zend_symtable_str_update(ht, ZEND_STRL("cookies"), &cookies);
+       
 -      add_assoc_long(&array, "flags", list->flags);
 -      add_assoc_long(&array, "expires", (long) list->expires);
 -      add_assoc_long(&array, "max-age", (long) list->max_age);
 -      add_assoc_string(&array, "path", STR_PTR(list->path), 1);
 -      add_assoc_string(&array, "domain", STR_PTR(list->domain), 1);
++      array_init_size(&extras, zend_hash_num_elements(&list->extras));
++      array_copy(&list->extras, Z_ARRVAL(extras));
++      zend_symtable_str_update(ht, ZEND_STRL("extras"), &extras);
+       
 -php_http_cookie_list_t *php_http_cookie_list_from_struct(php_http_cookie_list_t *list, zval *strct TSRMLS_DC)
++      ZVAL_LONG(&tmp, list->flags);
++      zend_symtable_str_update(ht, ZEND_STRL("flags"), &tmp);
++      ZVAL_LONG(&tmp, list->expires);
++      zend_symtable_str_update(ht, ZEND_STRL("expires"), &tmp);
++      ZVAL_LONG(&tmp, list->max_age);
++      zend_symtable_str_update(ht, ZEND_STRL("max-age"), &tmp);
++      ZVAL_STRING(&tmp, STR_PTR(list->path));
++      zend_symtable_str_update(ht, ZEND_STRL("path"), &tmp);
++      ZVAL_STRING(&tmp, STR_PTR(list->domain));
++      zend_symtable_str_update(ht, ZEND_STRL("domain"), &tmp);
+ }
 -      zval **tmp, *cpy;
 -      HashTable *ht = HASH_OF(strct);
 -      
 -      list = php_http_cookie_list_init(list TSRMLS_CC);
 -      
 -      if (SUCCESS == zend_hash_find(ht, "cookies", sizeof("cookies"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_ARRAY) {
 -              zend_hash_copy(&list->cookies, Z_ARRVAL_PP(tmp), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
++php_http_cookie_list_t *php_http_cookie_list_from_struct(php_http_cookie_list_t *list, zval *strct)
+ {
 -      if (SUCCESS == zend_hash_find(ht, "extras", sizeof("extras"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_ARRAY) {
 -              zend_hash_copy(&list->extras, Z_ARRVAL_PP(tmp), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
++      zval *tmp;
++      HashTable *ht;
++
++      ht = HASH_OF(strct);
++      list = php_http_cookie_list_init(list);
++
++      if ((tmp = zend_hash_str_find_ind(ht, ZEND_STRL("cookies"))) && Z_TYPE_P(tmp) == IS_ARRAY){
++              array_copy(Z_ARRVAL_P(tmp), &list->cookies);
+       }
 -      if (SUCCESS == zend_hash_find(ht, "flags", sizeof("flags"), (void *) &tmp)) {
 -              cpy = php_http_ztyp(IS_LONG, *tmp);
 -              list->flags = Z_LVAL_P(cpy);
 -              zval_ptr_dtor(&cpy);
++      if ((tmp = zend_hash_str_find_ind(ht, ZEND_STRL("extras"))) && Z_TYPE_P(tmp) == IS_ARRAY){
++              array_copy(Z_ARRVAL_P(tmp), &list->extras);
+       }
 -      if (SUCCESS == zend_hash_find(ht, "expires", sizeof("expires"), (void *) &tmp)) {
 -              if (Z_TYPE_PP(tmp) == IS_LONG) {
 -                      list->expires = Z_LVAL_PP(tmp);
++      if ((tmp = zend_hash_str_find_ind(ht, ZEND_STRL("flags")))) {
++              list->flags = zval_get_long(tmp);
+       }
 -                      long lval;
++      if ((tmp = zend_hash_str_find_ind(ht, ZEND_STRL("expires")))) {
++              if (Z_TYPE_P(tmp) == IS_LONG) {
++                      list->expires = Z_LVAL_P(tmp);
+               } else {
 -                      cpy = php_http_ztyp(IS_STRING, *tmp);
 -                      if (IS_LONG == is_numeric_string(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy), &lval, NULL, 0)) {
++                      zend_long lval;
++                      zend_string *lstr = zval_get_string(tmp);
 -                              list->expires = php_parse_date(Z_STRVAL_P(cpy), NULL);
++                      if (IS_LONG == is_numeric_string(lstr->val, lstr->len, &lval, NULL, 0)) {
+                               list->expires = lval;
+                       } else {
 -                      zval_ptr_dtor(&cpy);
++                              list->expires = php_parse_date(lstr->val, NULL);
+                       }
 -      if (SUCCESS == zend_hash_find(ht, "max-age", sizeof("max-age"), (void *) &tmp)) {
 -              if (Z_TYPE_PP(tmp) == IS_LONG) {
 -                      list->max_age = Z_LVAL_PP(tmp);
++                      zend_string_release(lstr);
+               }
+       }
 -                      long lval;
++      if ((tmp = zend_hash_str_find_ind(ht, ZEND_STRL("max-age")))) {
++              if (Z_TYPE_P(tmp) == IS_LONG) {
++                      list->max_age = Z_LVAL_P(tmp);
+               } else {
 -                      cpy = php_http_ztyp(IS_STRING, *tmp);
 -                      if (IS_LONG == is_numeric_string(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy), &lval, NULL, 0)) {
++                      zend_long lval;
++                      zend_string *lstr = zval_get_string(tmp);
 -                      zval_ptr_dtor(&cpy);
++                      if (IS_LONG == is_numeric_string(lstr->val, lstr->len, &lval, NULL, 0)) {
+                               list->max_age = lval;
+                       }
 -      if (SUCCESS == zend_hash_find(ht, "path", sizeof("path"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_STRING) {
 -              list->path = estrndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
++                      zend_string_release(lstr);
+               }
+       }
 -      if (SUCCESS == zend_hash_find(ht, "domain", sizeof("domain"), (void *) &tmp) && Z_TYPE_PP(tmp) == IS_STRING) {
 -              list->domain = estrndup(Z_STRVAL_PP(tmp), Z_STRLEN_PP(tmp));
++      if ((tmp = zend_hash_str_find_ind(ht, ZEND_STRL("path")))) {
++              zend_string *str = zval_get_string(tmp);
++
++              list->path = estrndup(str->val, str->len);
++              zend_string_release(str);
+       }
 -      char *enc_str[2];
 -      int enc_len[2];
++      if ((tmp = zend_hash_str_find_ind(ht, ZEND_STRL("domain")))) {
++              zend_string *str = zval_get_string(tmp);
++
++              list->domain = estrndup(str->val, str->len);
++              zend_string_release(str);
+       }
+       
+       return list;
+ }
+ static inline void append_encoded(php_http_buffer_t *buf, const char *key, size_t key_len, const char *val, size_t val_len)
+ {
 -      enc_str[0] = php_raw_url_encode(key, key_len, &enc_len[0]);
 -      enc_str[1] = php_raw_url_encode(val, val_len, &enc_len[1]);
++      zend_string *enc_str[2];
+       
 -      php_http_buffer_append(buf, enc_str[0], enc_len[0]);
++      enc_str[0] = php_raw_url_encode(key, key_len);
++      enc_str[1] = php_raw_url_encode(val, val_len);
+       
 -      php_http_buffer_append(buf, enc_str[1], enc_len[1]);
++      php_http_buffer_append(buf, enc_str[0]->val, enc_str[0]->len);
+       php_http_buffer_appends(buf, "=");
 -      efree(enc_str[0]);
 -      efree(enc_str[1]);
++      php_http_buffer_append(buf, enc_str[1]->val, enc_str[1]->len);
+       php_http_buffer_appends(buf, "; ");
+       
 -      zval **val;
 -      php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 -      HashPosition pos;
 -      TSRMLS_FETCH_FROM_CTX(list->ts);
++      zend_string_release(enc_str[0]);
++      zend_string_release(enc_str[1]);
+ }
+ void php_http_cookie_list_to_string(php_http_cookie_list_t *list, char **str, size_t *len)
+ {
+       php_http_buffer_t buf;
 -      
 -      FOREACH_HASH_KEYVAL(pos, &list->cookies, key, val) {
 -              zval *tmp = php_http_ztyp(IS_STRING, *val);
++      zend_hash_key key;
++      zval *val;
+       
+       php_http_buffer_init(&buf);
 -              php_http_array_hashkey_stringify(&key);
 -              append_encoded(&buf, key.str, key.len-1, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
 -              php_http_array_hashkey_stringfree(&key);
 -              zval_ptr_dtor(&tmp);
++      ZEND_HASH_FOREACH_KEY_VAL(&list->cookies, key.h, key.key, val)
++      {
++              zend_string *str = zval_get_string(val);
++              php_http_arrkey_t arrkey = {0};
 -              char *date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), list->expires, 0 TSRMLS_CC);
 -              php_http_buffer_appendf(&buf, "expires=%s; ", date);
 -              efree(date);
++              php_http_arrkey_stringify(&arrkey, &key);
++              append_encoded(&buf, arrkey.key->val, arrkey.key->len, str->val, str->len);
++              php_http_arrkey_dtor(&arrkey);
++              zend_string_release(str);
+       }
++      ZEND_HASH_FOREACH_END();
+       
+       if (list->domain && *list->domain) {
+               php_http_buffer_appendf(&buf, "domain=%s; ", list->domain);
+       }
+       if (list->path && *list->path) {
+               php_http_buffer_appendf(&buf, "path=%s; ", list->path);
+       }
+       if (list->expires >= 0) {
 -      FOREACH_HASH_KEYVAL(pos, &list->extras, key, val) {
 -              zval *tmp = php_http_ztyp(IS_STRING, *val);
 -
 -              php_http_array_hashkey_stringify(&key);
 -              append_encoded(&buf, key.str, key.len-1, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
 -              php_http_array_hashkey_stringfree(&key);
 -
 -              zval_ptr_dtor(&tmp);
++              zend_string *date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), list->expires, 0);
++              php_http_buffer_appendf(&buf, "expires=%s; ", date->val);
++              zend_string_release(date);
+       }
+       if (list->max_age >= 0) {
+               php_http_buffer_appendf(&buf, "max-age=%ld; ", list->max_age);
+       }
+       
 -zend_object_value php_http_cookie_object_new(zend_class_entry *ce TSRMLS_DC)
++      ZEND_HASH_FOREACH_KEY_VAL(&list->extras, key.h, key.key, val)
++      {
++              zend_string *str = zval_get_string(val);
++              php_http_arrkey_t arrkey;
++
++              php_http_arrkey_stringify(&arrkey, &key);
++              append_encoded(&buf, arrkey.key->val, arrkey.key->len, str->val, str->len);
++              php_http_arrkey_dtor(&arrkey);
++              zend_string_release(str);
+       }
++      ZEND_HASH_FOREACH_END();
+       
+       if (list->flags & PHP_HTTP_COOKIE_SECURE) {
+               php_http_buffer_appends(&buf, "secure; ");
+       }
+       if (list->flags & PHP_HTTP_COOKIE_HTTPONLY) {
+               php_http_buffer_appends(&buf, "httpOnly; ");
+       }
+       
+       php_http_buffer_fix(&buf);
+       *str = buf.data;
+       *len = buf.used;
+ }
+ static zend_object_handlers php_http_cookie_object_handlers;
 -      return php_http_cookie_object_new_ex(ce, NULL, NULL TSRMLS_CC);
++zend_object *php_http_cookie_object_new(zend_class_entry *ce)
+ {
 -zend_object_value php_http_cookie_object_new_ex(zend_class_entry *ce, php_http_cookie_list_t *list, php_http_cookie_object_t **ptr TSRMLS_DC)
++      return &php_http_cookie_object_new_ex(ce, NULL)->zo;
+ }
 -      o = ecalloc(sizeof(*o), 1);
 -      zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
 -      object_properties_init((zend_object *) o, ce);
++php_http_cookie_object_t *php_http_cookie_object_new_ex(zend_class_entry *ce, php_http_cookie_list_t *list)
+ {
+       php_http_cookie_object_t *o;
 -      if (ptr) {
 -              *ptr = o;
 -      }
 -
 -      o->zv.handle = zend_objects_store_put(o, NULL, php_http_cookie_object_free, NULL TSRMLS_CC);
 -      o->zv.handlers = &php_http_cookie_object_handlers;
 -
 -      return o->zv;
++      if (!ce) {
++              ce = php_http_cookie_class_entry;
++      }
++
++      o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
++      zend_object_std_init(&o->zo, ce);
++      object_properties_init(&o->zo, ce);
++      o->zo.handlers = &php_http_cookie_object_handlers;
+       if (list) {
+               o->list = list;
+       }
 -                      obj->list = php_http_cookie_list_init(NULL TSRMLS_CC); \
++      return o;
+ }
+ #define PHP_HTTP_COOKIE_OBJECT_INIT(obj) \
+       do { \
+               if (!obj->list) { \
 -zend_object_value php_http_cookie_object_clone(zval *this_ptr TSRMLS_DC)
++                      obj->list = php_http_cookie_list_init(NULL); \
+               } \
+       } while(0)
 -      php_http_cookie_object_t *new_obj, *old_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      zend_object_value ov;
++zend_object *php_http_cookie_object_clone(zval *obj)
+ {
 -      ov = php_http_cookie_object_new_ex(old_obj->zo.ce, php_http_cookie_list_copy(old_obj->list, NULL), &new_obj TSRMLS_CC);
 -      zend_objects_clone_members((zend_object *) new_obj, ov, (zend_object *) old_obj, Z_OBJ_HANDLE_P(getThis()) TSRMLS_CC);
++      php_http_cookie_object_t *new_obj, *old_obj = PHP_HTTP_OBJ(NULL, obj);
+       PHP_HTTP_COOKIE_OBJECT_INIT(old_obj);
 -      return ov;
++      new_obj = php_http_cookie_object_new_ex(old_obj->zo.ce, php_http_cookie_list_copy(old_obj->list, NULL));
++      zend_objects_clone_members(&new_obj->zo, &old_obj->zo);
 -void php_http_cookie_object_free(void *object TSRMLS_DC)
++      return &new_obj->zo;
+ }
 -      php_http_cookie_object_t *obj = object;
++void php_http_cookie_object_free(zend_object *object)
+ {
 -      zend_object_std_dtor((zend_object *) obj TSRMLS_CC);
 -      efree(obj);
++      php_http_cookie_object_t *obj = PHP_HTTP_OBJ(object, NULL);
+       php_http_cookie_list_free(&obj->list);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!lH", &zcookie, &flags, &allowed_extras), invalid_arg, return);
++      zend_object_std_dtor(object);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie___construct, 0, 0, 0)
+       ZEND_ARG_INFO(0, cookie_string)
+       ZEND_ARG_INFO(0, parser_flags)
+       ZEND_ARG_INFO(0, allowed_extras)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpCookie, __construct)
+ {
+       php_http_cookie_object_t *obj;
+       zval *zcookie = NULL;
+       long flags = 0;
+       char **ae = NULL;
+       HashTable *allowed_extras = NULL;
+       zend_error_handling zeh;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!lH", &zcookie, &flags, &allowed_extras), invalid_arg, return);
 -      zend_replace_error_handling(EH_THROW, php_http_exception_runtime_class_entry, &zeh TSRMLS_CC);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
 -                      HashPosition pos;
 -                      zval **val;
++      zend_replace_error_handling(EH_THROW, php_http_exception_runtime_class_entry, &zeh);
+       if (zcookie) {
+               if (allowed_extras && zend_hash_num_elements(allowed_extras)) {
+                       char **ae_ptr = safe_emalloc(zend_hash_num_elements(allowed_extras) + 1, sizeof(char *), 0);
 -                      FOREACH_HASH_VAL(pos, allowed_extras, val) {
 -                              zval *cpy = php_http_ztyp(IS_STRING, *val);
++                      zval *val;
+                       ae = ae_ptr;
 -                              *ae_ptr++ = estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
 -                              zval_ptr_dtor(&cpy);
++                      ZEND_HASH_FOREACH_VAL(allowed_extras, val)
++                      {
++                              zend_string *str = zval_get_string(val);
 -                              if (instanceof_function(Z_OBJCE_P(zcookie), php_http_cookie_class_entry TSRMLS_CC)) {
 -                                      php_http_cookie_object_t *zco = zend_object_store_get_object(zcookie TSRMLS_CC);
++                              *ae_ptr++ = estrndup(str->val, str->len);
++                              zend_string_release(str);
+                       }
++                      ZEND_HASH_FOREACH_END();
+                       *ae_ptr = NULL;
+               }
+               switch (Z_TYPE_P(zcookie)) {
+                       case IS_OBJECT:
 -                              obj->list = php_http_cookie_list_from_struct(obj->list, zcookie TSRMLS_CC);
++                              if (instanceof_function(Z_OBJCE_P(zcookie), php_http_cookie_class_entry)) {
++                                      php_http_cookie_object_t *zco = PHP_HTTP_OBJ(NULL, zcookie);
+                                       if (zco->list) {
+                                               obj->list = php_http_cookie_list_copy(zco->list, NULL);
+                                       }
+                                       break;
+                               }
+                               /* no break */
+                       case IS_ARRAY:
 -                              zval *cpy = php_http_ztyp(IS_STRING, zcookie);
++                              obj->list = php_http_cookie_list_from_struct(obj->list, zcookie);
+                               break;
+                       default: {
 -                              obj->list = php_http_cookie_list_parse(obj->list, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy), flags, ae TSRMLS_CC);
 -                              zval_ptr_dtor(&cpy);
++                              zend_string *str = zval_get_string(zcookie);
 -      zend_restore_error_handling(&zeh TSRMLS_CC);
++                              obj->list = php_http_cookie_list_parse(obj->list, str->val, str->len, flags, ae);
++                              zend_string_release(str);
+                               break;
+                       }
+               }
+               if (ae) {
+                       char **ae_ptr;
+                       for (ae_ptr = ae; *ae_ptr; ++ae_ptr) {
+                               efree(*ae_ptr);
+                       }
+                       efree(ae);
+               }
+       }
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      zend_restore_error_handling(&zeh);
+       PHP_HTTP_COOKIE_OBJECT_INIT(obj);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_getCookies, 0, 0, 0)
+ ZEND_END_ARG_INFO();;
+ static PHP_METHOD(HttpCookie, getCookies)
+ {
+       php_http_cookie_object_t *obj;
+       if (SUCCESS != zend_parse_parameters_none()) {
+               return;
+       }
 -      array_init(return_value);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       PHP_HTTP_COOKIE_OBJECT_INIT(obj);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|H", &cookies), invalid_arg, return);
++      array_init_size(return_value, zend_hash_num_elements(&obj->list->cookies));
+       array_copy(&obj->list->cookies, Z_ARRVAL_P(return_value));
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_setCookies, 0, 0, 0)
+       ZEND_ARG_INFO(0, cookies)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpCookie, setCookies)
+ {
+       HashTable *cookies = NULL;
+       php_http_cookie_object_t *obj;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|H", &cookies), invalid_arg, return);
 -      RETVAL_ZVAL(getThis(), 1, 0);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       PHP_HTTP_COOKIE_OBJECT_INIT(obj);
+       zend_hash_clean(&obj->list->cookies);
+       if (cookies) {
+               array_copy_strings(cookies, &obj->list->cookies);
+       }
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &cookies), invalid_arg, return);
++      RETURN_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_addCookies, 0, 0, 1)
+       ZEND_ARG_INFO(0, cookies)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpCookie, addCookies)
+ {
+       HashTable *cookies = NULL;
+       php_http_cookie_object_t *obj;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "H", &cookies), invalid_arg, return);
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       PHP_HTTP_COOKIE_OBJECT_INIT(obj);
+       array_join(cookies, &obj->list->cookies, 1, ARRAY_JOIN_STRONLY|ARRAY_JOIN_STRINGIFY);
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_getExtras, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpCookie, getExtras)
+ {
+       php_http_cookie_object_t *obj;
+       if (SUCCESS != zend_parse_parameters_none()) {
+               return;
+       }
 -      array_init(return_value);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       PHP_HTTP_COOKIE_OBJECT_INIT(obj);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|H", &extras), invalid_arg, return);
++      array_init_size(return_value, zend_hash_num_elements(&obj->list->extras));
+       array_copy(&obj->list->extras, Z_ARRVAL_P(return_value));
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_setExtras, 0, 0, 0)
+       ZEND_ARG_INFO(0, extras)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpCookie, setExtras)
+ {
+       HashTable *extras = NULL;
+       php_http_cookie_object_t *obj;
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|H", &extras), invalid_arg, return);
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H", &extras), invalid_arg, return);
++      obj = PHP_HTTP_OBJ(NULL, getThis());
+       PHP_HTTP_COOKIE_OBJECT_INIT(obj);
+       zend_hash_clean(&obj->list->extras);
+       if (extras) {
+               array_copy_strings(extras, &obj->list->extras);
+       }
+       RETVAL_ZVAL(getThis(), 1, 0);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpCookie_addExtras, 0, 0, 1)
+       ZEND_ARG_INFO(0, extras)