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