|
20 | 20 |
|
21 | 21 | #define URL_DEFAULT_ARG_SEP "&"
|
22 | 22 |
|
| 23 | +static void php_url_encode_scalar(zval *scalar, smart_str *form_str, |
| 24 | + int encoding_type, zend_ulong index_int, |
| 25 | + const char *index_string, size_t index_string_len, |
| 26 | + const char *num_prefix, size_t num_prefix_len, |
| 27 | + const char *key_prefix, size_t key_prefix_len, |
| 28 | + const char *key_suffix, size_t key_suffix_len, |
| 29 | + const char *arg_sep, size_t arg_sep_len) |
| 30 | +{ |
| 31 | + if (form_str->s) { |
| 32 | + smart_str_appendl(form_str, arg_sep, arg_sep_len); |
| 33 | + } |
| 34 | + /* Simple key=value */ |
| 35 | + if (key_prefix) { |
| 36 | + smart_str_appendl(form_str, key_prefix, key_prefix_len); |
| 37 | + } |
| 38 | + if (index_string) { |
| 39 | + zend_string *encoded_key; |
| 40 | + if (encoding_type == PHP_QUERY_RFC3986) { |
| 41 | + encoded_key = php_raw_url_encode(index_string, index_string_len); |
| 42 | + } else { |
| 43 | + encoded_key = php_url_encode(index_string, index_string_len); |
| 44 | + } |
| 45 | + smart_str_append(form_str, encoded_key); |
| 46 | + zend_string_free(encoded_key); |
| 47 | + } else { |
| 48 | + /* Numeric key */ |
| 49 | + if (num_prefix) { |
| 50 | + smart_str_appendl(form_str, num_prefix, num_prefix_len); |
| 51 | + } |
| 52 | + smart_str_append_long(form_str, index_int); |
| 53 | + } |
| 54 | + if (key_suffix) { |
| 55 | + smart_str_appendl(form_str, key_suffix, key_suffix_len); |
| 56 | + } |
| 57 | + smart_str_appendc(form_str, '='); |
| 58 | + |
| 59 | + switch (Z_TYPE_P(scalar)) { |
| 60 | + case IS_STRING: { |
| 61 | + zend_string *encoded_data; |
| 62 | + if (encoding_type == PHP_QUERY_RFC3986) { |
| 63 | + encoded_data = php_raw_url_encode(Z_STRVAL_P(scalar), Z_STRLEN_P(scalar)); |
| 64 | + } else { |
| 65 | + encoded_data = php_url_encode(Z_STRVAL_P(scalar), Z_STRLEN_P(scalar)); |
| 66 | + } |
| 67 | + smart_str_append(form_str, encoded_data); |
| 68 | + zend_string_free(encoded_data); |
| 69 | + break; |
| 70 | + } |
| 71 | + case IS_LONG: |
| 72 | + smart_str_append_long(form_str, Z_LVAL_P(scalar)); |
| 73 | + break; |
| 74 | + case IS_DOUBLE: { |
| 75 | + zend_string *encoded_data; |
| 76 | + zend_string *tmp = zend_double_to_str(Z_DVAL_P(scalar)); |
| 77 | + if (encoding_type == PHP_QUERY_RFC3986) { |
| 78 | + encoded_data = php_raw_url_encode(ZSTR_VAL(tmp), ZSTR_LEN(tmp)); |
| 79 | + } else { |
| 80 | + encoded_data = php_url_encode(ZSTR_VAL(tmp), ZSTR_LEN(tmp)); |
| 81 | + } |
| 82 | + smart_str_append(form_str, encoded_data); |
| 83 | + zend_string_free(tmp); |
| 84 | + zend_string_free(encoded_data); |
| 85 | + break; |
| 86 | + } |
| 87 | + case IS_FALSE: |
| 88 | + smart_str_appendc(form_str, '0'); |
| 89 | + break; |
| 90 | + case IS_TRUE: |
| 91 | + smart_str_appendc(form_str, '1'); |
| 92 | + break; |
| 93 | + /* All possible types are either handled here or previously */ |
| 94 | + EMPTY_SWITCH_DEFAULT_CASE(); |
| 95 | + } |
| 96 | +} |
| 97 | + |
23 | 98 | /* {{{ php_url_encode_hash */
|
24 | 99 | PHPAPI void php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
|
25 | 100 | const char *num_prefix, size_t num_prefix_len,
|
@@ -151,70 +226,13 @@ PHPAPI void php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
|
151 | 226 | /* Skip these types */
|
152 | 227 | continue;
|
153 | 228 | } else {
|
154 |
| - if (formstr->s) { |
155 |
| - smart_str_appendl(formstr, arg_sep, arg_sep_len); |
156 |
| - } |
157 |
| - /* Simple key=value */ |
158 |
| - if (key_prefix) { |
159 |
| - smart_str_appendl(formstr, key_prefix, key_prefix_len); |
160 |
| - } |
161 |
| - if (key) { |
162 |
| - zend_string *ekey; |
163 |
| - if (enc_type == PHP_QUERY_RFC3986) { |
164 |
| - ekey = php_raw_url_encode(prop_name, prop_len); |
165 |
| - } else { |
166 |
| - ekey = php_url_encode(prop_name, prop_len); |
167 |
| - } |
168 |
| - smart_str_append(formstr, ekey); |
169 |
| - zend_string_free(ekey); |
170 |
| - } else { |
171 |
| - /* Numeric key */ |
172 |
| - if (num_prefix) { |
173 |
| - smart_str_appendl(formstr, num_prefix, num_prefix_len); |
174 |
| - } |
175 |
| - smart_str_append_long(formstr, idx); |
176 |
| - } |
177 |
| - if (key_suffix) { |
178 |
| - smart_str_appendl(formstr, key_suffix, key_suffix_len); |
179 |
| - } |
180 |
| - smart_str_appendl(formstr, "=", 1); |
181 |
| - switch (Z_TYPE_P(zdata)) { |
182 |
| - case IS_STRING: { |
183 |
| - zend_string *ekey; |
184 |
| - if (enc_type == PHP_QUERY_RFC3986) { |
185 |
| - ekey = php_raw_url_encode(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)); |
186 |
| - } else { |
187 |
| - ekey = php_url_encode(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata)); |
188 |
| - } |
189 |
| - smart_str_append(formstr, ekey); |
190 |
| - zend_string_free(ekey); |
191 |
| - } |
192 |
| - break; |
193 |
| - case IS_LONG: |
194 |
| - smart_str_append_long(formstr, Z_LVAL_P(zdata)); |
195 |
| - break; |
196 |
| - case IS_DOUBLE: { |
197 |
| - zend_string *ekey; |
198 |
| - zend_string *tmp = zend_double_to_str(Z_DVAL_P(zdata)); |
199 |
| - if (enc_type == PHP_QUERY_RFC3986) { |
200 |
| - ekey = php_raw_url_encode(ZSTR_VAL(tmp), ZSTR_LEN(tmp)); |
201 |
| - } else { |
202 |
| - ekey = php_url_encode(ZSTR_VAL(tmp), ZSTR_LEN(tmp)); |
203 |
| - } |
204 |
| - smart_str_append(formstr, ekey); |
205 |
| - zend_string_free(tmp); |
206 |
| - zend_string_free(ekey); |
207 |
| - break; |
208 |
| - } |
209 |
| - case IS_FALSE: |
210 |
| - smart_str_appendl(formstr, "0", sizeof("0")-1); |
211 |
| - break; |
212 |
| - case IS_TRUE: |
213 |
| - smart_str_appendl(formstr, "1", sizeof("1")-1); |
214 |
| - break; |
215 |
| - /* All possible types are either handled here or previously */ |
216 |
| - EMPTY_SWITCH_DEFAULT_CASE(); |
217 |
| - } |
| 229 | + php_url_encode_scalar(zdata, formstr, |
| 230 | + enc_type, idx, |
| 231 | + prop_name, prop_len, |
| 232 | + num_prefix, num_prefix_len, |
| 233 | + key_prefix, key_prefix_len, |
| 234 | + key_suffix, key_suffix_len, |
| 235 | + arg_sep, arg_sep_len); |
218 | 236 | }
|
219 | 237 | } ZEND_HASH_FOREACH_END();
|
220 | 238 | }
|
|
0 commit comments