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