comparison http-response-status-codes.inc @ 5:18aafb1a8986

Better handling of errors and globals.
author Tom Fredrik "BFG" Klaussen <bfg@blenning.no>
date Fri, 20 May 2011 13:25:53 +0200
parents
children 9dab5b96b789
comparison
equal deleted inserted replaced
4:74196528fc64 5:18aafb1a8986
1 <?php
2
3 /**
4
5 * StatusCodes provides named constants for
6
7 * HTTP protocol status codes. Written for the
8
9 * Recess Framework (http://www.recessframework.com/)
10
11 *
12
13 * @author Kris Jordan
14
15 * @license MIT
16
17 * @package recess.http
18
19 */
20
21 class StatusCodes {
22
23 // [Informational 1xx]
24
25 const HTTP_CONTINUE = 100;
26
27 const HTTP_SWITCHING_PROTOCOLS = 101;
28
29 // [Successful 2xx]
30
31 const HTTP_OK = 200;
32
33 const HTTP_CREATED = 201;
34
35 const HTTP_ACCEPTED = 202;
36
37 const HTTP_NONAUTHORITATIVE_INFORMATION = 203;
38
39 const HTTP_NO_CONTENT = 204;
40
41 const HTTP_RESET_CONTENT = 205;
42
43 const HTTP_PARTIAL_CONTENT = 206;
44
45 // [Redirection 3xx]
46
47 const HTTP_MULTIPLE_CHOICES = 300;
48
49 const HTTP_MOVED_PERMANENTLY = 301;
50
51 const HTTP_FOUND = 302;
52
53 const HTTP_SEE_OTHER = 303;
54
55 const HTTP_NOT_MODIFIED = 304;
56
57 const HTTP_USE_PROXY = 305;
58
59 const HTTP_UNUSED= 306;
60
61 const HTTP_TEMPORARY_REDIRECT = 307;
62
63 // [Client Error 4xx]
64
65 const errorCodesBeginAt = 400;
66
67 const HTTP_BAD_REQUEST = 400;
68
69 const HTTP_UNAUTHORIZED = 401;
70
71 const HTTP_PAYMENT_REQUIRED = 402;
72
73 const HTTP_FORBIDDEN = 403;
74
75 const HTTP_NOT_FOUND = 404;
76
77 const HTTP_METHOD_NOT_ALLOWED = 405;
78
79 const HTTP_NOT_ACCEPTABLE = 406;
80
81 const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407;
82
83 const HTTP_REQUEST_TIMEOUT = 408;
84
85 const HTTP_CONFLICT = 409;
86
87 const HTTP_GONE = 410;
88
89 const HTTP_LENGTH_REQUIRED = 411;
90
91 const HTTP_PRECONDITION_FAILED = 412;
92
93 const HTTP_REQUEST_ENTITY_TOO_LARGE = 413;
94
95 const HTTP_REQUEST_URI_TOO_LONG = 414;
96
97 const HTTP_UNSUPPORTED_MEDIA_TYPE = 415;
98
99 const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416;
100
101 const HTTP_EXPECTATION_FAILED = 417;
102
103 // [Server Error 5xx]
104
105 const HTTP_INTERNAL_SERVER_ERROR = 500;
106
107 const HTTP_NOT_IMPLEMENTED = 501;
108
109 const HTTP_BAD_GATEWAY = 502;
110
111 const HTTP_SERVICE_UNAVAILABLE = 503;
112
113 const HTTP_GATEWAY_TIMEOUT = 504;
114
115 const HTTP_VERSION_NOT_SUPPORTED = 505;
116
117
118
119 private static $messages = array(
120
121 // [Informational 1xx]
122
123 100=>'100 Continue',
124
125 101=>'101 Switching Protocols',
126
127 // [Successful 2xx]
128
129 200=>'200 OK',
130
131 201=>'201 Created',
132
133 202=>'202 Accepted',
134
135 203=>'203 Non-Authoritative Information',
136
137 204=>'204 No Content',
138
139 205=>'205 Reset Content',
140
141 206=>'206 Partial Content',
142
143 // [Redirection 3xx]
144
145 300=>'300 Multiple Choices',
146
147 301=>'301 Moved Permanently',
148
149 302=>'302 Found',
150
151 303=>'303 See Other',
152
153 304=>'304 Not Modified',
154
155 305=>'305 Use Proxy',
156
157 306=>'306 (Unused)',
158
159 307=>'307 Temporary Redirect',
160
161 // [Client Error 4xx]
162
163 400=>'400 Bad Request',
164
165 401=>'401 Unauthorized',
166
167 402=>'402 Payment Required',
168
169 403=>'403 Forbidden',
170
171 404=>'404 Not Found',
172
173 405=>'405 Method Not Allowed',
174
175 406=>'406 Not Acceptable',
176
177 407=>'407 Proxy Authentication Required',
178
179 408=>'408 Request Timeout',
180
181 409=>'409 Conflict',
182
183 410=>'410 Gone',
184
185 411=>'411 Length Required',
186
187 412=>'412 Precondition Failed',
188
189 413=>'413 Request Entity Too Large',
190
191 414=>'414 Request-URI Too Long',
192
193 415=>'415 Unsupported Media Type',
194
195 416=>'416 Requested Range Not Satisfiable',
196
197 417=>'417 Expectation Failed',
198
199 // [Server Error 5xx]
200
201 500=>'500 Internal Server Error',
202
203 501=>'501 Not Implemented',
204
205 502=>'502 Bad Gateway',
206
207 503=>'503 Service Unavailable',
208
209 504=>'504 Gateway Timeout',
210
211 505=>'505 HTTP Version Not Supported'
212
213 );
214
215
216
217 public static function httpHeaderFor($code) {
218
219 return 'HTTP/1.1 ' . self::$messages[$code];
220
221 }
222
223 public static function getMessageForCode($code) {
224
225 return self::$messages[$code];
226
227 }
228
229
230 public static function isError($code) {
231
232 return is_numeric($code) && $code >= self::HTTP_BAD_REQUEST;
233
234 }
235
236
237 public static function canHaveBody($code) {
238
239 return
240
241 // True if not in 100s
242
243 ($code < self::HTTP_CONTINUE || $code >= self::HTTP_OK)
244
245 && // and not 204 NO CONTENT
246
247 $code != self::HTTP_NO_CONTENT
248
249 && // and not 304 NOT MODIFIED
250
251 $code != self::HTTP_NOT_MODIFIED;
252
253 }
254 }
255
256 ?>