]> WPIA git - gigi.git/blob - lib/jetty/org/eclipse/jetty/http/HttpMethod.java
Importing upstream Jetty jetty-9.2.1.v20140609
[gigi.git] / lib / jetty / org / eclipse / jetty / http / HttpMethod.java
1 //
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.
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     PROXY;
43
44     /* ------------------------------------------------------------ */
45     /**
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.
51      */
52     public static HttpMethod lookAheadGet(byte[] bytes, final int position, int limit)
53     {
54         int length=limit-position;
55         if (length<4)
56             return null;
57         switch(bytes[position])
58         {
59             case 'G':
60                 if (bytes[position+1]=='E' && bytes[position+2]=='T' && bytes[position+3]==' ')
61                     return GET;
62                 break;
63             case 'P':
64                 if (bytes[position+1]=='O' && bytes[position+2]=='S' && bytes[position+3]=='T' && length>=5 && bytes[position+4]==' ')
65                     return POST;
66                 if (bytes[position+1]=='R' && bytes[position+2]=='O' && bytes[position+3]=='X' && length>=6 && bytes[position+4]=='Y' && bytes[position+5]==' ')
67                     return PROXY;
68                 if (bytes[position+1]=='U' && bytes[position+2]=='T' && bytes[position+3]==' ')
69                     return PUT;
70                 break;
71             case 'H':
72                 if (bytes[position+1]=='E' && bytes[position+2]=='A' && bytes[position+3]=='D' && length>=5 && bytes[position+4]==' ')
73                     return HEAD;
74                 break;
75             case 'O':
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]==' ' )
78                     return OPTIONS;
79                 break;
80             case 'D':
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]==' ' )
83                     return DELETE;
84                 break;
85             case 'T':
86                 if (bytes[position+1]=='R' && bytes[position+2]=='A' && bytes[position+3]=='C' && length>=6 &&
87                 bytes[position+4]=='E' && bytes[position+5]==' ' )
88                     return TRACE;
89                 break;
90             case 'C':
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]==' ' )
93                     return CONNECT;
94                 break;
95             case 'M':
96                 if (bytes[position+1]=='O' && bytes[position+2]=='V' && bytes[position+3]=='E' &&  bytes[position+4]==' ')
97                     return MOVE;
98                 break;
99
100             default:
101                 break;
102         }
103         return null;
104     }
105
106     /* ------------------------------------------------------------ */
107     /**
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.
111      */
112     public static HttpMethod lookAheadGet(ByteBuffer buffer)
113     {
114         if (buffer.hasArray())
115             return lookAheadGet(buffer.array(),buffer.arrayOffset()+buffer.position(),buffer.arrayOffset()+buffer.limit());
116
117         // TODO use cache and check for space
118         // return CACHE.getBest(buffer,0,buffer.remaining());
119         return null;
120     }
121
122     /* ------------------------------------------------------------ */
123     public final static Trie<HttpMethod> CACHE= new ArrayTrie<>();
124     static
125     {
126         for (HttpMethod method : HttpMethod.values())
127             CACHE.put(method.toString(),method);
128     }
129
130     /* ------------------------------------------------------------ */
131     private final ByteBuffer _buffer;
132     private final byte[] _bytes;
133
134     /* ------------------------------------------------------------ */
135     HttpMethod()
136     {
137         _bytes=StringUtil.getBytes(toString());
138         _buffer=ByteBuffer.wrap(_bytes);
139     }
140
141     /* ------------------------------------------------------------ */
142     public byte[] getBytes()
143     {
144         return _bytes;
145     }
146
147     /* ------------------------------------------------------------ */
148     public boolean is(String s)
149     {
150         return toString().equalsIgnoreCase(s);
151     }
152
153     /* ------------------------------------------------------------ */
154     public ByteBuffer asBuffer()
155     {
156         return _buffer.asReadOnlyBuffer();
157     }
158
159     /* ------------------------------------------------------------ */
160     public String asString()
161     {
162         return toString();
163     }
164
165     /**
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
169      */
170     public static HttpMethod fromString(String method)
171     {
172         return CACHE.get(method);
173     }
174 }