fix tests
[m6w6/ext-http] / php_http_message_body.c
index 969f860af9b1a934fc752c3a2b67098900937da8..22179e413a52555f11c04a3af1286a2c739cfdd3 100644 (file)
     | 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-2014, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
-/* $Id: http_message_body.c 292841 2009-12-31 08:48:57Z mike $ */
+#include "php_http_api.h"
 
-#include "php_http.h"
+#include <ext/standard/php_lcg.h>
 
-typedef struct curl_httppost *post_data[2];
+#define BOUNDARY_OPEN(body) \
+       do {\
+               size_t size = php_http_message_body_size(body); \
+               if (size) { \
+                       php_stream_truncate_set_size(php_http_message_body_stream(body), size - lenof("--" PHP_HTTP_CRLF)); \
+                       php_http_message_body_append(body, ZEND_STRL(PHP_HTTP_CRLF)); \
+               } else { \
+                       php_http_message_body_appendf(body, "--%s" PHP_HTTP_CRLF, php_http_message_body_boundary(body)); \
+               } \
+       } while(0)
 
-static inline STATUS add_field(php_http_message_body_t *body, const char *name, const char *value_str, size_t value_len);
-static inline STATUS add_file(php_http_message_body_t *body, const char *name, const char *path, const char *ctype);
-static STATUS recursive_fields(post_data http_post_data, HashTable *fields, const char *prefix TSRMLS_DC);
-static STATUS recursive_files(post_data http_post_data, HashTable *files, const char *prefix TSRMLS_DC);
+#define BOUNDARY_CLOSE(body) \
+               php_http_message_body_appendf(body, PHP_HTTP_CRLF "--%s--" PHP_HTTP_CRLF, php_http_message_body_boundary(body))
 
-PHP_HTTP_API php_http_message_body_t *php_http_message_body_init(php_http_message_body_t *body, php_stream *stream TSRMLS_DC)
+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)
 {
-       if (!body) {
-               body = emalloc(sizeof(php_http_message_body_t));
+       php_http_message_body_t *body;
+
+       if (body_ptr && *body_ptr) {
+               body = *body_ptr;
+               ++body->refcount;
+               return body;
        }
        
+       body = ecalloc(1, sizeof(php_http_message_body_t));
+       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_new();
-               php_stream_auto_cleanup(stream);
-               body->stream_id = php_stream_get_resource_id(stream);
+               stream = php_stream_temp_create(TEMP_STREAM_DEFAULT, 0xffff);
+               body->res = stream->res;
+       }
+       php_stream_auto_cleanup(stream);
+
+       if (body_ptr) {
+               *body_ptr = body;
        }
-       TSRMLS_SET_CTX(body->ts);
 
        return body;
 }
 
-PHP_HTTP_API php_http_message_body_t *php_http_message_body_copy(php_http_message_body_t *from, php_http_message_body_t *to, zend_bool dup_internal_stream_and_contents)
+unsigned php_http_message_body_addref(php_http_message_body_t *body)
 {
-       if (!from) {
-               return NULL;
-       } else {
-               TSRMLS_FETCH_FROM_CTX(from->ts);
-               
-               if (dup_internal_stream_and_contents) {
-                       to = php_http_message_body_init(to, NULL TSRMLS_CC);
-                       php_http_message_body_to_stream(from, php_http_message_body_stream(to), 0, 0);
+       return ++body->refcount;
+}
+
+php_http_message_body_t *php_http_message_body_copy(php_http_message_body_t *from, php_http_message_body_t *to)
+{
+       if (from) {
+               if (to) {
+                       php_stream_truncate_set_size(php_http_message_body_stream(to), 0);
                } else {
-                       to = php_http_message_body_init(to, php_http_message_body_stream(from) 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);
 
-               return to;
+               if (to->boundary) {
+                       efree(to->boundary);
+               }
+               if (from->boundary) {
+                       to->boundary = estrdup(from->boundary);
+               }
+       } else {
+               to = NULL;
        }
+       return to;
 }
 
-PHP_HTTP_API void php_http_message_body_dtor(php_http_message_body_t *body)
+void php_http_message_body_free(php_http_message_body_t **body_ptr)
 {
-       if (body) {
-               /* NO FIXME: shows leakinfo in DEBUG mode */
-               zend_list_delete(body->stream_id);
-       }
-}
+       if (*body_ptr) {
+               php_http_message_body_t *body = *body_ptr;
 
-PHP_HTTP_API void php_http_message_body_free(php_http_message_body_t **body)
-{
-       if (*body) {
-               php_http_message_body_dtor(*body);
-               efree(*body);
-               *body = NULL;
+               if (!--body->refcount) {
+                       //zend_list_close(body->res);
+                       PTR_FREE(body->boundary);
+                       efree(body);
+               }
+               *body_ptr = NULL;
        }
 }
 
-PHP_HTTP_API php_stream_statbuf *php_http_message_body_stat(php_http_message_body_t *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;
 }
 
-PHP_HTTP_API char *php_http_message_body_etag(php_http_message_body_t *body)
+const char *php_http_message_body_boundary(php_http_message_body_t *body)
+{
+       if (!body->boundary) {
+               union { double dbl; int num[2]; } data;
+
+               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)
 {
-       TSRMLS_FETCH_FROM_CTX(body->ts);
-       php_stream_statbuf *ssb = php_http_message_body_stat(body);
+       php_http_etag_t *etag;
+       php_stream *s = php_http_message_body_stream(body);
 
        /* real file or temp buffer ? */
-       if (body->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 {
-               void *ctx = php_http_etag_init(TSRMLS_C);
+               if (body->ssb.sb.st_mtime) {
+                       char *etag;
 
-               php_http_message_body_to_callback(body, php_http_etag_update, ctx, 0, 0);
-               return php_http_etag_finish(ctx TSRMLS_CC);
+                       spprintf(&etag, 0, "%lx-%lx-%lx", body->ssb.sb.st_ino, body->ssb.sb.st_mtime, body->ssb.sb.st_size);
+                       return etag;
+               }
        }
+
+       /* 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;
 }
 
-PHP_HTTP_API 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)
 {
-       TSRMLS_FETCH_FROM_CTX(body->ts);
        php_stream *s = php_http_message_body_stream(body);
 
        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);
 }
 
-PHP_HTTP_API void 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)
 {
-       TSRMLS_FETCH_FROM_CTX(body->ts);
        php_stream *s = php_http_message_body_stream(body);
 
        php_stream_seek(s, offset, SEEK_SET);
+
        if (!forlen) {
                forlen = -1;
        }
-       php_stream_copy_to_stream_ex(s, dst, forlen, NULL);
+       return php_stream_copy_to_stream_ex(s, dst, forlen, NULL);
 }
 
-PHP_HTTP_API void 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)
 {
-       TSRMLS_FETCH_FROM_CTX(body->ts);
        php_stream *s = php_http_message_body_stream(body);
+       char *buf = emalloc(0x1000);
 
        php_stream_seek(s, offset, SEEK_SET);
 
@@ -137,11 +178,12 @@ PHP_HTTP_API void php_http_message_body_to_callback(php_http_message_body_t *bod
                forlen = -1;
        }
        while (!php_stream_eof(s)) {
-               char buf[0x1000];
-               size_t read = php_stream_read(s, buf, MIN(forlen, sizeof(buf)));
+               size_t read = php_stream_read(s, buf, MIN(forlen, 0x1000));
 
                if (read) {
-                       cb(cb_arg, buf, read);
+                       if (-1 == cb(cb_arg, buf, read)) {
+                               return FAILURE;
+                       }
                }
 
                if (read < MIN(forlen, sizeof(buf))) {
@@ -152,531 +194,704 @@ PHP_HTTP_API void php_http_message_body_to_callback(php_http_message_body_t *bod
                        break;
                }
        }
+       efree(buf);
+
+       return SUCCESS;
 }
 
-PHP_HTTP_API STATUS php_http_message_body_to_callback_in_chunks(php_http_message_body_t *body, php_http_pass_callback_t cb, void *cb_arg, HashTable *chunks)
+size_t php_http_message_body_append(php_http_message_body_t *body, const char *buf, size_t len)
 {
-       TSRMLS_FETCH_FROM_CTX(body->ts);
-       php_stream *s = php_http_message_body_stream(body);
-       HashPosition pos;
-       zval **chunk;
-
-       FOREACH_HASH_VAL(pos, chunks, chunk) {
-               zval **begin, **end;
-
-               if (IS_ARRAY == Z_TYPE_PP(chunk)
-               &&      SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(chunk), 0, (void *) &begin)
-               &&      IS_LONG == Z_TYPE_PP(begin)
-               &&      SUCCESS == zend_hash_index_find(Z_ARRVAL_PP(chunk), 1, (void *) &end)
-               &&      IS_LONG == Z_TYPE_PP(end)
-               ) {
-                       if (SUCCESS != php_stream_seek(s, Z_LVAL_PP(begin), SEEK_SET)) {
-                               return FAILURE;
-                       } else {
-                               long length = Z_LVAL_PP(end) - Z_LVAL_PP(begin) + 1;
+       php_stream *s;
+       size_t written;
 
-                               while (length > 0 && !php_stream_eof(s)) {
-                                       char buf[0x1000];
-                                       size_t read = php_stream_read(s, buf, MIN(length, sizeof(buf)));
+       if (!(s = php_http_message_body_stream(body))) {
+               return -1;
+       }
 
-                                       if (read) {
-                                               cb(cb_arg, buf, read);
-                                       }
+       if (s->ops->seek) {
+               php_stream_seek(s, 0, SEEK_END);
+       }
 
-                                       if (read < MIN(length, sizeof(buf))) {
-                                               break;
-                                       }
+       written = php_stream_write(s, buf, len);
 
-                                       length -= read;
-                               }
-                       }
-               }
+       if (written != len) {
+               php_error_docref(NULL, E_WARNING, "Failed to append %zu bytes to body; wrote %zu", len, written);
        }
 
-       return SUCCESS;
+       return len;
 }
 
-PHP_HTTP_API size_t php_http_message_body_append(php_http_message_body_t *body, const char *buf, size_t len)
+size_t php_http_message_body_appendf(php_http_message_body_t *body, const char *fmt, ...)
 {
-       TSRMLS_FETCH_FROM_CTX(body->ts);
-       php_stream *s = php_http_message_body_stream(body);
+       va_list argv;
+       char *print_str;
+       size_t print_len;
+
+       va_start(argv, fmt);
+       print_len = vspprintf(&print_str, 0, fmt, argv);
+       va_end(argv);
+
+       print_len = php_http_message_body_append(body, print_str, print_len);
+       efree(print_str);
 
-       php_stream_seek(s, 0, SEEK_END);
-       return php_stream_write(s, buf, len);
+       return print_len;
 }
 
-PHP_HTTP_API STATUS php_http_message_body_add(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)
 {
-       post_data http_post_data = {NULL, NULL};
-       TSRMLS_FETCH_FROM_CTX(body->ts);
-
-       if (fields && SUCCESS != recursive_fields(http_post_data, fields, NULL TSRMLS_CC)) {
-               return FAILURE;
-       }
-       if (files && (zend_hash_num_elements(files) > 0) && (SUCCESS != recursive_files(http_post_data, files, NULL TSRMLS_CC))) {
-               return FAILURE;
+       if (fields) {
+               if (SUCCESS != add_recursive_fields(body, NULL, fields)) {
+                       return FAILURE;
+               }
        }
-       if (CURLE_OK != curl_formget(http_post_data[0], body, (curl_formget_callback) php_http_message_body_append)) {
-               return FAILURE;
+       if (files) {
+               if (SUCCESS != add_recursive_files(body, NULL, files)) {
+                       return FAILURE;
+               }
        }
-       return SUCCESS;
-}
 
-PHP_HTTP_API STATUS php_http_message_body_add_field(php_http_message_body_t *body, const char *name, const char *value_str, size_t value_len)
-{
-       return add_field(body, name, value_str, value_len);
+       return SUCCESS;
 }
 
-PHP_HTTP_API STATUS php_http_message_body_add_file(php_http_message_body_t *body, const char *name, const char *path, const char *ctype)
+void php_http_message_body_add_part(php_http_message_body_t *body, php_http_message_t *part)
 {
-       return add_file(body, name, path, ctype);
+       BOUNDARY_OPEN(body);
+       php_http_message_to_callback(part, (php_http_pass_callback_t) php_http_message_body_append, body);
+       BOUNDARY_CLOSE(body);
 }
 
-static inline char *format_key(uint type, char *str, ulong num, const char *prefix, int numeric_key_for_empty_prefix) {
-       char *new_key = NULL;
-       
-       if (prefix && *prefix) {
-               if (type == HASH_KEY_IS_STRING) {
-                       spprintf(&new_key, 0, "%s[%s]", prefix, str);
-               } else {
-                       spprintf(&new_key, 0, "%s[%lu]", prefix, num);
-               }
-       } else if (type == HASH_KEY_IS_STRING) {
-               new_key = estrdup(str);
-       } else if (numeric_key_for_empty_prefix) {
-               spprintf(&new_key, 0, "%lu", num);
-       }
-       
-       return new_key;
-}
 
-static inline STATUS add_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)
 {
-       post_data http_post_data = {NULL, NULL};
-       CURLcode err;
+       zend_string *safe_name = zend_string_init(name, strlen(name), 0);
 
-       err = curl_formadd(&http_post_data[0], &http_post_data[1],
-               CURLFORM_COPYNAME, name,
-               CURLFORM_COPYCONTENTS, value_str,
-               CURLFORM_CONTENTSLENGTH, (long) value_len,
-               CURLFORM_END
-       );
+       safe_name = php_addslashes(safe_name, 1);
 
-       if (CURLE_OK != err) {
-               return FAILURE;
-       }
-
-       err = curl_formget(http_post_data[0], body, (curl_formget_callback) php_http_message_body_append);
-
-       if (CURLE_OK != err) {
-               curl_formfree(http_post_data[0]);
-               return FAILURE;
-       }
+       BOUNDARY_OPEN(body);
+       php_http_message_body_appendf(
+               body,
+               "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
+               "" PHP_HTTP_CRLF,
+               safe_name->val
+       );
+       php_http_message_body_append(body, value_str, value_len);
+       BOUNDARY_CLOSE(body);
 
-       curl_formfree(http_post_data[0]);
+       zend_string_release(safe_name);
        return SUCCESS;
 }
 
-static inline STATUS add_file(php_http_message_body_t *body, const char *name, const char *path, const char *ctype)
+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)
 {
-       post_data http_post_data = {NULL, NULL};
-       CURLcode err;
-
-       err = curl_formadd(&http_post_data[0], &http_post_data[1],
-               CURLFORM_COPYNAME, name,
-               CURLFORM_FILE, path,
-               CURLFORM_CONTENTTYPE, ctype ? ctype : "application/octet-stream",
-               CURLFORM_END
+       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(safe_name, 1);
+       base_name = php_basename(path_dup, path_len, NULL, 0);
+
+       BOUNDARY_OPEN(body);
+       php_http_message_body_appendf(
+               body,
+               "Content-Disposition: form-data; name=\"%s\"; filename=\"%s\"" PHP_HTTP_CRLF
+               "Content-Transfer-Encoding: binary" PHP_HTTP_CRLF
+               "Content-Type: %s" PHP_HTTP_CRLF
+               PHP_HTTP_CRLF,
+               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);
 
-       if (CURLE_OK != err) {
-               return FAILURE;
-       }
+       zend_string_release(safe_name);
+       zend_string_release(base_name);
+       efree(path_dup);
 
-       err = curl_formget(http_post_data[0], body, (curl_formget_callback) php_http_message_body_append);
+       return SUCCESS;
+}
+
+static inline char *format_key(php_http_arrkey_t *key, const char *prefix) {
+       char *new_key = NULL;
 
-       if (CURLE_OK != err) {
-               curl_formfree(http_post_data[0]);
-               return FAILURE;
+       if (prefix && *prefix) {
+               if (key->key) {
+                       spprintf(&new_key, 0, "%s[%s]", prefix, key->key->val);
+               } else {
+                       spprintf(&new_key, 0, "%s[%lu]", prefix, key->h);
+               }
+       } else if (key->key) {
+               new_key = estrdup(key->key->val);
+       } else {
+               new_key = estrdup("");
        }
 
-       curl_formfree(http_post_data[0]);
-       return SUCCESS;
+       return new_key;
 }
 
-static STATUS recursive_fields(post_data http_post_data, HashTable *fields, const char *prefix TSRMLS_DC) {
-       php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
-       zval **data_ptr;
-       HashPosition pos;
-       char *new_key = NULL;
-       CURLcode err = 0;
-       
-       if (fields && !fields->nApplyCount) {
-               FOREACH_HASH_KEYVAL(pos, fields, key, data_ptr) {
-                       if (key.type != HASH_KEY_IS_STRING || *key.str) {
-                               new_key = format_key(key.type, key.str, key.num, prefix, 1);
-                               
-                               switch (Z_TYPE_PP(data_ptr)) {
-                                       case IS_ARRAY:
-                                       case IS_OBJECT: {
-                                               STATUS status;
-                                               
-                                               ++fields->nApplyCount;
-                                               status = recursive_fields(http_post_data, HASH_OF(*data_ptr), new_key TSRMLS_CC);
-                                               --fields->nApplyCount;
-                                               
-                                               if (SUCCESS != status) {
-                                                       goto error;
-                                               }
-                                               break;
-                                       }
-                                               
-                                       default: {
-                                               zval *data = php_http_zsep(IS_STRING, *data_ptr);
-                                               
-                                               err = curl_formadd(&http_post_data[0], &http_post_data[1],
-                                                       CURLFORM_COPYNAME,                      new_key,
-                                                       CURLFORM_COPYCONTENTS,          Z_STRVAL_P(data),
-                                                       CURLFORM_CONTENTSLENGTH,        (long) Z_STRLEN_P(data),
-                                                       CURLFORM_END
-                                               );
-                                               
-                                               zval_ptr_dtor(&data);
-                                               
-                                               if (CURLE_OK != err) {
-                                                       goto error;
-                                               }
-                                               break;
-                                       }
+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)
+{
+       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);
+
+                       if (Z_TYPE_P(val) != IS_ARRAY && Z_TYPE_P(val) != IS_OBJECT) {
+                               if (SUCCESS != add_recursive_field_value(body, str, val)) {
+                                       efree(str);
+                                       ZEND_HASH_DEC_APPLY_COUNT(fields);
+                                       return FAILURE;
                                }
-                               STR_FREE(new_key);
+                       } else if (SUCCESS != add_recursive_fields(body, str, HASH_OF(val))) {
+                               efree(str);
+                               ZEND_HASH_DEC_APPLY_COUNT(fields);
+                               return FAILURE;
                        }
+                       efree(str);
                }
+               ZEND_HASH_FOREACH_END();
+               ZEND_HASH_DEC_APPLY_COUNT(fields);
        }
-       
+
        return SUCCESS;
-       
-error:
-       if (new_key) {
-               efree(new_key);
-       }
-       if (http_post_data[0]) {
-               curl_formfree(http_post_data[0]);
-       }
-       if (err) {
-               php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Could not encode post fields: %s", curl_easy_strerror(err));
-       } else {
-               php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Could not encode post fields: unknown error");
-       }
-       return FAILURE;
 }
 
-static STATUS recursive_files(post_data http_post_data, HashTable *files, const char *prefix TSRMLS_DC) {
-       php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
-       zval **data_ptr;
-       HashPosition pos;
-       char *new_key = NULL;
-       CURLcode err = 0;
-       
-       if (files && !files->nApplyCount) {
-               FOREACH_HASH_KEYVAL(pos, files, key, data_ptr) {
-                       zval **file_ptr, **type_ptr, **name_ptr;
-                       
-                       if (key.type != HASH_KEY_IS_STRING || *key.str) {
-                               new_key = format_key(key.type, key.str, key.num, prefix, 0);
-                               
-                               if (Z_TYPE_PP(data_ptr) != IS_ARRAY && Z_TYPE_PP(data_ptr) != IS_OBJECT) {
-                                       if (new_key || key.type == HASH_KEY_IS_STRING) {
-                                               php_http_error(HE_NOTICE, PHP_HTTP_E_INVALID_PARAM, "Unrecognized type of post file array entry '%s'", new_key ? new_key : key.str);
-                                       } else {
-                                               php_http_error(HE_NOTICE, PHP_HTTP_E_INVALID_PARAM, "Unrecognized type of post file array entry '%lu'", key.num);
-                                       }
-                               } else if (     SUCCESS != zend_hash_find(HASH_OF(*data_ptr), "name", sizeof("name"), (void *) &name_ptr) ||
-                                                       SUCCESS != zend_hash_find(HASH_OF(*data_ptr), "type", sizeof("type"), (void *) &type_ptr) ||
-                                                       SUCCESS != zend_hash_find(HASH_OF(*data_ptr), "file", sizeof("file"), (void *) &file_ptr)) {
-                                       STATUS status;
-                                       
-                                       ++files->nApplyCount;
-                                       status = recursive_files(http_post_data, HASH_OF(*data_ptr), new_key TSRMLS_CC);
-                                       --files->nApplyCount;
-                                       
-                                       if (SUCCESS != status) {
-                                               goto error;
-                                       }
-                               } else {
-                                       const char *path;
-                                       zval *file = php_http_zsep(IS_STRING, *file_ptr);
-                                       zval *type = php_http_zsep(IS_STRING, *type_ptr);
-                                       zval *name = php_http_zsep(IS_STRING, *name_ptr);
-                                       
-                                       if (SUCCESS != php_check_open_basedir(Z_STRVAL_P(file) TSRMLS_CC)) {
-                                               goto error;
-                                       }
-                                       
-                                       /* this is blatant but should be sufficient for most cases */
-                                       if (strncasecmp(Z_STRVAL_P(file), "file://", lenof("file://"))) {
-                                               path = Z_STRVAL_P(file);
-                                       } else {
-                                               path = Z_STRVAL_P(file) + lenof("file://");
-                                       }
-                                       
-                                       if (new_key) {
-                                               char *tmp_key = format_key(HASH_KEY_IS_STRING, Z_STRVAL_P(name), 0, new_key, 0);
-                                               STR_SET(new_key, tmp_key);
-                                       }
-                                       
-                                       err = curl_formadd(&http_post_data[0], &http_post_data[1],
-                                               CURLFORM_COPYNAME,              new_key ? new_key : Z_STRVAL_P(name),
-                                               CURLFORM_FILE,                  path,
-                                               CURLFORM_CONTENTTYPE,   Z_STRVAL_P(type),
-                                               CURLFORM_END
-                                       );
-                                       
-                                       zval_ptr_dtor(&file);
-                                       zval_ptr_dtor(&type);
-                                       zval_ptr_dtor(&name);
-                                       
-                                       if (CURLE_OK != err) {
-                                               goto error;
+static ZEND_RESULT_CODE add_recursive_files(php_http_message_body_t *body, const char *name, HashTable *files)
+{
+       zval *zdata = NULL, *zfile, *zname, *ztype;
+
+       /* 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;
+               php_http_arrkey_t key;
+
+               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, HASH_OF(val))) {
+                                               efree(str);
+                                               ZEND_HASH_DEC_APPLY_COUNT(files);
+                                               return FAILURE;
                                        }
+                                       efree(str);
                                }
-                               STR_FREE(new_key);
                        }
+                       ZEND_HASH_FOREACH_END();
+                       ZEND_HASH_DEC_APPLY_COUNT(files);
                }
-       }
-       
-       return SUCCESS;
-       
-error:
-       if (new_key) {
-               efree(new_key);
-       }
-       if (http_post_data[0]) {
-               curl_formfree(http_post_data[0]);
-       }
-       if (err) {
-               php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Could not encode post files: %s", curl_easy_strerror(err));
+               return SUCCESS;
        } else {
-               php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Could not encode post files: unknown error");
+               /* stream entry */
+               php_stream *stream;
+               zend_string *zfc = zval_get_string(zfile);
+
+               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 {
+                               zend_string *tmp = zval_get_string(zdata);
+
+                               stream = php_stream_memory_open(TEMP_STREAM_READONLY, tmp->val, tmp->len);
+                               zend_string_release(tmp);
+                       }
+               } else {
+                       stream = php_stream_open_wrapper(zfc->val, "r", REPORT_ERRORS|USE_PATH, NULL);
+               }
+
+               if (!stream) {
+                       zend_string_release(zfc);
+                       return FAILURE;
+               } else {
+                       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);
+                       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;
+               }
+
        }
-       return FAILURE;
 }
 
-/* PHP */
+struct splitbody_arg {
+       php_http_buffer_t buf;
+       php_http_message_parser_t *parser;
+       char *boundary_str;
+       size_t boundary_len;
+       size_t consumed;
+};
 
-#define PHP_HTTP_BEGIN_ARGS(method, req_args)                  PHP_HTTP_BEGIN_ARGS_EX(HttpMessageBody, method, 0, req_args)
-#define PHP_HTTP_EMPTY_ARGS(method)                                            PHP_HTTP_EMPTY_ARGS_EX(HttpMessageBody, method, 0)
-#define PHP_HTTP_MESSAGE_BODY_ME(method, visibility)   PHP_ME(HttpMessageBody, method, PHP_HTTP_ARGS(HttpMessageBody, method), visibility)
+static size_t splitbody(void *opaque, char *buf, size_t len)
+{
+       struct splitbody_arg *arg = opaque;
+       const char *boundary = NULL;
+       size_t consumed = 0;
+       int first_boundary;
+
+       do {
+               first_boundary = !(consumed || arg->consumed);
+
+               if ((boundary = php_http_locate_str(buf, len, arg->boundary_str + first_boundary, arg->boundary_len - first_boundary))) {
+                       size_t real_boundary_len = arg->boundary_len - 1, cut;
+                       const char *real_boundary = boundary + !first_boundary;
+                       int eol_len = 0;
+
+                       if (buf + len <= real_boundary + real_boundary_len) {
+                               /* if we just have enough data for the boundary, it's just a byte too less */
+                               arg->consumed += consumed;
+                               return consumed;
+                       }
 
-PHP_HTTP_BEGIN_ARGS(__construct, 0)
-       PHP_HTTP_ARG_VAL(stream, 0)
-PHP_HTTP_END_ARGS;
+                       if (!first_boundary) {
+                               /* this is not the first boundary, read rest of this message */
+                               php_http_buffer_append(&arg->buf, buf, real_boundary - buf);
+                               php_http_message_parser_parse(arg->parser, &arg->buf, 0, &arg->parser->message);
+                       }
 
-PHP_HTTP_EMPTY_ARGS(__toString);
+                       /* move after the boundary */
+                       cut = real_boundary - buf + real_boundary_len;
+                       buf += cut;
+                       len -= cut;
+                       consumed += cut;
+
+                       if (buf == php_http_locate_bin_eol(buf, len, &eol_len)) {
+                               /* skip CRLF */
+                               buf += eol_len;
+                               len -= eol_len;
+                               consumed += eol_len;
+
+                               if (!first_boundary) {
+                                       /* advance messages */
+                                       php_http_message_t *msg;
+
+                                       msg = php_http_message_init(NULL, 0, NULL);
+                                       msg->parent = arg->parser->message;
+                                       arg->parser->message = msg;
+                               }
+                       } else {
+                               /* is this the last boundary? */
+                               if (*buf == '-') {
+                                       /* ignore the rest */
+                                       consumed += len;
+                                       len = 0;
+                               } else {
+                                       /* let this be garbage */
+                                       php_error_docref(NULL, E_WARNING, "Malformed multipart boundary at pos %zu", consumed);
+                                       return -1;
+                               }
+                       }
+               }
+       } while (boundary && len);
 
-PHP_HTTP_BEGIN_ARGS(toStream, 1)
-       PHP_HTTP_ARG_VAL(stream, 0)
-PHP_HTTP_END_ARGS;
+       /* let there be room for the next boundary */
+       if (len > arg->boundary_len) {
+               consumed += len - arg->boundary_len;
+               php_http_buffer_append(&arg->buf, buf, len - arg->boundary_len);
+               php_http_message_parser_parse(arg->parser, &arg->buf, 0, &arg->parser->message);
+       }
 
-PHP_HTTP_BEGIN_ARGS(toCallback, 1)
-       PHP_HTTP_ARG_VAL(callback, 0)
-PHP_HTTP_END_ARGS;
+       arg->consumed += consumed;
+       return consumed;
+}
 
-PHP_HTTP_BEGIN_ARGS(append, 1)
-       PHP_HTTP_ARG_VAL(string, 0)
-PHP_HTTP_END_ARGS;
+php_http_message_t *php_http_message_body_split(php_http_message_body_t *body, const char *boundary)
+{
+       php_stream *s = php_http_message_body_stream(body);
+       php_http_buffer_t *tmp = NULL;
+       php_http_message_t *msg = NULL;
+       struct splitbody_arg arg;
 
-PHP_HTTP_BEGIN_ARGS(add, 0)
-       PHP_HTTP_ARG_VAL(fields, 0)
-       PHP_HTTP_ARG_VAL(files, 0)
-PHP_HTTP_END_ARGS;
+       php_http_buffer_init(&arg.buf);
+       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);
+       }
 
-zend_class_entry *php_http_message_body_class_entry;
-zend_function_entry php_http_message_body_method_entry[] = {
-       PHP_HTTP_MESSAGE_BODY_ME(__construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
-       PHP_HTTP_MESSAGE_BODY_ME(__toString, ZEND_ACC_PUBLIC)
-       PHP_MALIAS(HttpMessageBody, toString, __toString, args_for_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
-       PHP_HTTP_MESSAGE_BODY_ME(toStream, ZEND_ACC_PUBLIC)
-       PHP_HTTP_MESSAGE_BODY_ME(toCallback, ZEND_ACC_PUBLIC)
-       PHP_HTTP_MESSAGE_BODY_ME(append, ZEND_ACC_PUBLIC)
-       PHP_HTTP_MESSAGE_BODY_ME(add, ZEND_ACC_PUBLIC)
-       EMPTY_FUNCTION_ENTRY
-};
-static zend_object_handlers php_http_message_body_object_handlers;
+       msg = arg.parser->message;
+       arg.parser->message = NULL;
 
-PHP_MINIT_FUNCTION(http_message_body)
-{
-       PHP_HTTP_REGISTER_CLASS(http\\message, Body, http_message_body, php_http_object_class_entry, 0);
-       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.clone_obj = php_http_message_body_object_clone;
+       php_http_buffer_free(&tmp);
+       php_http_message_parser_free(&arg.parser);
+       php_http_buffer_dtor(&arg.buf);
+       PTR_FREE(arg.boundary_str);
 
-       return SUCCESS;
+       return msg;
 }
 
-zend_object_value php_http_message_body_object_new(zend_class_entry *ce TSRMLS_DC)
+static zend_object_handlers php_http_message_body_object_handlers;
+
+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)
 {
-       zend_object_value ov;
        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;
        }
 
-       ov.handle = zend_objects_store_put((zend_object *) o, NULL, php_http_message_body_object_free, NULL TSRMLS_CC);
-       ov.handlers = &php_http_message_body_object_handlers;
+       o->zo.handlers = &php_http_message_body_object_handlers;
 
-       return ov;
+       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, php_http_message_body_copy(old_obj->body, NULL, 1), &new_obj);
-       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((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); \
+               } \
+       } while(0)
+
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___construct, 0, 0, 0)
+       ZEND_ARG_INFO(0, stream)
+ZEND_END_ARG_INFO();
 PHP_METHOD(HttpMessageBody, __construct)
 {
-       php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
-       zval *zstream;
+       php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
+       zval *zstream = NULL;
        php_stream *stream;
 
-       with_error_handling(EH_THROW, PHP_HTTP_EX_CE(runtime)) {
-               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "r", &zstream)) {
-                       php_stream_from_zval(stream, &zstream);
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "|r!", &zstream), invalid_arg, return);
 
-                       if (stream) {
-                               if (obj->body) {
-                                       php_http_message_body_dtor(obj->body);
-                               }
-                               obj->body = php_http_message_body_init(obj->body, stream TSRMLS_CC);
-                       }
+       if (zstream) {
+               php_http_expect(php_stream_from_zval_no_verify(stream, zstream), unexpected_val, return);
+
+               if (obj->body) {
+                       php_http_message_body_free(&obj->body);
                }
-       } end_error_handling();
+               obj->body = php_http_message_body_init(NULL, stream);
+       }
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody___toString, 0, 0, 0)
+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();
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_unserialize, 0, 0, 1)
+       ZEND_ARG_INFO(0, serialized)
+ZEND_END_ARG_INFO();
+PHP_METHOD(HttpMessageBody, unserialize)
+{
+       char *us_str;
+       size_t us_len;
+
+       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);
+       }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toStream, 0, 0, 1)
+       ZEND_ARG_INFO(0, stream)
+       ZEND_ARG_INFO(0, offset)
+       ZEND_ARG_INFO(0, maxlen)
+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_TRUE;
+               RETURN_ZVAL(getThis(), 1, 0);
        }
-       RETURN_FALSE;
 }
 
-struct fcd {
-       zval *fcz;
-       zend_fcall_info *fci;
-       zend_fcall_info_cache *fcc;
-};
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_toCallback, 0, 0, 1)
+       ZEND_ARG_INFO(0, callback)
+       ZEND_ARG_INFO(0, offset)
+       ZEND_ARG_INFO(0, maxlen)
+ZEND_END_ARG_INFO();
+PHP_METHOD(HttpMessageBody, toCallback)
+{
+       php_http_pass_fcall_arg_t fcd;
+       zend_long offset = 0, forlen = 0;
+
+       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);
 
-static size_t pass(void *cb_arg, const char *str, size_t len TSRMLS_DC)
+               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_FAST(getThis());
+       }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getResource, 0, 0, 0)
+ZEND_END_ARG_INFO();
+PHP_METHOD(HttpMessageBody, getResource)
 {
-       struct fcd *fcd = cb_arg;
-       zval *zdata;
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_http_message_body_object_t *obj = PHP_HTTP_OBJ(NULL, getThis());
+
+               PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
 
-       MAKE_STD_ZVAL(zdata);
-       ZVAL_STRINGL(zdata, str, len, 1);
-       if (SUCCESS == zend_fcall_info_argn(fcd->fci TSRMLS_CC, 2, fcd->fcz, zdata)) {
-               zend_fcall_info_call(fcd->fci, fcd->fcc, NULL, NULL TSRMLS_CC);
-               zend_fcall_info_args_clear(fcd->fci, 0);
+               ++GC_REFCOUNT(obj->body->res);
+               RETVAL_RES(obj->body->res);
        }
-       zval_ptr_dtor(&zdata);
-       return len;
 }
 
-PHP_METHOD(HttpMessageBody, toCallback)
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_getBoundary, 0, 0, 0)
+ZEND_END_ARG_INFO();
+PHP_METHOD(HttpMessageBody, getBoundary)
 {
-       struct fcd fcd;
-       long offset = 0, forlen = 0;
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_http_message_body_object_t * obj = PHP_HTTP_OBJ(NULL, getThis());
 
-       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);
-               fcd.fcz = getThis();
-               Z_ADDREF_P(fcd.fcz);
-               php_http_message_body_to_callback(obj->body, pass, &fcd, offset, forlen);
-               zval_ptr_dtor(&fcd.fcz);
-               RETURN_TRUE;
+               PHP_HTTP_MESSAGE_BODY_OBJECT_INIT(obj);
+
+               if (obj->body->boundary) {
+                       RETURN_STRING(obj->body->boundary);
+               }
        }
-       RETURN_FALSE;
 }
 
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_append, 0, 0, 1)
+       ZEND_ARG_INFO(0, string)
+ZEND_END_ARG_INFO();
 PHP_METHOD(HttpMessageBody, append)
 {
        char *str;
-       int len;
+       size_t len;
+       php_http_message_body_object_t *obj;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", &str, &len)) {
-               php_http_message_body_object_t *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);
 
-               RETURN_LONG(php_http_message_body_append(obj->body, str, len));
-       }
-       RETURN_FALSE;
+       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_FAST(getThis());
 }
 
-PHP_METHOD(HttpMessageBody, add)
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addForm, 0, 0, 0)
+       ZEND_ARG_ARRAY_INFO(0, fields, 1)
+       ZEND_ARG_ARRAY_INFO(0, files, 1)
+ZEND_END_ARG_INFO();
+PHP_METHOD(HttpMessageBody, addForm)
 {
        HashTable *fields = NULL, *files = NULL;
+       php_http_message_body_object_t *obj;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|hh", &fields, &files)) {
-               php_http_message_body_object_t *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);
 
-               RETURN_SUCCESS(php_http_message_body_add(obj->body, fields, files));
+       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_FAST(getThis());
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_addPart, 0, 0, 1)
+       ZEND_ARG_OBJ_INFO(0, message, http\\Message, 0)
+ZEND_END_ARG_INFO();
+PHP_METHOD(HttpMessageBody, addPart)
+{
+       zval *zobj;
+       php_http_message_body_object_t *obj;
+       php_http_message_object_t *mobj;
+       zend_error_handling zeh;
+
+       php_http_expect(SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS(), "O", &zobj, php_http_message_class_entry), invalid_arg, return);
+
+       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);
+       php_http_message_body_add_part(obj->body, mobj->message);
+       zend_restore_error_handling(&zeh);
+
+       if (!EG(exception)) {
+               RETURN_ZVAL_FAST(getThis());
        }
-       RETURN_FALSE;
 }
+
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_etag, 0, 0, 0)
+ZEND_END_ARG_INFO();
+PHP_METHOD(HttpMessageBody, etag)
+{
+       if (SUCCESS == zend_parse_parameters_none()) {
+               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_STR(php_http_cs2zs(etag, strlen(etag)));
+               } else {
+                       RETURN_FALSE;
+               }
+       }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpMessageBody_stat, 0, 0, 0)
+       ZEND_ARG_INFO(0, field)
+ZEND_END_ARG_INFO();
+PHP_METHOD(HttpMessageBody, stat)
+{
+       char *field_str = NULL;
+       size_t field_len = 0;
+
+       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);
+
+               if ((sb = php_http_message_body_stat(obj->body))) {
+                       if (field_str && field_len) {
+                                       switch (*field_str) {
+                                               case 's':
+                                               case 'S':
+                                                       RETURN_LONG(sb->sb.st_size);
+                                                       break;
+                                               case 'a':
+                                               case 'A':
+                                                       RETURN_LONG(sb->sb.st_atime);
+                                                       break;
+                                               case 'm':
+                                               case 'M':
+                                                       RETURN_LONG(sb->sb.st_mtime);
+                                                       break;
+                                               case 'c':
+                                               case 'C':
+                                                       RETURN_LONG(sb->sb.st_ctime);
+                                                       break;
+                                               default:
+                                                       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_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);
+                       }
+               }
+       }
+}
+
+static zend_function_entry php_http_message_body_methods[] = {
+       PHP_ME(HttpMessageBody, __construct,  ai_HttpMessageBody___construct,  ZEND_ACC_PUBLIC|ZEND_ACC_CTOR)
+       PHP_ME(HttpMessageBody, __toString,   ai_HttpMessageBody___toString,   ZEND_ACC_PUBLIC)
+       PHP_MALIAS(HttpMessageBody, toString, __toString, ai_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
+       PHP_MALIAS(HttpMessageBody, serialize, __toString, ai_HttpMessageBody___toString, ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessageBody, unserialize,  ai_HttpMessageBody_unserialize,  ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessageBody, toStream,     ai_HttpMessageBody_toStream,     ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessageBody, toCallback,   ai_HttpMessageBody_toCallback,   ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessageBody, getResource,  ai_HttpMessageBody_getResource,  ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessageBody, getBoundary,  ai_HttpMessageBody_getBoundary,  ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessageBody, append,       ai_HttpMessageBody_append,       ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessageBody, addForm,      ai_HttpMessageBody_addForm,      ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessageBody, addPart,      ai_HttpMessageBody_addPart,      ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessageBody, etag,         ai_HttpMessageBody_etag,         ZEND_ACC_PUBLIC)
+       PHP_ME(HttpMessageBody, stat,         ai_HttpMessageBody_stat,         ZEND_ACC_PUBLIC)
+       EMPTY_FUNCTION_ENTRY
+};
+
+zend_class_entry *php_http_message_body_class_entry;
+
+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);
+       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;
+       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;
+}
+
 /*
  * Local variables:
  * tab-width: 4