+PHP_HTTP_API HashTable *php_http_params_parse(HashTable *params, const php_http_params_opts_t *opts TSRMLS_DC)
+{
+ php_http_params_state_t state = {{NULL,0}, {NULL,0}, {NULL,0}, {NULL,0}, {NULL,NULL,NULL}, 0, 0};
+
+ state.input.str = opts->input.str;
+ state.input.len = opts->input.len;
+
+ if (!params) {
+ ALLOC_HASHTABLE(params);
+ ZEND_INIT_SYMTABLE(params);
+ }
+
+ while (state.input.len) {
+ if (*state.input.str == '"' && !state.escape) {
+ state.quotes = !state.quotes;
+ } else {
+ state.escape = (*state.input.str == '\\');
+ }
+
+ if (!state.param.str) {
+ /* initialize */
+ skip_sep(0, &state, opts->param, opts->arg, opts->val TSRMLS_CC);
+ state.param.str = state.input.str;
+ } else {
+ size_t sep_len;
+ /* are we at a param separator? */
+ if (0 < (sep_len = check_sep(&state, opts->param))) {
+ push_param(params, &state, opts TSRMLS_CC);
+
+ skip_sep(sep_len, &state, opts->param, opts->arg, opts->val TSRMLS_CC);
+
+ /* start off with a new param */
+ state.param.str = state.input.str;
+ state.param.len = 0;
+ state.arg.str = NULL;
+ 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))) {
+ push_param(params, &state, opts TSRMLS_CC);
+
+ skip_sep(sep_len, &state, NULL, opts->arg, opts->val TSRMLS_CC);
+
+ /* continue with a new arg */
+ state.arg.str = state.input.str;
+ 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))) {
+ /* only handle separator if we're not already reading in a val */
+ if (!state.val.str) {
+ push_param(params, &state, opts TSRMLS_CC);
+
+ skip_sep(sep_len, &state, NULL, NULL, opts->val TSRMLS_CC);
+
+ state.val.str = state.input.str;
+ state.val.len = 0;
+
+ continue;
+ }
+ }
+ }
+
+ if (state.input.len) {
+ ++state.input.str;
+ --state.input.len;
+ }
+ }
+ /* finalize */
+ push_param(params, &state, opts TSRMLS_CC);
+
+ return params;
+}
+
+static inline void shift_key(php_http_buffer_t *buf, char *key_str, size_t key_len, const char *ass, size_t asl, unsigned flags TSRMLS_DC)
+{
+ char *str;
+ size_t len;
+
+ if (buf->used) {
+ php_http_buffer_append(buf, ass, asl);
+ }
+
+ prepare_key(flags, key_str, key_len, &str, &len TSRMLS_CC);
+ php_http_buffer_append(buf, str, len);
+ efree(str);
+}
+
+static inline void shift_val(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags TSRMLS_DC)
+{
+ if (Z_TYPE_P(zvalue) != IS_BOOL) {
+ zval *tmp = php_http_zsep(1, 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_P(zvalue)) {
+ php_http_buffer_append(buf, vss, vsl);
+ php_http_buffer_appends(buf, "0");
+ }
+}
+
+static void shift_arg(php_http_buffer_t *buf, char *key_str, size_t key_len, zval *zvalue, const char *ass, size_t asl, const char *vss, size_t vsl, unsigned flags TSRMLS_DC)
+{
+ if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) {
+ HashPosition pos;
+ php_http_array_hashkey_t key = php_http_array_hashkey_init(0);
+ zval **val;
+
+ shift_key(buf, key_str, key_len, ass, asl, flags TSRMLS_CC);
+ FOREACH_KEYVAL(pos, zvalue, key, val) {
+ /* did you mean recursion? */
+ php_http_array_hashkey_stringify(&key);
+ shift_arg(buf, key.str, key.len-1, *val, ass, asl, vss, vsl, flags TSRMLS_CC);
+ php_http_array_hashkey_stringfree(&key);
+ }
+ } else {
+ shift_key(buf, key_str, key_len, ass, asl, flags TSRMLS_CC);
+ shift_val(buf, zvalue, vss, vsl, flags TSRMLS_CC);
+ }
+}
+
+static void shift_param(php_http_buffer_t *buf, char *key_str, size_t key_len, zval *zvalue, const char *pss, size_t psl, const char *ass, size_t asl, const char *vss, size_t vsl, unsigned flags TSRMLS_DC)
+{
+ if (Z_TYPE_P(zvalue) == IS_ARRAY || Z_TYPE_P(zvalue) == IS_OBJECT) {
+ /* treat as arguments, unless we care for dimensions */
+ if (flags & PHP_HTTP_PARAMS_DIMENSION) {
+ php_http_buffer_t *keybuf = php_http_buffer_from_string(key_str, key_len);
+ prepare_dimension(buf, keybuf, zvalue, pss, psl, vss, vsl, flags TSRMLS_CC);
+ php_http_buffer_free(&keybuf);
+ } else {
+ shift_arg(buf, key_str, key_len, zvalue, ass, asl, vss, vsl, flags TSRMLS_CC);
+ }
+ } else {
+ shift_key(buf, key_str, key_len, pss, psl, flags TSRMLS_CC);
+ shift_val(buf, zvalue, vss, vsl, flags TSRMLS_CC);
+ }
+}
+
+PHP_HTTP_API php_http_buffer_t *php_http_params_to_string(php_http_buffer_t *buf, HashTable *params, const char *pss, size_t psl, const char *ass, size_t asl, const char *vss, size_t vsl, unsigned flags TSRMLS_DC)
+{
+ zval **zparam;
+ HashPosition pos, pos1;
+ php_http_array_hashkey_t key = php_http_array_hashkey_init(0), key1 = php_http_array_hashkey_init(0);
+
+ if (!buf) {
+ buf = php_http_buffer_init(NULL);
+ }
+
+ FOREACH_HASH_KEYVAL(pos, params, key, zparam) {
+ zval **zvalue, **zargs;
+
+ if (Z_TYPE_PP(zparam) != IS_ARRAY || SUCCESS != zend_hash_find(Z_ARRVAL_PP(zparam), ZEND_STRS("value"), (void *) &zvalue)) {
+ zvalue = zparam;
+ }
+
+ php_http_array_hashkey_stringify(&key);
+ shift_param(buf, key.str, key.len - 1, *zvalue, pss, psl, ass, asl, vss, vsl, flags TSRMLS_CC);
+ 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)) {
+ if (zvalue == zparam) {
+ continue;
+ }
+ zvalue = zparam;
+ }
+
+ 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;
+ }
+
+ php_http_array_hashkey_stringify(&key1);
+ shift_arg(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);
+
+ return buf;
+}
+
+PHP_HTTP_API php_http_params_token_t **php_http_params_separator_init(zval *zv TSRMLS_DC)
+{
+ zval **sep;
+ HashPosition pos;
+ php_http_params_token_t **ret, **tmp;
+
+ if (!zv) {
+ return NULL;
+ }
+
+ zv = php_http_ztyp(IS_ARRAY, zv);
+ ret = ecalloc(zend_hash_num_elements(Z_ARRVAL_P(zv)) + 1, sizeof(*ret));
+
+ tmp = ret;
+ FOREACH_VAL(pos, zv, sep) {
+ zval *zt = php_http_ztyp(IS_STRING, *sep);
+
+ if (Z_STRLEN_P(zt)) {
+ *tmp = emalloc(sizeof(**tmp));
+ (*tmp)->str = estrndup(Z_STRVAL_P(zt), (*tmp)->len = Z_STRLEN_P(zt));
+ ++tmp;
+ }
+ zval_ptr_dtor(&zt);
+ }
+ zval_ptr_dtor(&zv);
+
+ *tmp = NULL;
+ return ret;
+}
+
+PHP_HTTP_API void php_http_params_separator_free(php_http_params_token_t **separator)
+{
+ php_http_params_token_t **sep = separator;
+ if (sep) {
+ while (*sep) {
+ STR_FREE((*sep)->str);
+ efree(*sep);
+ ++sep;
+ }
+ efree(separator);
+ }
+}
+
+ZEND_BEGIN_ARG_INFO_EX(ai_HttpParams___construct, 0, 0, 0)
+ ZEND_ARG_INFO(0, params)
+ ZEND_ARG_INFO(0, param_sep)
+ ZEND_ARG_INFO(0, arg_sep)
+ ZEND_ARG_INFO(0, val_sep)
+ ZEND_ARG_INFO(0, flags)
+ZEND_END_ARG_INFO();
+PHP_METHOD(HttpParams, __construct)
+{
+ with_error_handling(EH_THROW, php_http_exception_class_entry) {
+ zval *zcopy, *zparams = NULL, *param_sep = NULL, *arg_sep = NULL, *val_sep = NULL;
+ long flags = PHP_HTTP_PARAMS_DEFAULT;
+
+ if (SUCCESS == zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|z!/z/z/z/l", &zparams, ¶m_sep, &arg_sep, &val_sep, &flags)) {
+ switch (ZEND_NUM_ARGS()) {
+ case 5:
+ zend_update_property_long(php_http_params_class_entry, getThis(), ZEND_STRL("flags"), flags TSRMLS_CC);
+ /* no break */
+ case 4:
+ zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("val_sep"), val_sep TSRMLS_CC);
+ /* no break */
+ case 3:
+ zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("arg_sep"), arg_sep TSRMLS_CC);
+ /* no break */
+ case 2:
+ zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("param_sep"), param_sep TSRMLS_CC);
+ /* no break */
+ }
+
+ if (zparams) {
+ switch (Z_TYPE_P(zparams)) {
+ case IS_OBJECT:
+ case IS_ARRAY:
+ zcopy = php_http_zsep(1, IS_ARRAY, zparams);
+ zend_update_property(php_http_params_class_entry, getThis(), ZEND_STRL("params"), zcopy TSRMLS_CC);
+ zval_ptr_dtor(&zcopy);
+ break;