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