Response.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace Mpdf\PsrHttpMessageShim;
  3. use Psr\Http\Message\StreamInterface;
  4. /**
  5. * PSR-7 URI implementation ported from nyholm/psr7 and adapted for PHP 5.6
  6. *
  7. * @link https://github.com/Nyholm/psr7/blob/master/src/Uri.php
  8. */
  9. class Response implements \Psr\Http\Message\ResponseInterface
  10. {
  11. /** @var array Map of standard HTTP status code/reason phrases */
  12. private static $phrases = [
  13. 100 => 'Continue', 101 => 'Switching Protocols', 102 => 'Processing',
  14. 200 => 'OK', 201 => 'Created', 202 => 'Accepted', 203 => 'Non-Authoritative Information', 204 => 'No Content', 205 => 'Reset Content', 206 => 'Partial Content', 207 => 'Multi-status', 208 => 'Already Reported',
  15. 300 => 'Multiple Choices', 301 => 'Moved Permanently', 302 => 'Found', 303 => 'See Other', 304 => 'Not Modified', 305 => 'Use Proxy', 306 => 'Switch Proxy', 307 => 'Temporary Redirect',
  16. 400 => 'Bad Request', 401 => 'Unauthorized', 402 => 'Payment Required', 403 => 'Forbidden', 404 => 'Not Found', 405 => 'Method Not Allowed', 406 => 'Not Acceptable', 407 => 'Proxy Authentication Required', 408 => 'Request Time-out', 409 => 'Conflict', 410 => 'Gone', 411 => 'Length Required', 412 => 'Precondition Failed', 413 => 'Request Entity Too Large', 414 => 'Request-URI Too Large', 415 => 'Unsupported Media Type', 416 => 'Requested range not satisfiable', 417 => 'Expectation Failed', 418 => 'I\'m a teapot', 422 => 'Unprocessable Entity', 423 => 'Locked', 424 => 'Failed Dependency', 425 => 'Unordered Collection', 426 => 'Upgrade Required', 428 => 'Precondition Required', 429 => 'Too Many Requests', 431 => 'Request Header Fields Too Large', 451 => 'Unavailable For Legal Reasons',
  17. 500 => 'Internal Server Error', 501 => 'Not Implemented', 502 => 'Bad Gateway', 503 => 'Service Unavailable', 504 => 'Gateway Time-out', 505 => 'HTTP Version not supported', 506 => 'Variant Also Negotiates', 507 => 'Insufficient Storage', 508 => 'Loop Detected', 511 => 'Network Authentication Required',
  18. ];
  19. /** @var string */
  20. private $reasonPhrase;
  21. /** @var int */
  22. private $statusCode;
  23. /** @var array Map of all registered headers, as original name => array of values */
  24. private $headers = [];
  25. /** @var array Map of lowercase header name => original name at registration */
  26. private $headerNames = [];
  27. /** @var string */
  28. private $protocol;
  29. /** @var \Psr\Http\Message\StreamInterface */
  30. private $stream;
  31. /**
  32. * @param int $status Status code
  33. * @param array $headers Response headers
  34. * @param string|resource|StreamInterface|null $body Response body
  35. * @param string $version Protocol version
  36. * @param string|null $reason Reason phrase (when empty a default will be used based on the status code)
  37. */
  38. public function __construct($status = 200, array $headers = [], $body = null, $version = '1.1', $reason = null)
  39. {
  40. // If we got no body, defer initialization of the stream until Response::getBody()
  41. if ('' !== $body && null !== $body) {
  42. $this->stream = Stream::create($body);
  43. }
  44. $this->statusCode = $status;
  45. $this->setHeaders($headers);
  46. if (null === $reason && isset(self::$phrases[$this->statusCode])) {
  47. $this->reasonPhrase = self::$phrases[$status];
  48. } else {
  49. $this->reasonPhrase = isset($reason) ? $reason : '';
  50. }
  51. $this->protocol = $version;
  52. }
  53. public function getStatusCode()
  54. {
  55. return $this->statusCode;
  56. }
  57. public function getReasonPhrase()
  58. {
  59. return $this->reasonPhrase;
  60. }
  61. public function withStatus($code, $reasonPhrase = '')
  62. {
  63. if (!\is_int($code) && !\is_string($code)) {
  64. throw new \InvalidArgumentException('Status code has to be an integer');
  65. }
  66. $code = (int) $code;
  67. if ($code < 100 || $code > 599) {
  68. throw new \InvalidArgumentException(\sprintf('Status code has to be an integer between 100 and 599. A status code of %d was given', $code));
  69. }
  70. $new = clone $this;
  71. $new->statusCode = $code;
  72. if ((null === $reasonPhrase || '' === $reasonPhrase) && isset(self::$phrases[$new->statusCode])) {
  73. $reasonPhrase = self::$phrases[$new->statusCode];
  74. }
  75. $new->reasonPhrase = $reasonPhrase;
  76. return $new;
  77. }
  78. public function getProtocolVersion()
  79. {
  80. return $this->protocol;
  81. }
  82. public function withProtocolVersion($version)
  83. {
  84. if ($this->protocol === $version) {
  85. return $this;
  86. }
  87. $new = clone $this;
  88. $new->protocol = $version;
  89. return $new;
  90. }
  91. public function getHeaders()
  92. {
  93. return $this->headers;
  94. }
  95. public function hasHeader($header)
  96. {
  97. return isset($this->headerNames[strtolower($header)]);
  98. }
  99. public function getHeader($header)
  100. {
  101. $header = strtolower($header);
  102. if (!isset($this->headerNames[$header])) {
  103. return [];
  104. }
  105. $header = $this->headerNames[$header];
  106. return $this->headers[$header];
  107. }
  108. public function getHeaderLine($header)
  109. {
  110. return implode(', ', $this->getHeader($header));
  111. }
  112. public function withHeader($header, $value)
  113. {
  114. if (!is_array($value)) {
  115. $value = [$value];
  116. }
  117. $value = $this->trimHeaderValues($value);
  118. $normalized = strtolower($header);
  119. $new = clone $this;
  120. if (isset($new->headerNames[$normalized])) {
  121. unset($new->headers[$new->headerNames[$normalized]]);
  122. }
  123. $new->headerNames[$normalized] = $header;
  124. $new->headers[$header] = $value;
  125. return $new;
  126. }
  127. public function withAddedHeader($header, $value)
  128. {
  129. if (!is_array($value)) {
  130. $value = [$value];
  131. }
  132. $value = $this->trimHeaderValues($value);
  133. $normalized = strtolower($header);
  134. $new = clone $this;
  135. if (isset($new->headerNames[$normalized])) {
  136. $header = $this->headerNames[$normalized];
  137. $new->headers[$header] = array_merge($this->headers[$header], $value);
  138. } else {
  139. $new->headerNames[$normalized] = $header;
  140. $new->headers[$header] = $value;
  141. }
  142. return $new;
  143. }
  144. public function withoutHeader($header)
  145. {
  146. $normalized = strtolower($header);
  147. if (!isset($this->headerNames[$normalized])) {
  148. return $this;
  149. }
  150. $header = $this->headerNames[$normalized];
  151. $new = clone $this;
  152. unset($new->headers[$header], $new->headerNames[$normalized]);
  153. return $new;
  154. }
  155. public function getBody()
  156. {
  157. if (!$this->stream) {
  158. $this->stream = Stream::create('');
  159. }
  160. return $this->stream;
  161. }
  162. public function withBody(StreamInterface $body)
  163. {
  164. if ($body === $this->stream) {
  165. return $this;
  166. }
  167. $new = clone $this;
  168. $new->stream = $body;
  169. return $new;
  170. }
  171. private function setHeaders(array $headers)
  172. {
  173. $this->headerNames = $this->headers = [];
  174. foreach ($headers as $header => $value) {
  175. if (!is_array($value)) {
  176. $value = [$value];
  177. }
  178. $value = $this->trimHeaderValues($value);
  179. $normalized = strtolower($header);
  180. if (isset($this->headerNames[$normalized])) {
  181. $header = $this->headerNames[$normalized];
  182. $this->headers[$header] = array_merge($this->headers[$header], $value);
  183. } else {
  184. $this->headerNames[$normalized] = $header;
  185. $this->headers[$header] = $value;
  186. }
  187. }
  188. }
  189. /**
  190. * Trims whitespace from the header values.
  191. *
  192. * Spaces and tabs ought to be excluded by parsers when extracting the field value from a header field.
  193. *
  194. * header-field = field-name ":" OWS field-value OWS
  195. * OWS = *( SP / HTAB )
  196. *
  197. * @param string[] $values Header values
  198. *
  199. * @return string[] Trimmed header values
  200. *
  201. * @see https://tools.ietf.org/html/rfc7230#section-3.2.4
  202. */
  203. private function trimHeaderValues(array $values)
  204. {
  205. return array_map(function ($value) {
  206. return trim($value, " \t");
  207. }, $values);
  208. }
  209. }