PageRenderTime 43ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/wp-includes/SimplePie/HTTP/Parser.php

https://gitlab.com/campus-academy/krowkaramel
PHP | 520 lines | 316 code | 36 blank | 168 comment | 36 complexity | 5030b3d8b4f035133701e32c8c9c097a MD5 | raw file
  1. <?php
  2. /**
  3. * SimplePie
  4. *
  5. * A PHP-Based RSS and Atom Feed Framework.
  6. * Takes the hard work out of managing a complete RSS/Atom solution.
  7. *
  8. * Copyright (c) 2004-2016, Ryan Parman, Sam Sneddon, Ryan McCue, and contributors
  9. * All rights reserved.
  10. *
  11. * Redistribution and use in source and binary forms, with or without modification, are
  12. * permitted provided that the following conditions are met:
  13. *
  14. * * Redistributions of source code must retain the above copyright notice, this list of
  15. * conditions and the following disclaimer.
  16. *
  17. * * Redistributions in binary form must reproduce the above copyright notice, this list
  18. * of conditions and the following disclaimer in the documentation and/or other materials
  19. * provided with the distribution.
  20. *
  21. * * Neither the name of the SimplePie Team nor the names of its contributors may be used
  22. * to endorse or promote products derived from this software without specific prior
  23. * written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
  26. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  27. * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS
  28. * AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  29. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  30. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  31. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  32. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. *
  35. * @package SimplePie
  36. * @copyright 2004-2016 Ryan Parman, Sam Sneddon, Ryan McCue
  37. * @author Ryan Parman
  38. * @author Sam Sneddon
  39. * @author Ryan McCue
  40. * @link http://simplepie.org/ SimplePie
  41. * @license http://www.opensource.org/licenses/bsd-license.php BSD License
  42. */
  43. /**
  44. * HTTP Response Parser
  45. *
  46. * @package SimplePie
  47. * @subpackage HTTP
  48. */
  49. class SimplePie_HTTP_Parser
  50. {
  51. /**
  52. * HTTP Version
  53. *
  54. * @var float
  55. */
  56. public $http_version = 0.0;
  57. /**
  58. * Status code
  59. *
  60. * @var int
  61. */
  62. public $status_code = 0;
  63. /**
  64. * Reason phrase
  65. *
  66. * @var string
  67. */
  68. public $reason = '';
  69. /**
  70. * Key/value pairs of the headers
  71. *
  72. * @var array
  73. */
  74. public $headers = array();
  75. /**
  76. * Body of the response
  77. *
  78. * @var string
  79. */
  80. public $body = '';
  81. /**
  82. * Current state of the state machine
  83. *
  84. * @var string
  85. */
  86. protected $state = 'http_version';
  87. /**
  88. * Input data
  89. *
  90. * @var string
  91. */
  92. protected $data = '';
  93. /**
  94. * Input data length (to avoid calling strlen() everytime this is needed)
  95. *
  96. * @var int
  97. */
  98. protected $data_length = 0;
  99. /**
  100. * Current position of the pointer
  101. *
  102. * @var int
  103. */
  104. protected $position = 0;
  105. /**
  106. * Name of the hedaer currently being parsed
  107. *
  108. * @var string
  109. */
  110. protected $name = '';
  111. /**
  112. * Value of the hedaer currently being parsed
  113. *
  114. * @var string
  115. */
  116. protected $value = '';
  117. /**
  118. * Create an instance of the class with the input data
  119. *
  120. * @param string $data Input data
  121. */
  122. public function __construct($data)
  123. {
  124. $this->data = $data;
  125. $this->data_length = strlen($this->data);
  126. }
  127. /**
  128. * Parse the input data
  129. *
  130. * @return bool true on success, false on failure
  131. */
  132. public function parse()
  133. {
  134. while ($this->state && $this->state !== 'emit' && $this->has_data())
  135. {
  136. $state = $this->state;
  137. $this->$state();
  138. }
  139. $this->data = '';
  140. if ($this->state === 'emit' || $this->state === 'body')
  141. {
  142. return true;
  143. }
  144. $this->http_version = '';
  145. $this->status_code = '';
  146. $this->reason = '';
  147. $this->headers = array();
  148. $this->body = '';
  149. return false;
  150. }
  151. /**
  152. * Check whether there is data beyond the pointer
  153. *
  154. * @return bool true if there is further data, false if not
  155. */
  156. protected function has_data()
  157. {
  158. return (bool) ($this->position < $this->data_length);
  159. }
  160. /**
  161. * See if the next character is LWS
  162. *
  163. * @return bool true if the next character is LWS, false if not
  164. */
  165. protected function is_linear_whitespace()
  166. {
  167. return (bool) ($this->data[$this->position] === "\x09"
  168. || $this->data[$this->position] === "\x20"
  169. || ($this->data[$this->position] === "\x0A"
  170. && isset($this->data[$this->position + 1])
  171. && ($this->data[$this->position + 1] === "\x09" || $this->data[$this->position + 1] === "\x20")));
  172. }
  173. /**
  174. * Parse the HTTP version
  175. */
  176. protected function http_version()
  177. {
  178. if (strpos($this->data, "\x0A") !== false && strtoupper(substr($this->data, 0, 5)) === 'HTTP/')
  179. {
  180. $len = strspn($this->data, '0123456789.', 5);
  181. $this->http_version = substr($this->data, 5, $len);
  182. $this->position += 5 + $len;
  183. if (substr_count($this->http_version, '.') <= 1)
  184. {
  185. $this->http_version = (float) $this->http_version;
  186. $this->position += strspn($this->data, "\x09\x20", $this->position);
  187. $this->state = 'status';
  188. }
  189. else
  190. {
  191. $this->state = false;
  192. }
  193. }
  194. else
  195. {
  196. $this->state = false;
  197. }
  198. }
  199. /**
  200. * Parse the status code
  201. */
  202. protected function status()
  203. {
  204. if ($len = strspn($this->data, '0123456789', $this->position))
  205. {
  206. $this->status_code = (int) substr($this->data, $this->position, $len);
  207. $this->position += $len;
  208. $this->state = 'reason';
  209. }
  210. else
  211. {
  212. $this->state = false;
  213. }
  214. }
  215. /**
  216. * Parse the reason phrase
  217. */
  218. protected function reason()
  219. {
  220. $len = strcspn($this->data, "\x0A", $this->position);
  221. $this->reason = trim(substr($this->data, $this->position, $len), "\x09\x0D\x20");
  222. $this->position += $len + 1;
  223. $this->state = 'new_line';
  224. }
  225. /**
  226. * Deal with a new line, shifting data around as needed
  227. */
  228. protected function new_line()
  229. {
  230. $this->value = trim($this->value, "\x0D\x20");
  231. if ($this->name !== '' && $this->value !== '')
  232. {
  233. $this->name = strtolower($this->name);
  234. // We should only use the last Content-Type header. c.f. issue #1
  235. if (isset($this->headers[$this->name]) && $this->name !== 'content-type')
  236. {
  237. $this->headers[$this->name] .= ', ' . $this->value;
  238. }
  239. else
  240. {
  241. $this->headers[$this->name] = $this->value;
  242. }
  243. }
  244. $this->name = '';
  245. $this->value = '';
  246. if (substr($this->data[$this->position], 0, 2) === "\x0D\x0A")
  247. {
  248. $this->position += 2;
  249. $this->state = 'body';
  250. }
  251. elseif ($this->data[$this->position] === "\x0A")
  252. {
  253. $this->position++;
  254. $this->state = 'body';
  255. }
  256. else
  257. {
  258. $this->state = 'name';
  259. }
  260. }
  261. /**
  262. * Parse a header name
  263. */
  264. protected function name()
  265. {
  266. $len = strcspn($this->data, "\x0A:", $this->position);
  267. if (isset($this->data[$this->position + $len]))
  268. {
  269. if ($this->data[$this->position + $len] === "\x0A")
  270. {
  271. $this->position += $len;
  272. $this->state = 'new_line';
  273. }
  274. else
  275. {
  276. $this->name = substr($this->data, $this->position, $len);
  277. $this->position += $len + 1;
  278. $this->state = 'value';
  279. }
  280. }
  281. else
  282. {
  283. $this->state = false;
  284. }
  285. }
  286. /**
  287. * Parse LWS, replacing consecutive LWS characters with a single space
  288. */
  289. protected function linear_whitespace()
  290. {
  291. do
  292. {
  293. if (substr($this->data, $this->position, 2) === "\x0D\x0A")
  294. {
  295. $this->position += 2;
  296. }
  297. elseif ($this->data[$this->position] === "\x0A")
  298. {
  299. $this->position++;
  300. }
  301. $this->position += strspn($this->data, "\x09\x20", $this->position);
  302. } while ($this->has_data() && $this->is_linear_whitespace());
  303. $this->value .= "\x20";
  304. }
  305. /**
  306. * See what state to move to while within non-quoted header values
  307. */
  308. protected function value()
  309. {
  310. if ($this->is_linear_whitespace())
  311. {
  312. $this->linear_whitespace();
  313. }
  314. else
  315. {
  316. switch ($this->data[$this->position])
  317. {
  318. case '"':
  319. // Workaround for ETags: we have to include the quotes as
  320. // part of the tag.
  321. if (strtolower($this->name) === 'etag')
  322. {
  323. $this->value .= '"';
  324. $this->position++;
  325. $this->state = 'value_char';
  326. break;
  327. }
  328. $this->position++;
  329. $this->state = 'quote';
  330. break;
  331. case "\x0A":
  332. $this->position++;
  333. $this->state = 'new_line';
  334. break;
  335. default:
  336. $this->state = 'value_char';
  337. break;
  338. }
  339. }
  340. }
  341. /**
  342. * Parse a header value while outside quotes
  343. */
  344. protected function value_char()
  345. {
  346. $len = strcspn($this->data, "\x09\x20\x0A\"", $this->position);
  347. $this->value .= substr($this->data, $this->position, $len);
  348. $this->position += $len;
  349. $this->state = 'value';
  350. }
  351. /**
  352. * See what state to move to while within quoted header values
  353. */
  354. protected function quote()
  355. {
  356. if ($this->is_linear_whitespace())
  357. {
  358. $this->linear_whitespace();
  359. }
  360. else
  361. {
  362. switch ($this->data[$this->position])
  363. {
  364. case '"':
  365. $this->position++;
  366. $this->state = 'value';
  367. break;
  368. case "\x0A":
  369. $this->position++;
  370. $this->state = 'new_line';
  371. break;
  372. case '\\':
  373. $this->position++;
  374. $this->state = 'quote_escaped';
  375. break;
  376. default:
  377. $this->state = 'quote_char';
  378. break;
  379. }
  380. }
  381. }
  382. /**
  383. * Parse a header value while within quotes
  384. */
  385. protected function quote_char()
  386. {
  387. $len = strcspn($this->data, "\x09\x20\x0A\"\\", $this->position);
  388. $this->value .= substr($this->data, $this->position, $len);
  389. $this->position += $len;
  390. $this->state = 'value';
  391. }
  392. /**
  393. * Parse an escaped character within quotes
  394. */
  395. protected function quote_escaped()
  396. {
  397. $this->value .= $this->data[$this->position];
  398. $this->position++;
  399. $this->state = 'quote';
  400. }
  401. /**
  402. * Parse the body
  403. */
  404. protected function body()
  405. {
  406. $this->body = substr($this->data, $this->position);
  407. if (!empty($this->headers['transfer-encoding']))
  408. {
  409. unset($this->headers['transfer-encoding']);
  410. $this->state = 'chunked';
  411. }
  412. else
  413. {
  414. $this->state = 'emit';
  415. }
  416. }
  417. /**
  418. * Parsed a "Transfer-Encoding: chunked" body
  419. */
  420. protected function chunked()
  421. {
  422. if (!preg_match('/^([0-9a-f]+)[^\r\n]*\r\n/i', trim($this->body)))
  423. {
  424. $this->state = 'emit';
  425. return;
  426. }
  427. $decoded = '';
  428. $encoded = $this->body;
  429. while (true)
  430. {
  431. $is_chunked = (bool) preg_match( '/^([0-9a-f]+)[^\r\n]*\r\n/i', $encoded, $matches );
  432. if (!$is_chunked)
  433. {
  434. // Looks like it's not chunked after all
  435. $this->state = 'emit';
  436. return;
  437. }
  438. $length = hexdec(trim($matches[1]));
  439. if ($length === 0)
  440. {
  441. // Ignore trailer headers
  442. $this->state = 'emit';
  443. $this->body = $decoded;
  444. return;
  445. }
  446. $chunk_length = strlen($matches[0]);
  447. $decoded .= $part = substr($encoded, $chunk_length, $length);
  448. $encoded = substr($encoded, $chunk_length + $length + 2);
  449. if (trim($encoded) === '0' || empty($encoded))
  450. {
  451. $this->state = 'emit';
  452. $this->body = $decoded;
  453. return;
  454. }
  455. }
  456. }
  457. /**
  458. * Prepare headers (take care of proxies headers)
  459. *
  460. * @param string $headers Raw headers
  461. * @param integer $count Redirection count. Default to 1.
  462. *
  463. * @return string
  464. */
  465. static public function prepareHeaders($headers, $count = 1)
  466. {
  467. $data = explode("\r\n\r\n", $headers, $count);
  468. $data = array_pop($data);
  469. if (false !== stripos($data, "HTTP/1.0 200 Connection established\r\n")) {
  470. $exploded = explode("\r\n\r\n", $data, 2);
  471. $data = end($exploded);
  472. }
  473. if (false !== stripos($data, "HTTP/1.1 200 Connection established\r\n")) {
  474. $exploded = explode("\r\n\r\n", $data, 2);
  475. $data = end($exploded);
  476. }
  477. return $data;
  478. }
  479. }