remove superfluous buffer macros
[m6w6/ext-http] / php_http_message.c
index b25f69741126b67d85ab6f167f63558432b8ba64..39f857d5d7c280d30f140311d94f965190384f6a 100644 (file)
 
 #include "php_http_api.h"
 
+static zval *message_header_strval(zval **header TSRMLS_DC);
+static void message_headers(php_http_message_t *msg, php_http_buffer_t *str);
+
 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 || old->type || zend_hash_num_elements(&old->hdrs) || PHP_HTTP_BUFFER_LEN(old)) {
+       if (!old || old->type || zend_hash_num_elements(&old->hdrs)) {
                (*message) = php_http_message_init(NULL, 0 TSRMLS_CC);
                (*message)->parent = old;
                if (headers) {
@@ -41,6 +44,8 @@ PHP_HTTP_API php_http_message_t *php_http_message_init(php_http_message_t *messa
        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);
        php_http_message_body_init(&message->body, NULL TSRMLS_CC);
 
@@ -59,9 +64,6 @@ PHP_HTTP_API php_http_message_t *php_http_message_init_env(php_http_message_t *m
                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) TSRMLS_CC);
-                       } else {
-                               message->http.version.major = 1;
-                               message->http.version.minor = 1;
                        }
                        if ((sval = php_http_env_get_server_var(ZEND_STRL("REQUEST_METHOD"), 1 TSRMLS_CC))) {
                                message->http.info.request.method = estrdup(Z_STRVAL_P(sval));
@@ -80,8 +82,6 @@ PHP_HTTP_API php_http_message_t *php_http_message_init_env(php_http_message_t *m
                        
                case PHP_HTTP_RESPONSE:
                        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->http.version.major = 1;
-                               message->http.version.minor = 1;
                                if (!(message->http.info.response.code = SG(sapi_headers).http_response_code)) {
                                        message->http.info.response.code = 200;
                                }
@@ -89,7 +89,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_VERSION_ID >= 50400
                        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));
@@ -102,6 +102,7 @@ PHP_HTTP_API php_http_message_t *php_http_message_init_env(php_http_message_t *m
                                        zval_dtor(&tval);
                                }
                        }
+#endif
                        break;
                        
                default:
@@ -117,10 +118,11 @@ PHP_HTTP_API php_http_message_t *php_http_message_init_env(php_http_message_t *m
        return message;
 }
 
-PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *msg, const char *str, size_t len TSRMLS_DC)
+PHP_HTTP_API 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_parser_t p;
        php_http_buffer_t buf;
+       unsigned flags = PHP_HTTP_MESSAGE_PARSER_CLEANUP;
        int free_msg;
 
        php_http_buffer_from_string_ex(&buf, str, len);
@@ -130,7 +132,10 @@ PHP_HTTP_API php_http_message_t *php_http_message_parse(php_http_message_t *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 (greedy) {
+               flags |= PHP_HTTP_MESSAGE_PARSER_GREEDY;
+       }
+       if (FAILURE == php_http_message_parser_parse(&p, &buf, flags, &msg)) {
                if (free_msg) {
                        php_http_message_free(&msg);
                }
@@ -150,21 +155,9 @@ PHP_HTTP_API zval *php_http_message_header(php_http_message_t *msg, char *key_st
 
        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_ztyp(IS_STRING, *val);
-                               php_http_buffer_appendf(&str, PHP_HTTP_BUFFER_LEN(&str) ? ", %s":"%s", Z_STRVAL_P(strval));
-                               zval_ptr_dtor(&strval);
-                       }
-                       php_http_buffer_fix(&str);
-                       ZVAL_STRINGL(header_str, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str), 0);
-                       ret = header_str;
+                       ret = message_header_strval(header TSRMLS_CC);
                } else {
                        Z_ADDREF_PP(header);
                        ret = *header;
@@ -280,28 +273,14 @@ PHP_HTTP_API void php_http_message_set_info(php_http_message_t *message, php_htt
        }
 }
 
-static inline void message_headers(php_http_message_t *msg, php_http_buffer_t *str)
+PHP_HTTP_API void php_http_message_update_headers(php_http_message_t *msg)
 {
-       php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
-       HashPosition pos1;
-       zval **header, *h;
+       zval *h;
        size_t size;
-       TSRMLS_FETCH_FROM_CTX(msg->ts);
-
-       switch (msg->type) {
-               case PHP_HTTP_REQUEST:
-                       php_http_buffer_appendf(str, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg->http, PHP_HTTP_CRLF));
-                       break;
-
-               case PHP_HTTP_RESPONSE:
-                       php_http_buffer_appendf(str, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg->http, PHP_HTTP_CRLF));
-                       break;
-
-               default:
-                       break;
-       }
 
-       if ((size = php_http_message_body_size(&msg->body))) {
+       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 ((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);
@@ -324,51 +303,90 @@ static inline void message_headers(php_http_message_t *msg, php_http_buffer_t *s
                        }
                }
        }
+}
+
+static zval *message_header_strval(zval **header TSRMLS_DC)
+{
+       zval *ret;
+
+       if (Z_TYPE_PP(header) == IS_BOOL) {
+               MAKE_STD_ZVAL(ret);
+               ZVAL_STRING(ret, Z_BVAL_PP(header) ? "true" : "false", 1);
+       } else if (Z_TYPE_PP(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 = message_header_strval(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);
+       }
+
+       return ret;
+}
+
+static void message_headers(php_http_message_t *msg, php_http_buffer_t *str)
+{
+       php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
+       HashPosition pos1;
+       zval **header;
+       TSRMLS_FETCH_FROM_CTX(msg->ts);
+
+       switch (msg->type) {
+               case PHP_HTTP_REQUEST:
+                       php_http_buffer_appendf(str, PHP_HTTP_INFO_REQUEST_FMT_ARGS(&msg->http, PHP_HTTP_CRLF));
+                       break;
+
+               case PHP_HTTP_RESPONSE:
+                       php_http_buffer_appendf(str, PHP_HTTP_INFO_RESPONSE_FMT_ARGS(&msg->http, PHP_HTTP_CRLF));
+                       break;
+
+               default:
+                       break;
+       }
+
+       php_http_message_update_headers(msg);
 
        FOREACH_HASH_KEYVAL(pos1, &msg->hdrs, key, header) {
                if (key.type == HASH_KEY_IS_STRING) {
-                       HashPosition pos2;
-                       zval **single_header;
-
-                       switch (Z_TYPE_PP(header)) {
-                               case IS_BOOL:
-                                       php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_BVAL_PP(header)?"true":"false");
-                                       break;
-                                       
-                               case IS_LONG:
-                                       php_http_buffer_appendf(str, "%s: %ld" PHP_HTTP_CRLF, key.str, Z_LVAL_PP(header));
-                                       break;
-                                       
-                               case IS_DOUBLE:
-                                       php_http_buffer_appendf(str, "%s: %F" PHP_HTTP_CRLF, key.str, Z_DVAL_PP(header));
-                                       break;
-                                       
-                               case IS_STRING:
-                                       if (Z_STRVAL_PP(header)[Z_STRLEN_PP(header)-1] == '\r') fprintf(stderr, "DOH!\n");
-                                       php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_STRVAL_PP(header));
-                                       break;
-
-                               case IS_ARRAY:
-                                       FOREACH_VAL(pos2, *header, single_header) {
-                                               switch (Z_TYPE_PP(single_header)) {
-                                                       case IS_BOOL:
-                                                               php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_BVAL_PP(single_header)?"true":"false");
-                                                               break;
-                                                               
-                                                       case IS_LONG:
-                                                               php_http_buffer_appendf(str, "%s: %ld" PHP_HTTP_CRLF, key.str, Z_LVAL_PP(single_header));
-                                                               break;
-                                                               
-                                                       case IS_DOUBLE:
-                                                               php_http_buffer_appendf(str, "%s: %F" PHP_HTTP_CRLF, key.str, Z_DVAL_PP(single_header));
-                                                               break;
-                                                               
-                                                       case IS_STRING:
-                                                               php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_STRVAL_PP(single_header));
-                                                               break;
+                       if (key.len == sizeof("Set-Cookie") && !strcasecmp(key.str, "Set-Cookie") && Z_TYPE_PP(header) == IS_ARRAY) {
+                               HashPosition pos2;
+                               zval **single_header;
+
+                               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);
+
+                                               if (cookie) {
+                                                       char *buf;
+                                                       size_t len;
+
+                                                       php_http_cookie_list_to_string(cookie, &buf, &len);
+                                                       php_http_buffer_appendf(str, "Set-Cookie: %s" PHP_HTTP_CRLF, buf);
+                                                       php_http_cookie_list_free(&cookie);
+                                                       efree(buf);
                                                }
+                                       } else {
+                                               zval *strval = message_header_strval(single_header TSRMLS_CC);
+
+                                               php_http_buffer_appendf(str, "Set-Cookie: %s" PHP_HTTP_CRLF, Z_STRVAL_P(strval));
+                                               zval_ptr_dtor(&strval);
                                        }
-                                       break;
+                               }
+                       } else {
+                               zval *strval = message_header_strval(header TSRMLS_CC);
+
+                               php_http_buffer_appendf(str, "%s: %s" PHP_HTTP_CRLF, key.str, Z_STRVAL_P(strval));
+                               zval_ptr_dtor(&strval);
                        }
                }
        }
@@ -380,7 +398,7 @@ PHP_HTTP_API void php_http_message_to_callback(php_http_message_t *msg, php_http
 
        php_http_buffer_init_ex(&str, 0x1000, 0);
        message_headers(msg, &str);
-       cb(cb_arg, PHP_HTTP_BUFFER_VAL(&str), PHP_HTTP_BUFFER_LEN(&str));
+       cb(cb_arg, str.data, str.used);
        php_http_buffer_dtor(&str);
 
        if (php_http_message_body_size(&msg->body)) {
@@ -458,6 +476,24 @@ PHP_HTTP_API php_http_message_t *php_http_message_reverse(php_http_message_t *ms
        return msg;
 }
 
+PHP_HTTP_API php_http_message_t *php_http_message_zip(php_http_message_t *one, php_http_message_t *two)
+{
+       php_http_message_t *dst = php_http_message_copy(one, NULL), *src = php_http_message_copy(two, NULL), *tmp_dst, *tmp_src, *ret = dst;
+
+       while(dst && src) {
+               tmp_dst = dst->parent;
+               tmp_src = src->parent;
+               dst->parent = src;
+               if (tmp_dst) {
+                       src->parent = tmp_dst;
+               }
+               src = tmp_src;
+               dst = tmp_dst;
+       }
+
+       return ret;
+}
+
 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;
@@ -535,6 +571,7 @@ PHP_HTTP_API void php_http_message_free(php_http_message_t **message)
 
 PHP_HTTP_BEGIN_ARGS(__construct, 0)
        PHP_HTTP_ARG_VAL(message, 0)
+       PHP_HTTP_ARG_VAL(greedy, 0)
 PHP_HTTP_END_ARGS;
 
 PHP_HTTP_EMPTY_ARGS(getBody);
@@ -542,6 +579,10 @@ PHP_HTTP_BEGIN_ARGS(setBody, 1)
        PHP_HTTP_ARG_VAL(body, 0)
 PHP_HTTP_END_ARGS;
 
+PHP_HTTP_BEGIN_ARGS(addBody, 1)
+       PHP_HTTP_ARG_VAL(body, 0)
+PHP_HTTP_END_ARGS;
+
 PHP_HTTP_BEGIN_ARGS(getHeader, 1)
        PHP_HTTP_ARG_VAL(header, 0)
 PHP_HTTP_END_ARGS;
@@ -610,7 +651,7 @@ PHP_HTTP_BEGIN_ARGS(toCallback, 1)
        PHP_HTTP_ARG_VAL(callback, 0)
 PHP_HTTP_END_ARGS;
 PHP_HTTP_BEGIN_ARGS(toStream, 1)
-       HTTP_ARG_VAL(stream, 0)
+       PHP_HTTP_ARG_VAL(stream, 0)
 PHP_HTTP_END_ARGS;
 
 PHP_HTTP_EMPTY_ARGS(count);
@@ -637,16 +678,23 @@ PHP_HTTP_BEGIN_ARGS(isMultipart, 0)
 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);
+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 zval **php_http_message_object_get_prop_ptr(zval *object, zval *member PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC);
 static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC);
 
-zend_class_entry *php_http_message_class_entry;
-zend_function_entry php_http_message_method_entry[] = {
+static zend_class_entry *php_http_message_class_entry;
+
+zend_class_entry *php_http_message_get_class_entry(void)
+{
+       return php_http_message_class_entry;
+}
+
+static zend_function_entry php_http_message_method_entry[] = {
        PHP_HTTP_MESSAGE_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
        PHP_HTTP_MESSAGE_ME(getBody, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_ME(setBody, ZEND_ACC_PUBLIC)
+       PHP_HTTP_MESSAGE_ME(addBody, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_ME(getHeader, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_ME(setHeader, ZEND_ACC_PUBLIC)
        PHP_HTTP_MESSAGE_ME(addHeader, ZEND_ACC_PUBLIC)
@@ -815,13 +863,7 @@ static void php_http_message_object_prophandler_get_body(php_http_message_object
        }
 }
 static void php_http_message_object_prophandler_set_body(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_body_class_entry TSRMLS_CC)) {
-               if (obj->body.handle) {
-                       zend_objects_store_del_ref_by_handle(obj->body.handle TSRMLS_CC);
-               }
-               Z_OBJ_ADDREF_P(value);
-               obj->body = Z_OBJVAL_P(value);
-       }
+       php_http_message_object_set_body(obj, value TSRMLS_CC);
 }
 static void php_http_message_object_prophandler_get_parent_message(php_http_message_object_t *obj, zval *return_value TSRMLS_DC) {
        if (obj->message->parent) {
@@ -832,17 +874,20 @@ static void php_http_message_object_prophandler_get_parent_message(php_http_mess
 }
 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);
+
                if (obj->message->parent) {
                        zend_objects_store_del_ref_by_handle(obj->parent.handle TSRMLS_CC);
                }
                Z_OBJ_ADDREF_P(value);
                obj->parent = Z_OBJVAL_P(value);
+               obj->message->parent = parent_obj->message;
        }
 }
 
 PHP_MINIT_FUNCTION(http_message)
 {
-       PHP_HTTP_REGISTER_CLASS(http, Message, http_message, php_http_object_class_entry, 0);
+       PHP_HTTP_REGISTER_CLASS(http, Message, http_message, php_http_object_get_class_entry(), 0);
        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.clone_obj = php_http_message_object_clone;
@@ -856,7 +901,7 @@ PHP_MINIT_FUNCTION(http_message)
        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);
        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_string(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 TSRMLS_CC);
        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);
        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);
@@ -898,7 +943,7 @@ void php_http_message_object_reverse(zval *this_ptr, zval *return_value TSRMLS_D
        if (i > 1) {
                zend_object_value *ovalues = NULL;
                php_http_message_object_t **objects = NULL;
-               int last = i - 1;
+               int last;
 
                objects = ecalloc(i, sizeof(**objects));
                ovalues = ecalloc(i, sizeof(*ovalues));
@@ -974,6 +1019,68 @@ void php_http_message_object_prepend(zval *this_ptr, zval *prepend, zend_bool to
        }
 }
 
+STATUS php_http_message_object_set_body(php_http_message_object_t *msg_obj, zval *zbody TSRMLS_DC)
+{
+       zval *tmp = NULL;
+       php_stream *s;
+       zend_object_value ov;
+       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);
+                       if (!s) {
+                               php_http_error(HE_THROW, PHP_HTTP_E_CLIENT, "not a valid stream resource");
+                               return FAILURE;
+                       }
+
+                       is_resource:
+
+                       body = php_http_message_body_init(NULL, s TSRMLS_CC);
+                       if (SUCCESS != php_http_new(&ov, php_http_message_body_get_class_entry(), (php_http_new_t) php_http_message_body_object_new_ex, NULL, body, NULL TSRMLS_CC)) {
+                               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_get_class_entry() TSRMLS_CC)) {
+                               Z_OBJ_ADDREF_P(zbody);
+                               break;
+                       }
+                       /* no break */
+
+               default:
+                       tmp = php_http_ztyp(IS_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;
+                       goto is_resource;
+
+       }
+
+       body_obj = zend_object_store_get_object(zbody TSRMLS_CC);
+
+       if (msg_obj->body.handle) {
+               zend_objects_store_del_ref_by_handle(msg_obj->body.handle TSRMLS_CC);
+               php_http_message_body_dtor(&msg_obj->message->body);
+       }
+
+       php_http_message_body_copy(body_obj->body, &msg_obj->message->body, 0);
+       msg_obj->body = Z_OBJVAL_P(zbody);
+
+       if (tmp) {
+               zval_ptr_dtor(&tmp);
+       }
+
+       return SUCCESS;
+}
+
 zend_object_value php_http_message_object_new(zend_class_entry *ce TSRMLS_DC)
 {
        return php_http_message_object_new_ex(ce, NULL, NULL TSRMLS_CC);
@@ -997,7 +1104,7 @@ zend_object_value php_http_message_object_new_ex(zend_class_entry *ce, php_http_
                if (msg->parent) {
                        o->parent = php_http_message_object_new_ex(ce, msg->parent, NULL TSRMLS_CC);
                }
-               o->body = php_http_message_body_object_new_ex(php_http_message_body_class_entry, php_http_message_body_copy(&msg->body, NULL, 0), NULL TSRMLS_CC);
+               o->body = php_http_message_body_object_new_ex(php_http_message_body_get_class_entry(), php_http_message_body_copy(&msg->body, NULL, 0), NULL TSRMLS_CC);
        }
 
        ov.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_object_free, NULL TSRMLS_CC);
@@ -1043,7 +1150,7 @@ 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)
+static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC)
 {
        php_http_message_object_prophandler_t *handler;
        zval *copy = php_http_ztyp(IS_STRING, member);
@@ -1054,10 +1161,10 @@ static zval **php_http_message_object_get_prop_ptr(zval *object, zval *member, c
        }
        zval_ptr_dtor(&copy);
 
-       return zend_get_std_object_handlers()->get_property_ptr_ptr(object, member, literal_key TSRMLS_CC);
+       return zend_get_std_object_handlers()->get_property_ptr_ptr(object, member PHP_HTTP_ZEND_LITERAL_CC TSRMLS_CC);
 }
 
-static zval *php_http_message_object_read_prop(zval *object, zval *member, int type, const zend_literal *literal_key TSRMLS_DC)
+static zval *php_http_message_object_read_prop(zval *object, zval *member, int type PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC)
 {
        php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
        php_http_message_object_prophandler_t *handler;
@@ -1075,7 +1182,7 @@ static zval *php_http_message_object_read_prop(zval *object, zval *member, int t
                        return_value = NULL;
                }
        } else {
-               return_value = zend_get_std_object_handlers()->read_property(object, member, type, literal_key TSRMLS_CC);
+               return_value = zend_get_std_object_handlers()->read_property(object, member, type PHP_HTTP_ZEND_LITERAL_CC TSRMLS_CC);
        }
 
        zval_ptr_dtor(&copy);
@@ -1083,7 +1190,7 @@ static zval *php_http_message_object_read_prop(zval *object, zval *member, int t
        return return_value;
 }
 
-static void php_http_message_object_write_prop(zval *object, zval *member, zval *value, const zend_literal *literal_key TSRMLS_DC)
+static void php_http_message_object_write_prop(zval *object, zval *member, zval *value PHP_HTTP_ZEND_LITERAL_DC TSRMLS_DC)
 {
        php_http_message_object_t *obj = zend_object_store_get_object(object TSRMLS_CC);
        php_http_message_object_prophandler_t *handler;
@@ -1092,7 +1199,7 @@ static void php_http_message_object_write_prop(zval *object, zval *member, zval
        if (SUCCESS == php_http_message_object_get_prophandler(Z_STRVAL_P(copy), Z_STRLEN_P(copy), &handler)) {
                handler->write(obj, value TSRMLS_CC);
        } else {
-               zend_get_std_object_handlers()->write_property(object, member, value, literal_key TSRMLS_CC);
+               zend_get_std_object_handlers()->write_property(object, member, value PHP_HTTP_ZEND_LITERAL_CC TSRMLS_CC);
        }
 
        zval_ptr_dtor(&copy);
@@ -1163,7 +1270,7 @@ static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC)
 
        MAKE_STD_ZVAL(body);
        if (!obj->body.handle) {
-               php_http_new(&obj->body, php_http_message_body_class_entry, (php_http_new_t) php_http_message_body_object_new_ex, NULL, (void *) php_http_message_body_copy(&obj->message->body, NULL, 0), NULL TSRMLS_CC);
+               php_http_new(&obj->body, php_http_message_body_get_class_entry(), (php_http_new_t) php_http_message_body_object_new_ex, NULL, (void *) php_http_message_body_copy(&obj->message->body, NULL, 0), NULL TSRMLS_CC);
        }
        ZVAL_OBJVAL(body, obj->body, 1);
        ASSOC_PROP(array, zval, "body", body);
@@ -1183,24 +1290,33 @@ static HashTable *php_http_message_object_get_props(zval *object TSRMLS_DC)
 
 PHP_METHOD(HttpMessage, __construct)
 {
+       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);
 
-       with_error_handling(EH_THROW, php_http_exception_class_entry) {
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!", &zmessage) && zmessage) {
+       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
+               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!b", &zmessage, &greedy) && zmessage) {
                        if (Z_TYPE_P(zmessage) == IS_RESOURCE) {
                                php_stream *s;
                                php_http_message_parser_t p;
 
                                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);
+                                       unsigned flags = (greedy ? PHP_HTTP_MESSAGE_PARSER_GREEDY : 0);
+                                       
+                                       php_http_message_parser_parse_stream(&p, s, flags, &msg);
                                        php_http_message_parser_dtor(&p);
                                }
+                               
+                               if (!msg) {
+                                       php_http_error(HE_THROW, PHP_HTTP_E_MESSAGE, "could not parse message from stream");
+                               }
                        } else {
                                zmessage = php_http_ztyp(IS_STRING, zmessage);
-                               msg = php_http_message_parse(NULL, Z_STRVAL_P(zmessage), Z_STRLEN_P(zmessage) TSRMLS_CC);
+                               if (!(msg = php_http_message_parse(NULL, Z_STRVAL_P(zmessage), Z_STRLEN_P(zmessage), greedy TSRMLS_CC))) {
+                                       php_http_error(HE_THROW, PHP_HTTP_E_MESSAGE, "could not parse message: %.*s", MIN(25, Z_STRLEN_P(zmessage)), Z_STRVAL_P(zmessage));
+                               }
                                zval_ptr_dtor(&zmessage);
                        }
 
@@ -1210,8 +1326,6 @@ PHP_METHOD(HttpMessage, __construct)
                                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) {
@@ -1223,7 +1337,7 @@ PHP_METHOD(HttpMessage, __construct)
 
 PHP_METHOD(HttpMessage, getBody)
 {
-       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
                if (SUCCESS == zend_parse_parameters_none()) {
                        php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
@@ -1231,7 +1345,7 @@ PHP_METHOD(HttpMessage, getBody)
                                obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);
                        }
 
-                       if (obj->body.handle || SUCCESS == php_http_new(&obj->body, php_http_message_body_class_entry, (php_http_new_t) php_http_message_body_object_new_ex, NULL, (void *) php_http_message_body_copy(&obj->message->body, NULL, 0), NULL TSRMLS_CC)) {
+                       if (obj->body.handle || SUCCESS == php_http_new(&obj->body, php_http_message_body_get_class_entry(), (php_http_new_t) php_http_message_body_object_new_ex, NULL, (void *) php_http_message_body_copy(&obj->message->body, NULL, 0), NULL TSRMLS_CC)) {
                                RETVAL_OBJVAL(obj->body, 1);
                        }
                }
@@ -1242,22 +1356,31 @@ PHP_METHOD(HttpMessage, setBody)
 {
        zval *zbody;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zbody, php_http_message_body_class_entry)) {
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &zbody, php_http_message_body_get_class_entry())) {
                php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
-               php_http_message_body_object_t *body_obj = zend_object_store_get_object(zbody TSRMLS_CC);
 
                if (!obj->message) {
                        obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);
                }
+               php_http_message_object_prophandler_set_body(obj, zbody TSRMLS_CC);
+       }
+       RETVAL_ZVAL(getThis(), 1, 0);
+}
+
+PHP_METHOD(HttpMessage, addBody)
+{
+       zval *new_body;
 
-               php_http_message_body_dtor(&obj->message->body);
-               php_http_message_body_copy(body_obj->body, &obj->message->body, 0);
-               Z_OBJ_ADDREF_P(zbody);
-               obj->body = Z_OBJVAL_P(zbody);
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "O", &new_body, php_http_message_body_get_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);
+
+               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);
 }
 
+
 PHP_METHOD(HttpMessage, getHeader)
 {
        char *header_str;
@@ -1355,6 +1478,7 @@ PHP_METHOD(HttpMessage, addHeader)
                if ((header = php_http_message_header(obj->message, name, name_len, 0))) {
                        convert_to_array(header);
                        zend_hash_next_index_insert(Z_ARRVAL_P(header), &zvalue, sizeof(void *), NULL);
+                       zval_ptr_dtor(&header);
                } else {
                        zend_symtable_update(&obj->message->hdrs, name, name_len + 1, &zvalue, sizeof(void *), NULL);
                }
@@ -1658,7 +1782,7 @@ PHP_METHOD(HttpMessage, setRequestUrl)
 
 PHP_METHOD(HttpMessage, getParentMessage)
 {
-       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
                if (SUCCESS == zend_parse_parameters_none()) {
                        php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
@@ -1720,9 +1844,7 @@ PHP_METHOD(HttpMessage, toStream)
 
 PHP_METHOD(HttpMessage, toCallback)
 {
-       zend_bool include_parent = 0;
        php_http_pass_fcall_arg_t fcd;
-       long offset = 0, forlen = 0;
 
        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);
@@ -1770,7 +1892,7 @@ PHP_METHOD(HttpMessage, unserialize)
                        php_http_message_dtor(obj->message);
                        efree(obj->message);
                }
-               if ((msg = php_http_message_parse(NULL, serialized, (size_t) length TSRMLS_CC))) {
+               if ((msg = php_http_message_parse(NULL, serialized, (size_t) length, 1 TSRMLS_CC))) {
                        obj->message = msg;
                } else {
                        obj->message = php_http_message_init(NULL, 0 TSRMLS_CC);
@@ -1781,7 +1903,7 @@ PHP_METHOD(HttpMessage, unserialize)
 
 PHP_METHOD(HttpMessage, detach)
 {
-       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+       with_error_handling(EH_THROW, php_http_exception_get_class_entry()) {
                if (SUCCESS == zend_parse_parameters_none()) {
                        php_http_message_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);