1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package io.netty5.handler.codec.http;
18
19 import io.netty5.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
56
57
58 public static HttpStatusClass valueOf(int code) {
59 if (INFORMATIONAL.contains(code)) {
60 return INFORMATIONAL;
61 }
62 if (SUCCESS.contains(code)) {
63 return SUCCESS;
64 }
65 if (REDIRECTION.contains(code)) {
66 return REDIRECTION;
67 }
68 if (CLIENT_ERROR.contains(code)) {
69 return CLIENT_ERROR;
70 }
71 if (SERVER_ERROR.contains(code)) {
72 return SERVER_ERROR;
73 }
74 return UNKNOWN;
75 }
76
77
78
79
80
81 public static HttpStatusClass valueOf(CharSequence code) {
82 if (code != null && code.length() == 3) {
83 char c0 = code.charAt(0);
84 return isDigit(c0) && isDigit(code.charAt(1)) && isDigit(code.charAt(2)) ? valueOf(digit(c0) * 100)
85 : UNKNOWN;
86 }
87 return UNKNOWN;
88 }
89
90 private static int digit(char c) {
91 return c - '0';
92 }
93
94 private static boolean isDigit(char c) {
95 return c >= '0' && c <= '9';
96 }
97
98 private final int min;
99 private final int max;
100 private final AsciiString defaultReasonPhrase;
101
102 HttpStatusClass(int min, int max, String defaultReasonPhrase) {
103 this.min = min;
104 this.max = max;
105 this.defaultReasonPhrase = AsciiString.cached(defaultReasonPhrase);
106 }
107
108
109
110
111 public boolean contains(int code) {
112 return code >= min && code < max;
113 }
114
115
116
117
118 AsciiString defaultReasonPhrase() {
119 return defaultReasonPhrase;
120 }
121 }