View Javadoc

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