MediaWiki master
HttpStatus.php
Go to the documentation of this file.
1<?php
27
31 private static $headersSentCallback = null;
32
33 public static function registerHeadersSentCallback( callable $callback ): ?callable {
34 $old = self::$headersSentCallback;
35 self::$headersSentCallback = $callback;
36
37 return $old;
38 }
39
46 public static function getMessage( $code ) {
47 static $statusMessage = [
48 100 => 'Continue',
49 101 => 'Switching Protocols',
50 102 => 'Processing',
51 200 => 'OK',
52 201 => 'Created',
53 202 => 'Accepted',
54 203 => 'Non-Authoritative Information',
55 204 => 'No Content',
56 205 => 'Reset Content',
57 206 => 'Partial Content',
58 207 => 'Multi-Status',
59 300 => 'Multiple Choices',
60 301 => 'Moved Permanently',
61 302 => 'Found',
62 303 => 'See Other',
63 304 => 'Not Modified',
64 305 => 'Use Proxy',
65 307 => 'Temporary Redirect',
66 400 => 'Bad Request',
67 401 => 'Unauthorized',
68 402 => 'Payment Required',
69 403 => 'Forbidden',
70 404 => 'Not Found',
71 405 => 'Method Not Allowed',
72 406 => 'Not Acceptable',
73 407 => 'Proxy Authentication Required',
74 408 => 'Request Timeout',
75 409 => 'Conflict',
76 410 => 'Gone',
77 411 => 'Length Required',
78 412 => 'Precondition Failed',
79 413 => 'Request Entity Too Large',
80 414 => 'Request-URI Too Large',
81 415 => 'Unsupported Media Type',
82 416 => 'Request Range Not Satisfiable',
83 417 => 'Expectation Failed',
84 422 => 'Unprocessable Entity',
85 423 => 'Locked',
86 424 => 'Failed Dependency',
87 428 => 'Precondition Required',
88 429 => 'Too Many Requests',
89 431 => 'Request Header Fields Too Large',
90 500 => 'Internal Server Error',
91 501 => 'Not Implemented',
92 502 => 'Bad Gateway',
93 503 => 'Service Unavailable',
94 504 => 'Gateway Timeout',
95 505 => 'HTTP Version Not Supported',
96 507 => 'Insufficient Storage',
97 511 => 'Network Authentication Required',
98 ];
99 return $statusMessage[$code] ?? null;
100 }
101
109 public static function getHeader( $code ): string {
110 static $version = null;
111 $message = self::getMessage( $code );
112 if ( $message === null ) {
113 throw new InvalidArgumentException( "Unknown HTTP status code $code" );
114 }
115
116 if ( $version === null ) {
117 $version = isset( $_SERVER['SERVER_PROTOCOL'] ) &&
118 $_SERVER['SERVER_PROTOCOL'] === 'HTTP/1.0' ?
119 '1.0' :
120 '1.1';
121 }
122
123 return "HTTP/$version $code $message";
124 }
125
132 public static function header( $code ) {
133 if ( headers_sent() ) {
134 if ( self::$headersSentCallback ) {
135 ( self::$headersSentCallback )();
136 return;
137 }
138
139 // NOTE: If there is no custom callback, we continue normally and
140 // rely on the implementation of header() to emit a warning.
141 }
142
143 try {
144 header( self::getHeader( $code ) );
145 } catch ( InvalidArgumentException $ex ) {
146 trigger_error( "Unknown HTTP status code $code", E_USER_WARNING );
147 }
148 }
149
150}
static header( $code)
Output an HTTP status code header.
static getHeader( $code)
Construct an HTTP status code header.
static getMessage( $code)
Get the message associated with an HTTP response status code.
static registerHeadersSentCallback(callable $callback)