xref: /PHP-7.3/ext/standard/http.c (revision 8e34de47)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Authors: Sara Golemon <pollita@php.net>                              |
16    +----------------------------------------------------------------------+
17 */
18 
19 #include "php_http.h"
20 #include "php_ini.h"
21 #include "url.h"
22 
23 #define URL_DEFAULT_ARG_SEP "&"
24 
25 /* {{{ php_url_encode_hash */
php_url_encode_hash_ex(HashTable * ht,smart_str * formstr,const char * num_prefix,size_t num_prefix_len,const char * key_prefix,size_t key_prefix_len,const char * key_suffix,size_t key_suffix_len,zval * type,char * arg_sep,int enc_type)26 PHPAPI int php_url_encode_hash_ex(HashTable *ht, smart_str *formstr,
27 				const char *num_prefix, size_t num_prefix_len,
28 				const char *key_prefix, size_t key_prefix_len,
29 				const char *key_suffix, size_t key_suffix_len,
30 			  zval *type, char *arg_sep, int enc_type)
31 {
32 	zend_string *key = NULL;
33 	char *newprefix, *p;
34 	const char *prop_name;
35 	size_t arg_sep_len, newprefix_len, prop_len;
36 	zend_ulong idx;
37 	zval *zdata = NULL;
38 
39 	if (!ht) {
40 		return FAILURE;
41 	}
42 
43 	if (GC_IS_RECURSIVE(ht)) {
44 		/* Prevent recursion */
45 		return SUCCESS;
46 	}
47 
48 	if (!arg_sep) {
49 		arg_sep = INI_STR("arg_separator.output");
50 		if (!arg_sep || !strlen(arg_sep)) {
51 			arg_sep = URL_DEFAULT_ARG_SEP;
52 		}
53 	}
54 	arg_sep_len = strlen(arg_sep);
55 
56 	ZEND_HASH_FOREACH_KEY_VAL_IND(ht, idx, key, zdata) {
57 		/* handling for private & protected object properties */
58 		if (key) {
59 			if (ZSTR_VAL(key)[0] == '\0' && type != NULL) {
60 				const char *tmp;
61 
62 				zend_object *zobj = Z_OBJ_P(type);
63 				if (zend_check_property_access(zobj, key) != SUCCESS) {
64 					/* private or protected property access outside of the class */
65 					continue;
66 				}
67 				zend_unmangle_property_name_ex(key, &tmp, &prop_name, &prop_len);
68 			} else {
69 				prop_name = ZSTR_VAL(key);
70 				prop_len = ZSTR_LEN(key);
71 			}
72 		} else {
73 			prop_name = NULL;
74 			prop_len = 0;
75 		}
76 
77 		ZVAL_DEREF(zdata);
78 		if (Z_TYPE_P(zdata) == IS_ARRAY || Z_TYPE_P(zdata) == IS_OBJECT) {
79 			if (key) {
80 				zend_string *ekey;
81 				if (enc_type == PHP_QUERY_RFC3986) {
82 					ekey = php_raw_url_encode(prop_name, prop_len);
83 				} else {
84 					ekey = php_url_encode(prop_name, prop_len);
85 				}
86 				newprefix_len = key_suffix_len + ZSTR_LEN(ekey) + key_prefix_len + 3 /* %5B */;
87 				newprefix = emalloc(newprefix_len + 1);
88 				p = newprefix;
89 
90 				if (key_prefix) {
91 					memcpy(p, key_prefix, key_prefix_len);
92 					p += key_prefix_len;
93 				}
94 
95 				memcpy(p, ZSTR_VAL(ekey), ZSTR_LEN(ekey));
96 				p += ZSTR_LEN(ekey);
97 				zend_string_free(ekey);
98 
99 				if (key_suffix) {
100 					memcpy(p, key_suffix, key_suffix_len);
101 					p += key_suffix_len;
102 				}
103 				*(p++) = '%';
104 				*(p++) = '5';
105 				*(p++) = 'B';
106 				*p = '\0';
107 			} else {
108 				char *ekey;
109 				size_t ekey_len;
110 				/* Is an integer key */
111 				ekey_len = spprintf(&ekey, 0, ZEND_LONG_FMT, idx);
112 				newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 3 /* %5B */;
113 				newprefix = emalloc(newprefix_len + 1);
114 				p = newprefix;
115 
116 				if (key_prefix) {
117 					memcpy(p, key_prefix, key_prefix_len);
118 					p += key_prefix_len;
119 				}
120 
121 				memcpy(p, num_prefix, num_prefix_len);
122 				p += num_prefix_len;
123 
124 				memcpy(p, ekey, ekey_len);
125 				p += ekey_len;
126 				efree(ekey);
127 
128 				if (key_suffix) {
129 					memcpy(p, key_suffix, key_suffix_len);
130 					p += key_suffix_len;
131 				}
132 				*(p++) = '%';
133 				*(p++) = '5';
134 				*(p++) = 'B';
135 				*p = '\0';
136 			}
137 			if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) {
138 				GC_PROTECT_RECURSION(ht);
139 			}
140 			php_url_encode_hash_ex(HASH_OF(zdata), formstr, NULL, 0, newprefix, newprefix_len, "%5D", 3, (Z_TYPE_P(zdata) == IS_OBJECT ? zdata : NULL), arg_sep, enc_type);
141 			if (!(GC_FLAGS(ht) & GC_IMMUTABLE)) {
142 				GC_UNPROTECT_RECURSION(ht);
143 			}
144 			efree(newprefix);
145 		} else if (Z_TYPE_P(zdata) == IS_NULL || Z_TYPE_P(zdata) == IS_RESOURCE) {
146 			/* Skip these types */
147 			continue;
148 		} else {
149 			if (formstr->s) {
150 				smart_str_appendl(formstr, arg_sep, arg_sep_len);
151 			}
152 			/* Simple key=value */
153 			smart_str_appendl(formstr, key_prefix, key_prefix_len);
154 			if (key) {
155 				zend_string *ekey;
156 				if (enc_type == PHP_QUERY_RFC3986) {
157 					ekey = php_raw_url_encode(prop_name, prop_len);
158 				} else {
159 					ekey = php_url_encode(prop_name, prop_len);
160 				}
161 				smart_str_append(formstr, ekey);
162 				zend_string_free(ekey);
163 			} else {
164 				/* Numeric key */
165 				if (num_prefix) {
166 					smart_str_appendl(formstr, num_prefix, num_prefix_len);
167 				}
168 				smart_str_append_long(formstr, idx);
169 			}
170 			smart_str_appendl(formstr, key_suffix, key_suffix_len);
171 			smart_str_appendl(formstr, "=", 1);
172 			switch (Z_TYPE_P(zdata)) {
173 				case IS_STRING: {
174 						zend_string *ekey;
175 						if (enc_type == PHP_QUERY_RFC3986) {
176 							ekey = php_raw_url_encode(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata));
177 						} else {
178 							ekey = php_url_encode(Z_STRVAL_P(zdata), Z_STRLEN_P(zdata));
179 						}
180 						smart_str_append(formstr, ekey);
181 						zend_string_free(ekey);
182 					}
183 					break;
184 				case IS_LONG:
185 					smart_str_append_long(formstr, Z_LVAL_P(zdata));
186 					break;
187 				case IS_FALSE:
188 					smart_str_appendl(formstr, "0", sizeof("0")-1);
189 					break;
190 				case IS_TRUE:
191 					smart_str_appendl(formstr, "1", sizeof("1")-1);
192 					break;
193 				default:
194 					{
195 						zend_string *ekey;
196 						zend_string *tmp;
197 						zend_string *str= zval_get_tmp_string(zdata, &tmp);
198 						if (enc_type == PHP_QUERY_RFC3986) {
199 							ekey = php_raw_url_encode(ZSTR_VAL(str), ZSTR_LEN(str));
200 						} else {
201 							ekey = php_url_encode(ZSTR_VAL(str), ZSTR_LEN(str));
202 						}
203 						smart_str_append(formstr, ekey);
204 						zend_tmp_string_release(tmp);
205 						zend_string_free(ekey);
206 					}
207 			}
208 		}
209 	} ZEND_HASH_FOREACH_END();
210 
211 	return SUCCESS;
212 }
213 /* }}} */
214 
215 /* {{{ proto string http_build_query(mixed formdata [, string prefix [, string arg_separator [, int enc_type]]])
216    Generates a form-encoded query string from an associative array or object. */
PHP_FUNCTION(http_build_query)217 PHP_FUNCTION(http_build_query)
218 {
219 	zval *formdata;
220 	char *prefix = NULL, *arg_sep=NULL;
221 	size_t arg_sep_len = 0, prefix_len = 0;
222 	smart_str formstr = {0};
223 	zend_long enc_type = PHP_QUERY_RFC1738;
224 
225 	ZEND_PARSE_PARAMETERS_START(1, 4)
226 		Z_PARAM_ZVAL(formdata)
227 		Z_PARAM_OPTIONAL
228 		Z_PARAM_STRING(prefix, prefix_len)
229 		Z_PARAM_STRING(arg_sep, arg_sep_len)
230 		Z_PARAM_LONG(enc_type)
231 	ZEND_PARSE_PARAMETERS_END_EX(RETURN_FALSE);
232 
233 	if (Z_TYPE_P(formdata) != IS_ARRAY && Z_TYPE_P(formdata) != IS_OBJECT) {
234 		php_error_docref(NULL, E_WARNING, "Parameter 1 expected to be Array or Object.  Incorrect value given");
235 		RETURN_FALSE;
236 	}
237 
238 	if (php_url_encode_hash_ex(HASH_OF(formdata), &formstr, prefix, prefix_len, NULL, 0, NULL, 0, (Z_TYPE_P(formdata) == IS_OBJECT ? formdata : NULL), arg_sep, (int)enc_type) == FAILURE) {
239 		if (formstr.s) {
240 			smart_str_free(&formstr);
241 		}
242 		RETURN_FALSE;
243 	}
244 
245 	if (!formstr.s) {
246 		RETURN_EMPTY_STRING();
247 	}
248 
249 	smart_str_0(&formstr);
250 
251 	RETURN_NEW_STR(formstr.s);
252 }
253 /* }}} */
254 
255 /*
256  * Local variables:
257  * tab-width: 4
258  * c-basic-offset: 4
259  * End:
260  * vim600: sw=4 ts=4 fdm=marker
261  * vim<600: sw=4 ts=4
262  */
263