Fix operator for Visual Studio
[m6w6/ext-http] / src / php_http_message_body.c
index 18859a3b409cb8fe1a4d30479892f62afa37fc55..97b1a148bf3ae87c9b3984530cd563b3b7133b55 100644 (file)
@@ -40,13 +40,13 @@ php_http_message_body_t *php_http_message_body_init(php_http_message_body_t **bo
                php_http_message_body_addref(body);
                return body;
        }
-       
+
        body = ecalloc(1, sizeof(php_http_message_body_t));
        body->refcount = 1;
 
        if (stream) {
                body->res = stream->res;
-               ++GC_REFCOUNT(body->res);
+               GC_ADDREF(body->res);
        } else {
                body->res = php_stream_temp_create(TEMP_STREAM_DEFAULT, 0xffff)->res;
        }
@@ -90,9 +90,9 @@ void php_http_message_body_free(php_http_message_body_t **body_ptr)
 {
        if (*body_ptr) {
                php_http_message_body_t *body = *body_ptr;
-
                if (!--body->refcount) {
                        zend_list_delete(body->res);
+                       body->res = NULL;
                        PTR_FREE(body->boundary);
                        efree(body);
                }
@@ -214,7 +214,7 @@ size_t php_http_message_body_append(php_http_message_body_t *body, const char *b
        written = php_stream_write(s, buf, len);
 
        if (written != len) {
-               php_error_docref(NULL, E_WARNING, "Failed to append %zu bytes to body; wrote %zu", len, written);
+               php_error_docref(NULL, E_WARNING, "Failed to append %zu bytes to body; wrote %zu", len, written == (size_t) -1 ? 0 : written);
        }
 
        return len;
@@ -262,9 +262,14 @@ void php_http_message_body_add_part(php_http_message_body_t *body, php_http_mess
 
 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)
 {
-       zend_string *safe_name = zend_string_init(name, strlen(name), 0);
+       zend_string *safe_name, *zstr_name = zend_string_init(name, strlen(name), 0);
 
-       safe_name = php_addslashes(safe_name, 1);
+#if PHP_VERSION_ID < 70300
+       safe_name = php_addslashes(zstr_name, 1);
+#else
+       safe_name = php_addslashes(zstr_name);
+       zend_string_release_ex(zstr_name, 0);
+#endif
 
        BOUNDARY_OPEN(body);
        php_http_message_body_appendf(
@@ -284,9 +289,14 @@ ZEND_RESULT_CODE php_http_message_body_add_form_file(php_http_message_body_t *bo
 {
        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);
+       zend_string *base_name, *safe_name, *zstr_name = zend_string_init(name, strlen(name), 0);
+
+#if PHP_VERSION_ID < 70300
+       safe_name = php_addslashes(zstr_name, 1);
+#else
+       safe_name = php_addslashes(zstr_name);
+       zend_string_release_ex(zstr_name, 0);
+#endif
        base_name = php_basename(path_dup, path_len, NULL, 0);
 
        BOUNDARY_OPEN(body);
@@ -320,7 +330,7 @@ static inline char *format_key(php_http_arrkey_t *key, const char *prefix) {
        } else if (key->key) {
                new_key = estrdup(key->key->val);
        } else {
-               new_key = estrdup("");
+               spprintf(&new_key, 0, "%lu", key->h);
        }
 
        return new_key;
@@ -339,8 +349,8 @@ static ZEND_RESULT_CODE add_recursive_fields(php_http_message_body_t *body, cons
        zval *val;
        php_http_arrkey_t key;
 
-       if (!ZEND_HASH_GET_APPLY_COUNT(fields)) {
-               ZEND_HASH_INC_APPLY_COUNT(fields);
+       if (!HT_IS_RECURSIVE(fields)) {
+               HT_PROTECT_RECURSION(fields);
                ZEND_HASH_FOREACH_KEY_VAL_IND(fields, key.h, key.key, val)
                {
                        char *str = format_key(&key, name);
@@ -348,18 +358,18 @@ static ZEND_RESULT_CODE add_recursive_fields(php_http_message_body_t *body, cons
                        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);
+                                       HT_UNPROTECT_RECURSION(fields);
                                        return FAILURE;
                                }
                        } else if (SUCCESS != add_recursive_fields(body, str, HASH_OF(val))) {
                                efree(str);
-                               ZEND_HASH_DEC_APPLY_COUNT(fields);
+                               HT_UNPROTECT_RECURSION(fields);
                                return FAILURE;
                        }
                        efree(str);
                }
                ZEND_HASH_FOREACH_END();
-               ZEND_HASH_DEC_APPLY_COUNT(fields);
+               HT_UNPROTECT_RECURSION(fields);
        }
 
        return SUCCESS;
@@ -377,23 +387,26 @@ static ZEND_RESULT_CODE add_recursive_files(php_http_message_body_t *body, const
                zval *val;
                php_http_arrkey_t key;
 
-               if (!ZEND_HASH_GET_APPLY_COUNT(files)) {
-                       ZEND_HASH_INC_APPLY_COUNT(files);
+               if (!HT_IS_RECURSIVE(files)) {
+                       HT_PROTECT_RECURSION(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);
+                                       char *str = key.key ? format_key(&key, name) : NULL;
+                                       const char *prefix = str ? str : name;
 
-                                       if (SUCCESS != add_recursive_files(body, str, HASH_OF(val))) {
+                                       if (SUCCESS != add_recursive_files(body, prefix, HASH_OF(val))) {
                                                efree(str);
-                                               ZEND_HASH_DEC_APPLY_COUNT(files);
+                                               HT_UNPROTECT_RECURSION(files);
                                                return FAILURE;
                                        }
-                                       efree(str);
+                                       if (str) {
+                                               efree(str);
+                                       }
                                }
                        }
                        ZEND_HASH_FOREACH_END();
-                       ZEND_HASH_DEC_APPLY_COUNT(files);
+                       HT_UNPROTECT_RECURSION(files);
                }
                return SUCCESS;
        } else {
@@ -570,8 +583,6 @@ php_http_message_body_object_t *php_http_message_body_object_new_ex(zend_class_e
 
        if (body) {
                o->body = body;
-               php_stream_to_zval(php_http_message_body_stream(o->body), o->gc);
-
        }
 
        o->zo.handlers = &php_http_message_body_object_handlers;
@@ -581,7 +592,7 @@ php_http_message_body_object_t *php_http_message_body_object_new_ex(zend_class_e
 
 zend_object *php_http_message_body_object_clone(zval *object)
 {
-       php_http_message_body_object_t *new_obj = NULL;
+       php_http_message_body_object_t *new_obj;
        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);
 
@@ -597,12 +608,18 @@ static HashTable *php_http_message_body_object_get_gc(zval *object, zval **table
        HashTable *props = Z_OBJPROP_P(object);
        uint32_t count = zend_hash_num_elements(props);
 
-       *n = 1;
+       obj->gc = erealloc(obj->gc, (1 + count) * sizeof(zval));
+
+       if (php_http_message_body_stream(obj->body)) {
+               *n = 1;
+               php_stream_to_zval(php_http_message_body_stream(obj->body), obj->gc);
+       } else {
+               *n = 0;
+       }
+
        if (count) {
                zval *val;
 
-               obj->gc = erealloc(obj->gc, (*n + count) * sizeof(zval));
-
                ZEND_HASH_FOREACH_VAL(props, val)
                {
                        ZVAL_COPY_VALUE(&obj->gc[(*n)++], val);
@@ -893,7 +910,7 @@ PHP_METHOD(HttpMessageBody, stat)
 }
 
 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, __construct,  ai_HttpMessageBody___construct,  ZEND_ACC_PUBLIC)
        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)