HttpStatus.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. <?php
  2. /**
  3. * List of HTTP status codes.
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License along
  16. * with this program; if not, write to the Free Software Foundation, Inc.,
  17. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  18. * http://www.gnu.org/copyleft/gpl.html
  19. *
  20. * @file
  21. */
  22. /**
  23. * @todo document
  24. */
  25. class HttpStatus {
  26. /**
  27. * Get the message associated with an HTTP response status code
  28. *
  29. * @param int $code Status code
  30. * @return string|null Message, or null if $code is not known
  31. */
  32. public static function getMessage( $code ) {
  33. static $statusMessage = [
  34. 100 => 'Continue',
  35. 101 => 'Switching Protocols',
  36. 102 => 'Processing',
  37. 200 => 'OK',
  38. 201 => 'Created',
  39. 202 => 'Accepted',
  40. 203 => 'Non-Authoritative Information',
  41. 204 => 'No Content',
  42. 205 => 'Reset Content',
  43. 206 => 'Partial Content',
  44. 207 => 'Multi-Status',
  45. 300 => 'Multiple Choices',
  46. 301 => 'Moved Permanently',
  47. 302 => 'Found',
  48. 303 => 'See Other',
  49. 304 => 'Not Modified',
  50. 305 => 'Use Proxy',
  51. 307 => 'Temporary Redirect',
  52. 400 => 'Bad Request',
  53. 401 => 'Unauthorized',
  54. 402 => 'Payment Required',
  55. 403 => 'Forbidden',
  56. 404 => 'Not Found',
  57. 405 => 'Method Not Allowed',
  58. 406 => 'Not Acceptable',
  59. 407 => 'Proxy Authentication Required',
  60. 408 => 'Request Timeout',
  61. 409 => 'Conflict',
  62. 410 => 'Gone',
  63. 411 => 'Length Required',
  64. 412 => 'Precondition Failed',
  65. 413 => 'Request Entity Too Large',
  66. 414 => 'Request-URI Too Large',
  67. 415 => 'Unsupported Media Type',
  68. 416 => 'Request Range Not Satisfiable',
  69. 417 => 'Expectation Failed',
  70. 422 => 'Unprocessable Entity',
  71. 423 => 'Locked',
  72. 424 => 'Failed Dependency',
  73. 428 => 'Precondition Required',
  74. 429 => 'Too Many Requests',
  75. 431 => 'Request Header Fields Too Large',
  76. 500 => 'Internal Server Error',
  77. 501 => 'Not Implemented',
  78. 502 => 'Bad Gateway',
  79. 503 => 'Service Unavailable',
  80. 504 => 'Gateway Timeout',
  81. 505 => 'HTTP Version Not Supported',
  82. 507 => 'Insufficient Storage',
  83. 511 => 'Network Authentication Required',
  84. ];
  85. return $statusMessage[$code] ?? null;
  86. }
  87. /**
  88. * Output an HTTP status code header
  89. *
  90. * @since 1.26
  91. * @param int $code Status code
  92. */
  93. public static function header( $code ) {
  94. static $version = null;
  95. $message = self::getMessage( $code );
  96. if ( $message === null ) {
  97. trigger_error( "Unknown HTTP status code $code", E_USER_WARNING );
  98. return;
  99. }
  100. MediaWiki\HeaderCallback::warnIfHeadersSent();
  101. if ( $version === null ) {
  102. $version = isset( $_SERVER['SERVER_PROTOCOL'] ) &&
  103. $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.0' ?
  104. '1.0' :
  105. '1.1';
  106. }
  107. header( "HTTP/$version $code $message" );
  108. }
  109. }