X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=php_http_params.c;h=da78303d3e0affa311b0d3444ad0fa8b1a15b27e;hb=729594cae203b09419d195ec0dabde0430c9f1cd;hp=bea53a652f9cc3f6d6908add53f5e049b51a7232;hpb=4b368d86db1aae978d41a8ec0350f3ba340739d8;p=m6w6%2Fext-http diff --git a/php_http_params.c b/php_http_params.c index bea53a6..da78303 100644 --- a/php_http_params.c +++ b/php_http_params.c @@ -16,11 +16,12 @@ static php_http_params_token_t def_param_sep = {",", 1}, *def_param_sep_ptr[] = static php_http_params_token_t def_arg_sep = {";", 1}, *def_arg_sep_ptr[] = {&def_arg_sep, NULL}; static php_http_params_token_t def_val_sep = {"=", 1}, *def_val_sep_ptr[] = {&def_val_sep, NULL}; static php_http_params_opts_t def_opts = { - .param = def_param_sep_ptr, - .arg = def_arg_sep_ptr, - .val = def_val_sep_ptr, - .defval = NULL, - .flags = PHP_HTTP_PARAMS_DEFAULT + {{0},0}, + def_param_sep_ptr, + def_arg_sep_ptr, + def_val_sep_ptr, + NULL, + PHP_HTTP_PARAMS_DEFAULT }; PHP_HTTP_API php_http_params_opts_t *php_http_params_opts_default_get(php_http_params_opts_t *opts) @@ -146,7 +147,12 @@ static void sanitize_dimension(zval *zv TSRMLS_DC) if (zend_hash_num_elements(Z_ARRVAL_P(arr))) { zval_dtor(zv); +#if PHP_VERSION_ID >= 50400 ZVAL_COPY_VALUE(zv, arr); +#else + zv->value = arr->value; + Z_TYPE_P(zv) = Z_TYPE_P(arr); +#endif FREE_ZVAL(arr); } else { zval_ptr_dtor(&arr); @@ -244,7 +250,7 @@ static void prepare_value(unsigned flags, zval *zv TSRMLS_DC) } } -static void merge_param(HashTable *params, zval *zdata, zval ***cur TSRMLS_DC) +static void merge_param(HashTable *params, zval *zdata, zval ***current_param, zval ***current_args TSRMLS_DC) { zval **ptr, **zdata_ptr; php_http_array_hashkey_t hkey = php_http_array_hashkey_init(0); @@ -264,7 +270,7 @@ static void merge_param(HashTable *params, zval *zdata, zval ***cur TSRMLS_DC) if ((hkey.type == HASH_KEY_IS_STRING && !zend_hash_exists(params, hkey.str, hkey.len)) || (hkey.type == HASH_KEY_IS_LONG && !zend_hash_index_exists(params, hkey.num)) ) { - zval *tmp; + zval *tmp, *arg, **args; /* create the entry if it doesn't exist */ zend_hash_get_current_data(Z_ARRVAL_P(zdata), (void *) &ptr); @@ -273,6 +279,11 @@ static void merge_param(HashTable *params, zval *zdata, zval ***cur TSRMLS_DC) array_init(tmp); add_assoc_zval_ex(tmp, ZEND_STRS("value"), *ptr); + MAKE_STD_ZVAL(arg); + array_init(arg); + zend_hash_update(Z_ARRVAL_P(tmp), "arguments", sizeof("arguments"), (void *) &arg, sizeof(zval *), (void *) &args); + *current_args = args; + if (hkey.type == HASH_KEY_IS_STRING) { zend_hash_update(params, hkey.str, hkey.len, (void *) &tmp, sizeof(zval *), (void *) &ptr); } else { @@ -350,7 +361,7 @@ static void merge_param(HashTable *params, zval *zdata, zval ***cur TSRMLS_DC) /* bubble up */ while (Z_TYPE_PP(ptr) == IS_ARRAY && SUCCESS == zend_hash_get_current_data(Z_ARRVAL_PP(ptr), (void *) &ptr)); - *cur = ptr; + *current_param = ptr; } static void push_param(HashTable *params, php_http_params_state_t *state, const php_http_params_opts_t *opts TSRMLS_DC) @@ -380,14 +391,19 @@ static void push_param(HashTable *params, php_http_params_state_t *state, const ZVAL_NULL(key); sanitize_key(opts->flags, state->param.str, state->param.len, key TSRMLS_CC); if (Z_TYPE_P(key) != IS_STRING) { - merge_param(params, key, &state->current.val TSRMLS_CC); + merge_param(params, key, &state->current.val, &state->current.args TSRMLS_CC); } else if (Z_STRLEN_P(key)) { MAKE_STD_ZVAL(prm); array_init(prm); MAKE_STD_ZVAL(val); if (opts->defval) { +#if PHP_VERSION_ID >= 50400 ZVAL_COPY_VALUE(val, opts->defval); +#else + val->value = opts->defval->value; + Z_TYPE_P(val) = Z_TYPE_P(opts->defval); +#endif zval_copy_ctor(val); } else { ZVAL_TRUE(val); @@ -473,6 +489,9 @@ PHP_HTTP_API HashTable *php_http_params_parse(HashTable *params, const php_http_ state.arg.len = 0; state.val.str = NULL; state.val.len = 0; + + continue; + } else /* are we at an arg separator? */ if (0 < (sep_len = check_sep(&state, opts->arg))) { @@ -485,6 +504,9 @@ PHP_HTTP_API HashTable *php_http_params_parse(HashTable *params, const php_http_ state.arg.len = 0; state.val.str = NULL; state.val.len = 0; + + continue; + } else /* are we at a val separator? */ if (0 < (sep_len = check_sep(&state, opts->val))) { @@ -496,6 +518,8 @@ PHP_HTTP_API HashTable *php_http_params_parse(HashTable *params, const php_http_ state.val.str = state.input.str; state.val.len = 0; + + continue; } } } @@ -601,142 +625,25 @@ PHP_HTTP_API php_http_buffer_t *php_http_params_to_string(php_http_buffer_t *buf php_http_array_hashkey_stringfree(&key); if (Z_TYPE_PP(zparam) == IS_ARRAY && SUCCESS != zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("arguments"), (void *) &zvalue)) { - zvalue = zparam; - } - - FOREACH_KEYVAL(pos1, *zvalue, key1, zargs) { - if (zvalue == zparam && key1.type == HASH_KEY_IS_STRING && !strcmp(key1.str, "value")) { + if (zvalue == zparam) { continue; } - - php_http_array_hashkey_stringify(&key1); - shift_param(buf, key1.str, key1.len - 1, zargs, ass, asl, vss, vsl, flags TSRMLS_CC); - php_http_array_hashkey_stringfree(&key1); - } - } -/* - FOREACH_HASH_KEYVAL(pos1, params, key1, zparam) { - if (PHP_HTTP_BUFFER_LEN(buf)) { - php_http_buffer_append(buf, pss, psl); - } - - if (key1.type == HASH_KEY_IS_STRING) { - char *key; - size_t len; - - prepare_key(flags, key1.str, key1.len - 1, &key, &len TSRMLS_CC); - php_http_buffer_append(buf, key, len); - efree(key); - } else { - php_http_buffer_appendf(buf, "%lu", key1.num); + zvalue = zparam; } - if (Z_TYPE_PP(zparam) != IS_ARRAY) { - zval *tmp = php_http_ztyp(IS_STRING, *zparam); - - prepare_value(flags, tmp TSRMLS_CC); - - php_http_buffer_append(buf, vss, vsl); - php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); - zval_ptr_dtor(&tmp); - } else { - zval **zvalue, **zargs, **zarg; - - if (SUCCESS == zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("value"), (void *) &zvalue)) { - if (Z_TYPE_PP(zvalue) != IS_BOOL) { - zval *tmp, *tmp2; - - if (Z_TYPE_PP(zvalue) == IS_ARRAY) { - tmp = php_http_zsep(1, IS_ARRAY, *zvalue); - do { - if (key1.type == HASH_KEY_IS_STRING) { - char *key; - size_t len; - - prepare_key(flags, key1.str, key1.len - 1, &key, &len TSRMLS_CC); - php_http_buffer_append(buf, key, len); - efree(key); - } else { - php_http_buffer_appendf(buf, "%lu", key1.num); - } - - tmp2 = php_http_zsep(1, IS_ARRAY, tmp); - prepare_value(flags, tmp2 TSRMLS_CC); - php_http_buffer_append(buf, Z_STRVAL_P(tmp2), Z_STRLEN_P(tmp2)); - zval_ptr_dtor(&tmp2); - - while (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_P(tmp), (void *) &zvalue) && Z_TYPE_PP(zvalue) == IS_ARRAY); - - tmp2 = php_http_ztyp(IS_STRING, *zvalue); - prepare_value(flags, tmp2 TSRMLS_CC); - php_http_buffer_append(buf, vss, vsl); - php_http_buffer_append(buf, Z_STRVAL_P(tmp2), Z_STRLEN_P(tmp2)); - zval_ptr_dtor(&tmp2); - } while (SUCCESS == zend_hash_move_forward(Z_ARRVAL_P(tmp))); - zval_ptr_dtor(&tmp); - } else { - - tmp = php_http_ztyp(IS_STRING, *zvalue); - prepare_value(flags, tmp TSRMLS_CC); - php_http_buffer_append(buf, vss, vsl); - php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); - zval_ptr_dtor(&tmp); - } - } else if (!Z_BVAL_PP(zvalue)) { - php_http_buffer_append(buf, vss, vsl); - php_http_buffer_appends(buf, "0"); + if (Z_TYPE_PP(zvalue) == IS_ARRAY) { + FOREACH_KEYVAL(pos1, *zvalue, key1, zargs) { + if (zvalue == zparam && key1.type == HASH_KEY_IS_STRING && !strcmp(key1.str, "value")) { + continue; } - } - if (SUCCESS != zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("arguments"), (void *) &zargs)) { - zargs = zparam; - } - - if (Z_TYPE_PP(zargs) == IS_ARRAY) { - FOREACH_KEYVAL(pos2, *zargs, key2, zarg) { - if (zargs == zparam && key2.type == HASH_KEY_IS_STRING && !strcmp(key2.str, "value")) { - continue; - } - if (PHP_HTTP_BUFFER_LEN(buf)) { - php_http_buffer_append(buf, ass, asl); - } - - if (key2.type == HASH_KEY_IS_STRING) { - char *key; - size_t len; - - prepare_key(flags, key2.str, key2.len - 1, &key, &len TSRMLS_CC); - php_http_buffer_append(buf, key, len); - efree(key); - } else { - php_http_buffer_appendf(buf, "%lu", key2.num); - } - if (Z_TYPE_PP(zarg) != IS_BOOL) { - zval *tmp; - - if (Z_TYPE_PP(zarg) == IS_ARRAY) { - tmp = php_http_zsep(1, IS_ARRAY, *zarg); - prepare_value(flags, tmp TSRMLS_CC); - php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); - zval_ptr_dtor(&tmp); - - while (SUCCESS == zend_hash_get_current_data(Z_ARRVAL_PP(zarg), (void *) &zarg) && Z_TYPE_PP(zarg) == IS_ARRAY); - } - - tmp = php_http_ztyp(IS_STRING, *zarg); - prepare_value(flags, tmp TSRMLS_CC); - php_http_buffer_append(buf, vss, vsl); - php_http_buffer_append(buf, Z_STRVAL_P(tmp), Z_STRLEN_P(tmp)); - zval_ptr_dtor(&tmp); - } else if (!Z_BVAL_PP(zarg)) { - php_http_buffer_append(buf, vss, vsl); - php_http_buffer_appends(buf, "0"); - } - } + php_http_array_hashkey_stringify(&key1); + shift_param(buf, key1.str, key1.len - 1, zargs, ass, asl, vss, vsl, flags TSRMLS_CC); + php_http_array_hashkey_stringfree(&key1); } } } -*/ + php_http_buffer_shrink(buf); php_http_buffer_fix(buf); @@ -901,14 +808,11 @@ PHP_METHOD(HttpParams, __construct) zcopy = php_http_ztyp(IS_STRING, zparams); if (Z_STRLEN_P(zcopy)) { php_http_params_opts_t opts = { - .input = { - .str = Z_STRVAL_P(zcopy), - .len = Z_STRLEN_P(zcopy) - }, - .param = php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), 0 TSRMLS_CC) TSRMLS_CC), - .arg = php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), 0 TSRMLS_CC) TSRMLS_CC), - .val = php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), 0 TSRMLS_CC) TSRMLS_CC), - .flags = flags + {Z_STRVAL_P(zcopy), Z_STRLEN_P(zcopy)}, + php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), 0 TSRMLS_CC) TSRMLS_CC), + php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), 0 TSRMLS_CC) TSRMLS_CC), + php_http_params_separator_init(zend_read_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), 0 TSRMLS_CC) TSRMLS_CC), + NULL, flags }; MAKE_STD_ZVAL(zparams);