View Javadoc

1   //
2   //  ========================================================================
3   //  Copyright (c) 1995-2013 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.
8   //
9   //      The Eclipse Public License is available at
10  //      http://www.eclipse.org/legal/epl-v10.html
11  //
12  //      The Apache License v2.0 is available at
13  //      http://www.opensource.org/licenses/apache2.0.php
14  //
15  //  You may elect to redistribute this code under either of these licenses.
16  //  ========================================================================
17  //
18  
19  package org.eclipse.jetty.http;
20  
21  import java.nio.ByteBuffer;
22  
23  import org.eclipse.jetty.util.ArrayTrie;
24  import org.eclipse.jetty.util.StringUtil;
25  import org.eclipse.jetty.util.Trie;
26  
27  
28  /* ------------------------------------------------------------------------------- */
29  /**
30   */
31  public enum HttpMethod
32  {
33      GET,
34      POST,
35      HEAD,
36      PUT,
37      OPTIONS,
38      DELETE,
39      TRACE,
40      CONNECT,
41      MOVE;
42  
43      /* ------------------------------------------------------------ */
44      /**
45       * Optimised lookup to find a method name and trailing space in a byte array.
46       * @param bytes Array containing ISO-8859-1 characters
47       * @param position The first valid index
48       * @param limit The first non valid index
49       * @return A HttpMethod if a match or null if no easy match.
50       */
51      public static HttpMethod lookAheadGet(byte[] bytes, int position, int limit)
52      {
53          int length=limit-position;
54          if (length<4)
55              return null;
56          switch(bytes[position])
57          {
58              case 'G':
59                  if (bytes[position+1]=='E' && bytes[position+2]=='T' && bytes[position+3]==' ')
60                      return GET;
61                  break;
62              case 'P':
63                  if (bytes[position+1]=='O' && bytes[position+2]=='S' && bytes[position+3]=='T' && length>=5 && bytes[position+4]==' ')
64                      return POST;
65                  if (bytes[position+1]=='U' && bytes[position+2]=='T' && bytes[position+3]==' ')
66                      return PUT;
67                  break;
68              case 'H':
69                  if (bytes[position+1]=='E' && bytes[position+2]=='A' && bytes[position+3]=='D' && length>=5 && bytes[position+4]==' ')
70                      return HEAD;
71                  break;
72              case 'O':
73                  if (bytes[position+1]=='O' && bytes[position+2]=='T' && bytes[position+3]=='I' && length>=8 &&
74                  bytes[position+4]=='O' && bytes[position+5]=='N' && bytes[position+6]=='S' && bytes[position+7]==' ' )
75                      return OPTIONS;
76                  break;
77              case 'D':
78                  if (bytes[position+1]=='E' && bytes[position+2]=='L' && bytes[position+3]=='E' && length>=7 &&
79                  bytes[position+4]=='T' && bytes[position+5]=='E' && bytes[position+6]==' ' )
80                      return DELETE;
81                  break;
82              case 'T':
83                  if (bytes[position+1]=='R' && bytes[position+2]=='A' && bytes[position+3]=='C' && length>=6 &&
84                  bytes[position+4]=='E' && bytes[position+5]==' ' )
85                      return TRACE;
86                  break;
87              case 'C':
88                  if (bytes[position+1]=='O' && bytes[position+2]=='N' && bytes[position+3]=='N' && length>=8 &&
89                  bytes[position+4]=='E' && bytes[position+5]=='C' && bytes[position+6]=='T' && bytes[position+7]==' ' )
90                      return CONNECT;
91                  break;
92              case 'M':
93                  if (bytes[position+1]=='O' && bytes[position+2]=='V' && bytes[position+3]=='E' &&  bytes[position+4]==' ')
94                      return MOVE;
95                  break;
96  
97              default:
98                  break;
99          }
100         return null;
101     }
102 
103     /* ------------------------------------------------------------ */
104     /**
105      * Optimised lookup to find a method name and trailing space in a byte array.
106      * @param buffer buffer containing ISO-8859-1 characters
107      * @return A HttpMethod if a match or null if no easy match.
108      */
109     public static HttpMethod lookAheadGet(ByteBuffer buffer)
110     {
111         if (buffer.hasArray())
112             return lookAheadGet(buffer.array(),buffer.arrayOffset()+buffer.position(),buffer.arrayOffset()+buffer.limit());
113 
114         // TODO use cache and check for space
115         // return CACHE.getBest(buffer,0,buffer.remaining());
116         return null;
117     }
118 
119     /* ------------------------------------------------------------ */
120     public final static Trie<HttpMethod> CACHE= new ArrayTrie<>();
121     static
122     {
123         for (HttpMethod method : HttpMethod.values())
124             CACHE.put(method.toString(),method);
125     }
126 
127     /* ------------------------------------------------------------ */
128     private final ByteBuffer _buffer;
129     private final byte[] _bytes;
130 
131     /* ------------------------------------------------------------ */
132     HttpMethod()
133     {
134         _bytes=StringUtil.getBytes(toString());
135         _buffer=ByteBuffer.wrap(_bytes);
136     }
137 
138     /* ------------------------------------------------------------ */
139     public byte[] getBytes()
140     {
141         return _bytes;
142     }
143 
144     /* ------------------------------------------------------------ */
145     public boolean is(String s)
146     {
147         return toString().equalsIgnoreCase(s);
148     }
149 
150     /* ------------------------------------------------------------ */
151     public ByteBuffer asBuffer()
152     {
153         return _buffer.asReadOnlyBuffer();
154     }
155 
156     /* ------------------------------------------------------------ */
157     public String asString()
158     {
159         return toString();
160     }
161 
162     /**
163      * Converts the given String parameter to an HttpMethod
164      * @param method the String to get the equivalent HttpMethod from
165      * @return the HttpMethod or null if the parameter method is unknown
166      */
167     public static HttpMethod fromString(String method)
168     {
169         return CACHE.get(method);
170     }
171 }