Fix error: redefinition of typedef 'php_http_message_t' during RHEL build
[m6w6/ext-http] / php_http_message_body.c
index 969f860af9b1a934fc752c3a2b67098900937da8..41f036c1cc2c29de97624a7534cab82f99cdc559 100644 (file)
@@ -6,33 +6,43 @@
     | modification, are permitted provided that the conditions mentioned |
     | in the accompanying LICENSE file are met.                          |
     +--------------------------------------------------------------------+
-    | Copyright (c) 2004-2010, Michael Wallner <mike@php.net>            |
+    | Copyright (c) 2004-2011, Michael Wallner <mike@php.net>            |
     +--------------------------------------------------------------------+
 */
 
-/* $Id: http_message_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)
+#define BOUNDARY_CLOSE(body) \
+               php_http_message_body_appendf(body, PHP_HTTP_CRLF "--%s--" PHP_HTTP_CRLF, php_http_message_body_boundary(body))
 
-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);
+static STATUS add_recursive_fields(php_http_message_body_t *body, const char *name, zval *value);
+static STATUS add_recursive_files(php_http_message_body_t *body, const char *name, zval *value);
 
 PHP_HTTP_API php_http_message_body_t *php_http_message_body_init(php_http_message_body_t *body, php_stream *stream TSRMLS_DC)
 {
        if (!body) {
                body = emalloc(sizeof(php_http_message_body_t));
        }
+       memset(body, 0, sizeof(*body));
        
        if (stream) {
                php_stream_auto_cleanup(stream);
                body->stream_id = php_stream_get_resource_id(stream);
                zend_list_addref(body->stream_id);
        } else {
-               stream = php_stream_temp_new();
+               stream = php_stream_temp_create(TEMP_STREAM_DEFAULT, 0xffff);
                php_stream_auto_cleanup(stream);
                body->stream_id = php_stream_get_resource_id(stream);
        }
@@ -55,16 +65,20 @@ PHP_HTTP_API php_http_message_body_t *php_http_message_body_copy(php_http_messag
                        to = php_http_message_body_init(to, php_http_message_body_stream(from) TSRMLS_CC);
                }
 
+               if (from->boundary) {
+                       to->boundary = estrdup(from->boundary);
+               }
+
                return to;
        }
 }
 
 PHP_HTTP_API void php_http_message_body_dtor(php_http_message_body_t *body)
 {
-       if (body) {
-               /* NO FIXME: shows leakinfo in DEBUG mode */
-               zend_list_delete(body->stream_id);
-       }
+       TSRMLS_FETCH_FROM_CTX(body->ts);
+       /* NO FIXME: shows leakinfo in DEBUG mode */
+       zend_list_delete(body->stream_id);
+       STR_FREE(body->boundary);
 }
 
 PHP_HTTP_API void php_http_message_body_free(php_http_message_body_t **body)
@@ -76,29 +90,45 @@ PHP_HTTP_API void php_http_message_body_free(php_http_message_body_t **body)
        }
 }
 
-PHP_HTTP_API php_stream_statbuf *php_http_message_body_stat(php_http_message_body_t *body)
+PHP_HTTP_API 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 const char *php_http_message_body_boundary(php_http_message_body_t *body)
+{
+       if (!body->boundary) {
+               union { double dbl; int num[2]; } data;
+               TSRMLS_FETCH_FROM_CTX(body->ts);
+
+               data.dbl = php_combined_lcg(TSRMLS_C);
+               spprintf(&body->boundary, 0, "%x.%x", data.num[0], data.num[1]);
+       }
+       return body->boundary;
+}
+
 PHP_HTTP_API 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);
+       const php_stream_statbuf *ssb = php_http_message_body_stat(body);
 
        /* real file or temp buffer ? */
-       if (body->ssb.sb.st_mtime) {
+       if (ssb && ssb->sb.st_mtime) {
                char *etag;
 
                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);
+               php_http_etag_t *etag = php_http_etag_init(PHP_HTTP_G->env.etag_mode TSRMLS_CC);
 
-               php_http_message_body_to_callback(body, php_http_etag_update, ctx, 0, 0);
-               return php_http_etag_finish(ctx TSRMLS_CC);
+               if (etag) {
+                       php_http_message_body_to_callback(body, (php_http_pass_callback_t) php_http_etag_update, etag, 0, 0);
+                       return php_http_etag_finish(etag);
+               } else {
+                       return NULL;
+               }
        }
 }
 
@@ -130,6 +160,7 @@ PHP_HTTP_API void php_http_message_body_to_callback(php_http_message_body_t *bod
 {
        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,8 +168,7 @@ 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);
@@ -152,89 +182,105 @@ PHP_HTTP_API void php_http_message_body_to_callback(php_http_message_body_t *bod
                        break;
                }
        }
+       efree(buf);
 }
 
-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)
+PHP_HTTP_API size_t php_http_message_body_append(php_http_message_body_t *body, const char *buf, size_t len)
 {
+       php_stream *s;
        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;
-
-                               while (length > 0 && !php_stream_eof(s)) {
-                                       char buf[0x1000];
-                                       size_t read = php_stream_read(s, buf, MIN(length, sizeof(buf)));
 
-                                       if (read) {
-                                               cb(cb_arg, buf, read);
-                                       }
-
-                                       if (read < MIN(length, sizeof(buf))) {
-                                               break;
-                                       }
-
-                                       length -= read;
-                               }
-                       }
-               }
-       }
-
-       return SUCCESS;
+       s = php_http_message_body_stream(body);
+       php_stream_seek(s, 0, SEEK_END);
+       return php_stream_write(s, buf, len);
 }
 
-PHP_HTTP_API size_t php_http_message_body_append(php_http_message_body_t *body, const char *buf, size_t len)
+PHP_HTTP_API 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;
 
-       php_stream_seek(s, 0, SEEK_END);
-       return php_stream_write(s, buf, 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);
+
+       return print_len;
 }
 
 PHP_HTTP_API STATUS php_http_message_body_add(php_http_message_body_t *body, HashTable *fields, HashTable *files)
 {
-       post_data http_post_data = {NULL, NULL};
-       TSRMLS_FETCH_FROM_CTX(body->ts);
+       zval tmp;
 
-       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) {
+               INIT_PZVAL_ARRAY(&tmp, fields);
+               if (SUCCESS != add_recursive_fields(body, NULL, &tmp)) {
+                       return FAILURE;
+               }
        }
-       if (CURLE_OK != curl_formget(http_post_data[0], body, (curl_formget_callback) php_http_message_body_append)) {
-               return FAILURE;
+       if (files) {
+               INIT_PZVAL_ARRAY(&tmp, files);
+               if (SUCCESS != add_recursive_files(body, NULL, &tmp)) {
+                       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);
+       char *safe_name;
+       TSRMLS_FETCH_FROM_CTX(body->ts);
+
+       safe_name = php_addslashes(estrdup(name), strlen(name), NULL, 1 TSRMLS_CC);
+
+       BOUNDARY_OPEN(body);
+       php_http_message_body_appendf(
+               body,
+               "Content-Disposition: form-data; name=\"%s\"" PHP_HTTP_CRLF
+               "" PHP_HTTP_CRLF,
+               safe_name
+       );
+       php_http_message_body_append(body, value_str, value_len);
+       BOUNDARY_CLOSE(body);
+
+       efree(safe_name);
+       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)
+PHP_HTTP_API STATUS php_http_message_body_add_file(php_http_message_body_t *body, const char *name, const char *ctype, const char *path, php_stream *in)
 {
-       return add_file(body, name, path, ctype);
+       char *safe_name, *path_dup = estrdup(path);
+       TSRMLS_FETCH_FROM_CTX(body->ts);
+
+       safe_name = php_addslashes(estrdup(name), strlen(name), NULL, 1 TSRMLS_CC);
+
+       BOUNDARY_OPEN(body);
+       php_http_message_body_appendf(
+               body,
+               "Content-Disposition: attachment; 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, basename(path_dup), ctype
+       );
+       php_stream_copy_to_stream_ex(in, php_http_message_body_stream(body), PHP_STREAM_COPY_ALL, NULL);
+       BOUNDARY_CLOSE(body);
+
+       efree(safe_name);
+       efree(path_dup);
+
+       return SUCCESS;
 }
 
-static inline char *format_key(uint type, char *str, ulong num, const char *prefix, int numeric_key_for_empty_prefix) {
+static inline char *format_key(uint type, char *str, ulong num, const char *prefix) {
        char *new_key = NULL;
-       
+
        if (prefix && *prefix) {
                if (type == HASH_KEY_IS_STRING) {
                        spprintf(&new_key, 0, "%s[%s]", prefix, str);
@@ -243,223 +289,223 @@ static inline char *format_key(uint type, char *str, ulong num, const char *pref
                }
        } else if (type == HASH_KEY_IS_STRING) {
                new_key = estrdup(str);
-       } else if (numeric_key_for_empty_prefix) {
-               spprintf(&new_key, 0, "%lu", num);
+       } else {
+               new_key = estrdup("");
        }
-       
+
        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)
+static STATUS add_recursive_fields(php_http_message_body_t *body, const char *name, zval *value)
 {
-       post_data http_post_data = {NULL, NULL};
-       CURLcode err;
-
-       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
-       );
-
-       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;
+       if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) {
+               zval **val;
+               HashTable *ht;
+               HashPosition pos;
+               php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
+               TSRMLS_FETCH_FROM_CTX(body->ts);
+
+               ht = HASH_OF(value);
+               if (!ht->nApplyCount) {
+                       ++ht->nApplyCount;
+                       FOREACH_KEYVAL(pos, value, key, val) {
+                               char *str = format_key(key.type, key.str, key.num, name);
+                               if (SUCCESS != add_recursive_fields(body, str, *val)) {
+                                       efree(str);
+                                       ht->nApplyCount--;
+                                       return FAILURE;
+                               }
+                               efree(str);
+                       }
+                       --ht->nApplyCount;
+               }
+       } else {
+               zval *cpy = php_http_ztyp(IS_STRING, value);
+               php_http_message_body_add_field(body, name, Z_STRVAL_P(cpy), Z_STRLEN_P(cpy));
+               zval_ptr_dtor(&cpy);
        }
 
-       curl_formfree(http_post_data[0]);
        return SUCCESS;
 }
 
-static inline STATUS add_file(php_http_message_body_t *body, const char *name, const char *path, const char *ctype)
+static STATUS add_recursive_files(php_http_message_body_t *body, const char *name, zval *value)
 {
-       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
-       );
+       zval **zdata = NULL, **zfile, **zname, **ztype;
+       HashTable *ht;
+       TSRMLS_FETCH_FROM_CTX(body->ts);
 
-       if (CURLE_OK != err) {
+       if (Z_TYPE_P(value) != IS_ARRAY && Z_TYPE_P(value) != IS_OBJECT) {
+               php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Expected array or object (name, type, file) for message body file to add");
                return FAILURE;
        }
 
-       err = curl_formget(http_post_data[0], body, (curl_formget_callback) php_http_message_body_append);
+       ht = HASH_OF(value);
 
-       if (CURLE_OK != err) {
-               curl_formfree(http_post_data[0]);
-               return FAILURE;
-       }
+       if ((SUCCESS != zend_hash_find(ht, ZEND_STRS("name"), (void *) &zname))
+       ||      (SUCCESS != zend_hash_find(ht, ZEND_STRS("type"), (void *) &ztype))
+       ||      (SUCCESS != zend_hash_find(ht, ZEND_STRS("file"), (void *) &zfile))
+       ) {
+               zval **val;
+               HashPosition pos;
+               php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
 
-       curl_formfree(http_post_data[0]);
-       return SUCCESS;
-}
+               if (!ht->nApplyCount) {
+                       ++ht->nApplyCount;
+                       FOREACH_HASH_KEYVAL(pos, ht, key, val) {
+                               if (Z_TYPE_PP(val) == IS_ARRAY || Z_TYPE_PP(val) == IS_OBJECT) {
+                                       char *str = format_key(key.type, key.str, key.num, name);
 
-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;
+                                       if (SUCCESS != add_recursive_files(body, str, *val)) {
+                                               efree(str);
+                                               --ht->nApplyCount;
+                                               return FAILURE;
                                        }
+                                       efree(str);
                                }
-                               STR_FREE(new_key);
                        }
+                       --ht->nApplyCount;
                }
-       }
-       
-       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));
+               return SUCCESS;
        } else {
-               php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Could not encode post fields: unknown error");
+               php_stream *stream;
+               zval *zfc = php_http_ztyp(IS_STRING, *zfile);
+
+               if (SUCCESS == zend_hash_find(ht, ZEND_STRS("data"), (void *) &zdata)) {
+                       if (Z_TYPE_PP(zdata) == IS_RESOURCE) {
+                               php_stream_from_zval_no_verify(stream, zdata);
+                       } else {
+                               zval *tmp = php_http_ztyp(IS_STRING, *zdata);
+
+                               stream = php_stream_memory_open(TEMP_STREAM_READONLY, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp));
+                               zval_ptr_dtor(&tmp);
+                       }
+               } else {
+                       stream = php_stream_open_wrapper(Z_STRVAL_P(zfc), "r", REPORT_ERRORS|USE_PATH, NULL);
+               }
+
+               if (!stream) {
+                       zval_ptr_dtor(&zfc);
+                       return FAILURE;
+               } else {
+                       zval *znc = php_http_ztyp(IS_STRING, *zname), *ztc = php_http_ztyp(IS_STRING, *ztype);
+                       char *key = format_key(HASH_KEY_IS_STRING, Z_STRVAL_P(znc), 0, name);
+                       STATUS ret =  php_http_message_body_add_file(body, key, Z_STRVAL_P(ztc), Z_STRVAL_P(zfc), stream);
+
+                       efree(key);
+                       zval_ptr_dtor(&znc);
+                       zval_ptr_dtor(&ztc);
+                       zval_ptr_dtor(&zfc);
+                       if (!zdata || Z_TYPE_PP(zdata) != IS_RESOURCE) {
+                               php_stream_close(stream);
+                       }
+                       return ret;
+               }
+
        }
-       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;
-                                       }
+struct splitbody_arg {
+       php_http_buffer_t buf;
+       php_http_message_parser_t *parser;
+       char *boundary_str;
+       size_t boundary_len;
+       size_t consumed;
+};
+
+static size_t splitbody(void *opaque, char *buf, size_t len TSRMLS_DC)
+{
+       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;
+
+                       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;
+                       }
+
+                       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);
+                       }
+
+                       /* 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, NULL)) {
+                               if (!first_boundary) {
+                                       /* advance messages */
+                                       php_http_message_t *msg;
+
+                                       msg = php_http_message_init(NULL, 0 TSRMLS_CC);
+                                       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 {
-                                       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;
-                                       }
+                                       /* let this be garbage */
+                                       php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Malformed multipart boundary at pos %zu", consumed);
+                                       return -1;
                                }
-                               STR_FREE(new_key);
                        }
                }
+       } while (boundary && len);
+
+       /* 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);
        }
-       
-       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));
-       } else {
-               php_http_error(HE_WARNING, PHP_HTTP_E_ENCODING, "Could not encode post files: unknown error");
+
+       arg->consumed += consumed;
+       return consumed;
+}
+
+PHP_HTTP_API 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;
+       TSRMLS_FETCH_FROM_CTX(body->ts);
+
+       php_http_buffer_init(&arg.buf);
+       arg.parser = php_http_message_parser_init(NULL TSRMLS_CC);
+       arg.boundary_len = spprintf(&arg.boundary_str, 0, "\n--%s", boundary);
+       arg.consumed = 0;
+
+       php_stream_rewind(s);
+       while (!php_stream_eof(s)) {
+               php_http_buffer_passthru(&tmp, 0x1000, (php_http_buffer_pass_func_t) _php_stream_read, s, splitbody, &arg TSRMLS_CC);
        }
-       return FAILURE;
+
+       msg = arg.parser->message;
+       arg.parser->message = NULL;
+
+       php_http_buffer_free(&tmp);
+       php_http_message_parser_free(&arg.parser);
+       php_http_buffer_dtor(&arg.buf);
+       STR_FREE(arg.boundary_str);
+
+       return msg;
 }
 
 /* PHP */
@@ -487,10 +533,15 @@ PHP_HTTP_BEGIN_ARGS(append, 1)
 PHP_HTTP_END_ARGS;
 
 PHP_HTTP_BEGIN_ARGS(add, 0)
-       PHP_HTTP_ARG_VAL(fields, 0)
-       PHP_HTTP_ARG_VAL(files, 0)
+       PHP_HTTP_ARG_ARR(fields, 1, 0)
+       PHP_HTTP_ARG_ARR(files, 1, 0)
 PHP_HTTP_END_ARGS;
 
+PHP_HTTP_EMPTY_ARGS(etag);
+
+PHP_HTTP_BEGIN_ARGS(stat, 0)
+       PHP_HTTP_ARG_VAL(what, 0)
+PHP_HTTP_END_ARGS;
 
 zend_class_entry *php_http_message_body_class_entry;
 zend_function_entry php_http_message_body_method_entry[] = {
@@ -501,13 +552,15 @@ zend_function_entry php_http_message_body_method_entry[] = {
        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)
+       PHP_HTTP_MESSAGE_BODY_ME(etag, ZEND_ACC_PUBLIC)
+       PHP_HTTP_MESSAGE_BODY_ME(stat, ZEND_ACC_PUBLIC)
        EMPTY_FUNCTION_ENTRY
 };
 static zend_object_handlers php_http_message_body_object_handlers;
 
 PHP_MINIT_FUNCTION(http_message_body)
 {
-       PHP_HTTP_REGISTER_CLASS(http\\message, Body, http_message_body, php_http_object_class_entry, 0);
+       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;
@@ -549,7 +602,7 @@ zend_object_value php_http_message_body_object_clone(zval *object TSRMLS_DC)
        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);
 
-       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);
+       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 TSRMLS_CC);
        zend_objects_clone_members(&new_obj->zo, new_ov, &old_obj->zo, Z_OBJ_HANDLE_P(object) TSRMLS_CC);
 
        return new_ov;
@@ -568,18 +621,23 @@ void php_http_message_body_object_free(void *object TSRMLS_DC)
 PHP_METHOD(HttpMessageBody, __construct)
 {
        php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
-       zval *zstream;
+       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);
+       with_error_handling(EH_THROW, php_http_exception_class_entry) {
+               if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|r!", &zstream)) {
+                       if (zstream) {
+                               php_stream_from_zval(stream, &zstream);
 
-                       if (stream) {
-                               if (obj->body) {
-                                       php_http_message_body_dtor(obj->body);
+                               if (stream) {
+                                       if (obj->body) {
+                                               php_http_message_body_dtor(obj->body);
+                                       }
+                                       obj->body = php_http_message_body_init(obj->body, stream TSRMLS_CC);
                                }
-                               obj->body = php_http_message_body_init(obj->body, stream TSRMLS_CC);
+                       }
+                       if (!obj->body) {
+                               obj->body = php_http_message_body_init(NULL, NULL TSRMLS_CC);
                        }
                }
        } end_error_handling();
@@ -618,20 +676,24 @@ PHP_METHOD(HttpMessageBody, toStream)
 
 struct fcd {
        zval *fcz;
-       zend_fcall_info *fci;
-       zend_fcall_info_cache *fcc;
+       zend_fcall_info fci;
+       zend_fcall_info_cache fcc;
+#ifdef ZTS
+       void ***ts;
+#endif
 };
 
-static size_t pass(void *cb_arg, const char *str, size_t len TSRMLS_DC)
+static size_t pass(void *cb_arg, const char *str, size_t len)
 {
        struct fcd *fcd = cb_arg;
        zval *zdata;
+       TSRMLS_FETCH_FROM_CTX(fcd->ts);
 
        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);
+       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);
        }
        zval_ptr_dtor(&zdata);
        return len;
@@ -639,14 +701,19 @@ static size_t pass(void *cb_arg, const char *str, size_t len TSRMLS_DC)
 
 PHP_METHOD(HttpMessageBody, toCallback)
 {
-       struct fcd fcd;
+       struct fcd fcd = {0};
        long offset = 0, forlen = 0;
 
        if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "f|ll", &fcd.fci, &fcd.fcc, &offset, &forlen)) {
                php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+
                fcd.fcz = getThis();
                Z_ADDREF_P(fcd.fcz);
+               TSRMLS_SET_CTX(fcd.ts);
+
                php_http_message_body_to_callback(obj->body, pass, &fcd, offset, forlen);
+               zend_fcall_info_args_clear(&fcd.fci, 1);
+
                zval_ptr_dtor(&fcd.fcz);
                RETURN_TRUE;
        }
@@ -670,13 +737,72 @@ PHP_METHOD(HttpMessageBody, add)
 {
        HashTable *fields = NULL, *files = NULL;
 
-       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|hh", &fields, &files)) {
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|h!h!", &fields, &files)) {
                php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
 
                RETURN_SUCCESS(php_http_message_body_add(obj->body, fields, files));
        }
        RETURN_FALSE;
 }
+
+PHP_METHOD(HttpMessageBody, etag)
+{
+       if (SUCCESS == zend_parse_parameters_none()) {
+               php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               char *etag = php_http_message_body_etag(obj->body);
+
+               if (etag) {
+                       RETURN_STRING(etag, 0);
+               }
+       }
+       RETURN_FALSE;
+}
+
+PHP_METHOD(HttpMessageBody, stat)
+{
+       char *field_str = NULL;
+       int field_len = 0;
+
+       if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s", &field_str, &field_len)) {
+               php_http_message_body_object_t *obj = zend_object_store_get_object(getThis() TSRMLS_CC);
+               const php_stream_statbuf *sb = php_http_message_body_stat(obj->body);
+
+               if (sb) {
+                       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_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "unknown stat field: '%s' (should be one of [s]ize, [a]time, [m]time or [c]time)", field_str);
+                                                       break;
+                                       }
+                       } else {
+                               array_init(return_value);
+                               add_assoc_long_ex(return_value, ZEND_STRS("size"), sb->sb.st_size);
+                               add_assoc_long_ex(return_value, ZEND_STRS("atime"), sb->sb.st_atime);
+                               add_assoc_long_ex(return_value, ZEND_STRS("mtime"), sb->sb.st_mtime);
+                               add_assoc_long_ex(return_value, ZEND_STRS("ctime"), sb->sb.st_ctime);
+                               return;
+                       }
+               }
+       }
+       RETURN_FALSE;
+}
+
 /*
  * Local variables:
  * tab-width: 4