url.c 18 KB

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