- fix HttpQueryString failures with objects as argument
[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-2006, Michael Wallner <mike@php.net> |
10 +--------------------------------------------------------------------+
11 */
12
13 /* $Id$ */
14
15 #define HTTP_WANT_SAPI
16 #define HTTP_WANT_NETDB
17 #include "php_http.h"
18
19 #include "zend_ini.h"
20 #include "php_output.h"
21 #include "ext/standard/php_string.h"
22
23 #include "php_http_api.h"
24 #include "php_http_querystring_api.h"
25 #include "php_http_url_api.h"
26
27 static inline char *localhostname(void)
28 {
29 char hostname[1024] = {0};
30
31 #ifdef PHP_WIN32
32 if (SUCCESS == gethostname(hostname, lenof(hostname))) {
33 return estrdup(hostname);
34 }
35 #elif defined(HAVE_UNISTD_H)
36 if (SUCCESS == gethostname(hostname, lenof(hostname))) {
37 size_t hlen = strlen(hostname);
38
39 if (hlen <= lenof(hostname) - lenof("(none)")) {
40 hostname[hlen++] = '.';
41 if (SUCCESS == getdomainname(&hostname[hlen], lenof(hostname) - hlen)) {
42 if (!strcmp(&hostname[hlen], "(none)")) {
43 hostname[hlen - 1] = '\0';
44 }
45 return estrdup(hostname);
46 }
47 }
48 }
49 #endif
50 return estrdup("localhost");
51 }
52
53 PHP_MINIT_FUNCTION(http_url)
54 {
55 HTTP_LONG_CONSTANT("HTTP_URL_REPLACE", HTTP_URL_REPLACE);
56 HTTP_LONG_CONSTANT("HTTP_URL_JOIN_PATH", HTTP_URL_JOIN_PATH);
57 HTTP_LONG_CONSTANT("HTTP_URL_JOIN_QUERY", HTTP_URL_JOIN_QUERY);
58 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_USER", HTTP_URL_STRIP_USER);
59 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PASS", HTTP_URL_STRIP_PASS);
60 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_AUTH", HTTP_URL_STRIP_AUTH);
61 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PORT", HTTP_URL_STRIP_PORT);
62 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_PATH", HTTP_URL_STRIP_PATH);
63 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_QUERY", HTTP_URL_STRIP_QUERY);
64 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_FRAGMENT", HTTP_URL_STRIP_FRAGMENT);
65 HTTP_LONG_CONSTANT("HTTP_URL_STRIP_ALL", HTTP_URL_STRIP_ALL);
66 return SUCCESS;
67 }
68
69 PHP_HTTP_API char *_http_absolute_url(const char *url TSRMLS_DC)
70 {
71 char *abs = NULL;
72 php_url *purl = NULL;
73
74 if (url) {
75 purl = php_url_parse(abs = estrdup(url));
76 STR_SET(abs, NULL);
77 if (!purl) {
78 http_error_ex(HE_WARNING, HTTP_E_URL, "Could not parse URL (%s)", url);
79 return NULL;
80 }
81 }
82
83 http_build_url(0, purl, NULL, NULL, &abs, NULL);
84
85 if (purl) {
86 php_url_free(purl);
87 }
88
89 return abs;
90 }
91
92 /* {{{ void http_build_url(int flags, const php_url *, const php_url *, php_url **, char **, size_t *) */
93 PHP_HTTP_API void _http_build_url(int flags, const php_url *old_url, const php_url *new_url, php_url **url_ptr, char **url_str, size_t *url_len TSRMLS_DC)
94 {
95 #ifdef HTTP_HAVE_NETDB
96 struct servent *se;
97 #endif
98 php_url *url = ecalloc(1, sizeof(php_url));
99
100 #define __URLSET(u,n) \
101 ((u)&&(u)->n)
102 #define __URLCPY(n) \
103 url->n = __URLSET(new_url,n) ? estrdup(new_url->n) : (__URLSET(old_url,n) ? estrdup(old_url->n) : NULL)
104
105 if (!(flags & HTTP_URL_STRIP_PORT)) {
106 url->port = (new_url&&new_url->port) ? new_url->port : ((old_url) ? old_url->port : 0);
107 }
108 if (!(flags & HTTP_URL_STRIP_USER)) {
109 __URLCPY(user);
110 }
111 if (!(flags & HTTP_URL_STRIP_PASS)) {
112 __URLCPY(pass);
113 }
114
115 __URLCPY(scheme);
116 __URLCPY(host);
117
118 if (!(flags & HTTP_URL_STRIP_PATH)) {
119 if ((flags & HTTP_URL_JOIN_PATH) && __URLSET(old_url, path) && __URLSET(new_url, path) && *new_url->path != '/') {
120 size_t old_path_len = strlen(old_url->path), new_path_len = strlen(new_url->path);
121
122 url->path = ecalloc(1, old_path_len + new_path_len + 1 + 1);
123
124 strcat(url->path, old_url->path);
125 if (url->path[old_path_len - 1] != '/') {
126 php_dirname(url->path, old_path_len);
127 strcat(url->path, "/");
128 }
129 strcat(url->path, new_url->path);
130 } else {
131 __URLCPY(path);
132 }
133 }
134 if (!(flags & HTTP_URL_STRIP_QUERY)) {
135 if ((flags & HTTP_URL_JOIN_QUERY) && __URLSET(new_url, query) && __URLSET(old_url, query)) {
136 zval qarr, qstr;
137
138 INIT_PZVAL(&qstr);
139 INIT_PZVAL(&qarr);
140 array_init(&qarr);
141
142 ZVAL_STRING(&qstr, old_url->query, 0);
143 http_querystring_modify(&qarr, &qstr);
144 ZVAL_STRING(&qstr, new_url->query, 0);
145 http_querystring_modify(&qarr, &qstr);
146
147 ZVAL_NULL(&qstr);
148 http_querystring_update(&qarr, &qstr);
149 url->query = Z_STRVAL(qstr);
150 zval_dtor(&qarr);
151 } else {
152 __URLCPY(query);
153 }
154 }
155 if (!(flags & HTTP_URL_STRIP_FRAGMENT)) {
156 __URLCPY(fragment);
157 }
158
159 if (!url->scheme) {
160 zval *https = http_get_server_var("HTTPS");
161 if (https && !strcasecmp(Z_STRVAL_P(https), "ON")) {
162 url->scheme = estrndup("https", lenof("https"));
163 } else switch (url->port) {
164 case 443:
165 url->scheme = estrndup("https", lenof("https"));
166 break;
167
168 #ifndef HTTP_HAVE_NETDB
169 default:
170 #endif
171 case 80:
172 url->scheme = estrndup("http", lenof("http"));
173 break;
174
175 #ifdef HTTP_HAVE_NETDB
176 default:
177 if ((se = getservbyport(htons(url->port), "tcp")) && se->s_name) {
178 url->scheme = estrdup(se->s_name);
179 } else {
180 url->scheme = estrndup("http", lenof("http"));
181 }
182 break;
183 #endif
184 }
185 }
186
187 if (!url->host) {
188 zval *zhost;
189
190 if ((((zhost = http_get_server_var("HTTP_HOST")) ||
191 (zhost = http_get_server_var("SERVER_NAME")))) && Z_STRLEN_P(zhost)) {
192 url->host = estrndup(Z_STRVAL_P(zhost), Z_STRLEN_P(zhost));
193 } else {
194 url->host = localhostname();
195 }
196 }
197
198 if (!url->path) {
199 if (SG(request_info).request_uri && SG(request_info).request_uri[0]) {
200 const char *q = strchr(SG(request_info).request_uri, '?');
201
202 if (q) {
203 url->path = estrndup(SG(request_info).request_uri, q - SG(request_info).request_uri);
204 } else {
205 url->path = estrdup(SG(request_info).request_uri);
206 }
207 } else {
208 url->path = estrndup("/", 1);
209 }
210 } else if (url->path[0] != '/') {
211 if (SG(request_info).request_uri && SG(request_info).request_uri[0]) {
212 size_t ulen = strlen(SG(request_info).request_uri);
213 size_t plen = strlen(url->path);
214 char *path;
215
216 if (SG(request_info).request_uri[ulen-1] != '/') {
217 for (--ulen; ulen && SG(request_info).request_uri[ulen - 1] != '/'; --ulen);
218 }
219
220 path = emalloc(ulen + plen + 1);
221 memcpy(path, SG(request_info).request_uri, ulen);
222 memcpy(path + ulen, url->path, plen);
223 path[ulen + plen] = '\0';
224 STR_SET(url->path, path);
225 } else {
226 size_t plen = strlen(url->path);
227 char *path = emalloc(plen + 1 + 1);
228
229 path[0] = '/';
230 memcpy(&path[1], url->path, plen + 1);
231 STR_SET(url->path, path);
232 }
233 }
234 /* replace directory references if path is not a single slash */
235 if (url->path[0] && (url->path[0] != '/' || url->path[1])) {
236 char *ptr, *end = url->path + strlen(url->path) + 1;
237
238 for (ptr = strstr(url->path, "/."); ptr; ptr = strstr(ptr, "/.")) {
239 switch (ptr[2]) {
240 case '\0':
241 ptr[1] = '\0';
242 break;
243
244 case '/':
245 memmove(&ptr[1], &ptr[3], end - &ptr[3]);
246 break;
247
248 case '.':
249 if (ptr[3] == '/') {
250 char *pos = &ptr[4];
251 while (ptr != url->path) {
252 if (*--ptr == '/') {
253 break;
254 }
255 }
256 memmove(&ptr[1], pos, end - pos);
257 }
258 break;
259
260 default:
261 /* something else */
262 ++ptr;
263 break;
264 }
265 }
266 }
267
268 if (url->port) {
269 if ( ((url->port == 80) && !strcmp(url->scheme, "http"))
270 || ((url->port ==443) && !strcmp(url->scheme, "https"))
271 #ifdef HTTP_HAVE_NETDB
272 || ((se = getservbyname(url->scheme, "tcp")) && se->s_port &&
273 (url->port == ntohs(se->s_port)))
274 #endif
275 ) {
276 url->port = 0;
277 }
278 }
279
280 if (url_str) {
281 size_t len;
282
283 *url_str = emalloc(HTTP_URL_MAXLEN + 1);
284
285 **url_str = '\0';
286 strlcat(*url_str, url->scheme, HTTP_URL_MAXLEN);
287 strlcat(*url_str, "://", HTTP_URL_MAXLEN);
288
289 if (url->user && *url->user) {
290 strlcat(*url_str, url->user, HTTP_URL_MAXLEN);
291 if (url->pass && *url->pass) {
292 strlcat(*url_str, ":", HTTP_URL_MAXLEN);
293 strlcat(*url_str, url->pass, HTTP_URL_MAXLEN);
294 }
295 strlcat(*url_str, "@", HTTP_URL_MAXLEN);
296 }
297
298 strlcat(*url_str, url->host, HTTP_URL_MAXLEN);
299
300 if (url->port) {
301 char port_str[8] = {0};
302
303 snprintf(port_str, lenof(port_str), "%d", (int) url->port);
304 strlcat(*url_str, ":", HTTP_URL_MAXLEN);
305 strlcat(*url_str, port_str, HTTP_URL_MAXLEN);
306 }
307
308 strlcat(*url_str, url->path, HTTP_URL_MAXLEN);
309
310 if (url->query && *url->query) {
311 strlcat(*url_str, "?", HTTP_URL_MAXLEN);
312 strlcat(*url_str, url->query, HTTP_URL_MAXLEN);
313 }
314
315 if (url->fragment && *url->fragment) {
316 strlcat(*url_str, "#", HTTP_URL_MAXLEN);
317 strlcat(*url_str, url->fragment, HTTP_URL_MAXLEN);
318 }
319
320 if (HTTP_URL_MAXLEN == (len = strlen(*url_str))) {
321 http_error(HE_NOTICE, HTTP_E_URL, "Length of URL exceeds HTTP_URL_MAXLEN");
322 }
323 if (url_len) {
324 *url_len = len;
325 }
326 }
327
328 if (url_ptr) {
329 *url_ptr = url;
330 } else {
331 php_url_free(url);
332 }
333 }
334 /* }}} */
335
336 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
337 PHP_HTTP_API STATUS _http_urlencode_hash_ex(HashTable *hash, zend_bool override_argsep,
338 char *pre_encoded_data, size_t pre_encoded_len,
339 char **encoded_data, size_t *encoded_len TSRMLS_DC)
340 {
341 char *arg_sep;
342 size_t arg_sep_len;
343 phpstr *qstr = phpstr_new();
344
345 if (override_argsep || !(arg_sep_len = strlen(arg_sep = INI_STR("arg_separator.output")))) {
346 arg_sep = HTTP_URL_ARGSEP;
347 arg_sep_len = lenof(HTTP_URL_ARGSEP);
348 }
349
350 if (pre_encoded_len && pre_encoded_data) {
351 phpstr_append(qstr, pre_encoded_data, pre_encoded_len);
352 }
353
354 if (SUCCESS != http_urlencode_hash_recursive(hash, qstr, arg_sep, arg_sep_len, NULL, 0)) {
355 phpstr_free(&qstr);
356 return FAILURE;
357 }
358
359 phpstr_data(qstr, encoded_data, encoded_len);
360 phpstr_free(&qstr);
361
362 return SUCCESS;
363 }
364 /* }}} */
365
366 /* {{{ http_urlencode_hash_recursive */
367 PHP_HTTP_API STATUS _http_urlencode_hash_recursive(HashTable *ht, phpstr *str, const char *arg_sep, size_t arg_sep_len, const char *prefix, size_t prefix_len TSRMLS_DC)
368 {
369 char *key = NULL;
370 uint len = 0;
371 ulong idx = 0;
372 zval **data = NULL;
373 HashPosition pos;
374
375 if (!ht || !str) {
376 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid parameters");
377 return FAILURE;
378 }
379 if (ht->nApplyCount > 0) {
380 return SUCCESS;
381 }
382
383 FOREACH_HASH_KEYLENVAL(pos, ht, key, len, idx, data) {
384 char *encoded_key;
385 int encoded_len;
386 phpstr new_prefix;
387
388 if (!data || !*data) {
389 phpstr_dtor(str);
390 return FAILURE;
391 }
392
393 if (key) {
394 if (!*key) {
395 /* only public properties */
396 continue;
397 }
398 if (len && key[len - 1] == '\0') {
399 --len;
400 }
401 encoded_key = php_url_encode(key, len, &encoded_len);
402 key = NULL;
403 } else {
404 encoded_len = spprintf(&encoded_key, 0, "%ld", idx);
405 }
406
407 {
408 phpstr_init(&new_prefix);
409 if (prefix && prefix_len) {
410 phpstr_append(&new_prefix, prefix, prefix_len);
411 phpstr_appends(&new_prefix, "%5B");
412 }
413
414 phpstr_append(&new_prefix, encoded_key, encoded_len);
415 efree(encoded_key);
416
417 if (prefix && prefix_len) {
418 phpstr_appends(&new_prefix, "%5D");
419 }
420 phpstr_fix(&new_prefix);
421 }
422
423 if (Z_TYPE_PP(data) == IS_ARRAY || Z_TYPE_PP(data) == IS_OBJECT) {
424 STATUS status;
425 ++ht->nApplyCount;
426 status = http_urlencode_hash_recursive(HASH_OF(*data), str, arg_sep, arg_sep_len, PHPSTR_VAL(&new_prefix), PHPSTR_LEN(&new_prefix));
427 --ht->nApplyCount;
428 if (SUCCESS != status) {
429 phpstr_dtor(&new_prefix);
430 phpstr_dtor(str);
431 return FAILURE;
432 }
433 } else {
434 zval *val = zval_copy(IS_STRING, *data);
435
436 if (PHPSTR_LEN(str)) {
437 phpstr_append(str, arg_sep, arg_sep_len);
438 }
439 phpstr_append(str, PHPSTR_VAL(&new_prefix), PHPSTR_LEN(&new_prefix));
440 phpstr_appends(str, "=");
441
442 if (Z_STRLEN_P(val) && Z_STRVAL_P(val)) {
443 char *encoded_val;
444 int encoded_len;
445
446 encoded_val = php_url_encode(Z_STRVAL_P(val), Z_STRLEN_P(val), &encoded_len);
447 phpstr_append(str, encoded_val, encoded_len);
448 efree(encoded_val);
449 }
450
451 zval_free(&val);
452 }
453 phpstr_dtor(&new_prefix);
454 }
455 return SUCCESS;
456 }
457 /* }}} */
458
459 /*
460 * Local variables:
461 * tab-width: 4
462 * c-basic-offset: 4
463 * End:
464 * vim600: noet sw=4 ts=4 fdm=marker
465 * vim<600: noet sw=4 ts=4
466 */
467