1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty.handler.codec.http;
17
18 import io.netty.util.AsciiString;
19
20 import static io.netty.util.internal.MathUtil.findNextPositivePowerOfTwo;
21 import static io.netty.util.internal.ObjectUtil.checkNonEmptyAfterTrim;
22
23
24
25
26
27
28 public class HttpMethod implements Comparable<HttpMethod> {
29
30 private static final String GET_STRING = "GET";
31 private static final String POST_STRING = "POST";
32
33
34
35
36
37
38
39
40 public static final HttpMethod OPTIONS = new HttpMethod("OPTIONS");
41
42
43
44
45
46
47
48 public static final HttpMethod GET = new HttpMethod(GET_STRING);
49
50
51
52
53
54 public static final HttpMethod HEAD = new HttpMethod("HEAD");
55
56
57
58
59
60
61 public static final HttpMethod POST = new HttpMethod(POST_STRING);
62
63
64
65
66 public static final HttpMethod PUT = new HttpMethod("PUT");
67
68
69
70
71
72 public static final HttpMethod PATCH = new HttpMethod("PATCH");
73
74
75
76
77
78 public static final HttpMethod DELETE = new HttpMethod("DELETE");
79
80
81
82
83
84 public static final HttpMethod TRACE = new HttpMethod("TRACE");
85
86
87
88
89
90 public static final HttpMethod CONNECT = new HttpMethod("CONNECT");
91
92 private static final EnumNameMap<HttpMethod> methodMap;
93
94 static {
95 methodMap = new EnumNameMap<HttpMethod>(
96 new EnumNameMap.Node<HttpMethod>(OPTIONS.toString(), OPTIONS),
97 new EnumNameMap.Node<HttpMethod>(GET.toString(), GET),
98 new EnumNameMap.Node<HttpMethod>(HEAD.toString(), HEAD),
99 new EnumNameMap.Node<HttpMethod>(POST.toString(), POST),
100 new EnumNameMap.Node<HttpMethod>(PUT.toString(), PUT),
101 new EnumNameMap.Node<HttpMethod>(PATCH.toString(), PATCH),
102 new EnumNameMap.Node<HttpMethod>(DELETE.toString(), DELETE),
103 new EnumNameMap.Node<HttpMethod>(TRACE.toString(), TRACE),
104 new EnumNameMap.Node<HttpMethod>(CONNECT.toString(), CONNECT));
105 }
106
107
108
109
110
111
112 public static HttpMethod valueOf(String name) {
113
114 if (name == GET_STRING) {
115 return GET;
116 }
117 if (name == POST_STRING) {
118 return POST;
119 }
120
121 HttpMethod result = methodMap.get(name);
122 return result != null ? result : new HttpMethod(name);
123 }
124
125 private final AsciiString name;
126
127
128
129
130
131
132
133
134 public HttpMethod(String name) {
135 name = checkNonEmptyAfterTrim(name, "name");
136 int index = HttpUtil.validateToken(name);
137 if (index != -1) {
138 throw new IllegalArgumentException(
139 "Illegal character in HTTP Method: 0x" + Integer.toHexString(name.charAt(index)));
140 }
141 this.name = AsciiString.cached(name);
142 }
143
144
145
146
147 public String name() {
148 return name.toString();
149 }
150
151
152
153
154 public AsciiString asciiName() {
155 return name;
156 }
157
158 @Override
159 public int hashCode() {
160 return name().hashCode();
161 }
162
163 @Override
164 public boolean equals(Object o) {
165 if (this == o) {
166 return true;
167 }
168 if (!(o instanceof HttpMethod)) {
169 return false;
170 }
171
172 HttpMethod that = (HttpMethod) o;
173 return name().equals(that.name());
174 }
175
176 @Override
177 public String toString() {
178 return name.toString();
179 }
180
181 @Override
182 public int compareTo(HttpMethod o) {
183 if (o == this) {
184 return 0;
185 }
186 return name().compareTo(o.name());
187 }
188
189 private static final class EnumNameMap<T> {
190 private final EnumNameMap.Node<T>[] values;
191 private final int valuesMask;
192
193 EnumNameMap(EnumNameMap.Node<T>... nodes) {
194 values = (EnumNameMap.Node<T>[]) new EnumNameMap.Node[findNextPositivePowerOfTwo(nodes.length)];
195 valuesMask = values.length - 1;
196 for (EnumNameMap.Node<T> node : nodes) {
197 int i = hashCode(node.key) & valuesMask;
198 if (values[i] != null) {
199 throw new IllegalArgumentException("index " + i + " collision between values: [" +
200 values[i].key + ", " + node.key + ']');
201 }
202 values[i] = node;
203 }
204 }
205
206 T get(String name) {
207 EnumNameMap.Node<T> node = values[hashCode(name) & valuesMask];
208 return node == null || !node.key.equals(name) ? null : node.value;
209 }
210
211 private static int hashCode(String name) {
212
213
214
215
216
217 return name.hashCode() >>> 6;
218 }
219
220 private static final class Node<T> {
221 final String key;
222 final T value;
223
224 Node(String key, T value) {
225 this.key = key;
226 this.value = value;
227 }
228 }
229 }
230 }