Response.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. <?php
  2. declare(strict_types=1);
  3. namespace Sabre\HTTP;
  4. /**
  5. * This class represents a single HTTP response.
  6. *
  7. * @copyright Copyright (C) fruux GmbH (https://fruux.com/)
  8. * @author Evert Pot (http://evertpot.com/)
  9. * @license http://sabre.io/license/ Modified BSD License
  10. */
  11. class Response extends Message implements ResponseInterface
  12. {
  13. /**
  14. * This is the list of currently registered HTTP status codes.
  15. *
  16. * @var array
  17. */
  18. public static $statusCodes = [
  19. 100 => 'Continue',
  20. 101 => 'Switching Protocols',
  21. 102 => 'Processing',
  22. 200 => 'OK',
  23. 201 => 'Created',
  24. 202 => 'Accepted',
  25. 203 => 'Non-Authoritative Information',
  26. 204 => 'No Content',
  27. 205 => 'Reset Content',
  28. 206 => 'Partial Content',
  29. 207 => 'Multi-Status', // RFC 4918
  30. 208 => 'Already Reported', // RFC 5842
  31. 226 => 'IM Used', // RFC 3229
  32. 300 => 'Multiple Choices',
  33. 301 => 'Moved Permanently',
  34. 302 => 'Found',
  35. 303 => 'See Other',
  36. 304 => 'Not Modified',
  37. 305 => 'Use Proxy',
  38. 307 => 'Temporary Redirect',
  39. 308 => 'Permanent Redirect',
  40. 400 => 'Bad Request',
  41. 401 => 'Unauthorized',
  42. 402 => 'Payment Required',
  43. 403 => 'Forbidden',
  44. 404 => 'Not Found',
  45. 405 => 'Method Not Allowed',
  46. 406 => 'Not Acceptable',
  47. 407 => 'Proxy Authentication Required',
  48. 408 => 'Request Timeout',
  49. 409 => 'Conflict',
  50. 410 => 'Gone',
  51. 411 => 'Length Required',
  52. 412 => 'Precondition failed',
  53. 413 => 'Request Entity Too Large',
  54. 414 => 'Request-URI Too Long',
  55. 415 => 'Unsupported Media Type',
  56. 416 => 'Requested Range Not Satisfiable',
  57. 417 => 'Expectation Failed',
  58. 418 => 'I\'m a teapot', // RFC 2324
  59. 421 => 'Misdirected Request', // RFC7540 (HTTP/2)
  60. 422 => 'Unprocessable Entity', // RFC 4918
  61. 423 => 'Locked', // RFC 4918
  62. 424 => 'Failed Dependency', // RFC 4918
  63. 426 => 'Upgrade Required',
  64. 428 => 'Precondition Required', // RFC 6585
  65. 429 => 'Too Many Requests', // RFC 6585
  66. 431 => 'Request Header Fields Too Large', // RFC 6585
  67. 451 => 'Unavailable For Legal Reasons', // draft-tbray-http-legally-restricted-status
  68. 500 => 'Internal Server Error',
  69. 501 => 'Not Implemented',
  70. 502 => 'Bad Gateway',
  71. 503 => 'Service Unavailable',
  72. 504 => 'Gateway Timeout',
  73. 505 => 'HTTP Version not supported',
  74. 506 => 'Variant Also Negotiates',
  75. 507 => 'Insufficient Storage', // RFC 4918
  76. 508 => 'Loop Detected', // RFC 5842
  77. 509 => 'Bandwidth Limit Exceeded', // non-standard
  78. 510 => 'Not extended',
  79. 511 => 'Network Authentication Required', // RFC 6585
  80. ];
  81. /**
  82. * HTTP status code.
  83. *
  84. * @var int
  85. */
  86. protected $status;
  87. /**
  88. * HTTP status text.
  89. *
  90. * @var string
  91. */
  92. protected $statusText;
  93. /**
  94. * Creates the response object.
  95. *
  96. * @param string|int $status
  97. * @param resource $body
  98. */
  99. public function __construct($status = 500, ?array $headers = null, $body = null)
  100. {
  101. if (null !== $status) {
  102. $this->setStatus($status);
  103. }
  104. if (null !== $headers) {
  105. $this->setHeaders($headers);
  106. }
  107. if (null !== $body) {
  108. $this->setBody($body);
  109. }
  110. }
  111. /**
  112. * Returns the current HTTP status code.
  113. */
  114. public function getStatus(): int
  115. {
  116. return $this->status;
  117. }
  118. /**
  119. * Returns the human-readable status string.
  120. *
  121. * In the case of a 200, this may for example be 'OK'.
  122. */
  123. public function getStatusText(): string
  124. {
  125. return $this->statusText;
  126. }
  127. /**
  128. * Sets the HTTP status code.
  129. *
  130. * This can be either the full HTTP status code with human-readable string,
  131. * for example: "403 I can't let you do that, Dave".
  132. *
  133. * Or just the code, in which case the appropriate default message will be
  134. * added.
  135. *
  136. * @param string|int $status
  137. *
  138. * @throws \InvalidArgumentException
  139. */
  140. public function setStatus($status)
  141. {
  142. if (is_int($status) || ctype_digit($status)) {
  143. $statusCode = $status;
  144. $statusText = self::$statusCodes[$status] ?? 'Unknown';
  145. } else {
  146. list(
  147. $statusCode,
  148. $statusText
  149. ) = explode(' ', $status, 2);
  150. $statusCode = (int) $statusCode;
  151. }
  152. if ($statusCode < 100 || $statusCode > 999) {
  153. throw new \InvalidArgumentException('The HTTP status code must be exactly 3 digits');
  154. }
  155. $this->status = $statusCode;
  156. $this->statusText = $statusText;
  157. }
  158. /**
  159. * Serializes the response object as a string.
  160. *
  161. * This is useful for debugging purposes.
  162. */
  163. public function __toString(): string
  164. {
  165. $str = 'HTTP/'.$this->httpVersion.' '.$this->getStatus().' '.$this->getStatusText()."\r\n";
  166. foreach ($this->getHeaders() as $key => $value) {
  167. foreach ($value as $v) {
  168. $str .= $key.': '.$v."\r\n";
  169. }
  170. }
  171. $str .= "\r\n";
  172. $str .= $this->getBodyAsString();
  173. return $str;
  174. }
  175. }