2 // ========================================================================
3 // Copyright (c) 1995-2016 Mort Bay Consulting Pty. Ltd.
4 // ------------------------------------------------------------------------
5 // All rights reserved. This program and the accompanying materials
6 // are made available under the terms of the Eclipse Public License v1.0
7 // and Apache License v2.0 which accompanies this distribution.
9 // The Eclipse Public License is available at
10 // http://www.eclipse.org/legal/epl-v10.html
12 // The Apache License v2.0 is available at
13 // http://www.opensource.org/licenses/apache2.0.php
15 // You may elect to redistribute this code under either of these licenses.
16 // ========================================================================
19 package org.eclipse.jetty.http;
21 import java.nio.ByteBuffer;
23 import org.eclipse.jetty.util.ArrayTrie;
24 import org.eclipse.jetty.util.StringUtil;
25 import org.eclipse.jetty.util.Trie;
28 /* ------------------------------------------------------------------------------- */
31 public enum HttpMethod
45 /* ------------------------------------------------------------ */
47 * Optimized lookup to find a method name and trailing space in a byte array.
48 * @param bytes Array containing ISO-8859-1 characters
49 * @param position The first valid index
50 * @param limit The first non valid index
51 * @return A HttpMethod if a match or null if no easy match.
53 public static HttpMethod lookAheadGet(byte[] bytes, final int position, int limit)
55 int length=limit-position;
58 switch(bytes[position])
61 if (bytes[position+1]=='E' && bytes[position+2]=='T' && bytes[position+3]==' ')
65 if (bytes[position+1]=='O' && bytes[position+2]=='S' && bytes[position+3]=='T' && length>=5 && bytes[position+4]==' ')
67 if (bytes[position+1]=='R' && bytes[position+2]=='O' && bytes[position+3]=='X' && length>=6 && bytes[position+4]=='Y' && bytes[position+5]==' ')
69 if (bytes[position+1]=='U' && bytes[position+2]=='T' && bytes[position+3]==' ')
71 if (bytes[position+1]=='R' && bytes[position+2]=='I' && bytes[position+3]==' ')
75 if (bytes[position+1]=='E' && bytes[position+2]=='A' && bytes[position+3]=='D' && length>=5 && bytes[position+4]==' ')
79 if (bytes[position+1]=='P' && bytes[position+2]=='T' && bytes[position+3]=='I' && length>=8 &&
80 bytes[position+4]=='O' && bytes[position+5]=='N' && bytes[position+6]=='S' && bytes[position+7]==' ' )
84 if (bytes[position+1]=='E' && bytes[position+2]=='L' && bytes[position+3]=='E' && length>=7 &&
85 bytes[position+4]=='T' && bytes[position+5]=='E' && bytes[position+6]==' ' )
89 if (bytes[position+1]=='R' && bytes[position+2]=='A' && bytes[position+3]=='C' && length>=6 &&
90 bytes[position+4]=='E' && bytes[position+5]==' ' )
94 if (bytes[position+1]=='O' && bytes[position+2]=='N' && bytes[position+3]=='N' && length>=8 &&
95 bytes[position+4]=='E' && bytes[position+5]=='C' && bytes[position+6]=='T' && bytes[position+7]==' ' )
99 if (bytes[position+1]=='O' && bytes[position+2]=='V' && bytes[position+3]=='E' && length>=5 && bytes[position+4]==' ')
109 /* ------------------------------------------------------------ */
111 * Optimized lookup to find a method name and trailing space in a byte array.
112 * @param buffer buffer containing ISO-8859-1 characters, it is not modified.
113 * @return A HttpMethod if a match or null if no easy match.
115 public static HttpMethod lookAheadGet(ByteBuffer buffer)
117 if (buffer.hasArray())
118 return lookAheadGet(buffer.array(),buffer.arrayOffset()+buffer.position(),buffer.arrayOffset()+buffer.limit());
120 int l = buffer.remaining();
123 HttpMethod m = CACHE.getBest(buffer,0,l);
126 int ml = m.asString().length();
127 if (l>ml && buffer.get(buffer.position()+ml)==' ')
134 /* ------------------------------------------------------------ */
135 public final static Trie<HttpMethod> CACHE= new ArrayTrie<>();
138 for (HttpMethod method : HttpMethod.values())
139 CACHE.put(method.toString(),method);
142 /* ------------------------------------------------------------ */
143 private final ByteBuffer _buffer;
144 private final byte[] _bytes;
146 /* ------------------------------------------------------------ */
149 _bytes=StringUtil.getBytes(toString());
150 _buffer=ByteBuffer.wrap(_bytes);
153 /* ------------------------------------------------------------ */
154 public byte[] getBytes()
159 /* ------------------------------------------------------------ */
160 public boolean is(String s)
162 return toString().equalsIgnoreCase(s);
165 /* ------------------------------------------------------------ */
166 public ByteBuffer asBuffer()
168 return _buffer.asReadOnlyBuffer();
171 /* ------------------------------------------------------------ */
172 public String asString()
177 /* ------------------------------------------------------------ */
179 * Converts the given String parameter to an HttpMethod
180 * @param method the String to get the equivalent HttpMethod from
181 * @return the HttpMethod or null if the parameter method is unknown
183 public static HttpMethod fromString(String method)
185 return CACHE.get(method);