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 PROXY;
43
44
45
46
47
48
49
50
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
109
110
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
118
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
167
168
169
170 public static HttpMethod fromString(String method)
171 {
172 return CACHE.get(method);
173 }
174 }