X-Git-Url: https://git.m6w6.name/?p=m6w6%2Fext-http;a=blobdiff_plain;f=src%2Fphp_http_querystring.c;h=391bccf675f0534d36f079b2bb75b892a1434f9c;hp=ea84d8d1e6fed8a94a74113144218892a2849074;hb=82923890d9ce44a0642ac2f5ea0cb343aaf50fe3;hpb=53a654ed6e112b2766b41c01a1c2bb79373c59e4 diff --git a/src/php_http_querystring.c b/src/php_http_querystring.c index ea84d8d..391bccf 100644 --- a/src/php_http_querystring.c +++ b/src/php_http_querystring.c @@ -12,15 +12,15 @@ #include "php_http_api.h" -#include -#include +#include "php_variables.h" +#include "ext/spl/spl_array.h" -#ifdef PHP_HTTP_HAVE_ICONV +#if PHP_HTTP_HAVE_ICONV # ifndef HAVE_ICONV # define HAVE_ICONV 1 # endif # undef PHP_ATOM_INC -# include +# include "ext/iconv/php_iconv.h" #endif static zend_class_entry *php_http_querystring_class_entry; @@ -63,7 +63,7 @@ static inline void php_http_querystring_str(zval *instance, zval *return_value) } } -static inline void php_http_querystring_get(zval *instance, int type, char *name, uint name_len, zval *defval, zend_bool del, zval *return_value) +static inline void php_http_querystring_get(zval *instance, int type, char *name, uint32_t name_len, zval *defval, zend_bool del, zval *return_value) { zval *arrval, qarray_tmp, *qarray = zend_read_property(php_http_querystring_class_entry, instance, ZEND_STRL("queryArray"), 0, &qarray_tmp); @@ -92,7 +92,7 @@ static inline void php_http_querystring_get(zval *instance, int type, char *name } } -#ifdef PHP_HTTP_HAVE_ICONV +#if PHP_HTTP_HAVE_ICONV ZEND_RESULT_CODE php_http_querystring_xlate(zval *dst, zval *src, const char *ie, const char *oe) { zval *entry; @@ -103,7 +103,7 @@ ZEND_RESULT_CODE php_http_querystring_xlate(zval *dst, zval *src, const char *ie { if (key.key) { if (PHP_ICONV_ERR_SUCCESS != php_iconv_string(key.key->val, key.key->len, &xkey, oe, ie)) { - php_error_docref(NULL, E_WARNING, "Failed to convert '%.*s' from '%s' to '%s'", key.key->len, key.key->val, ie, oe); + php_error_docref(NULL, E_WARNING, "Failed to convert '%.*s' from '%s' to '%s'", (int) key.key->len, key.key->val, ie, oe); return FAILURE; } } @@ -113,7 +113,7 @@ ZEND_RESULT_CODE php_http_querystring_xlate(zval *dst, zval *src, const char *ie if (key.key) { zend_string_release(xkey); } - php_error_docref(NULL, E_WARNING, "Failed to convert '%.*s' from '%s' to '%s'", Z_STRLEN_P(entry), Z_STRVAL_P(entry), ie, oe); + php_error_docref(NULL, E_WARNING, "Failed to convert '%.*s' from '%s' to '%s'", (int) Z_STRLEN_P(entry), Z_STRVAL_P(entry), ie, oe); return FAILURE; } if (key.key) { @@ -160,7 +160,7 @@ static int apply_querystring(zval *val) zval *zvalue; if ((zvalue = zend_hash_str_find(Z_ARRVAL_P(val), ZEND_STRL("value")))) { - zval tmp; + zval tmp = {0}; ZVAL_COPY(&tmp, zvalue); zval_dtor(val); @@ -171,6 +171,25 @@ static int apply_querystring(zval *val) return ZEND_HASH_APPLY_KEEP; } +static int apply_querystring_filter(zval *val) +{ + switch (Z_TYPE_P(val)) { + case IS_NULL: + return ZEND_HASH_APPLY_REMOVE; + case IS_ARRAY: + case IS_OBJECT: + zend_hash_apply(HASH_OF(val), apply_querystring_filter); + if (!zend_hash_num_elements(HASH_OF(val))) { + return ZEND_HASH_APPLY_REMOVE; + } + break; + default: + break; + } + + return ZEND_HASH_APPLY_KEEP; +} + ZEND_RESULT_CODE php_http_querystring_parse(HashTable *ht, const char *str, size_t len) { ZEND_RESULT_CODE rv = FAILURE; @@ -200,7 +219,7 @@ ZEND_RESULT_CODE php_http_querystring_parse(HashTable *ht, const char *str, size zval_ptr_dtor(&arr); } - ZVAL_NULL(&opts.defval); + ZVAL_TRUE(&opts.defval); if (php_http_params_parse(ht, &opts)) { zend_hash_apply(ht, apply_querystring); @@ -224,7 +243,9 @@ ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *o } /* modify qarray */ - if (params) { + if (!params) { + zend_hash_apply(Z_ARRVAL_P(qarray), apply_querystring_filter); + } else { HashTable *ht; php_http_arrkey_t key; zval zv, *params_entry, *qarray_entry; @@ -268,25 +289,24 @@ ZEND_RESULT_CODE php_http_querystring_update(zval *qarray, zval *params, zval *o /* * update */ - zval equal, tmp, *entry = &tmp; + zval equal, tmp, *entry = NULL; ZVAL_UNDEF(&tmp); /* recursive */ if (Z_TYPE_P(params_entry) == IS_ARRAY || Z_TYPE_P(params_entry) == IS_OBJECT) { - ZVAL_DUP(entry, qarray_entry); - convert_to_array(entry); - php_http_querystring_update(entry, params_entry, NULL); - } else if ((FAILURE == is_equal_function(&equal, qarray_entry, params_entry)) || Z_TYPE(equal) != IS_TRUE) { + ZVAL_DUP(&tmp, qarray_entry); + convert_to_array(&tmp); + php_http_querystring_update(&tmp, params_entry, NULL); + entry = &tmp; + } else if ((FAILURE == is_identical_function(&equal, qarray_entry, params_entry)) || Z_TYPE(equal) != IS_TRUE) { Z_TRY_ADDREF_P(params_entry); entry = params_entry; } - if (entry) { - if (key.key) { - zend_hash_update(Z_ARRVAL_P(qarray), key.key, entry); - } else { - zend_hash_index_update(Z_ARRVAL_P(qarray), key.h, entry); - } + if (key.key) { + zend_hash_update(Z_ARRVAL_P(qarray), key.key, entry ? entry : &tmp); + } else { + zend_hash_index_update(Z_ARRVAL_P(qarray), key.h, entry ? entry : &tmp); } } else { zval entry, *entry_ptr = &entry; @@ -350,25 +370,28 @@ ZEND_END_ARG_INFO(); PHP_METHOD(HttpQueryString, getGlobalInstance) { zval *instance, *_GET; - zend_string *zs; php_http_expect(SUCCESS == zend_parse_parameters_none(), invalid_arg, return); - zs = zend_string_init(ZEND_STRL("instance"), 0); - instance = zend_std_get_static_property(php_http_querystring_class_entry, zs, 0); - zend_string_release(zs); + instance = zend_read_static_property(php_http_querystring_class_entry, ZEND_STRL("instance"), 0); if (Z_TYPE_P(instance) == IS_OBJECT) { RETVAL_ZVAL(instance, 1, 0); } else if ((_GET = php_http_env_get_superglobal(ZEND_STRL("_GET")))) { + zval tmp, *qa; + ZVAL_OBJ(return_value, php_http_querystring_object_new(php_http_querystring_class_entry)); - ZVAL_MAKE_REF(_GET); - zend_update_property(php_http_querystring_class_entry, return_value, ZEND_STRL("queryArray"), _GET); + ZVAL_STRING(&tmp, "queryArray"); + qa = Z_OBJ_HT_P(return_value)->get_property_ptr_ptr(return_value, &tmp, BP_VAR_RW, NULL); + zval_ptr_dtor(&tmp); + + ZVAL_NEW_REF(_GET, _GET); + ZVAL_COPY(qa, _GET); zend_update_static_property(php_http_querystring_class_entry, ZEND_STRL("instance"), return_value); } else { - php_http_throw(unexpected_val, "Could not acquire reference to superglobal GET array", NULL); + php_http_throw(unexpected_val, "Could not acquire reference to superglobal GET array"); } } @@ -514,7 +537,7 @@ PHP_HTTP_QUERYSTRING_GETTER(getString, IS_STRING); PHP_HTTP_QUERYSTRING_GETTER(getArray, IS_ARRAY); PHP_HTTP_QUERYSTRING_GETTER(getObject, IS_OBJECT); -#ifdef PHP_HTTP_HAVE_ICONV +#if PHP_HTTP_HAVE_ICONV ZEND_BEGIN_ARG_INFO_EX(ai_HttpQueryString_xlate, 0, 0, 2) ZEND_ARG_INFO(0, from_encoding) ZEND_ARG_INFO(0, to_encoding) @@ -662,7 +685,7 @@ PHP_METHOD(HttpQueryString, offsetUnset) } static zend_function_entry php_http_querystring_methods[] = { - PHP_ME(HttpQueryString, __construct, ai_HttpQueryString___construct, ZEND_ACC_PUBLIC|ZEND_ACC_CTOR|ZEND_ACC_FINAL) + PHP_ME(HttpQueryString, __construct, ai_HttpQueryString___construct, ZEND_ACC_PUBLIC|ZEND_ACC_FINAL) PHP_ME(HttpQueryString, toArray, ai_HttpQueryString_toArray, ZEND_ACC_PUBLIC) PHP_ME(HttpQueryString, toString, ai_HttpQueryString_toString, ZEND_ACC_PUBLIC) @@ -682,7 +705,7 @@ static zend_function_entry php_http_querystring_methods[] = { PHP_ME(HttpQueryString, getIterator, ai_HttpQueryString_getIterator, ZEND_ACC_PUBLIC) PHP_ME(HttpQueryString, getGlobalInstance, ai_HttpQueryString_getGlobalInstance, ZEND_ACC_PUBLIC|ZEND_ACC_STATIC) -#ifdef PHP_HTTP_HAVE_ICONV +#if PHP_HTTP_HAVE_ICONV PHP_ME(HttpQueryString, xlate, ai_HttpQueryString_xlate, ZEND_ACC_PUBLIC) #endif