comparison StatusCodes.inc.php @ 134:b6b4a58c7625

Using .inc.php rather than just .inc for include files.
author Tom Fredrik Blenning <bfg@bfgconsult.no>
date Sun, 22 Jan 2023 19:22:00 +0100
parents StatusCodes.inc@ff5fc61aa5ea
children
comparison
equal deleted inserted replaced
133:00255ca89459 134:b6b4a58c7625
1 <?php
2 /**
3 * StatusCodes provides named constants for
4 * HTTP protocol status codes. Written for the
5 * Recess Framework (http://www.recessframework.com/)
6 *
7 * @author Kris Jordan
8 * @author Tom Fredrik Blenning Klaussen
9 * @copyright MIT
10 */
11 class StatusCodes
12 {
13 // [Informational 1xx]
14 /**
15 * <a href="http://httpstatus.es/100">HTTP_CONTINUE</a>
16 */
17 const HTTP_CONTINUE = 100;
18 /**
19 * <a href="http://httpstatus.es/101">HTTP_SWITCHING_PROTOCOLS</a>
20 */
21 const HTTP_SWITCHING_PROTOCOLS = 101;
22
23 // [Successful 2xx]
24 /**
25 * <a href="http://httpstatus.es/200">HTTP_OK</a>
26 */
27 const HTTP_OK = 200;
28 /**
29 * <a href="http://httpstatus.es/201">HTTP_CREATED</a>
30 */
31 const HTTP_CREATED = 201;
32 /**
33 * <a href="http://httpstatus.es/202">HTTP_ACCEPTED</a>
34 */
35 const HTTP_ACCEPTED = 202;
36 /**
37 * <a href="http://httpstatus.es/203">HTTP_ACCEPTED</a>
38 */
39 const HTTP_NONAUTHORITATIVE_INFORMATION = 203;
40 /**
41 * <a href="http://httpstatus.es/204">HTTP_NO_CONTENT</a>
42 */
43 const HTTP_NO_CONTENT = 204;
44 /**
45 * <a href="http://httpstatus.es/205">HTTP_RESET_CONTENT</a>
46 */
47 const HTTP_RESET_CONTENT = 205;
48 /**
49 * <a href="http://httpstatus.es/206">HTTP_PARTIAL_CONTENT</a>
50 */
51 const HTTP_PARTIAL_CONTENT = 206;
52
53 // [Redirection 3xx]
54 /**
55 * <a href="http://httpstatus.es/300">HTTP_MULTIPLE_CHOICES</a>
56 */
57 const HTTP_MULTIPLE_CHOICES = 300;
58 /**
59 * <a href="http://httpstatus.es/301">HTTP_MOVED_PERMANENTLY</a>
60 */
61 const HTTP_MOVED_PERMANENTLY = 301;
62 /**
63 * <a href="http://httpstatus.es/302">HTTP_FOUND</a>
64 */
65 const HTTP_FOUND = 302;
66 /**
67 * <a href="http://httpstatus.es/303">HTTP_SEE_OTHER</a>
68 */
69 const HTTP_SEE_OTHER = 303;
70 /**
71 * <a href="http://httpstatus.es/304">HTTP_NOT_MODIFIED</a>
72 */
73 const HTTP_NOT_MODIFIED = 304;
74 /**
75 * <a href="http://httpstatus.es/305">HTTP_USE_PROXY</a>
76 */
77 const HTTP_USE_PROXY = 305;
78 /**
79 * <a href="http://httpstatus.es/306">HTTP_UNUSED</a>
80 */
81 const HTTP_UNUSED = 306;
82 /**
83 * <a href="http://httpstatus.es/307">HTTP_TEMPORARY_REDIRECT</a>
84 */
85 const HTTP_TEMPORARY_REDIRECT = 307;
86
87 // [Client Error 4xx]
88 /**
89 * Defines the beginning of errorCodes
90 * @private
91 */
92 const errorCodesBeginAt = 400;
93 /**
94 * <a href="http://httpstatus.es/400">HTTP_BAD_REQUEST</a>
95 */
96 const HTTP_BAD_REQUEST = 400;
97 /**
98 * <a href="http://httpstatus.es/401">HTTP_UNAUTHORIZED</a>
99 */
100 const HTTP_UNAUTHORIZED = 401;
101 /**
102 * <a href="http://httpstatus.es/402">HTTP_PAYMENT_REQUIRED</a>
103 */
104 const HTTP_PAYMENT_REQUIRED = 402;
105 /**
106 * <a href="http://httpstatus.es/403">HTTP_FORBIDDEN</a>
107 */
108 const HTTP_FORBIDDEN = 403;
109 /**
110 * <a href="http://httpstatus.es/404">HTTP_NOT_FOUND</a>
111 */
112 const HTTP_NOT_FOUND = 404;
113 /**
114 * <a href="http://httpstatus.es/405">HTTP_METHOD_NOT_ALLOWED</a>
115 */
116 const HTTP_METHOD_NOT_ALLOWED = 405;
117 /**
118 * <a href="http://httpstatus.es/406">HTTP_NOT_ACCEPTABLE</a>
119 */
120 const HTTP_NOT_ACCEPTABLE = 406;
121 /**
122 * <a href="http://httpstatus.es/407">HTTP_PROXY_AUTHENTICATION_REQUIRED</a>
123 */
124 const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
125 /**
126 * <a href="http://httpstatus.es/408">HTTP_REQUEST_TIMEOUT</a>
127 */
128 const HTTP_REQUEST_TIMEOUT = 408;
129 /**
130 * <a href="http://httpstatus.es/409">HTTP_CONFLICT</a>
131 */
132 const HTTP_CONFLICT = 409;
133 /**
134 * <a href="http://httpstatus.es/410">HTTP_GONE</a>
135 */
136 const HTTP_GONE = 410;
137 /**
138 * <a href="http://httpstatus.es/411">HTTP_LENGTH_REQUIRED</a>
139 */
140 const HTTP_LENGTH_REQUIRED = 411;
141 /**
142 * <a href="http://httpstatus.es/412">HTTP_PRECONDITION_FAILED</a>
143 */
144 const HTTP_PRECONDITION_FAILED = 412;
145 /**
146 * <a href="http://httpstatus.es/413">HTTP_REQUEST_ENTITY_TOO_LARGE</a>
147 */
148 const HTTP_REQUEST_ENTITY_TOO_LARGE = 413;
149 /**
150 * <a href="http://httpstatus.es/414">HTTP_REQUEST_URI_TOO_LONG</a>
151 */
152 const HTTP_REQUEST_URI_TOO_LONG = 414;
153 /**
154 * <a href="http://httpstatus.es/415">HTTP_UNSUPPORTED_MEDIA_TYPE</a>
155 */
156 const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
157 /**
158 * <a href="http://httpstatus.es/416">HTTP_REQUESTED_RANGE_NOT_SATISFIABLE</a>
159 */
160 const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
161 /**
162 * <a href="http://httpstatus.es/417">HTTP_EXPECTATION_FAILED</a>
163 */
164 const HTTP_EXPECTATION_FAILED = 417;
165
166 // [Server Error 5xx]
167 /**
168 * <a href="http://httpstatus.es/500">HTTP_INTERNAL_SERVER_ERROR</a>
169 */
170 const HTTP_INTERNAL_SERVER_ERROR = 500;
171 /**
172 * <a href="http://httpstatus.es/501">HTTP_NOT_IMPLEMENTED</a>
173 */
174 const HTTP_NOT_IMPLEMENTED = 501;
175 /**
176 * <a href="http://httpstatus.es/502">HTTP_BAD_GATEWAY</a>
177 */
178 const HTTP_BAD_GATEWAY = 502;
179 /**
180 * <a href="http://httpstatus.es/503">HTTP_SERVICE_UNAVAILABLE</a>
181 */
182 const HTTP_SERVICE_UNAVAILABLE = 503;
183 /**
184 * <a href="http://httpstatus.es/504">HTTP_GATEWAY_TIMEOUT</a>
185 */
186 const HTTP_GATEWAY_TIMEOUT = 504;
187 /**
188 * <a href="http://httpstatus.es/505">HTTP_VERSION_NOT_SUPPORTED</a>
189 */
190 const HTTP_VERSION_NOT_SUPPORTED = 505;
191
192 private static $messages =
193 array(
194 // [Informational 1xx]
195 100=>'100 Continue',
196 101=>'101 Switching Protocols',
197
198 // [Successful 2xx]
199 200=>'200 OK',
200 201=>'201 Created',
201 202=>'202 Accepted',
202 203=>'203 Non-Authoritative Information',
203 204=>'204 No Content',
204 205=>'205 Reset Content',
205 206=>'206 Partial Content',
206
207 // [Redirection 3xx]
208 300=>'300 Multiple Choices',
209 301=>'301 Moved Permanently',
210 302=>'302 Found',
211 303=>'303 See Other',
212 304=>'304 Not Modified',
213 305=>'305 Use Proxy',
214 306=>'306 (Unused)',
215 307=>'307 Temporary Redirect',
216
217 // [Client Error 4xx]
218 400=>'400 Bad Request',
219 401=>'401 Unauthorized',
220 402=>'402 Payment Required',
221 403=>'403 Forbidden',
222 404=>'404 Not Found',
223 405=>'405 Method Not Allowed',
224 406=>'406 Not Acceptable',
225 407=>'407 Proxy Authentication Required',
226 408=>'408 Request Timeout',
227 409=>'409 Conflict',
228 410=>'410 Gone',
229 411=>'411 Length Required',
230 412=>'412 Precondition Failed',
231 413=>'413 Request Entity Too Large',
232 414=>'414 Request-URI Too Long',
233 415=>'415 Unsupported Media Type',
234 416=>'416 Requested Range Not Satisfiable',
235 417=>'417 Expectation Failed',
236
237 // [Server Error 5xx]
238 500=>'500 Internal Server Error',
239 501=>'501 Not Implemented',
240 502=>'502 Bad Gateway',
241 503=>'503 Service Unavailable',
242 504=>'504 Gateway Timeout',
243 505=>'505 HTTP Version Not Supported'
244 );
245
246 /**
247 * Get the header for the specified code.
248 *
249 * @param $code Http status code
250 * @return A textual representation of the header
251 */
252 public static function httpHeaderFor($code)
253 {
254 return 'HTTP/1.1 ' . self::$messages[$code];
255 }
256
257 /**
258 * Get a canonical status message for the specified code
259 *
260 * @param $code Http status code
261 * @return Text for the specified code
262 */
263 public static function getMessageForCode($code)
264 {
265 return self::$messages[$code];
266 }
267
268 /**
269 * Checks if the specified code is an error code.
270 *
271 * @param $code Http status code
272 * @return bool Answer
273 */
274 public static function isError($code)
275 {
276 return is_numeric($code) && $code >= self::HTTP_BAD_REQUEST;
277 }
278
279 /**
280 * Can the specified status code have a body?
281 *
282 * @param $code Http status code
283 * @return bool Answer
284 */
285 public static function canHaveBody($code)
286 {
287 return
288 // True if not in 100s
289 ($code < self::HTTP_CONTINUE || $code >= self::HTTP_OK)
290 && // and not 204 NO CONTENT
291 $code != self::HTTP_NO_CONTENT
292 && // and not 304 NOT MODIFIED
293 $code != self::HTTP_NOT_MODIFIED;
294 }
295
296 /**
297 * Extract the numeric code from a header
298 *
299 * @param $header an http top header
300 */
301 public static function codeFromHeader($header)
302 {
303 $matches = array();
304 preg_match('/HTTP\/\S+\s(\d+)/', $header, $matches);
305 if (count($matches) < 1)
306 throw new InvalidArgumentException("Not an http header");
307 $n = $matches[1];
308 return $n;
309 }
310 }
311 ?>