#include "php_http_api.h"
-#include <ext/standard/php_lcg.h>
+#include "ext/standard/php_lcg.h"
#define BOUNDARY_OPEN(body) \
do {\
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_ADDREF(body->res);
} else {
body->res = php_stream_temp_create(TEMP_STREAM_DEFAULT, 0xffff)->res;
}
- ++GC_REFCOUNT(body->res);
+ php_stream_auto_cleanup(php_http_message_body_stream(body));
if (body_ptr) {
*body_ptr = body;
{
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);
}
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;
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(
{
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);
} else if (key->key) {
new_key = estrdup(key->key->val);
} else {
- new_key = estrdup("");
+ spprintf(&new_key, 0, "%lu", key->h);
}
return new_key;
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);
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;
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 ?: 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 {
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;
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);
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);
}
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)