View Javadoc
1   /*
2    * Copyright 2012 The Netty Project
3    *
4    * The Netty Project licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
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   * The request method of HTTP or its derived protocols, such as
25   * <a href="https://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
26   * <a href="https://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>.
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       * The OPTIONS method represents a request for information about the communication options
35       * available on the request/response chain identified by the Request-URI. This method allows
36       * the client to determine the options and/or requirements associated with a resource, or the
37       * capabilities of a server, without implying a resource action or initiating a resource
38       * retrieval.
39       */
40      public static final HttpMethod OPTIONS = new HttpMethod("OPTIONS");
41  
42      /**
43       * The GET method means retrieve whatever information (in the form of an entity) is identified
44       * by the Request-URI.  If the Request-URI refers to a data-producing process, it is the
45       * produced data which shall be returned as the entity in the response and not the source text
46       * of the process, unless that text happens to be the output of the process.
47       */
48      public static final HttpMethod GET = new HttpMethod(GET_STRING);
49  
50      /**
51       * The HEAD method is identical to GET except that the server MUST NOT return a message-body
52       * in the response.
53       */
54      public static final HttpMethod HEAD = new HttpMethod("HEAD");
55  
56      /**
57       * The POST method is used to request that the origin server accept the entity enclosed in the
58       * request as a new subordinate of the resource identified by the Request-URI in the
59       * Request-Line.
60       */
61      public static final HttpMethod POST = new HttpMethod(POST_STRING);
62  
63      /**
64       * The PUT method requests that the enclosed entity be stored under the supplied Request-URI.
65       */
66      public static final HttpMethod PUT = new HttpMethod("PUT");
67  
68      /**
69       * The PATCH method requests that a set of changes described in the
70       * request entity be applied to the resource identified by the Request-URI.
71       */
72      public static final HttpMethod PATCH = new HttpMethod("PATCH");
73  
74      /**
75       * The DELETE method requests that the origin server delete the resource identified by the
76       * Request-URI.
77       */
78      public static final HttpMethod DELETE = new HttpMethod("DELETE");
79  
80      /**
81       * The TRACE method is used to invoke a remote, application-layer loop- back of the request
82       * message.
83       */
84      public static final HttpMethod TRACE = new HttpMethod("TRACE");
85  
86      /**
87       * This specification reserves the method name CONNECT for use with a proxy that can dynamically
88       * switch to being a tunnel
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      * Returns the {@link HttpMethod} represented by the specified name.
109      * If the specified name is a standard HTTP method name, a cached instance
110      * will be returned.  Otherwise, a new instance will be returned.
111      */
112     public static HttpMethod valueOf(String name) {
113         // fast-path
114         if (name == GET_STRING) {
115             return GET;
116         }
117         if (name == POST_STRING) {
118             return POST;
119         }
120         // "slow"-path
121         HttpMethod result = methodMap.get(name);
122         return result != null ? result : new HttpMethod(name);
123     }
124 
125     private final AsciiString name;
126 
127     /**
128      * Creates a new HTTP method with the specified name.  You will not need to
129      * create a new method unless you are implementing a protocol derived from
130      * HTTP, such as
131      * <a href="https://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
132      * <a href="https://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>
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      * Returns the name of this method.
146      */
147     public String name() {
148         return name.toString();
149     }
150 
151     /**
152      * Returns the name of this method.
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             // This hash code needs to produce a unique index in the "values" array for each HttpMethod. If new
213             // HttpMethods are added this algorithm will need to be adjusted. The constructor will "fail fast" if there
214             // are duplicates detected.
215             // For example with the current set of HttpMethods it just so happens that the String hash code value
216             // shifted right by 6 bits modulo 16 is unique relative to all other HttpMethod values.
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 }