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.ObjectUtil.checkNonEmptyAfterTrim; 21 22 /** 23 * The request method of HTTP or its derived protocols, such as 24 * <a href="https://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and 25 * <a href="https://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a>. 26 */ 27 public class HttpMethod implements Comparable<HttpMethod> { 28 29 private static final String GET_STRING = "GET"; 30 private static final String POST_STRING = "POST"; 31 32 /** 33 * The OPTIONS method represents a request for information about the communication options 34 * available on the request/response chain identified by the Request-URI. This method allows 35 * the client to determine the options and/or requirements associated with a resource, or the 36 * capabilities of a server, without implying a resource action or initiating a resource 37 * retrieval. 38 */ 39 public static final HttpMethod OPTIONS = new HttpMethod("OPTIONS"); 40 41 /** 42 * The GET method means retrieve whatever information (in the form of an entity) is identified 43 * by the Request-URI. If the Request-URI refers to a data-producing process, it is the 44 * produced data which shall be returned as the entity in the response and not the source text 45 * of the process, unless that text happens to be the output of the process. 46 */ 47 public static final HttpMethod GET = new HttpMethod(GET_STRING); 48 49 /** 50 * The HEAD method is identical to GET except that the server MUST NOT return a message-body 51 * in the response. 52 */ 53 public static final HttpMethod HEAD = new HttpMethod("HEAD"); 54 55 /** 56 * The POST method is used to request that the origin server accept the entity enclosed in the 57 * request as a new subordinate of the resource identified by the Request-URI in the 58 * Request-Line. 59 */ 60 public static final HttpMethod POST = new HttpMethod(POST_STRING); 61 62 /** 63 * The PUT method requests that the enclosed entity be stored under the supplied Request-URI. 64 */ 65 public static final HttpMethod PUT = new HttpMethod("PUT"); 66 67 /** 68 * The PATCH method requests that a set of changes described in the 69 * request entity be applied to the resource identified by the Request-URI. 70 */ 71 public static final HttpMethod PATCH = new HttpMethod("PATCH"); 72 73 /** 74 * The DELETE method requests that the origin server delete the resource identified by the 75 * Request-URI. 76 */ 77 public static final HttpMethod DELETE = new HttpMethod("DELETE"); 78 79 /** 80 * The TRACE method is used to invoke a remote, application-layer loop- back of the request 81 * message. 82 */ 83 public static final HttpMethod TRACE = new HttpMethod("TRACE"); 84 85 /** 86 * This specification reserves the method name CONNECT for use with a proxy that can dynamically 87 * switch to being a tunnel 88 */ 89 public static final HttpMethod CONNECT = new HttpMethod("CONNECT"); 90 91 /** 92 * Returns the {@link HttpMethod} represented by the specified name. 93 * If the specified name is a standard HTTP method name, a cached instance 94 * will be returned. Otherwise, a new instance will be returned. 95 */ 96 public static HttpMethod valueOf(String name) { 97 switch (name) { 98 case "OPTIONS": return HttpMethod.OPTIONS; 99 case "GET": return HttpMethod.GET; 100 case "HEAD": return HttpMethod.HEAD; 101 case "POST": return HttpMethod.POST; 102 case "PUT": return HttpMethod.PUT; 103 case "PATCH": return HttpMethod.PATCH; 104 case "DELETE": return HttpMethod.DELETE; 105 case "TRACE": return HttpMethod.TRACE; 106 case "CONNECT": return HttpMethod.CONNECT; 107 default: return new HttpMethod(name); 108 } 109 } 110 111 private final AsciiString name; 112 113 /** 114 * Creates a new HTTP method with the specified name. You will not need to 115 * create a new method unless you are implementing a protocol derived from 116 * HTTP, such as 117 * <a href="https://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol">RTSP</a> and 118 * <a href="https://en.wikipedia.org/wiki/Internet_Content_Adaptation_Protocol">ICAP</a> 119 */ 120 public HttpMethod(String name) { 121 name = checkNonEmptyAfterTrim(name, "name"); 122 int index = HttpUtil.validateToken(name); 123 if (index != -1) { 124 throw new IllegalArgumentException( 125 "Illegal character in HTTP Method: 0x" + Integer.toHexString(name.charAt(index))); 126 } 127 this.name = AsciiString.cached(name); 128 } 129 130 /** 131 * Returns the name of this method. 132 */ 133 public String name() { 134 return name.toString(); 135 } 136 137 /** 138 * Returns the name of this method. 139 */ 140 public AsciiString asciiName() { 141 return name; 142 } 143 144 @Override 145 public int hashCode() { 146 return name().hashCode(); 147 } 148 149 @Override 150 public boolean equals(Object o) { 151 if (this == o) { 152 return true; 153 } 154 if (!(o instanceof HttpMethod)) { 155 return false; 156 } 157 158 HttpMethod that = (HttpMethod) o; 159 return name().equals(that.name()); 160 } 161 162 @Override 163 public String toString() { 164 return name.toString(); 165 } 166 167 @Override 168 public int compareTo(HttpMethod o) { 169 if (o == this) { 170 return 0; 171 } 172 return name().compareTo(o.name()); 173 } 174 175 }