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