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