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