Merge branch 'master' into phpng
authorMichael Wallner <mike@php.net>
Wed, 18 Feb 2015 09:33:35 +0000 (10:33 +0100)
committerMichael Wallner <mike@php.net>
Wed, 18 Feb 2015 10:26:54 +0000 (11:26 +0100)
Conflicts:
package.xml
php_http_client_curl.c
php_http_env.c
php_http_env_response.c
php_http_header_parser.c
php_http_message.c
php_http_message_body.c
php_http_message_parser.c
php_http_misc.c
php_http_strlist.c
php_http_url.h
tests/info.phpt

20 files changed:
1  2 
config9.m4
package.xml
php_http.c
php_http_api.h
php_http_client.c
php_http_client.h
php_http_client_curl.c
php_http_env.c
php_http_env_response.c
php_http_header.c
php_http_header_parser.c
php_http_header_parser.h
php_http_message.c
php_http_message_body.c
php_http_message_parser.c
php_http_message_parser.h
php_http_misc.h
php_http_url.h
tests/messageparser002.phpt
tests/phpinfo.phpt

diff --combined config9.m4
index 46f70f9f4d2c501a518b75c6bd5026562a904bb7,af3d55fd25531df5b77f6a1f8ecc07cf759f06d8..8a1011818c9fd9a00c0f8ae9b60abce5604fe743
@@@ -547,7 -547,6 +547,6 @@@ dnl ---
                php_http_options.c \
                php_http_params.c \
                php_http_querystring.c \
-               php_http_strlist.c \
                php_http_url.c \
                php_http_version.c \
        "
                php_http_options.h \
                php_http_params.h \
                php_http_querystring.h \
-               php_http_strlist.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)
  
        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
diff --combined package.xml
index 726d2c2a740c7dda83183c00501e663eece77b28,4f04faed49e5cd56df4a8e93086df77c943d5567..8fcb5379f78d165131f18849fcf7d440f20e2f35
@@@ -22,7 -22,13 +22,7 @@@ arbitrary data with caching and resumin
  It provides powerful request functionality with support for
  parallel requests.
  
 -Documentation:
 -v1: http://php.net/http
 -v2: http://devel-m6w6.rhcloud.com/mdref/http
 -
 -Code Coverage:
 -v1: http://dev.iworks.at/ext-http/lcov_html/ext/http/
 -v2: http://dev.iworks.at/ext-http/lcov/ext/http/
 +http://devel-m6w6.rhcloud.com/mdref/http
  
  ]]></description>
   <lead>
    <email>mike@php.net</email>
    <active>yes</active>
   </lead>
 - <date>2015-01-27</date>
 + <date>2014-12-17</date>
   <version>
 -  <release>2.3.0</release>
 -  <api>2.3.0</api>
 +  <release>3.0.0dev</release>
 +  <api>3.0.0</api>
   </version>
   <stability>
    <release>beta</release>
-   <api>stable</api>
+   <api>beta</api>
   </stability>
   <license>BSD, revised</license>
   <notes><![CDATA[
- *
+ + Preliminiary HTTP2 support for http\Client (libcurl with nghttp2 support)
+ + Improved performance of HTTP info parser (request/response line)
+ + Improved performance of updating client observers
+ + Improved performance of http\Env\Response output to streams
+ + Improved the error messages of the header parser
+ + Added http\Header\Parser class
+ + Added http\Client::configure() method accepting an array with the following options for libcurl:
+   . maxconnects (int, size of the connection cache)
+   . max_host_connections (int, max number of connections to a single host, libcurl >= 7.30.0)
+   . max_pipeline_length (int, max number of requests in a pipeline, libcurl >= 7.30.0)
+   . max_total_connections (int, max number of simultaneous open connections of this client, libcurl >= 7.30.0)
+   . pipelining (bool, whether to enable HTTP/1.1 pipelining)
+   . chunk_length_penalty_size (int, chunk length threshold for pipelining, libcurl >= 7.30.0)
+   . content_length_penalty_size (int, size threshold for pipelining, libcurl >= 7.30.0)
+   . pipelining_server_bl (array, list of server software names to blacklist for pipelining, libcurl >= 7.30.0)
+   . pipelining_site_bl (array, list of server host names to blacklist for pipelining, libcurl >= 7.30.0)
+   . use_eventloop (bool, whether to use libevent, libcurl+libevent)
+ + Added http\Client::getAvailableOptions() and http\Client::getAvailableConfiguration() methods
+ + Added support for HTTP2 if libcurl was built with nghttp2 support.
+ + Added http\Client\Curl\HTTP_VERSION_2_0 constant (libcurl >= 7.33.0)
+ + Added http\Client\Curl\TLS_AUTH_SRP constant (libcurl >= 7.21.4)
+ + Added pinned_publickey SSL request option (libcurl >= 7.39.0)
+ + Added tlsauthtype, tlsauthuser and tlsauthpass SSL request option (libcurl >= 7.21.4)
+ + Added verifystatus (a.k.a OCSP) SSL request option (libcurl >= 7.41.0)
+ + Added proxyheader request option (libcurl >= 7.37.0)
+ + Added unix_socket_path request option (libcurl >= 7.40.0)
+ * Fixed compress request option
+ * Fixed parsing authorities of CONNECT messages
+ * Fixed parsing Content-Range messages
+ * Fixed http\Env\Response to default to chunked encoding over streams
+ * Fixed superfluous output of Content-Length:0 headers
+ * Fixed persistent easy handles to be only created for persistent multi handles
+ * Fixed the header parser to accept not-yet-complete header lines
+ * Fixed http\Message::toStream() crash in ZTS mode
+ * Fixed the message stream parser to handle intermediary data bigger than 4k
+ * Fixed the message stream parser to handle single header lines without EOL
+ * Fixed http\Message\Body to not generate stat based etags for temporary streams
+ - Deprecated http\Client::enablePipelining(), use http\Client::configure(["pipelining" => true]) instead
+ - Deprecated http\Client::enableEvents(), use http\Client::configure(["use_eventloop" => true]) instead
+ - Removed the cookies entry from the transfer info, wich was very slow and generated a Netscape formatted list of cookies
+ - Changed the header parser to reject illegal characters
  ]]></notes>
   <contents>
    <dir name="/">
     <file role="src" name="php_http_params.h"/>
     <file role="src" name="php_http_querystring.c"/>
     <file role="src" name="php_http_querystring.h"/>
-    <file role="src" name="php_http_strlist.c"/>
-    <file role="src" name="php_http_strlist.h"/>
+    <file role="src" name="php_http_response_codes.h"/>
     <file role="src" name="php_http_url.c"/>
     <file role="src" name="php_http_url.h"/>
     <file role="src" name="php_http_utf8.h"/>
  
     <dir name="tests">
      <file role="test" name="skipif.inc"/>
-     <dir name="data">
-      <file role="test" name="message_r_multipart_put.txt"/>
-      <file role="test" name="message_rr_empty.txt"/>
-      <file role="test" name="message_rr_empty_chunked.txt"/>
-      <file role="test" name="message_rr_empty_gzip.txt"/>
-      <file role="test" name="message_rr_helloworld_chunked.txt"/>
-      <file role="test" name="urls.txt"/>
-     </dir>
+      <dir name="data">
+       <file role="test" name="message_r_content_range.txt"/>
+       <file role="test" name="message_r_multipart_put.txt"/>
+       <file role="test" name="message_rr_empty.txt"/>
+       <file role="test" name="message_rr_empty_chunked.txt"/>
+       <file role="test" name="message_rr_empty_gzip.txt"/>
+       <file role="test" name="message_rr_helloworld_chunked.txt"/>
+       <file role="test" name="urls.txt"/>
+      </dir>
+      <dir name="helper">
+       <file role="test" name="cookie.inc"/>
+       <file role="test" name="http2.crt"/>
+       <file role="test" name="http2.key"/>
+       <file role="test" name="pipeline.inc"/>
+       <file role="test" name="proxy.inc"/>
+       <file role="test" name="server.inc"/>
+       <dir name="html">
+        <file role="test" name="index.html"/>
+       </dir>
+      </dir>
       <file role="test" name="bug61444.phpt"/>
       <file role="test" name="bug66388.phpt"/>
+      <file role="test" name="bug66891.phpt"/>
       <file role="test" name="bug67932.phpt"/>
+      <file role="test" name="bug69000.phpt"/>
       <file role="test" name="client001.phpt"/>
       <file role="test" name="client002.phpt"/>
       <file role="test" name="client003.phpt"/>
       <file role="test" name="client014.phpt"/>
       <file role="test" name="client015.phpt"/>
       <file role="test" name="client016.phpt"/>
+      <file role="test" name="client017.phpt"/>
+      <file role="test" name="client018.phpt"/>
+      <file role="test" name="client019.phpt"/>
+      <file role="test" name="client020.phpt"/>
+      <file role="test" name="client021.phpt"/>
+      <file role="test" name="client022.phpt"/>
+      <file role="test" name="client023.phpt"/>
+      <file role="test" name="client024.phpt"/>
+      <file role="test" name="client025.phpt"/>
+      <file role="test" name="client026.phpt"/>
       <file role="test" name="clientrequest001.phpt"/>
       <file role="test" name="clientrequest002.phpt"/>
       <file role="test" name="clientrequest003.phpt"/>
       <file role="test" name="envresponse014.phpt"/>
       <file role="test" name="envresponse015.phpt"/>
       <file role="test" name="envresponse016.phpt"/>
+      <file role="test" name="envresponse017.phpt"/>
+      <file role="test" name="envresponse018.phpt"/>
       <file role="test" name="envresponsebody001.phpt"/>
       <file role="test" name="envresponsebody002.phpt"/>
       <file role="test" name="envresponsecodes.phpt"/>
       <file role="test" name="header007.phpt"/>
       <file role="test" name="header008.phpt"/>
       <file role="test" name="header009.phpt"/>
-      <file role="test" name="info_001.phpt"/>
-      <file role="test" name="info.phpt"/>
+      <file role="test" name="headerparser001.phpt"/>
+      <file role="test" name="headerparser002.phpt"/>
+      <file role="test" name="headerparser003.phpt"/>
+      <file role="test" name="info001.phpt"/>
+      <file role="test" name="info002.phpt"/>
       <file role="test" name="message001.phpt"/>
       <file role="test" name="message002.phpt"/>
       <file role="test" name="message003.phpt"/>
       <file role="test" name="message013.phpt"/>
       <file role="test" name="message014.phpt"/>
       <file role="test" name="message015.phpt"/>
+      <file role="test" name="message016.phpt"/>
       <file role="test" name="messagebody001.phpt"/>
       <file role="test" name="messagebody002.phpt"/>
       <file role="test" name="messagebody003.phpt"/>
       <file role="test" name="params013.phpt"/>
       <file role="test" name="params014.phpt"/>
       <file role="test" name="params015.phpt"/>
+      <file role="test" name="phpinfo.phpt"/>
       <file role="test" name="propertyproxy001.phpt"/>
       <file role="test" name="querystring001.phpt"/>
       <file role="test" name="querystring002.phpt"/>
       <file role="test" name="urlparser008.phpt"/>
       <file role="test" name="urlparser009.phpt"/>
       <file role="test" name="urlparser010.phpt"/>
+      <file role="test" name="urlparser011.phpt"/>
       <file role="test" name="version001.phpt"/>
     </dir>
    </dir>
diff --combined php_http.c
index 7a28933b03e37be7f6f99ba6c1690992d7caab35,17d9925b5af8d3b01409b613aeb2dca69f76545b..98df8433b403f1c3b12113204c660ea00aa97339
@@@ -117,6 -117,14 +117,6 @@@ static inline void php_http_globals_fre
  }
  #endif
  
 -#if ZTS && PHP_DEBUG && !HAVE_GCOV
 -zend_php_http_globals *php_http_globals(void)
 -{
 -      TSRMLS_FETCH();
 -      return PHP_HTTP_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()
@@@ -128,12 -136,12 +128,13 @@@ PHP_MINIT_FUNCTION(http
        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)
diff --combined php_http_api.h
index 5aa96b7efcdb821bfdd46e9a5451a0c125e73cbd,712c8cb1aedc3f53ed52984db9fa6ef888f1083e..68fdac88015029a05592dda8732e12d17288a2bb
@@@ -41,6 -41,9 +41,6 @@@
  # define PHP_HTTP_API extern
  #endif
  
 -/* make functions that return SUCCESS|FAILURE more obvious */
 -typedef int STATUS;
 -
  #if (defined(HAVE_ICONV) || defined(PHP_HTTP_HAVE_EXT_ICONV)) && (PHP_HTTP_SHARED_DEPS || !defined(COMPILE_DL_ICONV))
  #     define PHP_HTTP_HAVE_ICONV
  #endif
@@@ -76,7 -79,6 +76,6 @@@
  #include "php_http.h"
  
  #include "php_http_buffer.h"
- #include "php_http_strlist.h"
  #include "php_http_misc.h"
  #include "php_http_options.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 PHP_HTTP_G ((zend_php_http_globals *) (*((void ***) tsrm_ls))[TSRM_UNSHUFFLE_RSRC_ID(php_http_globals_id)])
 +#     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) void ***tsrm_ls = ((ctx)?(ctx):ts_resource_ex(0, NULL))
 +#     define TSRMLS_FETCH_FROM_CTX(ctx) ERROR
  #else
  #     define PHP_HTTP_G (&php_http_globals)
  #endif
diff --combined php_http_client.c
index 22fc6ca0497960ec60a2a8a58c648415c3cbfcaf,4f0de3cb6fc604334470937c0881b4efea3e56a2..467ed2a4197c54eabe769ffbac501caf876ae439
   */
  static HashTable php_http_client_drivers;
  
 -STATUS php_http_client_driver_add(php_http_client_driver_t *driver)
 +static void php_http_client_driver_hash_dtor(zval *pData)
  {
 -      return zend_hash_add(&php_http_client_drivers, driver->name_str, driver->name_len + 1, (void *) driver, sizeof(php_http_client_driver_t), NULL);
 +      pefree(Z_PTR_P(pData), 1);
  }
  
 -STATUS php_http_client_driver_get(const char *name_str, size_t name_len, php_http_client_driver_t *driver)
 +ZEND_RESULT_CODE php_http_client_driver_add(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;
 +}
 +
 +php_http_client_driver_t *php_http_client_driver_get(zend_string *name)
 +{
 +      zval *ztmp;
        php_http_client_driver_t *tmp;
  
 -      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;
 +      if (name && (tmp = zend_hash_find_ptr(&php_http_client_drivers, name))) {
 +              return tmp;
        }
 -      return FAILURE;
 +      if ((ztmp = zend_hash_get_current_data(&php_http_client_drivers))) {
 +              return Z_PTR_P(ztmp);
 +      }
 +      return NULL;
  }
  
 -static int apply_driver_list(void *p, void *arg TSRMLS_DC)
 +static int apply_driver_list(zval *p, void *arg)
  {
 -      php_http_client_driver_t *d = p;
 -      zval *zname;
 +      php_http_client_driver_t *d = Z_PTR_P(p);
 +      zval zname;
  
 -      MAKE_STD_ZVAL(zname);
 -      ZVAL_STRINGL(zname, d->name_str, d->name_len, 1);
 +      ZVAL_STR_COPY(&zname, d->driver_name);
  
 -      zend_hash_next_index_insert(arg, &zname, sizeof(zval *), NULL);
 +      zend_hash_next_index_insert(arg, &zname);
        return ZEND_HASH_APPLY_KEEP;
  }
  
 -void php_http_client_driver_list(HashTable *ht TSRMLS_DC)
 +void php_http_client_driver_list(HashTable *ht)
  {
 -      zend_hash_apply_with_argument(&php_http_client_drivers, apply_driver_list, ht TSRMLS_CC);
 +      zend_hash_apply_with_argument(&php_http_client_drivers, apply_driver_list, ht);
  }
  
 -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_subr(zval *instance, char *key, size_t len, zval *opts, int overwrite)
  {
        if (overwrite || (opts && zend_hash_num_elements(Z_ARRVAL_P(opts)))) {
 -              zend_class_entry *this_ce = Z_OBJCE_P(getThis());
 -              zval *old_opts, *new_opts, **entry = NULL;
 +              zend_class_entry *this_ce = Z_OBJCE_P(instance);
 +              zval old_opts_tmp, *old_opts, new_opts, *entry = NULL;
  
 -              MAKE_STD_ZVAL(new_opts);
 -              array_init(new_opts);
 -              old_opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
 +              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) {
 -                      array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
 +                      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_update(Z_ARRVAL_P(new_opts), key, len, (void *) &opts, sizeof(zval *), NULL);
 +                              zend_symtable_str_update(Z_ARRVAL(new_opts), key, len, opts);
                        } else {
 -                              zend_symtable_del(Z_ARRVAL_P(new_opts), key, len);
 +                              zend_symtable_str_del(Z_ARRVAL(new_opts), key, len);
                        }
                } else if (opts && zend_hash_num_elements(Z_ARRVAL_P(opts))) {
 -                      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);
 +                      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_symtable_update(Z_ARRVAL_P(new_opts), key, len, (void *) &opts, sizeof(zval *), NULL);
 +                              zend_symtable_str_update(Z_ARRVAL(new_opts), key, len, opts);
                        }
                }
  
 -              zend_update_property(this_ce, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC);
 +              zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts);
                zval_ptr_dtor(&new_opts);
        }
  }
  
 -void php_http_client_options_set(zval *this_ptr, zval *opts TSRMLS_DC)
 +void php_http_client_options_set(zval *instance, zval *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);
 +      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);
  
 -      MAKE_STD_ZVAL(new_opts);
 -      array_init(new_opts);
 +      array_init(&new_opts);
  
        if (!opts || !zend_hash_num_elements(Z_ARRVAL_P(opts))) {
 -              zend_update_property(this_ce, getThis(), ZEND_STRL("options"), new_opts TSRMLS_CC);
 +              zend_update_property(this_ce, instance, ZEND_STRL("options"), &new_opts);
                zval_ptr_dtor(&new_opts);
        } else {
 -              zval *old_opts, *add_opts, **opt;
 +              zval old_opts_tmp, *old_opts, add_opts, *opt;
  
 -              MAKE_STD_ZVAL(add_opts);
 -              array_init(add_opts);
 +              array_init(&add_opts);
                /* some options need extra attention -- thus cannot use array_merge() directly */
 -              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);
 +              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) {
 -                                              zend_symtable_del(Z_ARRVAL_P(old_opts), key.str, key.len);
 +                                              zend_symtable_del(Z_ARRVAL_P(old_opts), key.key);
                                        }
                                } else {
 -                                      Z_ADDREF_P(*opt);
 -                                      add_assoc_zval_ex(add_opts, key.str, key.len, *opt);
 +                                      Z_TRY_ADDREF_P(opt);
 +                                      add_assoc_zval_ex(&add_opts, key.key->val, key.key->len, opt);
                                }
                        }
                }
 +              ZEND_HASH_FOREACH_END();
  
 -              old_opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
 +              old_opts = zend_read_property(this_ce, instance, ZEND_STRL("options"), 0, &old_opts_tmp);
                if (Z_TYPE_P(old_opts) == IS_ARRAY) {
 -                      array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL_P(new_opts));
 +                      array_copy(Z_ARRVAL_P(old_opts), Z_ARRVAL(new_opts));
                }
 -              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_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);
        }
  }
  
 -void php_http_client_options_get_subr(zval *this_ptr, char *key, size_t len, zval *return_value TSRMLS_DC)
 +void php_http_client_options_get_subr(zval *instance, char *key, size_t len, zval *return_value)
  {
 -      zend_class_entry *this_ce = Z_OBJCE_P(getThis());
 -      zval **options, *opts = zend_read_property(this_ce, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
 +      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);
  
 -      if ((Z_TYPE_P(opts) == IS_ARRAY) && (SUCCESS == zend_symtable_find(Z_ARRVAL_P(opts), key, len, (void *) &options))) {
 -              RETVAL_ZVAL(*options, 1, 0);
 +      if ((Z_TYPE_P(opts) == IS_ARRAY) && (options = zend_symtable_str_find(Z_ARRVAL_P(opts), key, len))) {
 +              RETVAL_ZVAL_FAST(options);
        }
  }
  
@@@ -161,7 -157,7 +161,7 @@@ static void queue_dtor(void *enqueued
        }
  }
  
 -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_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;
  
        }
        zend_llist_init(&h->requests, sizeof(php_http_client_enqueue_t), queue_dtor, 0);
        zend_llist_init(&h->responses, sizeof(void *), NULL, 0);
 -      TSRMLS_SET_CTX(h->ts);
  
        if (h->ops->init) {
                if (!(h = h->ops->init(h, init_arg))) {
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not initialize client");
 -                      if (free_h) {
 -                              efree(free_h);
 -                      }
 +                      php_error_docref(NULL, E_WARNING, "Could not initialize client");
 +                      PTR_FREE(free_h);
                }
        }
  
@@@ -217,11 -216,13 +217,11 @@@ void php_http_client_free(php_http_clie
        }
  }
  
 -STATUS php_http_client_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
 +ZEND_RESULT_CODE php_http_client_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
  {
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
 -
        if (h->ops->enqueue) {
                if (php_http_client_enqueued(h, enqueue->request, NULL)) {
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to enqueue request; request already in queue");
 +                      php_error_docref(NULL, E_WARNING, "Failed to enqueue request; request already in queue");
                        return FAILURE;
                }
                return h->ops->enqueue(h, enqueue);
        return FAILURE;
  }
  
 -STATUS php_http_client_dequeue(php_http_client_t *h, php_http_message_t *request)
 +ZEND_RESULT_CODE php_http_client_dequeue(php_http_client_t *h, php_http_message_t *request)
  {
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
 -
        if (h->ops->dequeue) {
                php_http_client_enqueue_t *enqueue = php_http_client_enqueued(h, request, NULL);
  
                if (!enqueue) {
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to dequeue request; request not in queue");
 +                      php_error_docref(NULL, E_WARNING, "Failed to dequeue request; request not in queue");
                        return FAILURE;
                }
                return h->ops->dequeue(h, enqueue);
@@@ -264,7 -267,7 +264,7 @@@ php_http_client_enqueue_t *php_http_cli
        return el ? (php_http_client_enqueue_t *) el->data : NULL;
  }
  
 -STATUS php_http_client_wait(php_http_client_t *h, struct timeval *custom_timeout)
 +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);
@@@ -282,7 -285,7 +282,7 @@@ int php_http_client_once(php_http_clien
        return FAILURE;
  }
  
 -STATUS php_http_client_exec(php_http_client_t *h)
 +ZEND_RESULT_CODE php_http_client_exec(php_http_client_t *h)
  {
        if (h->ops->exec) {
                return h->ops->exec(h);
@@@ -301,7 -304,7 +301,7 @@@ void php_http_client_reset(php_http_cli
        zend_llist_clean(&h->responses);
  }
  
 -STATUS php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
 +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;
  }
  
 -STATUS php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void *res_ptr)
 +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);
  zend_class_entry *php_http_client_class_entry;
  static zend_object_handlers php_http_client_object_handlers;
  
 -void php_http_client_object_free(void *object TSRMLS_DC)
 +void php_http_client_object_free(zend_object *object)
  {
 -      php_http_client_object_t *o = (php_http_client_object_t *) object;
 +      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_std_dtor((zend_object *) o TSRMLS_CC);
 -      efree(o);
 +      zend_object_std_dtor(object);
  }
  
 -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)
 +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;
  
 -      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);
 +      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;
  
 -      if (ptr) {
 -              *ptr = o;
 -      }
 -
 -      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;
 +      o->zo.handlers = &php_http_client_object_handlers;
  
 -      return o->zv;
 +      return o;
  }
  
 -zend_object_value php_http_client_object_new(zend_class_entry *ce TSRMLS_DC)
 +zend_object *php_http_client_object_new(zend_class_entry *ce)
  {
 -      return php_http_client_object_new_ex(ce, NULL, NULL TSRMLS_CC);
 +      return &php_http_client_object_new_ex(ce, NULL)->zo;
  }
  
 -static void handle_history(zval *zclient, php_http_message_t *request, php_http_message_t *response TSRMLS_DC)
 +static void handle_history(zval *zclient, php_http_message_t *request, php_http_message_t *response)
  {
 -      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);
 +      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);
  
 -      MAKE_STD_ZVAL(new_hist);
 -      ZVAL_OBJVAL(new_hist, ov, 0);
 +      ZVAL_OBJ(&new_hist, &obj->zo);
  
        if (Z_TYPE_P(old_hist) == IS_OBJECT) {
 -              php_http_message_object_prepend(new_hist, old_hist, 1 TSRMLS_CC);
 +              php_http_message_object_prepend(&new_hist, old_hist, 1);
        }
  
 -      zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), new_hist TSRMLS_CC);
 +      zend_update_property(php_http_client_class_entry, zclient, ZEND_STRL("history"), &new_hist);
        zval_ptr_dtor(&new_hist);
  }
  
 -static STATUS handle_response(void *arg, php_http_client_t *client, php_http_client_enqueue_t *e, php_http_message_t **response)
 +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;
 -      TSRMLS_FETCH_FROM_CTX(client->ts);
  
 -      INIT_PZVAL(&zclient);
 -      ZVAL_OBJVAL(&zclient, ((php_http_client_object_t*) arg)->zv, 0);
 +      ZVAL_OBJ(&zclient, &((php_http_client_object_t*) arg)->zo);
  
        if ((msg = *response)) {
                php_http_message_object_t *msg_obj;
 -              zval *info, *zresponse, *zrequest;
 +              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);
  
 -              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);
 +              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;
  
 -              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);
 +              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(zrequest);
 -              ZVAL_OBJVAL(zrequest, ((php_http_message_object_t *) e->opaque)->zv, 1);
 +              php_http_message_object_prepend(&zresponse, &zrequest, 1);
  
 -              php_http_message_object_prepend(zresponse, zrequest, 1 TSRMLS_CC);
 -
 -              MAKE_STD_ZVAL(info);
 -              object_init(info);
 -              info_ht = HASH_OF(info);
 +              object_init(&info);
 +              info_ht = HASH_OF(&info);
                php_http_client_getopt(client, PHP_HTTP_CLIENT_OPT_TRANSFER_INFO, e->request, &info_ht);
 -              zend_update_property(php_http_client_response_class_entry, zresponse, ZEND_STRL("transferInfo"), info TSRMLS_CC);
 +              zend_update_property(php_http_client_response_class_entry, &zresponse, ZEND_STRL("transferInfo"), &info);
                zval_ptr_dtor(&info);
  
 -              zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
 +              Z_ADDREF(zresponse);
                zend_llist_add_element(&client->responses, &msg_obj);
  
                if (e->closure.fci.size) {
 -                      zval *retval = NULL;
 +                      zval retval;
                        zend_error_handling zeh;
  
 -                      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_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);
  
 -                      if (retval) {
 -                              if (Z_TYPE_P(retval) == IS_BOOL) {
 -                                      dequeue = Z_BVAL_P(retval);
 -                              }
 -                              zval_ptr_dtor(&retval);
 +                      if (Z_TYPE(retval) == IS_TRUE) {
 +                              dequeue = 1;
                        }
 +                      zval_ptr_dtor(&retval);
                }
  
                zval_ptr_dtor(&zresponse);
  
  static void handle_progress(void *arg, php_http_client_t *client, php_http_client_enqueue_t *e, php_http_client_progress_state_t *progress)
  {
 -      zval *zrequest, *zprogress, *zclient, **args[2];
 +      zval zclient, args[2];
        php_http_client_object_t *client_obj = arg;
        zend_error_handling zeh;
 -      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_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);
 -      zval_ptr_dtor(&zrequest);
 -      zval_ptr_dtor(&zprogress);
 +      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;
 -      TSRMLS_FETCH_FROM_CTX(msg_obj->message->ts);
  
 -      zend_objects_store_del_ref_by_handle_ex(msg_obj->zv.handle, msg_obj->zv.handlers TSRMLS_CC);
 +      zend_objects_store_del(&msg_obj->zo);
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_construct, 0, 0, 0)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpClient, __construct)
  {
 -              char *driver_str = NULL, *persistent_handle_str = NULL;
 -              int driver_len = 0, persistent_handle_len = 0;
 -              php_http_client_driver_t driver;
 +              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;
 -              zval *os;
 +              zval os;
  
 -              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);
 +              php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|S!S!", &driver_name, &persistent_handle_name), invalid_arg, return);
  
 -              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);
 +              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;
                }
  
 -              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);
 +              object_init_ex(&os, spl_ce_SplObjectStorage);
 +              zend_update_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), &os);
                zval_ptr_dtor(&os);
  
 -              if (persistent_handle_len) {
 -                      char *name_str;
 -                      size_t name_len;
 +              if (persistent_handle_name) {
                        php_persistent_handle_factory_t *pf;
  
 -                      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 ((pf = php_persistent_handle_concede(NULL, driver->client_name, persistent_handle_name, NULL, NULL))) {
                                rf = php_resource_factory_init(NULL, php_persistent_handle_get_resource_factory_ops(), pf, (void (*)(void *)) php_persistent_handle_abandon);
                        }
 -
 -                      efree(name_str);
                }
  
 -              obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              obj = PHP_HTTP_OBJ(NULL, getThis());
  
 -              php_http_expect(obj->client = php_http_client_init(NULL, driver.client_ops, rf, NULL TSRMLS_CC), runtime, return);
 +              php_http_expect(obj->client = php_http_client_init(NULL, driver->client_ops, rf, NULL), runtime, return);
  
 -              php_http_object_method_init(&obj->notify, getThis(), ZEND_STRL("notify") 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;
@@@ -532,27 -560,26 +532,27 @@@ static PHP_METHOD(HttpClient, reset
        php_http_client_object_t *obj;
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
  
        obj->iterator = 0;
        php_http_client_reset(obj->client);
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
 -static HashTable *combined_options(zval *client, zval *request TSRMLS_DC)
 +static HashTable *combined_options(zval *client, zval *request)
  {
        HashTable *options;
 -      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);
 +      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));
        }
 -      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));
 +      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;
                }
        if (Z_TYPE_P(z_coptions) == IS_ARRAY) {
                array_copy(Z_ARRVAL_P(z_coptions), options);
        }
 -      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);
 +      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;
 -      TSRMLS_FETCH_FROM_CTX(msg_obj->message->ts);
  
 -      zend_objects_store_del_ref_by_handle_ex(msg_obj->zv.handle, msg_obj->zv.handlers TSRMLS_CC);
 +      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);
 -              if (e->closure.fci.object_ptr) {
 -                      zval_ptr_dtor(&e->closure.fci.object_ptr);
 +              if (e->closure.fci.object) {
 +                      zend_objects_store_del(e->closure.fci.object);
                }
        }
  }
@@@ -599,10 -628,10 +599,10 @@@ static PHP_METHOD(HttpClient, enqueue
        php_http_message_object_t *msg_obj;
        php_http_client_enqueue_t q;
  
 -      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);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O|f", &request, php_http_client_request_class_entry, &fci, &fcc), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      msg_obj = zend_object_store_get_object(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);
        }
  
        q.request = msg_obj->message;
 -      q.options = combined_options(getThis(), request TSRMLS_CC);
 +      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) {
 -              Z_ADDREF_P(fci.function_name);
 -              if (fci.object_ptr) {
 -                      Z_ADDREF_P(fci.object_ptr);
 +              Z_TRY_ADDREF(fci.function_name);
 +              if (fci.object) {
 +                      ++GC_REFCOUNT(fci.object);
                }
        }
  
 -      zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
 +      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);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_dequeue, 0, 0, 1)
@@@ -642,10 -671,10 +642,10 @@@ static PHP_METHOD(HttpClient, dequeue
        php_http_client_object_t *obj;
        php_http_message_object_t *msg_obj;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_class_entry), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, php_http_client_request_class_entry), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      msg_obj = zend_object_store_get_object(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 dequeue request; request not in queue", NULL);
  
        php_http_expect(SUCCESS == php_http_client_dequeue(obj->client, msg_obj->message), runtime, return);
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_requeue, 0, 0, 1)
@@@ -670,47 -699,47 +670,47 @@@ static PHP_METHOD(HttpClient, requeue
        php_http_message_object_t *msg_obj;
        php_http_client_enqueue_t q;
  
 -      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);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O|f", &request, php_http_client_request_class_entry, &fci, &fcc), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      msg_obj = zend_object_store_get_object(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;
 -      q.options = combined_options(getThis(), request TSRMLS_CC);
 +      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) {
 -              Z_ADDREF_P(fci.function_name);
 -              if (fci.object_ptr) {
 -                      Z_ADDREF_P(fci.object_ptr);
 +              Z_TRY_ADDREF(fci.function_name);
 +              if (fci.object) {
 +                      ++GC_REFCOUNT(fci.object);
                }
        }
  
 -      zend_objects_store_add_ref_by_handle(msg_obj->zv.handle TSRMLS_CC);
 +      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);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_count, 0, 0, 0)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpClient, count)
  {
 -      long count_mode = -1;
 +      zend_long count_mode = -1;
  
 -      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);
 +      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));
        }
@@@ -724,20 -753,20 +724,20 @@@ static PHP_METHOD(HttpClient, getRespon
        zval *zrequest = NULL;
        php_http_client_object_t *obj;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &zrequest, php_http_client_request_class_entry), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|O", &zrequest, php_http_client_request_class_entry), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
  
        if (zrequest) {
                /* lookup the response with the request */
                zend_llist_element *el = NULL;
 -              php_http_message_object_t *req_obj = zend_object_store_get_object(zrequest TSRMLS_CC);
 +              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) {
 -                              RETURN_OBJVAL(response_obj->zv, 1);
 +                              RETURN_OBJECT(&response_obj->zo, 1);
                        }
                }
  
  
                /* pop off and go */
                if (response_obj) {
 -                      RETVAL_OBJVAL(response_obj->zv, 1);
 +                      RETVAL_OBJECT(&response_obj->zo, 1);
                        zend_llist_remove_tail(&obj->client->responses);
                }
        }
@@@ -762,12 -791,12 +762,12 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_ge
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpClient, getHistory)
  {
 -      zval *zhistory;
 +      zval zhistory_tmp, *zhistory;
  
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
  
 -      zhistory = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0 TSRMLS_CC);
 -      RETVAL_ZVAL(zhistory, 1, 0);
 +      zhistory = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("history"), 0, &zhistory_tmp);
 +      RETVAL_ZVAL_FAST(zhistory);
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_send, 0, 0, 0)
@@@ -778,11 -807,11 +778,11 @@@ static PHP_METHOD(HttpClient, send
  
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
  
 -      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);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_once, 0, 0, 0)
@@@ -790,7 -819,7 +790,7 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpClient, once)
  {
        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());
  
                RETURN_BOOL(0 < php_http_client_once(obj->client));
        }
@@@ -803,9 -832,9 +803,9 @@@ static PHP_METHOD(HttpClient, wait
  {
        double timeout = 0;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|d", &timeout)) {
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|d", &timeout)) {
                struct timeval timeout_val;
 -              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());
  
                timeout_val.tv_sec = (time_t) timeout;
                timeout_val.tv_usec = PHP_HTTP_USEC(timeout) % PHP_HTTP_MCROSEC;
        }
  }
  
 -      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);
+ 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(), "|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();
@@@ -822,13 -867,13 +838,13 @@@ static PHP_METHOD(HttpClient, enablePip
        zend_bool enable = 1;
        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(), "|b", &enable), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      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);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_enableEvents, 0, 0, 0)
@@@ -839,32 -884,31 +855,32 @@@ static PHP_METHOD(HttpClient, enableEve
        zend_bool enable = 1;
        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(), "|b", &enable), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      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);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  struct notify_arg {
        php_http_object_method_t *cb;
 -      zval **args[3];
 +      zval args[3];
        int argc;
  };
  
 -static int notify(zend_object_iterator *iter, void *puser TSRMLS_DC)
 +static int notify(zend_object_iterator *iter, void *puser)
  {
 -      zval **observer = NULL;
 +      zval *observer;
        struct notify_arg *arg = 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);
 +      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;
 +              }
        }
 -      return FAILURE;
 +      return ZEND_HASH_APPLY_STOP;
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_notify, 0, 0, 0)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpClient, notify)
  {
 -      zval *request = NULL, *zprogress = NULL, *observers;
 +      zval *request = NULL, *zprogress = NULL, observers_tmp, *observers;
        php_http_client_object_t *client_obj;
        struct notify_arg arg = {NULL};
  
 -      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);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|O!o!", &request, php_http_client_request_class_entry, &zprogress), invalid_arg, return);
  
 -      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);
 +      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);
  
        if (client_obj->update) {
                arg.cb = client_obj->update;
 -
 -              Z_ADDREF_P(getThis());
 -              arg.args[0] = &getThis();
 +              ZVAL_COPY(&arg.args[0], getThis());
                arg.argc = 1;
  
                if (request) {
 -                      Z_ADDREF_P(request);
 -                      arg.args[1] = &request;
 +                      ZVAL_COPY(&arg.args[1], request);
                        arg.argc += 1;
                }
 -
                if (zprogress) {
 -                      Z_ADDREF_P(zprogress);
 -                      arg.args[2] = &zprogress;
 +                      ZVAL_COPY(&arg.args[2], zprogress);
                        arg.argc += 1;
                }
  
 -              spl_iterator_apply(observers, notify, &arg TSRMLS_CC);
 +              spl_iterator_apply(observers, notify, &arg);
  
 -              zval_ptr_dtor(&getThis());
 +              zval_ptr_dtor(getThis());
                if (request) {
 -                      zval_ptr_dtor(&request);
 +                      zval_ptr_dtor(request);
                }
                if (zprogress) {
 -                      zval_ptr_dtor(&zprogress);
 +                      zval_ptr_dtor(zprogress);
                }
        }
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_attach, 0, 0, 1)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpClient, attach)
  {
 -      zval *observers, *observer, *retval = NULL;
 +      zval observers_tmp, *observers, *observer, retval;
        php_http_client_object_t *client_obj;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &observer, spl_ce_SplObserver), invalid_arg, return);
  
 -      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);
 +      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);
        }
  
        if (!client_obj->update) {
 -              client_obj->update = php_http_object_method_init(NULL, observer, ZEND_STRL("update") TSRMLS_CC);
 +              client_obj->update = php_http_object_method_init(NULL, observer, ZEND_STRL("update"));
        }
  
 -      zend_call_method_with_1_params(&observers, NULL, NULL, "attach", &retval, observer);
 -      if (retval) {
 -              zval_ptr_dtor(&retval);
 -      }
 +      ZVAL_UNDEF(&retval);
 +      zend_call_method_with_1_params(observers, NULL, NULL, "attach", &retval, observer);
 +      zval_ptr_dtor(&retval);
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_detach, 0, 0, 1)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpClient, detach)
  {
 -      zval *observers, *observer, *retval = NULL;
 +      zval observers_tmp, *observers, *observer, retval;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &observer, spl_ce_SplObserver), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &observer, spl_ce_SplObserver), invalid_arg, return);
  
 -      observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
 +      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;
        }
  
 -      zend_call_method_with_1_params(&observers, NULL, NULL, "detach", &retval, observer);
 -      if (retval) {
 -              zval_ptr_dtor(&retval);
 -      }
 +      ZVAL_UNDEF(&retval);
 +      zend_call_method_with_1_params(observers, NULL, NULL, "detach", &retval, observer);
 +      zval_ptr_dtor(&retval);
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getObservers, 0, 0, 0)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpClient, getObservers)
  {
 -      zval *observers;
 +      zval observers_tmp, *observers;
  
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
  
 -      observers = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("observers"), 0 TSRMLS_CC);
 +      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);
 +      RETVAL_ZVAL_FAST(observers);
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getProgressInfo, 0, 0, 1)
@@@ -994,17 -1045,17 +1010,17 @@@ static PHP_METHOD(HttpClient, getProgre
        php_http_message_object_t *req_obj;
        php_http_client_progress_state_t *progress;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_class_entry), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, php_http_client_request_class_entry), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      req_obj = zend_object_store_get_object(request TSRMLS_CC);
 +      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);
 -      add_property_string(return_value, "info", STR_PTR(progress->info), 1);
 +      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);
@@@ -1021,10 -1072,10 +1037,10 @@@ static PHP_METHOD(HttpClient, getTransf
        php_http_client_object_t *obj;
        php_http_message_object_t *req_obj;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &request, php_http_client_request_class_entry), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &request, php_http_client_request_class_entry), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      req_obj = zend_object_store_get_object(request TSRMLS_CC);
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
 +      req_obj = PHP_HTTP_OBJ(NULL, request);
  
        object_init(return_value);
        info = HASH_OF(return_value);
@@@ -1038,11 -1089,11 +1054,11 @@@ static PHP_METHOD(HttpClient, setOption
  {
        zval *opts = NULL;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
  
 -      php_http_client_options_set(getThis(), opts TSRMLS_CC);
 +      php_http_client_options_set(getThis(), opts);
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getOptions, 0, 0, 0)
@@@ -1050,8 -1101,8 +1066,8 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpClient, getOptions)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              zval *options = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0 TSRMLS_CC);
 -              RETVAL_ZVAL(options, 1, 0);
 +              zval options_tmp, *options = zend_read_property(php_http_client_class_entry, getThis(), ZEND_STRL("options"), 0, &options_tmp);
 +              RETVAL_ZVAL_FAST(options);
        }
  }
  
@@@ -1062,11 -1113,11 +1078,11 @@@ static PHP_METHOD(HttpClient, setSslOpt
  {
        zval *opts = NULL;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
  
 -      php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 1 TSRMLS_CC);
 +      php_http_client_options_set_subr(getThis(), ZEND_STRL("ssl"), opts, 1);
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_addSslOptions, 0, 0, 0)
@@@ -1076,11 -1127,11 +1092,11 @@@ static PHP_METHOD(HttpClient, addSslOpt
  {
        zval *opts = NULL;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
  
 -      php_http_client_options_set_subr(getThis(), ZEND_STRS("ssl"), opts, 0 TSRMLS_CC);
 +      php_http_client_options_set_subr(getThis(), ZEND_STRL("ssl"), opts, 0);
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getSslOptions, 0, 0, 0)
@@@ -1088,7 -1139,7 +1104,7 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpClient, getSslOptions)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_client_options_get_subr(getThis(), ZEND_STRS("ssl"), return_value TSRMLS_CC);
 +              php_http_client_options_get_subr(getThis(), ZEND_STRL("ssl"), return_value);
        }
  }
  
@@@ -1099,11 -1150,11 +1115,11 @@@ static PHP_METHOD(HttpClient, setCookie
  {
        zval *opts = NULL;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
  
 -      php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 1 TSRMLS_CC);
 +      php_http_client_options_set_subr(getThis(), ZEND_STRL("cookies"), opts, 1);
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_addCookies, 0, 0, 0)
@@@ -1113,11 -1164,11 +1129,11 @@@ static PHP_METHOD(HttpClient, addCookie
  {
        zval *opts = NULL;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|a!/", &opts), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|a!/", &opts), invalid_arg, return);
  
 -      php_http_client_options_set_subr(getThis(), ZEND_STRS("cookies"), opts, 0 TSRMLS_CC);
 +      php_http_client_options_set_subr(getThis(), ZEND_STRL("cookies"), opts, 0);
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpClient_getCookies, 0, 0, 0)
@@@ -1125,20 -1176,43 +1141,44 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpClient, getCookies)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_client_options_get_subr(getThis(), ZEND_STRS("cookies"), return_value TSRMLS_CC);
 +              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();
 -static PHP_METHOD(HttpClient, getAvailableDrivers) {
 +static PHP_METHOD(HttpClient, getAvailableDrivers)
 +{
        if (SUCCESS == zend_parse_parameters_none()) {
                array_init(return_value);
 -              php_http_client_driver_list(Z_ARRVAL_P(return_value) TSRMLS_CC);
 +              php_http_client_driver_list(Z_ARRVAL_P(return_value));
        }
  }
  
 -              php_http_client_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+ 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_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, 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, enablePipelining,     ai_HttpClient_enablePipelining,     ZEND_ACC_PUBLIC)
-       PHP_ME(HttpClient, enableEvents,         ai_HttpClient_enableEvents,         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, 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
  };
  
@@@ -1176,19 -1253,17 +1219,19 @@@ PHP_MINIT_FUNCTION(http_client
        zend_class_entry ce = {0};
  
        INIT_NS_CLASS_ENTRY(ce, "http", "Client", php_http_client_methods);
 -      php_http_client_class_entry = zend_register_internal_class_ex(&ce, NULL, NULL TSRMLS_CC);
 +      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_class_implements(php_http_client_class_entry TSRMLS_CC, 2, spl_ce_SplSubject, spl_ce_Countable);
 +      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_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_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, NULL, 1);
 +      zend_hash_init(&php_http_client_drivers, 2, NULL, php_http_client_driver_hash_dtor, 1);
  
        return SUCCESS;
  }
diff --combined php_http_client.h
index 41671b9ff813a0c061615636ebc32615d297a138,4008e874f8ae095e24f842d2b785466e28616de3..792581a4da017906ffa05b5e550069b39a0b2157
  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_progress_state_t** */
-       PHP_HTTP_CLIENT_OPT_TRANSFER_INFO,              /* HashTable* */
+       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 {
@@@ -38,13 -41,13 +41,13 @@@ typedef struct php_http_client *(*php_h
  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 STATUS (*php_http_client_exec_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 STATUS (*php_http_client_wait_func_t)(struct php_http_client *p, struct timeval *custom_timeout);
 -typedef STATUS (*php_http_client_enqueue_func_t)(struct php_http_client *p, php_http_client_enqueue_t *enqueue);
 -typedef STATUS (*php_http_client_dequeue_func_t)(struct php_http_client *p, php_http_client_enqueue_t *enqueue);
 -typedef STATUS (*php_http_client_setopt_func_t)(struct php_http_client *p, php_http_client_setopt_opt_t opt, void *arg);
 -typedef STATUS (*php_http_client_getopt_func_t)(struct php_http_client *h, php_http_client_getopt_opt_t opt, void *arg, void **res);
 +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_ops_t;
  
  typedef struct php_http_client_driver {
 -      const char *name_str;
 -      size_t name_len;
 +      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 STATUS php_http_client_driver_add(php_http_client_driver_t *driver);
 -PHP_HTTP_API STATUS php_http_client_driver_get(const char *name_str, size_t name_len, php_http_client_driver_t *driver);
 +PHP_HTTP_API ZEND_RESULT_CODE php_http_client_driver_add(php_http_client_driver_t *driver);
 +PHP_HTTP_API php_http_client_driver_t *php_http_client_driver_get(zend_string *name);
  
  typedef struct php_http_client_progress_state {
        struct {
@@@ -85,7 -87,7 +88,7 @@@
        unsigned finished:1;
  } php_http_client_progress_state_t;
  
 -typedef STATUS (*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 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 {
  
        zend_llist requests;
        zend_llist responses;
 -
 -#ifdef ZTS
 -      void ***ts;
 -#endif
  } php_http_client_t;
  
  PHP_HTTP_API zend_class_entry *php_http_client_class_entry;
  
  typedef struct php_http_client_object {
 -      zend_object zo;
 -      zend_object_value zv;
        php_http_client_t *client;
 -      long iterator;
        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 TSRMLS_DC);
 +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 STATUS php_http_client_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue);
 -PHP_HTTP_API STATUS php_http_client_dequeue(php_http_client_t *h, php_http_message_t *request);
 +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 STATUS php_http_client_wait(php_http_client_t *h, struct timeval *custom_timeout);
 +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 STATUS php_http_client_exec(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 STATUS php_http_client_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg);
 -PHP_HTTP_API STATUS php_http_client_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void *res_ptr);
 +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 */
diff --combined php_http_client_curl.c
index c50779916ff3fc40cf609c03e9ee13d48ffe46b7,97408bddb469deb3901bd8ddd561880c0fd8e5dc..df832ecd9a8515421a2019d7910e433285f56d3d
@@@ -13,7 -13,7 +13,7 @@@
  #include "php_http_api.h"
  #include "php_http_client.h"
  
- #if 1||PHP_HTTP_HAVE_CURL
+ #if PHP_HTTP_HAVE_CURL
  
  #if PHP_HTTP_HAVE_EVENT
  #     if !PHP_HTTP_HAVE_EVENT2 && /* just be really sure */ !(LIBEVENT_VERSION_NUMBER >= 0x02000000)
@@@ -76,15 -76,15 +76,15 @@@ typedef struct php_http_client_curl_han
                php_http_buffer_t cookies;
                php_http_buffer_t ranges;
  
 -              long redirects;
 -              unsigned range_request:1;
 -              unsigned encode_cookies:1;
 -
                struct {
                        uint count;
                        double delay;
                } retry;
  
 +              long redirects;
 +              unsigned range_request:1;
 +              unsigned encode_cookies:1;
 +
        } options;
  
  } php_http_client_curl_handler_t;
@@@ -110,7 -110,7 +110,7 @@@ static inline php_http_curle_storage_t 
        return st;
  }
  
 -static void *php_http_curle_ctor(void *opaque, void *init_arg TSRMLS_DC)
 +static void *php_http_curle_ctor(void *opaque, void *init_arg)
  {
        void *ch;
  
        return NULL;
  }
  
 -static void *php_http_curle_copy(void *opaque, void *handle TSRMLS_DC)
 +static void *php_http_curle_copy(void *opaque, void *handle)
  {
        void *ch;
  
        return NULL;
  }
  
 -static void php_http_curle_dtor(void *opaque, void *handle TSRMLS_DC)
 +static void php_http_curle_dtor(void *opaque, void *handle)
  {
        php_http_curle_storage_t *st = php_http_curle_get_storage(handle);
  
@@@ -156,12 -156,12 +156,12 @@@ static php_resource_factory_ops_t php_h
        php_http_curle_dtor
  };
  
 -static void *php_http_curlm_ctor(void *opaque, void *init_arg TSRMLS_DC)
 +static void *php_http_curlm_ctor(void *opaque, void *init_arg)
  {
        return curl_multi_init();
  }
  
 -static void php_http_curlm_dtor(void *opaque, void *handle TSRMLS_DC)
 +static void php_http_curlm_dtor(void *opaque, void *handle)
  {
        curl_multi_cleanup(handle);
  }
@@@ -178,10 -178,11 +178,10 @@@ static size_t php_http_curle_read_callb
  {
        php_http_message_body_t *body = ctx;
  
 -      if (body && body->stream_id) {
 +      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();
        }
@@@ -313,163 -314,133 +313,163 @@@ static int php_http_curle_body_callback
        return php_http_message_body_append(h->response.body, data, n*l);
  }
  
 -static STATUS php_http_curle_get_info(CURL *ch, HashTable *info)
 +static ZEND_RESULT_CODE php_http_curle_get_info(CURL *ch, HashTable *info)
  {
 -      char *c;
 -      long l;
 -      double d;
 -      struct curl_slist *s, *p;
 -      zval *subarray, array;
 -      INIT_PZVAL_ARRAY(&array, info);
 +      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_string_ex(&array, "effective_url", sizeof("effective_url"), c ? c : "", 1);
 +              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_long_ex(&array, "response_code", sizeof("response_code"), l);
 +              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, "total_time", sizeof("total_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, "namelookup_time", sizeof("namelookup_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, "connect_time", sizeof("connect_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, "pretransfer_time", sizeof("pretransfer_time"), 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_upload", sizeof("size_upload"), 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, "size_download", sizeof("size_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_download", sizeof("speed_download"), 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_double_ex(&array, "speed_upload", sizeof("speed_upload"), d);
 +              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, "header_size", sizeof("header_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, "request_size", sizeof("request_size"), 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, "ssl_verifyresult", sizeof("ssl_verifyresult"), 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_long_ex(&array, "filetime", sizeof("filetime"), l);
 +              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_download", sizeof("content_length_download"), 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, "content_length_upload", sizeof("content_length_upload"), 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_double_ex(&array, "starttransfer_time", sizeof("starttransfer_time"), d);
 +              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_string_ex(&array, "content_type", sizeof("content_type"), c ? c : "", 1);
 +              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_double_ex(&array, "redirect_time", sizeof("redirect_time"), d);
 +              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, "redirect_count", sizeof("redirect_count"), 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, "connect_code", sizeof("connect_code"), 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, "httpauth_avail", sizeof("httpauth_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, "proxyauth_avail", sizeof("proxyauth_avail"), 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, "os_errno", sizeof("os_errno"), 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)) {
 -              add_assoc_long_ex(&array, "num_connects", sizeof("num_connects"), l);
 +              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)) {
 -              MAKE_STD_ZVAL(subarray);
 -              array_init(subarray);
 +              array_init(&tmp);
                for (p = s; p; p = p->next) {
                        if (p->data) {
 -                              add_next_index_string(subarray, p->data, 1);
 +                              add_next_index_string(&tmp, p->data);
                        }
                }
 -              add_assoc_zval_ex(&array, "ssl_engines", sizeof("ssl_engines"), subarray);
 +              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, "redirect_url", sizeof("redirect_url"), 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_string_ex(&array, "primary_ip", sizeof("primary_ip"), c ? c : "", 1);
 +              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_double_ex(&array, "appconnect_time", sizeof("appconnect_time"), d);
 +              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, "condition_unmet", sizeof("condition_unmet"), 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_long_ex(&array, "primary_port", sizeof("primary_port"), l);
 +              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_string_ex(&array, "local_ip", sizeof("local_ip"), c ? c : "", 1);
 +              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)) {
 -              add_assoc_long_ex(&array, "local_port", sizeof("local_port"), l);
 +              ZVAL_LONG(&tmp, l);
 +              zend_hash_str_update(info, "local_port", lenof("local_port"), &tmp);
        }
  #endif
  
  
  #if PHP_HTTP_CURL_VERSION(7,34,0)
        {
 -              zval *ti_array;
 +              zval ti_array, subarray;
                struct curl_tlssessioninfo *ti;
  
                if (CURLE_OK == curl_easy_getinfo(ch, CURLINFO_TLS_SESSION, &ti)) {
 -                      const char *backend;
 +                      char *backend;
  
 -                      MAKE_STD_ZVAL(subarray);
 -                      ZVAL_NULL(subarray);
 -                      MAKE_STD_ZVAL(ti_array);
 -                      array_init(ti_array);
 +                      ZVAL_NULL(&subarray);
 +                      array_init(&ti_array);
  
                        switch (ti->backend) {
                        case CURLSSLBACKEND_NONE:
                                {
                                        SSL_CTX *ctx = ti->internals;
  
 -                                      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));
 +                                      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;
                                        gnutls_session_t sess = ti->internals;
                                        char *desc;
  
 -                                      array_init(subarray);
 +                                      array_init(&subarray);
                                        if ((desc = gnutls_session_get_desc(sess))) {
 -                                              add_assoc_string_ex(subarray, ZEND_STRS("desc"), desc, 1);
 +                                              add_assoc_string_ex(&subarray, ZEND_STRL("desc"), desc);
                                                gnutls_free(desc);
                                        }
 -                                      add_assoc_bool_ex(subarray, ZEND_STRS("resumed"), gnutls_session_is_resumed(sess));
 +                                      add_assoc_bool_ex(&subarray, ZEND_STRL("resumed"), gnutls_session_is_resumed(sess));
                                }
  #endif
                                break;
                        default:
                                backend = "unknown";
                        }
 -                      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_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)
        {
                int i;
 -              zval *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(ci_array);
 -                      array_init(ci_array);
 +                      array_init(&ci_array);
  
                        for (i = 0; i < ci->num_of_certs; ++i) {
                                s = ci->certinfo[i];
  
 -                              MAKE_STD_ZVAL(subarray);
 -                              array_init(subarray);
 +                              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_assoc_string_ex(subarray, keyname, colon - p->data + 1, colon + 1, 1);
 +                                                      add_assoc_string_ex(&subarray, keyname, colon - p->data, colon + 1);
                                                        efree(keyname);
                                                } else {
 -                                                      add_next_index_string(subarray, p->data, 1);
 +                                                      add_next_index_string(&subarray, p->data);
                                                }
                                        }
                                }
 -                              add_next_index_zval(ci_array, subarray);
 +                              add_next_index_zval(&ci_array, &subarray);
                        }
 -                      add_assoc_zval_ex(&array, "certinfo", sizeof("certinfo"), ci_array);
 +                      zend_hash_str_update(info, "certinfo", lenof("certinfo"), &ci_array);
                }
        }
  #endif
        {
                php_http_curle_storage_t *st = php_http_curle_get_storage(ch);
  
 -              add_assoc_long_ex(&array, "curlcode", sizeof("curlcode"), st->errorcode);
 -              add_assoc_string_ex(&array, "error", sizeof("error"), st->errorbuffer, 1);
 +              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;
@@@ -627,24 -600,20 +627,24 @@@ static php_http_message_t *php_http_cur
        php_http_message_body_addref(h->response.body);
  
        /* let's update the response headers */
 -      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);
 +      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("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("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-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("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"));
        }
 -      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-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);
  
@@@ -656,6 -625,7 +656,6 @@@ static void php_http_curlm_responsehand
        int remaining = 0;
        php_http_client_enqueue_t *enqueue;
        php_http_client_curl_t *curl = context->ctx;
 -      TSRMLS_FETCH_FROM_CTX(context->ts);
  
        do {
                CURLMsg *msg = curl_multi_info_read(curl->handle, &remaining);
                if (msg && CURLMSG_DONE == msg->msg) {
                        if (CURLE_OK != msg->data.result) {
                                php_http_curle_storage_t *st = php_http_curle_get_storage(msg->easy_handle);
 -                              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s; %s (%s)", curl_easy_strerror(st->errorcode = msg->data.result), STR_PTR(st->errorbuffer), STR_PTR(st->url));
 +                              php_error_docref(NULL, E_WARNING, "%s; %s (%s)", curl_easy_strerror(st->errorcode = msg->data.result), STR_PTR(st->errorbuffer), STR_PTR(st->url));
                        }
  
                        if ((enqueue = php_http_client_enqueued(context, msg->easy_handle, compare_queue))) {
@@@ -712,6 -682,7 +712,6 @@@ static void php_http_curlm_timeout_call
  #endif
        if (curl->useevents) {
                CURLMcode rc;
 -              TSRMLS_FETCH_FROM_CTX(context->ts);
  
                /* ignore and use -1,0 on timeout */
                (void) socket;
                while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, CURL_SOCKET_TIMEOUT, 0, &curl->unfinished)));
  
                if (CURLM_OK != rc) {
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s",  curl_multi_strerror(rc));
 +                      php_error_docref(NULL, E_WARNING, "%s",  curl_multi_strerror(rc));
                }
  
                php_http_curlm_responsehandler(context);
@@@ -737,11 -708,12 +737,11 @@@ static void php_http_curlm_event_callba
  #endif
        if (curl->useevents) {
                CURLMcode rc = CURLM_OK;
 -              TSRMLS_FETCH_FROM_CTX(context->ts);
  
                while (CURLM_CALL_MULTI_PERFORM == (rc = curl_multi_socket_action(curl->handle, socket, etoca(action), &curl->unfinished)));
  
                if (CURLM_OK != rc) {
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", curl_multi_strerror(rc));
 +                      php_error_docref(NULL, E_WARNING, "%s", curl_multi_strerror(rc));
                }
  
                php_http_curlm_responsehandler(context);
@@@ -764,6 -736,7 +764,6 @@@ static int php_http_curlm_socket_callba
        if (curl->useevents) {
                int events = EV_PERSIST;
                php_http_curlm_event_t *ev = assign_data;
 -              TSRMLS_FETCH_FROM_CTX(context->ts);
  
                if (!ev) {
                        ev = ecalloc(1, sizeof(php_http_curlm_event_t));
                                return 0;
  
                        default:
 -                              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown socket action %d", action);
 +                              php_error_docref(NULL, E_WARNING, "Unknown socket action %d", action);
                                return -1;
                }
  
@@@ -833,24 -806,24 +833,24 @@@ static void php_http_curlm_timer_callba
  
  /* curl options */
  
- static php_http_options_t php_http_curle_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 STATUS php_http_curle_option_set_ssl_verifyhost(php_http_option_t *opt, zval *val, void *userdata)
 +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 (CURLE_OK != curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, Z_BVAL_P(val) ? 2 : 0)) {
 +      if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_SSL_VERIFYHOST, Z_TYPE_P(val) == IS_TRUE ? 2 : 0)) {
                return FAILURE;
        }
        return SUCCESS;
  }
  
 -static STATUS php_http_curle_option_set_cookiestore(php_http_option_t *opt, zval *val, void *userdata)
 +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;
        if (storage->cookiestore) {
                pefree(storage->cookiestore, 1);
        }
 -      if (val && Z_STRLEN_P(val)) {
 +      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;
        return SUCCESS;
  }
  
 -static STATUS php_http_curle_option_set_cookies(php_http_option_t *opt, zval *val, void *userdata)
 +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;
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
  
        if (val && Z_TYPE_P(val) != IS_NULL) {
 +              HashTable *ht = HASH_OF(val);
 +
                if (curl->options.encode_cookies) {
 -                      if (SUCCESS == php_http_url_encode_hash_ex(HASH_OF(val), &curl->options.cookies, ZEND_STRL(";"), ZEND_STRL("="), NULL, 0 TSRMLS_CC)) {
 +                      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;
                                return FAILURE;
                        }
                } else {
 -                      HashPosition pos;
 -                      php_http_array_hashkey_t cookie_key = php_http_array_hashkey_init(0);
 -                      zval **cookie_val;
 +                      php_http_arrkey_t cookie_key;
 +                      zval *cookie_val;
  
 -                      FOREACH_KEYVAL(pos, val, cookie_key, cookie_val) {
 -                              zval *zv = php_http_ztyp(IS_STRING, *cookie_val);
 +                      ZEND_HASH_FOREACH_KEY_VAL(ht, cookie_key.h, cookie_key.key, cookie_val)
 +                      {
 +                              zend_string *zs = zval_get_string(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);
 +                              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);
  
 -                              zval_ptr_dtor(&zv);
 +                              zend_string_release(zs);
                        }
 +                      ZEND_HASH_FOREACH_END();
  
                        php_http_buffer_fix(&curl->options.cookies);
                        if (curl->options.cookies.used) {
        return SUCCESS;
  }
  
 -static STATUS php_http_curle_option_set_encodecookies(php_http_option_t *opt, zval *val, void *userdata)
 +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;
  
 -      curl->options.encode_cookies = Z_BVAL_P(val);
 +      curl->options.encode_cookies = Z_TYPE_P(val) == IS_TRUE;
        return SUCCESS;
  }
  
 -static STATUS php_http_curle_option_set_lastmodified(php_http_option_t *opt, zval *val, void *userdata)
 +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;
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
  
        if (Z_LVAL_P(val)) {
                if (Z_LVAL_P(val) > 0) {
                                return FAILURE;
                        }
                } else {
 -                      if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) sapi_get_request_time(TSRMLS_C) + Z_LVAL_P(val))) {
 +                      if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_TIMEVALUE, (long) sapi_get_request_time() + Z_LVAL_P(val))) {
                                return FAILURE;
                        }
                }
        return SUCCESS;
  }
  
 -static STATUS php_http_curle_option_set_compress(php_http_option_t *opt, zval *val, void *userdata)
 +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 (CURLE_OK != curl_easy_setopt(ch, CURLOPT_ACCEPT_ENCODING, Z_BVAL_P(val) ? "" : NULL)) {
+ #if !PHP_HTTP_CURL_VERSION(7,21,6)
+ #     define CURLOPT_ACCEPT_ENCODING CURLOPT_ENCODING
+ #endif
 +      if (CURLE_OK != curl_easy_setopt(ch, CURLOPT_ACCEPT_ENCODING, Z_TYPE_P(val) == IS_TRUE ? "" : NULL)) {
                return FAILURE;
        }
        return SUCCESS;
  }
  
 -static STATUS php_http_curle_option_set_etag(php_http_option_t *opt, zval *val, void *userdata)
 +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;
  
 -      if (Z_STRLEN_P(val)) {
 +      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));
        return SUCCESS;
  }
  
 -static STATUS php_http_curle_option_set_range(php_http_option_t *opt, zval *val, void *userdata)
 +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;
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
  
        php_http_buffer_reset(&curl->options.ranges);
  
        if (val && Z_TYPE_P(val) != IS_NULL) {
 -              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));
 +              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);
                                                }
 -                                              zval_ptr_dtor(&rbl);
 -                                              zval_ptr_dtor(&rel);
                                        }
  
                                }
                        }
                }
 +              ZEND_HASH_FOREACH_END();
  
                if (curl->options.ranges.used) {
                        curl->options.range_request = 1;
        return SUCCESS;
  }
  
 -static STATUS php_http_curle_option_set_resume(php_http_option_t *opt, zval *val, void *userdata)
 +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;
        return SUCCESS;
  }
  
 -static STATUS php_http_curle_option_set_retrydelay(php_http_option_t *opt, zval *val, void *userdata)
 +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;
  
        return SUCCESS;
  }
  
 -static STATUS php_http_curle_option_set_retrycount(php_http_option_t *opt, zval *val, void *userdata)
 +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;
  
        return SUCCESS;
  }
  
 -static STATUS php_http_curle_option_set_redirect(php_http_option_t *opt, zval *val, void *userdata)
 +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;
        return SUCCESS;
  }
  
 -static STATUS php_http_curle_option_set_portrange(php_http_option_t *opt, zval *val, void *userdata)
 +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;
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
  
        if (val && Z_TYPE_P(val) != IS_NULL) {
 -              zval **z_port_start, *zps_copy = NULL, **z_port_end, *zpe_copy = NULL;
 +              zval *zps, *zpe;
  
 -              switch (php_http_array_list(Z_ARRVAL_P(val) TSRMLS_CC, 2, &z_port_start, &z_port_end)) {
 +              switch (php_http_array_list(Z_ARRVAL_P(val), 2, &zps, &zpe)) {
                case 2:
 -                      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;
 +                      localportrange = labs(zval_get_long(zps)-zval_get_long(zpe))+1L;
                        /* no break */
                case 1:
 -                      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);
 -                      }
 +                      localport = (zval_get_long(zpe) > 0) ? MIN(zval_get_long(zps), zval_get_long(zpe)) : zval_get_long(zps);
                        break;
                default:
                        break;
  }
  
  #if PHP_HTTP_CURL_VERSION(7,37,0)
 -static STATUS php_http_curle_option_set_proxyheader(php_http_option_t *opt, zval *val, void *userdata)
 +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;
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
  
        if (val && Z_TYPE_P(val) != IS_NULL) {
 -              php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0);
 -              zval **header_val, *header_cpy;
 -              HashPosition pos;
 +              php_http_arrkey_t header_key;
 +              zval *header_val;
                php_http_buffer_t header;
  
                php_http_buffer_init(&header);
 -              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));
 +              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);
  
 -                              zval_ptr_dtor(&header_cpy);
                        }
                }
 +              ZEND_HASH_FOREACH_END();
                php_http_buffer_dtor(&header);
        }
        if (CURLE_OK != curl_easy_setopt(curl->handle, CURLOPT_PROXYHEADER, curl->options.proxyheaders)) {
  #endif
  
  #if PHP_HTTP_CURL_VERSION(7,21,3)
 -static STATUS php_http_curle_option_set_resolve(php_http_option_t *opt, zval *val, void *userdata)
 +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;
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
  
        if (val && Z_TYPE_P(val) != IS_NULL) {
 -              php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 -              HashPosition pos;
 -              zval **data;
 +              HashTable *ht = HASH_OF(val);
 +              zval *data;
  
 -              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);
 +              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;
  #endif
  
  #if PHP_HTTP_CURL_VERSION(7,21,4) && defined(PHP_HTTP_CURL_TLSAUTH_SRP)
 -static STATUS php_http_curle_option_set_ssl_tlsauthtype(php_http_option_t *opt, zval *val, void *userdata)
 +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;
  }
  #endif
  
 -static void php_http_curle_options_init(php_http_options_t *registry TSRMLS_DC)
 +static void php_http_curle_options_init(php_http_options_t *registry)
  {
        php_http_option_t *opt;
  
        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("proxytunnel"), CURLOPT_HTTPPROXYTUNNEL, 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
                Z_LVAL(opt->defval) = 5;
        }
        */
 -      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("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("redirect"), CURLOPT_FOLLOWLOCATION, IS_LONG))) {
                opt->setter = php_http_curle_option_set_redirect;
        }
 -      php_http_option_register(registry, ZEND_STRL("unrestricted_auth"), CURLOPT_UNRESTRICTED_AUTH, 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
        if ((opt = php_http_option_register(registry, ZEND_STRL("referer"), CURLOPT_REFERER, IS_STRING))) {
                opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
        }
 -      if ((opt = php_http_option_register(registry, ZEND_STRL("autoreferer"), CURLOPT_AUTOREFERER, IS_BOOL))) {
 +      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 */
 -              ZVAL_STRING(&opt->defval,
 +              ZVAL_PSTRING(&opt->defval,
                                "PECL_HTTP/" PHP_PECL_HTTP_VERSION " "
                                "PHP/" PHP_VERSION " "
 -                              "libcurl/" LIBCURL_VERSION
 -                      , 0);
 +                              "libcurl/" LIBCURL_VERSION);
        }
  
        /* resume */
        }
  
        /* compression */
 -      if ((opt = php_http_option_register(registry, ZEND_STRL("compress"), 0, IS_BOOL))) {
 +      if ((opt = php_http_option_register(registry, ZEND_STRL("compress"), 0, _IS_BOOL))) {
                opt->setter = php_http_curle_option_set_compress;
        }
  
        }
  
        /* cookies */
 -      if ((opt = php_http_option_register(registry, ZEND_STRL("encodecookies"), 0, 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);
        }
        }
  
        /* cookiesession, don't load session cookies from cookiestore */
 -      php_http_option_register(registry, ZEND_STRL("cookiesession"), CURLOPT_COOKIESESSION, 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;
  #endif
  
        /* tcp */
 -      php_http_option_register(registry, ZEND_STRL("tcp_nodelay"), CURLOPT_TCP_NODELAY, IS_BOOL);
 +      php_http_option_register(registry, ZEND_STRL("tcp_nodelay"), CURLOPT_TCP_NODELAY, _IS_BOOL);
  #if PHP_HTTP_CURL_VERSION(7,25,0)
 -      php_http_option_register(registry, ZEND_STRL("tcp_keepalive"), CURLOPT_TCP_KEEPALIVE, IS_BOOL);
 +      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("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;
                }
                if ((opt = php_http_option_register(registry, ZEND_STRL("keytype"), CURLOPT_SSLKEYTYPE, 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("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("verifypeer"), CURLOPT_SSL_VERIFYPEER, IS_BOOL))) {
 +              if ((opt = php_http_option_register(registry, ZEND_STRL("verifypeer"), CURLOPT_SSL_VERIFYPEER, _IS_BOOL))) {
                        ZVAL_BOOL(&opt->defval, 1);
                }
 -              if ((opt = php_http_option_register(registry, ZEND_STRL("verifyhost"), CURLOPT_SSL_VERIFYHOST, 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;
                }
                        opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_STRLEN;
                        opt->flags |= PHP_HTTP_CURLE_OPTION_CHECK_BASEDIR;
  #ifdef PHP_HTTP_CURL_CAINFO
 -                      ZVAL_STRING(&opt->defval, PHP_HTTP_CURL_CAINFO, 0);
 +                      ZVAL_PSTRING(&opt->defval, PHP_HTTP_CURL_CAINFO);
  #endif
                }
                if ((opt = php_http_option_register(registry, ZEND_STRL("capath"), CURLOPT_CAPATH, IS_STRING))) {
  #     endif
  #endif
  #if PHP_HTTP_CURL_VERSION(7,19,1) && defined(PHP_HTTP_HAVE_OPENSSL)
 -              php_http_option_register(registry, ZEND_STRL("certinfo"), CURLOPT_CERTINFO, IS_BOOL);
 +              php_http_option_register(registry, ZEND_STRL("certinfo"), CURLOPT_CERTINFO, _IS_BOOL);
  #endif
  #if PHP_HTTP_CURL_VERSION(7,36,0)
 -              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("enable_npn"), CURLOPT_SSL_ENABLE_NPN, _IS_BOOL))) {
                        ZVAL_BOOL(&opt->defval, 1);
                }
 -              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_alpn"), CURLOPT_SSL_ENABLE_ALPN, _IS_BOOL))) {
                        ZVAL_BOOL(&opt->defval, 1);
                }
  #endif
@@@ -1506,32 -1493,30 +1509,33 @@@ static zval *php_http_curle_get_option(
        zval *option;
  
        if ((option = php_http_option_get(opt, options, NULL))) {
 -              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);
 +              zval zopt;
 +
 +              ZVAL_DUP(&zopt, option);
-               convert_to_explicit_type(option, opt->type);
++              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 STATUS php_http_curle_set_option(php_http_option_t *opt, zval *val, void *userdata)
 +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;
 -      STATUS rv = SUCCESS;
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
 +      ZEND_RESULT_CODE rv = SUCCESS;
  
        if (!val) {
                val = &opt->defval;
        }
  
        switch (opt->type) {
 -      case IS_BOOL:
 +      case _IS_BOOL:
                if (opt->setter) {
                        rv = opt->setter(opt, val, curl);
 -              } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) Z_BVAL_P(val))) {
 +              } else if (CURLE_OK != curl_easy_setopt(ch, opt->option, (long) (Z_TYPE_P(val) == IS_TRUE))) {
                        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;
                        }
 -              } 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 ((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;
                        }
                break;
        }
        if (rv != SUCCESS) {
 -              php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not set option %s (%s)", opt->name.s, curl_easy_strerror(rc));
 +              php_error_docref(NULL, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_easy_strerror(rc));
        }
        return rv;
  }
  
 -static STATUS php_http_curlm_option_set_pipelining_bl(php_http_option_t *opt, zval *value, void *userdata)
+ #if PHP_HTTP_CURL_VERSION(7,30,0)
 -              zval **entry;
 -              HashPosition pos;
++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;
+       TSRMLS_FETCH_FROM_CTX(client->ts);
+       /* 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);
 -static inline STATUS php_http_curlm_use_eventloop(php_http_client_t *h, zend_bool enable)
++              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 STATUS php_http_curlm_option_set_use_eventloop(php_http_option_t *opt, zval *value, void *userdata)
++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;
+ }
 -      return php_http_curlm_use_eventloop(client, value && Z_BVAL_P(value));
++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;
 -      php_http_option_register(registry, ZEND_STRL("pipelining"), CURLMOPT_PIPELINING, IS_BOOL);
++      return php_http_curlm_use_eventloop(client, value && Z_TYPE_P(value) == IS_TRUE);
+ }
+ #endif
+ static void php_http_curlm_options_init(php_http_options_t *registry TSRMLS_DC)
+ {
+       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
 -static STATUS php_http_curlm_set_option(php_http_option_t *opt, zval *val, void *userdata)
++      if ((opt = php_http_option_register(registry, ZEND_STRL("use_eventloop"), 0, _IS_BOOL))) {
+               opt->setter = php_http_curlm_option_set_use_eventloop;
+       }
+ #endif
+ }
 -      STATUS rv = SUCCESS;
 -      TSRMLS_FETCH_FROM_CTX(client->ts);
++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;
 -              val = php_http_ztyp(opt->type, val);
++      ZEND_RESULT_CODE rv = SUCCESS;
+       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) {
++              php_error_docref(NULL TSRMLS_CC, E_NOTICE, "Could not set option %s (%s)", opt->name->val, curl_easy_strerror(rc));
+       }
+       return rv;
+ }
  
  /* client ops */
  
 -static STATUS php_http_client_curl_handler_reset(php_http_client_curl_handler_t *curl)
 +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;
@@@ -1661,9 -1818,10 +1846,9 @@@ static php_http_client_curl_handler_t *
  {
        void *handle;
        php_http_client_curl_handler_t *handler;
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
  
 -      if (!(handle = php_resource_factory_handle_ctor(rf, NULL TSRMLS_CC))) {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to initialize curl handle");
 +      if (!(handle = php_resource_factory_handle_ctor(rf, NULL))) {
 +              php_error_docref(NULL, E_WARNING, "Failed to initialize curl handle");
                return NULL;
        }
  
        handler->rf = rf;
        handler->client = h;
        handler->handle = handle;
 -      handler->response.body = php_http_message_body_init(NULL, NULL TSRMLS_CC);
 +      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);
  }
  
  
 -static STATUS php_http_client_curl_handler_prepare(php_http_client_curl_handler_t *curl, php_http_client_enqueue_t *enqueue)
 +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);
 -      TSRMLS_FETCH_FROM_CTX(curl->client->ts);
  
        /* request url */
        if (!PHP_HTTP_INFO(msg).request.url) {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot request empty URL");
 +              php_error_docref(NULL, E_WARNING, "Cannot request empty URL");
                return FAILURE;
        }
        storage->errorbuffer[0] = '\0';
                        if (PHP_HTTP_INFO(msg).request.method) {
                                curl_easy_setopt(curl->handle, CURLOPT_CUSTOMREQUEST, PHP_HTTP_INFO(msg).request.method);
                        } else {
 -                              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot use empty request method");
 +                              php_error_docref(NULL, E_WARNING, "Cannot use empty request method");
                                return FAILURE;
                        }
                        break;
        /* request headers */
        php_http_message_update_headers(msg);
        if (zend_hash_num_elements(&msg->hdrs)) {
 -              php_http_array_hashkey_t header_key = php_http_array_hashkey_init(0);
 -              zval **header_val, *header_cpy;
 -              HashPosition pos;
 +              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)
 -              zval **ct = NULL;
 -
 -              zend_hash_find(&msg->hdrs, ZEND_STRS("Content-Length"), (void *) &ct);
 +              zval *ct = zend_hash_str_find(&msg->hdrs, ZEND_STRL("Content-Length"));
  #endif
  
                php_http_buffer_init(&header);
 -              FOREACH_HASH_KEYVAL(pos, &msg->hdrs, header_key, header_val) {
 -                      if (header_key.type == HASH_KEY_IS_STRING) {
 +              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 */
 -                              if (ct && *ct == *header_val) {
 +                              if (ct && ct == header_val) {
                                        continue;
                                }
  #endif
 -                              header_cpy = php_http_ztyp(IS_STRING, *header_val);
 -                              php_http_buffer_appendf(&header, "%s: %s", header_key.str, Z_STRVAL_P(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);
 -
 -                              zval_ptr_dtor(&header_cpy);
 +                              zend_string_release(header_str);
                        }
                }
 +              ZEND_HASH_FOREACH_END();
                php_http_buffer_dtor(&header);
        }
        curl_easy_setopt(curl->handle, CURLOPT_HTTPHEADER, curl->options.headers);
@@@ -1828,9 -1988,11 +2013,9 @@@ static void php_http_client_curl_handle
  
  static void php_http_client_curl_handler_dtor(php_http_client_curl_handler_t *handler)
  {
 -      TSRMLS_FETCH_FROM_CTX(handler->client->ts);
 -
        php_http_client_curl_handler_clear(handler);
  
 -      php_resource_factory_handle_dtor(handler->rf, handler->handle TSRMLS_CC);
 +      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->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;
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
  
 -      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");
 +      if (!handle && !(handle = php_resource_factory_handle_ctor(h->rf, NULL))) {
 +              php_error_docref(NULL, E_WARNING, "Failed to initialize curl handle");
                return NULL;
        }
  
  static void php_http_client_curl_dtor(php_http_client_t *h)
  {
        php_http_client_curl_t *curl = h->ctx;
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
  
  #if PHP_HTTP_HAVE_EVENT
        if (curl->timeout) {
  #endif
        curl->unfinished = 0;
  
 -      php_resource_factory_handle_dtor(h->rf, curl->handle TSRMLS_CC);
 +      php_resource_factory_handle_dtor(h->rf, curl->handle);
  
        efree(curl);
        h->ctx = NULL;
@@@ -1912,24 -2064,37 +2097,40 @@@ static void queue_dtor(php_http_client_
        php_http_client_curl_handler_dtor(handler);
  }
  
- static php_resource_factory_t *create_rf(php_http_url_t *url)
 -static php_resource_factory_t *create_rf(php_http_client_t *h, php_http_client_enqueue_t *enqueue TSRMLS_DC)
++static php_resource_factory_t *create_rf(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
  {
-       php_persistent_handle_factory_t *pf;
+       php_persistent_handle_factory_t *pf = NULL;
        php_resource_factory_t *rf = NULL;
-       zend_string *id;
-       char *id_str = NULL;
-       size_t id_len;
+       php_http_url_t *url = enqueue->request->http.info.request.url;
  
        if (!url || (!url->host && !url->path)) {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Cannot request empty URL");
 +              php_error_docref(NULL, E_WARNING, "Cannot request empty URL");
                return NULL;
        }
  
-       id_len = spprintf(&id_str, 0, "%s:%d", STR_PTR(url->host), url->port ? url->port : 80);
-       id = php_http_cs2zs(id_str, id_len);
+       /* only if the client itself is setup for persistence */
+       if (h->rf->dtor == (void (*)(void*)) php_persistent_handle_abandon) {
++              zend_string *id;
+               char *id_str = NULL;
+               size_t id_len;
+               int port = url->port ? url->port : 80;
 -              zval **zport;
++              zval *zport;
++
++              id_len = spprintf(&id_str, 0, "%s:%d", STR_PTR(url->host), url->port ? url->port : 80);
++              id = php_http_cs2zs(id_str, id_len);
 -              if (SUCCESS == zend_hash_find(enqueue->options, ZEND_STRS("port"), (void *) &zport)) {
 -                      zval *zcpy = php_http_ztyp(IS_LONG, *zport);
++              if ((zport = zend_hash_str_find(enqueue->options, ZEND_STRL("port")))) {
++                      zend_long lport = zval_get_long(zport);
 -                      if (Z_LVAL_P(zcpy)) {
 -                              port = Z_LVAL_P(zcpy);
++                      if (lport > 0) {
++                              port = lport;
+                       }
 -                      zval_ptr_dtor(&zcpy);
+               }
  
-       pf = php_persistent_handle_concede(NULL, PHP_HTTP_G->client.curl.driver.request_name, id, NULL, NULL);
-       zend_string_release(id);
+               id_len = spprintf(&id_str, 0, "%s:%d", STR_PTR(url->host), port);
 -              pf = php_persistent_handle_concede(NULL, ZEND_STRL("http\\Client\\Curl\\Request"), id_str, id_len, NULL, NULL TSRMLS_CC);
 -              efree(id_str);
++              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_resource_factory_init(NULL, php_persistent_handle_get_resource_factory_ops(), pf, (void (*)(void*)) php_persistent_handle_abandon);
        return rf;
  }
  
 -static STATUS php_http_client_curl_enqueue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
 +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;
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
  
-       rf = create_rf(enqueue->request->http.info.request.url);
 -      rf = create_rf(h, enqueue TSRMLS_CC);
++      rf = create_rf(h, enqueue);
        if (!rf) {
                return FAILURE;
        }
  
                return SUCCESS;
        } else {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not enqueue request: %s", curl_multi_strerror(rs));
 +              php_error_docref(NULL, E_WARNING, "Could not enqueue request: %s", curl_multi_strerror(rs));
                return FAILURE;
        }
  }
  
 -static STATUS php_http_client_curl_dequeue(php_http_client_t *h, php_http_client_enqueue_t *enqueue)
 +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;
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
  
        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 {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not dequeue request: %s", curl_multi_strerror(rs));
 +              php_error_docref(NULL, E_WARNING, "Could not dequeue request: %s", curl_multi_strerror(rs));
        }
  
        return FAILURE;
@@@ -2028,7 -2195,7 +2229,7 @@@ static inline void php_http_client_curl
  #     define SELECT_ERROR -1
  #endif
  
 -static STATUS php_http_client_curl_wait(php_http_client_t *h, struct timeval *custom_timeout)
 +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;
@@@ -2090,11 -2257,14 +2291,11 @@@ static int php_http_client_curl_once(ph
  
  }
  
 -static STATUS php_http_client_curl_exec(php_http_client_t *h)
 +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;
 -#endif
 -      TSRMLS_FETCH_FROM_CTX(h->ts);
  
 -#if PHP_HTTP_HAVE_EVENT
        if (curl->useevents) {
                php_http_curlm_timeout_callback(CURL_SOCKET_TIMEOUT, /*EV_READ|EV_WRITE*/0, h);
                do {
  #endif
  
                        if (ev_rc < 0) {
 -                              php_error_docref(NULL TSRMLS_CC, E_ERROR, "Error in event_base_dispatch()");
 +                              php_error_docref(NULL, E_ERROR, "Error in event_base_dispatch()");
                                return FAILURE;
                        }
                } while (curl->unfinished);
                        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, "WinSock error: %d", WSAGetLastError());
 +                              php_error_docref(NULL, E_WARNING, "WinSock error: %d", WSAGetLastError());
  #else
 -                              php_error_docref(NULL TSRMLS_CC, E_WARNING, "%s", strerror(errno));
 +                              php_error_docref(NULL, E_WARNING, "%s", strerror(errno));
  #endif
                                return FAILURE;
                        }
        return SUCCESS;
  }
  
 -static STATUS php_http_client_curl_setopt(php_http_client_t *h, php_http_client_setopt_opt_t opt, void *arg)
 +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;
  
                case PHP_HTTP_CLIENT_OPT_USE_EVENTS:
  #if PHP_HTTP_HAVE_EVENT
-                       if ((curl->useevents = *((zend_bool *) arg))) {
-                               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 php_http_curlm_use_eventloop(h, *(zend_bool *) arg);
                        break;
  #endif
  
        return SUCCESS;
  }
  
 -static int apply_available_options(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key)
++static int apply_available_options(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key)
+ {
 -      php_http_option_t *opt = pDest;
++      php_http_option_t *opt = Z_PTR_P(pDest);
+       HashTable *ht;
 -      zval *entry;
++      zval entry;
+       int c;
+       ht = va_arg(args, HashTable*);
 -      MAKE_STD_ZVAL(entry);
 -
+       if ((c = zend_hash_num_elements(&opt->suboptions.options))) {
 -              array_init_size(entry, c);
 -              zend_hash_apply_with_arguments(&opt->suboptions.options TSRMLS_CC, apply_available_options, 1, Z_ARRVAL_P(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_NULL(entry);
++                      ZVAL_NULL(&entry);
+               } else {
 -                      ZVAL_ZVAL(entry, &opt->defval, 1, 0);
++                      ZVAL_ZVAL(&entry, &opt->defval, 1, 0);
+               }
+       }
 -      if (hash_key->nKeyLength) {
 -              zend_hash_quick_update(ht, hash_key->arKey, hash_key->nKeyLength, hash_key->h, (void *) &entry, sizeof(zval *), NULL);
++      if (hash_key->key) {
++              zend_hash_update(ht, hash_key->key, &entry);
+       } else {
 -              zend_hash_index_update(ht, hash_key->h, (void *) &entry, sizeof(zval *), NULL);
++              zend_hash_index_update(ht, hash_key->h, &entry);
+       }
+       return ZEND_HASH_APPLY_KEEP;
+ }
 -static STATUS php_http_client_curl_getopt(php_http_client_t *h, php_http_client_getopt_opt_t opt, void *arg, void **res)
 +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;
+       TSRMLS_FETCH_FROM_CTX(h->ts);
  
        switch (opt) {
        case PHP_HTTP_CLIENT_OPT_PROGRESS_INFO:
                }
                break;
  
+       case PHP_HTTP_CLIENT_OPT_AVAILABLE_OPTIONS:
+               zend_hash_apply_with_arguments(&php_http_curle_options.options TSRMLS_CC, apply_available_options, 1, *(HashTable **) res);
+               break;
+       case PHP_HTTP_CLIENT_OPT_AVAILABLE_CONFIGURATION:
+               zend_hash_apply_with_arguments(&php_http_curlm_options.options TSRMLS_CC, apply_available_options, 1, *(HashTable **) res);
+               break;
        default:
                break;
        }
@@@ -2220,20 -2419,19 +2448,20 @@@ php_http_client_ops_t *php_http_client_
  PHP_MINIT_FUNCTION(http_client_curl)
  {
        php_http_options_t *options;
 -      php_http_client_driver_t driver = {
 -              ZEND_STRL("curl"),
 -              &php_http_client_curl_ops
 -      };
  
 -      if (SUCCESS != php_http_client_driver_add(&driver)) {
 +      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"), &php_http_curlm_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;
        }
 -      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.request_name, &php_http_curle_resource_factory_ops, NULL, NULL)) {
                return FAILURE;
        }
  
                options->getter = php_http_curle_get_option;
                options->setter = php_http_curle_set_option;
  
 -              php_http_curle_options_init(options TSRMLS_CC);
 +              php_http_curle_options_init(options);
        }
 -              php_http_curlm_options_init(options TSRMLS_CC);
+       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_http_curlm_options_init(options);
+       }
  
        /*
        * HTTP Protocol Version Constants
        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)
+ #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
  
  
  PHP_MSHUTDOWN_FUNCTION(http_client_curl)
  {
 -      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_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;
  }
diff --combined php_http_env.c
index 311541d7102ee095f480c8108d1a8e3f33452add,6de5ecb90a5ad419b8f098bb16dc31e1287a341c..0ede201e875a349a850f9b443225119c0d248938
@@@ -29,34 -29,41 +29,34 @@@ PHP_RINIT_FUNCTION(http_env
                SG(request_info).content_type_dup = ct_str;
  
                ZEND_INIT_SYMTABLE(&params);
 -              if (php_http_params_parse(&params, &opts TSRMLS_CC)) {
 -                      char *key_str;
 -                      uint key_len;
 -                      ulong key_num;
 +              if (php_http_params_parse(&params, &opts)) {
 +                      zend_string *key_str;
 +                      zend_ulong key_num;
  
 -                      if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(&params, &key_str, &key_len, &key_num, 0, NULL)) {
 +                      if (HASH_KEY_IS_STRING == zend_hash_get_current_key(&params, &key_str, &key_num)) {
                                sapi_post_entry *post_entry = NULL;
  
 -                              if (SUCCESS == zend_hash_find(&SG(known_post_content_types), key_str, key_len, (void *) &post_entry)) {
 -                                      zval *files = PG(http_globals)[TRACK_VARS_FILES];
 +                              if ((post_entry = zend_hash_find_ptr(&SG(known_post_content_types), key_str))) {
 +                                      SG(request_info).post_entry = post_entry;
  
 -                                      if (post_entry) {
 -                                              SG(request_info).post_entry = post_entry;
 -
 -                                              if (post_entry->post_reader) {
 -                                                      post_entry->post_reader(TSRMLS_C);
 -                                              }
 +                                      if (post_entry->post_reader) {
 +                                              post_entry->post_reader();
                                        }
  
                                        if (sapi_module.default_post_reader) {
 -                                              sapi_module.default_post_reader(TSRMLS_C);
 +                                              sapi_module.default_post_reader();
                                        }
  
 -                                      sapi_handle_post(PG(http_globals)[TRACK_VARS_POST] TSRMLS_CC);
 +                                      sapi_handle_post(&PG(http_globals)[TRACK_VARS_POST]);
  
                                        /*
                                         * the rfc1867 handler is an awkward buddy
 +                                       * FIXME: this leaks because php_auto_globals_create_files()
 +                                       * as well as the rfc1867_handler call
 +                                       * array_init(&PG(http_globals)[TRACK_VARS_FILES])
                                         */
 -                                      if (files != PG(http_globals)[TRACK_VARS_FILES] && PG(http_globals)[TRACK_VARS_FILES]) {
 -                                              Z_ADDREF_P(PG(http_globals)[TRACK_VARS_FILES]);
 -                                              zend_hash_update(&EG(symbol_table), "_FILES", sizeof("_FILES"), &PG(http_globals)[TRACK_VARS_FILES], sizeof(zval *), NULL);
 -                                              if (files) {
 -                                                      zval_ptr_dtor(&files);
 -                                              }
 -                                      }
 +                                      Z_TRY_ADDREF(PG(http_globals)[TRACK_VARS_FILES]);
 +                                      zend_hash_str_update(&EG(symbol_table), "_FILES", lenof("_FILES"), &PG(http_globals)[TRACK_VARS_FILES]);
                                }
                        }
                        zend_hash_destroy(&params);
@@@ -80,7 -87,7 +80,7 @@@ PHP_RSHUTDOWN_FUNCTION(http_env
        }
  
        if (PHP_HTTP_G->env.server_var) {
 -              zval_ptr_dtor(&PHP_HTTP_G->env.server_var);
 +              zval_ptr_dtor(PHP_HTTP_G->env.server_var);
                PHP_HTTP_G->env.server_var = NULL;
        }
  
  
  void php_http_env_get_request_headers(HashTable *headers TSRMLS_DC)
  {
 -      php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 -      zval **hsv, **header;
 -      HashPosition pos;
 +      php_http_arrkey_t key;
 +      zval *hsv, *header;
  
        if (!PHP_HTTP_G->env.request.headers) {
                ALLOC_HASHTABLE(PHP_HTTP_G->env.request.headers);
 -              zend_hash_init(PHP_HTTP_G->env.request.headers, 0, NULL, ZVAL_PTR_DTOR, 0);
 +              ZEND_INIT_SYMTABLE(PHP_HTTP_G->env.request.headers);
  
 -              zend_is_auto_global("_SERVER", lenof("_SERVER") TSRMLS_CC);
 +              if ((hsv = php_http_env_get_superglobal(ZEND_STRL("_SERVER")))) {
 +                      ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(hsv), key.h, key.key, header)
 +                      {
 +                              if (key.key && key.key->len > 5 && *key.key->val == 'H' && !strncmp(key.key->val, "HTTP_", 5)) {
 +                                      size_t key_len = key.key->len - 5;
 +                                      char *key_str = php_http_pretty_key(estrndup(&key.key->val[5], key_len), key_len, 1, 1);
  
 -              if (SUCCESS == zend_hash_find(&EG(symbol_table), "_SERVER", sizeof("_SERVER"), (void *) &hsv) && Z_TYPE_PP(hsv) == IS_ARRAY) {
 -                      FOREACH_KEY(pos, *hsv, key) {
 -                              if (key.type == HASH_KEY_IS_STRING && key.len > 6 && *key.str == 'H' && !strncmp(key.str, "HTTP_", 5)) {
 -                                      key.len -= 5;
 -                                      key.str = php_http_pretty_key(estrndup(key.str + 5, key.len - 1), key.len - 1, 1, 1);
 +                                      Z_TRY_ADDREF_P(header);
 +                                      zend_symtable_str_update(PHP_HTTP_G->env.request.headers, key_str, key_len, header);
  
 -                                      zend_hash_get_current_data_ex(Z_ARRVAL_PP(hsv), (void *) &header, &pos);
 -                                      Z_ADDREF_P(*header);
 -                                      zend_symtable_update(PHP_HTTP_G->env.request.headers, key.str, key.len, (void *) header, sizeof(zval *), NULL);
 +                                      efree(key_str);
 +                              } else if (key.key && key.key->len > 8 && *key.key->val == 'C' && !strncmp(key.key->val, "CONTENT_", 8)) {
 +                                      char *key_str = php_http_pretty_key(estrndup(key.key->val, key.key->len), key.key->len, 1, 1);
  
 -                                      efree(key.str);
 -                              } else if (key.type == HASH_KEY_IS_STRING && key.len > 9 && *key.str == 'C' && !strncmp(key.str, "CONTENT_", 8)) {
 -                                      key.str = php_http_pretty_key(estrndup(key.str, key.len - 1), key.len - 1, 1, 1);
 +                                      Z_TRY_ADDREF_P(header);
 +                                      zend_symtable_str_update(PHP_HTTP_G->env.request.headers, key_str, key.key->len, header);
  
 -                                      zend_hash_get_current_data_ex(Z_ARRVAL_PP(hsv), (void *) &header, &pos);
 -                                      Z_ADDREF_P(*header);
 -                                      zend_symtable_update(PHP_HTTP_G->env.request.headers, key.str, key.len, (void *) header, sizeof(zval *), NULL);
 -
 -                                      efree(key.str);
 +                                      efree(key_str);
                                }
                        }
 +                      ZEND_HASH_FOREACH_END();
                }
        }
  
        if (headers) {
 -              zend_hash_copy(headers, PHP_HTTP_G->env.request.headers, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
 +              array_copy(PHP_HTTP_G->env.request.headers, headers);
        }
  }
  
 -char *php_http_env_get_request_header(const char *name_str, size_t name_len, size_t *len, php_http_message_t *request TSRMLS_DC)
 +char *php_http_env_get_request_header(const char *name_str, size_t name_len, size_t *len, php_http_message_t *request)
  {
        HashTable *request_headers;
 -      zval **zvalue = NULL;
 +      zval *zvalue = NULL;
        char *val = NULL, *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
  
        if (request) {
                request_headers = &request->hdrs;
        } else {
 -              php_http_env_get_request_headers(NULL TSRMLS_CC);
 +              php_http_env_get_request_headers(NULL);
                request_headers = PHP_HTTP_G->env.request.headers;
        }
  
 -      if (SUCCESS == zend_symtable_find(request_headers, key, name_len + 1, (void *) &zvalue)) {
 -              zval *zcopy = php_http_ztyp(IS_STRING, *zvalue);
 +      if ((zvalue = zend_symtable_str_find(request_headers, key, name_len))) {
 +              zend_string *zs = zval_get_string(zvalue);
  
 -              val = estrndup(Z_STRVAL_P(zcopy), Z_STRLEN_P(zcopy));
 +              val = estrndup(zs->val, zs->len);
                if (len) {
 -                      *len = Z_STRLEN_P(zcopy);
 +                      *len = zs->len;
                }
 -              zval_ptr_dtor(&zcopy);
 +              zend_string_release(zs);
        }
  
        efree(key);
        return val;
  }
  
 -int php_http_env_got_request_header(const char *name_str, size_t name_len, php_http_message_t *request TSRMLS_DC)
 +zend_bool php_http_env_got_request_header(const char *name_str, size_t name_len, php_http_message_t *request)
  {
        HashTable *request_headers;
        char *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
 -      int got;
 +      zend_bool got;
  
        if (request) {
                request_headers = &request->hdrs;
        } else {
 -              php_http_env_get_request_headers(NULL TSRMLS_CC);
 +              php_http_env_get_request_headers(NULL);
                request_headers = PHP_HTTP_G->env.request.headers;
        }
 -      got = zend_symtable_exists(request_headers, key, name_len + 1);
 +      got = zend_symtable_str_exists(request_headers, key, name_len);
        efree(key);
  
        return got;
  }
  
 -zval *php_http_env_get_superglobal(const char *key, size_t key_len TSRMLS_DC)
 +zval *php_http_env_get_superglobal(const char *key, size_t key_len)
  {
 -      zval **hsv;
 +      zval *hsv;
 +      zend_string *key_str = zend_string_init(key, key_len, 0);
  
 -      zend_is_auto_global(key, key_len TSRMLS_CC);
 +      zend_is_auto_global(key_str);
 +      hsv = zend_hash_find(&EG(symbol_table), key_str);
 +      zend_string_release(key_str);
  
 -      if ((SUCCESS != zend_hash_find(&EG(symbol_table), key, key_len + 1, (void *) &hsv)) || (Z_TYPE_PP(hsv) != IS_ARRAY)) {
 +      if (Z_TYPE_P(hsv) != IS_ARRAY) {
                return NULL;
        }
  
 -      return *hsv;
 +      return hsv;
  }
  
 -zval *php_http_env_get_server_var(const char *key, size_t key_len, zend_bool check TSRMLS_DC)
 +zval *php_http_env_get_server_var(const char *key, size_t key_len, zend_bool check)
  {
 -      zval *hsv, **var;
 -      char *env;
 +      zval *hsv, *var;
  
 -      /* if available, this is a lot faster than accessing $_SERVER */
 +      /* if available, this is a lot faster than accessing $_SERVER * /
        if (sapi_module.getenv) {
 -              if ((!(env = sapi_module.getenv((char *) key, key_len TSRMLS_CC))) || (check && !*env)) {
 +              char *env;
 +
 +              if ((!(env = sapi_module.getenv((char *) key, key_len))) || (check && !*env)) {
                        return NULL;
                }
                if (PHP_HTTP_G->env.server_var) {
                ZVAL_STRING(PHP_HTTP_G->env.server_var, env, 1);
                return PHP_HTTP_G->env.server_var;
        }
 +      / * */
  
 -      if (!(hsv = php_http_env_get_superglobal(ZEND_STRL("_SERVER") TSRMLS_CC))) {
 +      if (!(hsv = php_http_env_get_superglobal(ZEND_STRL("_SERVER")))) {
                return NULL;
        }
 -      if ((SUCCESS != zend_symtable_find(Z_ARRVAL_P(hsv), key, key_len + 1, (void *) &var))) {
 +      if (!(var = zend_symtable_str_find(Z_ARRVAL_P(hsv), key, key_len))) {
                return NULL;
        }
 -      if (check && !((Z_TYPE_PP(var) == IS_STRING) && Z_STRVAL_PP(var) && Z_STRLEN_PP(var))) {
 +      if (check && !((Z_TYPE_P(var) == IS_STRING) && Z_STRVAL_P(var) && Z_STRLEN_P(var))) {
                return NULL;
        }
 -      return *var;
 +      return var;
  }
  
 -php_http_message_body_t *php_http_env_get_request_body(TSRMLS_D)
 +php_http_message_body_t *php_http_env_get_request_body(void)
  {
        if (!PHP_HTTP_G->env.request.body) {
                php_stream *s = php_stream_temp_new();
 -#if PHP_VERSION_ID >= 50600
                php_stream *input = php_stream_open_wrapper("php://input", "r", 0, NULL);
  
                /* php://input does not support stat */
                php_stream_copy_to_stream_ex(input, s, -1, NULL);
                php_stream_close(input);
 -#else
 -              if (SG(request_info).post_data || SG(request_info).raw_post_data) {
 -                      /* php://input does not support seek() in PHP <= 5.5 */
 -                      if (SG(request_info).raw_post_data) {
 -                              php_stream_write(s, SG(request_info).raw_post_data, SG(request_info).raw_post_data_length);
 -                      } else {
 -                              php_stream_write(s, SG(request_info).post_data, SG(request_info).post_data_length);
 -                      }
 -              } else if (sapi_module.read_post && !SG(read_post_bytes)) {
 -                      char *buf = emalloc(4096);
 -                      int len;
 -
 -                      while (0 < (len = sapi_module.read_post(buf, 4096 TSRMLS_CC))) {
 -                              SG(read_post_bytes) += len;
 -                              php_stream_write(s, buf, len);
  
 -                              if (len < 4096) {
 -                                      break;
 -                              }
 -                      }
 -                      efree(buf);
 -              }
 -#endif
                php_stream_rewind(s);
 -              PHP_HTTP_G->env.request.body = php_http_message_body_init(NULL, s TSRMLS_CC);
 +              PHP_HTTP_G->env.request.body = php_http_message_body_init(NULL, s);
        }
  
        return PHP_HTTP_G->env.request.body;
  }
  
 -const char *php_http_env_get_request_method(php_http_message_t *request TSRMLS_DC)
 +const char *php_http_env_get_request_method(php_http_message_t *request)
  {
        const char *m;
  
        return m ? m : "GET";
  }
  
 -php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_t length, php_http_message_t *request TSRMLS_DC)
 +php_http_range_status_t php_http_env_get_request_ranges(HashTable *ranges, size_t length, php_http_message_t *request)
  {
 -      zval *zentry;
 +      zval zentry;
        char *range, *rp, c;
        long begin = -1, end = -1, *ptr;
  
 -      if (!(range = php_http_env_get_request_header(ZEND_STRL("Range"), NULL, request TSRMLS_CC))) {
 +      if (!(range = php_http_env_get_request_header(ZEND_STRL("Range"), NULL, request))) {
                return PHP_HTTP_RANGE_NO;
        }
        if (strncmp(range, "bytes=", lenof("bytes="))) {
                                        }
                                }
  
 -                              MAKE_STD_ZVAL(zentry);
 -                              array_init(zentry);
 -                              add_index_long(zentry, 0, begin);
 -                              add_index_long(zentry, 1, end);
 -                              zend_hash_next_index_insert(ranges, &zentry, sizeof(zval *), NULL);
 +                              array_init(&zentry);
 +                              add_index_long(&zentry, 0, begin);
 +                              add_index_long(&zentry, 1, end);
 +                              zend_hash_next_index_insert(ranges, &zentry);
  
                                begin = -1;
                                end = -1;
        return PHP_HTTP_RANGE_OK;
  }
  
 -static void grab_headers(void *data, void *arg TSRMLS_DC)
 +static void grab_headers(void *data, void *arg)
  {
        php_http_buffer_appendl(PHP_HTTP_BUFFER(arg), ((sapi_header_struct *)data)->header);
        php_http_buffer_appends(PHP_HTTP_BUFFER(arg), PHP_HTTP_CRLF);
  }
  
 -STATUS php_http_env_get_response_headers(HashTable *headers_ht TSRMLS_DC)
 +static void grab_header(void *data, void *arg)
 +{
 +      struct {
 +              char *name_str;
 +              size_t name_len;
 +              char *value_ptr;
 +      } *args = arg;
 +      sapi_header_struct *header = data;
 +
 +      if (    header->header_len > args->name_len
 +      && header->header[args->name_len] == ':'
 +      && !strncmp(header->header, args->name_str, args->name_len)
 +      ) {
 +              args->value_ptr = &header->header[args->name_len + 1];
 +              while (PHP_HTTP_IS_CTYPE(space, *args->value_ptr)) {
 +                      ++args->value_ptr;
 +              }
 +      }
 +}
 +
 +ZEND_RESULT_CODE php_http_env_get_response_headers(HashTable *headers_ht)
  {
 -      STATUS status;
 +      ZEND_RESULT_CODE status;
        php_http_buffer_t headers;
  
        php_http_buffer_init(&headers);
 -      zend_llist_apply_with_argument(&SG(sapi_headers).headers, grab_headers, &headers TSRMLS_CC);
 +      zend_llist_apply_with_argument(&SG(sapi_headers).headers, grab_headers, &headers);
        php_http_buffer_fix(&headers);
  
 -      status = php_http_header_parse(headers.data, headers.used, headers_ht, NULL, NULL TSRMLS_CC);
 +      status = php_http_header_parse(headers.data, headers.used, headers_ht, NULL, NULL);
        php_http_buffer_dtor(&headers);
  
        return status;
  }
  
 -char *php_http_env_get_response_header(const char *name_str, size_t name_len TSRMLS_DC)
 +char *php_http_env_get_response_header(const char *name_str, size_t name_len)
  {
 -      char *val = NULL;
 -      HashTable headers;
 +      struct {
 +              char *name_str;
 +              size_t name_len;
 +              char *value_ptr;
 +      } args;
  
 -      zend_hash_init(&headers, 0, NULL, ZVAL_PTR_DTOR, 0);
 -      if (SUCCESS == php_http_env_get_response_headers(&headers TSRMLS_CC)) {
 -              zval **zvalue;
 -              char *key = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
 +      args.name_str = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
 +      args.name_len = name_len;
 +      args.value_ptr = NULL;
 +      zend_llist_apply_with_argument(&SG(sapi_headers).headers, grab_header, &args);
 +      efree(args.name_str);
  
 -              if (SUCCESS == zend_symtable_find(&headers, key, name_len + 1, (void *) &zvalue)) {
 -                      zval *zcopy = php_http_ztyp(IS_STRING, *zvalue);
 -
 -                      val = estrndup(Z_STRVAL_P(zcopy), Z_STRLEN_P(zcopy));
 -                      zval_ptr_dtor(&zcopy);
 -              }
 -
 -              efree(key);
 -      }
 -      zend_hash_destroy(&headers);
 -
 -      return val;
 +      return args.value_ptr ? estrdup(args.value_ptr) : NULL;
  }
  
 -long php_http_env_get_response_code(TSRMLS_D)
 +long php_http_env_get_response_code(void)
  {
        long code = SG(sapi_headers).http_response_code;
        return code ? code : 200;
  }
  
 -STATUS php_http_env_set_response_code(long http_code TSRMLS_DC)
 +ZEND_RESULT_CODE php_http_env_set_response_code(long http_code)
  {
 -      return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) http_code TSRMLS_CC);
 +      return sapi_header_op(SAPI_HEADER_SET_STATUS, (void *) (zend_intptr_t) http_code);
  }
  
 -STATUS php_http_env_set_response_status_line(long code, php_http_version_t *v TSRMLS_DC)
 +ZEND_RESULT_CODE php_http_env_set_response_status_line(long code, php_http_version_t *v)
  {
        sapi_header_line h = {NULL, 0, 0};
 -      STATUS ret;
 +      ZEND_RESULT_CODE ret;
  
        h.line_len = spprintf(&h.line, 0, "HTTP/%u.%u %ld %s", v->major, v->minor, code, php_http_env_get_response_status_for_code(code));
 -      ret = sapi_header_op(SAPI_HEADER_REPLACE, (void *) &h TSRMLS_CC);
 +      ret = sapi_header_op(SAPI_HEADER_REPLACE, (void *) &h);
        efree(h.line);
  
        return ret;
  }
  
 -STATUS php_http_env_set_response_protocol_version(php_http_version_t *v TSRMLS_DC)
 +ZEND_RESULT_CODE php_http_env_set_response_protocol_version(php_http_version_t *v)
  {
 -      return php_http_env_set_response_status_line(php_http_env_get_response_code(TSRMLS_C), v TSRMLS_CC);
 +      return php_http_env_set_response_status_line(php_http_env_get_response_code(), v);
  }
  
 -STATUS php_http_env_set_response_header(long http_code, const char *header_str, size_t header_len, zend_bool replace TSRMLS_DC)
 +ZEND_RESULT_CODE php_http_env_set_response_header(long http_code, const char *header_str, size_t header_len, zend_bool replace)
  {
        sapi_header_line h = {estrndup(header_str, header_len), header_len, http_code};
 -      STATUS ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h TSRMLS_CC);
 +      ZEND_RESULT_CODE ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h);
        efree(h.line);
        return ret;
  }
  
 -STATUS php_http_env_set_response_header_va(long http_code, zend_bool replace, const char *fmt, va_list argv TSRMLS_DC)
 +ZEND_RESULT_CODE php_http_env_set_response_header_va(long http_code, zend_bool replace, const char *fmt, va_list argv)
  {
 -      STATUS ret = FAILURE;
 +      ZEND_RESULT_CODE ret = FAILURE;
        sapi_header_line h = {NULL, 0, http_code};
  
        h.line_len = vspprintf(&h.line, 0, fmt, argv);
  
        if (h.line) {
                if (h.line_len) {
 -                      ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h TSRMLS_CC);
 +                      ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h);
                }
                efree(h.line);
        }
        return ret;
  }
  
 -STATUS php_http_env_set_response_header_format(long http_code, zend_bool replace TSRMLS_DC, const char *fmt, ...)
 +ZEND_RESULT_CODE php_http_env_set_response_header_format(long http_code, zend_bool replace, const char *fmt, ...)
  {
 -      STATUS ret;
 +      ZEND_RESULT_CODE ret;
        va_list args;
  
        va_start(args, fmt);
 -      ret = php_http_env_set_response_header_va(http_code, replace, fmt, args TSRMLS_CC);
 +      ret = php_http_env_set_response_header_va(http_code, replace, fmt, args);
        va_end(args);
  
        return ret;
  }
  
 -STATUS php_http_env_set_response_header_value(long http_code, const char *name_str, size_t name_len, zval *value, zend_bool replace TSRMLS_DC)
 +ZEND_RESULT_CODE php_http_env_set_response_header_value(long http_code, const char *name_str, size_t name_len, zval *value, zend_bool replace)
  {
        if (!value) {
                sapi_header_line h = {(char *) name_str, name_len, http_code};
  
 -              return sapi_header_op(SAPI_HEADER_DELETE, (void *) &h TSRMLS_CC);
 +              return sapi_header_op(SAPI_HEADER_DELETE, (void *) &h);
        }
  
 -      if(Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
 -              HashPosition pos;
 +      if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
                int first = replace;
 -              zval **data_ptr;
 +              zval *data_ptr;
 +              HashTable *ht = HASH_OF(value);
  
 -              FOREACH_HASH_VAL(pos, HASH_OF(value), data_ptr) {
 -                      if (SUCCESS != php_http_env_set_response_header_value(http_code, name_str, name_len, *data_ptr, first TSRMLS_CC)) {
 +              ZEND_HASH_FOREACH_VAL_IND(ht, data_ptr)
 +              {
 +                      if (SUCCESS != php_http_env_set_response_header_value(http_code, name_str, name_len, data_ptr, first)) {
                                return FAILURE;
                        }
                        first = 0;
                }
 +              ZEND_HASH_FOREACH_END();
  
                return SUCCESS;
        } else {
 -              zval *data = php_http_ztyp(IS_STRING, value);
 +              zend_string *data = zval_get_string(value);
  
 -              if (!Z_STRLEN_P(data)) {
 -                      zval_ptr_dtor(&data);
 -                      return php_http_env_set_response_header_value(http_code, name_str, name_len, NULL, replace TSRMLS_CC);
 +              if (!data->len) {
 +                      zend_string_release(data);
 +                      return php_http_env_set_response_header_value(http_code, name_str, name_len, NULL, replace);
                } else {
                        sapi_header_line h;
 -                      STATUS ret;
 +                      ZEND_RESULT_CODE ret;
  
                        if (name_len > INT_MAX) {
 -                              name_len = INT_MAX;
 +                              return FAILURE;
                        }
                        h.response_code = http_code;
 -                      h.line_len = spprintf(&h.line, 0, "%.*s: %.*s", (int) name_len, name_str, Z_STRLEN_P(data), Z_STRVAL_P(data));
 +                      h.line_len = spprintf(&h.line, 0, "%.*s: %.*s", (int) name_len, name_str, data->len, data->val);
  
 -                      ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h TSRMLS_CC);
 +                      ret = sapi_header_op(replace ? SAPI_HEADER_REPLACE : SAPI_HEADER_ADD, (void *) &h);
  
 -                      zval_ptr_dtor(&data);
 +                      zend_string_release(data);
                        PTR_FREE(h.line);
  
                        return ret;
        }
  }
  
- static PHP_HTTP_STRLIST(php_http_env_response_status) =
-       PHP_HTTP_STRLIST_ITEM("Continue")
-       PHP_HTTP_STRLIST_ITEM("Switching Protocols")
-       PHP_HTTP_STRLIST_ITEM("Processing")
-       PHP_HTTP_STRLIST_NEXT
-       PHP_HTTP_STRLIST_ITEM("OK")
-       PHP_HTTP_STRLIST_ITEM("Created")
-       PHP_HTTP_STRLIST_ITEM("Accepted")
-       PHP_HTTP_STRLIST_ITEM("Non-Authoritative Information")
-       PHP_HTTP_STRLIST_ITEM("No Content")
-       PHP_HTTP_STRLIST_ITEM("Reset Content")
-       PHP_HTTP_STRLIST_ITEM("Partial Content")
-       PHP_HTTP_STRLIST_ITEM("Multi-Status")
-       PHP_HTTP_STRLIST_ITEM("Already Reported")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("IM Used")
-       PHP_HTTP_STRLIST_NEXT
-       PHP_HTTP_STRLIST_ITEM("Multiple Choices")
-       PHP_HTTP_STRLIST_ITEM("Moved Permanently")
-       PHP_HTTP_STRLIST_ITEM("Found")
-       PHP_HTTP_STRLIST_ITEM("See Other")
-       PHP_HTTP_STRLIST_ITEM("Not Modified")
-       PHP_HTTP_STRLIST_ITEM("Use Proxy")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("Temporary Redirect")
-       PHP_HTTP_STRLIST_ITEM("Permanent Redirect")
-       PHP_HTTP_STRLIST_NEXT
-       PHP_HTTP_STRLIST_ITEM("Bad Request")
-       PHP_HTTP_STRLIST_ITEM("Unauthorized")
-       PHP_HTTP_STRLIST_ITEM("Payment Required")
-       PHP_HTTP_STRLIST_ITEM("Forbidden")
-       PHP_HTTP_STRLIST_ITEM("Not Found")
-       PHP_HTTP_STRLIST_ITEM("Method Not Allowed")
-       PHP_HTTP_STRLIST_ITEM("Not Acceptable")
-       PHP_HTTP_STRLIST_ITEM("Proxy Authentication Required")
-       PHP_HTTP_STRLIST_ITEM("Request Timeout")
-       PHP_HTTP_STRLIST_ITEM("Conflict")
-       PHP_HTTP_STRLIST_ITEM("Gone")
-       PHP_HTTP_STRLIST_ITEM("Length Required")
-       PHP_HTTP_STRLIST_ITEM("Precondition Failed")
-       PHP_HTTP_STRLIST_ITEM("Request Entity Too Large")
-       PHP_HTTP_STRLIST_ITEM("Request URI Too Long")
-       PHP_HTTP_STRLIST_ITEM("Unsupported Media Type")
-       PHP_HTTP_STRLIST_ITEM("Requested Range Not Satisfiable")
-       PHP_HTTP_STRLIST_ITEM("Expectation Failed")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("Unprocessible Entity")
-       PHP_HTTP_STRLIST_ITEM("Locked")
-       PHP_HTTP_STRLIST_ITEM("Failed Dependency")
-       PHP_HTTP_STRLIST_ITEM("(Reserved)")
-       PHP_HTTP_STRLIST_ITEM("Upgrade Required")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("Precondition Required")
-       PHP_HTTP_STRLIST_ITEM("Too Many Requests")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("Request Header Fields Too Large")
-       PHP_HTTP_STRLIST_NEXT
-       PHP_HTTP_STRLIST_ITEM("Internal Server Error")
-       PHP_HTTP_STRLIST_ITEM("Not Implemented")
-       PHP_HTTP_STRLIST_ITEM("Bad Gateway")
-       PHP_HTTP_STRLIST_ITEM("Service Unavailable")
-       PHP_HTTP_STRLIST_ITEM("Gateway Timeout")
-       PHP_HTTP_STRLIST_ITEM("HTTP Version Not Supported")
-       PHP_HTTP_STRLIST_ITEM("Variant Also Negotiates")
-       PHP_HTTP_STRLIST_ITEM("Insufficient Storage")
-       PHP_HTTP_STRLIST_ITEM("Loop Detected")
-       PHP_HTTP_STRLIST_ITEM("(Unused)")
-       PHP_HTTP_STRLIST_ITEM("Not Extended")
-       PHP_HTTP_STRLIST_ITEM("Network Authentication Required")
-       PHP_HTTP_STRLIST_STOP
- ;
  const char *php_http_env_get_response_status_for_code(unsigned code)
  {
-       return php_http_strlist_find(php_http_env_response_status, 100, code);
+       switch (code) {
+ #define PHP_HTTP_RESPONSE_CODE(c, s) case c: return s;
+ #include "php_http_response_codes.h"
+ #undef PHP_HTTP_RESPONSE_CODE
+       default:
+               return NULL;
+       }
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getRequestHeader, 0, 0, 0)
@@@ -668,21 -597,21 +583,21 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpEnv, getRequestHeader)
  {
        char *header_name_str = NULL;
 -      int header_name_len = 0;
 +      size_t header_name_len = 0;
  
 -      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
 +      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str, &header_name_len)) {
                return;
        }
        if (header_name_str && header_name_len) {
                size_t header_length;
 -              char *header_value = php_http_env_get_request_header(header_name_str, header_name_len, &header_length, NULL TSRMLS_CC);
 +              char *header_value = php_http_env_get_request_header(header_name_str, header_name_len, &header_length, NULL);
  
                if (header_value) {
 -                      RETURN_STRINGL(header_value, header_length, 0);
 +                      RETURN_STR(php_http_cs2zs(header_value, header_length));
                }
        } else {
                array_init(return_value);
 -              php_http_env_get_request_headers(Z_ARRVAL_P(return_value) TSRMLS_CC);
 +              php_http_env_get_request_headers(Z_ARRVAL_P(return_value));
        }
  }
  
@@@ -691,16 -620,16 +606,16 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getRe
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpEnv, getRequestBody)
  {
 -      zend_object_value ov;
        php_http_message_body_t *body;
 +      php_http_message_body_object_t *body_obj;
        zend_class_entry *class_entry = php_http_message_body_class_entry;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|C", &class_entry), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|C", &class_entry), invalid_arg, return);
  
 -      body = php_http_env_get_request_body(TSRMLS_C);
 -      if (SUCCESS == php_http_new(&ov, class_entry, (php_http_new_t) php_http_message_body_object_new_ex, php_http_message_body_class_entry, body, NULL TSRMLS_CC)) {
 +      body = php_http_env_get_request_body();
 +      if (SUCCESS == php_http_new((void *) &body_obj, class_entry, (php_http_new_t) php_http_message_body_object_new_ex, php_http_message_body_class_entry, body)) {
                php_http_message_body_addref(body);
 -              RETVAL_OBJVAL(ov, 0);
 +              RETVAL_OBJ(&body_obj->zo);
        }
  }
  
@@@ -709,33 -638,30 +624,30 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getRe
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpEnv, getResponseStatusForCode)
  {
 -      long code;
 +      zend_long code;
+       const char *status;
  
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
                return;
        }
-       RETURN_STRING(php_http_env_get_response_status_for_code(code));
+       if ((status = php_http_env_get_response_status_for_code(code))) {
 -              RETURN_STRING(status, 1);
++              RETURN_STRING(status);
+       }
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseStatusForAllCodes, 0, 0, 0)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpEnv, getResponseStatusForAllCodes)
  {
-       const char *s;
-       unsigned c;
-       php_http_strlist_iterator_t i;
        if (SUCCESS != zend_parse_parameters_none()) {
                return;
        }
  
        array_init(return_value);
-       for (   php_http_strlist_iterator_init(&i, php_http_env_response_status, 100);
-                       *(s = php_http_strlist_iterator_this(&i, &c));
-                       php_http_strlist_iterator_next(&i)
-       ) {
-               add_index_string(return_value, c, s);
-       }
 -#define PHP_HTTP_RESPONSE_CODE(code, status) add_index_string(return_value, code, status, 1);
++#define PHP_HTTP_RESPONSE_CODE(code, status) add_index_string(return_value, code, status);
+ #include "php_http_response_codes.h"
+ #undef PHP_HTTP_RESPONSE_CODE
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_getResponseHeader, 0, 0, 0)
@@@ -744,20 -670,20 +656,20 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpEnv, getResponseHeader)
  {
        char *header_name_str = NULL;
 -      int header_name_len = 0;
 +      size_t header_name_len = 0;
  
 -      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
 +      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str, &header_name_len)) {
                return;
        }
        if (header_name_str && header_name_len) {
 -              char *header_value = php_http_env_get_response_header(header_name_str, header_name_len TSRMLS_CC);
 +              char *header_value = php_http_env_get_response_header(header_name_str, header_name_len);
  
                if (header_value) {
 -                      RETURN_STRING(header_value, 0);
 +                      RETURN_STR(php_http_cs2zs(header_value, strlen(header_value)));
                }
        } else {
                array_init(return_value);
 -              php_http_env_get_response_headers(Z_ARRVAL_P(return_value) TSRMLS_CC);
 +              php_http_env_get_response_headers(Z_ARRVAL_P(return_value));
        }
  }
  
@@@ -768,7 -694,7 +680,7 @@@ static PHP_METHOD(HttpEnv, getResponseC
        if (SUCCESS != zend_parse_parameters_none()) {
                return;
        }
 -      RETURN_LONG(php_http_env_get_response_code(TSRMLS_C));
 +      RETURN_LONG(php_http_env_get_response_code());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_setResponseHeader, 0, 0, 1)
@@@ -780,15 -706,15 +692,15 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpEnv, setResponseHeader)
  {
        char *header_name_str;
 -      int header_name_len;
 +      size_t header_name_len;
        zval *header_value = NULL;
 -      long code = 0;
 +      zend_long code = 0;
        zend_bool replace_header = 1;
  
 -      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z!lb", &header_name_str, &header_name_len, &header_value, &code, &replace_header)) {
 +      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "s|z!lb", &header_name_str, &header_name_len, &header_value, &code, &replace_header)) {
                return;
        }
 -      RETURN_BOOL(SUCCESS == php_http_env_set_response_header_value(code, header_name_str, header_name_len, header_value, replace_header TSRMLS_CC));
 +      RETURN_BOOL(SUCCESS == php_http_env_set_response_header_value(code, header_name_str, header_name_len, header_value, replace_header));
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_setResponseCode, 0, 0, 1)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpEnv, setResponseCode)
  {
 -      long code;
 +      zend_long code;
  
 -      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &code)) {
 +      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "l", &code)) {
                return;
        }
 -      RETURN_BOOL(SUCCESS == php_http_env_set_response_code(code TSRMLS_CC));
 +      RETURN_BOOL(SUCCESS == php_http_env_set_response_code(code));
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnv_negotiateLanguage, 0, 0, 1)
@@@ -817,7 -743,6 +729,7 @@@ static PHP_METHOD(HttpEnv, negotiateLan
                return;
        }
        if (rs_array) {
 +              ZVAL_DEREF(rs_array);
                zval_dtor(rs_array);
                array_init(rs_array);
        }
@@@ -834,11 -759,10 +746,11 @@@ static PHP_METHOD(HttpEnv, negotiateCha
        HashTable *supported;
        zval *rs_array = NULL;
  
 -      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
 +      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported, &rs_array)) {
                return;
        }
        if (rs_array) {
 +              ZVAL_DEREF(rs_array);
                zval_dtor(rs_array);
                array_init(rs_array);
        }
@@@ -854,11 -778,10 +766,11 @@@ static PHP_METHOD(HttpEnv, negotiateEnc
        HashTable *supported;
        zval *rs_array = NULL;
  
 -      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
 +      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported, &rs_array)) {
                return;
        }
        if (rs_array) {
 +              ZVAL_DEREF(rs_array);
                zval_dtor(rs_array);
                array_init(rs_array);
        }
@@@ -874,11 -797,10 +786,11 @@@ static PHP_METHOD(HttpEnv, negotiateCon
        HashTable *supported;
        zval *rs_array = NULL;
  
 -      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
 +      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported, &rs_array)) {
                return;
        }
        if (rs_array) {
 +              ZVAL_DEREF(rs_array);
                zval_dtor(rs_array);
                array_init(rs_array);
        }
@@@ -896,19 -818,19 +808,19 @@@ static PHP_METHOD(HttpEnv, negotiate
        HashTable *supported, *rs;
        zval *rs_array = NULL;
        char *value_str, *sep_str = NULL;
 -      int value_len, sep_len = 0;
 +      size_t value_len, sep_len = 0;
  
        if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sH|s!z", &value_str, &value_len, &supported, &sep_str, &sep_len, &rs_array)) {
                return;
        }
  
 -
        if (rs_array) {
 +              ZVAL_DEREF(rs_array);
                zval_dtor(rs_array);
                array_init(rs_array);
        }
  
 -      if ((rs = php_http_negotiate(value_str, value_len, supported, sep_str, sep_len TSRMLS_CC))) {
 +      if ((rs = php_http_negotiate(value_str, value_len, supported, sep_str, sep_len))) {
                PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(rs, supported, rs_array);
        } else {
                PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array);
@@@ -942,31 -864,36 +854,31 @@@ static zend_function_entry php_http_env
  static SAPI_POST_HANDLER_FUNC(php_http_json_post_handler)
  {
        zval *zarg = arg;
 -      char *json_str = NULL;
 -      size_t json_len = 0;
 +      zend_string *json = NULL;
  
 -#if PHP_VERSION_ID >= 50600
        if (SG(request_info).request_body) {
                /* FG(stream_wrappers) not initialized yet, so we cannot use php://input */
                php_stream_rewind(SG(request_info).request_body);
 -              json_len = php_stream_copy_to_mem(SG(request_info).request_body, &json_str, PHP_STREAM_COPY_ALL, 0);
 +              json = php_stream_copy_to_mem(SG(request_info).request_body, PHP_STREAM_COPY_ALL, 0);
        }
 -#else
 -      json_str = SG(request_info).raw_post_data;
 -      json_len = SG(request_info).raw_post_data_length;
 -#endif
  
 -      if (json_len) {
 -              zval zjson;
 +      if (json) {
 +              if (json->len) {
 +                      zval tmp;
 +
 +                      ZVAL_NULL(&tmp);
 +                      php_json_decode(&tmp, json->val, json->len, 1, PG(max_input_nesting_level));
  
 -              INIT_ZVAL(zjson);
 -              php_json_decode(&zjson, json_str, json_len, 1, PG(max_input_nesting_level) TSRMLS_CC);
 -              if (Z_TYPE(zjson) != IS_NULL) {
 -                      zval_dtor(zarg);
 -                      ZVAL_COPY_VALUE(zarg, (&zjson));
 +                      if (Z_TYPE(tmp) == IS_ARRAY) {
 +                              array_copy(Z_ARRVAL(tmp), Z_ARRVAL_P(zarg));
 +                      }
 +                      zval_ptr_dtor(&tmp);
                }
 +              zend_string_release(json);
        }
 -#if PHP_VERSION_ID >= 50600
 -      PTR_FREE(json_str);
 -#endif
  }
  
 -static void php_http_env_register_json_handler(TSRMLS_D)
 +static void php_http_env_register_json_handler(void)
  {
        sapi_post_entry entry = {NULL, 0, NULL, NULL};
  
  
        entry.content_type = "text/json";
        entry.content_type_len = lenof("text/json");
 -      sapi_register_post_entry(&entry TSRMLS_CC);
 +      sapi_register_post_entry(&entry);
  
        entry.content_type = "application/json";
        entry.content_type_len = lenof("application/json");
 -      sapi_register_post_entry(&entry TSRMLS_CC);
 +      sapi_register_post_entry(&entry);
  }
  #endif
  
@@@ -990,10 -917,10 +902,10 @@@ PHP_MINIT_FUNCTION(http_env
        zend_class_entry ce = {0};
  
        INIT_NS_CLASS_ENTRY(ce, "http", "Env", php_http_env_methods);
 -      php_http_env_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
 +      php_http_env_class_entry = zend_register_internal_class(&ce);
  
  #ifdef PHP_HTTP_HAVE_JSON
 -      php_http_env_register_json_handler(TSRMLS_C);
 +      php_http_env_register_json_handler();
  #endif
  
        return SUCCESS;
diff --combined php_http_env_response.c
index 7c76c081870ed8fbfe9ec7a6a345b2dbecc2cb4f,2f2f161a676655bab5578d412dea6bc4206aaffb..5b4682084278d460c6672caafa745177fd289c9a
  
  #include "php_http_api.h"
  
 -static void set_option(zval *options, const char *name_str, size_t name_len, int type, void *value_ptr, size_t value_len TSRMLS_DC)
 +static void set_option(zval *options, const char *name_str, size_t name_len, int type, void *value_ptr, size_t value_len)
  {
        if (Z_TYPE_P(options) == IS_OBJECT) {
                if (value_ptr) {
                        switch (type) {
                                case IS_DOUBLE:
 -                                      zend_update_property_double(Z_OBJCE_P(options), options, name_str, name_len, *(double *)value_ptr TSRMLS_CC);
 +                                      zend_update_property_double(Z_OBJCE_P(options), options, name_str, name_len, *(double *)value_ptr);
                                        break;
                                case IS_LONG:
 -                                      zend_update_property_long(Z_OBJCE_P(options), options, name_str, name_len, *(long *)value_ptr TSRMLS_CC);
 +                                      zend_update_property_long(Z_OBJCE_P(options), options, name_str, name_len, *(zend_long *)value_ptr);
                                        break;
                                case IS_STRING:
 -                                      zend_update_property_stringl(Z_OBJCE_P(options), options, name_str, name_len, value_ptr, value_len TSRMLS_CC);
 +                                      zend_update_property_stringl(Z_OBJCE_P(options), options, name_str, name_len, value_ptr, value_len);
                                        break;
                                case IS_ARRAY:
                                case IS_OBJECT:
 -                                      zend_update_property(Z_OBJCE_P(options), options, name_str, name_len, value_ptr TSRMLS_CC);
 +                                      zend_update_property(Z_OBJCE_P(options), options, name_str, name_len, value_ptr);
                                        break;
                        }
                } else {
 -                      zend_update_property_null(Z_OBJCE_P(options), options, name_str, name_len TSRMLS_CC);
 +                      zend_update_property_null(Z_OBJCE_P(options), options, name_str, name_len);
                }
        } else {
                convert_to_array(options);
                if (value_ptr) {
                        switch (type) {
                                case IS_DOUBLE:
 -                                      add_assoc_double_ex(options, name_str, name_len + 1, *(double *)value_ptr);
 +                                      add_assoc_double_ex(options, name_str, name_len, *(double *)value_ptr);
                                        break;
                                case IS_LONG:
 -                                      add_assoc_long_ex(options, name_str, name_len + 1, *(long *)value_ptr);
 +                                      add_assoc_long_ex(options, name_str, name_len, *(zend_long *)value_ptr);
                                        break;
                                case IS_STRING: {
 -                                      char *value = estrndup(value_ptr, value_len);
 -                                      add_assoc_stringl_ex(options, name_str, name_len + 1, value, value_len, 0);
 +                                      zend_string *value = zend_string_init(value_ptr, value_len, 0);
 +                                      add_assoc_str_ex(options, name_str, name_len, value);
                                        break;
                                case IS_ARRAY:
                                case IS_OBJECT:
                                        Z_ADDREF_P(value_ptr);
 -                                      add_assoc_zval_ex(options, name_str, name_len + 1, value_ptr);
 +                                      add_assoc_zval_ex(options, name_str, name_len, value_ptr);
                                        break;
                                }
                        }
                } else {
 -                      add_assoc_null_ex(options, name_str, name_len + 1);
 +                      add_assoc_null_ex(options, name_str, name_len);
                }
        }
  }
 -static zval *get_option(zval *options, const char *name_str, size_t name_len TSRMLS_DC)
 +static zval *get_option(zval *options, const char *name_str, size_t name_len, zval *tmp)
  {
 -      zval *val, **valptr;
 +      zval *val = NULL;
  
        if (Z_TYPE_P(options) == IS_OBJECT) {
 -              val = zend_read_property(Z_OBJCE_P(options), options, name_str, name_len, 0 TSRMLS_CC);
 +              val = zend_read_property(Z_OBJCE_P(options), options, name_str, name_len, 0, tmp);
 +      } else if (Z_TYPE_P(options) == IS_ARRAY) {
 +              val = zend_symtable_str_find(Z_ARRVAL_P(options), name_str, name_len);
        } else {
 -              if (SUCCESS == zend_symtable_find(Z_ARRVAL_P(options), name_str, name_len + 1, (void *) &valptr)) {
 -                      val = *valptr;
 -              } else {
 -                      val = NULL;
 -              }
 +              abort();
        }
        if (val) {
 -              Z_ADDREF_P(val);
 +              Z_TRY_ADDREF_P(val);
        }
        return val;
  }
 -static php_http_message_body_t *get_body(zval *options TSRMLS_DC)
 +static php_http_message_body_t *get_body(zval *options)
  {
 -      zval *zbody;
 +      zval zbody_tmp, *zbody;
        php_http_message_body_t *body = NULL;
  
 -      if ((zbody = get_option(options, ZEND_STRL("body") TSRMLS_CC))) {
 -              if ((Z_TYPE_P(zbody) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC)) {
 -                      php_http_message_body_object_t *body_obj = zend_object_store_get_object(zbody TSRMLS_CC);
 +      if ((zbody = get_option(options, ZEND_STRL("body"), &zbody_tmp))) {
 +              if ((Z_TYPE_P(zbody) == IS_OBJECT) && instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry)) {
 +                      php_http_message_body_object_t *body_obj = PHP_HTTP_OBJ(NULL, zbody);
  
                        body = body_obj->body;
                }
 -              zval_ptr_dtor(&zbody);
 +              Z_TRY_DELREF_P(zbody);
        }
  
        return body;
  }
 -static php_http_message_t *get_request(zval *options TSRMLS_DC)
 +static php_http_message_t *get_request(zval *options)
  {
 -      zval *zrequest;
 +      zval zrequest_tmp, *zrequest;
        php_http_message_t *request = NULL;
  
 -      if ((zrequest = get_option(options, ZEND_STRL("request") TSRMLS_CC))) {
 -              if (Z_TYPE_P(zrequest) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zrequest), php_http_message_class_entry TSRMLS_CC)) {
 -                      php_http_message_object_t *request_obj = zend_object_store_get_object(zrequest TSRMLS_CC);
 +      if ((zrequest = get_option(options, ZEND_STRL("request"), &zrequest_tmp))) {
 +              if (Z_TYPE_P(zrequest) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zrequest), php_http_message_class_entry)) {
 +                      php_http_message_object_t *request_obj = PHP_HTTP_OBJ(NULL, zrequest);
  
                        request = request_obj->message;
                }
 -              zval_ptr_dtor(&zrequest);
 +              Z_TRY_DELREF_P(zrequest);
        }
  
        return request;
  }
 -static void set_cookie(zval *options, zval *zcookie_new TSRMLS_DC)
 +static void set_cookie(zval *options, zval *zcookie_new)
  {
 -      HashPosition pos;
 -      zval *zcookies_set;
 -      php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 -      php_http_cookie_object_t *obj = zend_object_store_get_object(zcookie_new TSRMLS_CC);
 -
 -      zcookies_set = get_option(options, ZEND_STRL("cookies") TSRMLS_CC);
 -      if (!zcookies_set || Z_TYPE_P(zcookies_set) != IS_ARRAY) {
 -              if (zcookies_set) {
 -                      zval_ptr_dtor(&zcookies_set);
 -              }
 -              MAKE_STD_ZVAL(zcookies_set);
 -              array_init_size(zcookies_set, zend_hash_num_elements(&obj->list->cookies));
 -      } else {
 -              SEPARATE_ZVAL(&zcookies_set);
 +      zval tmp, zcookies_set_tmp, *zcookies_set;
 +      php_http_arrkey_t key;
 +      php_http_cookie_object_t *obj = PHP_HTTP_OBJ(NULL, zcookie_new);
 +
 +      array_init(&tmp);
 +      zcookies_set = get_option(options, ZEND_STRL("cookies"), &zcookies_set_tmp);
 +      if (zcookies_set && Z_TYPE_P(zcookies_set) == IS_ARRAY) {
 +              array_copy(Z_ARRVAL_P(zcookies_set), Z_ARRVAL(tmp));
 +              zval_ptr_dtor(zcookies_set);
        }
  
 -      FOREACH_HASH_KEY(pos, &obj->list->cookies, key) {
 +      ZEND_HASH_FOREACH_KEY(&obj->list->cookies, key.h, key.key)
 +      {
                Z_ADDREF_P(zcookie_new);
 -              if (key.type == HASH_KEY_IS_STRING) {
 -                      add_assoc_zval_ex(zcookies_set, key.str, key.len, zcookie_new);
 +              if (key.key) {
 +                      add_assoc_zval_ex(&tmp, key.key->val, key.key->len, zcookie_new);
                } else {
 -                      add_index_zval(zcookies_set, key.num, zcookie_new);
 +                      add_index_zval(&tmp, key.h, zcookie_new);
                }
        }
 +      ZEND_HASH_FOREACH_END();
  
 -      set_option(options, ZEND_STRL("cookies"), IS_ARRAY, zcookies_set, 0 TSRMLS_CC);
 -      zval_ptr_dtor(&zcookies_set);
 +      set_option(options, ZEND_STRL("cookies"), IS_ARRAY, &tmp, 0);
 +      zval_ptr_dtor(&tmp);
  }
  
 -php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *options, const char *header_str, size_t header_len, php_http_message_t *request TSRMLS_DC)
 +php_http_cache_status_t php_http_env_is_response_cached_by_etag(zval *options, const char *header_str, size_t header_len, php_http_message_t *request)
  {
        php_http_cache_status_t ret = PHP_HTTP_CACHE_NO;
 -      int free_etag = 0;
 -      char *header = NULL, *etag;
 +      char *header = NULL, *etag = NULL;
        php_http_message_body_t *body;
 -      zval *zetag;
 +      zval zetag_tmp, *zetag;
  
  
 -      if (!(body = get_body(options TSRMLS_CC))) {
 +      if (!(body = get_body(options))) {
                return ret;
        }
  
 -      if ((zetag = get_option(options, ZEND_STRL("etag") TSRMLS_CC))) {
 -              zval *zetag_copy = php_http_ztyp(IS_STRING, zetag);
 -              zval_ptr_dtor(&zetag);
 -              zetag = zetag_copy;
 +      if ((zetag = get_option(options, ZEND_STRL("etag"), &zetag_tmp)) && Z_TYPE_P(zetag) != IS_NULL) {
 +              zend_string *zs = zval_get_string(zetag);
 +              etag = estrndup(zs->val, zs->len);
 +              zend_string_release(zs);
 +              zval_ptr_dtor(zetag);
        }
  
 -      if (zetag && Z_STRLEN_P(zetag)) {
 -              etag = Z_STRVAL_P(zetag);
 -      } else if ((etag = php_http_message_body_etag(body))) {
 -              set_option(options, ZEND_STRL("etag"), IS_STRING, etag, strlen(etag) TSRMLS_CC);
 -              free_etag = 1;
 +      if (!etag && (etag = php_http_message_body_etag(body))) {
 +              set_option(options, ZEND_STRL("etag"), IS_STRING, etag, strlen(etag));
        }
  
 -      if (zetag) {
 -              zval_ptr_dtor(&zetag);
 -      }
 -
 -      if (etag && (header = php_http_env_get_request_header(header_str, header_len, NULL, request TSRMLS_CC))) {
 -              ret = php_http_match(header, etag, PHP_HTTP_MATCH_WORD)  ? PHP_HTTP_CACHE_HIT : PHP_HTTP_CACHE_MISS;
 -      }
 -
 -      if (free_etag) {
 -              efree(etag);
 +      if (etag && (header = php_http_env_get_request_header(header_str, header_len, NULL, request))) {
 +              ret = php_http_match(header, etag, PHP_HTTP_MATCH_WORD) ? PHP_HTTP_CACHE_HIT : PHP_HTTP_CACHE_MISS;
        }
  
 +      PTR_FREE(etag);
        PTR_FREE(header);
 +
        return ret;
  }
  
 -php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *options, const char *header_str, size_t header_len, php_http_message_t *request TSRMLS_DC)
 +php_http_cache_status_t php_http_env_is_response_cached_by_last_modified(zval *options, const char *header_str, size_t header_len, php_http_message_t *request)
  {
        php_http_cache_status_t ret = PHP_HTTP_CACHE_NO;
        char *header;
        time_t ums, lm = 0;
        php_http_message_body_t *body;
 -      zval *zlm;
 +      zval zlm_tmp, *zlm;
  
 -      if (!(body = get_body(options TSRMLS_CC))) {
 +      if (!(body = get_body(options))) {
                return ret;
        }
  
 -      if ((zlm = get_option(options, ZEND_STRL("lastModified") TSRMLS_CC))) {
 -              zval *zlm_copy = php_http_ztyp(IS_LONG, zlm);
 -              zval_ptr_dtor(&zlm);
 -              zlm = zlm_copy;
 +      if ((zlm = get_option(options, ZEND_STRL("lastModified"), &zlm_tmp))) {
 +              lm = zval_get_long(zlm);
 +              zval_ptr_dtor(zlm);
        }
  
 -      if (zlm && Z_LVAL_P(zlm) > 0) {
 -              lm = Z_LVAL_P(zlm);
 -      } else {
 +      if (lm <= 0) {
                lm = php_http_message_body_mtime(body);
 -              set_option(options, ZEND_STRL("lastModified"), IS_LONG, &lm, 0 TSRMLS_CC);
 -      }
 -
 -      if (zlm) {
 -              zval_ptr_dtor(&zlm);
 +              set_option(options, ZEND_STRL("lastModified"), IS_LONG, &lm, 0);
        }
  
 -      if ((header = php_http_env_get_request_header(header_str, header_len, NULL, request TSRMLS_CC))) {
 +      if ((header = php_http_env_get_request_header(header_str, header_len, NULL, request))) {
                ums = php_parse_date(header, NULL);
  
                if (ums > 0 && ums >= lm) {
  
  static zend_bool php_http_env_response_is_cacheable(php_http_env_response_t *r, php_http_message_t *request)
  {
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
 -
        if (r->ops->get_status(r) >= 400) {
                return 0;
        }
  
 -      if (php_http_env_got_request_header(ZEND_STRL("Authorization"), request TSRMLS_CC)) {
 +      if (php_http_env_got_request_header(ZEND_STRL("Authorization"), request)) {
                return 0;
        }
  
 -      if (-1 == php_http_select_str(php_http_env_get_request_method(request TSRMLS_CC), 2, "HEAD", "GET")) {
 +      if (-1 == php_http_select_str(php_http_env_get_request_method(request), 2, "HEAD", "GET")) {
                return 0;
        }
  
        return 1;
  }
  
 -static size_t output(void *context, char *buf, size_t len TSRMLS_DC)
 +static size_t output(void *context, char *buf, size_t len)
  {
        php_http_env_response_t *r = context;
  
        return len;
  }
  
 -#define php_http_env_response_send_done(r) php_http_env_response_send_data((r), NULL, 0)
 -static STATUS php_http_env_response_send_data(php_http_env_response_t *r, const char *buf, size_t len)
 +static ZEND_RESULT_CODE php_http_env_response_send_data(php_http_env_response_t *r, const char *buf, size_t len)
  {
        size_t chunks_sent, chunk = r->throttle.chunk ? r->throttle.chunk : PHP_HTTP_SENDBUF_SIZE;
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
  
        if (r->content.encoder) {
                char *enc_str = NULL;
                if (!enc_str) {
                        return SUCCESS;
                }
 -              chunks_sent = php_http_buffer_chunked_output(&r->buffer, enc_str, enc_len, buf ? chunk : 0, output, r TSRMLS_CC);
 +              chunks_sent = php_http_buffer_chunked_output(&r->buffer, enc_str, enc_len, buf ? chunk : 0, output, r);
                PTR_FREE(enc_str);
        } else {
 -              chunks_sent = php_http_buffer_chunked_output(&r->buffer, buf, len, buf ? chunk : 0, output, r TSRMLS_CC);
 +              chunks_sent = php_http_buffer_chunked_output(&r->buffer, buf, len, buf ? chunk : 0, output, r);
        }
  
        return chunks_sent != (size_t) -1 ? SUCCESS : FAILURE;
  }
  
 -php_http_env_response_t *php_http_env_response_init(php_http_env_response_t *r, zval *options, php_http_env_response_ops_t *ops, void *init_arg TSRMLS_DC)
 +static inline ZEND_RESULT_CODE php_http_env_response_send_done(php_http_env_response_t *r)
 +{
 +      return php_http_env_response_send_data(r, NULL, 0);
 +}
 +
 +php_http_env_response_t *php_http_env_response_init(php_http_env_response_t *r, zval *options, php_http_env_response_ops_t *ops, void *init_arg)
  {
        zend_bool free_r;
  
  
        r->buffer = php_http_buffer_init(NULL);
  
 -      Z_ADDREF_P(options);
 -      r->options = options;
 -
 -      TSRMLS_SET_CTX(r->ts);
 +      ZVAL_COPY(&r->options, options);
  
        if (r->ops->init && (SUCCESS != r->ops->init(r, init_arg))) {
                if (free_r) {
@@@ -328,63 -351,65 +328,63 @@@ void php_http_env_response_free(php_htt
        }
  }
  
 -static STATUS php_http_env_response_send_head(php_http_env_response_t *r, php_http_message_t *request)
 +static ZEND_RESULT_CODE php_http_env_response_send_head(php_http_env_response_t *r, php_http_message_t *request)
  {
 -      STATUS ret = SUCCESS;
 -      zval *zoption, *options = r->options;
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
 +      ZEND_RESULT_CODE ret = SUCCESS;
 +      zval zoption_tmp, *zoption, *options = &r->options;
  
        if (r->done) {
                return ret;
        }
  
 -      if ((zoption = get_option(options, ZEND_STRL("headers") TSRMLS_CC))) {
 +      if ((zoption = get_option(options, ZEND_STRL("headers"), &zoption_tmp))) {
                if (Z_TYPE_P(zoption) == IS_ARRAY) {
 -                      php_http_header_to_callback(Z_ARRVAL_P(zoption), 0, (php_http_pass_format_callback_t) r->ops->set_header, r TSRMLS_CC);
 +                      php_http_header_to_callback(Z_ARRVAL_P(zoption), 0, (php_http_pass_format_callback_t) r->ops->set_header, r);
                }
 -              zval_ptr_dtor(&zoption);
 +              zval_ptr_dtor(zoption);
        }
  
        if (ret != SUCCESS) {
                return ret;
        }
  
 -      if ((zoption = get_option(options, ZEND_STRL("responseCode") TSRMLS_CC))) {
 -              zval *zoption_copy = php_http_ztyp(IS_LONG, zoption);
 +      if ((zoption = get_option(options, ZEND_STRL("responseCode"), &zoption_tmp))) {
 +              zend_long rc = zval_get_long(zoption);
  
 -              zval_ptr_dtor(&zoption);
 -              if (Z_LVAL_P(zoption_copy) > 0) {
 -                      ret = r->ops->set_status(r, Z_LVAL_P(zoption_copy));
 +              zval_ptr_dtor(zoption);
 +              if (rc > 0) {
 +                      ret = r->ops->set_status(r, rc);
                }
 -              zval_ptr_dtor(&zoption_copy);
        }
  
        if (ret != SUCCESS) {
                return ret;
        }
  
 -      if ((zoption = get_option(options, ZEND_STRL("httpVersion") TSRMLS_CC))) {
 +      if ((zoption = get_option(options, ZEND_STRL("httpVersion"), &zoption_tmp))) {
                php_http_version_t v;
 -              zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
 +              zend_string *zs = zval_get_string(zoption);
  
 -              zval_ptr_dtor(&zoption);
 -              if (Z_STRLEN_P(zoption_copy) && php_http_version_parse(&v, Z_STRVAL_P(zoption_copy) TSRMLS_CC)) {
 +              zval_ptr_dtor(zoption);
 +              if (zs->len && php_http_version_parse(&v, zs->val)) {
                        ret = r->ops->set_protocol_version(r, &v);
                        php_http_version_dtor(&v);
                }
 -              zval_ptr_dtor(&zoption_copy);
 +              zend_string_release(zs);
        }
  
        if (ret != SUCCESS) {
                return ret;
        }
  
 -      if ((zoption = get_option(options, ZEND_STRL("cookies") TSRMLS_CC))) {
 +      if ((zoption = get_option(options, ZEND_STRL("cookies"), &zoption_tmp))) {
                if (Z_TYPE_P(zoption) == IS_ARRAY) {
 -                      HashPosition pos;
 -                      zval **zcookie;
 +                      zval *zcookie;
  
 -                      FOREACH_VAL(pos, zoption, zcookie) {
 -                              if (Z_TYPE_PP(zcookie) == IS_OBJECT && instanceof_function(Z_OBJCE_PP(zcookie), php_http_cookie_class_entry TSRMLS_CC)) {
 -                                      php_http_cookie_object_t *obj = zend_object_store_get_object(*zcookie TSRMLS_CC);
 +                      ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(zoption), zcookie)
 +                      {
 +                              if (Z_TYPE_P(zcookie) == IS_OBJECT && instanceof_function(Z_OBJCE_P(zcookie), php_http_cookie_class_entry)) {
 +                                      php_http_cookie_object_t *obj = PHP_HTTP_OBJ(NULL, zcookie);
                                        char *str;
                                        size_t len;
  
                                        efree(str);
                                }
                        }
 +                      ZEND_HASH_FOREACH_END();
                }
 -              zval_ptr_dtor(&zoption);
 +              zval_ptr_dtor(zoption);
        }
  
        if (ret != SUCCESS) {
                return ret;
        }
  
 -      if ((zoption = get_option(options, ZEND_STRL("contentType") TSRMLS_CC))) {
 -              zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
 +      if ((zoption = get_option(options, ZEND_STRL("contentType"), &zoption_tmp))) {
 +              zend_string *zs = zval_get_string(zoption);
  
 -              zval_ptr_dtor(&zoption);
 -              if (Z_STRLEN_P(zoption_copy) && strchr(Z_STRVAL_P(zoption_copy), '/')) {
 -                      if (SUCCESS == (ret = r->ops->set_header(r, "Content-Type: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy)))) {
 -                              r->content.type = estrndup(Z_STRVAL_P(zoption_copy), Z_STRLEN_P(zoption_copy));
 +              zval_ptr_dtor(zoption);
 +              if (zs->len && strchr(zs->val, '/')) {
 +                      if (SUCCESS == (ret = r->ops->set_header(r, "Content-Type: %.*s", zs->len, zs->val))) {
 +                              r->content.type = estrndup(zs->val, zs->len);
                        }
                }
 -              zval_ptr_dtor(&zoption_copy);
 +              zend_string_release(zs);
        }
  
        if (ret != SUCCESS) {
  
        if (r->range.status == PHP_HTTP_RANGE_OK) {
                if (zend_hash_num_elements(&r->range.values) == 1) {
 -                      zval **range, **begin, **end;
 +                      zval *range, *begin, *end;
  
 -                      if (    1 == php_http_array_list(&r->range.values TSRMLS_CC, 1, &range)
 -                              &&      2 == php_http_array_list(Z_ARRVAL_PP(range) TSRMLS_CC, 2, &begin, &end)
 +                      if (    1 == php_http_array_list(&r->range.values, 1, &range)
 +                              &&      2 == php_http_array_list(Z_ARRVAL_P(range), 2, &begin, &end)
                        ) {
                                if (SUCCESS == (ret = r->ops->set_status(r, 206))) {
 -                                      ret = r->ops->set_header(r, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_PP(begin), Z_LVAL_PP(end), r->content.length);
 +                                      ret = r->ops->set_header(r, "Content-Range: bytes %ld-%ld/%zu", Z_LVAL_P(begin), Z_LVAL_P(end), r->content.length);
                                }
                        } else {
                                /* this should never happen */
                                ret = FAILURE;
                        }
                } else {
 -                      php_http_boundary(r->range.boundary, sizeof(r->range.boundary) TSRMLS_CC);
 +                      php_http_boundary(r->range.boundary, sizeof(r->range.boundary));
                        if (SUCCESS == (ret = r->ops->set_status(r, 206))) {
                                ret = r->ops->set_header(r, "Content-Type: multipart/byteranges; boundary=%s", r->range.boundary);
                        }
                }
        } else {
 -              if ((zoption = get_option(options, ZEND_STRL("cacheControl") TSRMLS_CC))) {
 -                      zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
 +              if ((zoption = get_option(options, ZEND_STRL("cacheControl"), &zoption_tmp))) {
 +                      zend_string *zs = zval_get_string(zoption);
  
 -                      zval_ptr_dtor(&zoption);
 -                      if (Z_STRLEN_P(zoption_copy)) {
 -                              ret = r->ops->set_header(r, "Cache-Control: %.*s", Z_STRLEN_P(zoption_copy), Z_STRVAL_P(zoption_copy));
 +                      zval_ptr_dtor(zoption);
 +                      if (zs->len) {
 +                              ret = r->ops->set_header(r, "Cache-Control: %.*s", zs->len, zs->val);
                        }
 -                      zval_ptr_dtor(&zoption_copy);
 +                      zend_string_release(zs);
                }
  
                if (ret != SUCCESS) {
                        return ret;
                }
  
 -              if ((zoption = get_option(options, ZEND_STRL("contentDisposition") TSRMLS_CC))) {
 -                      zval *zoption_copy = php_http_ztyp(IS_ARRAY, zoption);
 -                      php_http_buffer_t buf;
 +              if ((zoption = get_option(options, ZEND_STRL("contentDisposition"), &zoption_tmp))) {
  
 -                      php_http_buffer_init(&buf);
 -                      if (php_http_params_to_string(&buf, Z_ARRVAL_P(zoption_copy), ZEND_STRL(","), ZEND_STRL(";"), ZEND_STRL("="), PHP_HTTP_PARAMS_DEFAULT TSRMLS_CC)) {
 -                              if (buf.used) {
 -                                      ret = r->ops->set_header(r, "Content-Disposition: %.*s", buf.used, buf.data);
 +                      if (Z_TYPE_P(zoption) == IS_ARRAY) {
 +                              php_http_buffer_t buf;
 +
 +                              php_http_buffer_init(&buf);
 +                              if (php_http_params_to_string(&buf, Z_ARRVAL_P(zoption), ZEND_STRL(","), ZEND_STRL(";"), ZEND_STRL("="), PHP_HTTP_PARAMS_DEFAULT)) {
 +                                      if (buf.used) {
 +                                              ret = r->ops->set_header(r, "Content-Disposition: %.*s", buf.used, buf.data);
 +                                      }
                                }
 -                      }
  
 -                      php_http_buffer_dtor(&buf);
 -                      zval_ptr_dtor(&zoption_copy);
 -                      zval_ptr_dtor(&zoption);
 +                              php_http_buffer_dtor(&buf);
 +                      }
 +                      zval_ptr_dtor(zoption);
                }
  
                if (ret != SUCCESS) {
                        return ret;
                }
  
 -              if ((zoption = get_option(options, ZEND_STRL("contentEncoding") TSRMLS_CC))) {
 -                      zval *zoption_copy = php_http_ztyp(IS_LONG, zoption);
 +              if ((zoption = get_option(options, ZEND_STRL("contentEncoding"), &zoption_tmp))) {
 +                      zend_long ce = zval_get_long(zoption);
                        zval zsupported;
                        HashTable *result = NULL;
  
 -                      zval_ptr_dtor(&zoption);
 -                      switch (Z_LVAL_P(zoption_copy)) {
 +                      zval_ptr_dtor(zoption);
 +                      switch (ce) {
                                case PHP_HTTP_CONTENT_ENCODING_GZIP:
 -                                      INIT_PZVAL(&zsupported);
                                        array_init(&zsupported);
 -                                      add_next_index_stringl(&zsupported, ZEND_STRL("none"), 1);
 -                                      add_next_index_stringl(&zsupported, ZEND_STRL("gzip"), 1);
 -                                      add_next_index_stringl(&zsupported, ZEND_STRL("deflate"), 1);
 +                                      add_next_index_stringl(&zsupported, ZEND_STRL("none"));
 +                                      add_next_index_stringl(&zsupported, ZEND_STRL("gzip"));
 +                                      add_next_index_stringl(&zsupported, ZEND_STRL("deflate"));
  
 -                                      if ((result = php_http_negotiate_encoding(Z_ARRVAL(zsupported), request TSRMLS_CC))) {
 -                                              char *key_str = NULL;
 -                                              uint key_len = 0;
 +                                      if ((result = php_http_negotiate_encoding(Z_ARRVAL(zsupported), request))) {
 +                                              zend_string *key_str = NULL;
 +                                              zend_ulong index = 0;
  
                                                zend_hash_internal_pointer_reset(result);
 -                                              if (HASH_KEY_IS_STRING == zend_hash_get_current_key_ex(result, &key_str, &key_len, NULL, 0, NULL)) {
 -                                                      if (!strcmp(key_str, "gzip")) {
 -                                                              if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_GZIP TSRMLS_CC))) {
 +                                              if (HASH_KEY_IS_STRING == zend_hash_get_current_key(result, &key_str, &index)) {
 +                                                      if (zend_string_equals_literal(key_str, "gzip")) {
 +                                                              if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_GZIP))) {
                                                                        ret = FAILURE;
                                                                } else if (SUCCESS == (ret = r->ops->set_header(r, "Content-Encoding: gzip"))) {
 -                                                                      r->content.encoding = estrndup(key_str, key_len - 1);
 +                                                                      r->content.encoding = estrndup(key_str->val, key_str->len);
                                                                }
 -                                                      } else if (!strcmp(key_str, "deflate")) {
 -                                                              if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_ZLIB TSRMLS_CC))) {
 +                                                      } else if (zend_string_equals_literal(key_str, "deflate")) {
 +                                                              if (!(r->content.encoder = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_deflate_ops(), PHP_HTTP_DEFLATE_TYPE_ZLIB))) {
                                                                        ret = FAILURE;
                                                                } else if (SUCCESS == (ret = r->ops->set_header(r, "Content-Encoding: deflate"))) {
 -                                                                      r->content.encoding = estrndup(key_str, key_len - 1);
 +                                                                      r->content.encoding = estrndup(key_str->val, key_str->len);
                                                                }
                                                        } else {
                                                                ret = r->ops->del_header(r, ZEND_STRL("Content-Encoding"));
                                        ret = r->ops->del_header(r, ZEND_STRL("Content-Encoding"));
                                        break;
                        }
 -                      zval_ptr_dtor(&zoption_copy);
                }
  
                if (SUCCESS != ret) {
                }
  
                if (php_http_env_response_is_cacheable(r, request)) {
 -                      switch (php_http_env_is_response_cached_by_etag(options, ZEND_STRL("If-None-Match"), request TSRMLS_CC)) {
 +                      switch (php_http_env_is_response_cached_by_etag(options, ZEND_STRL("If-None-Match"), request)) {
                                case PHP_HTTP_CACHE_MISS:
                                        break;
  
                                case PHP_HTTP_CACHE_NO:
 -                                      if (PHP_HTTP_CACHE_HIT != php_http_env_is_response_cached_by_last_modified(options, ZEND_STRL("If-Modified-Since"), request TSRMLS_CC)) {
 +                                      if (PHP_HTTP_CACHE_HIT != php_http_env_is_response_cached_by_last_modified(options, ZEND_STRL("If-Modified-Since"), request)) {
                                                break;
                                        }
                                        /*  no break */
                                        break;
                        }
  
 -                      if ((zoption = get_option(options, ZEND_STRL("etag") TSRMLS_CC))) {
 -                              zval *zoption_copy = php_http_ztyp(IS_STRING, zoption);
 +                      if ((zoption = get_option(options, ZEND_STRL("etag"), &zoption_tmp))) {
 +                              zend_string *zs = zval_get_string(zoption);
  
 -                              zval_ptr_dtor(&zoption);
 -                              if (*Z_STRVAL_P(zoption_copy) != '"' && strncmp(Z_STRVAL_P(zoption_copy), "W/\"", 3)) {
 -                                      ret = r->ops->set_header(r, "ETag: \"%s\"", Z_STRVAL_P(zoption_copy));
 +                              zval_ptr_dtor(zoption);
 +                              if (*zs->val != '"' && strncmp(zs->val, "W/\"", 3)) {
 +                                      ret = r->ops->set_header(r, "ETag: \"%s\"", zs->val);
                                } else {
 -                                      ret = r->ops->set_header(r, "ETag: %s", Z_STRVAL_P(zoption_copy));
 +                                      ret = r->ops->set_header(r, "ETag: %s", zs->val);
                                }
 -                              zval_ptr_dtor(&zoption_copy);
 +                              zend_string_release(zs);
                        }
 -                      if ((zoption = get_option(options, ZEND_STRL("lastModified") TSRMLS_CC))) {
 -                              zval *zoption_copy = php_http_ztyp(IS_LONG, zoption);
 +                      if ((zoption = get_option(options, ZEND_STRL("lastModified"), &zoption_tmp))) {
 +                              zend_long lm = zval_get_long(zoption);
  
 -                              zval_ptr_dtor(&zoption);
 -                              if (Z_LVAL_P(zoption_copy)) {
 -                                      char *date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), Z_LVAL_P(zoption_copy), 0 TSRMLS_CC);
 +                              zval_ptr_dtor(zoption);
 +                              if (lm) {
 +                                      zend_string *date = php_format_date(ZEND_STRL(PHP_HTTP_DATE_FORMAT), lm, 0);
                                        if (date) {
 -                                              ret = r->ops->set_header(r, "Last-Modified: %s", date);
 -                                              efree(date);
 +                                              ret = r->ops->set_header(r, "Last-Modified: %s", date->val);
 +                                              zend_string_release(date);
                                        }
                                }
 -                              zval_ptr_dtor(&zoption_copy);
                        }
                }
        }
        return ret;
  }
  
 -static STATUS php_http_env_response_send_body(php_http_env_response_t *r)
 +static ZEND_RESULT_CODE php_http_env_response_send_body(php_http_env_response_t *r)
  {
 -      STATUS ret = SUCCESS;
 -      zval *zoption;
 +      ZEND_RESULT_CODE ret = SUCCESS;
 +      zval zoption_tmp, *zoption;
        php_http_message_body_t *body;
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
  
        if (r->done) {
                return ret;
        }
  
 -      if ((body = get_body(r->options TSRMLS_CC))) {
 -              if ((zoption = get_option(r->options, ZEND_STRL("throttleDelay") TSRMLS_CC))) {
 -                      if (Z_TYPE_P(zoption) == IS_DOUBLE) {
 -                              r->throttle.delay =  Z_DVAL_P(zoption);
 -                      }
 -                      zval_ptr_dtor(&zoption);
 +      if ((body = get_body(&r->options))) {
 +              if ((zoption = get_option(&r->options, ZEND_STRL("throttleDelay"), &zoption_tmp))) {
 +                      r->throttle.delay = zval_get_double(zoption);
 +                      zval_ptr_dtor(zoption);
                }
 -              if ((zoption = get_option(r->options, ZEND_STRL("throttleChunk") TSRMLS_CC))) {
 -                      if (Z_TYPE_P(zoption) == IS_LONG) {
 -                              r->throttle.chunk = Z_LVAL_P(zoption);
 -                      }
 -                      zval_ptr_dtor(&zoption);
 +              if ((zoption = get_option(&r->options, ZEND_STRL("throttleChunk"), &zoption_tmp))) {
 +                      r->throttle.chunk = zval_get_long(zoption);
 +                      zval_ptr_dtor(zoption);
                }
  
                if (r->range.status == PHP_HTTP_RANGE_OK) {
                        if (zend_hash_num_elements(&r->range.values) == 1) {
                                /* single range */
 -                              zval **range, **begin, **end;
 +                              zval *range, *begin, *end;
  
 -                              if (    1 == php_http_array_list(&r->range.values TSRMLS_CC, 1, &range)
 -                                      &&      2 == php_http_array_list(Z_ARRVAL_PP(range) TSRMLS_CC, 2, &begin, &end)
 +                              if (    1 == php_http_array_list(&r->range.values, 1, &range)
 +                                      &&      2 == php_http_array_list(Z_ARRVAL_P(range), 2, &begin, &end)
                                ) {
                                        /* send chunk */
 -                                      ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1);
 +                                      ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_P(begin), Z_LVAL_P(end) - Z_LVAL_P(begin) + 1);
                                        if (ret == SUCCESS) {
                                                ret = php_http_env_response_send_done(r);
                                        }
  
                        } else {
                                /* send multipart/byte-ranges message */
 -                              HashPosition pos;
 -                              zval **chunk;
 +                              zval *chunk;
  
 -                              FOREACH_HASH_VAL(pos, &r->range.values, chunk) {
 -                                      zval **begin, **end;
 +                              ZEND_HASH_FOREACH_VAL(&r->range.values, chunk)
 +                              {
 +                                      zval *begin, *end;
  
 -                                      if (2 == php_http_array_list(Z_ARRVAL_PP(chunk) TSRMLS_CC, 2, &begin, &end)) {
 +                                      if (2 == php_http_array_list(Z_ARRVAL_P(chunk), 2, &begin, &end)) {
                                                php_http_buffer_appendf(r->buffer,
                                                                PHP_HTTP_CRLF
                                                                "--%s" PHP_HTTP_CRLF
                                                                /* - */
                                                                r->range.boundary,
                                                                r->content.type ? r->content.type : "application/octet-stream",
 -                                                              Z_LVAL_PP(begin),
 -                                                              Z_LVAL_PP(end),
 +                                                              Z_LVAL_P(begin),
 +                                                              Z_LVAL_P(end),
                                                                r->content.length
                                                );
 -                                              ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_PP(begin), Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1);
 +                                              ret = php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_env_response_send_data, r, Z_LVAL_P(begin), Z_LVAL_P(end) - Z_LVAL_P(begin) + 1);
                                        }
                                }
 +                              ZEND_HASH_FOREACH_END();
  
                                if (ret == SUCCESS) {
                                        php_http_buffer_appendf(r->buffer, PHP_HTTP_CRLF "--%s--", r->range.boundary);
        return ret;
  }
  
 -STATUS php_http_env_response_send(php_http_env_response_t *r)
 +ZEND_RESULT_CODE php_http_env_response_send(php_http_env_response_t *r)
  {
        php_http_message_t *request;
        php_http_message_body_t *body;
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
  
 -      request = get_request(r->options TSRMLS_CC);
 +      request = get_request(&r->options);
  
        /* check for ranges */
 -      if ((body = get_body(r->options TSRMLS_CC))) {
 +      if ((body = get_body(&r->options))) {
                r->content.length = php_http_message_body_size(body);
  
                if (SUCCESS != r->ops->set_header(r, "Accept-Ranges: bytes")) {
                        return FAILURE;
                } else {
 -                      zend_hash_init(&r->range.values, 0, NULL, ZVAL_PTR_DTOR, 0);
 -                      r->range.status = php_http_env_get_request_ranges(&r->range.values, r->content.length, request TSRMLS_CC);
 +                      ZEND_INIT_SYMTABLE_EX(&r->range.values, 0, 0);
 +                      r->range.status = php_http_env_get_request_ranges(&r->range.values, r->content.length, request);
  
                        switch (r->range.status) {
                                case PHP_HTTP_RANGE_NO:
                                        break;
  
                                case PHP_HTTP_RANGE_ERR:
 -                                      if (php_http_env_got_request_header(ZEND_STRL("If-Range"), request TSRMLS_CC)) {
 +                                      if (php_http_env_got_request_header(ZEND_STRL("If-Range"), request)) {
                                                r->range.status = PHP_HTTP_RANGE_NO;
                                                zend_hash_destroy(&r->range.values);
                                        } else {
                                        break;
  
                                case PHP_HTTP_RANGE_OK:
 -                                      if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Range"), request TSRMLS_CC)
 -                                      ||      PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Range"), request TSRMLS_CC)
 +                                      if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(&r->options, ZEND_STRL("If-Range"), request)
 +                                      ||      PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(&r->options, ZEND_STRL("If-Range"), request)
                                        ) {
                                                r->range.status = PHP_HTTP_RANGE_NO;
                                                zend_hash_destroy(&r->range.values);
                                                break;
                                        }
 -                                      if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(r->options, ZEND_STRL("If-Match"), request TSRMLS_CC)
 -                                      ||      PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("If-Unmodified-Since"), request TSRMLS_CC)
 -                                      ||      PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(r->options, ZEND_STRL("Unless-Modified-Since"), request TSRMLS_CC)
 +                                      if (PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_etag(&r->options, ZEND_STRL("If-Match"), request)
 +                                      ||      PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(&r->options, ZEND_STRL("If-Unmodified-Since"), request)
 +                                      ||      PHP_HTTP_CACHE_MISS == php_http_env_is_response_cached_by_last_modified(&r->options, ZEND_STRL("Unless-Modified-Since"), request)
                                        ) {
                                                r->done = 1;
                                                zend_hash_destroy(&r->range.values);
        }
  
        if (SUCCESS != php_http_env_response_send_head(r, request)) {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to send response headers");
 +              php_error_docref(NULL, E_WARNING, "Failed to send response headers");
                return FAILURE;
        }
  
        if (SUCCESS != php_http_env_response_send_body(r)) {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to send response body");
 +              php_error_docref(NULL, E_WARNING, "Failed to send response body");
                return FAILURE;
        }
  
        if (SUCCESS != r->ops->finish(r)) {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to finish response");
 +              php_error_docref(NULL, E_WARNING, "Failed to finish response");
                return FAILURE;
        }
  
  
  static long php_http_env_response_sapi_get_status(php_http_env_response_t *r)
  {
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
 -
 -      return php_http_env_get_response_code(TSRMLS_C);
 +      return php_http_env_get_response_code();
  }
 -static STATUS php_http_env_response_sapi_set_status(php_http_env_response_t *r, long http_code)
 +static ZEND_RESULT_CODE php_http_env_response_sapi_set_status(php_http_env_response_t *r, long http_code)
  {
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
 -
 -      return php_http_env_set_response_code(http_code TSRMLS_CC);
 +      return php_http_env_set_response_code(http_code);
  }
 -static STATUS php_http_env_response_sapi_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v)
 +static ZEND_RESULT_CODE php_http_env_response_sapi_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v)
  {
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
  
 -      return php_http_env_set_response_protocol_version(v TSRMLS_CC);
 +      return php_http_env_set_response_protocol_version(v);
  }
 -static STATUS php_http_env_response_sapi_set_header(php_http_env_response_t *r, const char *fmt, ...)
 +static ZEND_RESULT_CODE php_http_env_response_sapi_set_header(php_http_env_response_t *r, const char *fmt, ...)
  {
 -      STATUS ret;
 +      ZEND_RESULT_CODE ret;
        va_list args;
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
  
        va_start(args, fmt);
 -      ret = php_http_env_set_response_header_va(0, 1, fmt, args TSRMLS_CC);
 +      ret = php_http_env_set_response_header_va(0, 1, fmt, args);
        va_end(args);
  
        return ret;
  }
 -static STATUS php_http_env_response_sapi_add_header(php_http_env_response_t *r, const char *fmt, ...)
 +static ZEND_RESULT_CODE php_http_env_response_sapi_add_header(php_http_env_response_t *r, const char *fmt, ...)
  {
 -      STATUS ret;
 +      ZEND_RESULT_CODE ret;
        va_list args;
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
  
        va_start(args, fmt);
 -      ret = php_http_env_set_response_header_va(0, 0, fmt, args TSRMLS_CC);
 +      ret = php_http_env_set_response_header_va(0, 0, fmt, args);
        va_end(args);
  
        return ret;
  }
 -static STATUS php_http_env_response_sapi_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len)
 +static ZEND_RESULT_CODE php_http_env_response_sapi_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len)
  {
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
 -
 -      return php_http_env_set_response_header_value(0, header_str, header_len, NULL, 1 TSRMLS_CC);
 +      return php_http_env_set_response_header_value(0, header_str, header_len, NULL, 1);
  }
 -static STATUS php_http_env_response_sapi_write(php_http_env_response_t *r, const char *data_str, size_t data_len)
 +static ZEND_RESULT_CODE php_http_env_response_sapi_write(php_http_env_response_t *r, const char *data_str, size_t data_len)
  {
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
 -
        if (0 < PHPWRITE(data_str, data_len)) {
                return SUCCESS;
        }
        return FAILURE;
  }
 -static STATUS php_http_env_response_sapi_flush(php_http_env_response_t *r)
 +static ZEND_RESULT_CODE php_http_env_response_sapi_flush(php_http_env_response_t *r)
  {
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
 -
 -#if PHP_VERSION_ID >= 50400
 -      if (php_output_get_level(TSRMLS_C)) {
 -              php_output_flush_all(TSRMLS_C);
 +      if (php_output_get_level()) {
 +              php_output_flush_all();
        }
 -      if (!(php_output_get_status(TSRMLS_C) & PHP_OUTPUT_IMPLICITFLUSH)) {
 -              sapi_flush(TSRMLS_C);
 +      if (!(php_output_get_status() & PHP_OUTPUT_IMPLICITFLUSH)) {
 +              sapi_flush();
        }
 -#else
 -      php_end_ob_buffer(1, 1 TSRMLS_CC);
 -      sapi_flush(TSRMLS_C);
 -#endif
  
        return SUCCESS;
  }
 -static STATUS php_http_env_response_sapi_finish(php_http_env_response_t *r)
 +static ZEND_RESULT_CODE php_http_env_response_sapi_finish(php_http_env_response_t *r)
  {
        return SUCCESS;
  }
@@@ -834,6 -883,7 +834,7 @@@ typedef struct php_http_env_response_st
        long status_code;
  
        php_stream *stream;
+       php_stream_filter *chunked_filter;
        php_http_message_t *request;
  
        unsigned started:1;
        unsigned chunked:1;
  } php_http_env_response_stream_ctx_t;
  
 -static STATUS php_http_env_response_stream_init(php_http_env_response_t *r, void *init_arg)
 +static ZEND_RESULT_CODE php_http_env_response_stream_init(php_http_env_response_t *r, void *init_arg)
  {
        php_http_env_response_stream_ctx_t *ctx;
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
+       size_t buffer_size = 0x1000;
  
        ctx = ecalloc(1, sizeof(*ctx));
  
        ctx->stream = init_arg;
 -      if (!ctx->stream || SUCCESS != zend_list_addref(ctx->stream->rsrc_id)) {
 -              efree(ctx);
 -              return FAILURE;
 -      }
 +      ++GC_REFCOUNT(ctx->stream->res);
 +      ZEND_INIT_SYMTABLE(&ctx->header);
 +      php_http_version_init(&ctx->version, 1, 1);
+       php_stream_set_option(ctx->stream, PHP_STREAM_OPTION_WRITE_BUFFER, PHP_STREAM_BUFFER_FULL, &buffer_size);
 -      zend_hash_init(&ctx->header, 0, NULL, ZVAL_PTR_DTOR, 0);
 -      php_http_version_init(&ctx->version, 1, 1 TSRMLS_CC);
        ctx->status_code = 200;
        ctx->chunked = 1;
 -      ctx->request = get_request(r->options TSRMLS_CC);
 +      ctx->request = get_request(&r->options);
  
        /* there are some limitations regarding TE:chunked, see https://tools.ietf.org/html/rfc7230#section-3.3.1 */
        if (ctx->request && ctx->request->http.version.major == 1 && ctx->request->http.version.minor == 0) {
  static void php_http_env_response_stream_dtor(php_http_env_response_t *r)
  {
        php_http_env_response_stream_ctx_t *ctx = r->ctx;
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
  
 -              php_stream_filter_free(ctx->chunked_filter TSRMLS_CC);
+       if (ctx->chunked_filter) {
++              ctx->chunked_filter = php_stream_filter_remove(ctx->chunked_filter, 1);
+       }
        zend_hash_destroy(&ctx->header);
 -      zend_list_delete(ctx->stream->rsrc_id);
 +      zend_list_delete(ctx->stream->res);
        efree(ctx);
        r->ctx = NULL;
  }
- static void php_http_env_response_stream_header(php_http_env_response_stream_ctx_t *ctx, HashTable *header)
 -static void php_http_env_response_stream_header(php_http_env_response_stream_ctx_t *ctx, HashTable *header, php_http_buffer_t *buf TSRMLS_DC)
++static void php_http_env_response_stream_header(php_http_env_response_stream_ctx_t *ctx, HashTable *header, php_http_buffer_t *buf)
  {
 -      HashPosition pos;
 -      zval **val;
 +      zval *val;
  
 -      FOREACH_HASH_VAL(pos, header, val) {
 -              if (Z_TYPE_PP(val) == IS_ARRAY) {
 -                      php_http_env_response_stream_header(ctx, Z_ARRVAL_PP(val), buf TSRMLS_CC);
 +      ZEND_HASH_FOREACH_VAL(header, val)
 +      {
 +              if (Z_TYPE_P(val) == IS_ARRAY) {
-                       php_http_env_response_stream_header(ctx, Z_ARRVAL_P(val));
++                      php_http_env_response_stream_header(ctx, Z_ARRVAL_P(val), buf);
                } else {
 -                      zval *tmp = php_http_ztyp(IS_STRING, *val);
 +                      zend_string *zs = zval_get_string(val);
  
                        if (ctx->chunked) {
                                /* disable chunked transfer encoding if we've got an explicit content-length */
 -                              if (!strncasecmp(Z_STRVAL_P(tmp), "Content-Length:", lenof("Content-Length:"))) {
 +                              if (!strncasecmp(zs->val, "Content-Length:", lenof("Content-Length:"))) {
                                        ctx->chunked = 0;
                                }
                        }
-                       php_stream_write(ctx->stream, zs->val, zs->len);
-                       php_stream_write_string(ctx->stream, PHP_HTTP_CRLF);
 -                      php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
++                      php_http_buffer_append(buf, zs->val, zs->len);
+                       php_http_buffer_appends(buf, PHP_HTTP_CRLF);
 -                      zval_ptr_dtor(&tmp);
 +                      zend_string_release(zs);
                }
        }
 +      ZEND_HASH_FOREACH_END();
  }
 -static STATUS php_http_env_response_stream_start(php_http_env_response_stream_ctx_t *ctx TSRMLS_DC)
 +static ZEND_RESULT_CODE php_http_env_response_stream_start(php_http_env_response_stream_ctx_t *ctx)
  {
+       php_http_buffer_t header_buf;
        if (ctx->started || ctx->finished) {
                return FAILURE;
        }
  
-       php_stream_printf(ctx->stream, "HTTP/%u.%u %ld %s" PHP_HTTP_CRLF, ctx->version.major, ctx->version.minor, ctx->status_code, php_http_env_get_response_status_for_code(ctx->status_code));
+       php_http_buffer_init(&header_buf);
+       php_http_buffer_appendf(&header_buf, "HTTP/%u.%u %ld %s" PHP_HTTP_CRLF, ctx->version.major, ctx->version.minor, ctx->status_code, php_http_env_get_response_status_for_code(ctx->status_code));
  
        /* there are some limitations regarding TE:chunked, see https://tools.ietf.org/html/rfc7230#section-3.3.1 */
        if (ctx->version.major == 1 && ctx->version.minor == 0) {
                ctx->chunked = 0;
        }
  
-       php_http_env_response_stream_header(ctx, &ctx->header);
 -      php_http_env_response_stream_header(ctx, &ctx->header, &header_buf TSRMLS_CC);
++      php_http_env_response_stream_header(ctx, &ctx->header, &header_buf);
  
        /* enable chunked transfer encoding */
        if (ctx->chunked) {
-               php_stream_write_string(ctx->stream, "Transfer-Encoding: chunked" PHP_HTTP_CRLF);
+               php_http_buffer_appends(&header_buf, "Transfer-Encoding: chunked" PHP_HTTP_CRLF);
        }
-       php_stream_write_string(ctx->stream, PHP_HTTP_CRLF);
 +
+       php_http_buffer_appends(&header_buf, PHP_HTTP_CRLF);
  
-       ctx->started = 1;
+       if (header_buf.used == php_stream_write(ctx->stream, header_buf.data, header_buf.used)) {
+               ctx->started = 1;
+       }
+       php_http_buffer_dtor(&header_buf);
+       php_stream_flush(ctx->stream);
  
-       return SUCCESS;
+       if (ctx->chunked) {
 -              ctx->chunked_filter = php_stream_filter_create("http.chunked_encode", NULL, 0 TSRMLS_CC);
++              ctx->chunked_filter = php_stream_filter_create("http.chunked_encode", NULL, 0);
+               php_stream_filter_append(&ctx->stream->writefilters, ctx->chunked_filter);
+       }
+       return ctx->started ? SUCCESS : FAILURE;
  }
  static long php_http_env_response_stream_get_status(php_http_env_response_t *r)
  {
  
        return ctx->status_code;
  }
 -static STATUS php_http_env_response_stream_set_status(php_http_env_response_t *r, long http_code)
 +static ZEND_RESULT_CODE php_http_env_response_stream_set_status(php_http_env_response_t *r, long http_code)
  {
        php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
  
  
        return SUCCESS;
  }
 -static STATUS php_http_env_response_stream_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v)
 +static ZEND_RESULT_CODE php_http_env_response_stream_set_protocol_version(php_http_env_response_t *r, php_http_version_t *v)
  {
        php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
  
  
        return SUCCESS;
  }
 -static STATUS php_http_env_response_stream_set_header_ex(php_http_env_response_t *r, zend_bool replace, const char *fmt, va_list argv)
 +static ZEND_RESULT_CODE php_http_env_response_stream_set_header_ex(php_http_env_response_t *r, zend_bool replace, const char *fmt, va_list argv)
  {
        php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
        char *header_end, *header_str = NULL;
        size_t header_len = 0;
 -      zval *zheader, **zheader_ptr;
 +      zval zheader, *zheader_ptr;
 +      zend_string *header_key;
 +      ZEND_RESULT_CODE rv;
  
        if (stream_ctx->started || stream_ctx->finished) {
                return FAILURE;
                return FAILURE;
        }
  
 -      *header_end = '\0';
 +      header_key = zend_string_init(header_str, header_end - header_str, 0);
  
 -      if (!replace && (SUCCESS == zend_hash_find(&stream_ctx->header, header_str, header_end - header_str + 1, (void *) &zheader_ptr))) {
 -              convert_to_array(*zheader_ptr);
 -              *header_end = ':';
 -              return add_next_index_stringl(*zheader_ptr, header_str, header_len, 0);
 +      if (!replace && (zheader_ptr = zend_hash_find(&stream_ctx->header, header_key))) {
 +              convert_to_array(zheader_ptr);
 +              rv = add_next_index_str(zheader_ptr, php_http_cs2zs(header_str, header_len));
        } else {
 -              MAKE_STD_ZVAL(zheader);
 -              ZVAL_STRINGL(zheader, header_str, header_len, 0);
 -
 -              if (SUCCESS != zend_hash_update(&stream_ctx->header, header_str, header_end - header_str + 1, (void *) &zheader, sizeof(zval *), NULL)) {
 -                      zval_ptr_dtor(&zheader);
 -                      return FAILURE;
 -              }
 +              ZVAL_STR(&zheader, php_http_cs2zs(header_str, header_len));
  
 -              *header_end = ':';
 -              return SUCCESS;
 +              rv = zend_hash_update(&stream_ctx->header, header_key, &zheader)
 +                      ? SUCCESS : FAILURE;
        }
 +
 +      zend_string_release(header_key);
 +
 +      return rv;
  }
 -static STATUS php_http_env_response_stream_set_header(php_http_env_response_t *r, const char *fmt, ...)
 +static ZEND_RESULT_CODE php_http_env_response_stream_set_header(php_http_env_response_t *r, const char *fmt, ...)
  {
 -      STATUS ret;
 +      ZEND_RESULT_CODE ret;
        va_list argv;
  
        va_start(argv, fmt);
  
        return ret;
  }
 -static STATUS php_http_env_response_stream_add_header(php_http_env_response_t *r, const char *fmt, ...)
 +static ZEND_RESULT_CODE php_http_env_response_stream_add_header(php_http_env_response_t *r, const char *fmt, ...)
  {
 -      STATUS ret;
 +      ZEND_RESULT_CODE ret;
        va_list argv;
  
        va_start(argv, fmt);
  
        return ret;
  }
 -static STATUS php_http_env_response_stream_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len)
 +static ZEND_RESULT_CODE php_http_env_response_stream_del_header(php_http_env_response_t *r, const char *header_str, size_t header_len)
  {
        php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
  
                return FAILURE;
        }
  
 -      zend_hash_del(&stream_ctx->header, header_str, header_len + 1);
 +      zend_hash_str_del(&stream_ctx->header, header_str, header_len);
        return SUCCESS;
  }
 -static STATUS php_http_env_response_stream_write(php_http_env_response_t *r, const char *data_str, size_t data_len)
 +static ZEND_RESULT_CODE php_http_env_response_stream_write(php_http_env_response_t *r, const char *data_str, size_t data_len)
  {
        php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
  
        if (stream_ctx->finished) {
                return FAILURE;
        }
        if (!stream_ctx->started) {
 -              if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) {
 +              if (SUCCESS != php_http_env_response_stream_start(stream_ctx)) {
                        return FAILURE;
                }
        }
  
-       if (stream_ctx->chunked && 0 == php_stream_printf(stream_ctx->stream TSRMLS_CC, "%lx" PHP_HTTP_CRLF, (unsigned long) data_len)) {
-               return FAILURE;
-       }
        if (data_len != php_stream_write(stream_ctx->stream, data_str, data_len)) {
                return FAILURE;
        }
  
-       if (stream_ctx->chunked && 2 != php_stream_write_string(stream_ctx->stream, PHP_HTTP_CRLF)) {
-               return FAILURE;
-       }
        return SUCCESS;
  }
 -static STATUS php_http_env_response_stream_flush(php_http_env_response_t *r)
 +static ZEND_RESULT_CODE php_http_env_response_stream_flush(php_http_env_response_t *r)
  {
        php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
  
        if (stream_ctx->finished) {
                return FAILURE;
        }
        if (!stream_ctx->started) {
 -              if (SUCCESS != php_http_env_response_stream_start(stream_ctx TSRMLS_CC)) {
 +              if (SUCCESS != php_http_env_response_stream_start(stream_ctx)) {
                        return FAILURE;
                }
        }
  
        return php_stream_flush(stream_ctx->stream);
  }
 -static STATUS php_http_env_response_stream_finish(php_http_env_response_t *r)
 +static ZEND_RESULT_CODE php_http_env_response_stream_finish(php_http_env_response_t *r)
  {
-       php_http_env_response_stream_ctx_t *stream_ctx = r->ctx;
+       php_http_env_response_stream_ctx_t *ctx = r->ctx;
 -      TSRMLS_FETCH_FROM_CTX(r->ts);
  
-       if (stream_ctx->finished) {
+       if (ctx->finished) {
                return FAILURE;
        }
-       if (!stream_ctx->started) {
-               if (SUCCESS != php_http_env_response_stream_start(stream_ctx)) {
+       if (!ctx->started) {
 -              if (SUCCESS != php_http_env_response_stream_start(ctx TSRMLS_CC)) {
++              if (SUCCESS != php_http_env_response_stream_start(ctx)) {
                        return FAILURE;
                }
        }
  
-       if (stream_ctx->chunked && 5 != php_stream_write_string(stream_ctx->stream, "0" PHP_HTTP_CRLF PHP_HTTP_CRLF)) {
-               return FAILURE;
+       php_stream_flush(ctx->stream);
+       if (ctx->chunked && ctx->chunked_filter) {
+               php_stream_filter_flush(ctx->chunked_filter, 1);
+               ctx->chunked_filter = php_stream_filter_remove(ctx->chunked_filter, 1 TSRMLS_CC);
        }
  
-       stream_ctx->finished = 1;
+       ctx->finished = 1;
  
        return SUCCESS;
  }
@@@ -1112,7 -1180,7 +1124,7 @@@ php_http_env_response_ops_t *php_http_e
  #define PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj) \
        do { \
                if (!obj->message) { \
 -                      obj->message = php_http_message_init_env(NULL, PHP_HTTP_RESPONSE TSRMLS_CC); \
 +                      obj->message = php_http_message_init_env(NULL, PHP_HTTP_RESPONSE); \
                } \
        } while (0)
  
@@@ -1124,9 -1192,9 +1136,9 @@@ static PHP_METHOD(HttpEnvResponse, __co
  
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
  
 -      php_http_expect(obj->message = php_http_message_init_env(obj->message, PHP_HTTP_RESPONSE TSRMLS_CC), unexpected_val, return);
 +      php_http_expect(obj->message = php_http_message_init_env(obj->message, PHP_HTTP_RESPONSE), unexpected_val, return);
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse___invoke, 0, 0, 1)
@@@ -1136,17 -1204,21 +1148,17 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpEnvResponse, __invoke)
  {
        char *ob_str;
 -      int ob_len;
 -      long ob_flags = 0;
 +      size_t ob_len;
 +      zend_long ob_flags = 0;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|l", &ob_str, &ob_len, &ob_flags)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s|l", &ob_str, &ob_len, &ob_flags)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_ENV_RESPONSE_OBJECT_INIT(obj);
  
                php_http_message_object_init_body_object(obj);
                php_http_message_body_append(obj->message->body, ob_str, ob_len);
 -#if PHP_VERSION_ID >= 50400
                RETURN_TRUE;
 -#else
 -              RETURN_EMPTY_STRING();
 -#endif
        }
  }
  
@@@ -1157,10 -1229,10 +1169,10 @@@ static PHP_METHOD(HttpEnvResponse, setE
  {
        zval *env_req = NULL;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|O", &env_req, php_http_message_class_entry), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|O", &env_req, php_http_message_class_entry), invalid_arg, return);
  
 -      set_option(getThis(), ZEND_STRL("request"), IS_OBJECT, env_req, 0 TSRMLS_CC);
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      set_option(getThis(), ZEND_STRL("request"), IS_OBJECT, env_req, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentType, 0, 0, 1)
@@@ -1169,12 -1241,12 +1181,12 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpEnvResponse, setContentType)
  {
        char *ct_str = NULL;
 -      int ct_len = 0;
 +      size_t ct_len = 0;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &ct_str, &ct_len), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &ct_str, &ct_len), invalid_arg, return);
  
 -      set_option(getThis(), ZEND_STRL("contentType"), IS_STRING, ct_str, ct_len TSRMLS_CC);
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      set_option(getThis(), ZEND_STRL("contentType"), IS_STRING, ct_str, ct_len);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentDisposition, 0, 0, 1)
@@@ -1184,10 -1256,10 +1196,10 @@@ static PHP_METHOD(HttpEnvResponse, setC
  {
        zval *zdisposition;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a", &zdisposition), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "a", &zdisposition), invalid_arg, return);
  
 -      zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), zdisposition TSRMLS_CC);
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      zend_update_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("contentDisposition"), zdisposition);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setContentEncoding, 0, 0, 1)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpEnvResponse, setContentEncoding)
  {
 -      long ce;
 +      zend_long ce;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &ce), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &ce), invalid_arg, return);
  
 -      set_option(getThis(), ZEND_STRL("contentEncoding"), IS_LONG, &ce, 0 TSRMLS_CC);
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      set_option(getThis(), ZEND_STRL("contentEncoding"), IS_LONG, &ce, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCacheControl, 0, 0, 1)
@@@ -1209,12 -1281,12 +1221,12 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpEnvResponse, setCacheControl)
  {
        char *cc_str = NULL;
 -      int cc_len = 0;
 +      size_t cc_len = 0;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &cc_str, &cc_len), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &cc_str, &cc_len), invalid_arg, return);
  
 -      set_option(getThis(), ZEND_STRL("cacheControl"), IS_STRING, cc_str, cc_len TSRMLS_CC);
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      set_option(getThis(), ZEND_STRL("cacheControl"), IS_STRING, cc_str, cc_len);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setLastModified, 0, 0, 1)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpEnvResponse, setLastModified)
  {
 -      long last_modified;
 +      zend_long last_modified;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &last_modified), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &last_modified), invalid_arg, return);
  
 -      set_option(getThis(), ZEND_STRL("lastModified"), IS_LONG, &last_modified, 0 TSRMLS_CC);
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      set_option(getThis(), ZEND_STRL("lastModified"), IS_LONG, &last_modified, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByLastModified, 0, 0, 0)
@@@ -1236,15 -1308,15 +1248,15 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpEnvResponse, isCachedByLastModified)
  {
        char *header_name_str = NULL;
 -      int header_name_len = 0;
 +      size_t header_name_len = 0;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s!", &header_name_str, &header_name_len)) {
                if (!header_name_str || !header_name_len) {
                        header_name_str = "If-Modified-Since";
                        header_name_len = lenof("If-Modified-Since");
                }
  
 -              RETURN_LONG(php_http_env_is_response_cached_by_last_modified(getThis(), header_name_str, header_name_len, get_request(getThis() TSRMLS_CC) TSRMLS_CC));
 +              RETURN_LONG(php_http_env_is_response_cached_by_last_modified(getThis(), header_name_str, header_name_len, get_request(getThis())));
        }
  }
  
@@@ -1254,12 -1326,12 +1266,12 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpEnvResponse, setEtag)
  {
        char *etag_str = NULL;
 -      int etag_len = 0;
 +      size_t etag_len = 0;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s!", &etag_str, &etag_len), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s!", &etag_str, &etag_len), invalid_arg, return);
  
 -      set_option(getThis(), ZEND_STRL("etag"), IS_STRING, etag_str, etag_len TSRMLS_CC);
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      set_option(getThis(), ZEND_STRL("etag"), IS_STRING, etag_str, etag_len);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_isCachedByEtag, 0, 0, 0)
@@@ -1268,14 -1340,14 +1280,14 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpEnvResponse, isCachedByEtag)
  {
        char *header_name_str = NULL;
 -      int header_name_len = 0;
 +      size_t header_name_len = 0;
  
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!", &header_name_str, &header_name_len)) {
                if (!header_name_str || !header_name_len) {
                        header_name_str = "If-None-Match";
                        header_name_len = lenof("If-None-Match");
                }
 -              RETURN_LONG(php_http_env_is_response_cached_by_etag(getThis(), header_name_str, header_name_len, get_request(getThis() TSRMLS_CC) TSRMLS_CC));
 +              RETURN_LONG(php_http_env_is_response_cached_by_etag(getThis(), header_name_str, header_name_len, get_request(getThis())));
        }
  }
  
@@@ -1285,14 -1357,14 +1297,14 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvRespon
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpEnvResponse, setThrottleRate)
  {
 -      long chunk_size;
 +      zend_long chunk_size;
        double delay = 1;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|d", &chunk_size, &delay), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "l|d", &chunk_size, &delay), invalid_arg, return);
  
 -      set_option(getThis(), ZEND_STRL("throttleDelay"), IS_DOUBLE, &delay, 0 TSRMLS_CC);
 -      set_option(getThis(), ZEND_STRL("throttleChunk"), IS_LONG, &chunk_size, 0 TSRMLS_CC);
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      set_option(getThis(), ZEND_STRL("throttleDelay"), IS_DOUBLE, &delay, 0);
 +      set_option(getThis(), ZEND_STRL("throttleChunk"), IS_LONG, &chunk_size, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_setCookie, 0, 0, 1)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpEnvResponse, setCookie)
  {
 -      zval *zcookie_new;
 +      zval *zcookie_new, tmp;
 +      zend_string *zs;
        zend_error_handling zeh;
        php_http_cookie_list_t *list = NULL;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zcookie_new), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zcookie_new), invalid_arg, return);
  
 -      zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh TSRMLS_CC);
 +      zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh);
        switch (Z_TYPE_P(zcookie_new)) {
        case IS_OBJECT:
 -              if (instanceof_function(Z_OBJCE_P(zcookie_new), php_http_cookie_class_entry TSRMLS_CC)) {
 +              if (instanceof_function(Z_OBJCE_P(zcookie_new), php_http_cookie_class_entry)) {
                        Z_ADDREF_P(zcookie_new);
                        break;
                }
                /* no break */
        case IS_ARRAY:
 -              list = php_http_cookie_list_from_struct(NULL, zcookie_new TSRMLS_CC);
 -              MAKE_STD_ZVAL(zcookie_new);
 -              ZVAL_OBJVAL(zcookie_new, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0);
 +              list = php_http_cookie_list_from_struct(NULL, zcookie_new);
 +              zcookie_new = &tmp;
 +              ZVAL_OBJECT(zcookie_new, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo, 1);
                break;
  
        default:
 -              zcookie_new = php_http_ztyp(IS_STRING, zcookie_new);
 -              list = php_http_cookie_list_parse(NULL, Z_STRVAL_P(zcookie_new), Z_STRLEN_P(zcookie_new), 0, NULL TSRMLS_CC);
 -              zval_ptr_dtor(&zcookie_new);
 -              MAKE_STD_ZVAL(zcookie_new);
 -              ZVAL_OBJVAL(zcookie_new, php_http_cookie_object_new_ex(php_http_cookie_class_entry, list, NULL TSRMLS_CC), 0);
 +              zs = zval_get_string(zcookie_new);
 +              list = php_http_cookie_list_parse(NULL, zs->val, zs->len, 0, NULL);
 +              zend_string_release(zs);
 +              zcookie_new = &tmp;
 +              ZVAL_OBJECT(zcookie_new, &php_http_cookie_object_new_ex(php_http_cookie_class_entry, list)->zo, 1);
        }
 -      zend_restore_error_handling(&zeh TSRMLS_CC);
 +      zend_restore_error_handling(&zeh);
  
 -      set_cookie(getThis(), zcookie_new TSRMLS_CC);
 -      zval_ptr_dtor(&zcookie_new);
 +      set_cookie(getThis(), zcookie_new);
 +      zval_ptr_dtor(zcookie_new);
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpEnvResponse_send, 0, 0, 0)
@@@ -1344,16 -1415,20 +1356,16 @@@ static PHP_METHOD(HttpEnvResponse, send
        zval *zstream = NULL;
        php_stream *s = NULL;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r", &zstream)) {
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|r", &zstream)) {
                /* first flush the output layer to avoid conflicting headers and output;
                 * also, ob_start($thisEnvResponse) might have been called */
 -#if PHP_VERSION_ID >= 50400
 -              php_output_end_all(TSRMLS_C);
 -#else
 -              php_end_ob_buffers(1 TSRMLS_CC);
 -#endif
 +              php_output_end_all();
  
                if (zstream) {
                        php_http_env_response_t *r;
  
 -                      php_stream_from_zval(s, &zstream);
 -                      r = php_http_env_response_init(NULL, getThis(), php_http_env_response_get_stream_ops(), s TSRMLS_CC);
 +                      php_stream_from_zval(s, zstream);
 +                      r = php_http_env_response_init(NULL, getThis(), php_http_env_response_get_stream_ops(), s);
                        if (!r) {
                                RETURN_FALSE;
                        }
                } else {
                        php_http_env_response_t r;
  
 -                      if (!php_http_env_response_init(&r, getThis(), NULL, NULL TSRMLS_CC)) {
 +                      if (!php_http_env_response_init(&r, getThis(), NULL, NULL)) {
                                RETURN_FALSE;
                        }
  
@@@ -1398,25 -1473,25 +1410,25 @@@ PHP_MINIT_FUNCTION(http_env_response
        zend_class_entry ce = {0};
  
        INIT_NS_CLASS_ENTRY(ce, "http\\Env", "Response", php_http_env_response_methods);
 -      php_http_env_response_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry, NULL TSRMLS_CC);
 -
 -      zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_ENCODING_NONE"), PHP_HTTP_CONTENT_ENCODING_NONE TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_ENCODING_GZIP"), PHP_HTTP_CONTENT_ENCODING_GZIP TSRMLS_CC);
 -
 -      zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_NO"), PHP_HTTP_CACHE_NO TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_HIT"), PHP_HTTP_CACHE_HIT TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_MISS"), PHP_HTTP_CACHE_MISS TSRMLS_CC);
 -
 -      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("request"), ZEND_ACC_PROTECTED TSRMLS_CC);
 -      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("cookies"), ZEND_ACC_PROTECTED TSRMLS_CC);
 -      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentType"), ZEND_ACC_PROTECTED TSRMLS_CC);
 -      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentDisposition"), ZEND_ACC_PROTECTED TSRMLS_CC);
 -      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentEncoding"), ZEND_ACC_PROTECTED TSRMLS_CC);
 -      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("cacheControl"), ZEND_ACC_PROTECTED TSRMLS_CC);
 -      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("etag"), ZEND_ACC_PROTECTED TSRMLS_CC);
 -      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("lastModified"), ZEND_ACC_PROTECTED TSRMLS_CC);
 -      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleDelay"), ZEND_ACC_PROTECTED TSRMLS_CC);
 -      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleChunk"), ZEND_ACC_PROTECTED TSRMLS_CC);
 +      php_http_env_response_class_entry = zend_register_internal_class_ex(&ce, php_http_message_class_entry);
 +
 +      zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_ENCODING_NONE"), PHP_HTTP_CONTENT_ENCODING_NONE);
 +      zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CONTENT_ENCODING_GZIP"), PHP_HTTP_CONTENT_ENCODING_GZIP);
 +
 +      zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_NO"), PHP_HTTP_CACHE_NO);
 +      zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_HIT"), PHP_HTTP_CACHE_HIT);
 +      zend_declare_class_constant_long(php_http_env_response_class_entry, ZEND_STRL("CACHE_MISS"), PHP_HTTP_CACHE_MISS);
 +
 +      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("request"), ZEND_ACC_PROTECTED);
 +      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("cookies"), ZEND_ACC_PROTECTED);
 +      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentType"), ZEND_ACC_PROTECTED);
 +      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentDisposition"), ZEND_ACC_PROTECTED);
 +      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("contentEncoding"), ZEND_ACC_PROTECTED);
 +      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("cacheControl"), ZEND_ACC_PROTECTED);
 +      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("etag"), ZEND_ACC_PROTECTED);
 +      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("lastModified"), ZEND_ACC_PROTECTED);
 +      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleDelay"), ZEND_ACC_PROTECTED);
 +      zend_declare_property_null(php_http_env_response_class_entry, ZEND_STRL("throttleChunk"), ZEND_ACC_PROTECTED);
  
        return SUCCESS;
  }
diff --combined php_http_header.c
index 4651ef5a5d6cfca1abd695f154f36876a7733ed0,5a9ecd3bf136c8bfde5f21f931c45032760fae17..19cb936a7a3efb8a2fae5de5cd845e0a475ebd24
@@@ -12,7 -12,7 +12,7 @@@
  
  #include "php_http_api.h"
  
 -STATUS php_http_header_parse(const char *header, size_t length, HashTable *headers, php_http_info_callback_t callback_func, void **callback_data TSRMLS_DC)
 +ZEND_RESULT_CODE php_http_header_parse(const char *header, size_t length, HashTable *headers, php_http_info_callback_t callback_func, void **callback_data)
  {
        php_http_header_parser_t ctx;
        php_http_buffer_t buf;
        php_http_header_parser_dtor(&ctx);
        php_http_buffer_dtor(&buf);
  
-       if (rs == PHP_HTTP_HEADER_PARSER_STATE_FAILURE) {
-               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse headers");
-               return FAILURE;
-       }
-       
-       return SUCCESS;
+       return rs == PHP_HTTP_HEADER_PARSER_STATE_FAILURE ? FAILURE : SUCCESS;
  }
  
  void php_http_header_to_callback(HashTable *headers, zend_bool crlf, php_http_pass_format_callback_t cb, void *cb_arg TSRMLS_DC)
  {
 -      HashPosition pos1, pos2;
 -      php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 -      zval **header, **single_header;
 -
 -      FOREACH_HASH_KEYVAL(pos1, headers, key, header) {
 -              if (key.type == HASH_KEY_IS_STRING) {
 -                      if (key.len == sizeof("Set-Cookie") && !strcasecmp(key.str, "Set-Cookie") && Z_TYPE_PP(header) == IS_ARRAY) {
 -                              FOREACH_VAL(pos2, *header, single_header) {
 -                                      if (Z_TYPE_PP(single_header) == IS_ARRAY) {
 -                                              php_http_cookie_list_t *cookie = php_http_cookie_list_from_struct(NULL, *single_header TSRMLS_CC);
 +      php_http_arrkey_t key;
 +      zval *header, *single_header;
 +
 +      ZEND_HASH_FOREACH_KEY_VAL(headers, key.h, key.key, header)
 +      {
 +              if (key.key) {
 +                      if (zend_string_equals_literal(key.key, "Set-Cookie") && Z_TYPE_P(header) == IS_ARRAY) {
 +                              ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(header), single_header)
 +                              {
 +                                      if (Z_TYPE_P(single_header) == IS_ARRAY) {
 +                                              php_http_cookie_list_t *cookie = php_http_cookie_list_from_struct(NULL, single_header);
  
                                                if (cookie) {
                                                        char *buf;
                                                        efree(buf);
                                                }
                                        } else {
 -                                              zval *strval = php_http_header_value_to_string(*single_header TSRMLS_CC);
 +                                              zend_string *zs = php_http_header_value_to_string(single_header);
  
 -                                              cb(cb_arg, crlf ? "Set-Cookie: %s" PHP_HTTP_CRLF : "Set-Cookie: %s", Z_STRVAL_P(strval));
 -                                              zval_ptr_dtor(&strval);
 +                                              cb(cb_arg, crlf ? "Set-Cookie: %s" PHP_HTTP_CRLF : "Set-Cookie: %s", zs->val);
 +                                              zend_string_release(zs);
                                        }
                                }
 +                              ZEND_HASH_FOREACH_END();
                        } else {
 -                              zval *strval = php_http_header_value_to_string(*header TSRMLS_CC);
 +                              zend_string *zs = php_http_header_value_to_string(header);
  
 -                              cb(cb_arg, crlf ? "%s: %s" PHP_HTTP_CRLF : "%s: %s", key.str, Z_STRVAL_P(strval));
 -                              zval_ptr_dtor(&strval);
 +                              cb(cb_arg, crlf ? "%s: %s" PHP_HTTP_CRLF : "%s: %s", key.key->val, zs->val);
 +                              zend_string_release(zs);
                        }
                }
        }
 +      ZEND_HASH_FOREACH_END();
  }
  
  void php_http_header_to_string(php_http_buffer_t *str, HashTable *headers TSRMLS_DC)
        php_http_header_to_callback(headers, 1, (php_http_pass_format_callback_t) php_http_buffer_appendf, str TSRMLS_CC);
  }
  
 -zval *php_http_header_value_to_string(zval *header TSRMLS_DC)
 +zend_string *php_http_header_value_array_to_string(zval *header)
  {
 -      zval *ret;
 -
 -      if (Z_TYPE_P(header) == IS_BOOL) {
 -              MAKE_STD_ZVAL(ret);
 -              ZVAL_STRING(ret, Z_BVAL_P(header) ? "true" : "false", 1);
 -      } else if (Z_TYPE_P(header) == IS_ARRAY) {
 -              zval **val;
 -              HashPosition pos;
 -              php_http_buffer_t str;
 -
 -              php_http_buffer_init(&str);
 -              MAKE_STD_ZVAL(ret);
 -              FOREACH_VAL(pos,header, val) {
 -                      zval *strval = php_http_header_value_to_string(*val TSRMLS_CC);
 -
 -                      php_http_buffer_appendf(&str, str.used ? ", %s":"%s", Z_STRVAL_P(strval));
 -                      zval_ptr_dtor(&strval);
 -              }
 -              php_http_buffer_fix(&str);
 -              ZVAL_STRINGL(ret, str.data, str.used, 0);
 -      } else  {
 -              ret = php_http_zsep(1, IS_STRING, header);
 +      zval *val;
 +      php_http_buffer_t str;
 +
 +      php_http_buffer_init(&str);
 +      ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(header), val)
 +      {
 +              zend_string *zs = php_http_header_value_to_string(val);
 +
 +              php_http_buffer_appendf(&str, str.used ? ", %s":"%s", zs->val);
 +              zend_string_release(zs);
        }
 +      ZEND_HASH_FOREACH_END();
 +      php_http_buffer_fix(&str);
  
 -      return ret;
 +      return php_http_cs2zs(str.data, str.used);
 +}
 +
 +zend_string *php_http_header_value_to_string(zval *header)
 +{
 +      switch (Z_TYPE_P(header)) {
 +      case IS_TRUE:
 +              return zend_string_init(ZEND_STRL("true"), 0);
 +      case IS_FALSE:
 +              return zend_string_init(ZEND_STRL("false"), 0);
 +      case IS_ARRAY:
 +              return php_http_header_value_array_to_string(header);
 +      default:
 +              return zval_get_string(header);
 +      }
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader___construct, 0, 0, 0)
@@@ -128,17 -116,17 +123,17 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpHeader, __construct)
  {
        char *name_str = NULL, *value_str = NULL;
 -      int name_len = 0, value_len = 0;
 +      size_t name_len = 0, value_len = 0;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s!s!", &name_str, &name_len, &value_str, &value_len), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s!s!", &name_str, &name_len, &value_str, &value_len), invalid_arg, return);
  
        if (name_str && name_len) {
                char *pretty_str = estrndup(name_str, name_len);
 -              zend_update_property_stringl(php_http_header_class_entry, getThis(), ZEND_STRL("name"), php_http_pretty_key(pretty_str, name_len, 1, 1), name_len TSRMLS_CC);
 +              zend_update_property_stringl(php_http_header_class_entry, getThis(), ZEND_STRL("name"), php_http_pretty_key(pretty_str, name_len, 1, 1), name_len);
                efree(pretty_str);
        }
        if (value_str && value_len) {
 -              zend_update_property_stringl(php_http_header_class_entry, getThis(), ZEND_STRL("value"), value_str, value_len TSRMLS_CC);
 +              zend_update_property_stringl(php_http_header_class_entry, getThis(), ZEND_STRL("value"), value_str, value_len);
        }
  }
  
@@@ -148,24 -136,22 +143,24 @@@ PHP_METHOD(HttpHeader, serialize
  {
        if (SUCCESS == zend_parse_parameters_none()) {
                php_http_buffer_t buf;
 -              zval *zname, *zvalue;
 +              zend_string *zs;
 +              zval name_tmp, value_tmp;
  
                php_http_buffer_init(&buf);
 -              zname = php_http_ztyp(IS_STRING, zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("name"), 0 TSRMLS_CC));
 -              php_http_buffer_append(&buf, Z_STRVAL_P(zname), Z_STRLEN_P(zname));
 -              zval_ptr_dtor(&zname);
 -              zvalue = php_http_ztyp(IS_STRING, zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0 TSRMLS_CC));
 -              if (Z_STRLEN_P(zvalue)) {
 +              zs = zval_get_string(zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("name"), 0, &name_tmp));
 +              php_http_buffer_appendz(&buf, zs);
 +              zend_string_release(zs);
 +
 +              zs = zval_get_string(zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0, &value_tmp));
 +              if (zs->len) {
                        php_http_buffer_appends(&buf, ": ");
 -                      php_http_buffer_append(&buf, Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue));
 +                      php_http_buffer_appendz(&buf, zs);
                } else {
                        php_http_buffer_appends(&buf, ":");
                }
 -              zval_ptr_dtor(&zvalue);
 +              zend_string_release(zs);
  
 -              RETURN_PHP_HTTP_BUFFER_VAL(&buf);
 +              RETURN_STR(php_http_cs2zs(buf.data, buf.used));
        }
        RETURN_EMPTY_STRING();
  }
@@@ -176,31 -162,34 +171,31 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpHeader, unserialize)
  {
        char *serialized_str;
 -      int serialized_len;
 +      size_t serialized_len;
  
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized_str, &serialized_len)) {
                HashTable ht;
  
                zend_hash_init(&ht, 1, NULL, ZVAL_PTR_DTOR, 0);
 -              if (SUCCESS == php_http_header_parse(serialized_str, serialized_len, &ht, NULL, NULL TSRMLS_CC)) {
 +              if (SUCCESS == php_http_header_parse(serialized_str, serialized_len, &ht, NULL, NULL)) {
                        if (zend_hash_num_elements(&ht)) {
 -                              zval **val, *cpy;
 -                              char *str;
 -                              uint len;
 -                              ulong idx;
 +                              zend_string *zs, *key;
 +                              zend_ulong idx;
  
                                zend_hash_internal_pointer_reset(&ht);
 -                              switch (zend_hash_get_current_key_ex(&ht, &str, &len, &idx, 0, NULL)) {
 +                              switch (zend_hash_get_current_key(&ht, &key, &idx)) {
                                        case HASH_KEY_IS_STRING:
 -                                              zend_update_property_stringl(php_http_header_class_entry, getThis(), ZEND_STRL("name"), str, len - 1 TSRMLS_CC);
 +                                              zend_update_property_str(php_http_header_class_entry, getThis(), ZEND_STRL("name"), key);
                                                break;
                                        case HASH_KEY_IS_LONG:
 -                                              zend_update_property_long(php_http_header_class_entry, getThis(), ZEND_STRL("name"), idx TSRMLS_CC);
 +                                              zend_update_property_long(php_http_header_class_entry, getThis(), ZEND_STRL("name"), idx);
                                                break;
                                        default:
                                                break;
                                }
 -                              zend_hash_get_current_data(&ht, (void *) &val);
 -                              cpy = php_http_zsep(1, IS_STRING, *val);
 -                              zend_update_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), cpy TSRMLS_CC);
 -                              zval_ptr_dtor(&cpy);
 +                              zs = zval_get_string(zend_hash_get_current_data(&ht));
 +                              zend_update_property_str(php_http_header_class_entry, getThis(), ZEND_STRL("value"), zs);
 +                              zend_string_release(zs);
                        }
                }
                zend_hash_destroy(&ht);
@@@ -215,18 -204,17 +210,18 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpHeader, match)
  {
        char *val_str;
 -      int val_len;
 -      long flags = PHP_HTTP_MATCH_LOOSE;
 -      zval *zvalue;
 +      size_t val_len;
 +      zend_long flags = PHP_HTTP_MATCH_LOOSE;
 +      zend_string *zs;
 +      zval value_tmp;
  
 -      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|sl", &val_str, &val_len, &flags)) {
 +      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "|sl", &val_str, &val_len, &flags)) {
                return;
        }
  
 -      zvalue = php_http_ztyp(IS_STRING, zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0 TSRMLS_CC));
 -      RETVAL_BOOL(php_http_match(Z_STRVAL_P(zvalue), val_str, flags));
 -      zval_ptr_dtor(&zvalue);
 +      zs = zval_get_string(zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0, &value_tmp));
 +      RETVAL_BOOL(php_http_match(zs->val, val_str, flags));
 +      zend_string_release(zs);
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_negotiate, 0, 0, 1)
@@@ -236,37 -224,35 +231,37 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpHeader, negotiate)
  {
        HashTable *supported, *rs;
 -      zval *zname, *zvalue, *rs_array = NULL;
 +      zval name_tmp, value_tmp, *rs_array = NULL;
 +      zend_string *zs;
        char *sep_str = NULL;
        size_t sep_len = 0;
  
 -      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "H|z", &supported, &rs_array)) {
 +      if (SUCCESS != zend_parse_parameters(ZEND_NUM_ARGS(), "H|z", &supported, &rs_array)) {
                return;
        }
        if (rs_array) {
 +              ZVAL_DEREF(rs_array);
                zval_dtor(rs_array);
                array_init(rs_array);
        }
  
 -      zname = php_http_ztyp(IS_STRING, zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("name"), 0 TSRMLS_CC));
 -      if (!strcasecmp(Z_STRVAL_P(zname), "Accept")) {
 +      zs = zval_get_string(zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("name"), 0, &name_tmp));
 +      if (zend_string_equals_literal(zs, "Accept")) {
                sep_str = "/";
                sep_len = 1;
 -      } else if (!strcasecmp(Z_STRVAL_P(zname), "Accept-Language")) {
 +      } else if (zend_string_equals_literal(zs, "Accept-Language")) {
                sep_str = "-";
                sep_len = 1;
        }
 -      zval_ptr_dtor(&zname);
 +      zend_string_release(zs);
  
 -      zvalue = php_http_ztyp(IS_STRING, zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0 TSRMLS_CC));
 -      if ((rs = php_http_negotiate(Z_STRVAL_P(zvalue), Z_STRLEN_P(zvalue), supported, sep_str, sep_len TSRMLS_CC))) {
 +      zs = zval_get_string(zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0, &value_tmp));
 +      if ((rs = php_http_negotiate(zs->val, zs->len, supported, sep_str, sep_len))) {
                PHP_HTTP_DO_NEGOTIATE_HANDLE_RESULT(rs, supported, rs_array);
        } else {
                PHP_HTTP_DO_NEGOTIATE_HANDLE_DEFAULT(supported, rs_array);
        }
 -      zval_ptr_dtor(&zvalue);
 +      zend_string_release(zs);
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeader_getParams, 0, 0, 0)
  ZEND_END_ARG_INFO();
  PHP_METHOD(HttpHeader, getParams)
  {
 -      zval zctor, *zparams_obj, **zargs = NULL;
 +      zval value_tmp, zctor, zparams_obj, *zargs = NULL;
        
 -      INIT_PZVAL(&zctor);
 -      ZVAL_STRINGL(&zctor, "__construct", lenof("__construct"), 0);
 +      ZVAL_STRINGL(&zctor, "__construct", lenof("__construct"));
        
 -      MAKE_STD_ZVAL(zparams_obj);
 -      object_init_ex(zparams_obj, php_http_params_class_entry);
 +      object_init_ex(&zparams_obj, php_http_params_class_entry);
        
 -      zargs = (zval **) ecalloc(ZEND_NUM_ARGS()+1, sizeof(zval *));
 -      zargs[0] = zend_read_property(Z_OBJCE_P(getThis()), getThis(), ZEND_STRL("value"), 0 TSRMLS_CC);
 +      zargs = (zval *) ecalloc(ZEND_NUM_ARGS()+1, sizeof(zval));
 +      ZVAL_COPY_VALUE(&zargs[0], zend_read_property(php_http_header_class_entry, getThis(), ZEND_STRL("value"), 0, &value_tmp));
        if (ZEND_NUM_ARGS()) {
                zend_get_parameters_array(ZEND_NUM_ARGS(), ZEND_NUM_ARGS(), &zargs[1]);
        }
        
 -      if (SUCCESS == call_user_function(NULL, &zparams_obj, &zctor, return_value, ZEND_NUM_ARGS()+1, zargs TSRMLS_CC)) {
 -              RETVAL_ZVAL(zparams_obj, 0, 1);
 +      if (SUCCESS == call_user_function(NULL, &zparams_obj, &zctor, return_value, ZEND_NUM_ARGS()+1, zargs)) {
 +              RETVAL_ZVAL(&zparams_obj, 0, 1);
        }
        
 +      zval_ptr_dtor(&zctor);
        if (zargs) {
                efree(zargs);
        }
@@@ -306,43 -293,47 +301,43 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpHeader, parse)
  {
        char *header_str;
 -      int header_len;
 +      size_t header_len;
        zend_class_entry *ce = NULL;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|C", &header_str, &header_len, &ce)) {
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s|C", &header_str, &header_len, &ce)) {
                array_init(return_value);
  
 -              if (SUCCESS != php_http_header_parse(header_str, header_len, Z_ARRVAL_P(return_value), NULL, NULL TSRMLS_CC)) {
 +              if (SUCCESS != php_http_header_parse(header_str, header_len, Z_ARRVAL_P(return_value), NULL, NULL)) {
                        zval_dtor(return_value);
                        RETURN_FALSE;
                } else {
                        if (ce && instanceof_function(ce, php_http_header_class_entry TSRMLS_CC)) {
 -                              HashPosition pos;
 -                              php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 -                              zval **val;
 -
 -                              FOREACH_KEYVAL(pos, return_value, key, val) {
 -                                      zval *zho, *zkey, *zvalue;
 +                              php_http_arrkey_t key;
 +                              zval *val;
  
 -                                      Z_ADDREF_PP(val);
 -                                      zvalue = *val;
 +                              ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(return_value), key.h, key.key, val)
 +                              {
 +                                      zval zkey, zho;
  
 -                                      MAKE_STD_ZVAL(zkey);
 -                                      if (key.type == HASH_KEY_IS_LONG) {
 -                                              ZVAL_LONG(zkey, key.num);
 +                                      if (key.key) {
 +                                              ZVAL_STR_COPY(&zkey, key.key);
                                        } else {
 -                                              ZVAL_STRINGL(zkey, key.str, key.len - 1, 1);
 +                                              ZVAL_LONG(&zkey, key.h);
                                        }
  
 -                                      MAKE_STD_ZVAL(zho);
 -                                      object_init_ex(zho, ce);
 -                                      zend_call_method_with_2_params(&zho, ce, NULL, "__construct", NULL, zkey, zvalue);
 +                                      object_init_ex(&zho, ce);
 +                                      Z_TRY_ADDREF_P(val);
 +                                      zend_call_method_with_2_params(&zho, ce, NULL, "__construct", NULL, &zkey, val);
 +                                      zval_ptr_dtor(val);
 +                                      zval_ptr_dtor(&zkey);
  
 -                                      if (key.type == HASH_KEY_IS_LONG) {
 -                                              zend_hash_index_update(Z_ARRVAL_P(return_value), key.num, (void *) &zho, sizeof(zval *), NULL);
 +                                      if (key.key) {
 +                                              add_assoc_zval_ex(return_value, key.key->val, key.key->len, &zho);
                                        } else {
 -                                              zend_hash_update(Z_ARRVAL_P(return_value), key.str, key.len, (void *) &zho, sizeof(zval *), NULL);
 +                                              add_index_zval(return_value, key.h, &zho);
                                        }
 -
 -                                      zval_ptr_dtor(&zvalue);
 -                                      zval_ptr_dtor(&zkey);
                                }
 +                              ZEND_HASH_FOREACH_END();
                        }
                }
        }
@@@ -368,15 -359,15 +363,15 @@@ PHP_MINIT_FUNCTION(http_header
        zend_class_entry ce = {0};
  
        INIT_NS_CLASS_ENTRY(ce, "http", "Header", php_http_header_methods);
 -      php_http_header_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
 -      zend_class_implements(php_http_header_class_entry TSRMLS_CC, 1, zend_ce_serializable);
 -      zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_LOOSE"), PHP_HTTP_MATCH_LOOSE TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_CASE"), PHP_HTTP_MATCH_CASE TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_WORD"), PHP_HTTP_MATCH_WORD TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_FULL"), PHP_HTTP_MATCH_FULL TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_STRICT"), PHP_HTTP_MATCH_STRICT TSRMLS_CC);
 -      zend_declare_property_null(php_http_header_class_entry, ZEND_STRL("name"), ZEND_ACC_PUBLIC TSRMLS_CC);
 -      zend_declare_property_null(php_http_header_class_entry, ZEND_STRL("value"), ZEND_ACC_PUBLIC TSRMLS_CC);
 +      php_http_header_class_entry = zend_register_internal_class(&ce);
 +      zend_class_implements(php_http_header_class_entry, 1, zend_ce_serializable);
 +      zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_LOOSE"), PHP_HTTP_MATCH_LOOSE);
 +      zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_CASE"), PHP_HTTP_MATCH_CASE);
 +      zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_WORD"), PHP_HTTP_MATCH_WORD);
 +      zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_FULL"), PHP_HTTP_MATCH_FULL);
 +      zend_declare_class_constant_long(php_http_header_class_entry, ZEND_STRL("MATCH_STRICT"), PHP_HTTP_MATCH_STRICT);
 +      zend_declare_property_null(php_http_header_class_entry, ZEND_STRL("name"), ZEND_ACC_PUBLIC);
 +      zend_declare_property_null(php_http_header_class_entry, ZEND_STRL("value"), ZEND_ACC_PUBLIC);
  
        return SUCCESS;
  }
diff --combined php_http_header_parser.c
index e14b44333b1efe44b23b6f05fe2ba65f14bd3d11,cea2dbfa67e5a5e09e535327241cfe8f81032fce..cf30c84c48bc5ba0be2415a9a5572224adcfa940
  
  #include "php_http_api.h"
  
+ #ifndef DBG_PARSER
+ #     define DBG_PARSER 0
+ #endif
  typedef struct php_http_header_parser_state_spec {
        php_http_header_parser_state_t state;
        unsigned need_data:1;
@@@ -21,18 -25,20 +25,18 @@@ static const php_http_header_parser_sta
                {PHP_HTTP_HEADER_PARSER_STATE_START,            1},
                {PHP_HTTP_HEADER_PARSER_STATE_KEY,                      1},
                {PHP_HTTP_HEADER_PARSER_STATE_VALUE,            1},
-               {PHP_HTTP_HEADER_PARSER_STATE_VALUE_EX,         1},
+               {PHP_HTTP_HEADER_PARSER_STATE_VALUE_EX,         0},
                {PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE,      0},
                {PHP_HTTP_HEADER_PARSER_STATE_DONE,                     0}
  };
  
 -php_http_header_parser_t *php_http_header_parser_init(php_http_header_parser_t *parser TSRMLS_DC)
 +php_http_header_parser_t *php_http_header_parser_init(php_http_header_parser_t *parser)
  {
        if (!parser) {
                parser = emalloc(sizeof(*parser));
        }
        memset(parser, 0, sizeof(*parser));
  
 -      TSRMLS_SET_CTX(parser->ts);
 -
        return parser;
  }
  
@@@ -90,19 -96,38 +94,35 @@@ void php_http_header_parser_free(php_ht
        }
  }
  
 -static void php_http_header_parser_error(size_t valid_len, char *str, size_t len, const char *eol_str TSRMLS_DC)
+ /* NOTE: 'str' has to be null terminated */
 -      int escaped_len;
 -      char *escaped_str;
++static void php_http_header_parser_error(size_t valid_len, char *str, size_t len, const char *eol_str )
+ {
 -      escaped_str = php_addcslashes(str, len, &escaped_len, 0, ZEND_STRL("\x0..\x1F\x7F..\xFF") TSRMLS_CC);
++      zend_string *escaped_str = zend_string_init(str, len, 0);
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse headers: unexpected character '\\%03o' at pos %zu of '%.*s'", str[valid_len], valid_len, escaped_len, escaped_str);
++      escaped_str = php_addcslashes(escaped_str, 1, ZEND_STRL("\x0..\x1F\x7F..\xFF"));
+       if (valid_len != len && (!eol_str || (str+valid_len) != eol_str)) {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse headers: unexpected end of line at pos %zu of '%.*s'", eol_str - str, escaped_len, escaped_str);
++              php_error_docref(NULL, E_WARNING, "Failed to parse headers: unexpected character '\\%03o' at pos %zu of '%s'", str[valid_len], valid_len, escaped_str->val);
+       } else if (eol_str) {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse headers: unexpected end of input at pos %zu of '%.*s'", len, escaped_len, escaped_str);
++              php_error_docref(NULL, E_WARNING, "Failed to parse headers: unexpected end of line at pos %zu of '%s'", eol_str - str, escaped_str->val);
+       } else {
 -STATUS php_http_header_parser_parse(php_http_header_parser_t *parser, php_http_buffer_t *buffer, unsigned flags, HashTable *headers, php_http_info_callback_t callback_func, void *callback_arg)
++              php_error_docref(NULL, E_WARNING, "Failed to parse headers: unexpected end of input at pos %zu of '%s'", len, escaped_str->val);
+       }
+       efree(escaped_str);
+ }
 +php_http_header_parser_state_t php_http_header_parser_parse(php_http_header_parser_t *parser, php_http_buffer_t *buffer, unsigned flags, HashTable *headers, php_http_info_callback_t callback_func, void *callback_arg)
  {
 -      TSRMLS_FETCH_FROM_CTX(parser->ts);
 -
        while (buffer->used || !php_http_header_parser_states[php_http_header_parser_state_is(parser)].need_data) {
- #if 0
+ #if DBG_PARSER
                const char *state[] = {"START", "KEY", "VALUE", "VALUE_EX", "HEADER_DONE", "DONE"};
-               int num_headers = headers ? zend_hash_num_elements(headers) : 0;
-               fprintf(stderr, "#HP: (%d) %s (avail:%zu, num:%d)\n", php_http_header_parser_state_is(parser),
-                               php_http_header_parser_state_is(parser) < 0 ? "FAILURE" : state[php_http_header_parser_state_is(parser)],
-                                               buffer->used, num_headers);
+               fprintf(stderr, "#HP: %s (avail:%zu, num:%d cleanup:%u)\n", php_http_header_parser_state_is(parser) < 0 ? "FAILURE" : state[php_http_header_parser_state_is(parser)], buffer->used, headers?zend_hash_num_elements(headers):0, flags);
                _dpf(0, buffer->data, buffer->used);
  #endif
                switch (php_http_header_parser_state_pop(parser)) {
                        case PHP_HTTP_HEADER_PARSER_STATE_FAILURE:
+                               php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to parse headers");
                                return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_FAILURE);
  
                        case PHP_HTTP_HEADER_PARSER_STATE_START: {
                                        /* end of headers */
                                        php_http_buffer_cut(buffer, 0, eol_len);
                                        php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_DONE);
 -                              } else if (php_http_info_parse(&parser->info, php_http_buffer_fix(buffer)->data TSRMLS_CC)) {
 +                              } else if (php_http_info_parse(&parser->info, php_http_buffer_fix(buffer)->data)) {
                                        /* new message starting with request/response line */
                                        if (callback_func) {
 -                                              callback_func(callback_arg, &headers, &parser->info TSRMLS_CC);
 +                                              callback_func(callback_arg, &headers, &parser->info);
                                        }
                                        php_http_info_dtor(&parser->info);
                                        php_http_buffer_cut(buffer, 0, eol_str + eol_len - buffer->data);
                                        php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE);
                                } else if ((colon = memchr(buffer->data, ':', buffer->used)) && (!eol_str || eol_str > colon)) {
                                        /* header: string */
-                                       parser->_key.str = estrndup(buffer->data, parser->_key.len = colon - buffer->data);
+                                       size_t valid_len;
+                                       parser->_key.len = colon - buffer->data;
+                                       parser->_key.str = estrndup(buffer->data, parser->_key.len);
+                                       valid_len = strspn(parser->_key.str, PHP_HTTP_HEADER_NAME_CHARS);
+                                       if (valid_len != parser->_key.len) {
+                                               php_http_header_parser_error(valid_len, parser->_key.str, parser->_key.len, eol_str TSRMLS_CC);
+                                               PTR_SET(parser->_key.str, NULL);
+                                               return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_FAILURE);
+                                       }
                                        while (PHP_HTTP_IS_CTYPE(space, *++colon) && *colon != '\n' && *colon != '\r');
                                        php_http_buffer_cut(buffer, 0, colon - buffer->data);
                                        php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE);
-                               } else {
-                                       /* neither reqeust/response line nor header: string */
+                               } else if (eol_str || (flags & PHP_HTTP_HEADER_PARSER_CLEANUP)) {
+                                       /* neither reqeust/response line nor 'header:' string, or injected new line or NUL etc. */
+                                       php_http_buffer_fix(buffer);
+                                       php_http_header_parser_error(strspn(buffer->data, PHP_HTTP_HEADER_NAME_CHARS), buffer->data, buffer->used, eol_str TSRMLS_CC);
                                        return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_FAILURE);
+                               } else {
+                                       /* keep feeding */
+                                       return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_KEY);
                                }
                                break;
                        }
  
                                if ((eol_str = php_http_locate_bin_eol(buffer->data, buffer->used, &eol_len))) {
                                        SET_ADD_VAL(eol_str - buffer->data, eol_len);
-                                       if (buffer->used) {
-                                               if (*buffer->data != '\t' && *buffer->data != ' ') {
-                                                       php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE);
-                                                       break;
-                                               } else {
-                                                       php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE);
-                                                       break;
-                                               }
-                                       }
-                               }
-                               if (flags & PHP_HTTP_HEADER_PARSER_CLEANUP) {
+                                       php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE_EX);
+                               } else if (flags & PHP_HTTP_HEADER_PARSER_CLEANUP) {
                                        if (buffer->used) {
                                                SET_ADD_VAL(buffer->used, 0);
                                        }
                                        php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE);
                                } else {
-                                       return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE_EX);
+                                       return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE);
                                }
                                break;
                        }
  
                        case PHP_HTTP_HEADER_PARSER_STATE_VALUE_EX:
-                               if (*buffer->data == ' ' || *buffer->data == '\t') {
+                               if (buffer->used && (*buffer->data == ' ' || *buffer->data == '\t')) {
                                        php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE);
-                               } else {
+                               } else if (buffer->used || (flags & PHP_HTTP_HEADER_PARSER_CLEANUP)) {
                                        php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE);
+                               } else {
+                                       /* keep feeding */
+                                       return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_VALUE_EX);
                                }
                                break;
  
                        case PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE:
                                if (parser->_key.str && parser->_val.str) {
 -                                      zval array, **exist;
 +                                      zval tmp, *exist;
+                                       size_t valid_len = strlen(parser->_val.str);
+                                       /* check for truncation */
+                                       if (valid_len != parser->_val.len) {
+                                               php_http_header_parser_error(valid_len, parser->_val.str, parser->_val.len, NULL TSRMLS_CC);
+                                               PTR_SET(parser->_key.str, NULL);
+                                               PTR_SET(parser->_val.str, NULL);
+                                               return php_http_header_parser_state_push(parser, 1, PHP_HTTP_HEADER_PARSER_STATE_FAILURE);
+                                       }
  
                                        if (!headers && callback_func) {
 -                                              callback_func(callback_arg, &headers, NULL TSRMLS_CC);
 +                                              callback_func(callback_arg, &headers, NULL);
                                        }
  
 -                                      INIT_PZVAL_ARRAY(&array, headers);
                                        php_http_pretty_key(parser->_key.str, parser->_key.len, 1, 1);
 -                                      if (SUCCESS == zend_symtable_find(headers, parser->_key.str, parser->_key.len + 1, (void *) &exist)) {
 -                                              convert_to_array(*exist);
 -                                              add_next_index_stringl(*exist, parser->_val.str, parser->_val.len, 0);
 +                                      if ((exist = zend_symtable_str_find(headers, parser->_key.str, parser->_key.len))) {
 +                                              convert_to_array(exist);
 +                                              add_next_index_str(exist, php_http_cs2zs(parser->_val.str, parser->_val.len));
                                        } else {
 -                                              add_assoc_stringl_ex(&array, parser->_key.str, parser->_key.len + 1, parser->_val.str, parser->_val.len, 0);
 +                                              ZVAL_STR(&tmp, php_http_cs2zs(parser->_val.str, parser->_val.len));
 +                                              zend_symtable_str_update(headers, parser->_key.str, parser->_key.len, &tmp);
                                        }
                                        parser->_val.str = NULL;
                                }
        return php_http_header_parser_state_is(parser);
  }
  
 -zend_object_value php_http_header_parser_object_new(zend_class_entry *ce TSRMLS_DC)
+ php_http_header_parser_state_t php_http_header_parser_parse_stream(php_http_header_parser_t *parser, php_http_buffer_t *buf, php_stream *s, unsigned flags, HashTable *headers, php_http_info_callback_t callback_func, void *callback_arg)
+ {
+       php_http_message_parser_state_t state = PHP_HTTP_MESSAGE_PARSER_STATE_START;
+       TSRMLS_FETCH_FROM_CTX(parser->ts);
+       if (!buf->data) {
+               php_http_buffer_resize_ex(buf, 0x1000, 1, 0);
+       }
+       while (1) {
+               size_t justread = 0;
+ #if DBG_PARSER
+               const char *states[] = {"START", "KEY", "VALUE", "VALUE_EX", "HEADER_DONE", "DONE"};
+               fprintf(stderr, "#SHP: %s (f:%u)\n", states[state], flags);
+ #endif
+               /* resize if needed */
+               if (buf->free < 0x1000) {
+                       php_http_buffer_resize_ex(buf, 0x1000, 1, 0);
+               }
+               switch (state) {
+               case PHP_HTTP_HEADER_PARSER_STATE_FAILURE:
+               case PHP_HTTP_HEADER_PARSER_STATE_DONE:
+                       return state;
+               default:
+                       /* read line */
+                       php_stream_get_line(s, buf->data + buf->used, buf->free, &justread);
+                       /* if we fail reading a whole line, try a single char */
+                       if (!justread) {
+                               int c = php_stream_getc(s);
+                               if (c != EOF) {
+                                       char s[1] = {c};
+                                       justread = php_http_buffer_append(buf, s, 1);
+                               }
+                       }
+                       php_http_buffer_account(buf, justread);
+               }
+               if (justread) {
+                       state = php_http_header_parser_parse(parser, buf, flags, headers, callback_func, callback_arg);
+               } else if (php_stream_eof(s)) {
+                       return php_http_header_parser_parse(parser, buf, flags | PHP_HTTP_HEADER_PARSER_CLEANUP, headers, callback_func, callback_arg);
+               } else  {
+                       return state;
+               }
+       }
+       return PHP_HTTP_HEADER_PARSER_STATE_DONE;
+ }
+ zend_class_entry *php_http_header_parser_class_entry;
+ static zend_object_handlers php_http_header_parser_object_handlers;
 -      return php_http_header_parser_object_new_ex(ce, NULL, NULL TSRMLS_CC);
++zend_object *php_http_header_parser_object_new(zend_class_entry *ce)
+ {
 -zend_object_value php_http_header_parser_object_new_ex(zend_class_entry *ce, php_http_header_parser_t *parser, php_http_header_parser_object_t **ptr TSRMLS_DC)
++      return &php_http_header_parser_object_new_ex(ce, NULL)->zo;
+ }
 -      o = ecalloc(1, sizeof(php_http_header_parser_object_t));
 -      zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
 -      object_properties_init((zend_object *) o, ce);
 -
 -      if (ptr) {
 -              *ptr = o;
 -      }
++php_http_header_parser_object_t *php_http_header_parser_object_new_ex(zend_class_entry *ce, php_http_header_parser_t *parser)
+ {
+       php_http_header_parser_object_t *o;
 -              o->parser = php_http_header_parser_init(NULL TSRMLS_CC);
++      o = ecalloc(1, sizeof(php_http_header_parser_object_t) + zend_object_properties_size(ce));
++      zend_object_std_init(&o->zo, ce);
++      object_properties_init(&o->zo, ce);
+       if (parser) {
+               o->parser = parser;
+       } else {
 -      o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_header_parser_object_free, NULL TSRMLS_CC);
 -      o->zv.handlers = &php_http_header_parser_object_handlers;
++              o->parser = php_http_header_parser_init(NULL);
+       }
+       o->buffer = php_http_buffer_new();
 -      return o->zv;
++      o->zo.handlers = &php_http_header_parser_object_handlers;
 -void php_http_header_parser_object_free(void *object TSRMLS_DC)
++      return o;
+ }
 -      php_http_header_parser_object_t *o = (php_http_header_parser_object_t *) object;
++void php_http_header_parser_object_free(zend_object *object)
+ {
 -      zend_object_std_dtor((zend_object *) o TSRMLS_CC);
 -      efree(o);
++      php_http_header_parser_object_t *o = PHP_HTTP_OBJ(object, NULL);
+       if (o->parser) {
+               php_http_header_parser_free(&o->parser);
+       }
+       if (o->buffer) {
+               php_http_buffer_free(&o->buffer);
+       }
 -      php_http_header_parser_object_t *parser_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      zend_object_std_dtor(object);
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeaderParser_getState, 0, 0, 0)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpHeaderParser, getState)
+ {
 -      int data_len;
 -      long flags;
++      php_http_header_parser_object_t *parser_obj = PHP_HTTP_OBJ(NULL, getThis());
+       zend_parse_parameters_none();
+       /* always return the real state */
+       RETVAL_LONG(php_http_header_parser_state_is(parser_obj->parser));
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeaderParser_parse, 0, 0, 3)
+       ZEND_ARG_INFO(0, data)
+       ZEND_ARG_INFO(0, flags)
+       ZEND_ARG_ARRAY_INFO(1, headers, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpHeaderParser, parse)
+ {
+       php_http_header_parser_object_t *parser_obj;
+       zval *zmsg;
+       char *data_str;
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "slz", &data_str, &data_len, &flags, &zmsg), invalid_arg, return);
++      size_t data_len;
++      zend_long flags;
 -      parser_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "slz", &data_str, &data_len, &flags, &zmsg), invalid_arg, return);
++      ZVAL_DEREF(zmsg);
+       if (Z_TYPE_P(zmsg) != IS_ARRAY) {
+               zval_dtor(zmsg);
+               array_init(zmsg);
+       }
 -      long flags;
++      parser_obj = PHP_HTTP_OBJ(NULL, getThis());
+       php_http_buffer_append(parser_obj->buffer, data_str, data_len);
+       RETVAL_LONG(php_http_header_parser_parse(parser_obj->parser, parser_obj->buffer, flags, Z_ARRVAL_P(zmsg), NULL, NULL));
+ }
+ ZEND_BEGIN_ARG_INFO_EX(ai_HttpHeaderParser_stream, 0, 0, 3)
+       ZEND_ARG_INFO(0, stream)
+       ZEND_ARG_INFO(0, flags)
+       ZEND_ARG_ARRAY_INFO(1, headers, 1)
+ ZEND_END_ARG_INFO();
+ static PHP_METHOD(HttpHeaderParser, stream)
+ {
+       php_http_header_parser_object_t *parser_obj;
+       zend_error_handling zeh;
+       zval *zmsg, *zstream;
+       php_stream *s;
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz", &zstream, &flags, &zmsg), invalid_arg, return);
++      zend_long flags;
 -      zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh TSRMLS_CC);
 -      php_stream_from_zval(s, &zstream);
 -      zend_restore_error_handling(&zeh TSRMLS_CC);
++      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "rlz", &zstream, &flags, &zmsg), invalid_arg, return);
 -      parser_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh);
++      php_stream_from_zval(s, zstream);
++      zend_restore_error_handling(&zeh);
++      ZVAL_DEREF(zmsg);
+       if (Z_TYPE_P(zmsg) != IS_ARRAY) {
+               zval_dtor(zmsg);
+               array_init(zmsg);
+       }
 -      php_http_header_parser_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
++      parser_obj = PHP_HTTP_OBJ(NULL, getThis());
+       RETVAL_LONG(php_http_header_parser_parse_stream(parser_obj->parser, parser_obj->buffer, s, flags, Z_ARRVAL_P(zmsg), NULL, NULL));
+ }
+ static zend_function_entry php_http_header_parser_methods[] = {
+               PHP_ME(HttpHeaderParser, getState, ai_HttpHeaderParser_getState, ZEND_ACC_PUBLIC)
+               PHP_ME(HttpHeaderParser, parse, ai_HttpHeaderParser_parse, ZEND_ACC_PUBLIC)
+               PHP_ME(HttpHeaderParser, stream, ai_HttpHeaderParser_stream, ZEND_ACC_PUBLIC)
+               {NULL, NULL, NULL}
+ };
+ PHP_MINIT_FUNCTION(http_header_parser)
+ {
+       zend_class_entry ce;
+       INIT_NS_CLASS_ENTRY(ce, "http\\Header", "Parser", php_http_header_parser_methods);
 -      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("CLEANUP"), PHP_HTTP_HEADER_PARSER_CLEANUP TSRMLS_CC);
++      php_http_header_parser_class_entry = zend_register_internal_class(&ce);
+       memcpy(&php_http_header_parser_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+       php_http_header_parser_class_entry->create_object = php_http_header_parser_object_new;
++      php_http_header_parser_object_handlers.offset = XtOffsetOf(php_http_header_parser_object_t, zo);
+       php_http_header_parser_object_handlers.clone_obj = NULL;
++      php_http_header_parser_object_handlers.free_obj = php_http_header_parser_object_free;
 -      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_FAILURE"), PHP_HTTP_HEADER_PARSER_STATE_FAILURE TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_START"), PHP_HTTP_HEADER_PARSER_STATE_START TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_KEY"), PHP_HTTP_HEADER_PARSER_STATE_KEY TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_VALUE"), PHP_HTTP_HEADER_PARSER_STATE_VALUE TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_VALUE_EX"), PHP_HTTP_HEADER_PARSER_STATE_VALUE_EX TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_HEADER_DONE"), PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_DONE"), PHP_HTTP_HEADER_PARSER_STATE_DONE TSRMLS_CC);
++      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("CLEANUP"), PHP_HTTP_HEADER_PARSER_CLEANUP);
++      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_FAILURE"), PHP_HTTP_HEADER_PARSER_STATE_FAILURE);
++      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_START"), PHP_HTTP_HEADER_PARSER_STATE_START);
++      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_KEY"), PHP_HTTP_HEADER_PARSER_STATE_KEY);
++      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_VALUE"), PHP_HTTP_HEADER_PARSER_STATE_VALUE);
++      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_VALUE_EX"), PHP_HTTP_HEADER_PARSER_STATE_VALUE_EX);
++      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_HEADER_DONE"), PHP_HTTP_HEADER_PARSER_STATE_HEADER_DONE);
++      zend_declare_class_constant_long(php_http_header_parser_class_entry, ZEND_STRL("STATE_DONE"), PHP_HTTP_HEADER_PARSER_STATE_DONE);
+       return SUCCESS;
+ }
  /*
   * Local variables:
   * tab-width: 4
diff --combined php_http_header_parser.h
index 5b4993a459f070ed33fe27f2c40f8081f23c5e51,ed9ecaf1af5b3f9db7769cc51f54da99da6bcbea..33707bd6fb5709ec5643fe950567e9747447bca5
@@@ -38,15 -38,34 +38,30 @@@ typedef struct php_http_header_parser 
                char *str;
                size_t len;
        } _val;
 -#ifdef ZTS
 -      void ***ts;
 -#endif
  } php_http_header_parser_t;
  
 -PHP_HTTP_API php_http_header_parser_t *php_http_header_parser_init(php_http_header_parser_t *parser TSRMLS_DC);
 +PHP_HTTP_API php_http_header_parser_t *php_http_header_parser_init(php_http_header_parser_t *parser);
  PHP_HTTP_API php_http_header_parser_state_t php_http_header_parser_state_push(php_http_header_parser_t *parser, unsigned argc, ...);
  PHP_HTTP_API php_http_header_parser_state_t php_http_header_parser_state_is(php_http_header_parser_t *parser);
  PHP_HTTP_API php_http_header_parser_state_t php_http_header_parser_state_pop(php_http_header_parser_t *parser);
  PHP_HTTP_API void php_http_header_parser_dtor(php_http_header_parser_t *parser);
  PHP_HTTP_API void php_http_header_parser_free(php_http_header_parser_t **parser);
  PHP_HTTP_API php_http_header_parser_state_t php_http_header_parser_parse(php_http_header_parser_t *parser, php_http_buffer_t *buffer, unsigned flags, HashTable *headers, php_http_info_callback_t callback_func, void *callback_arg);
 -      zend_object zo;
 -      zend_object_value zv;
+ PHP_HTTP_API php_http_header_parser_state_t php_http_headerparser_parse_stream(php_http_header_parser_t *parser, php_http_buffer_t *buffer, php_stream *s, unsigned flags, HashTable *headers, php_http_info_callback_t callback_func, void *callback_arg);
+ typedef struct php_http_header_parser_object {
 -zend_object_value php_http_header_parser_object_new(zend_class_entry *ce TSRMLS_DC);
 -zend_object_value php_http_header_parser_object_new_ex(zend_class_entry *ce, php_http_header_parser_t *parser, php_http_header_parser_object_t **ptr TSRMLS_DC);
 -void php_http_header_parser_object_free(void *object TSRMLS_DC);
+       php_http_buffer_t *buffer;
+       php_http_header_parser_t *parser;
++      zend_object zo;
+ } php_http_header_parser_object_t;
+ PHP_HTTP_API zend_class_entry *php_http_header_parser_class_entry;
+ PHP_MINIT_FUNCTION(http_header_parser);
++zend_object *php_http_header_parser_object_new(zend_class_entry *ce);
++php_http_header_parser_object_t *php_http_header_parser_object_new_ex(zend_class_entry *ce, php_http_header_parser_t *parser);
++void php_http_header_parser_object_free(zend_object *object);
  
  #endif /* PHP_HTTP_HEADER_PARSER_H */
  
diff --combined php_http_message.c
index 6ecf6e737831eb818d75beda189f431271ea6c57,b7b500b313bbea8b1308c194e14e1e653cc0b33c..3d3822cfeeb8326e455e5aae29d8d1afa93e8b38
  
  static void message_headers(php_http_message_t *msg, php_http_buffer_t *str);
  
 -zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info TSRMLS_DC)
 +zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info)
  {
        php_http_message_t *old = *message;
  
        /* advance message */
        if (!old || old->type || zend_hash_num_elements(&old->hdrs)) {
 -              (*message) = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
 +              (*message) = php_http_message_init(NULL, 0, NULL);
                (*message)->parent = old;
                if (headers) {
                        (*headers) = &((*message)->hdrs);
        return old != *message;
  }
  
 -php_http_message_t *php_http_message_init(php_http_message_t *message, php_http_message_type_t type, php_http_message_body_t *body TSRMLS_DC)
 +php_http_message_t *php_http_message_init(php_http_message_t *message, php_http_message_type_t type, php_http_message_body_t *body)
  {
        if (!message) {
                message = emalloc(sizeof(*message));
        }
        memset(message, 0, sizeof(*message));
 -      TSRMLS_SET_CTX(message->ts);
  
        php_http_message_set_type(message, type);
        message->http.version.major = 1;
        message->http.version.minor = 1;
        zend_hash_init(&message->hdrs, 0, NULL, ZVAL_PTR_DTOR, 0);
 -      message->body = body ? body : php_http_message_body_init(NULL, NULL TSRMLS_CC);
 +      message->body = body ? body : php_http_message_body_init(NULL, NULL);
  
        return message;
  }
  
 -php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_http_message_type_t type TSRMLS_DC)
 +php_http_message_t *php_http_message_init_env(php_http_message_t *message, php_http_message_type_t type)
  {
        int free_msg = !message;
        zval *sval, tval;
        
        switch (type) {
                case PHP_HTTP_REQUEST:
 -                      mbody = php_http_env_get_request_body(TSRMLS_C);
 +                      mbody = php_http_env_get_request_body();
                        php_http_message_body_addref(mbody);
 -                      message = php_http_message_init(message, type, mbody TSRMLS_CC);
 -                      if ((sval = php_http_env_get_server_var(ZEND_STRL("SERVER_PROTOCOL"), 1 TSRMLS_CC)) && !strncmp(Z_STRVAL_P(sval), "HTTP/", lenof("HTTP/"))) {
 -                              php_http_version_parse(&message->http.version, Z_STRVAL_P(sval) TSRMLS_CC);
 +                      message = php_http_message_init(message, type, mbody);
 +                      if ((sval = php_http_env_get_server_var(ZEND_STRL("SERVER_PROTOCOL"), 1)) && !strncmp(Z_STRVAL_P(sval), "HTTP/", lenof("HTTP/"))) {
 +                              php_http_version_parse(&message->http.version, Z_STRVAL_P(sval));
                        }
 -                      if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1 TSRMLS_CC))) {
 +                      if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1))) {
                                message->http.info.request.method = estrdup(Z_STRVAL_P(sval));
                        }
 -                      if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1 TSRMLS_CC))) {
 -                              message->http.info.request.url = php_http_url_parse(Z_STRVAL_P(sval), Z_STRLEN_P(sval), ~0 TSRMLS_CC);
 +                      if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1))) {
 +                              message->http.info.request.url = php_http_url_parse(Z_STRVAL_P(sval), Z_STRLEN_P(sval), ~0);
                        }
                        
 -                      php_http_env_get_request_headers(&message->hdrs TSRMLS_CC);
 +                      php_http_env_get_request_headers(&message->hdrs);
                        break;
                        
                case PHP_HTTP_RESPONSE:
 -                      message = php_http_message_init(NULL, type, NULL TSRMLS_CC);
 -                      if (!SG(sapi_headers).http_status_line || !php_http_info_parse((php_http_info_t *) &message->http, SG(sapi_headers).http_status_line TSRMLS_CC)) {
 +                      message = php_http_message_init(NULL, type, NULL);
 +                      if (!SG(sapi_headers).http_status_line || !php_http_info_parse((php_http_info_t *) &message->http, SG(sapi_headers).http_status_line)) {
                                if (!(message->http.info.response.code = SG(sapi_headers).http_response_code)) {
                                        message->http.info.response.code = 200;
                                }
                                message->http.info.response.status = estrdup(php_http_env_get_response_status_for_code(message->http.info.response.code));
                        }
                        
 -                      php_http_env_get_response_headers(&message->hdrs TSRMLS_CC);
 -#if PHP_VERSION_ID >= 50400
 -                      if (php_output_get_level(TSRMLS_C)) {
 -                              if (php_output_get_status(TSRMLS_C) & PHP_OUTPUT_SENT) {
 -                                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(TSRMLS_C), php_output_get_start_lineno(TSRMLS_C));
 +                      php_http_env_get_response_headers(&message->hdrs);
 +                      if (php_output_get_level()) {
 +                              if (php_output_get_status() & PHP_OUTPUT_SENT) {
 +                                      php_error_docref(NULL, E_WARNING, "Could not fetch response body, output has already been sent at %s:%d", php_output_get_start_filename(TSRMLS_C), php_output_get_start_lineno(TSRMLS_C));
                                        goto error;
 -                              } else if (SUCCESS != php_output_get_contents(&tval TSRMLS_CC)) {
 -                                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch response body");
 +                              } else if (SUCCESS != php_output_get_contents(&tval)) {
 +                                      php_error_docref(NULL, E_WARNING, "Could not fetch response body");
                                        goto error;
                                } else {
                                        php_http_message_body_append(message->body, Z_STRVAL(tval), Z_STRLEN(tval));
                                        zval_dtor(&tval);
                                }
                        }
 -#else
 -                      if (OG(ob_nesting_level)) {
 -                              if (php_get_output_start_filename(TSRMLS_C)) {
 -                                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch response body, output has already been sent at %s:%d", php_get_output_start_filename(TSRMLS_C), php_get_output_start_lineno(TSRMLS_C));
 -                                      goto error;
 -                              } else if (SUCCESS != php_ob_get_buffer(&tval TSRMLS_CC)) {
 -                                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not fetch response body");
 -                                      goto error;
 -                              } else {
 -                                      php_http_message_body_append(message->body, Z_STRVAL(tval), Z_STRLEN(tval));
 -                                      zval_dtor(&tval);
 -                              }
 -                      }
 -#endif
                        break;
                        
                default:
        return message;
  }
  
 -php_http_message_t *php_http_message_parse(php_http_message_t *msg, const char *str, size_t len, zend_bool greedy TSRMLS_DC)
 +php_http_message_t *php_http_message_parse(php_http_message_t *msg, const char *str, size_t len, zend_bool greedy)
  {
        php_http_message_parser_t p;
        php_http_buffer_t buf;
        int free_msg;
  
        php_http_buffer_from_string_ex(&buf, str, len);
 -      php_http_message_parser_init(&p TSRMLS_CC);
 +      php_http_message_parser_init(&p);
  
        if ((free_msg = !msg)) {
 -              msg = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
 +              msg = php_http_message_init(NULL, 0, NULL);
        }
  
        if (greedy) {
        return msg;
  }
  
 -zval *php_http_message_header(php_http_message_t *msg, const char *key_str, size_t key_len, int join)
 +zval *php_http_message_header(php_http_message_t *msg, const char *key_str, size_t key_len)
  {
 -      zval *ret = NULL, **header;
 +      zval *ret;
        char *key;
        ALLOCA_FLAG(free_key);
  
        key = do_alloca(key_len + 1, free_key);
 +
        memcpy(key, key_str, key_len);
        key[key_len] = '\0';
        php_http_pretty_key(key, key_len, 1, 1);
  
 -      if (SUCCESS == zend_symtable_find(&msg->hdrs, key, key_len + 1, (void *) &header)) {
 -              if (join && Z_TYPE_PP(header) == IS_ARRAY) {
 -                      TSRMLS_FETCH_FROM_CTX(msg->ts);
 -
 -                      ret = php_http_header_value_to_string(*header TSRMLS_CC);
 -              } else {
 -                      Z_ADDREF_PP(header);
 -                      ret = *header;
 -              }
 -      }
 +      ret = zend_symtable_str_find(&msg->hdrs, key, key_len);
  
        free_alloca(key, free_key);
  
  
  zend_bool php_http_message_is_multipart(php_http_message_t *msg, char **boundary)
  {
 -      zval *ct = php_http_message_header(msg, ZEND_STRL("Content-Type"), 1);
 +      zend_string *ct = php_http_message_header_string(msg, ZEND_STRL("Content-Type"));
        zend_bool is_multipart = 0;
 -      TSRMLS_FETCH_FROM_CTX(msg->ts);
  
        if (ct) {
                php_http_params_opts_t popts;
  
                ZEND_INIT_SYMTABLE(&params);
                php_http_params_opts_default_get(&popts);
 -              popts.input.str = Z_STRVAL_P(ct);
 -              popts.input.len = Z_STRLEN_P(ct);
 +              popts.input.str = ct->val;
 +              popts.input.len = ct->len;
  
 -              if (php_http_params_parse(&params, &popts TSRMLS_CC)) {
 -                      zval **cur, **arg;
 -                      char *ct_str;
 +              if (php_http_params_parse(&params, &popts)) {
 +                      zval *cur, *arg;
 +                      zend_string *ct_str;
 +                      zend_ulong index;
  
                        zend_hash_internal_pointer_reset(&params);
  
 -                      if (SUCCESS == zend_hash_get_current_data(&params, (void *) &cur)
 -                      &&      Z_TYPE_PP(cur) == IS_ARRAY
 -                      &&      HASH_KEY_IS_STRING == zend_hash_get_current_key(&params, &ct_str, NULL, 0)
 +                      if ((cur = zend_hash_get_current_data(&params))
 +                      &&      (Z_TYPE_P(cur) == IS_ARRAY)
 +                      &&      (HASH_KEY_IS_STRING == zend_hash_get_current_key(&params, &ct_str, &index))
                        ) {
 -                              if (php_http_match(ct_str, "multipart", PHP_HTTP_MATCH_WORD)) {
 +                              if (php_http_match(ct_str->val, "multipart", PHP_HTTP_MATCH_WORD)) {
                                        is_multipart = 1;
  
                                        /* get boundary */
                                        if (boundary
 -                                      &&      SUCCESS == zend_hash_find(Z_ARRVAL_PP(cur), ZEND_STRS("arguments"), (void *) &arg)
 -                                      &&      Z_TYPE_PP(arg) == IS_ARRAY
 +                                      &&      (arg = zend_hash_str_find(Z_ARRVAL_P(cur), ZEND_STRL("arguments")))
 +                                      &&      Z_TYPE_P(arg) == IS_ARRAY
                                        ) {
 -                                              zval **val;
 -                                              HashPosition pos;
 -                                              php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 +                                              zval *val;
 +                                              php_http_arrkey_t key;
  
 -                                              FOREACH_KEYVAL(pos, *arg, key, val) {
 -                                                      if (key.type == HASH_KEY_IS_STRING && !strcasecmp(key.str, "boundary")) {
 -                                                              zval *bnd = php_http_ztyp(IS_STRING, *val);
 +                                              ZEND_HASH_FOREACH_KEY_VAL(Z_ARRVAL_P(arg), key.h, key.key, val)
 +                                              {
 +                                                      if (key.key && key.key->len == lenof("boundary") && !strcasecmp(key.key->val, "boundary")) {
 +                                                              zend_string *bnd = zval_get_string(val);
  
 -                                                              if (Z_STRLEN_P(bnd)) {
 -                                                                      *boundary = estrndup(Z_STRVAL_P(bnd), Z_STRLEN_P(bnd));
 +                                                              if (bnd->len) {
 +                                                                      *boundary = estrndup(bnd->val, bnd->len);
                                                                }
 -                                                              zval_ptr_dtor(&bnd);
 +                                                              zend_string_release(bnd);
                                                        }
                                                }
 +                                              ZEND_HASH_FOREACH_END();
                                        }
                                }
                        }
                }
                zend_hash_destroy(&params);
 -              zval_ptr_dtor(&ct);
 +              zend_string_release(ct);
        }
  
        return is_multipart;
@@@ -269,45 -292,52 +269,48 @@@ void php_http_message_set_info(php_http
  
  void php_http_message_update_headers(php_http_message_t *msg)
  {
 -      zval *h;
 +      zval h;
        size_t size;
 +      zend_string *cl;
  
        if (php_http_message_body_stream(msg->body)->readfilters.head) {
                /* if a read stream filter is attached to the body the caller must also care for the headers */
 -      } else if ((h = php_http_message_header(msg, ZEND_STRL("Content-Range"), 0))) {
++      } else if (php_http_message_header(msg, ZEND_STRL("Content-Range"))) {
+               /* don't mess around with a Content-Range message */
 -              zval_ptr_dtor(&h);
        } else if ((size = php_http_message_body_size(msg->body))) {
 -              MAKE_STD_ZVAL(h);
 -              ZVAL_LONG(h, size);
 -              zend_hash_update(&msg->hdrs, "Content-Length", sizeof("Content-Length"), &h, sizeof(zval *), NULL);
 +              ZVAL_LONG(&h, size);
 +              zend_hash_str_update(&msg->hdrs, "Content-Length", lenof("Content-Length"), &h);
  
                if (msg->body->boundary) {
                        char *str;
                        size_t len;
 +                      zend_string *ct;
  
 -                      if (!(h = php_http_message_header(msg, ZEND_STRL("Content-Type"), 1))) {
 +                      if (!(ct = php_http_message_header_string(msg, ZEND_STRL("Content-Type")))) {
                                len = spprintf(&str, 0, "multipart/form-data; boundary=\"%s\"", msg->body->boundary);
 -                              MAKE_STD_ZVAL(h);
 -                              ZVAL_STRINGL(h, str, len, 0);
 -                              zend_hash_update(&msg->hdrs, "Content-Type", sizeof("Content-Type"), &h, sizeof(zval *), NULL);
 -                      } else if (!php_http_match(Z_STRVAL_P(h), "boundary=", PHP_HTTP_MATCH_WORD)) {
 -                              zval_dtor(h);
 -                              Z_STRLEN_P(h) = spprintf(&Z_STRVAL_P(h), 0, "%s; boundary=\"%s\"", Z_STRVAL_P(h), msg->body->boundary);
 -                              zend_hash_update(&msg->hdrs, "Content-Type", sizeof("Content-Type"), &h, sizeof(zval *), NULL);
 +                              ZVAL_STR(&h, php_http_cs2zs(str, len));
 +                              zend_hash_str_update(&msg->hdrs, "Content-Type", lenof("Content-Type"), &h);
 +                      } else if (!php_http_match(ct->val, "boundary=", PHP_HTTP_MATCH_WORD)) {
 +                              len = spprintf(&str, 0, "%s; boundary=\"%s\"", ct->val, msg->body->boundary);
 +                              ZVAL_STR(&h, php_http_cs2zs(str, len));
 +                              zend_hash_str_update(&msg->hdrs, "Content-Type", lenof("Content-Type"), &h);
 +                              zend_string_release(ct);
                        } else {
 -                              zval_ptr_dtor(&h);
 +                              zend_string_release(ct);
                        }
                }
 -      } else if ((h = php_http_message_header(msg, ZEND_STRL("Content-Length"), 1))) {
 -              zval *h_cpy = php_http_ztyp(IS_LONG, h);
 -
 -              zval_ptr_dtor(&h);
 -              if (Z_LVAL_P(h_cpy)) {
 +      } else if ((cl = php_http_message_header_string(msg, ZEND_STRL("Content-Length")))) {
 +              if (!zend_string_equals_literal(cl, "0")) {
+                       /* body->size == 0, so get rid of old Content-Length */
 -                      zend_hash_del(&msg->hdrs, "Content-Length", sizeof("Content-Length"));
 +                      zend_hash_str_del(&msg->hdrs, ZEND_STRL("Content-Length"));
                }
 -              zval_ptr_dtor(&h_cpy);
 +              zend_string_release(cl);
        }
  }
  
  static void message_headers(php_http_message_t *msg, php_http_buffer_t *str)
  {
        char *tmp = NULL;
 -      TSRMLS_FETCH_FROM_CTX(msg->ts);
  
        switch (msg->type) {
                case PHP_HTTP_REQUEST:
        }
  
        php_http_message_update_headers(msg);
 -      php_http_header_to_string(str, &msg->hdrs TSRMLS_CC);
 +      php_http_header_to_string(str, &msg->hdrs);
  }
  
  void php_http_message_to_callback(php_http_message_t *msg, php_http_pass_callback_t cb, void *cb_arg)
@@@ -388,7 -418,9 +391,7 @@@ void php_http_message_serialize(php_htt
  
  php_http_message_t *php_http_message_reverse(php_http_message_t *msg)
  {
 -      int i, c = 0;
 -      
 -      php_http_message_count(c, msg);
 +      size_t i, c = php_http_message_count(msg);
        
        if (c > 1) {
                php_http_message_t *tmp = msg, **arr;
        return msg;
  }
  
 -php_http_message_t *php_http_message_zip(php_http_message_t *one, php_http_message_t *two)
 +php_http_message_t *php_http_message_zip(php_http_message_t *dst, php_http_message_t *src)
  {
 -      php_http_message_t *dst = php_http_message_copy(one, NULL), *src = php_http_message_copy(two, NULL), *tmp_dst, *tmp_src, *ret = dst;
 +      php_http_message_t *tmp_dst, *tmp_src, *ret = dst;
  
 -      while(dst && src) {
 +      while (dst && src) {
                tmp_dst = dst->parent;
                tmp_src = src->parent;
                dst->parent = src;
@@@ -432,22 -464,23 +435,22 @@@ php_http_message_t *php_http_message_co
  {
        php_http_message_t *temp, *copy = NULL;
        php_http_info_t info;
 -      TSRMLS_FETCH_FROM_CTX(from->ts);
        
        if (from) {
                info.type = from->type;
                info.http = from->http;
                
 -              copy = temp = php_http_message_init(to, 0, php_http_message_body_copy(from->body, NULL) TSRMLS_CC);
 +              copy = temp = php_http_message_init(to, 0, php_http_message_body_copy(from->body, NULL));
                php_http_message_set_info(temp, &info);
 -              zend_hash_copy(&temp->hdrs, &from->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
 +              zend_hash_copy(&temp->hdrs, &from->hdrs, (copy_ctor_func_t) zval_add_ref);
        
                if (parents) while (from->parent) {
                        info.type = from->parent->type;
                        info.http = from->parent->http;
                
 -                      temp->parent = php_http_message_init(NULL, 0, php_http_message_body_copy(from->parent->body, NULL) TSRMLS_CC);
 +                      temp->parent = php_http_message_init(NULL, 0, php_http_message_body_copy(from->parent->body, NULL));
                        php_http_message_set_info(temp->parent, &info);
 -                      zend_hash_copy(&temp->parent->hdrs, &from->parent->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
 +                      array_copy(&from->parent->hdrs, &temp->parent->hdrs);
                
                        temp = temp->parent;
                        from = from->parent;
        return copy;
  }
  
 -php_http_message_t *php_http_message_copy(php_http_message_t *from, php_http_message_t *to)
 -{
 -      return php_http_message_copy_ex(from, to, 1);
 -}
 -
  void php_http_message_dtor(php_http_message_t *message)
  {
        if (message) {
@@@ -491,154 -529,141 +494,154 @@@ void php_http_message_free(php_http_mes
        }
  }
  
 -static zval *php_http_message_object_read_prop(zval *object, zval *member, int type PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC);
 -static void php_http_message_object_write_prop(zval *object, zval *member, zval *value PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC);
 -static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC);
 +static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *rv);
 +static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot);
 +static HashTable *php_http_message_object_get_props(zval *object);
  
  static zend_object_handlers php_http_message_object_handlers;
  static HashTable php_http_message_object_prophandlers;
  
 -typedef void (*php_http_message_object_prophandler_func_t)(php_http_message_object_t *o, zval *v TSRMLS_DC);
 +static void php_http_message_object_prophandler_hash_dtor(zval *pData)
 +{
 +      pefree(Z_PTR_P(pData), 1);
 +}
 +
 +typedef void (*php_http_message_object_prophandler_func_t)(php_http_message_object_t *o, zval *v);
  
  typedef struct php_http_message_object_prophandler {
        php_http_message_object_prophandler_func_t read;
        php_http_message_object_prophandler_func_t write;
  } php_http_message_object_prophandler_t;
  
 -static STATUS php_http_message_object_add_prophandler(const char *prop_str, size_t prop_len, php_http_message_object_prophandler_func_t read, php_http_message_object_prophandler_func_t write) {
 +static ZEND_RESULT_CODE php_http_message_object_add_prophandler(const char *prop_str, size_t prop_len, php_http_message_object_prophandler_func_t read, php_http_message_object_prophandler_func_t write) {
        php_http_message_object_prophandler_t h = { read, write };
 -      return zend_hash_add(&php_http_message_object_prophandlers, prop_str, prop_len + 1, (void *) &h, sizeof(h), NULL);
 +      if (!zend_hash_str_add_mem(&php_http_message_object_prophandlers, prop_str, prop_len, (void *) &h, sizeof(h))) {
 +              return FAILURE;
 +      }
 +      return SUCCESS;
  }
 -static STATUS php_http_message_object_get_prophandler(const char *prop_str, size_t prop_len, php_http_message_object_prophandler_t **handler) {
 -      return zend_hash_find(&php_http_message_object_prophandlers, prop_str, prop_len + 1, (void *) handler);
 +static php_http_message_object_prophandler_t *php_http_message_object_get_prophandler(zend_string *name_str) {
 +      return zend_hash_str_find_ptr(&php_http_message_object_prophandlers, name_str->val, name_str->len);
  }
 -static void php_http_message_object_prophandler_get_type(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_get_type(php_http_message_object_t *obj, zval *return_value) {
        RETVAL_LONG(obj->message->type);
  }
 -static void php_http_message_object_prophandler_set_type(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
 -      zval *cpy = php_http_ztyp(IS_LONG, value);
 -      php_http_message_set_type(obj->message, Z_LVAL_P(cpy));
 -      zval_ptr_dtor(&cpy);
 +static void php_http_message_object_prophandler_set_type(php_http_message_object_t *obj, zval *value) {
 +      php_http_message_set_type(obj->message, zval_get_long(value));
  }
 -static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_get_request_method(php_http_message_object_t *obj, zval *return_value) {
        if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message) && obj->message->http.info.request.method) {
 -              RETVAL_STRING(obj->message->http.info.request.method, 1);
 +              RETVAL_STRING(obj->message->http.info.request.method);
        } else {
                RETVAL_NULL();
        }
  }
 -static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t *obj, zval *value) {
        if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
 -              zval *cpy = php_http_ztyp(IS_STRING, value);
 -              PTR_SET(obj->message->http.info.request.method, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
 -              zval_ptr_dtor(&cpy);
 +              zend_string *zs = zval_get_string(value);
 +              PTR_SET(obj->message->http.info.request.method, estrndup(zs->val, zs->len));
 +              zend_string_release(zs);
        }
  }
 -static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_get_request_url(php_http_message_object_t *obj, zval *return_value) {
        char *url_str;
        size_t url_len;
  
        if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message) && obj->message->http.info.request.url && php_http_url_to_string(obj->message->http.info.request.url, &url_str, &url_len, 0)) {
 -              RETVAL_STRINGL(url_str, url_len, 0);
 +              RETVAL_STR(php_http_cs2zs(url_str, url_len));
        } else {
                RETVAL_NULL();
        }
  }
 -static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t *obj, zval *value) {
        if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
 -              PTR_SET(obj->message->http.info.request.url, php_http_url_from_zval(value, ~0 TSRMLS_CC));
 +              PTR_SET(obj->message->http.info.request.url, php_http_url_from_zval(value, ~0));
        }
  }
 -static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_get_response_status(php_http_message_object_t *obj, zval *return_value) {
        if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message) && obj->message->http.info.response.status) {
 -              RETVAL_STRING(obj->message->http.info.response.status, 1);
 +              RETVAL_STRING(obj->message->http.info.response.status);
        } else {
                RETVAL_NULL();
        }
  }
 -static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t *obj, zval *value) {
        if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
 -              zval *cpy = php_http_ztyp(IS_STRING, value);
 -              PTR_SET(obj->message->http.info.response.status, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
 -              zval_ptr_dtor(&cpy);
 +              zend_string *zs = zval_get_string(value);
 +              PTR_SET(obj->message->http.info.response.status, estrndup(zs->val, zs->len));
 +              zend_string_release(zs);
        }
  }
 -static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_get_response_code(php_http_message_object_t *obj, zval *return_value) {
        if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
                RETVAL_LONG(obj->message->http.info.response.code);
        } else {
                RETVAL_NULL();
        }
  }
 -static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t *obj, zval *value) {
        if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
 -              zval *cpy = php_http_ztyp(IS_LONG, value);
 -              obj->message->http.info.response.code = Z_LVAL_P(cpy);
 +              obj->message->http.info.response.code = zval_get_long(value);
                PTR_SET(obj->message->http.info.response.status, estrdup(php_http_env_get_response_status_for_code(obj->message->http.info.response.code)));
 -              zval_ptr_dtor(&cpy);
        }
  }
 -static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_get_http_version(php_http_message_object_t *obj, zval *return_value) {
        char *version_str;
        size_t version_len;
  
 -      php_http_version_to_string(&obj->message->http.version, &version_str, &version_len, NULL, NULL TSRMLS_CC);
 -      RETVAL_STRINGL(version_str, version_len, 0);
 +      php_http_version_to_string(&obj->message->http.version, &version_str, &version_len, NULL, NULL);
 +      RETVAL_STR(php_http_cs2zs(version_str, version_len));
  }
 -static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
 -      zval *cpy = php_http_ztyp(IS_STRING, value);
 -      php_http_version_parse(&obj->message->http.version, Z_STRVAL_P(cpy) TSRMLS_CC);
 -      zval_ptr_dtor(&cpy);
 +static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t *obj, zval *value) {
 +      zend_string *zs = zval_get_string(value);
 +      php_http_version_parse(&obj->message->http.version, zs->val);
 +      zend_string_release(zs);
  }
 -static void php_http_message_object_prophandler_get_headers(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_get_headers(php_http_message_object_t *obj, zval *return_value ) {
        array_init(return_value);
 -      zend_hash_copy(Z_ARRVAL_P(return_value), &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
 +      array_copy(&obj->message->hdrs, Z_ARRVAL_P(return_value));
  }
 -static void php_http_message_object_prophandler_set_headers(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
 -      zval *cpy = php_http_ztyp(IS_ARRAY, value);
 +static void php_http_message_object_prophandler_set_headers(php_http_message_object_t *obj, zval *value) {
 +      HashTable *headers;
 +      zval *orig_value = value;
 +
 +      if (Z_TYPE_P(value) != IS_ARRAY && Z_TYPE_P(value) != IS_OBJECT) {
 +              convert_to_array_ex(value);
 +      }
 +      headers = HASH_OF(value);
  
        zend_hash_clean(&obj->message->hdrs);
 -      zend_hash_copy(&obj->message->hdrs, Z_ARRVAL_P(cpy), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
 -      zval_ptr_dtor(&cpy);
 +      array_copy(headers, &obj->message->hdrs);
 +
 +      if (orig_value != value) {
 +              zval_ptr_dtor(value);
 +      }
  }
 -static void php_http_message_object_prophandler_get_body(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_get_body(php_http_message_object_t *obj, zval *return_value) {
        if (obj->body) {
 -              RETVAL_OBJVAL(obj->body->zv, 1);
 +              RETVAL_OBJECT(&obj->body->zo, 1);
        } else {
                RETVAL_NULL();
        }
  }
 -static void php_http_message_object_prophandler_set_body(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
 -      php_http_message_object_set_body(obj, value TSRMLS_CC);
 +static void php_http_message_object_prophandler_set_body(php_http_message_object_t *obj, zval *value) {
 +      php_http_message_object_set_body(obj, value);
  }
 -static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
 +static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t *obj, zval *return_value) {
        if (obj->message->parent) {
 -              RETVAL_OBJVAL(obj->parent->zv, 1);
 +              RETVAL_OBJECT(&obj->parent->zo, 1);
        } else {
                RETVAL_NULL();
        }
  }
 -static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
 -      if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_http_message_class_entry TSRMLS_CC)) {
 -              php_http_message_object_t *parent_obj = zend_object_store_get_object(value TSRMLS_CC);
 +static void php_http_message_object_prophandler_set_parent_message(php_http_message_object_t *obj, zval *value) {
 +      if (Z_TYPE_P(value) == IS_OBJECT && instanceof_function(Z_OBJCE_P(value), php_http_message_class_entry)) {
 +              php_http_message_object_t *parent_obj = PHP_HTTP_OBJ(NULL, value);
  
                if (obj->message->parent) {
 -                      zend_objects_store_del_ref_by_handle(obj->parent->zv.handle TSRMLS_CC);
 +                      zend_objects_store_del(&obj->parent->zo);
                }
 -              Z_OBJ_ADDREF_P(value);
 +              Z_ADDREF_P(value);
                obj->parent = parent_obj;
                obj->message->parent = parent_obj->message;
        }
  #define PHP_HTTP_MESSAGE_OBJECT_INIT(obj) \
        do { \
                if (!obj->message) { \
 -                      obj->message = php_http_message_init(NULL, 0, NULL TSRMLS_CC); \
 +                      obj->message = php_http_message_init(NULL, 0, NULL); \
                } \
        } while(0)
  
  
 -void php_http_message_object_reverse(zval *this_ptr, zval *return_value TSRMLS_DC)
 +void php_http_message_object_reverse(zval *zmsg, zval *return_value)
  {
 -      int i = 0;
 -      php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      size_t i;
 +      php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, zmsg);
  
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
        /* count */
 -      php_http_message_count(i, obj->message);
 +      i = php_http_message_count(obj->message);
  
        if (i > 1) {
                php_http_message_object_t **objects;
                objects[0]->parent = NULL;
  
                /* add ref, because we previously have not been a parent message */
 -              Z_OBJ_ADDREF_P(getThis());
 -              RETVAL_OBJVAL(objects[last]->zv, 0);
 +              Z_ADDREF_P(zmsg);
 +              RETVAL_OBJECT(&objects[last]->zo, 1);
  
                efree(objects);
        } else {
 -              RETURN_ZVAL(getThis(), 1, 0);
 +              RETURN_ZVAL_FAST(zmsg);
        }
  }
  
 -void php_http_message_object_prepend(zval *this_ptr, zval *prepend, zend_bool top TSRMLS_DC)
 +void php_http_message_object_prepend(zval *this_ptr, zval *prepend, zend_bool top)
  {
 -      zval m;
        php_http_message_t *save_parent_msg = NULL;
 -      php_http_message_object_t *save_parent_obj = NULL, *obj = zend_object_store_get_object(this_ptr TSRMLS_CC);
 -      php_http_message_object_t *prepend_obj = zend_object_store_get_object(prepend TSRMLS_CC);
 -
 -      INIT_PZVAL(&m);
 -      m.type = IS_OBJECT;
 +      php_http_message_object_t *save_parent_obj = NULL, *obj = PHP_HTTP_OBJ(NULL, this_ptr);
 +      php_http_message_object_t *prepend_obj = PHP_HTTP_OBJ(NULL, prepend);
  
        if (!top) {
                save_parent_obj = obj->parent;
        obj->message->parent = prepend_obj->message;
  
        /* add ref */
 -      zend_objects_store_add_ref(prepend TSRMLS_CC);
 +      Z_ADDREF_P(prepend);
  
        if (!top) {
                prepend_obj->parent = save_parent_obj;
        }
  }
  
 -STATUS php_http_message_object_set_body(php_http_message_object_t *msg_obj, zval *zbody TSRMLS_DC)
 +ZEND_RESULT_CODE php_http_message_object_set_body(php_http_message_object_t *msg_obj, zval *zbody)
  {
 -      zval *tmp = NULL;
        php_stream *s;
 -      zend_object_value ov;
 +      zend_string *body_str;
        php_http_message_body_t *body;
        php_http_message_body_object_t *body_obj;
  
        switch (Z_TYPE_P(zbody)) {
                case IS_RESOURCE:
 -                      php_stream_from_zval_no_verify(s, &zbody);
 +                      php_stream_from_zval_no_verify(s, zbody);
                        if (!s) {
                                php_http_throw(unexpected_val, "The stream is not a valid resource", NULL);
                                return FAILURE;
  
                        is_resource:
  
 -                      body = php_http_message_body_init(NULL, s TSRMLS_CC);
 -                      if (SUCCESS != php_http_new(&ov, php_http_message_body_class_entry, (php_http_new_t) php_http_message_body_object_new_ex, NULL, body, NULL TSRMLS_CC)) {
 +                      body = php_http_message_body_init(NULL, s);
 +                      if (!(body_obj = php_http_message_body_object_new_ex(php_http_message_body_class_entry, body))) {
                                php_http_message_body_free(&body);
                                return FAILURE;
                        }
 -                      MAKE_STD_ZVAL(tmp);
 -                      ZVAL_OBJVAL(tmp, ov, 0);
 -                      zbody = tmp;
                        break;
  
                case IS_OBJECT:
 -                      if (instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry TSRMLS_CC)) {
 -                              Z_OBJ_ADDREF_P(zbody);
 +                      if (instanceof_function(Z_OBJCE_P(zbody), php_http_message_body_class_entry)) {
 +                              Z_ADDREF_P(zbody);
 +                              body_obj = PHP_HTTP_OBJ(NULL, zbody);
                                break;
                        }
                        /* no break */
  
                default:
 -                      tmp = php_http_ztyp(IS_STRING, zbody);
 +                      body_str = zval_get_string(zbody);
                        s = php_stream_temp_new();
 -                      php_stream_write(s, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
 -                      zval_ptr_dtor(&tmp);
 -                      tmp = NULL;
 +                      php_stream_write(s, body_str->val, body_str->len);
 +                      zend_string_release(body_str);
                        goto is_resource;
  
        }
  
 -      body_obj = zend_object_store_get_object(zbody TSRMLS_CC);
        if (!body_obj->body) {
 -              body_obj->body = php_http_message_body_init(NULL, NULL TSRMLS_CC);
 +              body_obj->body = php_http_message_body_init(NULL, NULL);
        }
        if (msg_obj->body) {
 -              zend_objects_store_del_ref_by_handle(msg_obj->body->zv.handle TSRMLS_CC);
 +              zend_objects_store_del(&msg_obj->body->zo);
        }
        if (msg_obj->message) {
                php_http_message_body_free(&msg_obj->message->body);
 -              msg_obj->message->body = php_http_message_body_init(&body_obj->body, NULL TSRMLS_CC);
 +              msg_obj->message->body = php_http_message_body_init(&body_obj->body, NULL);
        } else {
 -              msg_obj->message = php_http_message_init(NULL, 0, php_http_message_body_init(&body_obj->body, NULL TSRMLS_CC) TSRMLS_CC);
 +              msg_obj->message = php_http_message_init(NULL, 0, php_http_message_body_init(&body_obj->body, NULL));
        }
        msg_obj->body = body_obj;
  
 -      if (tmp) {
 -              FREE_ZVAL(tmp);
 -      }
        return SUCCESS;
  }
  
 -STATUS php_http_message_object_init_body_object(php_http_message_object_t *obj)
 +ZEND_RESULT_CODE php_http_message_object_init_body_object(php_http_message_object_t *obj)
  {
 -      TSRMLS_FETCH_FROM_CTX(obj->message->ts);
 -
        php_http_message_body_addref(obj->message->body);
 -      return php_http_new(NULL, php_http_message_body_class_entry, (php_http_new_t) php_http_message_body_object_new_ex, NULL, obj->message->body, (void *) &obj->body TSRMLS_CC);
 +      return php_http_new((void *) &obj->body, php_http_message_body_class_entry, (php_http_new_t) php_http_message_body_object_new_ex, NULL, obj->message->body);
  }
  
 -zend_object_value php_http_message_object_new(zend_class_entry *ce TSRMLS_DC)
 +zend_object *php_http_message_object_new(zend_class_entry *ce)
  {
 -      return php_http_message_object_new_ex(ce, NULL, NULL TSRMLS_CC);
 +      return &php_http_message_object_new_ex(ce, NULL)->zo;
  }
  
 -zend_object_value php_http_message_object_new_ex(zend_class_entry *ce, php_http_message_t *msg, php_http_message_object_t **ptr TSRMLS_DC)
 +php_http_message_object_t *php_http_message_object_new_ex(zend_class_entry *ce, php_http_message_t *msg)
  {
        php_http_message_object_t *o;
  
 -      o = ecalloc(1, sizeof(php_http_message_object_t));
 -      zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
 -      object_properties_init((zend_object *) o, ce);
 -
 -      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);
  
        if (msg) {
                o->message = msg;
                if (msg->parent) {
 -                      php_http_message_object_new_ex(ce, msg->parent, &o->parent TSRMLS_CC);
 +                      o->parent = php_http_message_object_new_ex(ce, msg->parent);
                }
 -              php_http_message_body_object_new_ex(php_http_message_body_class_entry, php_http_message_body_init(&msg->body, NULL TSRMLS_CC), &o->body TSRMLS_CC);
 +              o->body = php_http_message_body_object_new_ex(php_http_message_body_class_entry, php_http_message_body_init(&msg->body, NULL));
        }
  
 -      o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_object_free, NULL TSRMLS_CC);
 -      o->zv.handlers = &php_http_message_object_handlers;
 +      o->zo.handlers = &php_http_message_object_handlers;
  
 -      return o->zv;
 +      return o;
  }
  
 -zend_object_value php_http_message_object_clone(zval *this_ptr TSRMLS_DC)
 +zend_object *php_http_message_object_clone(zval *this_ptr TSRMLS_DC)
  {
 -      zend_object_value new_ov;
        php_http_message_object_t *new_obj = NULL;
 -      php_http_message_object_t *old_obj = zend_object_store_get_object(this_ptr TSRMLS_CC);
 +      php_http_message_object_t *old_obj = PHP_HTTP_OBJ(NULL, this_ptr);
  
 -      new_ov = php_http_message_object_new_ex(old_obj->zo.ce, php_http_message_copy(old_obj->message, NULL), &new_obj TSRMLS_CC);
 -      zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(this_ptr) TSRMLS_CC);
 +      new_obj = php_http_message_object_new_ex(old_obj->zo.ce, php_http_message_copy(old_obj->message, NULL));
 +      zend_objects_clone_members(&new_obj->zo, &old_obj->zo);
  
 -      return new_ov;
 +      return &new_obj->zo;
  }
  
 -void php_http_message_object_free(void *object TSRMLS_DC)
 +void php_http_message_object_free(zend_object *object)
  {
 -      php_http_message_object_t *o = (php_http_message_object_t *) object;
 +      php_http_message_object_t *o = PHP_HTTP_OBJ(object, NULL);
  
 -      if (o->iterator) {
 +      if (!Z_ISUNDEF(o->iterator)) {
                zval_ptr_dtor(&o->iterator);
 -              o->iterator = NULL;
 +              ZVAL_UNDEF(&o->iterator);
        }
        if (o->message) {
                /* do NOT free recursivly */
                o->message = NULL;
        }
        if (o->parent) {
 -              zend_objects_store_del_ref_by_handle(o->parent->zv.handle TSRMLS_CC);
 +              zend_objects_store_del(&o->parent->zo);
                o->parent = NULL;
        }
        if (o->body) {
 -              zend_objects_store_del_ref_by_handle(o->body->zv.handle TSRMLS_CC);
 +              zend_objects_store_del(&o->body->zo);
                o->body = NULL;
        }
 -      zend_object_std_dtor((zend_object *) o TSRMLS_CC);
 -      efree(o);
 +      zend_object_std_dtor(object);
  }
  
 -static zval *php_http_message_object_read_prop(zval *object, zval *member, int type PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC)
 +static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, void **cache_slot, zval *tmp)
  {
 -      php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
 -      php_http_message_object_prophandler_t *handler;
 -      zval *return_value, *copy = php_http_ztyp(IS_STRING, member);
 +      zval *return_value;
 +      zend_string *member_name = zval_get_string(member);
 +      php_http_message_object_prophandler_t *handler = php_http_message_object_get_prophandler(member_name);
  
 -      PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
 +      if (!handler || type == BP_VAR_R || type == BP_VAR_IS) {
 +              return_value = zend_get_std_object_handlers()->read_property(object, member, type, cache_slot, tmp);
  
 -      if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
 -              ALLOC_ZVAL(return_value);
 -              Z_SET_REFCOUNT_P(return_value, 0);
 -              Z_UNSET_ISREF_P(return_value);
 +              if (handler) {
 +                      php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
  
 -              if (type == BP_VAR_R) {
 -                      handler->read(obj, return_value TSRMLS_CC);
 -              } else {
 -                      php_property_proxy_t *proxy = php_property_proxy_init(object, Z_STRVAL_P(copy), Z_STRLEN_P(copy) TSRMLS_CC);
 -                      RETVAL_OBJVAL(php_property_proxy_object_new_ex(php_property_proxy_get_class_entry(), proxy, NULL TSRMLS_CC), 0);
 +                      PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
 +                      handler->read(obj, tmp);
 +
 +                      zval_ptr_dtor(return_value);
 +                      ZVAL_COPY_VALUE(return_value, tmp);
                }
        } else {
 -              return_value = zend_get_std_object_handlers()->read_property(object, member, type PHP_HTTP_ZEND_LITERAL_CC TSRMLS_CC);
 +              return_value = php_property_proxy_zval(object, member_name);
        }
  
 -      zval_ptr_dtor(&copy);
 +      zend_string_release(member_name);
  
        return return_value;
  }
  
 -static void php_http_message_object_write_prop(zval *object, zval *member, zval *value PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC)
 +static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, void **cache_slot)
  {
 -      php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
 +      php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
        php_http_message_object_prophandler_t *handler;
 -      zval *copy = php_http_ztyp(IS_STRING, member);
 +      zend_string *member_name = zval_get_string(member);
  
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
 -      if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
 -              handler->write(obj, value TSRMLS_CC);
 +      if ((handler = php_http_message_object_get_prophandler(member_name))) {
 +              handler->write(obj, value);
        } else {
 -              zend_get_std_object_handlers()->write_property(object, member, value PHP_HTTP_ZEND_LITERAL_CC TSRMLS_CC);
 +              zend_get_std_object_handlers()->write_property(object, member, value, cache_slot);
        }
  
 -      zval_ptr_dtor(&copy);
 +      zend_string_release(member_name);
  }
  
 -static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC)
 +static HashTable *php_http_message_object_get_props(zval *object)
  {
 -      zval *headers;
 -      php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
 -      HashTable *props = zend_get_std_object_handlers()->get_properties(object TSRMLS_CC);
 -      zval array, *parent, *body;
 +      zval tmp;
 +      php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, object);
 +      HashTable *props = zend_get_std_object_handlers()->get_properties(object);
        char *ver_str, *url_str = NULL;
        size_t ver_len, url_len = 0;
  
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
 -      INIT_PZVAL_ARRAY(&array, props);
        
 -#define ASSOC_PROP(ptype, n, val) \
 -      do { \
 -              zend_property_info *pi; \
 -              if (SUCCESS == zend_hash_find(&obj->zo.ce->properties_info, n, sizeof(n), (void *) &pi)) { \
 -                      add_assoc_ ##ptype## _ex(&array, pi->name, pi->name_length + 1, val); \
 -              } \
 -      } while(0) \
 -
 -#define ASSOC_STRING(name, val) ASSOC_STRINGL(name, val, strlen(val))
 -#define ASSOC_STRINGL(name, val, len) ASSOC_STRINGL_EX(name, val, len, 1)
 -#define ASSOC_STRINGL_EX(n, val, len, cpy) \
 +#define UPDATE_PROP(name_str, action_with_tmp) \
        do { \
                zend_property_info *pi; \
 -              if (SUCCESS == zend_hash_find(&obj->zo.ce->properties_info, n, sizeof(n), (void *) &pi)) { \
 -                      add_assoc_stringl_ex(&array, pi->name, pi->name_length + 1, val, len, cpy); \
 +              if ((pi = zend_hash_str_find_ptr(&obj->zo.ce->properties_info, name_str, lenof(name_str)))) { \
 +                      action_with_tmp; \
 +                      zend_hash_update(props, pi->name, &tmp); \
                } \
        } while(0)
  
 -      ASSOC_PROP(long, "type", obj->message->type);
 +      UPDATE_PROP("type", ZVAL_LONG(&tmp, obj->message->type));
 +
        ver_len = spprintf(&ver_str, 0, "%u.%u", obj->message->http.version.major, obj->message->http.version.minor);
 -      ASSOC_STRINGL_EX("httpVersion", ver_str, ver_len, 0);
 +      UPDATE_PROP("httpVersion", ZVAL_STR(&tmp, php_http_cs2zs(ver_str, ver_len)));
  
        switch (obj->message->type) {
                case PHP_HTTP_REQUEST:
 -                      ASSOC_PROP(long, "responseCode", 0);
 -                      ASSOC_STRINGL("responseStatus", "", 0);
 -                      ASSOC_STRING("requestMethod", STR_PTR(obj->message->http.info.request.method));
 +                      UPDATE_PROP("responseCode", ZVAL_LONG(&tmp, 0));
 +                      UPDATE_PROP("responseStatus", ZVAL_EMPTY_STRING(&tmp));
 +                      UPDATE_PROP("requestMethod", ZVAL_STRING(&tmp, STR_PTR(obj->message->http.info.request.method)));
                        if (obj->message->http.info.request.url) {
                                php_http_url_to_string(obj->message->http.info.request.url, &url_str, &url_len, 0);
 -                              ASSOC_STRINGL_EX("requestUrl", url_str, url_len, 0);
 +                              UPDATE_PROP("requestUrl", ZVAL_STR(&tmp, php_http_cs2zs(url_str, url_len)));
                        } else {
 -                              ASSOC_STRINGL("requestUrl", "", 0);
 +                              UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp));
                        }
  
                        break;
  
                case PHP_HTTP_RESPONSE:
 -                      ASSOC_PROP(long, "responseCode", obj->message->http.info.response.code);
 -                      ASSOC_STRING("responseStatus", STR_PTR(obj->message->http.info.response.status));
 -                      ASSOC_STRINGL("requestMethod", "", 0);
 -                      ASSOC_STRINGL("requestUrl", "", 0);
 +                      UPDATE_PROP("responseCode", ZVAL_LONG(&tmp, obj->message->http.info.response.code));
 +                      UPDATE_PROP("responseStatus", ZVAL_STRING(&tmp, STR_PTR(obj->message->http.info.response.status)));
 +                      UPDATE_PROP("requestMethod", ZVAL_EMPTY_STRING(&tmp));
 +                      UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp));
                        break;
  
                case PHP_HTTP_NONE:
                default:
 -                      ASSOC_PROP(long, "responseCode", 0);
 -                      ASSOC_STRINGL("responseStatus", "", 0);
 -                      ASSOC_STRINGL("requestMethod", "", 0);
 -                      ASSOC_STRINGL("requestUrl", "", 0);
 +                      UPDATE_PROP("responseCode", ZVAL_LONG(&tmp, 0));
 +                      UPDATE_PROP("responseStatus", ZVAL_EMPTY_STRING(&tmp));
 +                      UPDATE_PROP("requestMethod", ZVAL_EMPTY_STRING(&tmp));
 +                      UPDATE_PROP("requestUrl", ZVAL_EMPTY_STRING(&tmp));
                        break;
        }
  
 -      MAKE_STD_ZVAL(headers);
 -      array_init(headers);
 -      zend_hash_copy(Z_ARRVAL_P(headers), &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
 -      ASSOC_PROP(zval, "headers", headers);
 +      UPDATE_PROP("headers",
 +                      array_init(&tmp);
 +                      array_copy(&obj->message->hdrs, Z_ARRVAL(tmp));
 +      );
  
 -      MAKE_STD_ZVAL(body);
 -      if (obj->body) {
 -              ZVAL_OBJVAL(body, obj->body->zv, 1);
 -      } else {
 -              ZVAL_NULL(body);
 -      }
 -      ASSOC_PROP(zval, "body", body);
 +      UPDATE_PROP("body",
 +                      if (obj->body) {
 +                              ZVAL_OBJECT(&tmp, &obj->body->zo, 1);
 +                      } else {
 +                              ZVAL_NULL(&tmp);
 +                      }
 +      );
  
 -      MAKE_STD_ZVAL(parent);
 -      if (obj->message->parent) {
 -              ZVAL_OBJVAL(parent, obj->parent->zv, 1);
 -      } else {
 -              ZVAL_NULL(parent);
 -      }
 -      ASSOC_PROP(zval, "parentMessage", parent);
 +      UPDATE_PROP("parentMessage",
 +                      if (obj->message->parent) {
 +                              ZVAL_OBJECT(&tmp, &obj->parent->zo, 1);
 +                      } else {
 +                              ZVAL_NULL(&tmp);
 +                      }
 +      );
  
        return props;
  }
@@@ -981,22 -1038,22 +984,22 @@@ static PHP_METHOD(HttpMessage, __constr
        zend_bool greedy = 1;
        zval *zmessage = NULL;
        php_http_message_t *msg = NULL;
 -      php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
        zend_error_handling zeh;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!b", &zmessage, &greedy), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!b", &zmessage, &greedy), invalid_arg, return);
  
 -      zend_replace_error_handling(EH_THROW, php_http_exception_bad_message_class_entry, &zeh TSRMLS_CC);
 +      zend_replace_error_handling(EH_THROW, php_http_exception_bad_message_class_entry, &zeh);
        if (zmessage && Z_TYPE_P(zmessage) == IS_RESOURCE) {
                php_stream *s;
                php_http_message_parser_t p;
                zend_error_handling zeh;
  
 -              zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh TSRMLS_CC);
 -              php_stream_from_zval(s, &zmessage);
 -              zend_restore_error_handling(&zeh TSRMLS_CC);
 +              zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh);
 +              php_stream_from_zval(s, zmessage);
 +              zend_restore_error_handling(&zeh);
  
 -              if (s && php_http_message_parser_init(&p TSRMLS_CC)) {
 +              if (s && php_http_message_parser_init(&p)) {
                        unsigned flags = (greedy ? PHP_HTTP_MESSAGE_PARSER_GREEDY : 0);
                        php_http_buffer_t buf;
  
                        php_http_throw(bad_message, "Empty message received from stream", NULL);
                }
        } else if (zmessage) {
 -              zmessage = php_http_ztyp(IS_STRING, zmessage);
 -              msg = php_http_message_parse(NULL, Z_STRVAL_P(zmessage), Z_STRLEN_P(zmessage), greedy TSRMLS_CC);
 +              zend_string *zs_msg = zval_get_string(zmessage);
 +
 +              msg = php_http_message_parse(NULL, zs_msg->val, zs_msg->len, greedy);
  
                if (!msg && !EG(exception)) {
 -                      php_http_throw(bad_message, "Could not parse message: %.*s", MIN(25, Z_STRLEN_P(zmessage)), Z_STRVAL_P(zmessage));
 +                      php_http_throw(bad_message, "Could not parse message: %.*s", MIN(25, zs_msg->len), zs_msg->val);
                }
 -              zval_ptr_dtor(&zmessage);
 +              zend_string_release(zs_msg);
        }
  
        if (msg) {
                php_http_message_dtor(obj->message);
                obj->message = msg;
                if (obj->message->parent) {
 -                      php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, &obj->parent TSRMLS_CC);
 +                      obj->parent = php_http_message_object_new_ex(obj->zo.ce, obj->message->parent);
                }
        }
 -      zend_restore_error_handling(&zeh TSRMLS_CC);
 +      zend_restore_error_handling(&zeh);
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  }
  
@@@ -1043,7 -1099,8 +1046,7 @@@ static PHP_METHOD(HttpMessage, getBody
  
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
        if (!obj->body) {
  
        }
        if (obj->body) {
 -              RETVAL_OBJVAL(obj->body->zv, 1);
 +              RETVAL_OBJECT(&obj->body->zo, 1);
        }
  }
  
@@@ -1062,13 -1119,13 +1065,13 @@@ static PHP_METHOD(HttpMessage, setBody
  {
        zval *zbody;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zbody, php_http_message_body_class_entry)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zbody, php_http_message_body_class_entry)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
 -              php_http_message_object_prophandler_set_body(obj, zbody TSRMLS_CC);
 +              php_http_message_object_prophandler_set_body(obj, zbody);
        }
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addBody, 0, 0, 1)
@@@ -1078,14 -1135,14 +1081,14 @@@ static PHP_METHOD(HttpMessage, addBody
  {
        zval *new_body;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &new_body, php_http_message_body_class_entry)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -              php_http_message_body_object_t *new_obj = zend_object_store_get_object(new_body TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &new_body, php_http_message_body_class_entry)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
 +              php_http_message_body_object_t *new_obj = PHP_HTTP_OBJ(NULL, new_body);
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
                php_http_message_body_to_callback(new_obj->body, (php_http_pass_callback_t) php_http_message_body_append, obj->message->body, 0, 0);
        }
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHeader, 0, 0, 1)
@@@ -1095,36 -1152,39 +1098,36 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, getHeader)
  {
        char *header_str;
 -      int header_len;
 +      size_t header_len;
        zend_class_entry *header_ce = NULL;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|C!", &header_str, &header_len, &header_ce)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s|C!", &header_str, &header_len, &header_ce)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                zval *header;
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
 -              if ((header = php_http_message_header(obj->message, header_str, header_len, 0))) {
 +              if ((header = php_http_message_header(obj->message, header_str, header_len))) {
                        if (!header_ce) {
 -                              RETURN_ZVAL(header, 1, 1);
 -                      } else if (instanceof_function(header_ce, php_http_header_class_entry TSRMLS_CC)) {
 +                              RETURN_ZVAL_FAST(header);
 +                      } else if (instanceof_function(header_ce, php_http_header_class_entry)) {
                                php_http_object_method_t cb;
 -                              zval *header_name, **argv[2];
 +                              zval argv[2];
  
 -                              MAKE_STD_ZVAL(header_name);
 -                              ZVAL_STRINGL(header_name, header_str, header_len, 1);
 -
 -                              argv[0] = &header_name;
 -                              argv[1] = &header;
 +                              ZVAL_STRINGL(&argv[0], header_str, header_len);
 +                              ZVAL_COPY(&argv[1], header);
  
                                object_init_ex(return_value, header_ce);
 -                              php_http_object_method_init(&cb, return_value, ZEND_STRL("__construct") TSRMLS_CC);
 -                              php_http_object_method_call(&cb, return_value, NULL, 2, argv TSRMLS_CC);
 +                              php_http_object_method_init(&cb, return_value, ZEND_STRL("__construct"));
 +                              php_http_object_method_call(&cb, return_value, NULL, 2, argv);
                                php_http_object_method_dtor(&cb);
  
 -                              zval_ptr_dtor(&header_name);
 -                              zval_ptr_dtor(&header);
 +                              zval_ptr_dtor(&argv[0]);
 +                              zval_ptr_dtor(&argv[1]);
  
                                return;
                        } else {
 -                              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Class '%s' is not as descendant of http\\Header", header_ce->name);
 +                              php_error_docref(NULL, E_WARNING, "Class '%s' is not as descendant of http\\Header", header_ce->name->val);
                        }
                }
        }
@@@ -1136,7 -1196,7 +1139,7 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, getHeaders)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
@@@ -1153,23 -1213,23 +1156,23 @@@ static PHP_METHOD(HttpMessage, setHeade
  {
        zval *zvalue = NULL;
        char *name_str;
 -      int name_len;
 +      size_t name_len;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|z!", &name_str, &name_len, &zvalue)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s|z!", &name_str, &name_len, &zvalue)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
                if (!zvalue) {
 -                      zend_symtable_del(&obj->message->hdrs, name, name_len + 1);
 +                      zend_symtable_str_del(&obj->message->hdrs, name, name_len);
                } else {
 -                      Z_ADDREF_P(zvalue);
 -                      zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
 +                      Z_TRY_ADDREF_P(zvalue);
 +                      zend_symtable_str_update(&obj->message->hdrs, name, name_len, zvalue);
                }
                efree(name);
        }
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_setHeaders, 0, 0, 1)
@@@ -1179,8 -1239,8 +1182,8 @@@ static PHP_METHOD(HttpMessage, setHeade
  {
        zval *new_headers = NULL;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a/!", &new_headers)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "a/!", &new_headers)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
                        array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, 0, ARRAY_JOIN_PRETTIFY|ARRAY_JOIN_STRONLY);
                }
        }
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeader, 0, 0, 2)
@@@ -1200,25 -1260,26 +1203,25 @@@ static PHP_METHOD(HttpMessage, addHeade
  {
        zval *zvalue;
        char *name_str;
 -      int name_len;
 +      size_t name_len;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sz", &name_str, &name_len, &zvalue)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "sz", &name_str, &name_len, &zvalue)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                char *name = php_http_pretty_key(estrndup(name_str, name_len), name_len, 1, 1);
                zval *header;
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
 -              Z_ADDREF_P(zvalue);
 -              if ((header = php_http_message_header(obj->message, name, name_len, 0))) {
 +              Z_TRY_ADDREF_P(zvalue);
 +              if ((header = php_http_message_header(obj->message, name, name_len))) {
                        convert_to_array(header);
 -                      zend_hash_next_index_insert(Z_ARRVAL_P(header), &zvalue, sizeof(void *), NULL);
 -                      zval_ptr_dtor(&header);
 +                      zend_hash_next_index_insert(Z_ARRVAL_P(header), zvalue);
                } else {
 -                      zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
 +                      zend_symtable_str_update(&obj->message->hdrs, name, name_len, zvalue);
                }
                efree(name);
        }
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_addHeaders, 0, 0, 1)
@@@ -1230,14 -1291,14 +1233,14 @@@ static PHP_METHOD(HttpMessage, addHeade
        zval *new_headers;
        zend_bool append = 0;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "a|b", &new_headers, &append)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "a|b", &new_headers, &append)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
                array_join(Z_ARRVAL_P(new_headers), &obj->message->hdrs, append, ARRAY_JOIN_STRONLY|ARRAY_JOIN_PRETTIFY);
        }
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getType, 0, 0, 0)
@@@ -1245,7 -1306,7 +1248,7 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, getType)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
@@@ -1258,16 -1319,16 +1261,16 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_s
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpMessage, setType)
  {
 -      long type;
 +      zend_long type;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l", &type)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "l", &type)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
                php_http_message_set_type(obj->message, type);
        }
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getInfo, 0, 0, 0)
@@@ -1275,27 -1336,26 +1278,27 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, getInfo)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              char *tmp = NULL;
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              char *str, *tmp = NULL;
 +              size_t len;
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
                switch (obj->message->type) {
                        case PHP_HTTP_REQUEST:
 -                              Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj->message->http, tmp, ""));
 +                              len = spprintf(&str, 0, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&obj->message->http, tmp, ""));
                                PTR_FREE(tmp);
                                break;
                        case PHP_HTTP_RESPONSE:
 -                              Z_STRLEN_P(return_value) = spprintf(&Z_STRVAL_P(return_value), 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj->message->http, tmp, ""));
 +                              len = spprintf(&str, 0, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&obj->message->http, tmp, ""));
                                PTR_FREE(tmp);
                                break;
                        default:
                                RETURN_NULL();
                                break;
                }
 -              Z_TYPE_P(return_value) = IS_STRING;
 -              return;
 +
 +              RETVAL_STR(php_http_cs2zs(str, len));
        }
  }
  
@@@ -1305,16 -1365,16 +1308,16 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, setInfo)
  {
        char *str;
 -      int len;
 +      size_t len;
        php_http_message_object_t *obj;
        php_http_info_t inf;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str, &len), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
 -      if (!php_http_info_parse(&inf, str TSRMLS_CC)) {
 +      if (!php_http_info_parse(&inf, str)) {
                php_http_throw(bad_header, "Could not parse message info '%s'", str);
                return;
        }
        php_http_message_set_info(obj->message, &inf);
        php_http_info_dtor(&inf);
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getHttpVersion, 0, 0, 0)
@@@ -1332,12 -1392,12 +1335,12 @@@ static PHP_METHOD(HttpMessage, getHttpV
        if (SUCCESS == zend_parse_parameters_none()) {
                char *str;
                size_t len;
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
 -              php_http_version_to_string(&obj->message->http.version, &str, &len, NULL, NULL TSRMLS_CC);
 -              RETURN_STRINGL(str, len, 0);
 +              php_http_version_to_string(&obj->message->http.version, &str, &len, NULL, NULL);
 +              RETURN_STR(php_http_cs2zs(str, len));
        }
  }
  
@@@ -1347,20 -1407,20 +1350,20 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, setHttpVersion)
  {
        char *v_str;
 -      int v_len;
 +      size_t v_len;
        php_http_version_t version;
        php_http_message_object_t *obj;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &v_str, &v_len), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &v_str, &v_len), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
 -      php_http_expect(php_http_version_parse(&version, v_str TSRMLS_CC), unexpected_val, return);
 +      php_http_expect(php_http_version_parse(&version, v_str), unexpected_val, return);
  
        obj->message->http.version = version;
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseCode, 0, 0, 0)
@@@ -1368,12 -1428,12 +1371,12 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, getResponseCode)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
                if (obj->message->type != PHP_HTTP_RESPONSE) {
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "http\\Message is not if type response");
 +                      php_error_docref(NULL, E_WARNING, "http\\Message is not if type response");
                        RETURN_FALSE;
                }
  
@@@ -1387,13 -1447,14 +1390,13 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_s
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpMessage, setResponseCode)
  {
 -      long code;
 +      zend_long code;
        zend_bool strict = 1;
        php_http_message_object_t *obj;
  
        php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "l|b", &code, &strict), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
        if (obj->message->type != PHP_HTTP_RESPONSE) {
        obj->message->http.info.response.code = code;
        PTR_SET(obj->message->http.info.response.status, estrdup(php_http_env_get_response_status_for_code(code)));
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getResponseStatus, 0, 0, 0)
@@@ -1417,16 -1478,16 +1420,16 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, getResponseStatus)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
                if (obj->message->type != PHP_HTTP_RESPONSE) {
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "http\\Message is not of type response");
 +                      php_error_docref(NULL, E_WARNING, "http\\Message is not of type response");
                }
  
                if (obj->message->http.info.response.status) {
 -                      RETURN_STRING(obj->message->http.info.response.status, 1);
 +                      RETURN_STRING(obj->message->http.info.response.status);
                } else {
                        RETURN_EMPTY_STRING();
                }
@@@ -1439,12 -1500,12 +1442,12 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, setResponseStatus)
  {
        char *status;
 -      int status_len;
 +      size_t status_len;
        php_http_message_object_t *obj;
  
        php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &status, &status_len), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
  
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
        }
  
        PTR_SET(obj->message->http.info.response.status, estrndup(status, status_len));
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestMethod, 0, 0, 0)
@@@ -1461,17 -1522,17 +1464,17 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, getRequestMethod)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
                if (obj->message->type != PHP_HTTP_REQUEST) {
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "http\\Message is not of type request");
 +                      php_error_docref(NULL, E_WARNING, "http\\Message is not of type request");
                        RETURN_FALSE;
                }
  
                if (obj->message->http.info.request.method) {
 -                      RETURN_STRING(obj->message->http.info.request.method, 1);
 +                      RETURN_STRING(obj->message->http.info.request.method);
                } else {
                        RETURN_EMPTY_STRING();
                }
@@@ -1484,12 -1545,12 +1487,12 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, setRequestMethod)
  {
        char *method;
 -      int method_len;
 +      size_t method_len;
        php_http_message_object_t *obj;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &method, &method_len), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &method, &method_len), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
  
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
        }
  
        PTR_SET(obj->message->http.info.request.method, estrndup(method, method_len));
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getRequestUrl, 0, 0, 0)
@@@ -1512,12 -1573,12 +1515,12 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, getRequestUrl)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
                if (obj->message->type != PHP_HTTP_REQUEST) {
 -                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "http\\Message is not of type request");
 +                      php_error_docref(NULL, E_WARNING, "http\\Message is not of type request");
                        RETURN_FALSE;
                }
  
                        size_t url_len;
  
                        php_http_url_to_string(obj->message->http.info.request.url, &url_str, &url_len, 0);
 -                      RETURN_STRINGL(url_str, url_len, 0);
 +                      RETURN_STR(php_http_cs2zs(url_str, url_len));
                } else {
                        RETURN_EMPTY_STRING();
                }
@@@ -1543,9 -1604,9 +1546,9 @@@ static PHP_METHOD(HttpMessage, setReque
        php_http_message_object_t *obj;
        zend_error_handling zeh;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "z", &zurl), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "z", &zurl), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
  
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
                return;
        }
  
 -      zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh TSRMLS_CC);
 -      url = php_http_url_from_zval(zurl, ~0 TSRMLS_CC);
 -      zend_restore_error_handling(&zeh TSRMLS_CC);
 +      zend_replace_error_handling(EH_THROW, php_http_exception_bad_url_class_entry, &zeh);
 +      url = php_http_url_from_zval(zurl, ~0);
 +      zend_restore_error_handling(&zeh);
  
        if (php_http_url_is_empty(url)) {
                php_http_url_free(&url);
                PTR_SET(obj->message->http.info.request.url, url);
        }
  
 -      RETVAL_ZVAL(getThis(), 1, 0);
 +      RETVAL_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_getParentMessage, 0, 0, 0)
@@@ -1576,7 -1637,8 +1579,7 @@@ static PHP_METHOD(HttpMessage, getParen
  
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
        if (!obj->message->parent) {
                return;
        }
  
 -      RETVAL_OBJVAL(obj->parent->zv, 1);
 +      RETVAL_OBJECT(&obj->parent->zo, 1);
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage___toString, 0, 0, 0)
@@@ -1596,8 -1658,8 +1599,8 @@@ static PHP_METHOD(HttpMessage, toString
  {
        zend_bool include_parent = 0;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|b", &include_parent)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|b", &include_parent)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                char *string;
                size_t length;
  
                        php_http_message_to_string(obj->message, &string, &length);
                }
                if (string) {
 -                      RETURN_STRINGL(string, length, 0);
 +                      RETURN_STR(php_http_cs2zs(string, length));
                }
        }
        RETURN_EMPTY_STRING();
  }
  
 -#ifdef ZTS
 -static size_t write_to_stream(void *s, const char *str, size_t len)
 -{
 -      TSRMLS_FETCH();
 -      return php_stream_write(s, str, len);
 -}
 -#else
 -#     define write_to_stream (php_http_pass_callback_t)_php_stream_write
 -#endif
 -
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_toStream, 0, 0, 1)
        ZEND_ARG_INFO(0, stream)
  ZEND_END_ARG_INFO();
@@@ -1622,14 -1694,14 +1625,14 @@@ static PHP_METHOD(HttpMessage, toStream
  {
        zval *zstream;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "r", &zstream)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                php_stream *s;
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
 -              php_stream_from_zval(s, &zstream);
 -              php_http_message_to_callback(obj->message, write_to_stream, s);
 +              php_stream_from_zval(s, zstream);
 +              php_http_message_to_callback(obj->message, (php_http_pass_callback_t) _php_stream_write, s);
        }
  }
  
@@@ -1640,17 -1712,20 +1643,17 @@@ static PHP_METHOD(HttpMessage, toCallba
  {
        php_http_pass_fcall_arg_t fcd;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f", &fcd.fci, &fcd.fcc)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "f", &fcd.fci, &fcd.fcc)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
 -              fcd.fcz = getThis();
 -              Z_ADDREF_P(fcd.fcz);
 -              TSRMLS_SET_CTX(fcd.ts);
 -
 +              ZVAL_COPY(&fcd.fcz, getThis());
                php_http_message_to_callback(obj->message, php_http_pass_fcall_callback, &fcd);
                zend_fcall_info_args_clear(&fcd.fci, 1);
 -
                zval_ptr_dtor(&fcd.fcz);
 -              RETURN_ZVAL(getThis(), 1, 0);
 +
 +              RETURN_ZVAL_FAST(getThis());
        }
  }
  
@@@ -1659,14 -1734,14 +1662,14 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, serialize)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                char *string;
                size_t length;
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
                php_http_message_serialize(obj->message, &string, &length);
 -              RETURN_STRINGL(string, length, 0);
 +              RETURN_STR(php_http_cs2zs(string, length));
        }
        RETURN_EMPTY_STRING();
  }
@@@ -1676,22 -1751,21 +1679,22 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_u
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpMessage, unserialize)
  {
 -      int length;
 +      size_t length;
        char *serialized;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &serialized, &length)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &serialized, &length)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                php_http_message_t *msg;
  
                if (obj->message) {
 +                      /* do not free recursively */
                        php_http_message_dtor(obj->message);
                        efree(obj->message);
                }
 -              if ((msg = php_http_message_parse(NULL, serialized, (size_t) length, 1 TSRMLS_CC))) {
 +              if ((msg = php_http_message_parse(NULL, serialized, length, 1))) {
                        obj->message = msg;
                } else {
 -                      obj->message = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
 +                      obj->message = php_http_message_init(NULL, 0, NULL);
                        php_error_docref(NULL TSRMLS_CC, E_ERROR, "Could not unserialize http\\Message");
                }
        }
@@@ -1701,18 -1775,15 +1704,18 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_d
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpMessage, detach)
  {
 -      php_http_message_object_t *obj;
 +      php_http_message_object_t *obj, *new_obj;
 +      php_http_message_t *msg_cpy;
  
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
 -      RETVAL_OBJVAL(php_http_message_object_new_ex(obj->zo.ce, php_http_message_copy_ex(obj->message, NULL, 0), NULL TSRMLS_CC), 0);
 +      msg_cpy = php_http_message_copy_ex(obj->message, NULL, 0);
 +      new_obj = php_http_message_object_new_ex(obj->zo.ce, msg_cpy);
 +
 +      RETVAL_OBJ(&new_obj->zo);
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_prepend, 0, 0, 1)
@@@ -1726,11 -1797,11 +1729,11 @@@ static PHP_METHOD(HttpMessage, prepend
        php_http_message_t *msg[2];
        php_http_message_object_t *obj, *prepend_obj;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O|b", &prepend, php_http_message_class_entry, &top), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O|b", &prepend, php_http_message_class_entry, &top), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
 -      prepend_obj = zend_object_store_get_object(prepend TSRMLS_CC);
 +      prepend_obj = PHP_HTTP_OBJ(NULL, prepend);
        PHP_HTTP_MESSAGE_OBJECT_INIT(prepend_obj);
  
        /* safety check */
                }
        }
  
 -      php_http_message_object_prepend(getThis(), prepend, top TSRMLS_CC);
 -      RETURN_ZVAL(getThis(), 1, 0);
 +      php_http_message_object_prepend(getThis(), prepend, top);
 +      RETURN_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_reverse, 0, 0, 0)
@@@ -1753,7 -1824,7 +1756,7 @@@ static PHP_METHOD(HttpMessage, reverse
  {
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
  
 -      php_http_message_object_reverse(getThis(), return_value TSRMLS_CC);
 +      php_http_message_object_reverse(getThis(), return_value);
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_isMultipart, 0, 0, 0)
@@@ -1763,22 -1834,17 +1766,22 @@@ static PHP_METHOD(HttpMessage, isMultip
  {
        zval *zboundary = NULL;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z", &zboundary)) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|z!", &zboundary)) {
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                char *boundary = NULL;
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
 -              RETVAL_BOOL(php_http_message_is_multipart(obj->message, zboundary ? &boundary : NULL));
 +              if (php_http_message_is_multipart(obj->message, zboundary ? &boundary : NULL)) {
 +                      RETVAL_TRUE;
 +              } else {
 +                      RETVAL_FALSE;
 +              }
  
                if (zboundary && boundary) {
 +                      ZVAL_DEREF(zboundary);
                        zval_dtor(zboundary);
 -                      ZVAL_STRING(zboundary, boundary, 0);
 +                      ZVAL_STR(zboundary, php_http_cs2zs(boundary, strlen(boundary)));
                }
        }
  }
@@@ -1793,7 -1859,8 +1796,7 @@@ static PHP_METHOD(HttpMessage, splitMul
  
        php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
        PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
        if (!php_http_message_is_multipart(obj->message, &boundary)) {
  
        PTR_FREE(boundary);
  
 -      RETURN_OBJVAL(php_http_message_object_new_ex(php_http_message_class_entry, msg, NULL TSRMLS_CC), 0);
 +      RETURN_OBJ(&php_http_message_object_new_ex(obj->zo.ce, msg)->zo);
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessage_count, 0, 0, 0)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpMessage, count)
  {
 -      long count_mode = -1;
 +      zend_long count_mode = -1;
  
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|l", &count_mode)) {
 -              long i = 0;
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_OBJECT_INIT(obj);
  
 -              php_http_message_count(i, obj->message);
 -              RETURN_LONG(i);
 +              RETURN_LONG(php_http_message_count(obj->message));
        }
  }
  
@@@ -1829,12 -1898,13 +1832,12 @@@ static PHP_METHOD(HttpMessage, rewind
  {
        if (SUCCESS == zend_parse_parameters_none()) {
                zval *zobj = getThis();
 -              php_http_message_object_t *obj = zend_object_store_get_object(zobj TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
 -              if (obj->iterator) {
 +              if (!Z_ISUNDEF(obj->iterator)) {
                        zval_ptr_dtor(&obj->iterator);
                }
 -              Z_ADDREF_P(zobj);
 -              obj->iterator = zobj;
 +              ZVAL_COPY(&obj->iterator, zobj);
        }
  }
  
@@@ -1843,9 -1913,9 +1846,9 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, valid)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
 -              RETURN_BOOL(obj->iterator != NULL);
 +              RETURN_BOOL(!Z_ISUNDEF(obj->iterator));
        }
  }
  
@@@ -1854,20 -1924,19 +1857,20 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, next)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
 -              if (obj->iterator) {
 -                      php_http_message_object_t *itr = zend_object_store_get_object(obj->iterator TSRMLS_CC);
 +              if (!Z_ISUNDEF(obj->iterator)) {
 +                      php_http_message_object_t *itr = PHP_HTTP_OBJ(NULL, &obj->iterator);
  
 -                      if (itr && itr->parent) {
 -                              zval *old = obj->iterator;
 -                              MAKE_STD_ZVAL(obj->iterator);
 -                              ZVAL_OBJVAL(obj->iterator, itr->parent->zv, 1);
 -                              zval_ptr_dtor(&old);
 +                      if (itr->parent) {
 +                              zval tmp;
 +
 +                              ZVAL_OBJECT(&tmp, &itr->parent->zo, 1);
 +                              zval_ptr_dtor(&obj->iterator);
 +                              obj->iterator = tmp;
                        } else {
                                zval_ptr_dtor(&obj->iterator);
 -                              obj->iterator = NULL;
 +                              ZVAL_UNDEF(&obj->iterator);
                        }
                }
        }
@@@ -1878,9 -1947,9 +1881,9 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, key)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
 -              RETURN_LONG(obj->iterator ? obj->iterator->value.obj.handle:0);
 +              RETURN_LONG(Z_ISUNDEF(obj->iterator) ? 0 : Z_OBJ_HANDLE(obj->iterator));
        }
  }
  
@@@ -1889,10 -1958,10 +1892,10 @@@ ZEND_END_ARG_INFO()
  static PHP_METHOD(HttpMessage, current)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
 -              if (obj->iterator) {
 -                      RETURN_ZVAL(obj->iterator, 1, 0);
 +              if (!Z_ISUNDEF(obj->iterator)) {
 +                      RETURN_ZVAL_FAST(&obj->iterator);
                }
        }
  }
@@@ -1960,42 -2029,40 +1963,42 @@@ PHP_MINIT_FUNCTION(http_message
        zend_class_entry ce = {0};
  
        INIT_NS_CLASS_ENTRY(ce, "http", "Message", php_http_message_methods);
 -      php_http_message_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
 +      php_http_message_class_entry = zend_register_internal_class(&ce);
        php_http_message_class_entry->create_object = php_http_message_object_new;
        memcpy(&php_http_message_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
 +      php_http_message_object_handlers.offset = XtOffsetOf(php_http_message_object_t, zo);
        php_http_message_object_handlers.clone_obj = php_http_message_object_clone;
 +      php_http_message_object_handlers.free_obj = php_http_message_object_free;
        php_http_message_object_handlers.read_property = php_http_message_object_read_prop;
        php_http_message_object_handlers.write_property = php_http_message_object_write_prop;
        php_http_message_object_handlers.get_properties = php_http_message_object_get_props;
        php_http_message_object_handlers.get_property_ptr_ptr = NULL;
  
 -      zend_class_implements(php_http_message_class_entry TSRMLS_CC, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);
 +      zend_class_implements(php_http_message_class_entry, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator);
  
 -      zend_hash_init(&php_http_message_object_prophandlers, 9, NULL, NULL, 1);
 -      zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("type"), PHP_HTTP_NONE, ZEND_ACC_PROTECTED TSRMLS_CC);
 +      zend_hash_init(&php_http_message_object_prophandlers, 9, NULL, php_http_message_object_prophandler_hash_dtor, 1);
 +      zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("type"), PHP_HTTP_NONE, ZEND_ACC_PROTECTED);
        php_http_message_object_add_prophandler(ZEND_STRL("type"), php_http_message_object_prophandler_get_type, php_http_message_object_prophandler_set_type);
 -      zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("body"), ZEND_ACC_PROTECTED TSRMLS_CC);
 +      zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("body"), ZEND_ACC_PROTECTED);
        php_http_message_object_add_prophandler(ZEND_STRL("body"), php_http_message_object_prophandler_get_body, php_http_message_object_prophandler_set_body);
 -      zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
 +      zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestMethod"), "", ZEND_ACC_PROTECTED);
        php_http_message_object_add_prophandler(ZEND_STRL("requestMethod"), php_http_message_object_prophandler_get_request_method, php_http_message_object_prophandler_set_request_method);
 -      zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
 +      zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("requestUrl"), "", ZEND_ACC_PROTECTED);
        php_http_message_object_add_prophandler(ZEND_STRL("requestUrl"), php_http_message_object_prophandler_get_request_url, php_http_message_object_prophandler_set_request_url);
 -      zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED TSRMLS_CC);
 +      zend_declare_property_string(php_http_message_class_entry, ZEND_STRL("responseStatus"), "", ZEND_ACC_PROTECTED);
        php_http_message_object_add_prophandler(ZEND_STRL("responseStatus"), php_http_message_object_prophandler_get_response_status, php_http_message_object_prophandler_set_response_status);
 -      zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED TSRMLS_CC);
 +      zend_declare_property_long(php_http_message_class_entry, ZEND_STRL("responseCode"), 0, ZEND_ACC_PROTECTED);
        php_http_message_object_add_prophandler(ZEND_STRL("responseCode"), php_http_message_object_prophandler_get_response_code, php_http_message_object_prophandler_set_response_code);
 -      zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED TSRMLS_CC);
 +      zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("httpVersion"), ZEND_ACC_PROTECTED);
        php_http_message_object_add_prophandler(ZEND_STRL("httpVersion"), php_http_message_object_prophandler_get_http_version, php_http_message_object_prophandler_set_http_version);
 -      zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("headers"), ZEND_ACC_PROTECTED TSRMLS_CC);
 +      zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("headers"), ZEND_ACC_PROTECTED);
        php_http_message_object_add_prophandler(ZEND_STRL("headers"), php_http_message_object_prophandler_get_headers, php_http_message_object_prophandler_set_headers);
 -      zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED TSRMLS_CC);
 +      zend_declare_property_null(php_http_message_class_entry, ZEND_STRL("parentMessage"), ZEND_ACC_PROTECTED);
        php_http_message_object_add_prophandler(ZEND_STRL("parentMessage"), php_http_message_object_prophandler_get_parent_message, php_http_message_object_prophandler_set_parent_message);
  
 -      zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE TSRMLS_CC);
 +      zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_NONE"), PHP_HTTP_NONE);
 +      zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_REQUEST"), PHP_HTTP_REQUEST);
 +      zend_declare_class_constant_long(php_http_message_class_entry, ZEND_STRL("TYPE_RESPONSE"), PHP_HTTP_RESPONSE);
  
        return SUCCESS;
  }
diff --combined php_http_message_body.c
index 1c540c77389dba84e58b1040128ae040579cfec8,fa9155110d776d37a0e1e26bed4ec5ef627acee6..4a3174a62a785c649d563ad7281874435d97e88f
  #define BOUNDARY_CLOSE(body) \
                php_http_message_body_appendf(body, PHP_HTTP_CRLF "--%s--" PHP_HTTP_CRLF, php_http_message_body_boundary(body))
  
 -static STATUS add_recursive_fields(php_http_message_body_t *body, const char *name, zval *value);
 -static STATUS add_recursive_files(php_http_message_body_t *body, const char *name, zval *value);
 +static ZEND_RESULT_CODE add_recursive_fields(php_http_message_body_t *body, const char *name, HashTable *fields);
 +static ZEND_RESULT_CODE add_recursive_files(php_http_message_body_t *body, const char *name, HashTable *files);
  
 -php_http_message_body_t *php_http_message_body_init(php_http_message_body_t **body_ptr, php_stream *stream TSRMLS_DC)
 +php_http_message_body_t *php_http_message_body_init(php_http_message_body_t **body_ptr, php_stream *stream)
  {
        php_http_message_body_t *body;
  
        body->refcount = 1;
  
        if (stream) {
 -              php_stream_auto_cleanup(stream);
 -              body->stream_id = php_stream_get_resource_id(stream);
 -              zend_list_addref(body->stream_id);
 +              body->res = stream->res;
 +              ++GC_REFCOUNT(body->res);
        } else {
                stream = php_stream_temp_create(TEMP_STREAM_DEFAULT, 0xffff);
 -              php_stream_auto_cleanup(stream);
 -              body->stream_id = php_stream_get_resource_id(stream);
 +              body->res = stream->res;
        }
 -      TSRMLS_SET_CTX(body->ts);
 +      php_stream_auto_cleanup(stream);
  
        if (body_ptr) {
                *body_ptr = body;
@@@ -68,10 -70,12 +68,10 @@@ unsigned php_http_message_body_addref(p
  php_http_message_body_t *php_http_message_body_copy(php_http_message_body_t *from, php_http_message_body_t *to)
  {
        if (from) {
 -              TSRMLS_FETCH_FROM_CTX(from->ts);
 -              
                if (to) {
                        php_stream_truncate_set_size(php_http_message_body_stream(to), 0);
                } else {
 -                      to = php_http_message_body_init(NULL, NULL TSRMLS_CC);
 +                      to = php_http_message_body_init(NULL, NULL);
                }
                php_http_message_body_to_stream(from, php_http_message_body_stream(to), 0, 0);
  
@@@ -93,8 -97,9 +93,8 @@@ void php_http_message_body_free(php_htt
                php_http_message_body_t *body = *body_ptr;
  
                if (!--body->refcount) {
 -                      TSRMLS_FETCH_FROM_CTX(body->ts);
                        /* NOFIXME: shows leakinfo in DEBUG mode */
 -                      zend_list_delete(body->stream_id);
 +                      zend_list_delete(body->res);
                        PTR_FREE(body->boundary);
                        efree(body);
                }
  
  const php_stream_statbuf *php_http_message_body_stat(php_http_message_body_t *body)
  {
 -      TSRMLS_FETCH_FROM_CTX(body->ts);
        php_stream_stat(php_http_message_body_stream(body), &body->ssb);
        return &body->ssb;
  }
@@@ -112,8 -118,9 +112,8 @@@ const char *php_http_message_body_bound
  {
        if (!body->boundary) {
                union { double dbl; int num[2]; } data;
 -              TSRMLS_FETCH_FROM_CTX(body->ts);
  
--              data.dbl = php_combined_lcg(TSRMLS_C);
++              data.dbl = php_combined_lcg();
                spprintf(&body->boundary, 0, "%x.%x", data.num[0], data.num[1]);
        }
        return body->boundary;
  
  char *php_http_message_body_etag(php_http_message_body_t *body)
  {
-       const php_stream_statbuf *ssb = php_http_message_body_stat(body);
+       php_http_etag_t *etag;
+       php_stream *s = php_http_message_body_stream(body);
 -      TSRMLS_FETCH_FROM_CTX(body->ts);
  
        /* real file or temp buffer ? */
-       if (ssb->sb.st_mtime) {
-               char *etag;
+       if (s->ops != &php_stream_temp_ops && s->ops != &php_stream_memory_ops) {
+               php_stream_stat(php_http_message_body_stream(body), &body->ssb);
  
-               spprintf(&etag, 0, "%lx-%lx-%lx", ssb->sb.st_ino, ssb->sb.st_mtime, ssb->sb.st_size);
-               return etag;
-       } else {
-               php_http_etag_t *etag = php_http_etag_init(PHP_HTTP_G->env.etag_mode);
+               if (body->ssb.sb.st_mtime) {
+                       char *etag;
  
-               if (etag) {
-                       php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_etag_update, etag, 0, 0);
-                       return php_http_etag_finish(etag);
-               } else {
-                       return NULL;
+                       spprintf(&etag, 0, "%lx-%lx-%lx", body->ssb.sb.st_ino, body->ssb.sb.st_mtime, body->ssb.sb.st_size);
+                       return etag;
                }
        }
 -      if ((etag = php_http_etag_init(PHP_HTTP_G->env.etag_mode TSRMLS_CC))) {
+       /* content based */
++      if ((etag = php_http_etag_init(PHP_HTTP_G->env.etag_mode))) {
+               php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_etag_update, etag, 0, 0);
+               return php_http_etag_finish(etag);
+       }
+       return NULL;
  }
  
 -void php_http_message_body_to_string(php_http_message_body_t *body, char **buf, size_t *len, off_t offset, size_t forlen)
 +zend_string *php_http_message_body_to_string(php_http_message_body_t *body, off_t offset, size_t forlen)
  {
        php_stream *s = php_http_message_body_stream(body);
 -      TSRMLS_FETCH_FROM_CTX(body->ts);
  
        php_stream_seek(s, offset, SEEK_SET);
        if (!forlen) {
                forlen = -1;
        }
 -      *len = php_stream_copy_to_mem(s, buf, forlen, 0);
 +      return php_stream_copy_to_mem(s, forlen, 0);
  }
  
 -STATUS php_http_message_body_to_stream(php_http_message_body_t *body, php_stream *dst, off_t offset, size_t forlen)
 +ZEND_RESULT_CODE php_http_message_body_to_stream(php_http_message_body_t *body, php_stream *dst, off_t offset, size_t forlen)
  {
        php_stream *s = php_http_message_body_stream(body);
 -      TSRMLS_FETCH_FROM_CTX(body->ts);
  
        php_stream_seek(s, offset, SEEK_SET);
  
        return php_stream_copy_to_stream_ex(s, dst, forlen, NULL);
  }
  
 -STATUS php_http_message_body_to_callback(php_http_message_body_t *body, php_http_pass_callback_t cb, void *cb_arg, off_t offset, size_t forlen)
 +ZEND_RESULT_CODE php_http_message_body_to_callback(php_http_message_body_t *body, php_http_pass_callback_t cb, void *cb_arg, off_t offset, size_t forlen)
  {
        php_stream *s = php_http_message_body_stream(body);
        char *buf = emalloc(0x1000);
 -      TSRMLS_FETCH_FROM_CTX(body->ts);
  
        php_stream_seek(s, offset, SEEK_SET);
  
@@@ -200,6 -215,7 +204,6 @@@ size_t php_http_message_body_append(php
  {
        php_stream *s;
        size_t written;
 -      TSRMLS_FETCH_FROM_CTX(body->ts);
  
        if (!(s = php_http_message_body_stream(body))) {
                return -1;
        written = php_stream_write(s, buf, len);
  
        if (written != len) {
--              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Failed to append %zu bytes to body; wrote %zu", len, written);
++              php_error_docref(NULL, E_WARNING, "Failed to append %zu bytes to body; wrote %zu", len, written);
        }
  
        return len;
@@@ -234,15 -250,19 +238,15 @@@ size_t php_http_message_body_appendf(ph
        return print_len;
  }
  
 -STATUS php_http_message_body_add_form(php_http_message_body_t *body, HashTable *fields, HashTable *files)
 +ZEND_RESULT_CODE php_http_message_body_add_form(php_http_message_body_t *body, HashTable *fields, HashTable *files)
  {
 -      zval tmp;
 -
        if (fields) {
 -              INIT_PZVAL_ARRAY(&tmp, fields);
 -              if (SUCCESS != add_recursive_fields(body, NULL, &tmp)) {
 +              if (SUCCESS != add_recursive_fields(body, NULL, fields)) {
                        return FAILURE;
                }
        }
        if (files) {
 -              INIT_PZVAL_ARRAY(&tmp, files);
 -              if (SUCCESS != add_recursive_files(body, NULL, &tmp)) {
 +              if (SUCCESS != add_recursive_files(body, NULL, files)) {
                        return FAILURE;
                }
        }
  
  void php_http_message_body_add_part(php_http_message_body_t *body, php_http_message_t *part)
  {
 -      TSRMLS_FETCH_FROM_CTX(body->ts);
 -
        BOUNDARY_OPEN(body);
        php_http_message_to_callback(part, (php_http_pass_callback_t) php_http_message_body_append, body);
        BOUNDARY_CLOSE(body);
  }
  
  
 -STATUS php_http_message_body_add_form_field(php_http_message_body_t *body, const char *name, const char *value_str, size_t value_len)
 +ZEND_RESULT_CODE php_http_message_body_add_form_field(php_http_message_body_t *body, const char *name, const char *value_str, size_t value_len)
  {
 -      char *safe_name;
 -      TSRMLS_FETCH_FROM_CTX(body->ts);
 +      zend_string *safe_name = zend_string_init(name, strlen(name), 0);
  
 -      safe_name = php_addslashes(estrdup(name), strlen(name), NULL, 1 TSRMLS_CC);
 +      safe_name = php_addslashes(safe_name, 1);
  
        BOUNDARY_OPEN(body);
        php_http_message_body_appendf(
                body,
                "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
                "" PHP_HTTP_CRLF,
 -              safe_name
 +              safe_name->val
        );
        php_http_message_body_append(body, value_str, value_len);
        BOUNDARY_CLOSE(body);
  
 -      efree(safe_name);
 +      zend_string_release(safe_name);
        return SUCCESS;
  }
  
 -STATUS php_http_message_body_add_form_file(php_http_message_body_t *body, const char *name, const char *ctype, const char *path, php_stream *in)
 +ZEND_RESULT_CODE php_http_message_body_add_form_file(php_http_message_body_t *body, const char *name, const char *ctype, const char *path, php_stream *in)
  {
 -      char *safe_name, *path_dup = estrdup(path), *bname;
 -      size_t bname_len;
 -      TSRMLS_FETCH_FROM_CTX(body->ts);
 +      size_t path_len = strlen(path);
 +      char *path_dup = estrndup(path, path_len);
 +      zend_string *base_name, *safe_name = zend_string_init(name, strlen(name), 0);
  
 -      safe_name = php_addslashes(estrdup(name), strlen(name), NULL, 1 TSRMLS_CC);
 -      
 -      php_basename(path_dup, strlen(path_dup), NULL, 0, &bname, &bname_len TSRMLS_CC); 
 +      safe_name = php_addslashes(safe_name, 1);
 +      base_name = php_basename(path_dup, path_len, NULL, 0);
  
        BOUNDARY_OPEN(body);
        php_http_message_body_appendf(
                "Content-Transfer-Encoding: binary" PHP_HTTP_CRLF
                "Content-Type: %s" PHP_HTTP_CRLF
                PHP_HTTP_CRLF,
 -              safe_name, bname, ctype
 +              safe_name->val, base_name->val, ctype
        );
        php_stream_copy_to_stream_ex(in, php_http_message_body_stream(body), PHP_STREAM_COPY_ALL, NULL);
        BOUNDARY_CLOSE(body);
  
 -      efree(safe_name);
 +      zend_string_release(safe_name);
 +      zend_string_release(base_name);
        efree(path_dup);
 -      efree(bname);
  
        return SUCCESS;
  }
  
 -static inline char *format_key(uint type, char *str, ulong num, const char *prefix) {
 +static inline char *format_key(php_http_arrkey_t *key, const char *prefix) {
        char *new_key = NULL;
  
        if (prefix && *prefix) {
 -              if (type == HASH_KEY_IS_STRING) {
 -                      spprintf(&new_key, 0, "%s[%s]", prefix, str);
 +              if (key->key) {
 +                      spprintf(&new_key, 0, "%s[%s]", prefix, key->key->val);
                } else {
 -                      spprintf(&new_key, 0, "%s[%lu]", prefix, num);
 +                      spprintf(&new_key, 0, "%s[%lu]", prefix, key->h);
                }
 -      } else if (type == HASH_KEY_IS_STRING) {
 -              new_key = estrdup(str);
 +      } else if (key->key) {
 +              new_key = estrdup(key->key->val);
        } else {
                new_key = estrdup("");
        }
        return new_key;
  }
  
 -static STATUS add_recursive_fields(php_http_message_body_t *body, const char *name, zval *value)
 +static ZEND_RESULT_CODE add_recursive_field_value(php_http_message_body_t *body, const char *name, zval *value)
 +{
 +      zend_string *zs = zval_get_string(value);
 +      ZEND_RESULT_CODE rc = php_http_message_body_add_form_field(body, name, zs->val, zs->len);
 +      zend_string_release(zs);
 +      return rc;
 +}
 +
 +static ZEND_RESULT_CODE add_recursive_fields(php_http_message_body_t *body, const char *name, HashTable *fields)
  {
 -      if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
 -              zval **val;
 -              HashTable *ht;
 -              HashPosition pos;
 -              php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 -              TSRMLS_FETCH_FROM_CTX(body->ts);
 +      zval *val;
 +      php_http_arrkey_t key;
 +
 +      if (!ZEND_HASH_GET_APPLY_COUNT(fields)) {
 +              ZEND_HASH_INC_APPLY_COUNT(fields);
 +              ZEND_HASH_FOREACH_KEY_VAL_IND(fields, key.h, key.key, val)
 +              {
 +                      char *str = format_key(&key, name);
  
 -              ht = HASH_OF(value);
 -              if (!ht->nApplyCount) {
 -                      ++ht->nApplyCount;
 -                      FOREACH_KEYVAL(pos, value, key, val) {
 -                              char *str = format_key(key.type, key.str, key.num, name);
 -                              if (SUCCESS != add_recursive_fields(body, str, *val)) {
 +                      if (Z_TYPE_P(val) != IS_ARRAY && Z_TYPE_P(val) != IS_OBJECT) {
 +                              if (SUCCESS != add_recursive_field_value(body, str, val)) {
                                        efree(str);
 -                                      ht->nApplyCount--;
 +                                      ZEND_HASH_DEC_APPLY_COUNT(fields);
                                        return FAILURE;
                                }
 +                      } else if (SUCCESS != add_recursive_fields(body, str, HASH_OF(val))) {
                                efree(str);
 +                              ZEND_HASH_DEC_APPLY_COUNT(fields);
 +                              return FAILURE;
                        }
 -                      --ht->nApplyCount;
 +                      efree(str);
                }
 -      } else {
 -              zval *cpy = php_http_ztyp(IS_STRING, value);
 -              php_http_message_body_add_form_field(body, name, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
 -              zval_ptr_dtor(&cpy);
 +              ZEND_HASH_FOREACH_END();
 +              ZEND_HASH_DEC_APPLY_COUNT(fields);
        }
  
        return SUCCESS;
  }
  
 -static STATUS add_recursive_files(php_http_message_body_t *body, const char *name, zval *value)
 +static ZEND_RESULT_CODE add_recursive_files(php_http_message_body_t *body, const char *name, HashTable *files)
  {
 -      zval **zdata = NULL, **zfile, **zname, **ztype;
 -      HashTable *ht;
 -      TSRMLS_FETCH_FROM_CTX(body->ts);
 -
 -      if (Z_TYPE_P(value) != IS_ARRAY && Z_TYPE_P(value) != IS_OBJECT) {
 -              php_error_docref(NULL TSRMLS_CC, E_WARNING, "Expected array or object (name, type, file) for message body file to add");
 -              return FAILURE;
 -      }
 -
 -      ht = HASH_OF(value);
 +      zval *zdata = NULL, *zfile, *zname, *ztype;
  
 -      if ((SUCCESS != zend_hash_find(ht, ZEND_STRS("name"), (void *) &zname))
 -      ||      (SUCCESS != zend_hash_find(ht, ZEND_STRS("type"), (void *) &ztype))
 -      ||      (SUCCESS != zend_hash_find(ht, ZEND_STRS("file"), (void *) &zfile))
 +      /* single entry */
 +      if (!(zname = zend_hash_str_find(files, ZEND_STRL("name")))
 +      ||      !(ztype = zend_hash_str_find(files, ZEND_STRL("type")))
 +      ||      !(zfile = zend_hash_str_find(files, ZEND_STRL("file")))
        ) {
 -              zval **val;
 -              HashPosition pos;
 -              php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 +              zval *val;
 +              php_http_arrkey_t key;
  
 -              if (!ht->nApplyCount) {
 -                      ++ht->nApplyCount;
 -                      FOREACH_HASH_KEYVAL(pos, ht, key, val) {
 -                              if (Z_TYPE_PP(val) == IS_ARRAY || Z_TYPE_PP(val) == IS_OBJECT) {
 -                                      char *str = format_key(key.type, key.str, key.num, name);
 +              if (!ZEND_HASH_GET_APPLY_COUNT(files)) {
 +                      ZEND_HASH_INC_APPLY_COUNT(files);
 +                      ZEND_HASH_FOREACH_KEY_VAL_IND(files, key.h, key.key, val)
 +                      {
 +                              if (Z_TYPE_P(val) == IS_ARRAY || Z_TYPE_P(val) == IS_OBJECT) {
 +                                      char *str = format_key(&key, name);
  
 -                                      if (SUCCESS != add_recursive_files(body, str, *val)) {
 +                                      if (SUCCESS != add_recursive_files(body, str, HASH_OF(val))) {
                                                efree(str);
 -                                              --ht->nApplyCount;
 +                                              ZEND_HASH_DEC_APPLY_COUNT(files);
                                                return FAILURE;
                                        }
                                        efree(str);
                                }
                        }
 -                      --ht->nApplyCount;
 +                      ZEND_HASH_FOREACH_END();
 +                      ZEND_HASH_DEC_APPLY_COUNT(files);
                }
                return SUCCESS;
        } else {
 +              /* stream entry */
                php_stream *stream;
 -              zval *zfc = php_http_ztyp(IS_STRING, *zfile);
 +              zend_string *zfc = zval_get_string(zfile);
  
 -              if (SUCCESS == zend_hash_find(ht, ZEND_STRS("data"), (void *) &zdata)) {
 -                      if (Z_TYPE_PP(zdata) == IS_RESOURCE) {
 +              if ((zdata = zend_hash_str_find(files, ZEND_STRL("data")))) {
 +                      if (Z_TYPE_P(zdata) == IS_RESOURCE) {
                                php_stream_from_zval_no_verify(stream, zdata);
                        } else {
 -                              zval *tmp = php_http_ztyp(IS_STRING, *zdata);
 +                              zend_string *tmp = zval_get_string(zdata);
  
 -                              stream = php_stream_memory_open(TEMP_STREAM_READONLY, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
 -                              zval_ptr_dtor(&tmp);
 +                              stream = php_stream_memory_open(TEMP_STREAM_READONLY, tmp->val, tmp->len);
 +                              zend_string_release(tmp);
                        }
                } else {
 -                      stream = php_stream_open_wrapper(Z_STRVAL_P(zfc), "r", REPORT_ERRORS|USE_PATH, NULL);
 +                      stream = php_stream_open_wrapper(zfc->val, "r", REPORT_ERRORS|USE_PATH, NULL);
                }
  
                if (!stream) {
 -                      zval_ptr_dtor(&zfc);
 +                      zend_string_release(zfc);
                        return FAILURE;
                } else {
 -                      zval *znc = php_http_ztyp(IS_STRING, *zname), *ztc = php_http_ztyp(IS_STRING, *ztype);
 -                      char *key = format_key(HASH_KEY_IS_STRING, Z_STRVAL_P(znc), 0, name);
 -                      STATUS ret =  php_http_message_body_add_form_file(body, key, Z_STRVAL_P(ztc), Z_STRVAL_P(zfc), stream);
 +                      zend_string *znc = zval_get_string(zname), *ztc = zval_get_string(ztype);
 +                      php_http_arrkey_t arrkey = {0, znc};
 +                      char *key = format_key(&arrkey, name);
 +                      ZEND_RESULT_CODE ret = php_http_message_body_add_form_file(body, key, ztc->val, zfc->val, stream);
  
                        efree(key);
 -                      zval_ptr_dtor(&znc);
 -                      zval_ptr_dtor(&ztc);
 -                      zval_ptr_dtor(&zfc);
 -                      if (!zdata || Z_TYPE_PP(zdata) != IS_RESOURCE) {
 +                      zend_string_release(znc);
 +                      zend_string_release(ztc);
 +                      zend_string_release(zfc);
 +                      if (!zdata || Z_TYPE_P(zdata) != IS_RESOURCE) {
                                php_stream_close(stream);
                        }
                        return ret;
@@@ -442,7 -464,7 +446,7 @@@ struct splitbody_arg 
        size_t consumed;
  };
  
 -static size_t splitbody(void *opaque, char *buf, size_t len TSRMLS_DC)
 +static size_t splitbody(void *opaque, char *buf, size_t len)
  {
        struct splitbody_arg *arg = opaque;
        const char *boundary = NULL;
                                        /* advance messages */
                                        php_http_message_t *msg;
  
 -                                      msg = php_http_message_init(NULL, 0, NULL TSRMLS_CC);
 +                                      msg = php_http_message_init(NULL, 0, NULL);
                                        msg->parent = arg->parser->message;
                                        arg->parser->message = msg;
                                }
                                        len = 0;
                                } else {
                                        /* let this be garbage */
 -                                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Malformed multipart boundary at pos %zu", consumed);
 +                                      php_error_docref(NULL, E_WARNING, "Malformed multipart boundary at pos %zu", consumed);
                                        return -1;
                                }
                        }
@@@ -521,15 -543,16 +525,15 @@@ php_http_message_t *php_http_message_bo
        php_http_buffer_t *tmp = NULL;
        php_http_message_t *msg = NULL;
        struct splitbody_arg arg;
 -      TSRMLS_FETCH_FROM_CTX(body->ts);
  
        php_http_buffer_init(&arg.buf);
 -      arg.parser = php_http_message_parser_init(NULL TSRMLS_CC);
 +      arg.parser = php_http_message_parser_init(NULL);
        arg.boundary_len = spprintf(&arg.boundary_str, 0, "\n--%s", boundary);
        arg.consumed = 0;
  
        php_stream_rewind(s);
        while (!php_stream_eof(s)) {
 -              php_http_buffer_passthru(&tmp, 0x1000, (php_http_buffer_pass_func_t) _php_stream_read, s, splitbody, &arg TSRMLS_CC);
 +              php_http_buffer_passthru(&tmp, 0x1000, (php_http_buffer_pass_func_t) _php_stream_read, s, splitbody, &arg);
        }
  
        msg = arg.parser->message;
  
  static zend_object_handlers php_http_message_body_object_handlers;
  
 -zend_object_value php_http_message_body_object_new(zend_class_entry *ce TSRMLS_DC)
 +zend_object *php_http_message_body_object_new(zend_class_entry *ce)
  {
 -      return php_http_message_body_object_new_ex(ce, NULL, NULL TSRMLS_CC);
 +      return &php_http_message_body_object_new_ex(ce, NULL)->zo;
  }
  
 -zend_object_value php_http_message_body_object_new_ex(zend_class_entry *ce, php_http_message_body_t *body, php_http_message_body_object_t **ptr TSRMLS_DC)
 +php_http_message_body_object_t *php_http_message_body_object_new_ex(zend_class_entry *ce, php_http_message_body_t *body)
  {
        php_http_message_body_object_t *o;
  
 -      o = ecalloc(1, sizeof(php_http_message_body_object_t));
 -      zend_object_std_init((zend_object *) o, php_http_message_body_class_entry TSRMLS_CC);
 -      object_properties_init((zend_object *) o, ce);
 -
 -      if (ptr) {
 -              *ptr = o;
 -      }
 +      o = ecalloc(1, sizeof(*o) + zend_object_properties_size(ce));
 +      zend_object_std_init(&o->zo, php_http_message_body_class_entry);
 +      object_properties_init(&o->zo, ce);
  
        if (body) {
                o->body = body;
        }
  
 -      o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_body_object_free, NULL TSRMLS_CC);
 -      o->zv.handlers = &php_http_message_body_object_handlers;
 +      o->zo.handlers = &php_http_message_body_object_handlers;
  
 -      return o->zv;
 +      return o;
  }
  
 -zend_object_value php_http_message_body_object_clone(zval *object TSRMLS_DC)
 +zend_object *php_http_message_body_object_clone(zval *object)
  {
 -      zend_object_value new_ov;
        php_http_message_body_object_t *new_obj = NULL;
 -      php_http_message_body_object_t *old_obj = zend_object_store_get_object(object TSRMLS_CC);
 +      php_http_message_body_object_t *old_obj = PHP_HTTP_OBJ(NULL, object);
        php_http_message_body_t *body = php_http_message_body_copy(old_obj->body, NULL);
  
 -      new_ov = php_http_message_body_object_new_ex(old_obj->zo.ce, body, &new_obj TSRMLS_CC);
 -      zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(object) TSRMLS_CC);
 +      new_obj = php_http_message_body_object_new_ex(old_obj->zo.ce, body);
 +      zend_objects_clone_members(&new_obj->zo, &old_obj->zo);
  
 -      return new_ov;
 +      return &new_obj->zo;
  }
  
 -void php_http_message_body_object_free(void *object TSRMLS_DC)
 +void php_http_message_body_object_free(zend_object *object)
  {
 -      php_http_message_body_object_t *obj = object;
 +      php_http_message_body_object_t *obj = PHP_HTTP_OBJ(object, NULL);
  
        php_http_message_body_free(&obj->body);
-       zend_object_std_dtor(object TSRMLS_CC);
 -      zend_object_std_dtor((zend_object *) obj TSRMLS_CC);
 -      efree(obj);
++      zend_object_std_dtor(object);
  }
  
  #define PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj) \
        do { \
                if (!obj->body) { \
 -                      obj->body = php_http_message_body_init(NULL, NULL TSRMLS_CC); \
 +                      obj->body = php_http_message_body_init(NULL, NULL); \
                } \
        } while(0)
  
@@@ -599,19 -629,19 +603,19 @@@ ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBo
  ZEND_END_ARG_INFO();
  PHP_METHOD(HttpMessageBody, __construct)
  {
 -      php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
        zval *zstream = NULL;
        php_stream *stream;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r!", &zstream), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream), invalid_arg, return);
  
        if (zstream) {
 -              php_http_expect(php_stream_from_zval_no_verify(stream, &zstream), unexpected_val, return);
 +              php_http_expect(php_stream_from_zval_no_verify(stream, zstream), unexpected_val, return);
  
                if (obj->body) {
                        php_http_message_body_free(&obj->body);
                }
 -              obj->body = php_http_message_body_init(NULL, stream TSRMLS_CC);
 +              obj->body = php_http_message_body_init(NULL, stream);
        }
  }
  
@@@ -620,14 -650,15 +624,14 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpMessageBody, __toString)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -              char *str;
 -              size_t len;
 +              php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
 +              zend_string *zs;
  
                PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
  
 -              php_http_message_body_to_string(obj->body, &str, &len, 0, 0);
 -              if (str) {
 -                      RETURN_STRINGL(str, len, 0);
 +              zs = php_http_message_body_to_string(obj->body, 0, 0);
 +              if (zs) {
 +                      RETURN_STR(zs);
                }
        }
        RETURN_EMPTY_STRING();
@@@ -639,13 -670,13 +643,13 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpMessageBody, unserialize)
  {
        char *us_str;
 -      int us_len;
 +      size_t us_len;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &us_str, &us_len)) {
 -              php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &us_str, &us_len)) {
 +              php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                php_stream *s = php_stream_memory_open(0, us_str, us_len);
  
 -              obj->body = php_http_message_body_init(NULL, s TSRMLS_CC);
 +              obj->body = php_http_message_body_init(NULL, s);
        }
  }
  
@@@ -657,15 -688,15 +661,15 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpMessageBody, toStream)
  {
        zval *zstream;
 -      long offset = 0, forlen = 0;
 +      zend_long offset = 0, forlen = 0;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r|ll", &zstream, &offset, &forlen)) {
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "r|ll", &zstream, &offset, &forlen)) {
                php_stream *stream;
 -              php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
  
 -              php_stream_from_zval(stream, &zstream);
 +              php_stream_from_zval(stream, zstream);
                php_http_message_body_to_stream(obj->body, stream, offset, forlen);
                RETURN_ZVAL(getThis(), 1, 0);
        }
@@@ -679,18 -710,22 +683,18 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpMessageBody, toCallback)
  {
        php_http_pass_fcall_arg_t fcd;
 -      long offset = 0, forlen = 0;
 +      zend_long offset = 0, forlen = 0;
  
 -      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f|ll", &fcd.fci, &fcd.fcc, &offset, &forlen)) {
 -              php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "f|ll", &fcd.fci, &fcd.fcc, &offset, &forlen)) {
 +              php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
  
 -              fcd.fcz = getThis();
 -              Z_ADDREF_P(fcd.fcz);
 -              TSRMLS_SET_CTX(fcd.ts);
 -
 +              ZVAL_COPY(&fcd.fcz, getThis());
                php_http_message_body_to_callback(obj->body, php_http_pass_fcall_callback, &fcd, offset, forlen);
                zend_fcall_info_args_clear(&fcd.fci, 1);
 -
                zval_ptr_dtor(&fcd.fcz);
 -              RETURN_ZVAL(getThis(), 1, 0);
 +              RETURN_ZVAL_FAST(getThis());
        }
  }
  
@@@ -699,12 -734,12 +703,12 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpMessageBody, getResource)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
  
 -              zend_list_addref(obj->body->stream_id);
 -              RETVAL_RESOURCE(obj->body->stream_id);
 +              ++GC_REFCOUNT(obj->body->res);
 +              RETVAL_RES(obj->body->res);
        }
  }
  
@@@ -713,12 -748,12 +717,12 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpMessageBody, getBoundary)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_body_object_t * obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_body_object_t * obj = PHP_HTTP_OBJ(NULL, getThis());
  
                PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
  
                if (obj->body->boundary) {
 -                      RETURN_STRING(obj->body->boundary, 1);
 +                      RETURN_STRING(obj->body->boundary);
                }
        }
  }
@@@ -729,17 -764,18 +733,17 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpMessageBody, append)
  {
        char *str;
 -      int len;
 +      size_t len;
        php_http_message_body_object_t *obj;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len), invalid_arg, return);
 -
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "s", &str, &len), invalid_arg, return);
  
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
        PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
  
        php_http_expect(len == php_http_message_body_append(obj->body, str, len), runtime, return);
  
 -      RETURN_ZVAL(getThis(), 1, 0);
 +      RETURN_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addForm, 0, 0, 0)
@@@ -751,14 -787,15 +755,14 @@@ PHP_METHOD(HttpMessageBody, addForm
        HashTable *fields = NULL, *files = NULL;
        php_http_message_body_object_t *obj;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|h!h!", &fields, &files), invalid_arg, return);
 -
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|h!h!", &fields, &files), invalid_arg, return);
  
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
        PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
  
        php_http_expect(SUCCESS == php_http_message_body_add_form(obj->body, fields, files), runtime, return);
  
 -      RETURN_ZVAL(getThis(), 1, 0);
 +      RETURN_ZVAL_FAST(getThis());
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addPart, 0, 0, 1)
@@@ -771,19 -808,19 +775,19 @@@ PHP_METHOD(HttpMessageBody, addPart
        php_http_message_object_t *mobj;
        zend_error_handling zeh;
  
 -      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zobj, php_http_message_class_entry), invalid_arg, return);
 +      php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zobj, php_http_message_class_entry), invalid_arg, return);
  
 -      obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      mobj = zend_object_store_get_object(zobj TSRMLS_CC);
 +      obj = PHP_HTTP_OBJ(NULL, getThis());
 +      mobj = PHP_HTTP_OBJ(NULL, zobj);
  
        PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
  
 -      zend_replace_error_handling(EH_THROW, php_http_exception_runtime_class_entry, &zeh TSRMLS_CC);
 +      zend_replace_error_handling(EH_THROW, php_http_exception_runtime_class_entry, &zeh);
        php_http_message_body_add_part(obj->body, mobj->message);
 -      zend_restore_error_handling(&zeh TSRMLS_CC);
 +      zend_restore_error_handling(&zeh);
  
        if (!EG(exception)) {
 -              RETURN_ZVAL(getThis(), 1, 0);
 +              RETURN_ZVAL_FAST(getThis());
        }
  }
  
@@@ -792,13 -829,13 +796,13 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpMessageBody, etag)
  {
        if (SUCCESS == zend_parse_parameters_none()) {
 -              php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +              php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                char *etag;
  
                PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
  
                if ((etag = php_http_message_body_etag(obj->body))) {
 -                      RETURN_STRING(etag, 0);
 +                      RETURN_STR(php_http_cs2zs(etag, strlen(etag)));
                } else {
                        RETURN_FALSE;
                }
@@@ -811,10 -848,10 +815,10 @@@ ZEND_END_ARG_INFO()
  PHP_METHOD(HttpMessageBody, stat)
  {
        char *field_str = NULL;
 -      int field_len = 0;
 +      size_t field_len = 0;
  
--      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &field_str, &field_len)) {
 -              php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
++      if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|s", &field_str, &field_len)) {
 +              php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
                const php_stream_statbuf *sb;
  
                PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
                                                        RETURN_LONG(sb->sb.st_ctime);
                                                        break;
                                                default:
 -                                                      php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown stat field: '%s' (should be one of [s]ize, [a]time, [m]time or [c]time)", field_str);
 +                                                      php_error_docref(NULL, E_WARNING, "Unknown stat field: '%s' (should be one of [s]ize, [a]time, [m]time or [c]time)", field_str);
                                                        break;
                                        }
                        } else {
                                object_init(return_value);
 -                              add_property_long_ex(return_value, ZEND_STRS("size"), sb->sb.st_size TSRMLS_CC);
 -                              add_property_long_ex(return_value, ZEND_STRS("atime"), sb->sb.st_atime TSRMLS_CC);
 -                              add_property_long_ex(return_value, ZEND_STRS("mtime"), sb->sb.st_mtime TSRMLS_CC);
 -                              add_property_long_ex(return_value, ZEND_STRS("ctime"), sb->sb.st_ctime TSRMLS_CC);
 +                              add_property_long_ex(return_value, ZEND_STRL("size"), sb->sb.st_size);
 +                              add_property_long_ex(return_value, ZEND_STRL("atime"), sb->sb.st_atime);
 +                              add_property_long_ex(return_value, ZEND_STRL("mtime"), sb->sb.st_mtime);
 +                              add_property_long_ex(return_value, ZEND_STRL("ctime"), sb->sb.st_ctime);
                        }
                }
        }
@@@ -878,13 -915,11 +882,13 @@@ PHP_MINIT_FUNCTION(http_message_body
        zend_class_entry ce = {0};
  
        INIT_NS_CLASS_ENTRY(ce, "http\\Message", "Body", php_http_message_body_methods);
 -      php_http_message_body_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
 +      php_http_message_body_class_entry = zend_register_internal_class(&ce);
        php_http_message_body_class_entry->create_object = php_http_message_body_object_new;
        memcpy(&php_http_message_body_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
 +      php_http_message_body_object_handlers.offset = XtOffsetOf(php_http_message_body_object_t, zo);
        php_http_message_body_object_handlers.clone_obj = php_http_message_body_object_clone;
 -      zend_class_implements(php_http_message_body_class_entry TSRMLS_CC, 1, zend_ce_serializable);
 +      php_http_message_body_object_handlers.free_obj = php_http_message_body_object_free;
 +      zend_class_implements(php_http_message_body_class_entry, 1, zend_ce_serializable);
  
        return SUCCESS;
  }
index e40e7b3e79c1edb282da9dd16406b8ddc7e65920,3b55efbab7b295a9cd210fa57cb409769a3a97e6..7bf9a164f5a14cad90d42617cfb114fc7455ab80
@@@ -23,19 -23,20 +23,20 @@@ typedef struct php_http_message_parser_
  
  static const php_http_message_parser_state_spec_t php_http_message_parser_states[] = {
                {PHP_HTTP_MESSAGE_PARSER_STATE_START,                   1},
-               {PHP_HTTP_MESSAGE_PARSER_STATE_HEADER,                  1},
+               {PHP_HTTP_MESSAGE_PARSER_STATE_HEADER,                  0},
                {PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE,             0},
                {PHP_HTTP_MESSAGE_PARSER_STATE_BODY,                    0},
                {PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB,               1},
                {PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH,             1},
                {PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED,    1},
                {PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE,               0},
+               {PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL,               0},
                {PHP_HTTP_MESSAGE_PARSER_STATE_DONE,                    0}
  };
  
  #if DBG_PARSER
  const char *php_http_message_parser_state_name(php_http_message_parser_state_t state) {
-       const char *states[] = {"START", "HEADER", "HEADER_DONE", "BODY", "BODY_DUMB", "BODY_LENGTH", "BODY_CHUNK", "BODY_DONE", "DONE"};
+       const char *states[] = {"START", "HEADER", "HEADER_DONE", "BODY", "BODY_DUMB", "BODY_LENGTH", "BODY_CHUNK", "BODY_DONE", "UPDATE_CL", "DONE"};
        
        if (state < 0 || state > (sizeof(states)/sizeof(char*))-1) {
                return "FAILURE";
  }
  #endif
  
 -php_http_message_parser_t *php_http_message_parser_init(php_http_message_parser_t *parser TSRMLS_DC)
 +php_http_message_parser_t *php_http_message_parser_init(php_http_message_parser_t *parser)
  {
        if (!parser) {
                parser = emalloc(sizeof(*parser));
        }
        memset(parser, 0, sizeof(*parser));
  
 -      TSRMLS_SET_CTX(parser->ts);
 -
 -      php_http_header_parser_init(&parser->header TSRMLS_CC);
 +      php_http_header_parser_init(&parser->header);
  
        return parser;
  }
@@@ -116,22 -119,35 +117,34 @@@ void php_http_message_parser_free(php_h
  php_http_message_parser_state_t php_http_message_parser_parse_stream(php_http_message_parser_t *parser, php_http_buffer_t *buf, php_stream *s, unsigned flags, php_http_message_t **message)
  {
        php_http_message_parser_state_t state = PHP_HTTP_MESSAGE_PARSER_STATE_START;
 -      TSRMLS_FETCH_FROM_CTX(parser->ts);
  
        if (!buf->data) {
                php_http_buffer_resize_ex(buf, 0x1000, 1, 0);
        }
-       while (!php_stream_eof(s)) {
+       while (1) {
                size_t justread = 0;
  #if DBG_PARSER
                fprintf(stderr, "#SP: %s (f:%u)\n", php_http_message_parser_state_name(state), flags);
  #endif
+               /* resize if needed */
+               if (buf->free < 0x1000) {
+                       php_http_buffer_resize_ex(buf, 0x1000, 1, 0);
+               }
                switch (state) {
                        case PHP_HTTP_MESSAGE_PARSER_STATE_START:
                        case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER:
                        case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE:
                                /* read line */
                                php_stream_get_line(s, buf->data + buf->used, buf->free, &justread);
+                               /* if we fail reading a whole line, try a single char */
+                               if (!justread) {
+                                       int c = php_stream_getc(s);
+                                       if (c != EOF) {
+                                               char s[1] = {c};
+                                               justread = php_http_buffer_append(buf, s, 1);
+                                       }
+                               }
                                php_http_buffer_account(buf, justread);
                                break;
  
  
                        case PHP_HTTP_MESSAGE_PARSER_STATE_BODY:
                        case PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL:
                                /* should not occur */
                                abort();
                                break;
  
                if (justread) {
                        state = php_http_message_parser_parse(parser, buf, flags, message);
-               } else  {
+               } else if (php_stream_eof(s)) {
+                       return php_http_message_parser_parse(parser, buf, flags | PHP_HTTP_MESSAGE_PARSER_CLEANUP, message);
+               } else {
                        return state;
                }
        }
@@@ -191,6 -210,7 +207,6 @@@ php_http_message_parser_state_t php_htt
        char *str = NULL;
        size_t len = 0;
        size_t cut = 0;
 -      TSRMLS_FETCH_FROM_CTX(parser->ts);
  
        while (buffer->used || !php_http_message_parser_states[php_http_message_parser_state_is(parser)].need_data) {
  #if DBG_PARSER
                                                break;
  
                                        default:
-                                               php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_HEADER);
-                                               if (buffer->used) {
-                                                       return PHP_HTTP_MESSAGE_PARSER_STATE_HEADER;
+                                               if (buffer->used || !(flags & PHP_HTTP_MESSAGE_PARSER_CLEANUP)) {
+                                                       return php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_HEADER);
+                                               } else {
+                                                       php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE);
                                                }
                                }
                                break;
  
                        case PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE:
                        {
 -                              zval *h, *h_loc = NULL, *h_con = NULL, **h_cl = NULL, **h_cr = NULL, **h_te = NULL;
 +                              zval h, *h_ptr, *h_loc = NULL, *h_con = NULL, *h_ce;
 +                              zend_bool chunked = 0;
 +                              zend_long content_length = -1;
 +                              zend_string *content_range = NULL;
  
 -                              if ((h = php_http_message_header(*message, ZEND_STRL("Transfer-Encoding"), 1))) {
 -                                      zend_hash_update(&(*message)->hdrs, "X-Original-Transfer-Encoding", sizeof("X-Original-Transfer-Encoding"), (void *) &h, sizeof(zval *), (void *) &h_te);
 -                                      zend_hash_del(&(*message)->hdrs, "Transfer-Encoding", sizeof("Transfer-Encoding"));
+                               /* Content-Range has higher precedence than Content-Length,
+                                * and content-length denotes the original length of the entity,
+                                * so let's *NOT* remove CR/CL, because that would fundamentally
+                                * change the meaning of the whole message
+                                */
 +                              if ((h_ptr = php_http_message_header(*message, ZEND_STRL("Transfer-Encoding")))) {
 +                                      zend_string *zs = zval_get_string(h_ptr);
 +
 +                                      chunked = zend_string_equals_literal(zs, "chunked");
 +                                      zend_string_release(zs);
 +
 +                                      Z_TRY_ADDREF_P(h_ptr);
 +                                      zend_hash_str_update(&(*message)->hdrs, "X-Original-Transfer-Encoding", lenof("X-Original-Transfer-Encoding"), h_ptr);
 +                                      zend_hash_str_del(&(*message)->hdrs, "Transfer-Encoding", lenof("Transfer-Encoding"));
  
-                               }
-                               if ((h_ptr = php_http_message_header(*message, ZEND_STRL("Content-Length")))) {
+                                       /* reset */
 -                                      MAKE_STD_ZVAL(h);
 -                                      ZVAL_LONG(h, 0);
 -                                      zend_hash_update(&(*message)->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &h, sizeof(zval *), NULL);
 -                              } else if ((h = php_http_message_header(*message, ZEND_STRL("Content-Length"), 1))) {
 -                                      zend_hash_update(&(*message)->hdrs, "X-Original-Content-Length", sizeof("X-Original-Content-Length"), (void *) &h, sizeof(zval *), (void *) &h_cl);
++                                      ZVAL_LONG(&h, 0);
++                                      zend_hash_str_update(&(*message)->hdrs, "Content-Length", lenof("Content-Length"), &h);
++                              } else if ((h_ptr = php_http_message_header(*message, ZEND_STRL("Content-Length")))) {
 +                                      content_length = zval_get_long(h_ptr);
 +                                      Z_TRY_ADDREF_P(h_ptr);
 +                                      zend_hash_str_update(&(*message)->hdrs, "X-Original-Content-Length", lenof("X-Original-Content-Length"), h_ptr);
                                }
-                               if ((h_ptr = php_http_message_header(*message, ZEND_STRL("Content-Range")))) {
-                                       content_range = zval_get_string(h_ptr);
  
-                                       Z_TRY_ADDREF_P(h_ptr);
-                                       zend_hash_str_update(&(*message)->hdrs, "X-Original-Content-Range", lenof("X-Original-Content-Range"), h_ptr);
-                                       zend_hash_str_del(&(*message)->hdrs, "Content-Range", lenof("Content-Range"));
 -                              if ((h = php_http_message_header(*message, ZEND_STRL("Content-Range"), 1))) {
 -                                      zend_hash_find(&(*message)->hdrs, ZEND_STRS("Content-Range"), (void *) &h_cr);
 -                                      if (h != *h_cr) {
 -                                              zend_hash_update(&(*message)->hdrs, "Content-Range", sizeof("Content-Range"), &h, sizeof(zval *), (void *) &h_cr);
 -                                      } else {
 -                                              zval_ptr_dtor(&h);
 -                                      }
++                              if ((content_range = php_http_message_header_string(*message, ZEND_STRL("Content-Range")))) {
++                                      ZVAL_STR_COPY(&h, content_range);
++                                      zend_hash_str_update(&(*message)->hdrs, "Content-Range", lenof("Content-Range"), &h);
                                }
  
-                               /* default */
-                               ZVAL_LONG(&h, 0);
-                               zend_hash_str_update(&(*message)->hdrs, "Content-Length", lenof("Content-Length"), &h);
                                /* so, if curl sees a 3xx code, a Location header and a Connection:close header
                                 * it decides not to read the response body.
                                 */
                                if ((flags & PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS)
                                &&      (*message)->type == PHP_HTTP_RESPONSE
                                &&      (*message)->http.info.response.code/100 == 3
 -                              &&      (h_loc = php_http_message_header(*message, ZEND_STRL("Location"), 1))
 -                              &&      (h_con = php_http_message_header(*message, ZEND_STRL("Connection"), 1))
 +                              &&      (h_loc = php_http_message_header(*message, ZEND_STRL("Location")))
 +                              &&      (h_con = php_http_message_header(*message, ZEND_STRL("Connection")))
                                ) {
--                                      if (php_http_match(Z_STRVAL_P(h_con), "close", PHP_HTTP_MATCH_WORD)) {
++                                      zend_string *con = zval_get_string(h_con);
++
++                                      if (php_http_match(con->val, "close", PHP_HTTP_MATCH_WORD)) {
++                                              zend_string_release(con);
                                                php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_DONE);
 -                                              zval_ptr_dtor(&h_loc);
 -                                              zval_ptr_dtor(&h_con);
                                                break;
                                        }
++                                      zend_string_release(con);
                                }
 -                              if (h_loc) {
 -                                      zval_ptr_dtor(&h_loc);
 -                              }
 -                              if (h_con) {
 -                                      zval_ptr_dtor(&h_con);
 -                              }
  
 -                              if ((h = php_http_message_header(*message, ZEND_STRL("Content-Encoding"), 1))) {
 -                                      if (php_http_match(Z_STRVAL_P(h), "gzip", PHP_HTTP_MATCH_WORD)
 -                                      ||      php_http_match(Z_STRVAL_P(h), "x-gzip", PHP_HTTP_MATCH_WORD)
 -                                      ||      php_http_match(Z_STRVAL_P(h), "deflate", PHP_HTTP_MATCH_WORD)
 +                              if ((h_ce = php_http_message_header(*message, ZEND_STRL("Content-Encoding")))) {
-                                       if (php_http_match(Z_STRVAL_P(h_ce), "gzip", PHP_HTTP_MATCH_WORD)
-                                       ||      php_http_match(Z_STRVAL_P(h_ce), "x-gzip", PHP_HTTP_MATCH_WORD)
-                                       ||      php_http_match(Z_STRVAL_P(h_ce), "deflate", PHP_HTTP_MATCH_WORD)
++                                      zend_string *ce = zval_get_string(h_ce);
++
++                                      if (php_http_match(ce->val, "gzip", PHP_HTTP_MATCH_WORD)
++                                      ||      php_http_match(ce->val, "x-gzip", PHP_HTTP_MATCH_WORD)
++                                      ||      php_http_match(ce->val, "deflate", PHP_HTTP_MATCH_WORD)
                                        ) {
                                                if (parser->inflate) {
                                                        php_http_encoding_stream_reset(&parser->inflate);
                                                } else {
 -                                                      parser->inflate = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_inflate_ops(), 0 TSRMLS_CC);
 +                                                      parser->inflate = php_http_encoding_stream_init(NULL, php_http_encoding_stream_get_inflate_ops(), 0);
                                                }
 -                                              zend_hash_update(&(*message)->hdrs, "X-Original-Content-Encoding", sizeof("X-Original-Content-Encoding"), &h, sizeof(zval *), NULL);
 -                                              zend_hash_del(&(*message)->hdrs, "Content-Encoding", sizeof("Content-Encoding"));
 -                                      } else {
 -                                              zval_ptr_dtor(&h);
 +                                              Z_TRY_ADDREF_P(h_ce);
 +                                              zend_hash_str_update(&(*message)->hdrs, "X-Original-Content-Encoding", lenof("X-Original-Content-Encoding"), h_ce);
 +                                              zend_hash_str_del(&(*message)->hdrs, "Content-Encoding", lenof("Content-Encoding"));
                                        }
++                                      zend_string_release(ce);
                                }
  
                                if ((flags & PHP_HTTP_MESSAGE_PARSER_DUMB_BODIES)) {
                                        php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB);
                                } else {
 -                                      if (h_te) {
 -                                              if (strstr(Z_STRVAL_PP(h_te), "chunked")) {
 -                                                      parser->dechunk = php_http_encoding_stream_init(parser->dechunk, php_http_encoding_stream_get_dechunk_ops(), 0 TSRMLS_CC);
 -                                                      php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED);
 -                                                      break;
 -                                              }
 +                                      if (chunked) {
 +                                              parser->dechunk = php_http_encoding_stream_init(parser->dechunk, php_http_encoding_stream_get_dechunk_ops(), 0);
 +                                              php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED);
 +                                              break;
                                        }
  
-                                       if (content_length >= 0) {
-                                               parser->body_length = content_length;
-                                               php_http_message_parser_state_push(parser, 1, !parser->body_length?PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
-                                               break;
-                                       }
 -                                      if (h_cr) {
 +                                      if (content_range) {
                                                ulong total = 0, start = 0, end = 0;
  
 -                                              if (!strncasecmp(Z_STRVAL_PP(h_cr), "bytes", lenof("bytes"))
 -                                              && (    Z_STRVAL_PP(h_cr)[lenof("bytes")] == ':'
 -                                                      ||      Z_STRVAL_PP(h_cr)[lenof("bytes")] == ' '
 -                                                      ||      Z_STRVAL_PP(h_cr)[lenof("bytes")] == '='
 +                                              if (!strncasecmp(content_range->val, "bytes", lenof("bytes"))
 +                                              && (    content_range->val[lenof("bytes")] == ':'
 +                                                      ||      content_range->val[lenof("bytes")] == ' '
 +                                                      ||      content_range->val[lenof("bytes")] == '='
                                                        )
                                                ) {
                                                        char *total_at = NULL, *end_at = NULL;
 -                                                      char *start_at = Z_STRVAL_PP(h_cr) + sizeof("bytes");
 +                                                      char *start_at = content_range->val + sizeof("bytes");
  
                                                        start = strtoul(start_at, &end_at, 10);
                                                        if (end_at) {
                                                                        total = strtoul(total_at + 1, NULL, 10);
                                                                }
  
-                                                               if (end >= start && (!total || end < total)) {
+                                                               if (end >= start && (!total || end <= total)) {
                                                                        parser->body_length = end + 1 - start;
                                                                        php_http_message_parser_state_push(parser, 1, !parser->body_length?PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
 +                                                                      zend_string_release(content_range);
                                                                        break;
                                                                }
                                                        }
                                                }
 -                                      }
  
 -                                      if (h_cl) {
 -                                              char *stop;
 -
 -                                              if (Z_TYPE_PP(h_cl) == IS_STRING) {
 -                                                      parser->body_length = strtoul(Z_STRVAL_PP(h_cl), &stop, 10);
 +                                              zend_string_release(content_range);
 +                                      }
  
 -                                                      if (stop != Z_STRVAL_PP(h_cl)) {
 -                                                              php_http_message_parser_state_push(parser, 1, !parser->body_length?PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
 -                                                              break;
 -                                                      }
 -                                              } else if (Z_TYPE_PP(h_cl) == IS_LONG) {
 -                                                      parser->body_length = Z_LVAL_PP(h_cl);
 -                                                      php_http_message_parser_state_push(parser, 1, !parser->body_length?PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
 -                                                      break;
 -                                              }
++                                      if (content_length >= 0) {
++                                              parser->body_length = content_length;
++                                              php_http_message_parser_state_push(parser, 1, !parser->body_length?PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE:PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
++                                              break;
+                                       }
  
                                        if ((*message)->type == PHP_HTTP_REQUEST) {
                                                php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_DONE);
                        case PHP_HTTP_MESSAGE_PARSER_STATE_BODY:
                        {
                                if (len) {
-                                       zval zcl;
 -                                      /* FIXME: what if we re-use the parser? */
                                        if (parser->inflate) {
                                                char *dec_str = NULL;
                                                size_t dec_len;
                                        }
  
                                        php_stream_write(php_http_message_body_stream((*message)->body), str, len);
--
-                                       /* keep track */
-                                       ZVAL_LONG(&zcl, php_http_message_body_size((*message)->body));
-                                       zend_hash_str_update(&(*message)->hdrs, "Content-Length", lenof("Content-Length"), &zcl);
                                }
  
                                if (cut) {
                        {
                                php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_DONE);
  
-                               if (parser->dechunk) {
+                               if (parser->dechunk && parser->dechunk->ctx) {
                                        char *dec_str = NULL;
                                        size_t dec_len;
  
                                                str = dec_str;
                                                len = dec_len;
                                                cut = 0;
-                                               php_http_message_parser_state_push(parser, 1, PHP_HTTP_MESSAGE_PARSER_STATE_BODY);
+                                               php_http_message_parser_state_push(parser, 2, PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL, PHP_HTTP_MESSAGE_PARSER_STATE_BODY);
                                        }
                                }
  
                                break;
                        }
  
-                       case PHP_HTTP_MESSAGE_PARSER_STATE_DONE: {
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL:
+                       {
 -                              zval *zcl;
 -                              MAKE_STD_ZVAL(zcl);
 -                              ZVAL_LONG(zcl, php_http_message_body_size((*message)->body));
 -                              zend_hash_update(&(*message)->hdrs, "Content-Length", sizeof("Content-Length"), &zcl, sizeof(zval *), NULL);
++                              zval zcl;
++
++                              ZVAL_LONG(&zcl, php_http_message_body_size((*message)->body));
++                              zend_hash_str_update(&(*message)->hdrs, "Content-Length", lenof("Content-Length"), &zcl);
+                               break;
+                       }
+                       case PHP_HTTP_MESSAGE_PARSER_STATE_DONE:
+                       {
                                char *ptr = buffer->data;
  
                                while (ptr - buffer->data < buffer->used && PHP_HTTP_IS_CTYPE(space, *ptr)) {
  zend_class_entry *php_http_message_parser_class_entry;
  static zend_object_handlers php_http_message_parser_object_handlers;
  
 -zend_object_value php_http_message_parser_object_new(zend_class_entry *ce TSRMLS_DC)
 +zend_object *php_http_message_parser_object_new(zend_class_entry *ce)
  {
 -      return php_http_message_parser_object_new_ex(ce, NULL, NULL TSRMLS_CC);
 +      return &php_http_message_parser_object_new_ex(ce, NULL)->zo;
  }
  
 -zend_object_value php_http_message_parser_object_new_ex(zend_class_entry *ce, php_http_message_parser_t *parser, php_http_message_parser_object_t **ptr TSRMLS_DC)
 +php_http_message_parser_object_t *php_http_message_parser_object_new_ex(zend_class_entry *ce, php_http_message_parser_t *parser)
  {
        php_http_message_parser_object_t *o;
  
 -      o = ecalloc(1, sizeof(php_http_message_parser_object_t));
 -      zend_object_std_init((zend_object *) o, ce TSRMLS_CC);
 -      object_properties_init((zend_object *) o, ce);
 -
 -      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);
  
        if (parser) {
                o->parser = parser;
        } else {
 -              o->parser = php_http_message_parser_init(NULL TSRMLS_CC);
 +              o->parser = php_http_message_parser_init(NULL);
        }
 -      o->buffer = php_http_buffer_new();
 +      php_http_buffer_init(&o->buffer);
 +      o->zo.handlers = &php_http_message_parser_object_handlers;
  
 -      o->zv.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_parser_object_free, NULL TSRMLS_CC);
 -      o->zv.handlers = &php_http_message_parser_object_handlers;
 -
 -      return o->zv;
 +      return o;
  }
  
 -void php_http_message_parser_object_free(void *object TSRMLS_DC)
 +void php_http_message_parser_object_free(zend_object *object)
  {
 -      php_http_message_parser_object_t *o = (php_http_message_parser_object_t *) object;
 +      php_http_message_parser_object_t *o = PHP_HTTP_OBJ(object, NULL);
  
        if (o->parser) {
                php_http_message_parser_free(&o->parser);
        }
 -      if (o->buffer) {
 -              php_http_buffer_free(&o->buffer);
 -      }
 -      zend_object_std_dtor((zend_object *) o TSRMLS_CC);
 -      efree(o);
 +      php_http_buffer_dtor(&o->buffer);
 +      zend_object_std_dtor(object);
  }
  
  ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageParser_getState, 0, 0, 0)
  ZEND_END_ARG_INFO();
  static PHP_METHOD(HttpMessageParser, getState)
  {
 -      php_http_message_parser_object_t *parser_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 +      php_http_message_parser_object_t *parser_obj = PHP_HTTP_OBJ(NULL, getThis());
  
        zend_parse_parameters_none();
        /* always return the real state */
@@@ -564,22 -612,18 +591,22 @@@ static PHP_METHOD(HttpMessageParser, pa
        php_http_message_parser_object_t *parser_obj;
        zval *zmsg;
        char *data_str;
 -      int data_len;
 -      long flags;
 +      size_t data_len;
 +      zend_long flags;
  
        php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "slz", &data_str, &data_len, &flags, &zmsg), invalid_arg, return);
  
 -      parser_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      php_http_buffer_append(parser_obj->buffer, data_str, data_len);
 -      RETVAL_LONG(php_http_message_parser_parse(parser_obj->parser, parser_obj->buffer, flags, &parser_obj->parser->message));
 +      parser_obj = PHP_HTTP_OBJ(NULL, getThis());
 +      php_http_buffer_append(&parser_obj->buffer, data_str, data_len);
 +      RETVAL_LONG(php_http_message_parser_parse(parser_obj->parser, &parser_obj->buffer, flags, &parser_obj->parser->message));
  
 +      ZVAL_DEREF(zmsg);
        zval_dtor(zmsg);
 +      ZVAL_NULL(zmsg);
        if (parser_obj->parser->message) {
 -              ZVAL_OBJVAL(zmsg, php_http_message_object_new_ex(php_http_message_class_entry, php_http_message_copy(parser_obj->parser->message, NULL), NULL TSRMLS_CC), 0);
 +              php_http_message_t *msg_cpy = php_http_message_copy(parser_obj->parser->message, NULL);
 +              php_http_message_object_t *msg_obj = php_http_message_object_new_ex(php_http_message_class_entry, msg_cpy);
 +              ZVAL_OBJ(zmsg, &msg_obj->zo);
        }
  }
  
@@@ -594,24 -638,20 +621,24 @@@ static PHP_METHOD(HttpMessageParser, st
        zend_error_handling zeh;
        zval *zmsg, *zstream;
        php_stream *s;
 -      long flags;
 +      zend_long flags;
  
        php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "rlz", &zstream, &flags, &zmsg), invalid_arg, return);
  
 -      zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh TSRMLS_CC);
 -      php_stream_from_zval(s, &zstream);
 -      zend_restore_error_handling(&zeh TSRMLS_CC);
 +      zend_replace_error_handling(EH_THROW, php_http_exception_unexpected_val_class_entry, &zeh);
 +      php_stream_from_zval(s, zstream);
 +      zend_restore_error_handling(&zeh);
  
 -      parser_obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 -      RETVAL_LONG(php_http_message_parser_parse_stream(parser_obj->parser, parser_obj->buffer, s, flags, &parser_obj->parser->message));
 +      parser_obj = PHP_HTTP_OBJ(NULL, getThis());
 +      RETVAL_LONG(php_http_message_parser_parse_stream(parser_obj->parser, &parser_obj->buffer, s, flags, &parser_obj->parser->message));
  
 +      ZVAL_DEREF(zmsg);
        zval_dtor(zmsg);
 +      ZVAL_NULL(zmsg);
        if (parser_obj->parser->message) {
 -              ZVAL_OBJVAL(zmsg, php_http_message_object_new_ex(php_http_message_class_entry, php_http_message_copy(parser_obj->parser->message, NULL), NULL TSRMLS_CC), 0);
 +              php_http_message_t *msg_cpy = php_http_message_copy(parser_obj->parser->message, NULL);
 +              php_http_message_object_t *msg_obj = php_http_message_object_new_ex(php_http_message_class_entry, msg_cpy);
 +              ZVAL_OBJ(zmsg, &msg_obj->zo);
        }
  }
  
@@@ -627,28 -667,27 +654,29 @@@ PHP_MINIT_FUNCTION(http_message_parser
        zend_class_entry ce;
  
        INIT_NS_CLASS_ENTRY(ce, "http\\Message", "Parser", php_http_message_parser_methods);
 -      php_http_message_parser_class_entry = zend_register_internal_class(&ce TSRMLS_CC);
 +      php_http_message_parser_class_entry = zend_register_internal_class(&ce);
        memcpy(&php_http_message_parser_object_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
        php_http_message_parser_class_entry->create_object = php_http_message_parser_object_new;
        php_http_message_parser_object_handlers.clone_obj = NULL;
 -
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("CLEANUP"), PHP_HTTP_MESSAGE_PARSER_CLEANUP TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("DUMB_BODIES"), PHP_HTTP_MESSAGE_PARSER_DUMB_BODIES TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("EMPTY_REDIRECTS"), PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("GREEDY"), PHP_HTTP_MESSAGE_PARSER_GREEDY TSRMLS_CC);
 -
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_FAILURE"), PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_START"), PHP_HTTP_MESSAGE_PARSER_STATE_START TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_HEADER"), PHP_HTTP_MESSAGE_PARSER_STATE_HEADER TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_HEADER_DONE"), PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY_DUMB"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY_LENGTH"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY_CHUNKED"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY_DONE"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_UPDATE_CL"), PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL TSRMLS_CC);
 -      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_DONE"), PHP_HTTP_MESSAGE_PARSER_STATE_DONE TSRMLS_CC);
 +      php_http_message_parser_object_handlers.free_obj = php_http_message_parser_object_free;
 +      php_http_message_parser_object_handlers.offset = XtOffsetOf(php_http_message_parser_object_t, zo);
 +
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("CLEANUP"), PHP_HTTP_MESSAGE_PARSER_CLEANUP);
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("DUMB_BODIES"), PHP_HTTP_MESSAGE_PARSER_DUMB_BODIES);
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("EMPTY_REDIRECTS"), PHP_HTTP_MESSAGE_PARSER_EMPTY_REDIRECTS);
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("GREEDY"), PHP_HTTP_MESSAGE_PARSER_GREEDY);
 +
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_FAILURE"), PHP_HTTP_MESSAGE_PARSER_STATE_FAILURE);
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_START"), PHP_HTTP_MESSAGE_PARSER_STATE_START);
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_HEADER"), PHP_HTTP_MESSAGE_PARSER_STATE_HEADER);
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_HEADER_DONE"), PHP_HTTP_MESSAGE_PARSER_STATE_HEADER_DONE);
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY);
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY_DUMB"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DUMB);
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY_LENGTH"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH);
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY_CHUNKED"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED);
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_BODY_DONE"), PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE);
++      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_UPDATE_CL"), PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL);
 +      zend_declare_class_constant_long(php_http_message_parser_class_entry, ZEND_STRL("STATE_DONE"), PHP_HTTP_MESSAGE_PARSER_STATE_DONE);
  
        return SUCCESS;
  }
index 71d91f066c964010013f0b671ef57a04042ac83d,0bac9da84ea60c5fcb2b11531a169d69c1f92afc..fa6385845bc7a8906f974430bf378ba1c216a996
@@@ -27,6 -27,7 +27,7 @@@ typedef enum php_http_message_parser_st
        PHP_HTTP_MESSAGE_PARSER_STATE_BODY_LENGTH,
        PHP_HTTP_MESSAGE_PARSER_STATE_BODY_CHUNKED,
        PHP_HTTP_MESSAGE_PARSER_STATE_BODY_DONE,
+       PHP_HTTP_MESSAGE_PARSER_STATE_UPDATE_CL,
        PHP_HTTP_MESSAGE_PARSER_STATE_DONE
  } php_http_message_parser_state_t;
  
@@@ -42,9 -43,12 +43,9 @@@ typedef struct php_http_message_parser 
        php_http_message_t *message;
        php_http_encoding_stream_t *dechunk;
        php_http_encoding_stream_t *inflate;
 -#ifdef ZTS
 -      void ***ts;
 -#endif
  } php_http_message_parser_t;
  
 -PHP_HTTP_API php_http_message_parser_t *php_http_message_parser_init(php_http_message_parser_t *parser TSRMLS_DC);
 +PHP_HTTP_API php_http_message_parser_t *php_http_message_parser_init(php_http_message_parser_t *parser);
  PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_state_push(php_http_message_parser_t *parser, unsigned argc, ...);
  PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_state_is(php_http_message_parser_t *parser);
  PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_state_pop(php_http_message_parser_t *parser);
@@@ -54,18 -58,19 +55,18 @@@ PHP_HTTP_API php_http_message_parser_st
  PHP_HTTP_API php_http_message_parser_state_t php_http_message_parser_parse_stream(php_http_message_parser_t *parser, php_http_buffer_t *buffer, php_stream *s, unsigned flags, php_http_message_t **message);
  
  typedef struct php_http_message_parser_object {
 -      zend_object zo;
 -      zend_object_value zv;
 -      php_http_buffer_t *buffer;
 +      php_http_buffer_t buffer;
        php_http_message_parser_t *parser;
 +      zend_object zo;
  } php_http_message_parser_object_t;
  
  PHP_HTTP_API zend_class_entry *php_http_message_parser_class_entry;
  
  PHP_MINIT_FUNCTION(http_message_parser);
  
 -zend_object_value php_http_message_parser_object_new(zend_class_entry *ce TSRMLS_DC);
 -zend_object_value php_http_message_parser_object_new_ex(zend_class_entry *ce, php_http_message_parser_t *parser, php_http_message_parser_object_t **ptr TSRMLS_DC);
 -void php_http_message_parser_object_free(void *object TSRMLS_DC);
 +zend_object *php_http_message_parser_object_new(zend_class_entry *ce);
 +php_http_message_parser_object_t *php_http_message_parser_object_new_ex(zend_class_entry *ce, php_http_message_parser_t *parser);
 +void php_http_message_parser_object_free(zend_object *object);
  
  #endif
  
diff --combined php_http_misc.h
index d9b9e4ec5fb75df794cfbbf3bcda77c8d2a20d66,cf00f24ef5a71e3d3b21c1a8e0283f2c439e8b97..421c9912cf57082283554c2c4ca759c82a098c4e
@@@ -27,6 -27,9 +27,9 @@@
  /* send buffer size */
  #define PHP_HTTP_SENDBUF_SIZE 40960
  
+ /* allowed characters of header field names */
+ #define PHP_HTTP_HEADER_NAME_CHARS "!#$%&'*+-.^_`|~1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
  /* SLEEP */
  
  #define PHP_HTTP_DIFFSEC (0.001)
@@@ -61,7 -64,7 +64,7 @@@ PHP_HTTP_API void php_http_sleep(doubl
  
  int php_http_match(const char *haystack, const char *needle, int flags);
  char *php_http_pretty_key(register char *key, size_t key_len, zend_bool uctitle, zend_bool xhyphen);
 -size_t php_http_boundary(char *buf, size_t len TSRMLS_DC);
 +size_t php_http_boundary(char *buf, size_t len);
  int php_http_select_str(const char *cmp, int argc, ...);
  
  /* See "A Reusable Duff Device" By Ralf Holly, August 01, 2005 */
@@@ -136,66 -139,126 +139,66 @@@ static inline const char *php_http_loca
  
  /* ZEND */
  
 -#if PHP_VERSION_ID < 50400
 -#     define object_properties_init(o, ce) zend_hash_copy(((zend_object *) o)->properties, &(ce->default_properties), (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval*))
 -#     define PHP_HTTP_ZEND_LITERAL_DC 
 -#     define PHP_HTTP_ZEND_LITERAL_CC
 -#     define PHP_HTTP_ZEND_LITERAL_CCN
 -#     define ZVAL_COPY_VALUE(zv, arr) do { \
 -              (zv)->value = (arr)->value; \
 -              Z_TYPE_P(zv) = Z_TYPE_P(arr); \
 -      } while (0)
 -#else
 -#     define PHP_HTTP_ZEND_LITERAL_DC , const zend_literal *literal_key
 -#     define PHP_HTTP_ZEND_LITERAL_CC , (literal_key)
 -#     define PHP_HTTP_ZEND_LITERAL_CCN , NULL
 -#endif
 -
 -#if PHP_VERSION_ID < 50700
 -#     define z_is_true zend_is_true
 -#else
 -#     define z_is_true(z) zend_is_true(z TSRMLS_CC)
 -#endif
 -
 -#define INIT_PZVAL_ARRAY(zv, ht) \
 -      { \
 -              INIT_PZVAL((zv)); \
 -              Z_TYPE_P(zv) = IS_ARRAY; \
 -              Z_ARRVAL_P(zv) = (ht); \
 -      }
 -
 -static inline zval *php_http_zconv(int type, zval *z)
 +static inline void *PHP_HTTP_OBJ(zend_object *zo, zval *zv)
  {
 -      switch (type) {
 -              case IS_NULL:   convert_to_null_ex(&z);         break;
 -              case IS_BOOL:   convert_to_boolean_ex(&z);      break;
 -              case IS_LONG:   convert_to_long_ex(&z);         break;
 -              case IS_DOUBLE: convert_to_double_ex(&z);       break;
 -              case IS_STRING: convert_to_string_ex(&z);       break;
 -              case IS_ARRAY:  convert_to_array_ex(&z);        break;
 -              case IS_OBJECT: convert_to_object_ex(&z);       break;
 +      if (!zo) {
 +              zo = Z_OBJ_P(zv);
        }
 -      return z;
 +      return (char *) zo - zo->handlers->offset;
  }
  
 -static inline zval *php_http_ztyp(int type, zval *z)
 +static inline zend_string *php_http_cs2zs(char *s, size_t l)
  {
 -      SEPARATE_ARG_IF_REF(z);
 -      return (Z_TYPE_P(z) == type) ? z : php_http_zconv(type, z);
 -}
 +      zend_string *str = erealloc(s, sizeof(*str) + l);
  
 -static inline zval *php_http_zsep(zend_bool add_ref, int type, zval *z)
 -{
 -      if (add_ref) {
 -              Z_ADDREF_P(z);
 -      }
 -      if (Z_TYPE_P(z) != type) {
 -              return php_http_zconv(type, z);
 -      } else {
 -              SEPARATE_ZVAL_IF_NOT_REF(&z);
 -              return z;
 -      }
 +      memmove(str->val, str, l);
 +      str->val[l] = 0;
 +      str->len = l;
 +      str->h = 0;
 +
 +      GC_REFCOUNT(str) = 1;
 +      GC_TYPE_INFO(str) = IS_STRING;
 +
 +      return str;
  }
  
 -static inline STATUS php_http_ini_entry(const char *name_str, size_t name_len, const char **value_str, size_t *value_len, zend_bool orig TSRMLS_DC)
 +static inline ZEND_RESULT_CODE php_http_ini_entry(const char *name_str, size_t name_len, const char **val_str, size_t *val_len, zend_bool orig)
  {
        zend_ini_entry *ini_entry;
  
 -      if (SUCCESS == zend_hash_find(EG(ini_directives), name_str, name_len + 1, (void *) &ini_entry)) {
 +      if ((ini_entry = zend_hash_str_find_ptr(EG(ini_directives), name_str, name_len))) {
                if (orig && ini_entry->modified) {
 -                      *value_str = ini_entry->orig_value;
 -                      *value_len = (size_t) ini_entry->orig_value_length;
 +                      *val_str = ini_entry->orig_value->val;
 +                      *val_len = ini_entry->orig_value->len;
                } else {
 -                      *value_str = ini_entry->value;
 -                      *value_len = (size_t) ini_entry->value_length;
 +                      *val_str = ini_entry->value->val;
 +                      *val_len = ini_entry->value->len;
                }
                return SUCCESS;
        }
        return FAILURE;
  }
  
 +#define Z_ISUSER(zv) (Z_TYPE(zv) <= 10)
 +#define Z_ISUSER_P(zvp) Z_ISUSER(*(zvp))
 +
 +#define RETVAL_STR_COPY(zs) ZVAL_STR_COPY(return_value, zs)
 +#define RETURN_STR_COPY(zs) do { \
 +      ZVAL_STR_COPY(return_value, zs); \
 +      return; \
 +}
 +
  /* return object(values) */
 +#define ZVAL_OBJECT(z, o, addref) \
 +      ZVAL_OBJ(z, o); \
 +      if (addref) { \
 +              Z_ADDREF_P(z); \
 +      }
  #define RETVAL_OBJECT(o, addref) \
 -      RETVAL_OBJVAL((o)->value.obj, addref)
 +      ZVAL_OBJECT(return_value, o, addref)
  #define RETURN_OBJECT(o, addref) \
        RETVAL_OBJECT(o, addref); \
        return
 -#define RETVAL_OBJVAL(ov, addref) \
 -      ZVAL_OBJVAL(return_value, ov, addref)
 -#define RETURN_OBJVAL(ov, addref) \
 -      RETVAL_OBJVAL(ov, addref); \
 -      return
 -#define ZVAL_OBJVAL(zv, ov, addref) \
 -      (zv)->type = IS_OBJECT; \
 -      (zv)->value.obj = (ov);\
 -      if (addref && Z_OBJ_HT_P(zv)->add_ref) { \
 -              Z_OBJ_HT_P(zv)->add_ref((zv) TSRMLS_CC); \
 -      }
 -
 -#define Z_OBJ_DELREF(z) \
 -      if (Z_OBJ_HT(z)->del_ref) { \
 -              Z_OBJ_HT(z)->del_ref(&(z) TSRMLS_CC); \
 -      }
 -#define Z_OBJ_ADDREF(z) \
 -      if (Z_OBJ_HT(z)->add_ref) { \
 -              Z_OBJ_HT(z)->add_ref(&(z) TSRMLS_CC); \
 -      }
 -#define Z_OBJ_DELREF_P(z) \
 -      if (Z_OBJ_HT_P(z)->del_ref) { \
 -              Z_OBJ_HT_P(z)->del_ref((z) TSRMLS_CC); \
 -      }
 -#define Z_OBJ_ADDREF_P(z) \
 -      if (Z_OBJ_HT_P(z)->add_ref) { \
 -              Z_OBJ_HT_P(z)->add_ref((z) TSRMLS_CC); \
 -      }
 -#define Z_OBJ_DELREF_PP(z) \
 -      if (Z_OBJ_HT_PP(z)->del_ref) { \
 -              Z_OBJ_HT_PP(z)->del_ref(*(z) TSRMLS_CC); \
 -      }
 -#define Z_OBJ_ADDREF_PP(z) \
 -      if (Z_OBJ_HT_PP(z)->add_ref) { \
 -              Z_OBJ_HT_PP(z)->add_ref(*(z) TSRMLS_CC); \
 -      }
  
  #define EMPTY_FUNCTION_ENTRY {NULL, NULL, NULL, 0, 0}
  
  
  /* ARRAYS */
  
 -#ifndef HASH_KEY_NON_EXISTENT
 -#     define HASH_KEY_NON_EXISTENT HASH_KEY_NON_EXISTANT
 -#endif
 -
 -PHP_HTTP_API unsigned php_http_array_list(HashTable *ht TSRMLS_DC, unsigned argc, ...);
 +PHP_HTTP_API unsigned php_http_array_list(HashTable *ht, unsigned argc, ...);
  
 -typedef struct php_http_array_hashkey {
 -      char *str;
 -      uint len;
 -      ulong num;
 -      uint dup:1;
 -      uint type:31;
 -} php_http_array_hashkey_t;
 -#define php_http_array_hashkey_init(dup) {NULL, 0, 0, (dup), 0}
 +typedef struct php_http_arrkey {
 +      zend_ulong h;
 +      zend_string *key;
 +      unsigned allocated:1;
 +      unsigned stringified:1;
 +} php_http_arrkey_t;
  
 -static inline void php_http_array_hashkey_stringify(php_http_array_hashkey_t *key)
 +static inline void *php_http_arrkey_stringify(php_http_arrkey_t *arrkey, zend_hash_key *key)
  {
 -      if (key->type != HASH_KEY_IS_STRING) {
 -              key->len = spprintf(&key->str, 0, "%lu", key->num) + 1;
 +      if (arrkey) {
 +              arrkey->allocated = 0;
 +      } else {
 +              arrkey = emalloc(sizeof(*arrkey));
 +              arrkey->allocated = 1;
 +      }
 +
 +      if (key) {
 +              memcpy(arrkey, key, sizeof(*key));
 +      }
 +      if ((arrkey->stringified = !arrkey->key)) {
 +              arrkey->key = zend_long_to_str(arrkey->h);
        }
 +      return arrkey;
  }
  
 -static inline void php_http_array_hashkey_stringfree(php_http_array_hashkey_t *key)
 +static inline void php_http_arrkey_dtor(php_http_arrkey_t *arrkey)
  {
 -      if (key->type != HASH_KEY_IS_STRING || key->dup) {
 -              PTR_FREE(key->str);
 +      if (arrkey->stringified) {
 +              zend_string_release(arrkey->key);
 +      }
 +      if (arrkey->allocated) {
 +              efree(arrkey);
        }
  }
  
 -#define FOREACH_VAL(pos, array, val) FOREACH_HASH_VAL(pos, HASH_OF(array), val)
 -#define FOREACH_HASH_VAL(pos, hash, val) \
 -      for (   zend_hash_internal_pointer_reset_ex(hash, &pos); \
 -                      zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
 -                      zend_hash_move_forward_ex(hash, &pos))
 -
 -#define FOREACH_KEY(pos, array, key) FOREACH_HASH_KEY(pos, HASH_OF(array), key)
 -#define FOREACH_HASH_KEY(pos, hash, _key) \
 -      for (   zend_hash_internal_pointer_reset_ex(hash, &pos); \
 -                      ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT; \
 -                      zend_hash_move_forward_ex(hash, &pos)) \
 -
 -#define FOREACH_KEYVAL(pos, array, key, val) FOREACH_HASH_KEYVAL(pos, HASH_OF(array), key, val)
 -#define FOREACH_HASH_KEYVAL(pos, hash, _key, val) \
 -      for (   zend_hash_internal_pointer_reset_ex(hash, &pos); \
 -                      ((_key).type = zend_hash_get_current_key_ex(hash, &(_key).str, &(_key).len, &(_key).num, (zend_bool) (_key).dup, &pos)) != HASH_KEY_NON_EXISTANT && \
 -                      zend_hash_get_current_data_ex(hash, (void *) &val, &pos) == SUCCESS; \
 -                      zend_hash_move_forward_ex(hash, &pos))
 -
 -#define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *))
 -#define array_copy_strings(src, dst) zend_hash_copy(dst, src, php_http_array_copy_strings, NULL, sizeof(zval *))
 +#define array_copy(src, dst) zend_hash_copy(dst, src, (copy_ctor_func_t) zval_add_ref)
 +#define array_copy_strings(src, dst) zend_hash_copy(dst, src, php_http_array_copy_strings)
  #define ARRAY_JOIN_STRONLY   0x01
  #define ARRAY_JOIN_PRETTIFY  0x02
  #define ARRAY_JOIN_STRINGIFY 0x04
 -#define array_join(src, dst, append, flags) zend_hash_apply_with_arguments(src TSRMLS_CC, (append)?php_http_array_apply_append_func:php_http_array_apply_merge_func, 2, dst, (int)flags)
 +#define array_join(src, dst, append, flags) zend_hash_apply_with_arguments(src, (append)?php_http_array_apply_append_func:php_http_array_apply_merge_func, 2, dst, (int)flags)
  
 -void php_http_array_copy_strings(void *zpp);
 -int php_http_array_apply_append_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
 -int php_http_array_apply_merge_func(void *pDest TSRMLS_DC, int num_args, va_list args, zend_hash_key *hash_key);
 +void php_http_array_copy_strings(zval *zp);
 +int php_http_array_apply_append_func(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key);
 +int php_http_array_apply_merge_func(zval *pDest, int num_args, va_list args, zend_hash_key *hash_key);
  
  /* PASS CALLBACK */
  
@@@ -261,9 -335,12 +264,9 @@@ typedef size_t (*php_http_pass_php_http
  typedef size_t (*php_http_pass_format_callback_t)(void *cb_arg, const char *fmt, ...);
  
  typedef struct php_http_pass_fcall_arg {
 -      zval *fcz;
 +      zval fcz;
        zend_fcall_info fci;
        zend_fcall_info_cache fcc;
 -#ifdef ZTS
 -      void ***ts;
 -#endif
  } php_http_pass_fcall_arg_t;
  
  PHP_HTTP_API size_t php_http_pass_fcall_callback(void *cb_arg, const char *str, size_t len);
diff --combined php_http_url.h
index b7402e93cff861a025be126e68b3f72d211291f5,303b9361b83fa895f188855948ed9420afb05be4..6ae0ac38af2b081d9967b1876b544e25fac0146c
@@@ -57,43 -57,25 +57,28 @@@ typedef struct php_http_url 
        char *fragment;
  } php_http_url_t;
  
 -PHP_HTTP_API php_http_url_t *php_http_url_parse(const char *str, size_t len, unsigned flags TSRMLS_DC);
 -PHP_HTTP_API php_http_url_t *php_http_url_parse_authority(const char *str, size_t len, unsigned flags TSRMLS_DC);
 -PHP_HTTP_API php_http_url_t *php_http_url_mod(const php_http_url_t *old_url, const php_http_url_t *new_url, unsigned flags TSRMLS_DC);
 +PHP_HTTP_API php_http_url_t *php_http_url_parse(const char *str, size_t len, unsigned flags);
 +PHP_HTTP_API php_http_url_t *php_http_url_parse_authority(const char *str, size_t len, unsigned flags);
 +PHP_HTTP_API php_http_url_t *php_http_url_mod(const php_http_url_t *old_url, const php_http_url_t *new_url, unsigned flags);
  PHP_HTTP_API php_http_url_t *php_http_url_copy(const php_http_url_t *url, zend_bool persistent);
  PHP_HTTP_API php_http_url_t *php_http_url_from_struct(HashTable *ht);
 -PHP_HTTP_API php_http_url_t *php_http_url_from_zval(zval *value, unsigned flags TSRMLS_DC);
 -PHP_HTTP_API HashTable *php_http_url_to_struct(const php_http_url_t *url, zval *strct TSRMLS_DC);
 +PHP_HTTP_API php_http_url_t *php_http_url_from_zval(zval *value, unsigned flags);
 +PHP_HTTP_API HashTable *php_http_url_to_struct(const php_http_url_t *url, zval *strct);
  PHP_HTTP_API char *php_http_url_to_string(const php_http_url_t *url, char **url_str, size_t *url_len, zend_bool persistent);
  PHP_HTTP_API char *php_http_url_authority_to_string(const php_http_url_t *url, char **url_str, size_t *url_len);
  PHP_HTTP_API void php_http_url_free(php_http_url_t **url);
  
 -PHP_HTTP_API STATUS php_http_url_encode_hash(HashTable *hash, const char *pre_encoded_str, size_t pre_encoded_len, char **encoded_str, size_t *encoded_len TSRMLS_DC);
 -PHP_HTTP_API STATUS php_http_url_encode_hash_ex(HashTable *hash, php_http_buffer_t *qstr, const char *arg_sep_str, size_t arg_sep_len, const char *val_sep_str, size_t val_sep_len, const char *pre_encoded_str, size_t pre_encoded_len TSRMLS_DC);
 +PHP_HTTP_API ZEND_RESULT_CODE php_http_url_encode_hash(HashTable *hash, const char *pre_encoded_str, size_t pre_encoded_len, char **encoded_str, size_t *encoded_len);
 +PHP_HTTP_API ZEND_RESULT_CODE php_http_url_encode_hash_ex(HashTable *hash, php_http_buffer_t *qstr, const char *arg_sep_str, size_t arg_sep_len, const char *val_sep_str, size_t val_sep_len, const char *pre_encoded_str, size_t pre_encoded_len);
  
 -static inline void php_http_url_argsep(const char **str, size_t *len TSRMLS_DC)
 +static inline void php_http_url_argsep(const char **str, size_t *len)
  {
 -      php_http_ini_entry(ZEND_STRL("arg_separator.output"), str, len, 0 TSRMLS_CC);
 +      if (SUCCESS != php_http_ini_entry(ZEND_STRL("arg_separator.output"), str, len, 0) || !*len) {
 +              *str = PHP_HTTP_URL_ARGSEP;
 +              *len = lenof(PHP_HTTP_URL_ARGSEP);
 +      }
  }
  
- static inline php_url *php_http_url_to_php_url(php_http_url_t *url)
- {
-       php_url *purl = ecalloc(1, sizeof(*purl));
-       if (url->scheme)   purl->scheme   = estrdup(url->scheme);
-       if (url->pass)     purl->pass     = estrdup(url->pass);
-       if (url->user)     purl->user     = estrdup(url->user);
-       if (url->host)     purl->host     = estrdup(url->host);
-       if (url->path)     purl->path     = estrdup(url->path);
-       if (url->query)    purl->query    = estrdup(url->query);
-       if (url->fragment) purl->fragment = estrdup(url->fragment);
-       return purl;
- }
  static inline zend_bool php_http_url_is_empty(const php_http_url_t *url) {
        return !(url->scheme || url->pass || url->user || url->host || url->port ||     url->path || url->query || url->fragment);
  }
index c70476667c07365f8d0313900549d93549bff8cc,71f2b5e2b2741874893336cc0caf340d22fedd83..acac6cd82d915ebe5d447f9e572b0bd9e9df4018
@@@ -37,7 -37,7 +37,7 @@@ object(http\Message)#%d (9) 
    ["type":protected]=>
    int(1)
    ["body":protected]=>
 -  object(http\Message\Body)#2 (0) {
 +  object(http\Message\Body)#%d (0) {
    }
    ["requestMethod":protected]=>
    string(3) "GET"
@@@ -54,7 -54,7 +54,7 @@@
      ["Host"]=>
      string(9) "localhost"
      ["Content-Length"]=>
-     int(3)
+     string(1) "3"
      ["X-Original-Content-Length"]=>
      string(1) "3"
    }
@@@ -63,4 -63,4 +63,4 @@@
  }
  string(3) "OK
  "
--DONE
++DONE
diff --combined tests/phpinfo.phpt
index 0000000000000000000000000000000000000000,373cb45e1ad11873b608042a5dee158ab02cdd8e..c1f58d93269c215dba5d64e974fe52c9fa2ec744
mode 000000,100644..100644
--- /dev/null
@@@ -1,0 -1,19 +1,19 @@@
 -Extension Version => 2.%s
+ --TEST--
+ phpinfo
+ --SKIPIF--
+ <?php
+ include "skipif.inc";
+ ?>
+ --FILE--
+ <?php
+ echo "Test\n";
+ phpinfo(INFO_MODULES);
+ ?>
+ Done
+ --EXPECTF--
+ Test
+ %a
+ HTTP Support => enabled
++Extension Version => 3.%s
+ %a
+ Done