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