1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
46
47
48
49
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
106
107
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 return CACHE.getBest(buffer,0,buffer.remaining());
114 }
115
116
117 public final static Trie<HttpMethod> CACHE= new ArrayTrie<HttpMethod>();
118 static
119 {
120 for (HttpMethod method : HttpMethod.values())
121 CACHE.put(method.toString(),method);
122 }
123
124
125 private final ByteBuffer _buffer;
126 private final byte[] _bytes;
127
128
129 HttpMethod()
130 {
131 _bytes=StringUtil.getBytes(toString());
132 _buffer=ByteBuffer.wrap(_bytes);
133 }
134
135
136 public byte[] getBytes()
137 {
138 return _bytes;
139 }
140
141
142 public boolean is(String s)
143 {
144 return toString().equalsIgnoreCase(s);
145 }
146
147
148 public ByteBuffer asBuffer()
149 {
150 return _buffer.asReadOnlyBuffer();
151 }
152
153
154 public String asString()
155 {
156 return toString();
157 }
158
159
160
161
162
163
164 public static HttpMethod fromString(String method)
165 {
166 return CACHE.get(method);
167 }
168 }