Message.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\HTTP;
  4. /**
  5. * This is the abstract base class for both the Request and Response objects.
  6. *
  7. * This object contains a few simple methods that are shared by both.
  8. *
  9. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  10. * @author Evert Pot (http://evertpot.com/)
  11. * @license http://sabre.io/license/ Modified BSD License
  12. */
  13. abstract class Message implements MessageInterface
  14. {
  15. /**
  16. * Request body.
  17. *
  18. * This should be a stream resource, string or a callback writing the body to php://output
  19. *
  20. * @var resource|string|callable
  21. */
  22. protected $body;
  23. /**
  24. * Contains the list of HTTP headers.
  25. *
  26. * @var array
  27. */
  28. protected $headers = [];
  29. /**
  30. * HTTP message version (1.0, 1.1 or 2.0).
  31. *
  32. * @var string
  33. */
  34. protected $httpVersion = '1.1';
  35. /**
  36. * Returns the body as a readable stream resource.
  37. *
  38. * Note that the stream may not be rewindable, and therefore may only be
  39. * read once.
  40. *
  41. * @return resource
  42. */
  43. public function getBodyAsStream()
  44. {
  45. $body = $this->getBody();
  46. if (is_callable($this->body)) {
  47. $body = $this->getBodyAsString();
  48. }
  49. if (is_string($body) || null === $body) {
  50. $stream = fopen('php://temp', 'r+');
  51. fwrite($stream, (string) $body);
  52. rewind($stream);
  53. return $stream;
  54. }
  55. return $body;
  56. }
  57. /**
  58. * Returns the body as a string.
  59. *
  60. * Note that because the underlying data may be based on a stream, this
  61. * method could only work correctly the first time.
  62. */
  63. public function getBodyAsString(): string
  64. {
  65. $body = $this->getBody();
  66. if (is_string($body)) {
  67. return $body;
  68. }
  69. if (null === $body) {
  70. return '';
  71. }
  72. if (is_callable($body)) {
  73. ob_start();
  74. $body();
  75. return ob_get_clean();
  76. }
  77. /**
  78. * @var string|int|null
  79. */
  80. $contentLength = $this->getHeader('Content-Length');
  81. if (null !== $contentLength && (is_int($contentLength) || ctype_digit($contentLength))) {
  82. return stream_get_contents($body, (int) $contentLength);
  83. }
  84. return stream_get_contents($body);
  85. }
  86. /**
  87. * Returns the message body, as its internal representation.
  88. *
  89. * This could be either a string, a stream or a callback writing the body to php://output.
  90. *
  91. * @return resource|string|callable
  92. */
  93. public function getBody()
  94. {
  95. return $this->body;
  96. }
  97. /**
  98. * Replaces the body resource with a new stream, string or a callback writing the body to php://output.
  99. *
  100. * @param resource|string|callable $body
  101. */
  102. public function setBody($body)
  103. {
  104. $this->body = $body;
  105. }
  106. /**
  107. * Returns all the HTTP headers as an array.
  108. *
  109. * Every header is returned as an array, with one or more values.
  110. */
  111. public function getHeaders(): array
  112. {
  113. $result = [];
  114. foreach ($this->headers as $headerInfo) {
  115. $result[$headerInfo[0]] = $headerInfo[1];
  116. }
  117. return $result;
  118. }
  119. /**
  120. * Will return true or false, depending on if a HTTP header exists.
  121. */
  122. public function hasHeader(string $name): bool
  123. {
  124. return isset($this->headers[strtolower($name)]);
  125. }
  126. /**
  127. * Returns a specific HTTP header, based on its name.
  128. *
  129. * The name must be treated as case-insensitive.
  130. * If the header does not exist, this method must return null.
  131. *
  132. * If a header appeared more than once in a HTTP request, this method will
  133. * concatenate all the values with a comma.
  134. *
  135. * Note that this not make sense for all headers. Some, such as
  136. * `Set-Cookie` cannot be logically combined with a comma. In those cases
  137. * you *should* use getHeaderAsArray().
  138. *
  139. * @return string|null
  140. */
  141. public function getHeader(string $name)
  142. {
  143. $name = strtolower($name);
  144. if (isset($this->headers[$name])) {
  145. return implode(',', $this->headers[$name][1]);
  146. }
  147. return null;
  148. }
  149. /**
  150. * Returns a HTTP header as an array.
  151. *
  152. * For every time the HTTP header appeared in the request or response, an
  153. * item will appear in the array.
  154. *
  155. * If the header did not exist, this method will return an empty array.
  156. *
  157. * @return string[]
  158. */
  159. public function getHeaderAsArray(string $name): array
  160. {
  161. $name = strtolower($name);
  162. if (isset($this->headers[$name])) {
  163. return $this->headers[$name][1];
  164. }
  165. return [];
  166. }
  167. /**
  168. * Updates a HTTP header.
  169. *
  170. * The case-sensitivity of the name value must be retained as-is.
  171. *
  172. * If the header already existed, it will be overwritten.
  173. *
  174. * @param string|string[] $value
  175. */
  176. public function setHeader(string $name, $value)
  177. {
  178. $this->headers[strtolower($name)] = [$name, (array) $value];
  179. }
  180. /**
  181. * Sets a new set of HTTP headers.
  182. *
  183. * The headers array should contain headernames for keys, and their value
  184. * should be specified as either a string or an array.
  185. *
  186. * Any header that already existed will be overwritten.
  187. */
  188. public function setHeaders(array $headers)
  189. {
  190. foreach ($headers as $name => $value) {
  191. $this->setHeader($name, $value);
  192. }
  193. }
  194. /**
  195. * Adds a HTTP header.
  196. *
  197. * This method will not overwrite any existing HTTP header, but instead add
  198. * another value. Individual values can be retrieved with
  199. * getHeadersAsArray.
  200. *
  201. * @param string|string[] $value
  202. */
  203. public function addHeader(string $name, $value)
  204. {
  205. $lName = strtolower($name);
  206. if (isset($this->headers[$lName])) {
  207. $this->headers[$lName][1] = array_merge(
  208. $this->headers[$lName][1],
  209. (array) $value
  210. );
  211. } else {
  212. $this->headers[$lName] = [
  213. $name,
  214. (array) $value,
  215. ];
  216. }
  217. }
  218. /**
  219. * Adds a new set of HTTP headers.
  220. *
  221. * Any existing headers will not be overwritten.
  222. */
  223. public function addHeaders(array $headers)
  224. {
  225. foreach ($headers as $name => $value) {
  226. $this->addHeader($name, $value);
  227. }
  228. }
  229. /**
  230. * Removes a HTTP header.
  231. *
  232. * The specified header name must be treated as case-insensitive.
  233. * This method should return true if the header was successfully deleted,
  234. * and false if the header did not exist.
  235. */
  236. public function removeHeader(string $name): bool
  237. {
  238. $name = strtolower($name);
  239. if (!isset($this->headers[$name])) {
  240. return false;
  241. }
  242. unset($this->headers[$name]);
  243. return true;
  244. }
  245. /**
  246. * Sets the HTTP version.
  247. *
  248. * Should be 1.0, 1.1 or 2.0.
  249. */
  250. public function setHttpVersion(string $version)
  251. {
  252. $this->httpVersion = $version;
  253. }
  254. /**
  255. * Returns the HTTP version.
  256. */
  257. public function getHttpVersion(): string
  258. {
  259. return $this->httpVersion;
  260. }
  261. }