Message.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. <?php
  2. /**
  3. * Lithium: the most rad php framework
  4. *
  5. * @copyright Copyright 2013, Union of RAD (http://union-of-rad.org)
  6. * @license http://opensource.org/licenses/bsd-license.php The BSD License
  7. */
  8. namespace lithium\net\http;
  9. /**
  10. * Base class for `lithium\net\http\Request` and `lithium\net\http\Response`. Implements basic
  11. * protocol handling for HTTP-based transactions.
  12. */
  13. class Message extends \lithium\net\Message {
  14. /**
  15. * The full protocol: HTTP/1.1
  16. *
  17. * @var string
  18. */
  19. public $protocol = null;
  20. /**
  21. * Specification version number
  22. *
  23. * @var string
  24. */
  25. public $version = '1.1';
  26. /**
  27. * headers
  28. *
  29. * @var array
  30. */
  31. public $headers = array();
  32. /**
  33. * Content-Type
  34. *
  35. * @var string
  36. */
  37. protected $_type = null;
  38. /**
  39. * Classes used by `Request`.
  40. *
  41. * @var array
  42. */
  43. protected $_classes = array(
  44. 'media' => 'lithium\net\http\Media',
  45. 'auth' => 'lithium\net\http\Auth'
  46. );
  47. /**
  48. * Adds config values to the public properties when a new object is created.
  49. *
  50. * @param array $config Configuration options : default value
  51. * - `scheme`: http
  52. * - `host`: localhost
  53. * - `port`: null
  54. * - `username`: null
  55. * - `password`: null
  56. * - `path`: null
  57. * - `version`: 1.1
  58. * - `headers`: array
  59. * - `body`: null
  60. */
  61. public function __construct(array $config = array()) {
  62. $defaults = array(
  63. 'scheme' => 'http',
  64. 'host' => 'localhost',
  65. 'port' => null,
  66. 'username' => null,
  67. 'password' => null,
  68. 'path' => null,
  69. 'query' => array(),
  70. 'fragment' => null,
  71. 'protocol' => null,
  72. 'version' => '1.1',
  73. 'headers' => array(),
  74. 'body' => null,
  75. 'auth' => null
  76. );
  77. $config += $defaults;
  78. parent::__construct($config);
  79. foreach (array_intersect_key(array_filter($config), $defaults) as $key => $value) {
  80. $this->{$key} = $value;
  81. }
  82. if (strpos($this->host, '/') !== false) {
  83. list($this->host, $this->path) = explode('/', $this->host, 2);
  84. }
  85. $this->path = str_replace('//', '/', "/{$this->path}");
  86. $this->protocol = $this->protocol ?: "HTTP/{$this->version}";
  87. }
  88. /**
  89. * Add a header to rendered output, or return a single header or full header list.
  90. *
  91. * @param string $key
  92. * @param string $value
  93. * @return array
  94. */
  95. public function headers($key = null, $value = null) {
  96. if (is_string($key) && strpos($key, ':') === false) {
  97. if ($value === null) {
  98. return isset($this->headers[$key]) ? $this->headers[$key] : null;
  99. }
  100. if ($value === false) {
  101. unset($this->headers[$key]);
  102. return $this->headers;
  103. }
  104. }
  105. foreach (($value ? array($key => $value) : (array) $key) as $header => $value) {
  106. if (!is_string($header)) {
  107. if (preg_match('/(.*?):(.+)/', $value, $match)) {
  108. $this->headers[$match[1]] = trim($match[2]);
  109. }
  110. } else {
  111. $this->headers[$header] = $value;
  112. }
  113. }
  114. $headers = array();
  115. foreach ($this->headers as $key => $value) {
  116. if (is_array($value)) {
  117. foreach ($value as $val) {
  118. $headers[] = "{$key}: {$val}";
  119. }
  120. continue;
  121. }
  122. $headers[] = "{$key}: {$value}";
  123. }
  124. return $headers;
  125. }
  126. /**
  127. * Sets/gets the content type.
  128. *
  129. * @param string $type A full content type i.e. `'application/json'` or simple name `'json'`
  130. * @return string A simple content type name, i.e. `'html'`, `'xml'`, `'json'`, etc., depending
  131. * on the content type of the request.
  132. */
  133. public function type($type = null) {
  134. if ($type === false) {
  135. unset($this->headers['Content-Type']);
  136. $this->_type = false;
  137. return;
  138. }
  139. $media = $this->_classes['media'];
  140. if (!$type && $this->_type) {
  141. return $this->_type;
  142. }
  143. $headers = $this->headers + array('Content-Type' => null);
  144. $type = $type ?: $headers['Content-Type'];
  145. if (!$type) {
  146. return;
  147. }
  148. $header = $type;
  149. if (!$data = $media::type($type)) {
  150. $this->headers('Content-Type', $type);
  151. return ($this->_type = $type);
  152. }
  153. if (is_string($data)) {
  154. $type = $data;
  155. } else if (!empty($data['content'])) {
  156. $header = is_array($data['content']) ? reset($data['content']) : $data['content'];
  157. }
  158. $this->headers('Content-Type', $header);
  159. return ($this->_type = $type);
  160. }
  161. /**
  162. * Add body parts.
  163. *
  164. * @param mixed $data
  165. * @param array $options
  166. * - `'buffer'`: split the body string
  167. * @return array
  168. */
  169. public function body($data = null, $options = array()) {
  170. $default = array('buffer' => null, 'encode' => false, 'decode' => false);
  171. $options += $default;
  172. $body = $this->body = array_filter(array_merge((array) $this->body, (array) $data));
  173. if (empty($options['buffer']) && empty($body)) {
  174. return "";
  175. }
  176. if ($options['encode']) {
  177. $body = $this->_encode($body);
  178. }
  179. $body = is_array($body) ? join("\r\n", $body) : $body;
  180. if ($options['decode']) {
  181. $body = $this->_decode($body);
  182. }
  183. return ($options['buffer']) ? str_split($body, $options['buffer']) : $body;
  184. }
  185. /**
  186. * Encodes the body based on the type
  187. *
  188. * @see lithium\net\http\Message::type()
  189. * @param mixed $body
  190. * @return string
  191. */
  192. protected function _encode($body) {
  193. $media = $this->_classes['media'];
  194. if ($type = $media::type($this->_type)) {
  195. $body = $media::encode($this->_type, $body) ?: $body;
  196. }
  197. return $body;
  198. }
  199. /**
  200. * Decodes the body based on the type
  201. *
  202. * @param string $body
  203. * @return mixed
  204. */
  205. protected function _decode($body) {
  206. $media = $this->_classes['media'];
  207. if (!$type = $media::type($this->_type)) {
  208. return $body;
  209. }
  210. return $media::decode($this->_type, $body) ?: $body;
  211. }
  212. }
  213. ?>