xref: /PHP-7.2/ext/standard/url.c (revision 2d3d7241)
1 /*
2    +----------------------------------------------------------------------+
3    | PHP Version 7                                                        |
4    +----------------------------------------------------------------------+
5    | Copyright (c) 1997-2018 The PHP Group                                |
6    +----------------------------------------------------------------------+
7    | This source file is subject to version 3.01 of the PHP license,      |
8    | that is bundled with this package in the file LICENSE, and is        |
9    | available through the world-wide-web at the following url:           |
10    | http://www.php.net/license/3_01.txt                                  |
11    | If you did not receive a copy of the PHP license and are unable to   |
12    | obtain it through the world-wide-web, please send a note to          |
13    | license@php.net so we can mail you a copy immediately.               |
14    +----------------------------------------------------------------------+
15    | Author: Jim Winstead <jimw@php.net>                                  |
16    +----------------------------------------------------------------------+
17  */
18 /* $Id$ */
19 
20 #include <stdlib.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <sys/types.h>
24 
25 #include "php.h"
26 
27 #include "url.h"
28 #include "file.h"
29 #ifdef _OSD_POSIX
30 #ifndef APACHE
31 #error On this EBCDIC platform, PHP is only supported as an Apache module.
32 #else /*APACHE*/
33 #ifndef CHARSET_EBCDIC
34 #define CHARSET_EBCDIC /* this machine uses EBCDIC, not ASCII! */
35 #endif
36 #include "ebcdic.h"
37 #endif /*APACHE*/
38 #endif /*_OSD_POSIX*/
39 
40 /* {{{ free_url
41  */
php_url_free(php_url * theurl)42 PHPAPI void php_url_free(php_url *theurl)
43 {
44 	if (theurl->scheme)
45 		efree(theurl->scheme);
46 	if (theurl->user)
47 		efree(theurl->user);
48 	if (theurl->pass)
49 		efree(theurl->pass);
50 	if (theurl->host)
51 		efree(theurl->host);
52 	if (theurl->path)
53 		efree(theurl->path);
54 	if (theurl->query)
55 		efree(theurl->query);
56 	if (theurl->fragment)
57 		efree(theurl->fragment);
58 	efree(theurl);
59 }
60 /* }}} */
61 
62 /* {{{ php_replace_controlchars
63  */
php_replace_controlchars_ex(char * str,size_t len)64 PHPAPI char *php_replace_controlchars_ex(char *str, size_t len)
65 {
66 	unsigned char *s = (unsigned char *)str;
67 	unsigned char *e = (unsigned char *)str + len;
68 
69 	if (!str) {
70 		return (NULL);
71 	}
72 
73 	while (s < e) {
74 
75 		if (iscntrl(*s)) {
76 			*s='_';
77 		}
78 		s++;
79 	}
80 
81 	return (str);
82 }
83 /* }}} */
84 
php_replace_controlchars(char * str)85 PHPAPI char *php_replace_controlchars(char *str)
86 {
87 	return php_replace_controlchars_ex(str, strlen(str));
88 }
89 
php_url_parse(char const * str)90 PHPAPI php_url *php_url_parse(char const *str)
91 {
92 	return php_url_parse_ex(str, strlen(str));
93 }
94 
is_userinfo_valid(const char * str,size_t len)95 static int is_userinfo_valid(const char *str, size_t len)
96 {
97 	char *valid = "-._~!$&'()*+,;=:";
98 	char *p = str;
99 	while (p - str < len) {
100 		if (isalpha(*p) || isdigit(*p) || strchr(valid, *p)) {
101 			p++;
102 		} else if (*p == '%' && p - str <= len - 3 && isdigit(*(p+1)) && isxdigit(*(p+2))) {
103 			p += 3;
104 		} else {
105 			return 0;
106 		}
107 	}
108 	return 1;
109 }
110 
111 /* {{{ php_url_parse
112  */
php_url_parse_ex(char const * str,size_t length)113 PHPAPI php_url *php_url_parse_ex(char const *str, size_t length)
114 {
115 	char port_buf[6];
116 	php_url *ret = ecalloc(1, sizeof(php_url));
117 	char const *s, *e, *p, *pp, *ue;
118 
119 	s = str;
120 	ue = s + length;
121 
122 	/* parse scheme */
123 	if ((e = memchr(s, ':', length)) && e != s) {
124 		/* validate scheme */
125 		p = s;
126 		while (p < e) {
127 			/* scheme = 1*[ lowalpha | digit | "+" | "-" | "." ] */
128 			if (!isalpha(*p) && !isdigit(*p) && *p != '+' && *p != '.' && *p != '-') {
129 				if (e + 1 < ue && e < s + strcspn(s, "?#")) {
130 					goto parse_port;
131 				} else if (s + 1 < ue && *s == '/' && *(s + 1) == '/') { /* relative-scheme URL */
132 					s += 2;
133 					e = 0;
134 					goto parse_host;
135 				} else {
136 					goto just_path;
137 				}
138 			}
139 			p++;
140 		}
141 
142 		if (e + 1 == ue) { /* only scheme is available */
143 			ret->scheme = estrndup(s, (e - s));
144 			php_replace_controlchars_ex(ret->scheme, (e - s));
145 			return ret;
146 		}
147 
148 		/*
149 		 * certain schemas like mailto: and zlib: may not have any / after them
150 		 * this check ensures we support those.
151 		 */
152 		if (*(e+1) != '/') {
153 			/* check if the data we get is a port this allows us to
154 			 * correctly parse things like a.com:80
155 			 */
156 			p = e + 1;
157 			while (p < ue && isdigit(*p)) {
158 				p++;
159 			}
160 
161 			if ((p == ue || *p == '/') && (p - e) < 7) {
162 				goto parse_port;
163 			}
164 
165 			ret->scheme = estrndup(s, (e-s));
166 			php_replace_controlchars_ex(ret->scheme, (e - s));
167 
168 			s = e + 1;
169 			goto just_path;
170 		} else {
171 			ret->scheme = estrndup(s, (e-s));
172 			php_replace_controlchars_ex(ret->scheme, (e - s));
173 
174 			if (e + 2 < ue && *(e + 2) == '/') {
175 				s = e + 3;
176 				if (!strncasecmp("file", ret->scheme, sizeof("file"))) {
177 					if (e + 3 < ue && *(e + 3) == '/') {
178 						/* support windows drive letters as in:
179 						   file:///c:/somedir/file.txt
180 						*/
181 						if (e + 5 < ue && *(e + 5) == ':') {
182 							s = e + 4;
183 						}
184 						goto just_path;
185 					}
186 				}
187 			} else {
188 				s = e + 1;
189 				goto just_path;
190 			}
191 		}
192 	} else if (e) { /* no scheme; starts with colon: look for port */
193 		parse_port:
194 		p = e + 1;
195 		pp = p;
196 
197 		while (pp < ue && pp - p < 6 && isdigit(*pp)) {
198 			pp++;
199 		}
200 
201 		if (pp - p > 0 && pp - p < 6 && (pp == ue || *pp == '/')) {
202 			zend_long port;
203 			memcpy(port_buf, p, (pp - p));
204 			port_buf[pp - p] = '\0';
205 			port = ZEND_STRTOL(port_buf, NULL, 10);
206 			if (port > 0 && port <= 65535) {
207 				ret->port = (unsigned short) port;
208 				if (s + 1 < ue && *s == '/' && *(s + 1) == '/') { /* relative-scheme URL */
209 				    s += 2;
210 				}
211 			} else {
212 				if (ret->scheme) efree(ret->scheme);
213 				efree(ret);
214 				return NULL;
215 			}
216 		} else if (p == pp && pp == ue) {
217 			if (ret->scheme) efree(ret->scheme);
218 			efree(ret);
219 			return NULL;
220 		} else if (s + 1 < ue && *s == '/' && *(s + 1) == '/') { /* relative-scheme URL */
221 			s += 2;
222 		} else {
223 			goto just_path;
224 		}
225 	} else if (s + 1 < ue && *s == '/' && *(s + 1) == '/') { /* relative-scheme URL */
226 		s += 2;
227 	} else {
228 		goto just_path;
229 	}
230 
231 	parse_host:
232 	/* Binary-safe strcspn(s, "/?#") */
233 	e = ue;
234 	if ((p = memchr(s, '/', e - s))) {
235 		e = p;
236 	}
237 	if ((p = memchr(s, '?', e - s))) {
238 		e = p;
239 	}
240 	if ((p = memchr(s, '#', e - s))) {
241 		e = p;
242 	}
243 
244 	/* check for login and password */
245 	if ((p = zend_memrchr(s, '@', (e-s)))) {
246 		if ((pp = memchr(s, ':', (p-s)))) {
247 			ret->user = estrndup(s, (pp-s));
248 			php_replace_controlchars_ex(ret->user, (pp - s));
249 
250 			pp++;
251 			ret->pass = estrndup(pp, (p-pp));
252 			php_replace_controlchars_ex(ret->pass, (p-pp));
253 		} else {
254 			if (!is_userinfo_valid(s, p-s)) {
255 				goto check_port;
256 			}
257 			ret->user = estrndup(s, (p-s));
258 			php_replace_controlchars_ex(ret->user, (p-s));
259 
260 		}
261 
262 		s = p + 1;
263 	}
264 
265 check_port:
266 	/* check for port */
267 	if (s < ue && *s == '[' && *(e-1) == ']') {
268 		/* Short circuit portscan,
269 		   we're dealing with an
270 		   IPv6 embedded address */
271 		p = NULL;
272 	} else {
273 		p = zend_memrchr(s, ':', (e-s));
274 	}
275 
276 	if (p) {
277 		if (!ret->port) {
278 			p++;
279 			if (e-p > 5) { /* port cannot be longer then 5 characters */
280 				if (ret->scheme) efree(ret->scheme);
281 				if (ret->user) efree(ret->user);
282 				if (ret->pass) efree(ret->pass);
283 				efree(ret);
284 				return NULL;
285 			} else if (e - p > 0) {
286 				zend_long port;
287 				memcpy(port_buf, p, (e - p));
288 				port_buf[e - p] = '\0';
289 				port = ZEND_STRTOL(port_buf, NULL, 10);
290 				if (port > 0 && port <= 65535) {
291 					ret->port = (unsigned short)port;
292 				} else {
293 					if (ret->scheme) efree(ret->scheme);
294 					if (ret->user) efree(ret->user);
295 					if (ret->pass) efree(ret->pass);
296 					efree(ret);
297 					return NULL;
298 				}
299 			}
300 			p--;
301 		}
302 	} else {
303 		p = e;
304 	}
305 
306 	/* check if we have a valid host, if we don't reject the string as url */
307 	if ((p-s) < 1) {
308 		if (ret->scheme) efree(ret->scheme);
309 		if (ret->user) efree(ret->user);
310 		if (ret->pass) efree(ret->pass);
311 		efree(ret);
312 		return NULL;
313 	}
314 
315 	ret->host = estrndup(s, (p-s));
316 	php_replace_controlchars_ex(ret->host, (p - s));
317 
318 	if (e == ue) {
319 		return ret;
320 	}
321 
322 	s = e;
323 
324 	just_path:
325 
326 	e = ue;
327 	p = memchr(s, '#', (e - s));
328 	if (p) {
329 		p++;
330 		if (p < e) {
331 			ret->fragment = estrndup(p, (e - p));
332 			php_replace_controlchars_ex(ret->fragment, (e - p));
333 		}
334 		e = p-1;
335 	}
336 
337 	p = memchr(s, '?', (e - s));
338 	if (p) {
339 		p++;
340 		if (p < e) {
341 			ret->query = estrndup(p, (e - p));
342 			php_replace_controlchars_ex(ret->query, (e - p));
343 		}
344 		e = p-1;
345 	}
346 
347 	if (s < e || s == ue) {
348 		ret->path = estrndup(s, (e - s));
349 		php_replace_controlchars_ex(ret->path, (e - s));
350 	}
351 
352 	return ret;
353 }
354 /* }}} */
355 
356 /* {{{ proto mixed parse_url(string url, [int url_component])
357    Parse a URL and return its components */
PHP_FUNCTION(parse_url)358 PHP_FUNCTION(parse_url)
359 {
360 	char *str;
361 	size_t str_len;
362 	php_url *resource;
363 	zend_long key = -1;
364 	zval tmp;
365 
366 	ZEND_PARSE_PARAMETERS_START(1, 2)
367 		Z_PARAM_STRING(str, str_len)
368 		Z_PARAM_OPTIONAL
369 		Z_PARAM_LONG(key)
370 	ZEND_PARSE_PARAMETERS_END();
371 
372 	resource = php_url_parse_ex(str, str_len);
373 	if (resource == NULL) {
374 		/* @todo Find a method to determine why php_url_parse_ex() failed */
375 		RETURN_FALSE;
376 	}
377 
378 	if (key > -1) {
379 		switch (key) {
380 			case PHP_URL_SCHEME:
381 				if (resource->scheme != NULL) RETVAL_STRING(resource->scheme);
382 				break;
383 			case PHP_URL_HOST:
384 				if (resource->host != NULL) RETVAL_STRING(resource->host);
385 				break;
386 			case PHP_URL_PORT:
387 				if (resource->port != 0) RETVAL_LONG(resource->port);
388 				break;
389 			case PHP_URL_USER:
390 				if (resource->user != NULL) RETVAL_STRING(resource->user);
391 				break;
392 			case PHP_URL_PASS:
393 				if (resource->pass != NULL) RETVAL_STRING(resource->pass);
394 				break;
395 			case PHP_URL_PATH:
396 				if (resource->path != NULL) RETVAL_STRING(resource->path);
397 				break;
398 			case PHP_URL_QUERY:
399 				if (resource->query != NULL) RETVAL_STRING(resource->query);
400 				break;
401 			case PHP_URL_FRAGMENT:
402 				if (resource->fragment != NULL) RETVAL_STRING(resource->fragment);
403 				break;
404 			default:
405 				php_error_docref(NULL, E_WARNING, "Invalid URL component identifier " ZEND_LONG_FMT, key);
406 				RETVAL_FALSE;
407 		}
408 		goto done;
409 	}
410 
411 	/* allocate an array for return */
412 	array_init(return_value);
413 
414     /* add the various elements to the array */
415 	if (resource->scheme != NULL) {
416 		ZVAL_STRING(&tmp, resource->scheme);
417 		zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_SCHEME), &tmp);
418 	}
419 	if (resource->host != NULL) {
420 		ZVAL_STRING(&tmp, resource->host);
421 		zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_HOST), &tmp);
422 	}
423 	if (resource->port != 0) {
424 		ZVAL_LONG(&tmp, resource->port);
425 		zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_PORT), &tmp);
426 	}
427 	if (resource->user != NULL) {
428 		ZVAL_STRING(&tmp, resource->user);
429 		zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_USER), &tmp);
430 	}
431 	if (resource->pass != NULL) {
432 		ZVAL_STRING(&tmp, resource->pass);
433 		zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_PASS), &tmp);
434 	}
435 	if (resource->path != NULL) {
436 		ZVAL_STRING(&tmp, resource->path);
437 		zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_PATH), &tmp);
438 	}
439 	if (resource->query != NULL) {
440 		ZVAL_STRING(&tmp, resource->query);
441 		zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_QUERY), &tmp);
442 	}
443 	if (resource->fragment != NULL) {
444 		ZVAL_STRING(&tmp, resource->fragment);
445 		zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_FRAGMENT), &tmp);
446 	}
447 done:
448 	php_url_free(resource);
449 }
450 /* }}} */
451 
452 /* {{{ php_htoi
453  */
php_htoi(char * s)454 static int php_htoi(char *s)
455 {
456 	int value;
457 	int c;
458 
459 	c = ((unsigned char *)s)[0];
460 	if (isupper(c))
461 		c = tolower(c);
462 	value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
463 
464 	c = ((unsigned char *)s)[1];
465 	if (isupper(c))
466 		c = tolower(c);
467 	value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;
468 
469 	return (value);
470 }
471 /* }}} */
472 
473 /* rfc1738:
474 
475    ...The characters ";",
476    "/", "?", ":", "@", "=" and "&" are the characters which may be
477    reserved for special meaning within a scheme...
478 
479    ...Thus, only alphanumerics, the special characters "$-_.+!*'(),", and
480    reserved characters used for their reserved purposes may be used
481    unencoded within a URL...
482 
483    For added safety, we only leave -_. unencoded.
484  */
485 
486 static unsigned char hexchars[] = "0123456789ABCDEF";
487 
488 /* {{{ php_url_encode
489  */
php_url_encode(char const * s,size_t len)490 PHPAPI zend_string *php_url_encode(char const *s, size_t len)
491 {
492 	register unsigned char c;
493 	unsigned char *to;
494 	unsigned char const *from, *end;
495 	zend_string *start;
496 
497 	from = (unsigned char *)s;
498 	end = (unsigned char *)s + len;
499 	start = zend_string_safe_alloc(3, len, 0, 0);
500 	to = (unsigned char*)ZSTR_VAL(start);
501 
502 	while (from < end) {
503 		c = *from++;
504 
505 		if (c == ' ') {
506 			*to++ = '+';
507 #ifndef CHARSET_EBCDIC
508 		} else if ((c < '0' && c != '-' && c != '.') ||
509 				   (c < 'A' && c > '9') ||
510 				   (c > 'Z' && c < 'a' && c != '_') ||
511 				   (c > 'z')) {
512 			to[0] = '%';
513 			to[1] = hexchars[c >> 4];
514 			to[2] = hexchars[c & 15];
515 			to += 3;
516 #else /*CHARSET_EBCDIC*/
517 		} else if (!isalnum(c) && strchr("_-.", c) == NULL) {
518 			/* Allow only alphanumeric chars and '_', '-', '.'; escape the rest */
519 			to[0] = '%';
520 			to[1] = hexchars[os_toascii[c] >> 4];
521 			to[2] = hexchars[os_toascii[c] & 15];
522 			to += 3;
523 #endif /*CHARSET_EBCDIC*/
524 		} else {
525 			*to++ = c;
526 		}
527 	}
528 	*to = '\0';
529 
530 	start = zend_string_truncate(start, to - (unsigned char*)ZSTR_VAL(start), 0);
531 
532 	return start;
533 }
534 /* }}} */
535 
536 /* {{{ proto string urlencode(string str)
537    URL-encodes string */
PHP_FUNCTION(urlencode)538 PHP_FUNCTION(urlencode)
539 {
540 	zend_string *in_str;
541 
542 	ZEND_PARSE_PARAMETERS_START(1, 1)
543 		Z_PARAM_STR(in_str)
544 	ZEND_PARSE_PARAMETERS_END();
545 
546 	RETURN_STR(php_url_encode(ZSTR_VAL(in_str), ZSTR_LEN(in_str)));
547 }
548 /* }}} */
549 
550 /* {{{ proto string urldecode(string str)
551    Decodes URL-encoded string */
PHP_FUNCTION(urldecode)552 PHP_FUNCTION(urldecode)
553 {
554 	zend_string *in_str, *out_str;
555 
556 	ZEND_PARSE_PARAMETERS_START(1, 1)
557 		Z_PARAM_STR(in_str)
558 	ZEND_PARSE_PARAMETERS_END();
559 
560 	out_str = zend_string_init(ZSTR_VAL(in_str), ZSTR_LEN(in_str), 0);
561 	ZSTR_LEN(out_str) = php_url_decode(ZSTR_VAL(out_str), ZSTR_LEN(out_str));
562 
563     RETURN_NEW_STR(out_str);
564 }
565 /* }}} */
566 
567 /* {{{ php_url_decode
568  */
php_url_decode(char * str,size_t len)569 PHPAPI size_t php_url_decode(char *str, size_t len)
570 {
571 	char *dest = str;
572 	char *data = str;
573 
574 	while (len--) {
575 		if (*data == '+') {
576 			*dest = ' ';
577 		}
578 		else if (*data == '%' && len >= 2 && isxdigit((int) *(data + 1))
579 				 && isxdigit((int) *(data + 2))) {
580 #ifndef CHARSET_EBCDIC
581 			*dest = (char) php_htoi(data + 1);
582 #else
583 			*dest = os_toebcdic[(unsigned char) php_htoi(data + 1)];
584 #endif
585 			data += 2;
586 			len -= 2;
587 		} else {
588 			*dest = *data;
589 		}
590 		data++;
591 		dest++;
592 	}
593 	*dest = '\0';
594 	return dest - str;
595 }
596 /* }}} */
597 
598 /* {{{ php_raw_url_encode
599  */
php_raw_url_encode(char const * s,size_t len)600 PHPAPI zend_string *php_raw_url_encode(char const *s, size_t len)
601 {
602 	register size_t x, y;
603 	zend_string *str;
604 
605 	str = zend_string_safe_alloc(3, len, 0, 0);
606 	for (x = 0, y = 0; len--; x++, y++) {
607 		ZSTR_VAL(str)[y] = (unsigned char) s[x];
608 #ifndef CHARSET_EBCDIC
609 		if ((ZSTR_VAL(str)[y] < '0' && ZSTR_VAL(str)[y] != '-' && ZSTR_VAL(str)[y] != '.') ||
610 			(ZSTR_VAL(str)[y] < 'A' && ZSTR_VAL(str)[y] > '9') ||
611 			(ZSTR_VAL(str)[y] > 'Z' && ZSTR_VAL(str)[y] < 'a' && ZSTR_VAL(str)[y] != '_') ||
612 			(ZSTR_VAL(str)[y] > 'z' && ZSTR_VAL(str)[y] != '~')) {
613 			ZSTR_VAL(str)[y++] = '%';
614 			ZSTR_VAL(str)[y++] = hexchars[(unsigned char) s[x] >> 4];
615 			ZSTR_VAL(str)[y] = hexchars[(unsigned char) s[x] & 15];
616 #else /*CHARSET_EBCDIC*/
617 		if (!isalnum(ZSTR_VAL(str)[y]) && strchr("_-.~", ZSTR_VAL(str)[y]) != NULL) {
618 			ZSTR_VAL(str)[y++] = '%';
619 			ZSTR_VAL(str)[y++] = hexchars[os_toascii[(unsigned char) s[x]] >> 4];
620 			ZSTR_VAL(str)[y] = hexchars[os_toascii[(unsigned char) s[x]] & 15];
621 #endif /*CHARSET_EBCDIC*/
622 		}
623 	}
624 	ZSTR_VAL(str)[y] = '\0';
625 	str = zend_string_truncate(str, y, 0);
626 
627 	return str;
628 }
629 /* }}} */
630 
631 /* {{{ proto string rawurlencode(string str)
632    URL-encodes string */
633 PHP_FUNCTION(rawurlencode)
634 {
635 	zend_string *in_str;
636 
637 	ZEND_PARSE_PARAMETERS_START(1, 1)
638 		Z_PARAM_STR(in_str)
639 	ZEND_PARSE_PARAMETERS_END();
640 
641 	RETURN_STR(php_raw_url_encode(ZSTR_VAL(in_str), ZSTR_LEN(in_str)));
642 }
643 /* }}} */
644 
645 /* {{{ proto string rawurldecode(string str)
646    Decodes URL-encodes string */
647 PHP_FUNCTION(rawurldecode)
648 {
649 	zend_string *in_str, *out_str;
650 
651 	ZEND_PARSE_PARAMETERS_START(1, 1)
652 		Z_PARAM_STR(in_str)
653 	ZEND_PARSE_PARAMETERS_END();
654 
655 	out_str = zend_string_init(ZSTR_VAL(in_str), ZSTR_LEN(in_str), 0);
656 	ZSTR_LEN(out_str) = php_raw_url_decode(ZSTR_VAL(out_str), ZSTR_LEN(out_str));
657 
658     RETURN_NEW_STR(out_str);
659 }
660 /* }}} */
661 
662 /* {{{ php_raw_url_decode
663  */
664 PHPAPI size_t php_raw_url_decode(char *str, size_t len)
665 {
666 	char *dest = str;
667 	char *data = str;
668 
669 	while (len--) {
670 		if (*data == '%' && len >= 2 && isxdigit((int) *(data + 1))
671 			&& isxdigit((int) *(data + 2))) {
672 #ifndef CHARSET_EBCDIC
673 			*dest = (char) php_htoi(data + 1);
674 #else
675 			*dest = os_toebcdic[(unsigned char) php_htoi(data + 1)];
676 #endif
677 			data += 2;
678 			len -= 2;
679 		} else {
680 			*dest = *data;
681 		}
682 		data++;
683 		dest++;
684 	}
685 	*dest = '\0';
686 	return dest - str;
687 }
688 /* }}} */
689 
690 /* {{{ proto array get_headers(string url[, int format[, resource context]])
691    fetches all the headers sent by the server in response to a HTTP request */
692 PHP_FUNCTION(get_headers)
693 {
694 	char *url;
695 	size_t url_len;
696 	php_stream *stream;
697 	zval *prev_val, *hdr = NULL, *h;
698 	HashTable *hashT;
699 	zend_long format = 0;
700 	zval *zcontext = NULL;
701 	php_stream_context *context;
702 
703 	ZEND_PARSE_PARAMETERS_START(1, 3)
704 		Z_PARAM_PATH(url, url_len)
705 		Z_PARAM_OPTIONAL
706 		Z_PARAM_LONG(format)
707 		Z_PARAM_RESOURCE_EX(zcontext, 1, 0)
708 	ZEND_PARSE_PARAMETERS_END();
709 
710 	context = php_stream_context_from_zval(zcontext, 0);
711 
712 	if (!(stream = php_stream_open_wrapper_ex(url, "r", REPORT_ERRORS | STREAM_USE_URL | STREAM_ONLY_GET_HEADERS, NULL, context))) {
713 		RETURN_FALSE;
714 	}
715 
716 	if (Z_TYPE(stream->wrapperdata) != IS_ARRAY) {
717 		php_stream_close(stream);
718 		RETURN_FALSE;
719 	}
720 
721 	array_init(return_value);
722 
723 	/* check for curl-wrappers that provide headers via a special "headers" element */
724 	if ((h = zend_hash_str_find(HASH_OF(&stream->wrapperdata), "headers", sizeof("headers")-1)) != NULL && Z_TYPE_P(h) == IS_ARRAY) {
725 		/* curl-wrappers don't load data until the 1st read */
726 		if (!Z_ARRVAL_P(h)->nNumOfElements) {
727 			php_stream_getc(stream);
728 		}
729 		h = zend_hash_str_find(HASH_OF(&stream->wrapperdata), "headers", sizeof("headers")-1);
730 		hashT = Z_ARRVAL_P(h);
731 	} else {
732 		hashT = HASH_OF(&stream->wrapperdata);
733 	}
734 
735 	ZEND_HASH_FOREACH_VAL(hashT, hdr) {
736 		if (Z_TYPE_P(hdr) != IS_STRING) {
737 			continue;
738 		}
739 		if (!format) {
740 no_name_header:
741 			add_next_index_str(return_value, zend_string_copy(Z_STR_P(hdr)));
742 		} else {
743 			char c;
744 			char *s, *p;
745 
746 			if ((p = strchr(Z_STRVAL_P(hdr), ':'))) {
747 				c = *p;
748 				*p = '\0';
749 				s = p + 1;
750 				while (isspace((int)*(unsigned char *)s)) {
751 					s++;
752 				}
753 
754 				if ((prev_val = zend_hash_str_find(Z_ARRVAL_P(return_value), Z_STRVAL_P(hdr), (p - Z_STRVAL_P(hdr)))) == NULL) {
755 					add_assoc_stringl_ex(return_value, Z_STRVAL_P(hdr), (p - Z_STRVAL_P(hdr)), s, (Z_STRLEN_P(hdr) - (s - Z_STRVAL_P(hdr))));
756 				} else { /* some headers may occur more than once, therefor we need to remake the string into an array */
757 					convert_to_array(prev_val);
758 					add_next_index_stringl(prev_val, s, (Z_STRLEN_P(hdr) - (s - Z_STRVAL_P(hdr))));
759 				}
760 
761 				*p = c;
762 			} else {
763 				goto no_name_header;
764 			}
765 		}
766 	} ZEND_HASH_FOREACH_END();
767 
768 	php_stream_close(stream);
769 }
770 /* }}} */
771 
772 /*
773  * Local variables:
774  * tab-width: 4
775  * c-basic-offset: 4
776  * End:
777  * vim600: sw=4 ts=4 fdm=marker
778  * vim<600: sw=4 ts=4
779  */
780