- even better use a object struct member for that crap
[m6w6/ext-http] / http_url_api.c
1 /*
2 +----------------------------------------------------------------------+
3 | PECL :: http |
4 +----------------------------------------------------------------------+
5 | This source file is subject to version 3.0 of the PHP license, that |
6 | is bundled with this package in the file LICENSE, and is available |
7 | through the world-wide-web at http://www.php.net/license/3_0.txt. |
8 | If you did not receive a copy of the PHP license and are unable to |
9 | obtain it through the world-wide-web, please send a note to |
10 | license@php.net so we can mail you a copy immediately. |
11 +----------------------------------------------------------------------+
12 | Copyright (c) 2004-2005 Michael Wallner <mike@php.net> |
13 +----------------------------------------------------------------------+
14 */
15
16 /* $Id$ */
17
18 #ifdef HAVE_CONFIG_H
19 # include "config.h"
20 #endif
21
22 #ifdef PHP_WIN32
23 # include <winsock2.h>
24 #elif defined(HAVE_NETDB_H)
25 # include <netdb.h>
26 #endif
27
28 #include "php.h"
29 #include "zend_ini.h"
30 #include "php_output.h"
31 #include "ext/standard/url.h"
32
33 #include "SAPI.h"
34
35 #include "phpstr/phpstr.h"
36
37 #include "php_http.h"
38 #include "php_http_api.h"
39 #include "php_http_url_api.h"
40 #include "php_http_std_defs.h"
41
42 ZEND_EXTERN_MODULE_GLOBALS(http);
43
44 /* {{{ char *http_absolute_url(char *) */
45 PHP_HTTP_API char *_http_absolute_url_ex(
46 const char *url, size_t url_len,
47 const char *proto, size_t proto_len,
48 const char *host, size_t host_len,
49 unsigned port TSRMLS_DC)
50 {
51 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
52 struct servent *se;
53 #endif
54 php_url *purl, furl = {NULL};
55 size_t full_len = 0;
56 zval *zhost = NULL;
57 char *scheme = NULL, *URL = ecalloc(1, HTTP_URI_MAXLEN + 1);
58
59 if ((!url || !url_len) && (
60 (!(url = SG(request_info).request_uri)) ||
61 (!(url_len = strlen(SG(request_info).request_uri))))) {
62 php_error_docref(NULL TSRMLS_CC, E_WARNING,
63 "Cannot build an absolute URI if supplied URL and REQUEST_URI is empty");
64 return NULL;
65 }
66
67 if (!(purl = php_url_parse((char *) url))) {
68 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Could not parse supplied URL");
69 return NULL;
70 }
71
72 furl.user = purl->user;
73 furl.pass = purl->pass;
74 furl.path = purl->path;
75 furl.query = purl->query;
76 furl.fragment = purl->fragment;
77
78 if (proto && proto_len) {
79 furl.scheme = scheme = estrdup(proto);
80 } else if (purl->scheme) {
81 furl.scheme = purl->scheme;
82 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
83 } else if (port && (se = getservbyport(port, "tcp"))) {
84 furl.scheme = (scheme = estrdup(se->s_name));
85 #endif
86 } else {
87 furl.scheme = "http";
88 }
89
90 if (port) {
91 furl.port = port;
92 } else if (purl->port) {
93 furl.port = purl->port;
94 } else if (strncmp(furl.scheme, "http", 4)) {
95 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
96 if (se = getservbyname(furl.scheme, "tcp")) {
97 furl.port = se->s_port;
98 }
99 #endif
100 } else {
101 furl.port = (furl.scheme[4] == 's') ? 443 : 80;
102 }
103
104 if (host) {
105 furl.host = (char *) host;
106 } else if (purl->host) {
107 furl.host = purl->host;
108 } else if ( (zhost = http_get_server_var("HTTP_HOST")) ||
109 (zhost = http_get_server_var("SERVER_NAME"))) {
110 furl.host = Z_STRVAL_P(zhost);
111 } else {
112 furl.host = "localhost";
113 }
114
115 #define HTTP_URI_STRLCATS(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, sizeof(add_string)-1)
116 #define HTTP_URI_STRLCATL(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, strlen(add_string))
117 #define HTTP_URI_STRLCAT(URL, full_len, add_string, add_len) \
118 if ((full_len += add_len) > HTTP_URI_MAXLEN) { \
119 php_error_docref(NULL TSRMLS_CC, E_NOTICE, \
120 "Absolute URI would have exceeded max URI length (%d bytes) - " \
121 "tried to add %d bytes ('%s')", \
122 HTTP_URI_MAXLEN, add_len, add_string); \
123 if (scheme) { \
124 efree(scheme); \
125 } \
126 php_url_free(purl); \
127 return URL; \
128 } else { \
129 strcat(URL, add_string); \
130 }
131
132 HTTP_URI_STRLCATL(URL, full_len, furl.scheme);
133 HTTP_URI_STRLCATS(URL, full_len, "://");
134
135 if (furl.user) {
136 HTTP_URI_STRLCATL(URL, full_len, furl.user);
137 if (furl.pass) {
138 HTTP_URI_STRLCATS(URL, full_len, ":");
139 HTTP_URI_STRLCATL(URL, full_len, furl.pass);
140 }
141 HTTP_URI_STRLCATS(URL, full_len, "@");
142 }
143
144 HTTP_URI_STRLCATL(URL, full_len, furl.host);
145
146 if ( (!strcmp(furl.scheme, "http") && (furl.port != 80)) ||
147 (!strcmp(furl.scheme, "https") && (furl.port != 443))) {
148 char port_string[8] = {0};
149 snprintf(port_string, 7, ":%u", furl.port);
150 HTTP_URI_STRLCATL(URL, full_len, port_string);
151 }
152
153 if (furl.path) {
154 if (furl.path[0] != '/') {
155 HTTP_URI_STRLCATS(URL, full_len, "/");
156 }
157 HTTP_URI_STRLCATL(URL, full_len, furl.path);
158 } else {
159 HTTP_URI_STRLCATS(URL, full_len, "/");
160 }
161
162 if (furl.query) {
163 HTTP_URI_STRLCATS(URL, full_len, "?");
164 HTTP_URI_STRLCATL(URL, full_len, furl.query);
165 }
166
167 if (furl.fragment) {
168 HTTP_URI_STRLCATS(URL, full_len, "#");
169 HTTP_URI_STRLCATL(URL, full_len, furl.fragment);
170 }
171
172 if (scheme) {
173 efree(scheme);
174 }
175 php_url_free(purl);
176
177 return URL;
178 }
179 /* }}} */
180
181 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
182 PHP_HTTP_API STATUS _http_urlencode_hash_ex(HashTable *hash, zend_bool override_argsep,
183 char *pre_encoded_data, size_t pre_encoded_len,
184 char **encoded_data, size_t *encoded_len TSRMLS_DC)
185 {
186 char *arg_sep;
187 phpstr *qstr = phpstr_new();
188
189 if (override_argsep || !strlen(arg_sep = INI_STR("arg_separator.output"))) {
190 arg_sep = HTTP_URL_ARGSEP;
191 }
192
193 if (pre_encoded_len && pre_encoded_data) {
194 phpstr_append(qstr, pre_encoded_data, pre_encoded_len);
195 }
196
197 if (SUCCESS != http_urlencode_hash_implementation(hash, qstr, arg_sep)) {
198 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Couldn't encode query data");
199 phpstr_free(qstr);
200 return FAILURE;
201 }
202
203 phpstr_data(qstr, encoded_data, encoded_len);
204 phpstr_free(qstr);
205
206 return SUCCESS;
207 }
208 /* }}} */
209
210 /* {{{ http_urlencode_hash_implementation
211 Original Author: Sara Golemon <pollita@php.net> */
212 PHP_HTTP_API STATUS _http_urlencode_hash_implementation_ex(
213 HashTable *ht, phpstr *formstr, char *arg_sep,
214 const char *num_prefix, int num_prefix_len,
215 const char *key_prefix, int key_prefix_len,
216 const char *key_suffix, int key_suffix_len,
217 zval *type TSRMLS_DC)
218 {
219 char *key = NULL, *ekey, *newprefix, *p;
220 int arg_sep_len, key_len, ekey_len, key_type, newprefix_len;
221 ulong idx;
222 zval **zdata = NULL, *copyzval;
223
224 if (!ht || !formstr) {
225 return FAILURE;
226 }
227
228 if (ht->nApplyCount > 0) {
229 /* Prevent recursion */
230 return SUCCESS;
231 }
232
233 if (!arg_sep || !strlen(arg_sep)) {
234 arg_sep = HTTP_URL_ARGSEP;
235 }
236 arg_sep_len = strlen(arg_sep);
237
238 for (zend_hash_internal_pointer_reset(ht);
239 (key_type = zend_hash_get_current_key_ex(ht, &key, &key_len, &idx, 0, NULL)) != HASH_KEY_NON_EXISTANT;
240 zend_hash_move_forward(ht)
241 ) {
242 if (key_type == HASH_KEY_IS_STRING && key_len && key[key_len-1] == '\0') {
243 /* We don't want that trailing NULL */
244 key_len -= 1;
245 }
246
247 #ifdef ZEND_ENGINE_2
248 /* handling for private & protected object properties */
249 if (key && *key == '\0' && type != NULL) {
250 char *tmp;
251
252 zend_object *zobj = zend_objects_get_address(type TSRMLS_CC);
253 if (zend_check_property_access(zobj, key TSRMLS_CC) != SUCCESS) {
254 /* private or protected property access outside of the class */
255 continue;
256 }
257 zend_unmangle_property_name(key, &tmp, &key);
258 key_len = strlen(key);
259 }
260 #endif
261
262 if (zend_hash_get_current_data_ex(ht, (void **)&zdata, NULL) == FAILURE || !zdata || !(*zdata)) {
263 php_error_docref(NULL TSRMLS_CC, E_WARNING, "Error traversing form data array.");
264 return FAILURE;
265 }
266 if (Z_TYPE_PP(zdata) == IS_ARRAY || Z_TYPE_PP(zdata) == IS_OBJECT) {
267 if (key_type == HASH_KEY_IS_STRING) {
268 ekey = php_url_encode(key, key_len, &ekey_len);
269 newprefix_len = key_suffix_len + ekey_len + key_prefix_len + 1;
270 newprefix = emalloc(newprefix_len + 1);
271 p = newprefix;
272
273 if (key_prefix) {
274 memcpy(p, key_prefix, key_prefix_len);
275 p += key_prefix_len;
276 }
277
278 memcpy(p, ekey, ekey_len);
279 p += ekey_len;
280 efree(ekey);
281
282 if (key_suffix) {
283 memcpy(p, key_suffix, key_suffix_len);
284 p += key_suffix_len;
285 }
286
287 *(p++) = '[';
288 *p = '\0';
289 } else {
290 /* Is an integer key */
291 ekey_len = spprintf(&ekey, 12, "%ld", idx);
292 newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 1;
293 newprefix = emalloc(newprefix_len + 1);
294 p = newprefix;
295
296 if (key_prefix) {
297 memcpy(p, key_prefix, key_prefix_len);
298 p += key_prefix_len;
299 }
300
301 memcpy(p, num_prefix, num_prefix_len);
302 p += num_prefix_len;
303
304 memcpy(p, ekey, ekey_len);
305 p += ekey_len;
306 efree(ekey);
307
308 if (key_suffix) {
309 memcpy(p, key_suffix, key_suffix_len);
310 p += key_suffix_len;
311 }
312 *(p++) = '[';
313 *p = '\0';
314 }
315 ht->nApplyCount++;
316 http_urlencode_hash_implementation_ex(HASH_OF(*zdata), formstr, arg_sep,
317 NULL, 0, newprefix, newprefix_len, "]", 1, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata : NULL));
318 ht->nApplyCount--;
319 efree(newprefix);
320 } else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) {
321 /* Skip these types */
322 continue;
323 } else {
324 if (formstr->used) {
325 phpstr_append(formstr, arg_sep, arg_sep_len);
326 }
327 /* Simple key=value */
328 phpstr_append(formstr, key_prefix, key_prefix_len);
329 if (key_type == HASH_KEY_IS_STRING) {
330 ekey = php_url_encode(key, key_len, &ekey_len);
331 phpstr_append(formstr, ekey, ekey_len);
332 efree(ekey);
333 } else {
334 /* Numeric key */
335 if (num_prefix) {
336 phpstr_append(formstr, num_prefix, num_prefix_len);
337 }
338 ekey_len = spprintf(&ekey, 12, "%ld", idx);
339 phpstr_append(formstr, ekey, ekey_len);
340 efree(ekey);
341 }
342 phpstr_append(formstr, key_suffix, key_suffix_len);
343 phpstr_appends(formstr, "=");
344 switch (Z_TYPE_PP(zdata)) {
345 case IS_STRING:
346 ekey = php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);
347 break;
348 case IS_LONG:
349 case IS_BOOL:
350 ekey_len = spprintf(&ekey, 12, "%ld", Z_LVAL_PP(zdata));
351 break;
352 case IS_DOUBLE:
353 ekey_len = spprintf(&ekey, 48, "%.*G", (int) EG(precision), Z_DVAL_PP(zdata));
354 break;
355 default:
356 /* fall back on convert to string */
357 MAKE_STD_ZVAL(copyzval);
358 *copyzval = **zdata;
359 zval_copy_ctor(copyzval);
360 convert_to_string_ex(&copyzval);
361 ekey = php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
362 zval_ptr_dtor(&copyzval);
363 }
364 phpstr_append(formstr, ekey, ekey_len);
365 efree(ekey);
366 }
367 }
368
369 return SUCCESS;
370 }
371 /* }}} */
372
373 /*
374 * Local variables:
375 * tab-width: 4
376 * c-basic-offset: 4
377 * End:
378 * vim600: noet sw=4 ts=4 fdm=marker
379 * vim<600: noet sw=4 ts=4
380 */
381