2 // ========================================================================
3 // Copyright (c) 1995-2014 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
44 /* ------------------------------------------------------------ */
46 * Optimised lookup to find a method name and trailing space in a byte array.
47 * @param bytes Array containing ISO-8859-1 characters
48 * @param position The first valid index
49 * @param limit The first non valid index
50 * @return A HttpMethod if a match or null if no easy match.
52 public static HttpMethod lookAheadGet(byte[] bytes, final int position, int limit)
54 int length=limit-position;
57 switch(bytes[position])
60 if (bytes[position+1]=='E' && bytes[position+2]=='T' && bytes[position+3]==' ')
64 if (bytes[position+1]=='O' && bytes[position+2]=='S' && bytes[position+3]=='T' && length>=5 && bytes[position+4]==' ')
66 if (bytes[position+1]=='R' && bytes[position+2]=='O' && bytes[position+3]=='X' && length>=6 && bytes[position+4]=='Y' && bytes[position+5]==' ')
68 if (bytes[position+1]=='U' && bytes[position+2]=='T' && bytes[position+3]==' ')
72 if (bytes[position+1]=='E' && bytes[position+2]=='A' && bytes[position+3]=='D' && length>=5 && bytes[position+4]==' ')
76 if (bytes[position+1]=='O' && bytes[position+2]=='T' && bytes[position+3]=='I' && length>=8 &&
77 bytes[position+4]=='O' && bytes[position+5]=='N' && bytes[position+6]=='S' && bytes[position+7]==' ' )
81 if (bytes[position+1]=='E' && bytes[position+2]=='L' && bytes[position+3]=='E' && length>=7 &&
82 bytes[position+4]=='T' && bytes[position+5]=='E' && bytes[position+6]==' ' )
86 if (bytes[position+1]=='R' && bytes[position+2]=='A' && bytes[position+3]=='C' && length>=6 &&
87 bytes[position+4]=='E' && bytes[position+5]==' ' )
91 if (bytes[position+1]=='O' && bytes[position+2]=='N' && bytes[position+3]=='N' && length>=8 &&
92 bytes[position+4]=='E' && bytes[position+5]=='C' && bytes[position+6]=='T' && bytes[position+7]==' ' )
96 if (bytes[position+1]=='O' && bytes[position+2]=='V' && bytes[position+3]=='E' && bytes[position+4]==' ')
106 /* ------------------------------------------------------------ */
108 * Optimised lookup to find a method name and trailing space in a byte array.
109 * @param buffer buffer containing ISO-8859-1 characters
110 * @return A HttpMethod if a match or null if no easy match.
112 public static HttpMethod lookAheadGet(ByteBuffer buffer)
114 if (buffer.hasArray())
115 return lookAheadGet(buffer.array(),buffer.arrayOffset()+buffer.position(),buffer.arrayOffset()+buffer.limit());
117 // TODO use cache and check for space
118 // return CACHE.getBest(buffer,0,buffer.remaining());
122 /* ------------------------------------------------------------ */
123 public final static Trie<HttpMethod> CACHE= new ArrayTrie<>();
126 for (HttpMethod method : HttpMethod.values())
127 CACHE.put(method.toString(),method);
130 /* ------------------------------------------------------------ */
131 private final ByteBuffer _buffer;
132 private final byte[] _bytes;
134 /* ------------------------------------------------------------ */
137 _bytes=StringUtil.getBytes(toString());
138 _buffer=ByteBuffer.wrap(_bytes);
141 /* ------------------------------------------------------------ */
142 public byte[] getBytes()
147 /* ------------------------------------------------------------ */
148 public boolean is(String s)
150 return toString().equalsIgnoreCase(s);
153 /* ------------------------------------------------------------ */
154 public ByteBuffer asBuffer()
156 return _buffer.asReadOnlyBuffer();
159 /* ------------------------------------------------------------ */
160 public String asString()
166 * Converts the given String parameter to an HttpMethod
167 * @param method the String to get the equivalent HttpMethod from
168 * @return the HttpMethod or null if the parameter method is unknown
170 public static HttpMethod fromString(String method)
172 return CACHE.get(method);