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