You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

734 lines
18 KiB

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