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 int index = HttpUtil.validateToken(name);
133 if (index != -1) {
134 throw new IllegalArgumentException(
135 "Illegal character in HTTP Method: 0x" + Integer.toHexString(name.charAt(index)));
136 }
137 this.name = AsciiString.cached(name);
138 }
139
140
141
142
143 public String name() {
144 return name.toString();
145 }
146
147
148
149
150 public AsciiString asciiName() {
151 return name;
152 }
153
154 @Override
155 public int hashCode() {
156 return name().hashCode();
157 }
158
159 @Override
160 public boolean equals(Object o) {
161 if (this == o) {
162 return true;
163 }
164 if (!(o instanceof HttpMethod)) {
165 return false;
166 }
167
168 HttpMethod that = (HttpMethod) o;
169 return name().equals(that.name());
170 }
171
172 @Override
173 public String toString() {
174 return name.toString();
175 }
176
177 @Override
178 public int compareTo(HttpMethod o) {
179 if (o == this) {
180 return 0;
181 }
182 return name().compareTo(o.name());
183 }
184
185 private static final class EnumNameMap<T> {
186 private final EnumNameMap.Node<T>[] values;
187 private final int valuesMask;
188
189 EnumNameMap(EnumNameMap.Node<T>... nodes) {
190 values = (EnumNameMap.Node<T>[]) new EnumNameMap.Node[findNextPositivePowerOfTwo(nodes.length)];
191 valuesMask = values.length - 1;
192 for (EnumNameMap.Node<T> node : nodes) {
193 int i = hashCode(node.key) & valuesMask;
194 if (values[i] != null) {
195 throw new IllegalArgumentException("index " + i + " collision between values: [" +
196 values[i].key + ", " + node.key + ']');
197 }
198 values[i] = node;
199 }
200 }
201
202 T get(String name) {
203 EnumNameMap.Node<T> node = values[hashCode(name) & valuesMask];
204 return node == null || !node.key.equals(name) ? null : node.value;
205 }
206
207 private static int hashCode(String name) {
208
209
210
211
212
213 return name.hashCode() >>> 6;
214 }
215
216 private static final class Node<T> {
217 final String key;
218 final T value;
219
220 Node(String key, T value) {
221 this.key = key;
222 this.value = value;
223 }
224 }
225 }
226 }