1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty.handler.codec.http;
18
19 import io.netty.util.AsciiString;
20
21
22
23
24 public enum HttpStatusClass {
25
26
27
28 INFORMATIONAL(100, 200, "Informational"),
29
30
31
32 SUCCESS(200, 300, "Success"),
33
34
35
36 REDIRECTION(300, 400, "Redirection"),
37
38
39
40 CLIENT_ERROR(400, 500, "Client Error"),
41
42
43
44 SERVER_ERROR(500, 600, "Server Error"),
45
46
47
48 UNKNOWN(0, 0, "Unknown Status") {
49 @Override
50 public boolean contains(int code) {
51 return code < 100 || code >= 600;
52 }
53 };
54
55 private static final HttpStatusClass[] statusArray = new HttpStatusClass[6];
56 static {
57 statusArray[1] = INFORMATIONAL;
58 statusArray[2] = SUCCESS;
59 statusArray[3] = REDIRECTION;
60 statusArray[4] = CLIENT_ERROR;
61 statusArray[5] = SERVER_ERROR;
62 }
63
64
65
66
67 public static HttpStatusClass valueOf(int code) {
68 if (UNKNOWN.contains(code)) {
69 return UNKNOWN;
70 }
71 return statusArray[fast_div100(code)];
72 }
73
74
75
76
77
78 private static int fast_div100(int dividend) {
79 return (int) ((dividend * 1374389535L) >> 37);
80 }
81
82
83
84
85
86 public static HttpStatusClass valueOf(CharSequence code) {
87 if (code != null && code.length() == 3) {
88 char c0 = code.charAt(0);
89 return isDigit(c0) && isDigit(code.charAt(1)) && isDigit(code.charAt(2)) ? valueOf(digit(c0) * 100)
90 : UNKNOWN;
91 }
92 return UNKNOWN;
93 }
94
95 private static int digit(char c) {
96 return c - '0';
97 }
98
99 private static boolean isDigit(char c) {
100 return c >= '0' && c <= '9';
101 }
102
103 private final int min;
104 private final int max;
105 private final AsciiString defaultReasonPhrase;
106
107 HttpStatusClass(int min, int max, String defaultReasonPhrase) {
108 this.min = min;
109 this.max = max;
110 this.defaultReasonPhrase = AsciiString.cached(defaultReasonPhrase);
111 }
112
113
114
115
116 public boolean contains(int code) {
117 return code >= min && code < max;
118 }
119
120
121
122
123 AsciiString defaultReasonPhrase() {
124 return defaultReasonPhrase;
125 }
126 }