@@ -27,24 +27,17 @@ static void php_url_encode_scalar(zval *scalar, smart_str *form_str,
2727 const char * index_string , size_t index_string_len ,
2828 const char * num_prefix , size_t num_prefix_len ,
2929 const zend_string * key_prefix ,
30- const zend_string * arg_sep )
30+ const char * arg_sep , size_t arg_sep_len )
3131{
3232 if (form_str -> s ) {
33- smart_str_append (form_str , arg_sep );
33+ smart_str_appendl (form_str , arg_sep , arg_sep_len );
3434 }
3535 /* Simple key=value */
3636 if (key_prefix ) {
3737 smart_str_append (form_str , key_prefix );
3838 }
3939 if (index_string ) {
40- zend_string * encoded_key ;
41- if (encoding_type == PHP_QUERY_RFC3986 ) {
42- encoded_key = php_raw_url_encode (index_string , index_string_len );
43- } else {
44- encoded_key = php_url_encode (index_string , index_string_len );
45- }
46- smart_str_append (form_str , encoded_key );
47- zend_string_free (encoded_key );
40+ php_url_encode_to_smart_str (form_str , index_string , index_string_len , encoding_type == PHP_QUERY_RFC3986 );
4841 } else {
4942 /* Numeric key */
5043 if (num_prefix ) {
@@ -59,31 +52,16 @@ static void php_url_encode_scalar(zval *scalar, smart_str *form_str,
5952
6053try_again :
6154 switch (Z_TYPE_P (scalar )) {
62- case IS_STRING : {
63- zend_string * encoded_data ;
64- if (encoding_type == PHP_QUERY_RFC3986 ) {
65- encoded_data = php_raw_url_encode (Z_STRVAL_P (scalar ), Z_STRLEN_P (scalar ));
66- } else {
67- encoded_data = php_url_encode (Z_STRVAL_P (scalar ), Z_STRLEN_P (scalar ));
68- }
69- smart_str_append (form_str , encoded_data );
70- zend_string_free (encoded_data );
55+ case IS_STRING :
56+ php_url_encode_to_smart_str (form_str , Z_STRVAL_P (scalar ), Z_STRLEN_P (scalar ), encoding_type == PHP_QUERY_RFC3986 );
7157 break ;
72- }
7358 case IS_LONG :
7459 smart_str_append_long (form_str , Z_LVAL_P (scalar ));
7560 break ;
7661 case IS_DOUBLE : {
77- zend_string * encoded_data ;
7862 zend_string * tmp = zend_double_to_str (Z_DVAL_P (scalar ));
79- if (encoding_type == PHP_QUERY_RFC3986 ) {
80- encoded_data = php_raw_url_encode (ZSTR_VAL (tmp ), ZSTR_LEN (tmp ));
81- } else {
82- encoded_data = php_url_encode (ZSTR_VAL (tmp ), ZSTR_LEN (tmp ));
83- }
84- smart_str_append (form_str , encoded_data );
63+ php_url_encode_to_smart_str (form_str , ZSTR_VAL (tmp ), ZSTR_LEN (tmp ), encoding_type == PHP_QUERY_RFC3986 );
8564 zend_string_free (tmp );
86- zend_string_free (encoded_data );
8765 break ;
8866 }
8967 case IS_FALSE :
@@ -109,7 +87,7 @@ static void php_url_encode_scalar(zval *scalar, smart_str *form_str,
10987PHPAPI void php_url_encode_hash_ex (HashTable * ht , smart_str * formstr ,
11088 const char * num_prefix , size_t num_prefix_len ,
11189 const zend_string * key_prefix ,
112- zval * type , const zend_string * arg_sep , int enc_type )
90+ zval * type , const char * arg_sep , size_t arg_sep_len , int enc_type )
11391{
11492 zend_string * key = NULL ;
11593 const char * prop_name ;
@@ -124,9 +102,12 @@ PHPAPI void php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
124102 }
125103
126104 if (!arg_sep ) {
127- arg_sep = zend_ini_str ("arg_separator.output" , strlen ("arg_separator.output" ), false);
128- if (ZSTR_LEN (arg_sep ) == 0 ) {
129- arg_sep = ZSTR_CHAR ('&' );
105+ arg_sep = PG (arg_separator .output );
106+ if (!arg_sep || !* arg_sep ) {
107+ arg_sep = "&" ;
108+ arg_sep_len = 1 ;
109+ } else {
110+ arg_sep_len = strlen (arg_sep );
130111 }
131112 }
132113
@@ -181,7 +162,7 @@ PHPAPI void php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
181162 } else {
182163 new_prefix = zend_string_concat2 (ZSTR_VAL (encoded_key ), ZSTR_LEN (encoded_key ), "%5B" , strlen ("%5B" ));
183164 }
184- zend_string_release_ex (encoded_key , false );
165+ zend_string_efree (encoded_key );
185166 } else { /* is integer index */
186167 char * index_int_as_str ;
187168 size_t index_int_as_str_len ;
@@ -208,9 +189,9 @@ PHPAPI void php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
208189 efree (index_int_as_str );
209190 }
210191 GC_TRY_PROTECT_RECURSION (ht );
211- php_url_encode_hash_ex (HASH_OF (zdata ), formstr , NULL , 0 , new_prefix , (Z_TYPE_P (zdata ) == IS_OBJECT ? zdata : NULL ), arg_sep , enc_type );
192+ php_url_encode_hash_ex (HASH_OF (zdata ), formstr , NULL , 0 , new_prefix , (Z_TYPE_P (zdata ) == IS_OBJECT ? zdata : NULL ), arg_sep , arg_sep_len , enc_type );
212193 GC_TRY_UNPROTECT_RECURSION (ht );
213- zend_string_release_ex (new_prefix , false );
194+ zend_string_efree (new_prefix );
214195 } else if (Z_TYPE_P (zdata ) == IS_NULL || Z_TYPE_P (zdata ) == IS_RESOURCE ) {
215196 /* Skip these types */
216197 continue ;
@@ -220,7 +201,7 @@ PHPAPI void php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
220201 prop_name , prop_len ,
221202 num_prefix , num_prefix_len ,
222203 key_prefix ,
223- arg_sep );
204+ arg_sep , arg_sep_len );
224205 }
225206 } ZEND_HASH_FOREACH_END ();
226207}
@@ -233,15 +214,16 @@ PHP_FUNCTION(http_build_query)
233214 zval * formdata ;
234215 char * prefix = NULL ;
235216 size_t prefix_len = 0 ;
236- zend_string * arg_sep = NULL ;
217+ char * arg_sep = NULL ;
218+ size_t arg_sep_len = 0 ;
237219 smart_str formstr = {0 };
238220 zend_long enc_type = PHP_QUERY_RFC1738 ;
239221
240222 ZEND_PARSE_PARAMETERS_START (1 , 4 )
241223 Z_PARAM_ARRAY_OR_OBJECT (formdata )
242224 Z_PARAM_OPTIONAL
243225 Z_PARAM_STRING (prefix , prefix_len )
244- Z_PARAM_STR_OR_NULL (arg_sep )
226+ Z_PARAM_STRING_OR_NULL (arg_sep , arg_sep_len )
245227 Z_PARAM_LONG (enc_type )
246228 ZEND_PARSE_PARAMETERS_END ();
247229
@@ -250,7 +232,7 @@ PHP_FUNCTION(http_build_query)
250232 RETURN_THROWS ();
251233 }
252234
253- php_url_encode_hash_ex (HASH_OF (formdata ), & formstr , prefix , prefix_len , /* key_prefix */ NULL , (Z_TYPE_P (formdata ) == IS_OBJECT ? formdata : NULL ), arg_sep , (int )enc_type );
235+ php_url_encode_hash_ex (HASH_OF (formdata ), & formstr , prefix , prefix_len , /* key_prefix */ NULL , (Z_TYPE_P (formdata ) == IS_OBJECT ? formdata : NULL ), arg_sep , arg_sep_len , (int )enc_type );
254236
255237 RETURN_STR (smart_str_extract (& formstr ));
256238}
0 commit comments