Fix error: redefinition of typedef 'php_http_message_t' during RHEL build
[m6w6/ext-http] / php_http_message.c
index 76c79ff0ec340c15d1ba8b5386b6da6def53e522..7dbf6085eb2b49004505ac5b1cbf413d66c66e12 100644 (file)
@@ -6,30 +6,28 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2010, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2011, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
-/* $Id: http_message_api.c 298689 2010-04-28 06:50:06Z mike $ */
-
-#include "php_http.h"
-
-#include <main/SAPI.h>
-#include <ext/spl/spl_iterators.h>
-#include <Zend/zend_interfaces.h>
+#include "php_http_api.h"
 
 PHP_HTTP_API zend_bool php_http_message_info_callback(php_http_message_t **message, HashTable **headers, php_http_info_t *info TSRMLS_DC)
 {
        php_http_message_t *old = *message;
 
        /* advance message */
-       if (old->type || zend_hash_num_elements(&old->hdrs) || PHP_HTTP_BUFFER_LEN(old)) {
-               (*message) = php_http_message_init(NULL, 0);
+       if (!old || old->type || zend_hash_num_elements(&old->hdrs) || PHP_HTTP_BUFFER_LEN(old)) {
+               (*message) = php_http_message_init(NULL, 0 TSRMLS_CC);
                (*message)->parent = old;
-               (*headers) = &((*message)->hdrs);
+               if (headers) {
+                       (*headers) = &((*message)->hdrs);
+               }
        }
 
-       php_http_message_set_info(*message, info);
+       if (info) {
+               php_http_message_set_info(*message, info);
+       }
 
        return old != *message;
 }
@@ -60,7 +58,7 @@ PHP_HTTP_API php_http_message_t *php_http_message_init_env(php_http_message_t *m
        switch (type) {
                case PHP_HTTP_REQUEST:
                        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));
+                               php_http_version_parse(&message->http.version, Z_STRVAL_P(sval) TSRMLS_CC);
                        } else {
                                message->http.version.major = 1;
                                message->http.version.minor = 1;
@@ -71,12 +69,20 @@ PHP_HTTP_API php_http_message_t *php_http_message_init_env(php_http_message_t *m
                        if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_URI"), 1 TSRMLS_CC))) {
                                message->http.info.request.url = estrdup(Z_STRVAL_P(sval));
                        }
+                       if ((sval = php_http_env_get_server_var(ZEND_STRL("CONTENT_TYPE"), 1 TSRMLS_CC))) {
+                               Z_ADDREF_P(sval);
+                               zend_hash_update(&message->hdrs, "Content-Type", sizeof("Content-Type"), (void *) &sval, sizeof(zval *), NULL);
+                       }
+                       if ((sval = php_http_env_get_server_var(ZEND_STRL("CONTENT_LENGTH"), 1 TSRMLS_CC))) {
+                               Z_ADDREF_P(sval);
+                               zend_hash_update(&message->hdrs, "Content-Length", sizeof("Content-Length"), (void *) &sval, sizeof(zval *), NULL);
+                       }
                        
                        php_http_env_get_request_headers(&message->hdrs TSRMLS_CC);
 
                        if ((mbody = php_http_env_get_request_body(TSRMLS_C))) {
                                php_http_message_body_dtor(&message->body);
-                               php_http_message_body_copy(mbody, &message->body, 0 TSRMLS_CC);
+                               php_http_message_body_copy(mbody, &message->body, 0);
                        }
                        break;
                        
@@ -99,7 +105,7 @@ PHP_HTTP_API php_http_message_t *php_http_message_init_env(php_http_message_t *m
                        
                        php_http_env_get_response_headers(&message->hdrs TSRMLS_CC);
 
-                       if (php_output_get_level()) {
+                       if (php_output_get_level(TSRMLS_C)) {
                                if (php_output_get_status(TSRMLS_C) & PHP_OUTPUT_SENT) {
                                        php_http_error(HE_WARNING, PHP_HTTP_E_RUNTIME, "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;
@@ -130,17 +136,25 @@ PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *msg,
 {
        php_http_message_parser_t p;
        php_http_buffer_t buf;
+       int free_msg;
 
-       if (!msg) {
-               msg = php_http_message_init(NULL, 0 TSRMLS_CC);
-       }
        php_http_buffer_from_string_ex(&buf, str, len);
        php_http_message_parser_init(&p TSRMLS_CC);
-       php_http_message_parser_parse(&p, &buf, PHP_HTTP_MESSAGE_PARSER_CLEANUP, &msg);
+
+       if ((free_msg = !msg)) {
+               msg = php_http_message_init(NULL, 0 TSRMLS_CC);
+       }
+
+       if (FAILURE == php_http_message_parser_parse(&p, &buf, PHP_HTTP_MESSAGE_PARSER_CLEANUP, &msg)) {
+               if (free_msg) {
+                       php_http_message_free(&msg);
+               }
+               msg = NULL;
+       }
+
        php_http_message_parser_dtor(&p);
        php_http_buffer_dtor(&buf);
 
-       /* FIXME */
        return msg;
 }
 
@@ -149,16 +163,17 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st
        zval *ret = NULL, **header;
        char *key = php_http_pretty_key(estrndup(key_str, key_len), key_len, 1, 1);
 
-       if (SUCCESS == zend_hash_find(&msg->hdrs, key, key_len + 1, (void *) &header)) {
+       if (SUCCESS == zend_symtable_find(&msg->hdrs, key, key_len + 1, (void *) &header)) {
                if (join && Z_TYPE_PP(header) == IS_ARRAY) {
                        zval *header_str, **val;
                        HashPosition pos;
                        php_http_buffer_t str;
+                       TSRMLS_FETCH_FROM_CTX(msg->ts);
 
                        php_http_buffer_init(&str);
                        MAKE_STD_ZVAL(header_str);
                        FOREACH_VAL(pos, *header, val) {
-                               zval *strval = php_http_zsep(IS_STRING, *val);
+                               zval *strval = php_http_ztyp(IS_STRING, *val);
                                php_http_buffer_appendf(&str, PHP_HTTP_BUFFER_LEN(&str) ? ", %s":"%s", Z_STRVAL_P(strval));
                                zval_ptr_dtor(&strval);
                        }
@@ -166,7 +181,8 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st
                        ZVAL_STRINGL(header_str, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str), 0);
                        ret = header_str;
                } else {
-                       ret = php_http_zsep(IS_STRING, *header);
+                       Z_ADDREF_PP(header);
+                       ret = *header;
                }
        }
 
@@ -175,6 +191,63 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st
        return ret;
 }
 
+PHP_HTTP_API 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_bool is_multipart = 0;
+       TSRMLS_FETCH_FROM_CTX(msg->ts);
+
+       if (ct) {
+               php_http_params_opts_t popts;
+               HashTable params;
+
+               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);
+
+               if (php_http_params_parse(&params, &popts TSRMLS_CC)) {
+                       zval **cur, **arg;
+                       char *ct_str;
+
+                       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 (php_http_match(ct_str, "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
+                                       ) {
+                                               zval **val;
+                                               HashPosition pos;
+                                               php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
+
+                                               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);
+
+                                                               if (Z_STRLEN_P(bnd)) {
+                                                                       *boundary = estrndup(Z_STRVAL_P(bnd), Z_STRLEN_P(bnd));
+                                                               }
+                                                               zval_ptr_dtor(&bnd);
+                                                       }
+                                               }
+                                       }
+                               }
+                       }
+               }
+               zend_hash_destroy(&params);
+               zval_ptr_dtor(&ct);
+       }
+
+       return is_multipart;
+}
 
 /* */
 PHP_HTTP_API void php_http_message_set_type(php_http_message_t *message, php_http_message_type_t type)
@@ -183,7 +256,7 @@ PHP_HTTP_API void php_http_message_set_type(php_http_message_t *message, php_htt
        if (type != message->type) {
 
                /* free request info */
-               switch (message->type = type) {
+               switch (message->type) {
                        case PHP_HTTP_REQUEST:
                                STR_FREE(message->http.info.request.method);
                                STR_FREE(message->http.info.request.url);
@@ -197,6 +270,7 @@ PHP_HTTP_API void php_http_message_set_type(php_http_message_t *message, php_htt
                                break;
                }
 
+               message->type = type;
                memset(&message->http, 0, sizeof(message->http));
        }
 }
@@ -227,6 +301,7 @@ static inline void message_headers(php_http_message_t *msg, php_http_buffer_t *s
        HashPosition pos1;
        zval **header, *h;
        size_t size;
+       TSRMLS_FETCH_FROM_CTX(msg->ts);
 
        switch (msg->type) {
                case PHP_HTTP_REQUEST:
@@ -244,7 +319,7 @@ static inline void message_headers(php_http_message_t *msg, php_http_buffer_t *s
        if ((size = php_http_message_body_size(&msg->body))) {
                MAKE_STD_ZVAL(h);
                ZVAL_LONG(h, size);
-               zend_hash_add(&msg->hdrs, "Content-Length", sizeof("Content-Length"), &h, sizeof(zval *), NULL);
+               zend_hash_update(&msg->hdrs, "Content-Length", sizeof("Content-Length"), &h, sizeof(zval *), NULL);
 
                if (msg->body.boundary) {
                        char *str;
@@ -317,7 +392,6 @@ static inline void message_headers(php_http_message_t *msg, php_http_buffer_t *s
 PHP_HTTP_API void php_http_message_to_callback(php_http_message_t *msg, php_http_pass_callback_t cb, void *cb_arg)
 {
        php_http_buffer_t str;
-       TSRMLS_FETCH_FROM_CTX(msg->ts);
 
        php_http_buffer_init_ex(&str, 0x1000, 0);
        message_headers(msg, &str);
@@ -325,9 +399,9 @@ PHP_HTTP_API void php_http_message_to_callback(php_http_message_t *msg, php_http
        php_http_buffer_dtor(&str);
 
        if (php_http_message_body_size(&msg->body)) {
-               cb(cb_arg, ZEND_STRL(PHP_HTTP_CRLF) TSRMLS_CC);
+               cb(cb_arg, ZEND_STRL(PHP_HTTP_CRLF));
                php_http_message_body_to_callback(&msg->body, cb, cb_arg, 0, 0);
-               cb(cb_arg, ZEND_STRL(PHP_HTTP_CRLF) TSRMLS_CC);
+               cb(cb_arg, ZEND_STRL(PHP_HTTP_CRLF));
        }
 }
 
@@ -381,8 +455,9 @@ PHP_HTTP_API php_http_message_t *php_http_message_reverse(php_http_message_t *ms
        php_http_message_count(c, msg);
        
        if (c > 1) {
-               php_http_message_t *tmp = msg, **arr = ecalloc(c, sizeof(**arr));
-               
+               php_http_message_t *tmp = msg, **arr;
+
+               arr = ecalloc(c, sizeof(**arr));
                for (i = 0; i < c; ++i) {
                        arr[i] = tmp;
                        tmp = tmp->parent;
@@ -425,11 +500,12 @@ PHP_HTTP_API php_http_message_t *php_http_message_interconnect(php_http_message_
        return m1;
 }
 
-PHP_HTTP_API void php_http_message_to_struct(php_http_message_t *msg, zval *obj TSRMLS_DC)
+PHP_HTTP_API void php_http_message_to_struct(php_http_message_t *msg, zval *obj)
 {
        zval strct;
        zval *headers;
        char *version;
+       TSRMLS_FETCH_FROM_CTX(msg->ts);
        
        INIT_PZVAL_ARRAY(&strct, HASH_OF(obj));
        
@@ -564,7 +640,7 @@ PHP_HTTP_API STATUS _http_message_send(http_message *message TSRMLS_DC)
 }
 */
 
-PHP_HTTP_API php_http_message_t *php_http_message_copy(php_http_message_t *from, php_http_message_t *to)
+PHP_HTTP_API php_http_message_t *php_http_message_copy_ex(php_http_message_t *from, php_http_message_t *to, zend_bool parents)
 {
        php_http_message_t *temp, *copy = NULL;
        php_http_info_t info;
@@ -574,19 +650,19 @@ PHP_HTTP_API php_http_message_t *php_http_message_copy(php_http_message_t *from,
                info.type = from->type;
                info.http = from->http;
                
-               copy = temp = php_http_message_init(to, 0);
+               copy = temp = php_http_message_init(to, 0 TSRMLS_CC);
                php_http_message_set_info(temp, &info);
                zend_hash_copy(&temp->hdrs, &from->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
                php_http_message_body_copy(&from->body, &temp->body, 1);
        
-               while (from->parent) {
+               if (parents) while (from->parent) {
                        info.type = from->parent->type;
                        info.http = from->parent->http;
                
-                       temp->parent = php_http_message_init(NULL, 0);
+                       temp->parent = php_http_message_init(NULL, 0 TSRMLS_CC);
                        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 *));
-                       php_http_message_body_copy(&from->body, &temp->body, 1);
+                       php_http_message_body_copy(&from->parent->body, &temp->parent->body, 1);
                
                        temp = temp->parent;
                        from = from->parent;
@@ -596,6 +672,11 @@ PHP_HTTP_API php_http_message_t *php_http_message_copy(php_http_message_t *from,
        return copy;
 }
 
+PHP_HTTP_API 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);
+}
+
 PHP_HTTP_API void php_http_message_dtor(php_http_message_t *message)
 {
        if (message) {
@@ -702,20 +783,12 @@ PHP_HTTP_BEGIN_ARGS(setHttpVersion, 1)
        PHP_HTTP_ARG_VAL(http_version, 0)
 PHP_HTTP_END_ARGS;
 
-PHP_HTTP_BEGIN_ARGS(guessContentType, 1)
-       PHP_HTTP_ARG_VAL(magic_file, 0)
-       PHP_HTTP_ARG_VAL(magic_mode, 0)
-PHP_HTTP_END_ARGS;
-
 PHP_HTTP_EMPTY_ARGS(getParentMessage);
-PHP_HTTP_EMPTY_ARGS(send);
 PHP_HTTP_EMPTY_ARGS(__toString);
 PHP_HTTP_BEGIN_ARGS(toString, 0)
        PHP_HTTP_ARG_VAL(include_parent, 0)
 PHP_HTTP_END_ARGS;
 
-PHP_HTTP_EMPTY_ARGS(toMessageTypeObject);
-
 PHP_HTTP_EMPTY_ARGS(count);
 
 PHP_HTTP_EMPTY_ARGS(serialize);
@@ -735,6 +808,11 @@ PHP_HTTP_BEGIN_ARGS(prepend, 1)
 PHP_HTTP_END_ARGS;
 PHP_HTTP_EMPTY_ARGS(reverse);
 
+PHP_HTTP_BEGIN_ARGS(isMultipart, 0)
+       PHP_HTTP_ARG_VAL(boundary, 1)
+PHP_HTTP_END_ARGS;
+PHP_HTTP_EMPTY_ARGS(splitMultipartBody);
+
 static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, const zend_literal *literal_key TSRMLS_DC);
 static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *literal_key TSRMLS_DC);
 static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member, const zend_literal *literal_key TSRMLS_DC);
@@ -788,6 +866,9 @@ zend_function_entry php_http_message_method_entry[] = {
        PHP_HTTP_MESSAGE_ME(prepend, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_ME(reverse, ZEND_ACC_PUBLIC)
 
+       PHP_HTTP_MESSAGE_ME(isMultipart, ZEND_ACC_PUBLIC)
+       PHP_HTTP_MESSAGE_ME(splitMultipartBody, ZEND_ACC_PUBLIC)
+
        EMPTY_FUNCTION_ENTRY
 };
 static zend_object_handlers php_http_message_object_handlers;
@@ -804,9 +885,11 @@ static STATUS php_http_message_object_add_prophandler(const char *prop_str, size
        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);
 }
+/*
 static int php_http_message_object_has_prophandler(const char *prop_str, size_t prop_len) {
        return zend_hash_exists(&php_http_message_object_prophandlers, prop_str, prop_len + 1);
 }
+*/
 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);
 }
@@ -814,7 +897,7 @@ static void php_http_message_object_prophandler_get_type(php_http_message_object
        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_zsep(IS_LONG, value);
+       zval *cpy = php_http_ztyp(IS_LONG, value);
        php_http_message_set_type(obj->message, Z_LVAL_P(cpy));
        zval_ptr_dtor(&cpy);
 }
@@ -827,7 +910,7 @@ static void php_http_message_object_prophandler_get_request_method(php_http_mess
 }
 static void php_http_message_object_prophandler_set_request_method(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
        if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
-               zval *cpy = php_http_zsep(IS_STRING, value);
+               zval *cpy = php_http_ztyp(IS_STRING, value);
                STR_SET(obj->message->http.info.request.method, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
                zval_ptr_dtor(&cpy);
        }
@@ -841,7 +924,7 @@ static void php_http_message_object_prophandler_get_request_url(php_http_message
 }
 static void php_http_message_object_prophandler_set_request_url(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
        if (PHP_HTTP_MESSAGE_TYPE(REQUEST, obj->message)) {
-               zval *cpy = php_http_zsep(IS_STRING, value);
+               zval *cpy = php_http_ztyp(IS_STRING, value);
                STR_SET(obj->message->http.info.request.url, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
                zval_ptr_dtor(&cpy);
        }
@@ -855,7 +938,7 @@ static void php_http_message_object_prophandler_get_response_status(php_http_mes
 }
 static void php_http_message_object_prophandler_set_response_status(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
        if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
-               zval *cpy = php_http_zsep(IS_STRING, value);
+               zval *cpy = php_http_ztyp(IS_STRING, value);
                STR_SET(obj->message->http.info.response.status, estrndup(Z_STRVAL_P(cpy), Z_STRLEN_P(cpy)));
                zval_ptr_dtor(&cpy);
        }
@@ -869,7 +952,7 @@ static void php_http_message_object_prophandler_get_response_code(php_http_messa
 }
 static void php_http_message_object_prophandler_set_response_code(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
        if (PHP_HTTP_MESSAGE_TYPE(RESPONSE, obj->message)) {
-               zval *cpy = php_http_zsep(IS_LONG, value);
+               zval *cpy = php_http_ztyp(IS_LONG, value);
                obj->message->http.info.response.code = Z_LVAL_P(cpy);
                zval_ptr_dtor(&cpy);
        }
@@ -882,7 +965,7 @@ static void php_http_message_object_prophandler_get_http_version(php_http_messag
        RETVAL_STRINGL(version_str, version_len, 0);
 }
 static void php_http_message_object_prophandler_set_http_version(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
-       zval *cpy = php_http_zsep(IS_STRING, value);
+       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);
 }
@@ -891,7 +974,7 @@ static void php_http_message_object_prophandler_get_headers(php_http_message_obj
        zend_hash_copy(Z_ARRVAL_P(return_value), &obj->message->hdrs, (copy_ctor_func_t) zval_add_ref, NULL, sizeof(zval *));
 }
 static void php_http_message_object_prophandler_set_headers(php_http_message_object_t *obj, zval *value TSRMLS_DC) {
-       zval *cpy = php_http_zsep(IS_ARRAY, value);
+       zval *cpy = php_http_ztyp(IS_ARRAY, 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 *));
@@ -941,7 +1024,7 @@ PHP_MINIT_FUNCTION(http_message)
        php_http_message_object_handlers.get_properties = php_http_message_object_get_props;
        php_http_message_object_handlers.get_property_ptr_ptr = php_http_message_object_get_prop_ptr;
 
-       zend_class_implements(php_http_message_class_entry TSRMLS_CC, 3, spl_ce_Countable, zend_ce_serializable, zend_ce_iterator, php_http_fluently_callable_class_entry);
+       zend_class_implements(php_http_message_class_entry TSRMLS_CC, 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);
@@ -1105,7 +1188,7 @@ zend_object_value php_http_message_object_clone(zval *this_ptr TSRMLS_DC)
        php_http_message_object_t *new_obj = NULL;
        php_http_message_object_t *old_obj = zend_object_store_get_object(this_ptr TSRMLS_CC);
 
-       new_ov = php_http_message_object_new_ex(old_obj->zo.ce, php_http_message_copy(old_obj->message, NULL), &new_obj);
+       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);
 
        return new_ov;
@@ -1139,11 +1222,11 @@ void php_http_message_object_free(void *object TSRMLS_DC)
 static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member, const zend_literal *literal_key TSRMLS_DC)
 {
        php_http_message_object_prophandler_t *handler;
-       zval *copy = php_http_zsep(IS_STRING, member);
+       zval *copy = php_http_ztyp(IS_STRING, member);
 
        if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
                zval_ptr_dtor(&copy);
-               return &php_http_property_proxy_init(NULL, object, member TSRMLS_CC)->myself;
+               return &php_http_property_proxy_init(NULL, object, member, NULL TSRMLS_CC)->myself;
        }
        zval_ptr_dtor(&copy);
 
@@ -1154,21 +1237,19 @@ static zval *php_http_message_object_read_prop(zval *object, zval *member, int t
 {
        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_zsep(IS_STRING, member);
+       zval *return_value, *copy = php_http_ztyp(IS_STRING, member);
 
        if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
-               if (type == BP_VAR_W) {
-                       zval_ptr_dtor(&copy);
+               if (type == BP_VAR_R) {
+                       ALLOC_ZVAL(return_value);
+                       Z_SET_REFCOUNT_P(return_value, 0);
+                       Z_UNSET_ISREF_P(return_value);
+
+                       handler->read(obj, return_value TSRMLS_CC);
+               } else {
                        zend_error(E_ERROR, "Cannot access HttpMessage properties by reference or array key/index");
-                       return NULL;
+                       return_value = NULL;
                }
-
-               ALLOC_ZVAL(return_value);
-               Z_SET_REFCOUNT_P(return_value, 0);
-               Z_UNSET_ISREF_P(return_value);
-
-               handler->read(obj, return_value TSRMLS_CC);
-
        } else {
                return_value = zend_get_std_object_handlers()->read_property(object, member, type, literal_key TSRMLS_CC);
        }
@@ -1182,7 +1263,7 @@ static void php_http_message_object_write_prop(zval *object, zval *member, zval
 {
        php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
        php_http_message_object_prophandler_t *handler;
-       zval *copy = php_http_zsep(IS_STRING, member);
+       zval *copy = php_http_ztyp(IS_STRING, member);
 
        if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
                handler->write(obj, value TSRMLS_CC);
@@ -1278,24 +1359,36 @@ static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC)
 
 PHP_METHOD(HttpMessage, __construct)
 {
-       int length = 0;
-       char *message = NULL;
+       zval *zmessage = NULL;
+       php_http_message_t *msg = NULL;
        php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
-       with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) {
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &message, &length) && message && length) {
-                       php_http_message_t *msg = obj->message;
+       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!", &zmessage) && zmessage) {
+                       if (Z_TYPE_P(zmessage) == IS_RESOURCE) {
+                               php_stream *s;
+                               php_http_message_parser_t p;
 
-                       php_http_message_dtor(msg);
-                       with_error_handling(EH_THROW, PHP_HTTP_EX_CE(message)) {
-                               if ((obj->message = php_http_message_parse(msg, message, length))) {
-                                       if (obj->message->parent) {
-                                               obj->parent = php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL TSRMLS_CC);
-                                       }
-                               } else {
-                                       obj->message = php_http_message_init(msg, 0 TSRMLS_CC);
+                               php_stream_from_zval(s, &zmessage);
+                               if (s && php_http_message_parser_init(&p TSRMLS_CC)) {
+                                       php_http_message_parser_parse_stream(&p, s, &msg);
+                                       php_http_message_parser_dtor(&p);
                                }
-                       } end_error_handling();
+                       } else {
+                               zmessage = php_http_ztyp(IS_STRING, zmessage);
+                               msg = php_http_message_parse(NULL, Z_STRVAL_P(zmessage), Z_STRLEN_P(zmessage) TSRMLS_CC);
+                               zval_ptr_dtor(&zmessage);
+                       }
+
+                       if (msg) {
+                               php_http_message_dtor(obj->message);
+                               obj->message = msg;
+                               if (obj->message->parent) {
+                                       obj->parent = php_http_message_object_new_ex(Z_OBJCE_P(getThis()), obj->message->parent, NULL TSRMLS_CC);
+                               }
+                       } else {
+                               php_http_error(HE_THROW, PHP_HTTP_E_MESSAGE, "could not parse message: %.*s", 25, Z_STRVAL_P(zmessage));
+                       }
                }
                if (!obj->message) {
                        obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);
@@ -1306,7 +1399,7 @@ PHP_METHOD(HttpMessage, __construct)
 
 PHP_METHOD(HttpMessage, getBody)
 {
-       with_error_handling(EH_THROW, PHP_HTTP_EX_CE(message)) {
+       with_error_handling(EH_THROW, php_http_exception_class_entry) {
                if (SUCCESS == zend_parse_parameters_none()) {
                        php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
@@ -1390,10 +1483,10 @@ PHP_METHOD(HttpMessage, setHeader)
                }
 
                if (!zvalue) {
-                       zend_hash_del(&obj->message->hdrs, name, name_len + 1);
+                       zend_symtable_del(&obj->message->hdrs, name, name_len + 1);
                } else {
                        Z_ADDREF_P(zvalue);
-                       zend_hash_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
+                       zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
                }
                efree(name);
        }
@@ -1439,7 +1532,7 @@ PHP_METHOD(HttpMessage, addHeader)
                        convert_to_array(header);
                        zend_hash_next_index_insert(Z_ARRVAL_P(header), &zvalue, sizeof(void *), NULL);
                } else {
-                       zend_hash_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
+                       zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
                }
                efree(name);
        }
@@ -1740,7 +1833,7 @@ PHP_METHOD(HttpMessage, setRequestUrl)
 
 PHP_METHOD(HttpMessage, getParentMessage)
 {
-       with_error_handling(EH_THROW, PHP_HTTP_EX_CE(message)) {
+       with_error_handling(EH_THROW, php_http_exception_class_entry) {
                if (SUCCESS == zend_parse_parameters_none()) {
                        php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
@@ -1823,7 +1916,7 @@ PHP_METHOD(HttpMessage, unserialize)
 
 PHP_METHOD(HttpMessage, detach)
 {
-       with_error_handling(EH_THROW, PHP_HTTP_EX_CE(message)) {
+       with_error_handling(EH_THROW, php_http_exception_class_entry) {
                if (SUCCESS == zend_parse_parameters_none()) {
                        php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
@@ -1874,6 +1967,40 @@ PHP_METHOD(HttpMessage, reverse)
        }
 }
 
+PHP_METHOD(HttpMessage, isMultipart)
+{
+       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);
+               char *boundary = NULL;
+
+               RETVAL_BOOL(php_http_message_is_multipart(obj->message, zboundary ? &boundary : NULL));
+
+               if (zboundary && boundary) {
+                       zval_dtor(zboundary);
+                       ZVAL_STRING(zboundary, boundary, 0);
+               }
+       }
+}
+
+PHP_METHOD(HttpMessage, splitMultipartBody)
+{
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               char *boundary = NULL;
+
+               if (php_http_message_is_multipart(obj->message, &boundary)) {
+                       php_http_message_t *msg;
+
+                       if ((msg = php_http_message_body_split(&obj->message->body, boundary))) {
+                               RETVAL_OBJVAL(php_http_message_object_new_ex(php_http_message_class_entry, msg, NULL TSRMLS_CC), 0);
+                       }
+               }
+               STR_FREE(boundary);
+       }
+}
+
 PHP_METHOD(HttpMessage, count)
 {
        if (SUCCESS == zend_parse_parameters_none()) {