- fix some gcc warnings
[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 #include "php.h"
22
23 #include "SAPI.h"
24 #include "zend_ini.h"
25 #include "php_output.h"
26 #include "ext/standard/url.h"
27
28 #include "php_http.h"
29 #include "php_http_api.h"
30 #include "php_http_url_api.h"
31 #include "php_http_std_defs.h"
32
33 #include "phpstr/phpstr.h"
34
35 #ifdef PHP_WIN32
36 # include <winsock2.h>
37 #elif defined(HAVE_NETDB_H)
38 # include <netdb.h>
39 #endif
40
41 ZEND_EXTERN_MODULE_GLOBALS(http);
42
43 /* {{{ char *http_absolute_url(char *) */
44 PHP_HTTP_API char *_http_absolute_url_ex(
45 const char *url, size_t url_len,
46 const char *proto, size_t proto_len,
47 const char *host, size_t host_len,
48 unsigned port TSRMLS_DC)
49 {
50 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
51 struct servent *se;
52 #endif
53 php_url *purl = NULL, furl;
54 size_t full_len = 0;
55 zval *zhost = NULL;
56 char *scheme = NULL, *uri, *URL = ecalloc(1, HTTP_URI_MAXLEN + 1);
57
58 if ((!url || !url_len) && (
59 (!(url = SG(request_info).request_uri)) ||
60 (!(url_len = strlen(SG(request_info).request_uri))))) {
61 http_error(E_WARNING, HTTP_E_PARAM, "Cannot build an absolute URI if supplied URL and REQUEST_URI is empty");
62 return NULL;
63 }
64
65 uri = estrndup(url, url_len);
66 if (!(purl = php_url_parse(uri))) {
67 http_error_ex(E_WARNING, HTTP_E_PARSE, "Could not parse supplied URL: %s", url);
68 return NULL;
69 }
70
71 furl.user = purl->user;
72 furl.pass = purl->pass;
73 furl.path = purl->path;
74 furl.query = purl->query;
75 furl.fragment = purl->fragment;
76
77 if (proto && proto_len) {
78 furl.scheme = scheme = estrdup(proto);
79 } else if (purl->scheme) {
80 furl.scheme = purl->scheme;
81 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
82 } else if (port && (se = getservbyport(port, "tcp"))) {
83 furl.scheme = (scheme = estrdup(se->s_name));
84 #endif
85 } else {
86 furl.scheme = "http";
87 }
88
89 if (port) {
90 furl.port = port;
91 } else if (purl->port) {
92 furl.port = purl->port;
93 } else if (strncmp(furl.scheme, "http", 4)) {
94 #if defined(PHP_WIN32) || defined(HAVE_NETDB_H)
95 if (se = getservbyname(furl.scheme, "tcp")) {
96 furl.port = se->s_port;
97 }
98 #endif
99 } else {
100 furl.port = (furl.scheme[4] == 's') ? 443 : 80;
101 }
102
103 if (host) {
104 furl.host = (char *) host;
105 } else if (purl->host) {
106 furl.host = purl->host;
107 } else if ( (zhost = http_get_server_var("HTTP_HOST")) ||
108 (zhost = http_get_server_var("SERVER_NAME"))) {
109 furl.host = Z_STRVAL_P(zhost);
110 } else {
111 furl.host = "localhost";
112 }
113
114 #define HTTP_URI_STRLCATS(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, sizeof(add_string)-1)
115 #define HTTP_URI_STRLCATL(URL, full_len, add_string) HTTP_URI_STRLCAT(URL, full_len, add_string, strlen(add_string))
116 #define HTTP_URI_STRLCAT(URL, full_len, add_string, add_len) \
117 if ((full_len += add_len) > HTTP_URI_MAXLEN) { \
118 http_error_ex(E_NOTICE, HTTP_E_URL, \
119 "Absolute URI would have exceeded max URI length (%d bytes) - " \
120 "tried to add %d bytes ('%s')", \
121 HTTP_URI_MAXLEN, add_len, add_string); \
122 if (scheme) { \
123 efree(scheme); \
124 } \
125 php_url_free(purl); \
126 efree(uri); \
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 efree(uri);
177
178 return URL;
179 }
180 /* }}} */
181
182 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
183 PHP_HTTP_API STATUS _http_urlencode_hash_ex(HashTable *hash, zend_bool override_argsep,
184 char *pre_encoded_data, size_t pre_encoded_len,
185 char **encoded_data, size_t *encoded_len TSRMLS_DC)
186 {
187 char *arg_sep;
188 phpstr *qstr = phpstr_new();
189
190 if (override_argsep || !strlen(arg_sep = INI_STR("arg_separator.output"))) {
191 arg_sep = HTTP_URL_ARGSEP;
192 }
193
194 if (pre_encoded_len && pre_encoded_data) {
195 phpstr_append(qstr, pre_encoded_data, pre_encoded_len);
196 }
197
198 if (SUCCESS != http_urlencode_hash_implementation(hash, qstr, arg_sep)) {
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 http_error(E_WARNING, HTTP_E_PARAM, "Invalid parameters");
226 return FAILURE;
227 }
228
229 if (ht->nApplyCount > 0) {
230 /* Prevent recursion */
231 return SUCCESS;
232 }
233
234 if (!arg_sep || !strlen(arg_sep)) {
235 arg_sep = HTTP_URL_ARGSEP;
236 }
237 arg_sep_len = strlen(arg_sep);
238
239 for (zend_hash_internal_pointer_reset(ht);
240 (key_type = zend_hash_get_current_key_ex(ht, &key, &key_len, &idx, 0, NULL)) != HASH_KEY_NON_EXISTANT;
241 zend_hash_move_forward(ht)
242 ) {
243 if (key_type == HASH_KEY_IS_STRING && key_len && key[key_len-1] == '\0') {
244 /* We don't want that trailing NULL */
245 key_len -= 1;
246 }
247
248 #ifdef ZEND_ENGINE_2
249 /* handling for private & protected object properties */
250 if (key && *key == '\0' && type != NULL) {
251 char *tmp;
252
253 zend_object *zobj = zend_objects_get_address(type TSRMLS_CC);
254 if (zend_check_property_access(zobj, key TSRMLS_CC) != SUCCESS) {
255 /* private or protected property access outside of the class */
256 continue;
257 }
258 zend_unmangle_property_name(key, &tmp, &key);
259 key_len = strlen(key);
260 }
261 #endif
262
263 if (zend_hash_get_current_data_ex(ht, (void **)&zdata, NULL) == FAILURE || !zdata || !(*zdata)) {
264 http_error(E_WARNING, HTTP_E_ENCODE, "Error traversing form data array.");
265 return FAILURE;
266 }
267 if (Z_TYPE_PP(zdata) == IS_ARRAY || Z_TYPE_PP(zdata) == IS_OBJECT) {
268 if (key_type == HASH_KEY_IS_STRING) {
269 ekey = php_url_encode(key, key_len, &ekey_len);
270 newprefix_len = key_suffix_len + ekey_len + key_prefix_len + 1;
271 newprefix = emalloc(newprefix_len + 1);
272 p = newprefix;
273
274 if (key_prefix) {
275 memcpy(p, key_prefix, key_prefix_len);
276 p += key_prefix_len;
277 }
278
279 memcpy(p, ekey, ekey_len);
280 p += ekey_len;
281 efree(ekey);
282
283 if (key_suffix) {
284 memcpy(p, key_suffix, key_suffix_len);
285 p += key_suffix_len;
286 }
287
288 *(p++) = '[';
289 *p = '\0';
290 } else {
291 /* Is an integer key */
292 ekey_len = spprintf(&ekey, 12, "%ld", idx);
293 newprefix_len = key_prefix_len + num_prefix_len + ekey_len + key_suffix_len + 1;
294 newprefix = emalloc(newprefix_len + 1);
295 p = newprefix;
296
297 if (key_prefix) {
298 memcpy(p, key_prefix, key_prefix_len);
299 p += key_prefix_len;
300 }
301
302 memcpy(p, num_prefix, num_prefix_len);
303 p += num_prefix_len;
304
305 memcpy(p, ekey, ekey_len);
306 p += ekey_len;
307 efree(ekey);
308
309 if (key_suffix) {
310 memcpy(p, key_suffix, key_suffix_len);
311 p += key_suffix_len;
312 }
313 *(p++) = '[';
314 *p = '\0';
315 }
316 ht->nApplyCount++;
317 http_urlencode_hash_implementation_ex(HASH_OF(*zdata), formstr, arg_sep,
318 NULL, 0, newprefix, newprefix_len, "]", 1, (Z_TYPE_PP(zdata) == IS_OBJECT ? *zdata : NULL));
319 ht->nApplyCount--;
320 efree(newprefix);
321 } else if (Z_TYPE_PP(zdata) == IS_NULL || Z_TYPE_PP(zdata) == IS_RESOURCE) {
322 /* Skip these types */
323 continue;
324 } else {
325 if (formstr->used) {
326 phpstr_append(formstr, arg_sep, arg_sep_len);
327 }
328 /* Simple key=value */
329 phpstr_append(formstr, key_prefix, key_prefix_len);
330 if (key_type == HASH_KEY_IS_STRING) {
331 ekey = php_url_encode(key, key_len, &ekey_len);
332 phpstr_append(formstr, ekey, ekey_len);
333 efree(ekey);
334 } else {
335 /* Numeric key */
336 if (num_prefix) {
337 phpstr_append(formstr, num_prefix, num_prefix_len);
338 }
339 ekey_len = spprintf(&ekey, 12, "%ld", idx);
340 phpstr_append(formstr, ekey, ekey_len);
341 efree(ekey);
342 }
343 phpstr_append(formstr, key_suffix, key_suffix_len);
344 phpstr_appends(formstr, "=");
345 switch (Z_TYPE_PP(zdata)) {
346 case IS_STRING:
347 ekey = php_url_encode(Z_STRVAL_PP(zdata), Z_STRLEN_PP(zdata), &ekey_len);
348 break;
349 case IS_LONG:
350 case IS_BOOL:
351 ekey_len = spprintf(&ekey, 12, "%ld", Z_LVAL_PP(zdata));
352 break;
353 case IS_DOUBLE:
354 ekey_len = spprintf(&ekey, 48, "%.*G", (int) EG(precision), Z_DVAL_PP(zdata));
355 break;
356 default:
357 /* fall back on convert to string */
358 MAKE_STD_ZVAL(copyzval);
359 *copyzval = **zdata;
360 zval_copy_ctor(copyzval);
361 convert_to_string_ex(&copyzval);
362 ekey = php_url_encode(Z_STRVAL_P(copyzval), Z_STRLEN_P(copyzval), &ekey_len);
363 zval_ptr_dtor(&copyzval);
364 }
365 phpstr_append(formstr, ekey, ekey_len);
366 efree(ekey);
367 }
368 }
369
370 return SUCCESS;
371 }
372 /* }}} */
373
374 /*
375 * Local variables:
376 * tab-width: 4
377 * c-basic-offset: 4
378 * End:
379 * vim600: noet sw=4 ts=4 fdm=marker
380 * vim<600: noet sw=4 ts=4
381 */
382