- all those handlers->add_ref(zobject) would prevent the objects from
[m6w6/ext-http] / http_url_api.c
1 /*
2 +--------------------------------------------------------------------+
3 | PECL :: http |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted provided that the conditions mentioned |
7 | in the accompanying LICENSE file are met. |
8 +--------------------------------------------------------------------+
9 | Copyright (c) 2004-2005, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #ifdef HAVE_CONFIG_H
16 # include "config.h"
17 #endif
18 #include "php.h"
19
20 #include "SAPI.h"
21 #include "zend_ini.h"
22 #include "php_output.h"
23 #include "ext/standard/url.h"
24
25 #include "php_http.h"
26 #include "php_http_api.h"
27 #include "php_http_url_api.h"
28 #include "php_http_std_defs.h"
29
30 #include "phpstr/phpstr.h"
31
32 #ifdef PHP_WIN32
33 # include <winsock2.h>
34 #elif defined(HAVE_NETDB_H)
35 # include <netdb.h>
36 #endif
37
38 ZEND_EXTERN_MODULE_GLOBALS(http);
39
40 /* {{{ char *http_absolute_url(char *) */
41 PHP_HTTP_API char *_http_absolute_url_ex(
42 const char *url, size_t url_len,
43 const char *proto, size_t proto_len,
44 const char *host, size_t host_len,
45 unsigned port TSRMLS_DC)
46 {
47 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
48 struct servent *se;
49 #endif
50 php_url *purl = NULL, furl;
51 size_t full_len = 0;
52 zval *zhost = NULL;
53 char *scheme = NULL, *uri, *URL;
54
55 if ((!url || !url_len) && (
56 (!(url = SG(request_info).request_uri)) ||
57 (!(url_len = strlen(SG(request_info).request_uri))))) {
58 http_error(HE_WARNING, HTTP_E_RUNTIME, "Cannot build an absolute URI if supplied URL and REQUEST_URI is empty");
59 return NULL;
60 }
61
62 URL = ecalloc(1, HTTP_URI_MAXLEN + 1);
63 uri = estrndup(url, url_len);
64 if (!(purl = php_url_parse(uri))) {
65 http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse supplied URL: %s", url);
66 return NULL;
67 }
68
69 furl.user = purl->user;
70 furl.pass = purl->pass;
71 furl.path = purl->path;
72 furl.query = purl->query;
73 furl.fragment = purl->fragment;
74
75 if (proto && proto_len) {
76 furl.scheme = scheme = estrdup(proto);
77 } else if (purl->scheme) {
78 furl.scheme = purl->scheme;
79 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
80 } else if (port && (se = getservbyport(port, "tcp"))) {
81 furl.scheme = (scheme = estrdup(se->s_name));
82 #endif
83 } else {
84 furl.scheme = "http";
85 }
86
87 if (port) {
88 furl.port = port;
89 } else if (purl->port) {
90 furl.port = purl->port;
91 } else if (strncmp(furl.scheme, "http", 4)) {
92 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
93 if (se = getservbyname(furl.scheme, "tcp")) {
94 furl.port = se->s_port;
95 }
96 #endif
97 } else {
98 furl.port = (furl.scheme[4] == 's') ? 443 : 80;
99 }
100
101 if (host) {
102 furl.host = (char *) host;
103 } else if (purl->host) {
104 furl.host = purl->host;
105 } else if ( (zhost = http_get_server_var("HTTP_HOST")) ||
106 (zhost = http_get_server_var("SERVER_NAME"))) {
107 furl.host = Z_STRVAL_P(zhost);
108 } else {
109 furl.host = "localhost";
110 }
111
112 #define HTTP_URI_STRLCATS(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, sizeof(add_string)-1)
113 #define HTTP_URI_STRLCATL(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, strlen(add_string))
114 #define HTTP_URI_STRLCAT(URL, full_len, add_string, add_len) \
115 if ((full_len += add_len) > HTTP_URI_MAXLEN) { \
116 http_error_ex(HE_NOTICE, HTTP_E_URL, \
117 "Absolute URI would have exceeded max URI length (%d bytes) - " \
118 "tried to add %d bytes ('%s')", \
119 HTTP_URI_MAXLEN, add_len, add_string); \
120 if (scheme) { \
121 efree(scheme); \
122 } \
123 php_url_free(purl); \
124 efree(uri); \
125 return URL; \
126 } else { \
127 strcat(URL, add_string); \
128 }
129
130 HTTP_URI_STRLCATL(URL, full_len, furl.scheme);
131 HTTP_URI_STRLCATS(URL, full_len, "://");
132
133 if (furl.user) {
134 HTTP_URI_STRLCATL(URL, full_len, furl.user);
135 if (furl.pass) {
136 HTTP_URI_STRLCATS(URL, full_len, ":");
137 HTTP_URI_STRLCATL(URL, full_len, furl.pass);
138 }
139 HTTP_URI_STRLCATS(URL, full_len, "@");
140 }
141
142 HTTP_URI_STRLCATL(URL, full_len, furl.host);
143
144 if ( (!strcmp(furl.scheme, "http") && (furl.port != 80)) ||
145 (!strcmp(furl.scheme, "https") && (furl.port != 443))) {
146 char port_string[8] = {0};
147 snprintf(port_string, 7, ":%u", furl.port);
148 HTTP_URI_STRLCATL(URL, full_len, port_string);
149 }
150
151 if (furl.path) {
152 if (furl.path[0] != '/') {
153 HTTP_URI_STRLCATS(URL, full_len, "/");
154 }
155 HTTP_URI_STRLCATL(URL, full_len, furl.path);
156 } else {
157 HTTP_URI_STRLCATS(URL, full_len, "/");
158 }
159
160 if (furl.query) {
161 HTTP_URI_STRLCATS(URL, full_len, "?");
162 HTTP_URI_STRLCATL(URL, full_len, furl.query);
163 }
164
165 if (furl.fragment) {
166 HTTP_URI_STRLCATS(URL, full_len, "#");
167 HTTP_URI_STRLCATL(URL, full_len, furl.fragment);
168 }
169
170 if (scheme) {
171 efree(scheme);
172 }
173 php_url_free(purl);
174 efree(uri);
175
176 return URL;
177 }
178 /* }}} */
179
180 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
181 PHP_HTTP_API STATUS _http_urlencode_hash_ex(HashTable *hash, zend_bool override_argsep,
182 char *pre_encoded_data, size_t pre_encoded_len,
183 char **encoded_data, size_t *encoded_len TSRMLS_DC)
184 {
185 char *arg_sep;
186 phpstr *qstr = phpstr_new();
187
188 if (override_argsep || !strlen(arg_sep = INI_STR("arg_separator.output"))) {
189 arg_sep = HTTP_URL_ARGSEP;
190 }
191
192 if (pre_encoded_len && pre_encoded_data) {
193 phpstr_append(qstr, pre_encoded_data, pre_encoded_len);
194 }
195
196 if (SUCCESS != http_urlencode_hash_implementation(hash, qstr, arg_sep)) {
197 phpstr_free(&qstr);
198 return FAILURE;
199 }
200
201 phpstr_data(qstr, encoded_data, encoded_len);
202 phpstr_free(&qstr);
203
204 return SUCCESS;
205 }
206 /* }}} */
207
208 /* {{{ http_urlencode_hash_implementation
209 Original Author: Sara Golemon <pollita@php.net> */
210 PHP_HTTP_API STATUS _http_urlencode_hash_implementation_ex(
211 HashTable *ht, phpstr *formstr, char *arg_sep,
212 const char *num_prefix, int num_prefix_len,
213 const char *key_prefix, int key_prefix_len,
214 const char *key_suffix, int key_suffix_len,
215 zval *type TSRMLS_DC)
216 {
217 char *key = NULL, *ekey, *newprefix, *p;
218 int arg_sep_len, ekey_len, key_type, newprefix_len;
219 uint key_len;
220 ulong idx;
221 zval **zdata = NULL, *copyzval;
222
223 if (!ht || !formstr) {
224 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid parameters");
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 http_error(HE_WARNING, HTTP_E_ENCODING, "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