2008-11-19 08:22:15 +01:00
|
|
|
/*
|
2012-06-04 22:31:44 +02:00
|
|
|
* Copyright 2012 The Netty Project
|
2009-06-19 19:48:17 +02:00
|
|
|
*
|
2011-12-09 06:18:34 +01:00
|
|
|
* The Netty Project licenses this file to you under the Apache License,
|
|
|
|
* version 2.0 (the "License"); you may not use this file except in compliance
|
|
|
|
* with the License. You may obtain a copy of the License at:
|
2008-11-19 08:22:15 +01:00
|
|
|
*
|
2012-06-04 22:31:44 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2008-11-19 08:22:15 +01:00
|
|
|
*
|
2009-08-28 09:15:49 +02:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
2011-12-09 06:18:34 +01:00
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
2009-08-28 09:15:49 +02:00
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2011-12-09 04:38:59 +01:00
|
|
|
package io.netty.handler.codec.http;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2015-08-19 21:42:31 +02:00
|
|
|
import io.netty.util.AsciiString;
|
2013-11-28 08:15:14 +01:00
|
|
|
|
2017-11-12 07:37:26 +01:00
|
|
|
import static io.netty.util.internal.MathUtil.findNextPositivePowerOfTwo;
|
|
|
|
import static io.netty.util.internal.ObjectUtil.checkNotNull;
|
2008-12-05 02:58:38 +01:00
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
/**
|
2015-08-19 21:42:31 +02:00
|
|
|
* The request method of HTTP or its derived protocols, such as
|
2009-06-19 16:15:20 +02:00
|
|
|
* <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
|
|
|
|
* <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2008-12-05 02:58:38 +01:00
|
|
|
public class HttpMethod implements Comparable<HttpMethod> {
|
2008-11-19 08:22:15 +01:00
|
|
|
/**
|
2015-08-19 21:42:31 +02:00
|
|
|
* The OPTIONS method represents a request for information about the communication options
|
|
|
|
* available on the request/response chain identified by the Request-URI. This method allows
|
2012-06-08 12:28:12 +02:00
|
|
|
* the client to determine the options and/or requirements associated with a resource, or the
|
|
|
|
* capabilities of a server, without implying a resource action or initiating a resource
|
|
|
|
* retrieval.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2015-08-19 21:42:31 +02:00
|
|
|
public static final HttpMethod OPTIONS = new HttpMethod("OPTIONS");
|
2008-11-19 08:22:15 +01:00
|
|
|
|
|
|
|
/**
|
2015-08-19 21:42:31 +02:00
|
|
|
* The GET method means retrieve whatever information (in the form of an entity) is identified
|
2012-06-08 12:28:12 +02:00
|
|
|
* by the Request-URI. If the Request-URI refers to a data-producing process, it is the
|
|
|
|
* produced data which shall be returned as the entity in the response and not the source text
|
|
|
|
* of the process, unless that text happens to be the output of the process.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2015-08-19 21:42:31 +02:00
|
|
|
public static final HttpMethod GET = new HttpMethod("GET");
|
2008-11-19 08:22:15 +01:00
|
|
|
|
|
|
|
/**
|
2015-08-19 21:42:31 +02:00
|
|
|
* The HEAD method is identical to GET except that the server MUST NOT return a message-body
|
2012-06-08 12:28:12 +02:00
|
|
|
* in the response.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2015-08-19 21:42:31 +02:00
|
|
|
public static final HttpMethod HEAD = new HttpMethod("HEAD");
|
2008-11-19 08:22:15 +01:00
|
|
|
|
|
|
|
/**
|
2015-08-19 21:42:31 +02:00
|
|
|
* The POST method is used to request that the origin server accept the entity enclosed in the
|
2012-06-08 12:28:12 +02:00
|
|
|
* request as a new subordinate of the resource identified by the Request-URI in the
|
|
|
|
* Request-Line.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2015-08-19 21:42:31 +02:00
|
|
|
public static final HttpMethod POST = new HttpMethod("POST");
|
2008-11-19 08:22:15 +01:00
|
|
|
|
|
|
|
/**
|
2015-08-19 21:42:31 +02:00
|
|
|
* The PUT method requests that the enclosed entity be stored under the supplied Request-URI.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2015-08-19 21:42:31 +02:00
|
|
|
public static final HttpMethod PUT = new HttpMethod("PUT");
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2009-12-16 08:23:35 +01:00
|
|
|
/**
|
2015-08-19 21:42:31 +02:00
|
|
|
* The PATCH method requests that a set of changes described in the
|
2009-12-16 08:23:35 +01:00
|
|
|
* request entity be applied to the resource identified by the Request-URI.
|
|
|
|
*/
|
2015-08-19 21:42:31 +02:00
|
|
|
public static final HttpMethod PATCH = new HttpMethod("PATCH");
|
2009-12-16 08:23:35 +01:00
|
|
|
|
2008-11-19 08:22:15 +01:00
|
|
|
/**
|
2015-08-19 21:42:31 +02:00
|
|
|
* The DELETE method requests that the origin server delete the resource identified by the
|
2012-06-08 12:28:12 +02:00
|
|
|
* Request-URI.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2015-08-19 21:42:31 +02:00
|
|
|
public static final HttpMethod DELETE = new HttpMethod("DELETE");
|
2008-11-19 08:22:15 +01:00
|
|
|
|
|
|
|
/**
|
2015-08-19 21:42:31 +02:00
|
|
|
* The TRACE method is used to invoke a remote, application-layer loop- back of the request
|
2012-06-08 12:28:12 +02:00
|
|
|
* message.
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2015-08-19 21:42:31 +02:00
|
|
|
public static final HttpMethod TRACE = new HttpMethod("TRACE");
|
2008-11-19 08:22:15 +01:00
|
|
|
|
|
|
|
/**
|
2015-08-19 21:42:31 +02:00
|
|
|
* This specification reserves the method name CONNECT for use with a proxy that can dynamically
|
2012-06-08 12:28:12 +02:00
|
|
|
* switch to being a tunnel
|
2008-11-19 08:22:15 +01:00
|
|
|
*/
|
2015-08-19 21:42:31 +02:00
|
|
|
public static final HttpMethod CONNECT = new HttpMethod("CONNECT");
|
2008-12-05 02:58:38 +01:00
|
|
|
|
2017-11-12 07:37:26 +01:00
|
|
|
private static final EnumNameMap<HttpMethod> methodMap;
|
2008-12-05 02:58:38 +01:00
|
|
|
|
|
|
|
static {
|
2017-11-12 07:37:26 +01:00
|
|
|
methodMap = new EnumNameMap<HttpMethod>(
|
|
|
|
new EnumNameMap.Node<HttpMethod>(OPTIONS.toString(), OPTIONS),
|
|
|
|
new EnumNameMap.Node<HttpMethod>(GET.toString(), GET),
|
|
|
|
new EnumNameMap.Node<HttpMethod>(HEAD.toString(), HEAD),
|
|
|
|
new EnumNameMap.Node<HttpMethod>(POST.toString(), POST),
|
|
|
|
new EnumNameMap.Node<HttpMethod>(PUT.toString(), PUT),
|
|
|
|
new EnumNameMap.Node<HttpMethod>(PATCH.toString(), PATCH),
|
|
|
|
new EnumNameMap.Node<HttpMethod>(DELETE.toString(), DELETE),
|
|
|
|
new EnumNameMap.Node<HttpMethod>(TRACE.toString(), TRACE),
|
|
|
|
new EnumNameMap.Node<HttpMethod>(CONNECT.toString(), CONNECT));
|
2008-12-05 02:58:38 +01:00
|
|
|
}
|
|
|
|
|
2009-06-19 16:15:20 +02:00
|
|
|
/**
|
|
|
|
* Returns the {@link HttpMethod} represented by the specified name.
|
2015-08-19 21:42:31 +02:00
|
|
|
* If the specified name is a standard HTTP method name, a cached instance
|
2009-06-19 16:15:20 +02:00
|
|
|
* will be returned. Otherwise, a new instance will be returned.
|
|
|
|
*/
|
2009-02-11 10:24:28 +01:00
|
|
|
public static HttpMethod valueOf(String name) {
|
|
|
|
HttpMethod result = methodMap.get(name);
|
2015-08-13 04:05:37 +02:00
|
|
|
return result != null ? result : new HttpMethod(name);
|
2008-12-05 02:58:38 +01:00
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2015-08-19 21:42:31 +02:00
|
|
|
private final AsciiString name;
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2009-06-19 16:15:20 +02:00
|
|
|
/**
|
2015-08-19 21:42:31 +02:00
|
|
|
* Creates a new HTTP method with the specified name. You will not need to
|
|
|
|
* create a new method unless you are implementing a protocol derived from
|
2009-06-19 17:01:47 +02:00
|
|
|
* HTTP, such as
|
2009-06-19 16:15:20 +02:00
|
|
|
* <a href="http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and
|
|
|
|
* <a href="http://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>
|
|
|
|
*/
|
2009-02-11 10:24:28 +01:00
|
|
|
public HttpMethod(String name) {
|
2015-08-13 04:05:37 +02:00
|
|
|
name = checkNotNull(name, "name").trim();
|
2012-11-09 17:24:04 +01:00
|
|
|
if (name.isEmpty()) {
|
2009-02-11 10:24:28 +01:00
|
|
|
throw new IllegalArgumentException("empty name");
|
|
|
|
}
|
2008-12-05 02:58:38 +01:00
|
|
|
|
2009-02-11 10:24:28 +01:00
|
|
|
for (int i = 0; i < name.length(); i ++) {
|
2014-04-24 14:13:19 +02:00
|
|
|
char c = name.charAt(i);
|
|
|
|
if (Character.isISOControl(c) || Character.isWhitespace(c)) {
|
2009-02-12 06:02:22 +01:00
|
|
|
throw new IllegalArgumentException("invalid character in name");
|
2009-02-11 10:24:28 +01:00
|
|
|
}
|
2008-12-05 02:58:38 +01:00
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
|
2017-08-12 21:18:22 +02:00
|
|
|
this.name = AsciiString.cached(name);
|
2009-02-11 10:24:28 +01:00
|
|
|
}
|
|
|
|
|
2009-06-19 16:15:20 +02:00
|
|
|
/**
|
2015-08-19 21:42:31 +02:00
|
|
|
* Returns the name of this method.
|
2009-06-19 16:15:20 +02:00
|
|
|
*/
|
2013-01-16 05:22:50 +01:00
|
|
|
public String name() {
|
2015-08-19 21:42:31 +02:00
|
|
|
return name.toString();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the name of this method.
|
|
|
|
*/
|
|
|
|
public AsciiString asciiName() {
|
2009-02-11 10:24:28 +01:00
|
|
|
return name;
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
|
|
|
|
2008-12-05 02:58:38 +01:00
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
2013-01-16 05:22:50 +01:00
|
|
|
return name().hashCode();
|
2008-12-05 02:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (!(o instanceof HttpMethod)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
HttpMethod that = (HttpMethod) o;
|
2013-01-16 05:22:50 +01:00
|
|
|
return name().equals(that.name());
|
2008-12-05 02:58:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public String toString() {
|
2015-08-19 21:42:31 +02:00
|
|
|
return name.toString();
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|
2008-12-05 02:58:38 +01:00
|
|
|
|
2010-11-12 01:45:39 +01:00
|
|
|
@Override
|
2008-12-05 02:58:38 +01:00
|
|
|
public int compareTo(HttpMethod o) {
|
2013-01-16 05:22:50 +01:00
|
|
|
return name().compareTo(o.name());
|
2008-12-05 02:58:38 +01:00
|
|
|
}
|
2017-11-12 07:37:26 +01:00
|
|
|
|
|
|
|
private static final class EnumNameMap<T> {
|
|
|
|
private final EnumNameMap.Node<T>[] values;
|
|
|
|
private final int valuesMask;
|
|
|
|
|
|
|
|
EnumNameMap(EnumNameMap.Node<T>... nodes) {
|
|
|
|
values = (EnumNameMap.Node<T>[]) new EnumNameMap.Node[findNextPositivePowerOfTwo(nodes.length)];
|
|
|
|
valuesMask = values.length - 1;
|
|
|
|
for (EnumNameMap.Node<T> node : nodes) {
|
|
|
|
int i = hashCode(node.key) & valuesMask;
|
|
|
|
if (values[i] != null) {
|
|
|
|
throw new IllegalArgumentException("index " + i + " collision between values: [" +
|
|
|
|
values[i].key + ", " + node.key + ']');
|
|
|
|
}
|
|
|
|
values[i] = node;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
T get(String name) {
|
|
|
|
EnumNameMap.Node<T> node = values[hashCode(name) & valuesMask];
|
|
|
|
return node == null || !node.key.equals(name) ? null : node.value;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static int hashCode(String name) {
|
|
|
|
// This hash code needs to produce a unique index in the "values" array for each HttpMethod. If new
|
|
|
|
// HttpMethods are added this algorithm will need to be adjusted. The constructor will "fail fast" if there
|
|
|
|
// are duplicates detected.
|
|
|
|
// For example with the current set of HttpMethods it just so happens that the String hash code value
|
|
|
|
// shifted right by 6 bits modulo 16 is unique relative to all other HttpMethod values.
|
|
|
|
return name.hashCode() >>> 6;
|
|
|
|
}
|
|
|
|
|
|
|
|
private static final class Node<T> {
|
|
|
|
final String key;
|
|
|
|
final T value;
|
|
|
|
|
|
|
|
Node(String key, T value) {
|
|
|
|
this.key = key;
|
|
|
|
this.value = value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2008-11-19 08:22:15 +01:00
|
|
|
}
|