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
114
115
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
164
165
166
167 public static HttpMethod fromString(String method)
168 {
169 return CACHE.get(method);
170 }
171 }