Fixed issue #63
[m6w6/ext-http] / src / php_http_params.c
index c9feccb6c56705e63088ded27a3a0f716acb7969..9b40cfefdc767dafc1945213db20fdd45c17001a 100644 (file)
@@ -63,24 +63,26 @@ static inline void sanitize_escaped(zval *zv)
        php_stripcslashes(Z_STR_P(zv));
 }
 
-static inline void quote_string(zend_string **zs, zend_bool force)
+static inline zend_string *quote_string(zend_string *zs, zend_bool force)
 {
-       int len = (*zs)->len;
+       size_t len = (zs)->len;
 
-       *zs = php_addcslashes(*zs, 1, ZEND_STRL("\0..\37\173\\\""));
+       zs = php_addcslashes(zs, 0, ZEND_STRL("\0..\37\173\\\""));
 
-       if (force || len != (*zs)->len || strpbrk((*zs)->val, "()<>@,;:\"[]?={} ")) {
-               int len = (*zs)->len + 2;
+       if (force || len != (zs)->len || strpbrk((zs)->val, "()<>@,;:\"[]?={} ")) {
+               int len = (zs)->len + 2;
 
-               *zs = zend_string_extend(*zs, len, 0);
+               zs = zend_string_extend(zs, len, 0);
 
-               memmove(&(*zs)->val[1], (*zs)->val, (*zs)->len);
-               (*zs)->val[0] = '"';
-               (*zs)->val[len-1] = '"';
-               (*zs)->val[len] = '\0';
+               memmove(&(zs)->val[1], (zs)->val, (zs)->len);
+               (zs)->val[0] = '"';
+               (zs)->val[len-1] = '"';
+               (zs)->val[len] = '\0';
 
-               zend_string_forget_hash_val(*zs);
+               zend_string_forget_hash_val(zs);
        }
+
+       return zs;
 }
 
 /*     if (Z_TYPE_P(zv) == IS_STRING) {
@@ -109,7 +111,10 @@ static inline void quote_string(zend_string **zs, zend_bool force)
 static inline void prepare_escaped(zval *zv)
 {
        if (Z_TYPE_P(zv) == IS_STRING) {
-               quote_string(&Z_STR_P(zv), 0);
+               zend_string *str = quote_string(Z_STR_P(zv), 0);
+
+               zval_dtor(zv);
+               ZVAL_STR(zv, str);
        } else {
                zval_dtor(zv);
                ZVAL_EMPTY_STRING(zv);
@@ -118,7 +123,7 @@ static inline void prepare_escaped(zval *zv)
 
 static inline void sanitize_urlencoded(zval *zv)
 {
-       Z_STRLEN_P(zv) = php_raw_url_decode(Z_STRVAL_P(zv), Z_STRLEN_P(zv));
+       Z_STRLEN_P(zv) = php_url_decode(Z_STRVAL_P(zv), Z_STRLEN_P(zv));
 }
 
 static inline void prepare_urlencoded(zval *zv)
@@ -248,7 +253,7 @@ static inline void sanitize_key(unsigned flags, const char *str, size_t len, zva
        if (flags & PHP_HTTP_PARAMS_ESCAPED) {
                sanitize_escaped(zv);
        }
-       
+
        if (!Z_STRLEN_P(zv)) {
                return;
        }
@@ -489,7 +494,7 @@ static void merge_param(HashTable *params, zval *zdata, zval **current_param, zv
                        zval *test_ptr;
 
                        while (Z_TYPE_P(zdata_ptr) == IS_ARRAY && (test_ptr = zend_hash_get_current_data(Z_ARRVAL_P(zdata_ptr)))) {
-                               if (Z_TYPE_P(test_ptr) == IS_ARRAY) {
+                               if (Z_TYPE_P(test_ptr) == IS_ARRAY && Z_TYPE_P(ptr) == IS_ARRAY) {
                                        zval *tmp_ptr = ptr;
 
                                        /* now find key in ptr */
@@ -554,7 +559,9 @@ static void merge_param(HashTable *params, zval *zdata, zval **current_param, zv
 static void push_param(HashTable *params, php_http_params_state_t *state, const php_http_params_opts_t *opts)
 {
        if (state->val.str) {
-               if (0 < (state->val.len = state->input.str - state->val.str)) {
+               if (!state->current.val) {
+                       return;
+               } else if (0 < (state->val.len = state->input.str - state->val.str)) {
                        sanitize_value(opts->flags, state->val.str, state->val.len, state->current.val, state->rfc5987);
                } else {
                        ZVAL_EMPTY_STRING(state->current.val);
@@ -639,7 +646,7 @@ static size_t check_sep(php_http_params_state_t *state, php_http_params_token_t
        if (state->quotes || state->escape) {
                return 0;
        }
-       
+
        if (sep) while (*sep) {
                if (check_str(state->input.str, state->input.len, (*sep)->str, (*sep)->len)) {
                        return (*sep)->len;
@@ -689,7 +696,7 @@ HashTable *php_http_params_parse(HashTable *params, const php_http_params_opts_t
                } else {
                        state.escape = (*state.input.str == '\\');
                }
-               
+
                if (!state.param.str) {
                        /* initialize */
                        skip_sep(0, &state, opts->param, opts->arg, opts->val);
@@ -743,7 +750,7 @@ HashTable *php_http_params_parse(HashTable *params, const php_http_params_opts_t
                                }
                        }
                }
-               
+
                if (state.input.len) {
                        ++state.input.str;
                        --state.input.len;
@@ -814,13 +821,14 @@ static inline void shift_rfc5988(php_http_buffer_t *buf, char *key_str, size_t k
 
 static inline void shift_rfc5988_val(php_http_buffer_t *buf, zval *zv, const char *vss, size_t vsl, unsigned flags)
 {
-       zend_string *zs = zval_get_string(zv);
-
-       quote_string(&zs, 1);
-       php_http_buffer_append(buf, vss, vsl);
-       php_http_buffer_append(buf, zs->val, zs->len);
+       zend_string *str, *zs = zval_get_string(zv);
 
+       str = quote_string(zs, 1);
        zend_string_release(zs);
+
+       php_http_buffer_append(buf, vss, vsl);
+       php_http_buffer_append(buf, str->val, str->len);
+       zend_string_release(str);
 }
 
 static inline void shift_val(php_http_buffer_t *buf, zval *zvalue, const char *vss, size_t vsl, unsigned flags)