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