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