- add HTTP_URL_STRIP_ALL constant
[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_AUTH)) && (!(flags & HTTP_URL_STRIP_USER))) {
109 __URLCPY(user);
110 }
111 if ((!(flags & HTTP_URL_STRIP_AUTH)) && (!(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
164 switch (url->port)
165 {
166 case 443:
167 url->scheme = estrndup("https", lenof("https"));
168 break;
169
170 #ifndef HTTP_HAVE_NETDB
171 default:
172 #endif
173 case 80:
174 url->scheme = estrndup("http", lenof("http"));
175 break;
176
177 #ifdef HTTP_HAVE_NETDB
178 default:
179 if ((se = getservbyport(htons(url->port), "tcp")) && se->s_name) {
180 url->scheme = estrdup(se->s_name);
181 } else {
182 url->scheme = estrndup("http", lenof("http"));
183 }
184 break;
185 #endif
186 }
187 }
188
189 if (!url->host) {
190 zval *zhost;
191
192 if ((((zhost = http_get_server_var("HTTP_HOST")) ||
193 (zhost = http_get_server_var("SERVER_NAME")))) && Z_STRLEN_P(zhost)) {
194 url->host = estrndup(Z_STRVAL_P(zhost), Z_STRLEN_P(zhost));
195 } else {
196 url->host = localhostname();
197 }
198 }
199
200 if (!url->path) {
201 if (SG(request_info).request_uri && SG(request_info).request_uri[0]) {
202 const char *q = strchr(SG(request_info).request_uri, '?');
203
204 if (q) {
205 url->path = estrndup(SG(request_info).request_uri, q - SG(request_info).request_uri);
206 } else {
207 url->path = estrdup(SG(request_info).request_uri);
208 }
209 } else {
210 url->path = estrndup("/", 1);
211 }
212 } else if (url->path[0] != '/' && SG(request_info).request_uri && SG(request_info).request_uri[0]) {
213 size_t ulen = strlen(SG(request_info).request_uri);
214 size_t plen = strlen(url->path);
215 char *path;
216
217 if (SG(request_info).request_uri[ulen-1] != '/') {
218 for (--ulen; ulen && SG(request_info).request_uri[ulen - 1] != '/'; --ulen);
219 }
220
221 path = emalloc(ulen + plen + 1);
222 memcpy(path, SG(request_info).request_uri, ulen);
223 memcpy(path + ulen, url->path, plen);
224 path[ulen + plen] = '\0';
225 STR_SET(url->path, path);
226 }
227 /* replace directory references if path is not a single slash */
228 if (url->path[0] && (url->path[0] != '/' || url->path[1])) {
229 char *ptr, *end = url->path + strlen(url->path) + 1;
230
231 for (ptr = strstr(url->path, "/."); ptr; ptr = strstr(ptr, "/.")) {
232 switch (ptr[2])
233 {
234 case '\0':
235 ptr[1] = '\0';
236 break;
237
238 case '/':
239 memmove(&ptr[1], &ptr[3], end - &ptr[3]);
240 break;
241
242 case '.':
243 if (ptr[3] == '/') {
244 char *pos = &ptr[4];
245 while (ptr != url->path) {
246 if (*--ptr == '/') {
247 break;
248 }
249 }
250 memmove(&ptr[1], pos, end - pos);
251 }
252 break;
253
254 default:
255 /* something else */
256 ++ptr;
257 break;
258 }
259 }
260 }
261
262 if (url->port) {
263 if ( ((url->port == 80) && !strcmp(url->scheme, "http"))
264 || ((url->port ==443) && !strcmp(url->scheme, "https"))
265 #ifdef HTTP_HAVE_NETDB
266 || ((se = getservbyname(url->scheme, "tcp")) && se->s_port &&
267 (url->port == ntohs(se->s_port)))
268 #endif
269 ) {
270 url->port = 0;
271 }
272 }
273
274 if (url_str) {
275 size_t len;
276
277 *url_str = emalloc(HTTP_URL_MAXLEN + 1);
278
279 **url_str = '\0';
280 strlcat(*url_str, url->scheme, HTTP_URL_MAXLEN);
281 strlcat(*url_str, "://", HTTP_URL_MAXLEN);
282
283 if (url->user && *url->user) {
284 strlcat(*url_str, url->user, HTTP_URL_MAXLEN);
285 if (url->pass && *url->pass) {
286 strlcat(*url_str, ":", HTTP_URL_MAXLEN);
287 strlcat(*url_str, url->pass, HTTP_URL_MAXLEN);
288 }
289 strlcat(*url_str, "@", HTTP_URL_MAXLEN);
290 }
291
292 strlcat(*url_str, url->host, HTTP_URL_MAXLEN);
293
294 if (url->port) {
295 char port_str[8] = {0};
296
297 snprintf(port_str, lenof(port_str), "%d", (int) url->port);
298 strlcat(*url_str, ":", HTTP_URL_MAXLEN);
299 strlcat(*url_str, port_str, HTTP_URL_MAXLEN);
300 }
301
302 if (*url->path != '/') {
303 strlcat(*url_str, "/", HTTP_URL_MAXLEN);
304 }
305 strlcat(*url_str, url->path, HTTP_URL_MAXLEN);
306
307 if (url->query && *url->query) {
308 strlcat(*url_str, "?", HTTP_URL_MAXLEN);
309 strlcat(*url_str, url->query, HTTP_URL_MAXLEN);
310 }
311
312 if (url->fragment && *url->fragment) {
313 strlcat(*url_str, "#", HTTP_URL_MAXLEN);
314 strlcat(*url_str, url->fragment, HTTP_URL_MAXLEN);
315 }
316
317 if (HTTP_URL_MAXLEN == (len = strlen(*url_str))) {
318 http_error(HE_NOTICE, HTTP_E_URL, "Length of URL exceeds HTTP_URL_MAXLEN");
319 }
320 if (url_len) {
321 *url_len = len;
322 }
323 }
324
325 if (url_ptr) {
326 *url_ptr = url;
327 } else {
328 php_url_free(url);
329 }
330 }
331 /* }}} */
332
333 /* {{{ STATUS http_urlencode_hash_ex(HashTable *, zend_bool, char *, size_t, char **, size_t *) */
334 PHP_HTTP_API STATUS _http_urlencode_hash_ex(HashTable *hash, zend_bool override_argsep,
335 char *pre_encoded_data, size_t pre_encoded_len,
336 char **encoded_data, size_t *encoded_len TSRMLS_DC)
337 {
338 char *arg_sep;
339 size_t arg_sep_len;
340 phpstr *qstr = phpstr_new();
341
342 if (override_argsep || !(arg_sep_len = strlen(arg_sep = INI_STR("arg_separator.output")))) {
343 arg_sep = HTTP_URL_ARGSEP;
344 arg_sep_len = lenof(HTTP_URL_ARGSEP);
345 }
346
347 if (pre_encoded_len && pre_encoded_data) {
348 phpstr_append(qstr, pre_encoded_data, pre_encoded_len);
349 }
350
351 if (SUCCESS != http_urlencode_hash_recursive(hash, qstr, arg_sep, arg_sep_len, NULL, 0)) {
352 phpstr_free(&qstr);
353 return FAILURE;
354 }
355
356 phpstr_data(qstr, encoded_data, encoded_len);
357 phpstr_free(&qstr);
358
359 return SUCCESS;
360 }
361 /* }}} */
362
363 /* {{{ http_urlencode_hash_recursive */
364 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)
365 {
366 char *key = NULL;
367 uint len = 0;
368 ulong idx = 0;
369 zval **data = NULL;
370 HashPosition pos;
371
372 if (!ht || !str) {
373 http_error(HE_WARNING, HTTP_E_INVALID_PARAM, "Invalid parameters");
374 return FAILURE;
375 }
376 if (ht->nApplyCount > 0) {
377 return SUCCESS;
378 }
379
380 FOREACH_HASH_KEYLENVAL(pos, ht, key, len, idx, data) {
381 char *encoded_key;
382 int encoded_len;
383 phpstr new_prefix;
384
385 if (!data || !*data) {
386 phpstr_dtor(str);
387 return FAILURE;
388 }
389
390 if (key) {
391 if (len && key[len - 1] == '\0') {
392 --len;
393 }
394 encoded_key = php_url_encode(key, len, &encoded_len);
395 key = NULL;
396 } else {
397 encoded_len = spprintf(&encoded_key, 0, "%ld", idx);
398 }
399
400 {
401 phpstr_init(&new_prefix);
402 if (prefix && prefix_len) {
403 phpstr_append(&new_prefix, prefix, prefix_len);
404 phpstr_appends(&new_prefix, "%5B");
405 }
406
407 phpstr_append(&new_prefix, encoded_key, encoded_len);
408 efree(encoded_key);
409
410 if (prefix && prefix_len) {
411 phpstr_appends(&new_prefix, "%5D");
412 }
413 phpstr_fix(&new_prefix);
414 }
415
416 if (Z_TYPE_PP(data) == IS_ARRAY) {
417 STATUS status;
418 ++ht->nApplyCount;
419 status = http_urlencode_hash_recursive(Z_ARRVAL_PP(data), str, arg_sep, arg_sep_len, PHPSTR_VAL(&new_prefix), PHPSTR_LEN(&new_prefix));
420 --ht->nApplyCount;
421 if (SUCCESS != status) {
422 phpstr_dtor(&new_prefix);
423 phpstr_dtor(str);
424 return FAILURE;
425 }
426 } else {
427 zval *val = zval_copy(IS_STRING, *data);
428
429 if (PHPSTR_LEN(str)) {
430 phpstr_append(str, arg_sep, arg_sep_len);
431 }
432 phpstr_append(str, PHPSTR_VAL(&new_prefix), PHPSTR_LEN(&new_prefix));
433 phpstr_appends(str, "=");
434
435 if (Z_STRLEN_P(val) && Z_STRVAL_P(val)) {
436 char *encoded_val;
437 int encoded_len;
438
439 encoded_val = php_url_encode(Z_STRVAL_P(val), Z_STRLEN_P(val), &encoded_len);
440 phpstr_append(str, encoded_val, encoded_len);
441 efree(encoded_val);
442 }
443
444 zval_free(&val);
445 }
446 phpstr_dtor(&new_prefix);
447 }
448 return SUCCESS;
449 }
450 /* }}} */
451
452 /*
453 * Local variables:
454 * tab-width: 4
455 * c-basic-offset: 4
456 * End:
457 * vim600: noet sw=4 ts=4 fdm=marker
458 * vim<600: noet sw=4 ts=4
459 */
460