From 4d68865693332389b875e4466f8b5368c0876e15 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Thu, 19 Jan 2012 08:56:36 +0000 Subject: [PATCH] remodel file adding to accept paths, streams and plain data as upload files --- php_http_message_body.c | 186 ++++++++++++++++++++++++---------------- php_http_message_body.h | 20 ++++- 2 files changed, 131 insertions(+), 75 deletions(-) diff --git a/php_http_message_body.c b/php_http_message_body.c index 14486a9..a33e330 100644 --- a/php_http_message_body.c +++ b/php_http_message_body.c @@ -248,44 +248,56 @@ PHP_HTTP_API STATUS php_http_message_body_add_field(php_http_message_body_t *bod 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) { - php_stream *in; - char *path_dup = estrdup(path); + php_stream_statbuf ssb = {{0}}; + php_stream_filter *tef = NULL; + char *safe_name, *path_dup = estrdup(path); TSRMLS_FETCH_FROM_CTX(body->ts); - if ((in = php_stream_open_wrapper(path_dup, "r", REPORT_ERRORS|USE_PATH|STREAM_MUST_SEEK, NULL))) { - php_stream_statbuf ssb = {{0}}; - - if (SUCCESS == php_stream_stat(in, &ssb) && S_ISREG(ssb.sb.st_mode)) { - char *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-Type: %s" PHP_HTTP_CRLF - "Content-Length: %zu" PHP_HTTP_CRLF - "" PHP_HTTP_CRLF, - safe_name, basename(path_dup), ctype, ssb.sb.st_size); - 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); - php_stream_close(in); - return SUCCESS; - } else { - efree(path_dup); - php_stream_close(in); - php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Not a valid regular file: %s", path); - return FAILURE; - } + 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-Type: %s" PHP_HTTP_CRLF, + safe_name, basename(path_dup), ctype + ); + + if (SUCCESS == php_stream_stat(in, &ssb)) { + php_http_message_body_appendf( + body, + "Content-Length: %zu" PHP_HTTP_CRLF + "" PHP_HTTP_CRLF, + ssb.sb.st_size + ); } else { - efree(path_dup); - return FAILURE; + php_http_message_body_append( + body, + ZEND_STRL( + "Transfer-Encoding: chunked" PHP_HTTP_CRLF + "" PHP_HTTP_CRLF + ) + ); + + if ((tef = php_http_filter_factory.create_filter("http.chunked_encode", NULL, 0 TSRMLS_CC))) { + php_stream_filter_append(&in->readfilters, tef); + } + } + + php_stream_copy_to_stream_ex(in, php_http_message_body_stream(body), PHP_STREAM_COPY_ALL, NULL); + BOUNDARY_CLOSE(body); + + if (tef) { + php_stream_filter_remove(tef, 1 TSRMLS_CC); + php_stream_filter_free(tef TSRMLS_CC); } + efree(safe_name); + efree(path_dup); + + return SUCCESS; } static inline char *format_key(uint type, char *str, ulong num, const char *prefix) { @@ -300,7 +312,7 @@ 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 { - spprintf(&new_key, 0, "%lu", num); + estrdup(""); } return new_key; @@ -310,22 +322,24 @@ static STATUS add_recursive_fields(php_http_message_body_t *body, const char *na { 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); - if (!HASH_OF(value)->nApplyCount) { - ++HASH_OF(value)->nApplyCount; + 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); - HASH_OF(value)->nApplyCount--; + ht->nApplyCount--; return FAILURE; } efree(str); } - --HASH_OF(value)->nApplyCount; + --ht->nApplyCount; } } else { zval *cpy = php_http_ztyp(IS_STRING, value); @@ -338,52 +352,76 @@ static STATUS add_recursive_fields(php_http_message_body_t *body, const char *na static STATUS add_recursive_files(php_http_message_body_t *body, const char *name, zval *value) { - if (Z_TYPE_P(value) == IS_ARRAY || Z_TYPE_P(value) == IS_OBJECT) { - zval **zfile, **zname, **ztype; - TSRMLS_FETCH_FROM_CTX(body->ts); + zval **zdata = NULL, **zfile, **zname, **ztype; + HashTable *ht; + TSRMLS_FETCH_FROM_CTX(body->ts); - if ((SUCCESS == zend_hash_find(HASH_OF(value), ZEND_STRS("name"), (void *) &zname)) - && (SUCCESS == zend_hash_find(HASH_OF(value), ZEND_STRS("file"), (void *) &zfile)) - && (SUCCESS == zend_hash_find(HASH_OF(value), ZEND_STRS("type"), (void *) &ztype)) - ) { - zval *zfc = php_http_ztyp(IS_STRING, *zfile), *znc = php_http_ztyp(IS_STRING, *zname), *ztc = php_http_ztyp(IS_STRING, *ztype); - char *str = format_key(HASH_KEY_IS_STRING, Z_STRVAL_P(znc), 0, name); - STATUS ret = php_http_message_body_add_file(body, str, Z_STRVAL_P(zfc), Z_STRVAL_P(ztc)); + 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; + } - efree(str); - zval_ptr_dtor(&znc); - zval_ptr_dtor(&zfc); - zval_ptr_dtor(&ztc); + ht = HASH_OF(value); - if (ret != SUCCESS) { - return ret; - } - } else { - zval **val; - HashPosition pos; - php_http_array_hashkey_t key = php_http_array_hashkey_init(0); - - if (!HASH_OF(value)->nApplyCount) { - ++HASH_OF(value)->nApplyCount; - FOREACH_KEYVAL(pos, value, key, val) { - char *str = format_key(key.type, key.str, key.num, name); - if (SUCCESS != add_recursive_files(body, str, *val)) { - efree(str); - --HASH_OF(value)->nApplyCount; - 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); + + if (!ht->nApplyCount) { + ++ht->nApplyCount; + FOREACH_HASH_KEYVAL(pos, ht, key, val) { + char *str = format_key(key.type, key.str, key.num, name); + if (SUCCESS != add_recursive_files(body, str, *val)) { efree(str); + --ht->nApplyCount; + return FAILURE; } - --HASH_OF(value)->nApplyCount; + efree(str); } + --ht->nApplyCount; } + return SUCCESS; } else { - TSRMLS_FETCH_FROM_CTX(body->ts); - php_http_error(HE_WARNING, PHP_HTTP_E_MESSAGE_BODY, "Unrecognized array format for message body file to add"); - return FAILURE; - } + php_stream *stream; + zval *zfc = php_http_ztyp(IS_STRING, *zfile); - return SUCCESS; + 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); + + if ((stream = php_stream_temp_new())) { + php_stream_write(stream, 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(&zfc); + if (!zdata || Z_TYPE_PP(zdata) != IS_RESOURCE) { + php_stream_close(stream); + } + return ret; + } + + } } /* PHP */ diff --git a/php_http_message_body.h b/php_http_message_body.h index 9147cbf..18670bd 100644 --- a/php_http_message_body.h +++ b/php_http_message_body.h @@ -22,12 +22,30 @@ typedef struct php_http_message_body { #endif } php_http_message_body_t; +typedef enum php_http_message_body_file_is { + PHP_HTTP_MESSAGE_BODY_FILE_IS_EMPTY, + PHP_HTTP_MESSAGE_BODY_FILE_IS_DATA, + PHP_HTTP_MESSAGE_BODY_FILE_IS_PATH, + PHP_HTTP_MESSAGE_BODY_FILE_IS_STREAM +} php_http_message_body_file_is_t; + +typedef struct php_http_message_body_file { + php_http_message_body_file_is_t type; + size_t length; + union { + char *data; + char *path; + php_stream *stream; + } file; +} php_http_message_body_file_t; + PHP_HTTP_API php_http_message_body_t *php_http_message_body_init(php_http_message_body_t *body, php_stream *stream TSRMLS_DC); 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); PHP_HTTP_API STATUS php_http_message_body_add(php_http_message_body_t *body, HashTable *fields, HashTable *files); 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); -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 *file, php_stream *stream); 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, ...); 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); PHP_HTTP_API void php_http_message_body_to_stream(php_http_message_body_t *body, php_stream *s, off_t offset, size_t forlen); 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); -- 2.30.2