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 public enum HttpVersion
30 {
31 HTTP_0_9("HTTP/0.9",9),
32 HTTP_1_0("HTTP/1.0",10),
33 HTTP_1_1("HTTP/1.1",11),
34 HTTP_2_0("HTTP/2.0",20);
35
36
37 public final static Trie<HttpVersion> CACHE= new ArrayTrie<HttpVersion>();
38 static
39 {
40 for (HttpVersion version : HttpVersion.values())
41 CACHE.put(version.toString(),version);
42 }
43
44
45
46
47
48
49
50
51
52 public static HttpVersion lookAheadGet(byte[] bytes, int position, int limit)
53 {
54 int length=limit-position;
55 if (length<9)
56 return null;
57
58 if (bytes[position+4]=='/' && bytes[position+6]=='.' && Character.isWhitespace((char)bytes[position+8]) &&
59 ((bytes[position]=='H' && bytes[position+1]=='T' && bytes[position+2]=='T' && bytes[position+3]=='P') ||
60 (bytes[position]=='h' && bytes[position+1]=='t' && bytes[position+2]=='t' && bytes[position+3]=='p')))
61 {
62 switch(bytes[position+5])
63 {
64 case '1':
65 switch(bytes[position+7])
66 {
67 case '0':
68 return HTTP_1_0;
69 case '1':
70 return HTTP_1_1;
71 }
72 break;
73 case '2':
74 switch(bytes[position+7])
75 {
76 case '0':
77 return HTTP_2_0;
78 }
79 break;
80 }
81 }
82
83 return null;
84 }
85
86
87
88
89
90
91
92 public static HttpVersion lookAheadGet(ByteBuffer buffer)
93 {
94 if (buffer.hasArray())
95 return lookAheadGet(buffer.array(),buffer.arrayOffset()+buffer.position(),buffer.arrayOffset()+buffer.limit());
96 return null;
97 }
98
99
100 private final String _string;
101 private final byte[] _bytes;
102 private final ByteBuffer _buffer;
103 private final int _version;
104
105
106 HttpVersion(String s,int version)
107 {
108 _string=s;
109 _bytes=StringUtil.getBytes(s);
110 _buffer=ByteBuffer.wrap(_bytes);
111 _version=version;
112 }
113
114
115 public byte[] toBytes()
116 {
117 return _bytes;
118 }
119
120
121 public ByteBuffer toBuffer()
122 {
123 return _buffer.asReadOnlyBuffer();
124 }
125
126
127 public int getVersion()
128 {
129 return _version;
130 }
131
132
133 public boolean is(String s)
134 {
135 return _string.equalsIgnoreCase(s);
136 }
137
138
139 public String asString()
140 {
141 return _string;
142 }
143
144
145 @Override
146 public String toString()
147 {
148 return _string;
149 }
150
151
152
153
154
155
156 public static HttpVersion fromString(String version)
157 {
158 return CACHE.get(version);
159 }
160
161
162 public static HttpVersion fromVersion(int version)
163 {
164 switch(version)
165 {
166 case 9: return HttpVersion.HTTP_0_9;
167 case 10: return HttpVersion.HTTP_1_0;
168 case 11: return HttpVersion.HTTP_1_1;
169 default: throw new IllegalArgumentException();
170 }
171 }
172 }