Http.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /**
  3. * Hoa
  4. *
  5. *
  6. * @license
  7. *
  8. * New BSD License
  9. *
  10. * Copyright © 2007-2017, Hoa community. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions are met:
  14. * * Redistributions of source code must retain the above copyright
  15. * notice, this list of conditions and the following disclaimer.
  16. * * Redistributions in binary form must reproduce the above copyright
  17. * notice, this list of conditions and the following disclaimer in the
  18. * documentation and/or other materials provided with the distribution.
  19. * * Neither the name of the Hoa nor the names of its contributors may be
  20. * used to endorse or promote products derived from this software without
  21. * specific prior written permission.
  22. *
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  24. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  25. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  26. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS AND CONTRIBUTORS BE
  27. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  28. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  29. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  30. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  31. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  32. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  33. * POSSIBILITY OF SUCH DAMAGE.
  34. */
  35. namespace Hoa\Http;
  36. use Hoa\Consistency;
  37. /**
  38. * Class \Hoa\Http.
  39. *
  40. * Generic class to manage HTTP headers (parse, set, get) only.
  41. *
  42. * @copyright Copyright © 2007-2017 Hoa community
  43. * @license New BSD License
  44. */
  45. abstract class Http implements \ArrayAccess, \IteratorAggregate, \Countable
  46. {
  47. /**
  48. * Whether PHP is running with FastCGI or not.
  49. *
  50. * @var bool
  51. */
  52. protected static $_fcgi = null;
  53. /**
  54. * Request HTTP version.
  55. *
  56. * @var float
  57. */
  58. protected $_httpVersion = 1.1;
  59. /**
  60. * Headers (not sent).
  61. *
  62. * @var array
  63. */
  64. protected $_headers = [];
  65. /**
  66. * Request body.
  67. *
  68. * @var string
  69. */
  70. protected $_body = null;
  71. /**
  72. * Constructor.
  73. *
  74. */
  75. public function __construct()
  76. {
  77. if (null === self::$_fcgi) {
  78. self::$_fcgi = 'cgi-fcgi' === PHP_SAPI;
  79. }
  80. return;
  81. }
  82. /**
  83. * Set request HTTP version.
  84. *
  85. * @param float $version HTTP version.
  86. * @return float
  87. */
  88. public function setHttpVersion($version)
  89. {
  90. $old = $this->_httpVersion;
  91. $this->_httpVersion = $version;
  92. return $old;
  93. }
  94. /**
  95. * Get request HTTP version.
  96. *
  97. * @return float
  98. */
  99. public function getHttpVersion()
  100. {
  101. return $this->_httpVersion;
  102. }
  103. /**
  104. * Parse a HTTP packet.
  105. *
  106. * @param string $packet HTTP packet.
  107. * @return void
  108. * @throws \Hoa\Http\Exception
  109. */
  110. abstract public function parse($packet);
  111. /**
  112. * Helper to parse HTTP headers and distribute them in array accesses.
  113. *
  114. * @param array $headers Headers to parse and distribute.
  115. * @return array
  116. */
  117. protected function _parse(array $headers)
  118. {
  119. unset($this->_headers);
  120. $this->_headers = [];
  121. foreach ($headers as $header) {
  122. list($name, $value) = explode(':', $header, 2);
  123. $this->_headers[strtolower($name)] = trim($value);
  124. }
  125. return $this->_headers;
  126. }
  127. /**
  128. * Get headers.
  129. *
  130. * @return array
  131. */
  132. public function getHeaders()
  133. {
  134. return $this->_headers;
  135. }
  136. /**
  137. * Get headers (formatted).
  138. *
  139. * @return array
  140. */
  141. public function getHeadersFormatted()
  142. {
  143. $out = [];
  144. foreach ($this->getHeaders() as $header => $value) {
  145. if ('x-' == strtolower(substr($header, 0, 2))) {
  146. $header = 'http_' . $header;
  147. }
  148. $out[strtoupper(str_replace('-', '_', $header))] = $value;
  149. }
  150. return $out;
  151. }
  152. /**
  153. * Check if header exists.
  154. *
  155. * @param string $offset Header.
  156. * @return bool
  157. */
  158. public function offsetExists($offset)
  159. {
  160. return array_key_exists($offset, $this->_headers);
  161. }
  162. /**
  163. * Get a header's value.
  164. *
  165. * @param string $offset Header.
  166. * @return string
  167. */
  168. public function offsetGet($offset)
  169. {
  170. if (false === $this->offsetExists($offset)) {
  171. return null;
  172. }
  173. return $this->_headers[$offset];
  174. }
  175. /**
  176. * Set a value to a header.
  177. *
  178. * @param string $offset Header.
  179. * @param string $value Value.
  180. * @return void
  181. */
  182. public function offsetSet($offset, $value)
  183. {
  184. $this->_headers[$offset] = $value;
  185. return;
  186. }
  187. /**
  188. * Unset a header.
  189. *
  190. * @param string $offset Header.
  191. * @return void
  192. */
  193. public function offsetUnset($offset)
  194. {
  195. unset($this->_headers[$offset]);
  196. return;
  197. }
  198. /**
  199. * Get iterator.
  200. *
  201. * @return \ArrayIterator
  202. */
  203. public function getIterator()
  204. {
  205. return new \ArrayIterator($this->getHeaders());
  206. }
  207. /**
  208. * Count number of headers.
  209. *
  210. * @return int
  211. */
  212. public function count()
  213. {
  214. return count($this->getHeaders());
  215. }
  216. /**
  217. * Set request body.
  218. *
  219. * @param string $body Body.
  220. * @return string
  221. */
  222. public function setBody($body)
  223. {
  224. $old = $this->_body;
  225. $this->_body = $body;
  226. return $old;
  227. }
  228. /**
  229. * Get request body.
  230. *
  231. * @return string
  232. */
  233. public function getBody()
  234. {
  235. return $this->_body;
  236. }
  237. /**
  238. * Dump (parse^-1).
  239. *
  240. * @return string
  241. */
  242. public function __toString()
  243. {
  244. $out = null;
  245. foreach ($this->getHeaders() as $key => $value) {
  246. $out .= $key . ': ' . $value . CRLF;
  247. }
  248. return $out;
  249. }
  250. }
  251. /**
  252. * Flex entity.
  253. */
  254. Consistency::flexEntity('Hoa\Http\Http');