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