/ext/standard/url.c

http://github.com/infusion/PHP · C · 773 lines · 576 code · 90 blank · 107 comment · 236 complexity · f992a0e0eb40d5ba0517e8ca6fe87443 MD5 · raw file

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