Protocol   A
last analyzed

Complexity

Total Complexity 2

Size/Duplication

Total Lines 534
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 80%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
wmc 2
c 4
b 0
f 0
lcom 0
cbo 0
dl 0
loc 534
ccs 4
cts 5
cp 0.8
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A getStatusReasonPhraseByCode() 0 8 2
1
<?php
2
3
/**
4
 * \AppserverIo\Psr\HttpMessage\Protocol
5
 *
6
 * NOTICE OF LICENSE
7
 *
8
 * This source file is subject to the Open Software License (OSL 3.0)
9
 * that is available through the world-wide-web at this URL:
10
 * http://opensource.org/licenses/osl-3.0.php
11
 *
12
 * PHP version 5
13
 *
14
 * @author    Johann Zelger <[email protected]>
15
 * @author    Tim Wagner <[email protected]>
16
 * @copyright 2015 TechDivision GmbH <[email protected]>
17
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
18
 * @link      https://github.com/appserver-io/http-message
19
 * @link      http://www.appserver.io
20
 */
21
22
namespace AppserverIo\Psr\HttpMessage;
23
24
/**
25
 * Basic constants for usage with the HTTP protocol.
26
 *
27
 * @author    Johann Zelger <[email protected]>
28
 * @author    Tim Wagner <[email protected]>
29
 * @copyright 2015 TechDivision GmbH <[email protected]>
30
 * @license   http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
31
 * @link      https://github.com/appserver-io/http-message
32
 * @link      http://www.appserver.io
33
 * @link      http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
34
 */
35
class Protocol
36
{
37
38
    /**
39
     * Header name value separator
40
     *
41
     * @var string
42
     */
43
    const HEADER_SEPARATOR = ': ';
44
45
    /**
46
     * Status header name.
47
     *
48
     * @var string
49
     */
50
    const HEADER_STATUS = 'Status';
51
52
    /**
53
     * Date header name.
54
     *
55
     * @var string
56
     */
57
    const HEADER_DATE = 'Date';
58
59
    /**
60
     * Connection header name.
61
     *
62
     * @var string
63
     */
64
    const HEADER_CONNECTION = 'Connection';
65
66
    /**
67
     * Connection header value for close
68
     *
69
     * @var string
70
     */
71
    const HEADER_CONNECTION_VALUE_CLOSE = 'Close';
72
73
    /**
74
     * Connection header value for keep-alive
75
     *
76
     * @var string
77
     */
78
    const HEADER_CONNECTION_VALUE_KEEPALIVE = 'Keep-Alive';
79
80
    /**
81
     * Content-Type header name.
82
     *
83
     * @var string
84
     */
85
    const HEADER_CONTENT_TYPE = 'Content-Type';
86
87
    /**
88
     * If-None-Match header name
89
     *
90
     * @var string
91
     */
92
    const HEADER_IF_NONE_MATCH = 'If-None-Match';
93
94
    /**
95
     * Content-Disposition header name.
96
     *
97
     * @var string
98
     */
99
    const HEADER_CONTENT_DISPOSITION = 'Content-Disposition';
100
101
    /**
102
     * Content-Length header name.
103
     *
104
     * @var string
105
     */
106
    const HEADER_CONTENT_LENGTH = 'Content-Length';
107
108
    /**
109
     * Content-Encoding header name.
110
     *
111
     * @var string
112
     */
113
    const HEADER_CONTENT_ENCODING = 'Content-Encoding';
114
115
    /**
116
     * Cache-Control header name.
117
     *
118
     * @var string
119
     */
120
    const HEADER_CACHE_CONTROL = 'Cache-Control';
121
122
    /**
123
     * Pragma header name.
124
     *
125
     * @var string
126
     */
127
    const HEADER_PRAGMA = 'Pragma';
128
129
    /**
130
     * Proxy-Connection header name
131
     *
132
     * @var string
133
     */
134
    const HEADER_PROXY_CONNECTION = 'Proxy-Connection';
135
136
    /**
137
     * X-Forward header name
138
     *
139
     * @var string
140
     */
141
    const HEADER_X_FORWARD = 'X-Forward';
142
143
    /**
144
     * Status header name.
145
     *
146
     * @var string
147
     */
148
    const HEADER_LAST_MODIFIED = 'Last-Modified';
149
150
    /**
151
     * Expires header name.
152
     *
153
     * @var string
154
     */
155
    const HEADER_EXPIRES = 'Expires';
156
157
    /**
158
     * If-Modified-Since header name.
159
     *
160
     * @var string
161
     */
162
    const HEADER_IF_MODIFIED_SINCE = 'If-Modified-Since';
163
164
    /**
165
     * Location header name.
166
     *
167
     * @var string
168
     */
169
    const HEADER_LOCATION = 'Location';
170
171
    /**
172
     * X-Powered-By header name.
173
     *
174
     * @var string
175
     */
176
    const HEADER_X_POWERED_BY = 'X-Powered-By';
177
178
    /**
179
     * Cookie header name.
180
     *
181
     * @var string
182
     */
183
    const HEADER_COOKIE = 'Cookie';
184
185
    /**
186
     * Set-Cookie header name.
187
     *
188
     * @var string
189
     */
190
    const HEADER_SET_COOKIE = 'Set-Cookie';
191
192
    /**
193
     * Host header name.
194
     *
195
     * @var string
196
     */
197
    const HEADER_HOST = 'Host';
198
199
    /**
200
     * Accept header name.
201
     *
202
     * @var string
203
     */
204
    const HEADER_ACCEPT = 'Accept';
205
206
    /**
207
     * Accept-Charset header name
208
     *
209
     * @var string
210
     */
211
    const HEADER_ACCEPT_CHARSET = 'Accept-Charset';
212
213
    /**
214
     * Accept-Language header name.
215
     *
216
     * @var string
217
     */
218
    const HEADER_ACCEPT_LANGUAGE = 'Accept-Language';
219
220
    /**
221
     * Accept-Encoding header name.
222
     *
223
     * @var string
224
     */
225
    const HEADER_ACCEPT_ENCODING = 'Accept-Encoding';
226
227
    /**
228
     * User-Agent header name.
229
     *
230
     * @var string
231
     */
232
    const HEADER_USER_AGENT = 'User-Agent';
233
234
    /**
235
     * Referer header name.
236
     *
237
     * @var string
238
     */
239
    const HEADER_REFERER = 'Referer';
240
241
    /**
242
     * Keep-Alive header name.
243
     *
244
     * @var string
245
     */
246
    const HEADER_KEEP_ALIVE = 'Keep-Alive';
247
248
    /**
249
     * Server header name.
250
     *
251
     * @var string
252
     */
253
    const HEADER_SERVER = 'Server';
254
255
    /**
256
     * WWW-Authenticate header name.
257
     *
258
     * @var string
259
     */
260
    const HEADER_WWW_AUTHENTICATE = 'Www-Authenticate';
261
262
    /**
263
     * Authorization header name.
264
     *
265
     * @var string
266
     */
267
    const HEADER_AUTHORIZATION = 'Authorization';
268
269
    /**
270
     * ETag header name.
271
     *
272
     * @var string
273
     */
274
    const HEADER_ETAG = 'ETag';
275
276
    /**
277
     * Mainly used to identify Ajax requests.
278
     * Most JavaScript frameworks send this header with value of XMLHttpRequest
279
     *
280
     * @var string
281
     */
282
    const HEADER_X_REQUESTED_WITH = 'X-Requested-With';
283
284
    /**
285
     * Access-Control-Allow-Origin header name.
286
     *
287
     * @var string
288
     */
289
    const HEADER_ACCESS_CONTROL_ALLOW_ORIGIN = 'Access-Control-Allow-Origin';
290
291
    /**
292
     * Access-Control-Allow-Credentials header name.
293
     *
294
     * @var string
295
     */
296
    const HEADER_ACCESS_CONTROL_ALLOW_CREDENTIALS = 'Access-Control-Allow-Credentials';
297
298
    /**
299
     * Access-Control-Allow-Headers header name.
300
     *
301
     * @var string
302
     */
303
    const HEADER_ACCESS_CONTROL_ALLOW_HEADERS = 'Access-Control-Allow-Headers';
304
305
    /**
306
     * Access-Control-Allow-Methods header name.
307
     *
308
     * @var string
309
     */
310
    const HEADER_ACCESS_CONTROL_ALLOW_METHODS = 'Access-Control-Allow-Methods';
311
312
    /**
313
     * Forwarded header name.
314
     *
315
     * @var string
316
     */
317
    const HEADER_FORWARDED = 'Forwarded';
318
319
    /**
320
     * The X-Frame-Options header has to be included in the HTTP response to protect browsers against
321
     * 'ClickJacking' attacks.
322
     *
323
     * @var string
324
     * @link http://blogs.msdn.com/b/ieinternals/archive/2010/03/30/combating-clickjacking-with-x-frame-options.aspx
325
     */
326
    const HEADER_X_FRAME_OPTIONS = 'X-Frame-Options';
327
328
    /**
329
     * If the X-Frame-Options value contains the token Deny, browsers will prevent the page from rendering if it will
330
     * be contained within a frame.
331
     *
332
     * @var string
333
     */
334
    const HEADER_X_FRAME_OPTIONS_VALUE_DENY = 'Deny';
335
336
    /**
337
     * If the X-Frame-Options value contains the token Sameorigin, the browser will block rendering only if the origin
338
     * of the top-level browsing-context is different than the origin of the content containing the X-Frame-Options
339
     * directive.
340
     *
341
     * @var string
342
     */
343
    const HEADER_X_FRAME_OPTIONS_VALUE_SAMEORIGIN = 'Sameorigin';
344
345
    /**
346
     * If the value contains the token Allow-From origin, the browser will block rendering only if the origin of the
347
     * top-level browsing context is different than the origin value supplied with the Allow-From directive.
348
     *
349
     * @var string
350
     */
351
    const HEADER_X_FRAME_OPTIONS_VALUE_ALLOW_FROM = 'Allow-From';
352
353
    /**
354
     * The X-XSS-Protection HTTP response header allows the web server to enable or disable the web browser's XSS
355
     * protection mechanism.
356
     *
357
     * @var string
358
     * @link https://www.owasp.org/index.php/XSS_(Cross_Site_Scripting)_Prevention_Cheat_Sheet
359
     * @link https://blog.veracode.com/2014/03/guidelines-for-setting-security-headers/
360
     */
361
    const HEADER_X_XSS_PROTECTION = 'X-Xss-Protection';
362
363
    /**
364
     * Ensures that the web browser's XSS filter is enabled.
365
     *
366
     * @var string
367
     */
368
    const HEADER_X_XSS_PROTECTION_VALUE_ON = 1;
369
370
    /**
371
     * Ensures that the web browser's XSS filter is disabled.
372
     *
373
     * @var string
374
     */
375
    const HEADER_X_XSS_PROTECTION_VALUE_OFF = 0;
376
377
    /**
378
     * If the Anti-MIME-Sniffing header X-Content-Type-Options was not set to 'nosniff', older versions of Internet
379
     * Explorer and Chrome are able to perform MIME-sniffing on the response body, potentially causing the response
380
     * body to be interpreted and displayed as a content type other than the declared content type.
381
     *
382
     * @var string
383
     * @link https://www.owasp.org/index.php/List_of_useful_HTTP_headers
384
     * @link http://msdn.microsoft.com/en-us/library/ie/gg622941%28v=vs.85%29.aspx
385
     */
386
    const HEADER_X_CONTENT_TYPE_OPTIONS = 'X-Content-Type-Options';
387
388
    /**
389
     * If the value of the X-Content-Type-Options is set to Nosniff, older versions of Internet Explorer and Chrome
390
     * are protected from performing MIME-sniffing on the response body.
391
     *
392
     * @var string
393
     */
394
    const HEADER_X_CONTENT_TYPE_OPTIONS_VALUE_NOSNIFF = 'Nosniff';
395
396
    /**
397
     * Defines the value consts for content-type header
398
     *
399
     * @var string
400
     */
401
    const HEADER_CONTENT_TYPE_VALUE_APPLICATION_X_WWW_FORM_URLENCODED = 'application/x-www-form-urlencoded';
402
    const HEADER_CONTENT_TYPE_VALUE_MULTIPART_FORM_DATA = 'multipart/form-data';
403
404
    /**
405
     * POST request method string.
406
     *
407
     * @var string
408
     */
409
    const METHOD_POST = 'POST';
410
411
    /**
412
     * GET request method string.
413
     *
414
     * @var string
415
     */
416
    const METHOD_GET = 'GET';
417
418
    /**
419
     * HEAD request method string.
420
     *
421
     * @var string
422
     */
423
    const METHOD_HEAD = 'HEAD';
424
425
    /**
426
     * PUT request method string.
427
     *
428
     * @var string
429
     */
430
    const METHOD_PUT = 'PUT';
431
432
    /**
433
     * DELETE request method string.
434
     *
435
     * @var string
436
     */
437
    const METHOD_DELETE = 'DELETE';
438
439
    /**
440
     * OPTIONS request method string.
441
     *
442
     * @var string
443
     */
444
    const METHOD_OPTIONS = 'OPTIONS';
445
446
    /**
447
     * TRACE request method string.
448
     *
449
     * @var string
450
     */
451
    const METHOD_TRACE = 'TRACE';
452
453
    /**
454
     * CONNECT request method string.
455
     *
456
     * @var string
457
     */
458
    const METHOD_CONNECT = 'CONNECT';
459
460
    /**
461
     * PATCH request method string.
462
     *
463
     * @var string
464
     */
465
    const METHOD_PATCH = 'PATCH';
466
467
    /**
468
     * Defines the http 1.0 protocol version identifier
469
     *
470
     * @var string
471
     */
472
    const VERSION_1_0 = 'HTTP/1.0';
473
474
    /**
475
     * Defines the http 1.1 protocol version identifier
476
     *
477
     * @var string
478
     */
479
    const VERSION_1_1 = 'HTTP/1.1';
480
481
    /**
482
     * Defines status const's
483
     *
484
     * @var string
485
     */
486
    const STATUS_REASONPHRASE_UNASSIGNED = 'Unassigned';
487
488
    /**
489
     * Defines reasonPhrases
490
     *
491
     * @var string
492
     */
493
    const STATUS_REASONPHRASE_100 = "Continue"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.2.1]
494
    const STATUS_REASONPHRASE_101 = "Switching Protocols"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.2.2]
495
    const STATUS_REASONPHRASE_102 = "Processing"; //[RFC2518]
496
    const STATUS_REASONPHRASE_200 = "OK"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.3.1]
497
    const STATUS_REASONPHRASE_201 = "Created"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.3.2]
498
    const STATUS_REASONPHRASE_202 = "Accepted"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.3.3]
499
    const STATUS_REASONPHRASE_203 = "Non-Authoritative Information"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.3.4]
500
    const STATUS_REASONPHRASE_204 = "No Content"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.3.5]
501
    const STATUS_REASONPHRASE_205 = "Reset Content"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.3.6]
502
    const STATUS_REASONPHRASE_206 = "Partial Content"; //[RFC-ietf-httpbis-p5-range-26, Section 4.1]
503
    const STATUS_REASONPHRASE_207 = "Multi-Status"; //[RFC4918]
504
    const STATUS_REASONPHRASE_208 = "Already Reported"; //[RFC5842]
505
    const STATUS_REASONPHRASE_226 = "IM Used"; //[RFC3229]
506
    const STATUS_REASONPHRASE_300 = "Multiple Choices"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.4.1]
507
    const STATUS_REASONPHRASE_301 = "Moved Permanently"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.4.2]
508
    const STATUS_REASONPHRASE_302 = "Found"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.4.3]
509
    const STATUS_REASONPHRASE_303 = "See Other"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.4.4]
510
    const STATUS_REASONPHRASE_304 = "Not Modified"; //[RFC-ietf-httpbis-p4-conditional-26, Section 4.1]
511
    const STATUS_REASONPHRASE_305 = "Use Proxy"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.4.5]
512
    const STATUS_REASONPHRASE_306 = "(Unused)"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.4.6]
513
    const STATUS_REASONPHRASE_307 = "Temporary Redirect"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.4.7]
514
    const STATUS_REASONPHRASE_308 = "Permanent Redirect"; //[RFC-reschke-http-status-308-07]
515
    const STATUS_REASONPHRASE_400 = "Bad Request"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.1]
516
    const STATUS_REASONPHRASE_401 = "Unauthorized"; //[RFC-ietf-httpbis-p7-auth-26, Section 3.1]
517
    const STATUS_REASONPHRASE_402 = "Payment Required"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.2]
518
    const STATUS_REASONPHRASE_403 = "Forbidden"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.3]
519
    const STATUS_REASONPHRASE_404 = "Not Found"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.4]
520
    const STATUS_REASONPHRASE_405 = "Method Not Allowed"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.5]
521
    const STATUS_REASONPHRASE_406 = "Not Acceptable"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.6]
522
    const STATUS_REASONPHRASE_407 = "Proxy Authentication Required"; //[RFC-ietf-httpbis-p7-auth-26, Section 3.2]
523
    const STATUS_REASONPHRASE_408 = "Request Timeout"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.7]
524
    const STATUS_REASONPHRASE_409 = "Conflict"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.8]
525
    const STATUS_REASONPHRASE_410 = "Gone"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.9]
526
    const STATUS_REASONPHRASE_411 = "Length Required"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.10]
527
    const STATUS_REASONPHRASE_412 = "Precondition Failed"; //[RFC-ietf-httpbis-p4-conditional-26, Section 4.2]
528
    const STATUS_REASONPHRASE_413 = "Payload Too Large"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.11]
529
    const STATUS_REASONPHRASE_414 = "URI Too Long"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.12]
530
    const STATUS_REASONPHRASE_415 = "Unsupported Media Type"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.13]
531
    const STATUS_REASONPHRASE_416 = "Requested Range Not Satisfiable"; //[RFC-ietf-httpbis-p5-range-26, Section 4.4]
532
    const STATUS_REASONPHRASE_417 = "Expectation Failed"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.14]
533
    const STATUS_REASONPHRASE_418 = "I’m a teapot"; //[RFC2324]
534
    const STATUS_REASONPHRASE_422 = "Unprocessable Entity"; //[RFC4918]
535
    const STATUS_REASONPHRASE_423 = "Locked"; //[RFC4918]
536
    const STATUS_REASONPHRASE_424 = "Failed Dependency"; //[RFC4918]
537
    const STATUS_REASONPHRASE_426 = "Upgrade Required"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.5.15]
538
    const STATUS_REASONPHRASE_428 = "Precondition Required"; //[RFC6585]
539
    const STATUS_REASONPHRASE_429 = "Too Many Requests"; //[RFC6585]
540
    const STATUS_REASONPHRASE_431 = "Request Header Fields Too Large"; //[RFC6585]
541
    const STATUS_REASONPHRASE_500 = "Internal Server Error"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.6.1]
542
    const STATUS_REASONPHRASE_501 = "Not Implemented"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.6.2]
543
    const STATUS_REASONPHRASE_502 = "Bad Gateway"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.6.3]
544
    const STATUS_REASONPHRASE_503 = "Service Unavailable"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.6.4]
545
    const STATUS_REASONPHRASE_504 = "Gateway Timeout"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.6.5]
546
    const STATUS_REASONPHRASE_505 = "HTTP Version Not Supported"; //[RFC-ietf-httpbis-p2-semantics-26, Section 6.6.6]
547
    const STATUS_REASONPHRASE_506 = "Variant Also Negotiates (Experimental)"; //[RFC2295]
548
    const STATUS_REASONPHRASE_507 = "Insufficient Storage"; //[RFC4918]
549
    const STATUS_REASONPHRASE_508 = "Loop Detected"; //[RFC5842]
550
    const STATUS_REASONPHRASE_510 = "Not Extended"; //[RFC2774]
551
    const STATUS_REASONPHRASE_511 = "Network Authentication Required"; //[RFC6585]
552
553
    /**
554
     * Returns the reason phrase by given status code
555
     *
556
     * @param int $statusCode The http status code
557
     *
558
     * @return string The reason phrase
559
     */
560 1
    public static function getStatusReasonPhraseByCode($statusCode)
561
    {
562 1
        $reasonPhraseConstant = 'STATUS_REASONPHRASE_' . (string)$statusCode;
563 1
        if (defined('\AppserverIo\Psr\HttpMessage\Protocol::' . $reasonPhraseConstant)) {
564 1
            return constant('\AppserverIo\Psr\HttpMessage\Protocol::' . $reasonPhraseConstant);
565
        }
566
        return Protocol::STATUS_REASONPHRASE_UNASSIGNED;
567
    }
568
}
569