url.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745
  1. /*
  2. +----------------------------------------------------------------------+
  3. | Copyright (c) The PHP Group |
  4. +----------------------------------------------------------------------+
  5. | This source file is subject to version 3.01 of the PHP license, |
  6. | that is bundled with this package in the file LICENSE, and is |
  7. | available through the world-wide-web at the following url: |
  8. | https://www.php.net/license/3_01.txt |
  9. | If you did not receive a copy of the PHP license and are unable to |
  10. | obtain it through the world-wide-web, please send a note to |
  11. | license@php.net so we can mail you a copy immediately. |
  12. +----------------------------------------------------------------------+
  13. | Author: Jim Winstead <jimw@php.net> |
  14. +----------------------------------------------------------------------+
  15. */
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19. #include <sys/types.h>
  20. #ifdef __SSE2__
  21. #include <emmintrin.h>
  22. #endif
  23. #include "php.h"
  24. #include "url.h"
  25. #include "file.h"
  26. /* {{{ free_url */
  27. PHPAPI void php_url_free(php_url *theurl)
  28. {
  29. if (theurl->scheme)
  30. zend_string_release_ex(theurl->scheme, 0);
  31. if (theurl->user)
  32. zend_string_release_ex(theurl->user, 0);
  33. if (theurl->pass)
  34. zend_string_release_ex(theurl->pass, 0);
  35. if (theurl->host)
  36. zend_string_release_ex(theurl->host, 0);
  37. if (theurl->path)
  38. zend_string_release_ex(theurl->path, 0);
  39. if (theurl->query)
  40. zend_string_release_ex(theurl->query, 0);
  41. if (theurl->fragment)
  42. zend_string_release_ex(theurl->fragment, 0);
  43. efree(theurl);
  44. }
  45. /* }}} */
  46. /* {{{ php_replace_controlchars_ex */
  47. PHPAPI char *php_replace_controlchars_ex(char *str, size_t len)
  48. {
  49. unsigned char *s = (unsigned char *)str;
  50. unsigned char *e = (unsigned char *)str + len;
  51. if (!str) {
  52. return (NULL);
  53. }
  54. while (s < e) {
  55. if (iscntrl(*s)) {
  56. *s='_';
  57. }
  58. s++;
  59. }
  60. return (str);
  61. }
  62. /* }}} */
  63. PHPAPI char *php_replace_controlchars(char *str)
  64. {
  65. return php_replace_controlchars_ex(str, strlen(str));
  66. }
  67. PHPAPI php_url *php_url_parse(char const *str)
  68. {
  69. return php_url_parse_ex(str, strlen(str));
  70. }
  71. static const char *binary_strcspn(const char *s, const char *e, const char *chars) {
  72. while (*chars) {
  73. const char *p = memchr(s, *chars, e - s);
  74. if (p) {
  75. e = p;
  76. }
  77. chars++;
  78. }
  79. return e;
  80. }
  81. /* {{{ php_url_parse */
  82. PHPAPI php_url *php_url_parse_ex(char const *str, size_t length)
  83. {
  84. bool has_port;
  85. return php_url_parse_ex2(str, length, &has_port);
  86. }
  87. /* {{{ php_url_parse_ex2
  88. */
  89. PHPAPI php_url *php_url_parse_ex2(char const *str, size_t length, bool *has_port)
  90. {
  91. char port_buf[6];
  92. php_url *ret = ecalloc(1, sizeof(php_url));
  93. char const *s, *e, *p, *pp, *ue;
  94. *has_port = 0;
  95. s = str;
  96. ue = s + length;
  97. /* parse scheme */
  98. if ((e = memchr(s, ':', length)) && e != s) {
  99. /* validate scheme */
  100. p = s;
  101. while (p < e) {
  102. /* scheme = 1*[ lowalpha | digit | "+" | "-" | "." ] */
  103. if (!isalpha(*p) && !isdigit(*p) && *p != '+' && *p != '.' && *p != '-') {
  104. if (e + 1 < ue && e < binary_strcspn(s, ue, "?#")) {
  105. goto parse_port;
  106. } else if (s + 1 < ue && *s == '/' && *(s + 1) == '/') { /* relative-scheme URL */
  107. s += 2;
  108. e = 0;
  109. goto parse_host;
  110. } else {
  111. goto just_path;
  112. }
  113. }
  114. p++;
  115. }
  116. if (e + 1 == ue) { /* only scheme is available */
  117. ret->scheme = zend_string_init(s, (e - s), 0);
  118. php_replace_controlchars_ex(ZSTR_VAL(ret->scheme), ZSTR_LEN(ret->scheme));
  119. return ret;
  120. }
  121. /*
  122. * certain schemas like mailto: and zlib: may not have any / after them
  123. * this check ensures we support those.
  124. */
  125. if (*(e+1) != '/') {
  126. /* check if the data we get is a port this allows us to
  127. * correctly parse things like a.com:80
  128. */
  129. p = e + 1;
  130. while (p < ue && isdigit(*p)) {
  131. p++;
  132. }
  133. if ((p == ue || *p == '/') && (p - e) < 7) {
  134. goto parse_port;
  135. }
  136. ret->scheme = zend_string_init(s, (e-s), 0);
  137. php_replace_controlchars_ex(ZSTR_VAL(ret->scheme), ZSTR_LEN(ret->scheme));
  138. s = e + 1;
  139. goto just_path;
  140. } else {
  141. ret->scheme = zend_string_init(s, (e-s), 0);
  142. php_replace_controlchars_ex(ZSTR_VAL(ret->scheme), ZSTR_LEN(ret->scheme));
  143. if (e + 2 < ue && *(e + 2) == '/') {
  144. s = e + 3;
  145. if (zend_string_equals_literal_ci(ret->scheme, "file")) {
  146. if (e + 3 < ue && *(e + 3) == '/') {
  147. /* support windows drive letters as in:
  148. file:///c:/somedir/file.txt
  149. */
  150. if (e + 5 < ue && *(e + 5) == ':') {
  151. s = e + 4;
  152. }
  153. goto just_path;
  154. }
  155. }
  156. } else {
  157. s = e + 1;
  158. goto just_path;
  159. }
  160. }
  161. } else if (e) { /* no scheme; starts with colon: look for port */
  162. parse_port:
  163. p = e + 1;
  164. pp = p;
  165. while (pp < ue && pp - p < 6 && isdigit(*pp)) {
  166. pp++;
  167. }
  168. if (pp - p > 0 && pp - p < 6 && (pp == ue || *pp == '/')) {
  169. zend_long port;
  170. char *end;
  171. memcpy(port_buf, p, (pp - p));
  172. port_buf[pp - p] = '\0';
  173. port = ZEND_STRTOL(port_buf, &end, 10);
  174. if (port >= 0 && port <= 65535 && end != port_buf) {
  175. *has_port = 1;
  176. ret->port = (unsigned short) port;
  177. if (s + 1 < ue && *s == '/' && *(s + 1) == '/') { /* relative-scheme URL */
  178. s += 2;
  179. }
  180. } else {
  181. php_url_free(ret);
  182. return NULL;
  183. }
  184. } else if (p == pp && pp == ue) {
  185. php_url_free(ret);
  186. return NULL;
  187. } else if (s + 1 < ue && *s == '/' && *(s + 1) == '/') { /* relative-scheme URL */
  188. s += 2;
  189. } else {
  190. goto just_path;
  191. }
  192. } else if (s + 1 < ue && *s == '/' && *(s + 1) == '/') { /* relative-scheme URL */
  193. s += 2;
  194. } else {
  195. goto just_path;
  196. }
  197. parse_host:
  198. e = binary_strcspn(s, ue, "/?#");
  199. /* check for login and password */
  200. if ((p = zend_memrchr(s, '@', (e-s)))) {
  201. if ((pp = memchr(s, ':', (p-s)))) {
  202. ret->user = zend_string_init(s, (pp-s), 0);
  203. php_replace_controlchars_ex(ZSTR_VAL(ret->user), ZSTR_LEN(ret->user));
  204. pp++;
  205. ret->pass = zend_string_init(pp, (p-pp), 0);
  206. php_replace_controlchars_ex(ZSTR_VAL(ret->pass), ZSTR_LEN(ret->pass));
  207. } else {
  208. ret->user = zend_string_init(s, (p-s), 0);
  209. php_replace_controlchars_ex(ZSTR_VAL(ret->user), ZSTR_LEN(ret->user));
  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. php_url_free(ret);
  227. return NULL;
  228. } else if (e - p > 0) {
  229. zend_long port;
  230. char *end;
  231. memcpy(port_buf, p, (e - p));
  232. port_buf[e - p] = '\0';
  233. port = ZEND_STRTOL(port_buf, &end, 10);
  234. if (port >= 0 && port <= 65535 && end != port_buf) {
  235. *has_port = 1;
  236. ret->port = (unsigned short)port;
  237. } else {
  238. php_url_free(ret);
  239. return NULL;
  240. }
  241. }
  242. p--;
  243. }
  244. } else {
  245. p = e;
  246. }
  247. /* check if we have a valid host, if we don't reject the string as url */
  248. if ((p-s) < 1) {
  249. php_url_free(ret);
  250. return NULL;
  251. }
  252. ret->host = zend_string_init(s, (p-s), 0);
  253. php_replace_controlchars_ex(ZSTR_VAL(ret->host), ZSTR_LEN(ret->host));
  254. if (e == ue) {
  255. return ret;
  256. }
  257. s = e;
  258. just_path:
  259. e = ue;
  260. p = memchr(s, '#', (e - s));
  261. if (p) {
  262. p++;
  263. if (p < e) {
  264. ret->fragment = zend_string_init(p, (e - p), 0);
  265. php_replace_controlchars_ex(ZSTR_VAL(ret->fragment), ZSTR_LEN(ret->fragment));
  266. } else {
  267. ret->fragment = ZSTR_EMPTY_ALLOC();
  268. }
  269. e = p-1;
  270. }
  271. p = memchr(s, '?', (e - s));
  272. if (p) {
  273. p++;
  274. if (p < e) {
  275. ret->query = zend_string_init(p, (e - p), 0);
  276. php_replace_controlchars_ex(ZSTR_VAL(ret->query), ZSTR_LEN(ret->query));
  277. } else {
  278. ret->query = ZSTR_EMPTY_ALLOC();
  279. }
  280. e = p-1;
  281. }
  282. if (s < e || s == ue) {
  283. ret->path = zend_string_init(s, (e - s), 0);
  284. php_replace_controlchars_ex(ZSTR_VAL(ret->path), ZSTR_LEN(ret->path));
  285. }
  286. return ret;
  287. }
  288. /* }}} */
  289. /* {{{ Parse a URL and return its components */
  290. PHP_FUNCTION(parse_url)
  291. {
  292. char *str;
  293. size_t str_len;
  294. php_url *resource;
  295. zend_long key = -1;
  296. zval tmp;
  297. bool has_port;
  298. ZEND_PARSE_PARAMETERS_START(1, 2)
  299. Z_PARAM_STRING(str, str_len)
  300. Z_PARAM_OPTIONAL
  301. Z_PARAM_LONG(key)
  302. ZEND_PARSE_PARAMETERS_END();
  303. resource = php_url_parse_ex2(str, str_len, &has_port);
  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_STR_COPY(resource->scheme);
  312. break;
  313. case PHP_URL_HOST:
  314. if (resource->host != NULL) RETVAL_STR_COPY(resource->host);
  315. break;
  316. case PHP_URL_PORT:
  317. if (has_port) RETVAL_LONG(resource->port);
  318. break;
  319. case PHP_URL_USER:
  320. if (resource->user != NULL) RETVAL_STR_COPY(resource->user);
  321. break;
  322. case PHP_URL_PASS:
  323. if (resource->pass != NULL) RETVAL_STR_COPY(resource->pass);
  324. break;
  325. case PHP_URL_PATH:
  326. if (resource->path != NULL) RETVAL_STR_COPY(resource->path);
  327. break;
  328. case PHP_URL_QUERY:
  329. if (resource->query != NULL) RETVAL_STR_COPY(resource->query);
  330. break;
  331. case PHP_URL_FRAGMENT:
  332. if (resource->fragment != NULL) RETVAL_STR_COPY(resource->fragment);
  333. break;
  334. default:
  335. zend_argument_value_error(2, "must be a valid URL component identifier, " ZEND_LONG_FMT " given", key);
  336. break;
  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. ZVAL_STR_COPY(&tmp, resource->scheme);
  345. zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_SCHEME), &tmp);
  346. }
  347. if (resource->host != NULL) {
  348. ZVAL_STR_COPY(&tmp, resource->host);
  349. zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_HOST), &tmp);
  350. }
  351. if (has_port) {
  352. ZVAL_LONG(&tmp, resource->port);
  353. zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_PORT), &tmp);
  354. }
  355. if (resource->user != NULL) {
  356. ZVAL_STR_COPY(&tmp, resource->user);
  357. zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_USER), &tmp);
  358. }
  359. if (resource->pass != NULL) {
  360. ZVAL_STR_COPY(&tmp, resource->pass);
  361. zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_PASS), &tmp);
  362. }
  363. if (resource->path != NULL) {
  364. ZVAL_STR_COPY(&tmp, resource->path);
  365. zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_PATH), &tmp);
  366. }
  367. if (resource->query != NULL) {
  368. ZVAL_STR_COPY(&tmp, resource->query);
  369. zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_QUERY), &tmp);
  370. }
  371. if (resource->fragment != NULL) {
  372. ZVAL_STR_COPY(&tmp, resource->fragment);
  373. zend_hash_add_new(Z_ARRVAL_P(return_value), ZSTR_KNOWN(ZEND_STR_FRAGMENT), &tmp);
  374. }
  375. done:
  376. php_url_free(resource);
  377. }
  378. /* }}} */
  379. /* {{{ php_htoi */
  380. static int php_htoi(char *s)
  381. {
  382. int value;
  383. int c;
  384. c = ((unsigned char *)s)[0];
  385. if (isupper(c))
  386. c = tolower(c);
  387. value = (c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10) * 16;
  388. c = ((unsigned char *)s)[1];
  389. if (isupper(c))
  390. c = tolower(c);
  391. value += c >= '0' && c <= '9' ? c - '0' : c - 'a' + 10;
  392. return (value);
  393. }
  394. /* }}} */
  395. /* rfc1738:
  396. ...The characters ";",
  397. "/", "?", ":", "@", "=" and "&" are the characters which may be
  398. reserved for special meaning within a scheme...
  399. ...Thus, only alphanumerics, the special characters "$-_.+!*'(),", and
  400. reserved characters used for their reserved purposes may be used
  401. unencoded within a URL...
  402. For added safety, we only leave -_. unencoded.
  403. */
  404. static const unsigned char hexchars[] = "0123456789ABCDEF";
  405. static zend_always_inline zend_string *php_url_encode_impl(const char *s, size_t len, bool raw) /* {{{ */ {
  406. unsigned char c;
  407. unsigned char *to;
  408. unsigned char const *from, *end;
  409. zend_string *start;
  410. from = (unsigned char *)s;
  411. end = (unsigned char *)s + len;
  412. start = zend_string_safe_alloc(3, len, 0, 0);
  413. to = (unsigned char*)ZSTR_VAL(start);
  414. #ifdef __SSE2__
  415. while (from + 16 < end) {
  416. __m128i mask;
  417. uint32_t bits;
  418. const __m128i _A = _mm_set1_epi8('A' - 1);
  419. const __m128i Z_ = _mm_set1_epi8('Z' + 1);
  420. const __m128i _a = _mm_set1_epi8('a' - 1);
  421. const __m128i z_ = _mm_set1_epi8('z' + 1);
  422. const __m128i _zero = _mm_set1_epi8('0' - 1);
  423. const __m128i nine_ = _mm_set1_epi8('9' + 1);
  424. const __m128i dot = _mm_set1_epi8('.');
  425. const __m128i minus = _mm_set1_epi8('-');
  426. const __m128i under = _mm_set1_epi8('_');
  427. __m128i in = _mm_loadu_si128((__m128i *)from);
  428. __m128i gt = _mm_cmpgt_epi8(in, _A);
  429. __m128i lt = _mm_cmplt_epi8(in, Z_);
  430. mask = _mm_and_si128(lt, gt); /* upper */
  431. gt = _mm_cmpgt_epi8(in, _a);
  432. lt = _mm_cmplt_epi8(in, z_);
  433. mask = _mm_or_si128(mask, _mm_and_si128(lt, gt)); /* lower */
  434. gt = _mm_cmpgt_epi8(in, _zero);
  435. lt = _mm_cmplt_epi8(in, nine_);
  436. mask = _mm_or_si128(mask, _mm_and_si128(lt, gt)); /* number */
  437. mask = _mm_or_si128(mask, _mm_cmpeq_epi8(in, dot));
  438. mask = _mm_or_si128(mask, _mm_cmpeq_epi8(in, minus));
  439. mask = _mm_or_si128(mask, _mm_cmpeq_epi8(in, under));
  440. if (!raw) {
  441. const __m128i blank = _mm_set1_epi8(' ');
  442. __m128i eq = _mm_cmpeq_epi8(in, blank);
  443. if (_mm_movemask_epi8(eq)) {
  444. in = _mm_add_epi8(in, _mm_and_si128(eq, _mm_set1_epi8('+' - ' ')));
  445. mask = _mm_or_si128(mask, eq);
  446. }
  447. }
  448. if (raw) {
  449. const __m128i wavy = _mm_set1_epi8('~');
  450. mask = _mm_or_si128(mask, _mm_cmpeq_epi8(in, wavy));
  451. }
  452. if (((bits = _mm_movemask_epi8(mask)) & 0xffff) == 0xffff) {
  453. _mm_storeu_si128((__m128i*)to, in);
  454. to += 16;
  455. } else {
  456. int i;
  457. unsigned char xmm[16];
  458. _mm_storeu_si128((__m128i*)xmm, in);
  459. for (i = 0; i < sizeof(xmm); i++) {
  460. if ((bits & (0x1 << i))) {
  461. *to++ = xmm[i];
  462. } else {
  463. *to++ = '%';
  464. *to++ = hexchars[xmm[i] >> 4];
  465. *to++ = hexchars[xmm[i] & 0xf];
  466. }
  467. }
  468. }
  469. from += 16;
  470. }
  471. #endif
  472. while (from < end) {
  473. c = *from++;
  474. if (!raw && c == ' ') {
  475. *to++ = '+';
  476. } else if ((c < '0' && c != '-' && c != '.') ||
  477. (c < 'A' && c > '9') ||
  478. (c > 'Z' && c < 'a' && c != '_') ||
  479. (c > 'z' && (!raw || c != '~'))) {
  480. to[0] = '%';
  481. to[1] = hexchars[c >> 4];
  482. to[2] = hexchars[c & 15];
  483. to += 3;
  484. } else {
  485. *to++ = c;
  486. }
  487. }
  488. *to = '\0';
  489. start = zend_string_truncate(start, to - (unsigned char*)ZSTR_VAL(start), 0);
  490. return start;
  491. }
  492. /* }}} */
  493. /* {{{ php_url_encode */
  494. PHPAPI zend_string *php_url_encode(char const *s, size_t len)
  495. {
  496. return php_url_encode_impl(s, len, 0);
  497. }
  498. /* }}} */
  499. /* {{{ URL-encodes string */
  500. PHP_FUNCTION(urlencode)
  501. {
  502. zend_string *in_str;
  503. ZEND_PARSE_PARAMETERS_START(1, 1)
  504. Z_PARAM_STR(in_str)
  505. ZEND_PARSE_PARAMETERS_END();
  506. RETURN_STR(php_url_encode(ZSTR_VAL(in_str), ZSTR_LEN(in_str)));
  507. }
  508. /* }}} */
  509. /* {{{ Decodes URL-encoded string */
  510. PHP_FUNCTION(urldecode)
  511. {
  512. zend_string *in_str, *out_str;
  513. ZEND_PARSE_PARAMETERS_START(1, 1)
  514. Z_PARAM_STR(in_str)
  515. ZEND_PARSE_PARAMETERS_END();
  516. out_str = zend_string_init(ZSTR_VAL(in_str), ZSTR_LEN(in_str), 0);
  517. ZSTR_LEN(out_str) = php_url_decode(ZSTR_VAL(out_str), ZSTR_LEN(out_str));
  518. RETURN_NEW_STR(out_str);
  519. }
  520. /* }}} */
  521. /* {{{ php_url_decode */
  522. PHPAPI size_t php_url_decode(char *str, size_t len)
  523. {
  524. char *dest = str;
  525. char *data = str;
  526. while (len--) {
  527. if (*data == '+') {
  528. *dest = ' ';
  529. }
  530. else if (*data == '%' && len >= 2 && isxdigit((int) *(data + 1))
  531. && isxdigit((int) *(data + 2))) {
  532. *dest = (char) php_htoi(data + 1);
  533. data += 2;
  534. len -= 2;
  535. } else {
  536. *dest = *data;
  537. }
  538. data++;
  539. dest++;
  540. }
  541. *dest = '\0';
  542. return dest - str;
  543. }
  544. /* }}} */
  545. /* {{{ php_raw_url_encode */
  546. PHPAPI zend_string *php_raw_url_encode(char const *s, size_t len)
  547. {
  548. return php_url_encode_impl(s, len, 1);
  549. }
  550. /* }}} */
  551. /* {{{ URL-encodes string */
  552. PHP_FUNCTION(rawurlencode)
  553. {
  554. zend_string *in_str;
  555. ZEND_PARSE_PARAMETERS_START(1, 1)
  556. Z_PARAM_STR(in_str)
  557. ZEND_PARSE_PARAMETERS_END();
  558. RETURN_STR(php_raw_url_encode(ZSTR_VAL(in_str), ZSTR_LEN(in_str)));
  559. }
  560. /* }}} */
  561. /* {{{ Decodes URL-encodes string */
  562. PHP_FUNCTION(rawurldecode)
  563. {
  564. zend_string *in_str, *out_str;
  565. ZEND_PARSE_PARAMETERS_START(1, 1)
  566. Z_PARAM_STR(in_str)
  567. ZEND_PARSE_PARAMETERS_END();
  568. out_str = zend_string_init(ZSTR_VAL(in_str), ZSTR_LEN(in_str), 0);
  569. ZSTR_LEN(out_str) = php_raw_url_decode(ZSTR_VAL(out_str), ZSTR_LEN(out_str));
  570. RETURN_NEW_STR(out_str);
  571. }
  572. /* }}} */
  573. /* {{{ php_raw_url_decode */
  574. PHPAPI size_t php_raw_url_decode(char *str, size_t len)
  575. {
  576. char *dest = str;
  577. char *data = str;
  578. while (len--) {
  579. if (*data == '%' && len >= 2 && isxdigit((int) *(data + 1))
  580. && isxdigit((int) *(data + 2))) {
  581. *dest = (char) php_htoi(data + 1);
  582. data += 2;
  583. len -= 2;
  584. } else {
  585. *dest = *data;
  586. }
  587. data++;
  588. dest++;
  589. }
  590. *dest = '\0';
  591. return dest - str;
  592. }
  593. /* }}} */
  594. /* {{{ fetches all the headers sent by the server in response to a HTTP request */
  595. PHP_FUNCTION(get_headers)
  596. {
  597. char *url;
  598. size_t url_len;
  599. php_stream *stream;
  600. zval *prev_val, *hdr = NULL;
  601. bool format = 0;
  602. zval *zcontext = NULL;
  603. php_stream_context *context;
  604. ZEND_PARSE_PARAMETERS_START(1, 3)
  605. Z_PARAM_PATH(url, url_len)
  606. Z_PARAM_OPTIONAL
  607. Z_PARAM_BOOL(format)
  608. Z_PARAM_RESOURCE_OR_NULL(zcontext)
  609. ZEND_PARSE_PARAMETERS_END();
  610. context = php_stream_context_from_zval(zcontext, 0);
  611. if (!(stream = php_stream_open_wrapper_ex(url, "r", REPORT_ERRORS | STREAM_USE_URL | STREAM_ONLY_GET_HEADERS, NULL, context))) {
  612. RETURN_FALSE;
  613. }
  614. if (Z_TYPE(stream->wrapperdata) != IS_ARRAY) {
  615. php_stream_close(stream);
  616. RETURN_FALSE;
  617. }
  618. array_init(return_value);
  619. ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(&stream->wrapperdata), hdr) {
  620. if (Z_TYPE_P(hdr) != IS_STRING) {
  621. continue;
  622. }
  623. if (!format) {
  624. no_name_header:
  625. add_next_index_str(return_value, zend_string_copy(Z_STR_P(hdr)));
  626. } else {
  627. char c;
  628. char *s, *p;
  629. if ((p = strchr(Z_STRVAL_P(hdr), ':'))) {
  630. c = *p;
  631. *p = '\0';
  632. s = p + 1;
  633. while (isspace((int)*(unsigned char *)s)) {
  634. s++;
  635. }
  636. if ((prev_val = zend_hash_str_find(Z_ARRVAL_P(return_value), Z_STRVAL_P(hdr), (p - Z_STRVAL_P(hdr)))) == NULL) {
  637. 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))));
  638. } else { /* some headers may occur more than once, therefore we need to remake the string into an array */
  639. convert_to_array(prev_val);
  640. add_next_index_stringl(prev_val, s, (Z_STRLEN_P(hdr) - (s - Z_STRVAL_P(hdr))));
  641. }
  642. *p = c;
  643. } else {
  644. goto no_name_header;
  645. }
  646. }
  647. } ZEND_HASH_FOREACH_END();
  648. php_stream_close(stream);
  649. }
  650. /* }}} */