|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the Borobudur-Http package. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Hexacodelabs <http://hexacodelabs.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Borobudur\Http; |
|
12
|
|
|
|
|
13
|
|
|
use Borobudur\Http\Exception\InvalidArgumentException; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* @author Iqbal Maulana <[email protected]> |
|
17
|
|
|
* @created 7/30/15 |
|
18
|
|
|
*/ |
|
19
|
|
|
class HttpStatus |
|
20
|
|
|
{ |
|
21
|
|
|
/** |
|
22
|
|
|
* @const int Status codes |
|
23
|
|
|
*/ |
|
24
|
|
|
const HTTP_CONTINUE = 100; |
|
25
|
|
|
const HTTP_SWITCHING_PROTOCOLS = 101; |
|
26
|
|
|
const HTTP_PROCESSING = 102; // RFC2518 |
|
27
|
|
|
const HTTP_OK = 200; |
|
28
|
|
|
const HTTP_CREATED = 201; |
|
29
|
|
|
const HTTP_ACCEPTED = 202; |
|
30
|
|
|
const HTTP_NON_AUTHORITATIVE_INFORMATION = 203; |
|
31
|
|
|
const HTTP_NO_CONTENT = 204; |
|
32
|
|
|
const HTTP_RESET_CONTENT = 205; |
|
33
|
|
|
const HTTP_PARTIAL_CONTENT = 206; |
|
34
|
|
|
const HTTP_MULTI_STATUS = 207; |
|
35
|
|
|
const HTTP_ALREADY_REPORTED = 208; |
|
36
|
|
|
const HTTP_IM_USED = 226; |
|
37
|
|
|
const HTTP_MULTIPLE_CHOICES = 300; |
|
38
|
|
|
const HTTP_MOVED_PERMANENTLY = 301; |
|
39
|
|
|
const HTTP_FOUND = 302; |
|
40
|
|
|
const HTTP_SEE_OTHER = 303; |
|
41
|
|
|
const HTTP_NOT_MODIFIED = 304; |
|
42
|
|
|
const HTTP_USE_PROXY = 305; |
|
43
|
|
|
const HTTP_RESERVED = 306; |
|
44
|
|
|
const HTTP_TEMPORARY_REDIRECT = 307; |
|
45
|
|
|
const HTTP_PERMANENTLY_REDIRECT = 308; |
|
46
|
|
|
const HTTP_BAD_REQUEST = 400; |
|
47
|
|
|
const HTTP_UNAUTHORIZED = 401; |
|
48
|
|
|
const HTTP_PAYMENT_REQUIRED = 402; |
|
49
|
|
|
const HTTP_FORBIDDEN = 403; |
|
50
|
|
|
const HTTP_NOT_FOUND = 404; |
|
51
|
|
|
const HTTP_METHOD_NOT_ALLOWED = 405; |
|
52
|
|
|
const HTTP_NOT_ACCEPTABLE = 406; |
|
53
|
|
|
const HTTP_PROXY_AUTHENTICATION_REQUIRED = 407; |
|
54
|
|
|
const HTTP_REQUEST_TIMEOUT = 408; |
|
55
|
|
|
const HTTP_CONFLICT = 409; |
|
56
|
|
|
const HTTP_GONE = 410; |
|
57
|
|
|
const HTTP_LENGTH_REQUIRED = 411; |
|
58
|
|
|
const HTTP_PRECONDITION_FAILED = 412; |
|
59
|
|
|
const HTTP_REQUEST_ENTITY_TOO_LARGE = 413; |
|
60
|
|
|
const HTTP_REQUEST_URI_TOO_LONG = 414; |
|
61
|
|
|
const HTTP_UNSUPPORTED_MEDIA_TYPE = 415; |
|
62
|
|
|
const HTTP_REQUESTED_RANGE_NOT_SATISFIABLE = 416; |
|
63
|
|
|
const HTTP_EXPECTATION_FAILED = 417; |
|
64
|
|
|
const HTTP_I_AM_A_TEAPOT = 418; |
|
65
|
|
|
const HTTP_UNPROCESSABLE_ENTITY = 422; |
|
66
|
|
|
const HTTP_LOCKED = 423; // RFC4918 |
|
67
|
|
|
const HTTP_FAILED_DEPENDENCY = 424; // RFC4918 |
|
68
|
|
|
const HTTP_RESERVED_FOR_WEBDAV_ADVANCED_COLLECTIONS_EXPIRED_PROPOSAL = 425; // RFC2817 |
|
69
|
|
|
const HTTP_UPGRADE_REQUIRED = 426; // RFC2817 |
|
70
|
|
|
const HTTP_PRECONDITION_REQUIRED = 428; // RFC6585 |
|
71
|
|
|
const HTTP_TOO_MANY_REQUESTS = 429; // RFC6585 |
|
72
|
|
|
const HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE = 431; // RFC6585 |
|
73
|
|
|
const HTTP_INTERNAL_SERVER_ERROR = 500; |
|
74
|
|
|
const HTTP_NOT_IMPLEMENTED = 501; |
|
75
|
|
|
const HTTP_BAD_GATEWAY = 502; |
|
76
|
|
|
const HTTP_SERVICE_UNAVAILABLE = 503; |
|
77
|
|
|
const HTTP_GATEWAY_TIMEOUT = 504; |
|
78
|
|
|
const HTTP_VERSION_NOT_SUPPORTED = 505; |
|
79
|
|
|
const HTTP_VARIANT_ALSO_NEGOTIATES_EXPERIMENTAL = 506; // RFC2295 |
|
80
|
|
|
const HTTP_INSUFFICIENT_STORAGE = 507; // RFC4918 |
|
81
|
|
|
const HTTP_LOOP_DETECTED = 508; // RFC5842 |
|
82
|
|
|
const HTTP_NOT_EXTENDED = 510; // RFC2774 |
|
83
|
|
|
const HTTP_NETWORK_AUTHENTICATION_REQUIRED = 511; // RFC6585 |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* @var array |
|
87
|
|
|
*/ |
|
88
|
|
|
private static $statusTexts = array( |
|
89
|
|
|
// Informational Codes |
|
90
|
|
|
100 => 'Continue', |
|
91
|
|
|
101 => 'Switching Protocols', |
|
92
|
|
|
102 => 'Processing', |
|
93
|
|
|
// Success Codes |
|
94
|
|
|
200 => 'OK', |
|
95
|
|
|
201 => 'Created', |
|
96
|
|
|
202 => 'Accepted', |
|
97
|
|
|
203 => 'Non-Authoritative Information', |
|
98
|
|
|
204 => 'No Content', |
|
99
|
|
|
205 => 'Reset Content', |
|
100
|
|
|
206 => 'Partial Content', |
|
101
|
|
|
207 => 'Multi-Status', |
|
102
|
|
|
208 => 'Already Reported', |
|
103
|
|
|
226 => 'IM Used', |
|
104
|
|
|
// Redirection Codes |
|
105
|
|
|
300 => 'Multiple Choices', |
|
106
|
|
|
301 => 'Moved Permanently', |
|
107
|
|
|
302 => 'Found', |
|
108
|
|
|
303 => 'See Other', |
|
109
|
|
|
304 => 'Not Modified', |
|
110
|
|
|
305 => 'Use Proxy', |
|
111
|
|
|
306 => 'Reserved', |
|
112
|
|
|
307 => 'Temporary Redirect', |
|
113
|
|
|
308 => 'Permanent Redirect', |
|
114
|
|
|
// Client Error |
|
115
|
|
|
400 => 'Bad Request', |
|
116
|
|
|
401 => 'Unauthorized', |
|
117
|
|
|
402 => 'Payment Required', |
|
118
|
|
|
403 => 'Forbidden', |
|
119
|
|
|
404 => 'Not Found', |
|
120
|
|
|
405 => 'Method Not Allowed', |
|
121
|
|
|
406 => 'Not Acceptable', |
|
122
|
|
|
407 => 'Proxy Authentication Required', |
|
123
|
|
|
408 => 'Request Timeout', |
|
124
|
|
|
409 => 'Conflict', |
|
125
|
|
|
410 => 'Gone', |
|
126
|
|
|
411 => 'Length Required', |
|
127
|
|
|
412 => 'Precondition Failed', |
|
128
|
|
|
413 => 'Request Entity Too Large', |
|
129
|
|
|
414 => 'Request-URI Too Long', |
|
130
|
|
|
415 => 'Unsupported Media Type', |
|
131
|
|
|
416 => 'Requested Range Not Satisfiable', |
|
132
|
|
|
417 => 'Expectation Failed', |
|
133
|
|
|
418 => 'I\'m a teapot', |
|
134
|
|
|
422 => 'Unprocessable Entity', |
|
135
|
|
|
423 => 'Locked', |
|
136
|
|
|
424 => 'Failed Dependency', |
|
137
|
|
|
425 => 'Reserved for WebDAV advanced collections expired proposal', |
|
138
|
|
|
426 => 'Upgrade Required', |
|
139
|
|
|
428 => 'Precondition Required', |
|
140
|
|
|
429 => 'Too Many Requests', |
|
141
|
|
|
431 => 'Request Header Fields Too Large', |
|
142
|
|
|
// Server Error |
|
143
|
|
|
500 => 'Internal Server Error', |
|
144
|
|
|
501 => 'Not Implemented', |
|
145
|
|
|
502 => 'Bad Gateway', |
|
146
|
|
|
503 => 'Service Unavailable', |
|
147
|
|
|
504 => 'Gateway Timeout', |
|
148
|
|
|
505 => 'HTTP Version Not Supported', |
|
149
|
|
|
506 => 'Variant Also Negotiates (Experimental)', |
|
150
|
|
|
507 => 'Insufficient Storage', |
|
151
|
|
|
508 => 'Loop Detected', |
|
152
|
|
|
510 => 'Not Extended', |
|
153
|
|
|
511 => 'Network Authentication Required', |
|
154
|
|
|
); |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* Get HTTP status text by code or return default value if not exist. |
|
158
|
|
|
* |
|
159
|
|
|
* @param int $statusCode |
|
160
|
|
|
* @param string|null $default |
|
161
|
|
|
* |
|
162
|
|
|
* @return string |
|
163
|
|
|
* |
|
164
|
|
|
* @throws InvalidArgumentException |
|
165
|
|
|
*/ |
|
166
|
|
|
public static function getStatusText($statusCode, $default = null) |
|
167
|
|
|
{ |
|
168
|
|
|
if (false === is_int($statusCode)) { |
|
169
|
|
|
throw new InvalidArgumentException(sprintf('Code should be integer, "%s" given.', gettype($statusCode))); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
if (isset(self::$statusTexts[$statusCode])) { |
|
173
|
|
|
return self::$statusTexts[$statusCode]; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
return $default; |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Check if status code is valid. |
|
181
|
|
|
* |
|
182
|
|
|
* @param int $statusCode |
|
183
|
|
|
* |
|
184
|
|
|
* @return bool |
|
185
|
|
|
*/ |
|
186
|
|
|
public static function isValid($statusCode) |
|
187
|
|
|
{ |
|
188
|
|
|
return (int) $statusCode >= 100 && (int) $statusCode < 600; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
|
|
/** |
|
192
|
|
|
* Check if status code is informational. |
|
193
|
|
|
* |
|
194
|
|
|
* @param int $statusCode |
|
195
|
|
|
* |
|
196
|
|
|
* @return bool |
|
197
|
|
|
*/ |
|
198
|
|
|
public static function isInformational($statusCode) |
|
199
|
|
|
{ |
|
200
|
|
|
return (int) $statusCode >= 100 && (int) $statusCode < 200; |
|
201
|
|
|
} |
|
202
|
|
|
|
|
203
|
|
|
/** |
|
204
|
|
|
* Check if status code is successful. |
|
205
|
|
|
* |
|
206
|
|
|
* @param int $statusCode |
|
207
|
|
|
* |
|
208
|
|
|
* @return bool |
|
209
|
|
|
*/ |
|
210
|
|
|
public static function isSuccessful($statusCode) |
|
211
|
|
|
{ |
|
212
|
|
|
return (int) $statusCode >= 200 && (int) $statusCode < 300; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
/** |
|
216
|
|
|
* Check if status code is redirection. |
|
217
|
|
|
* |
|
218
|
|
|
* @param int $statusCode |
|
219
|
|
|
* |
|
220
|
|
|
* @return bool |
|
221
|
|
|
*/ |
|
222
|
|
|
public static function isRedirection($statusCode) |
|
223
|
|
|
{ |
|
224
|
|
|
return (int) $statusCode >= 300 && (int) $statusCode < 400; |
|
225
|
|
|
} |
|
226
|
|
|
|
|
227
|
|
|
/** |
|
228
|
|
|
* Check if status code is client error. |
|
229
|
|
|
* |
|
230
|
|
|
* @param int $statusCode |
|
231
|
|
|
* |
|
232
|
|
|
* @return bool |
|
233
|
|
|
*/ |
|
234
|
|
|
public static function isClientError($statusCode) |
|
235
|
|
|
{ |
|
236
|
|
|
return (int) $statusCode >= 400 && (int) $statusCode < 500; |
|
237
|
|
|
} |
|
238
|
|
|
|
|
239
|
|
|
/** |
|
240
|
|
|
* Check if status code is server error. |
|
241
|
|
|
* |
|
242
|
|
|
* @param int $statusCode |
|
243
|
|
|
* |
|
244
|
|
|
* @return bool |
|
245
|
|
|
*/ |
|
246
|
|
|
public static function isServerError($statusCode) |
|
247
|
|
|
{ |
|
248
|
|
|
return (int) $statusCode >= 500 && (int) $statusCode < 600; |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Check if status code is ok. |
|
253
|
|
|
* |
|
254
|
|
|
* @param int $statusCode |
|
255
|
|
|
* |
|
256
|
|
|
* @return bool |
|
257
|
|
|
*/ |
|
258
|
|
|
public static function isOk($statusCode) |
|
259
|
|
|
{ |
|
260
|
|
|
return 200 === (int) $statusCode; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Check if status code is forbidden. |
|
265
|
|
|
* |
|
266
|
|
|
* @param int $statusCode |
|
267
|
|
|
* |
|
268
|
|
|
* @return bool |
|
269
|
|
|
*/ |
|
270
|
|
|
public static function isForbidden($statusCode) |
|
271
|
|
|
{ |
|
272
|
|
|
return 403 === (int) $statusCode; |
|
273
|
|
|
} |
|
274
|
|
|
|
|
275
|
|
|
/** |
|
276
|
|
|
* Check if status code is not found. |
|
277
|
|
|
* |
|
278
|
|
|
* @param int $statusCode |
|
279
|
|
|
* |
|
280
|
|
|
* @return bool |
|
281
|
|
|
*/ |
|
282
|
|
|
public static function isNotFound($statusCode) |
|
283
|
|
|
{ |
|
284
|
|
|
return 404 === (int) $statusCode; |
|
285
|
|
|
} |
|
286
|
|
|
|
|
287
|
|
|
/** |
|
288
|
|
|
* Check is status code is empty response. |
|
289
|
|
|
* |
|
290
|
|
|
* @param int $statusCode |
|
291
|
|
|
* |
|
292
|
|
|
* @return bool |
|
293
|
|
|
*/ |
|
294
|
|
|
public static function isEmpty($statusCode) |
|
295
|
|
|
{ |
|
296
|
|
|
return in_array((int) $statusCode, array(204, 304)); |
|
297
|
|
|
} |
|
298
|
|
|
} |
|
299
|
|
|
|