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.buffer.ByteBuf;
19 import io.netty.util.CharsetUtil;
20
21 import java.util.HashMap;
22 import java.util.Map;
23
24
25
26
27
28
29 public class HttpMethod implements Comparable<HttpMethod> {
30
31
32
33
34
35
36
37 public static final HttpMethod OPTIONS = new HttpMethod("OPTIONS", true);
38
39
40
41
42
43
44
45 public static final HttpMethod GET = new HttpMethod("GET", true);
46
47
48
49
50
51 public static final HttpMethod HEAD = new HttpMethod("HEAD", true);
52
53
54
55
56
57
58 public static final HttpMethod POST = new HttpMethod("POST", true);
59
60
61
62
63 public static final HttpMethod PUT = new HttpMethod("PUT", true);
64
65
66
67
68
69 public static final HttpMethod PATCH = new HttpMethod("PATCH", true);
70
71
72
73
74
75 public static final HttpMethod DELETE = new HttpMethod("DELETE", true);
76
77
78
79
80
81 public static final HttpMethod TRACE = new HttpMethod("TRACE", true);
82
83
84
85
86
87 public static final HttpMethod CONNECT = new HttpMethod("CONNECT", true);
88
89 private static final Map<String, HttpMethod> methodMap =
90 new HashMap<String, HttpMethod>();
91
92 static {
93 methodMap.put(OPTIONS.toString(), OPTIONS);
94 methodMap.put(GET.toString(), GET);
95 methodMap.put(HEAD.toString(), HEAD);
96 methodMap.put(POST.toString(), POST);
97 methodMap.put(PUT.toString(), PUT);
98 methodMap.put(PATCH.toString(), PATCH);
99 methodMap.put(DELETE.toString(), DELETE);
100 methodMap.put(TRACE.toString(), TRACE);
101 methodMap.put(CONNECT.toString(), CONNECT);
102 }
103
104
105
106
107
108
109 public static HttpMethod valueOf(String name) {
110 if (name == null) {
111 throw new NullPointerException("name");
112 }
113
114 name = name.trim();
115 if (name.isEmpty()) {
116 throw new IllegalArgumentException("empty name");
117 }
118
119 HttpMethod result = methodMap.get(name);
120 if (result != null) {
121 return result;
122 } else {
123 return new HttpMethod(name);
124 }
125 }
126
127 private final String name;
128 private final byte[] bytes;
129
130
131
132
133
134
135
136
137 public HttpMethod(String name) {
138 this(name, false);
139 }
140
141 private HttpMethod(String name, boolean bytes) {
142 if (name == null) {
143 throw new NullPointerException("name");
144 }
145
146 name = name.trim();
147 if (name.isEmpty()) {
148 throw new IllegalArgumentException("empty name");
149 }
150
151 for (int i = 0; i < name.length(); i ++) {
152 if (Character.isISOControl(name.charAt(i)) ||
153 Character.isWhitespace(name.charAt(i))) {
154 throw new IllegalArgumentException("invalid character in name");
155 }
156 }
157
158 this.name = name;
159 if (bytes) {
160 this.bytes = name.getBytes(CharsetUtil.US_ASCII);
161 } else {
162 this.bytes = null;
163 }
164 }
165
166
167
168
169 public String name() {
170 return name;
171 }
172
173 @Override
174 public int hashCode() {
175 return name().hashCode();
176 }
177
178 @Override
179 public boolean equals(Object o) {
180 if (!(o instanceof HttpMethod)) {
181 return false;
182 }
183
184 HttpMethod that = (HttpMethod) o;
185 return name().equals(that.name());
186 }
187
188 @Override
189 public String toString() {
190 return name();
191 }
192
193 @Override
194 public int compareTo(HttpMethod o) {
195 return name().compareTo(o.name());
196 }
197
198 void encode(ByteBuf buf) {
199 if (bytes == null) {
200 HttpHeaders.encodeAscii0(name, buf);
201 } else {
202 buf.writeBytes(bytes);
203 }
204 }
205 }