MediaWiki
master
Toggle main menu visibility
HttpStatus.php
Go to the documentation of this file.
1
<?php
8
9
namespace
Wikimedia\Http;
10
11
use InvalidArgumentException;
12
16
class
HttpStatus
{
17
21
private
static
$headersSentCallback =
null
;
22
23
public
static
function
registerHeadersSentCallback
( callable $callback ): ?callable {
24
$old = self::$headersSentCallback;
25
self::$headersSentCallback = $callback;
26
27
return
$old;
28
}
29
36
public
static
function
getMessage
( $code ) {
37
static
$statusMessage = [
38
100 =>
'Continue'
,
39
101 =>
'Switching Protocols'
,
40
102 =>
'Processing'
,
41
200 =>
'OK'
,
42
201 =>
'Created'
,
43
202 =>
'Accepted'
,
44
203 =>
'Non-Authoritative Information'
,
45
204 =>
'No Content'
,
46
205 =>
'Reset Content'
,
47
206 =>
'Partial Content'
,
48
207 =>
'Multi-Status'
,
49
300 =>
'Multiple Choices'
,
50
301 =>
'Moved Permanently'
,
51
302 =>
'Found'
,
52
303 =>
'See Other'
,
53
304 =>
'Not Modified'
,
54
305 =>
'Use Proxy'
,
55
307 =>
'Temporary Redirect'
,
56
400 =>
'Bad Request'
,
57
401 =>
'Unauthorized'
,
58
402 =>
'Payment Required'
,
59
403 =>
'Forbidden'
,
60
404 =>
'Not Found'
,
61
405 =>
'Method Not Allowed'
,
62
406 =>
'Not Acceptable'
,
63
407 =>
'Proxy Authentication Required'
,
64
408 =>
'Request Timeout'
,
65
409 =>
'Conflict'
,
66
410 =>
'Gone'
,
67
411 =>
'Length Required'
,
68
412 =>
'Precondition Failed'
,
69
413 =>
'Request Entity Too Large'
,
70
414 =>
'Request-URI Too Large'
,
71
415 =>
'Unsupported Media Type'
,
72
416 =>
'Request Range Not Satisfiable'
,
73
417 =>
'Expectation Failed'
,
74
422 =>
'Unprocessable Entity'
,
75
423 =>
'Locked'
,
76
424 =>
'Failed Dependency'
,
77
428 =>
'Precondition Required'
,
78
429 =>
'Too Many Requests'
,
79
431 =>
'Request Header Fields Too Large'
,
80
500 =>
'Internal Server Error'
,
81
501 =>
'Not Implemented'
,
82
502 =>
'Bad Gateway'
,
83
503 =>
'Service Unavailable'
,
84
504 =>
'Gateway Timeout'
,
85
505 =>
'HTTP Version Not Supported'
,
86
507 =>
'Insufficient Storage'
,
87
511 =>
'Network Authentication Required'
,
88
];
89
return
$statusMessage[$code] ??
null
;
90
}
91
99
public
static
function
getHeader
( $code ): string {
100
static $version = null;
101
$message = self::getMessage( $code );
102
if
( $message ===
null
) {
103
throw
new
InvalidArgumentException(
"Unknown HTTP status code $code"
);
104
}
105
106
if
( $version ===
null
) {
107
$version = isset( $_SERVER[
'SERVER_PROTOCOL'
] ) &&
108
$_SERVER[
'SERVER_PROTOCOL'
] ===
'HTTP/1.0'
?
109
'1.0'
:
110
'1.1'
;
111
}
112
113
return
"HTTP/$version $code $message"
;
114
}
115
122
public
static
function
header
( $code ) {
123
if
(
HttpHeader::headersSent
() ) {
124
if
( self::$headersSentCallback ) {
125
( self::$headersSentCallback )();
126
return
;
127
}
128
129
// NOTE: If there is no custom callback, we continue normally and
130
// rely on the implementation of header() to emit a warning.
131
}
132
133
try
{
134
HttpHeader::send
( self::getHeader( $code ) );
135
}
catch
( InvalidArgumentException ) {
136
trigger_error(
"Unknown HTTP status code $code"
, E_USER_WARNING );
137
}
138
}
139
140
}
141
143
class_alias( HttpStatus::class,
'HttpStatus'
);
Wikimedia\Http\HttpHeader\send
static send(string $header, bool $replace=true, int $responseCode=0)
Definition
HttpHeader.php:25
Wikimedia\Http\HttpHeader\headersSent
static headersSent(?string &$filename=null, ?int &$line=null)
Definition
HttpHeader.php:31
Wikimedia\Http\HttpStatus
Definition
HttpStatus.php:16
Wikimedia\Http\HttpStatus\registerHeadersSentCallback
static registerHeadersSentCallback(callable $callback)
Definition
HttpStatus.php:23
Wikimedia\Http\HttpStatus\getHeader
static getHeader( $code)
Construct an HTTP status code header.
Definition
HttpStatus.php:99
Wikimedia\Http\HttpStatus\header
static header( $code)
Output an HTTP status code header.
Definition
HttpStatus.php:122
Wikimedia\Http\HttpStatus\getMessage
static getMessage( $code)
Get the message associated with an HTTP response status code.
Definition
HttpStatus.php:36
includes
libs
Http
HttpStatus.php
Generated on
for MediaWiki by
1.18.0