1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.jboss.netty.handler.codec.spdy;
17
18 import org.jboss.netty.buffer.ChannelBuffer;
19
20 final class SpdyCodecUtil {
21
22 static final int SPDY_SESSION_STREAM_ID = 0;
23
24 static final int SPDY_HEADER_TYPE_OFFSET = 2;
25 static final int SPDY_HEADER_FLAGS_OFFSET = 4;
26 static final int SPDY_HEADER_LENGTH_OFFSET = 5;
27 static final int SPDY_HEADER_SIZE = 8;
28
29 static final int SPDY_MAX_LENGTH = 0xFFFFFF;
30
31 static final byte SPDY_DATA_FLAG_FIN = 0x01;
32
33 static final int SPDY_DATA_FRAME = 0;
34 static final int SPDY_SYN_STREAM_FRAME = 1;
35 static final int SPDY_SYN_REPLY_FRAME = 2;
36 static final int SPDY_RST_STREAM_FRAME = 3;
37 static final int SPDY_SETTINGS_FRAME = 4;
38 static final int SPDY_PUSH_PROMISE_FRAME = 5;
39 static final int SPDY_PING_FRAME = 6;
40 static final int SPDY_GOAWAY_FRAME = 7;
41 static final int SPDY_HEADERS_FRAME = 8;
42 static final int SPDY_WINDOW_UPDATE_FRAME = 9;
43
44 static final byte SPDY_FLAG_FIN = 0x01;
45 static final byte SPDY_FLAG_UNIDIRECTIONAL = 0x02;
46
47 static final byte SPDY_SETTINGS_CLEAR = 0x01;
48 static final byte SPDY_SETTINGS_PERSIST_VALUE = 0x01;
49 static final byte SPDY_SETTINGS_PERSISTED = 0x02;
50
51 static final int SPDY_SETTINGS_MAX_ID = 0xFFFFFF;
52
53 static final int SPDY_MAX_NV_LENGTH = 0xFFFF;
54
55
56 static final byte[] SPDY_DICT = {
57 0x00, 0x00, 0x00, 0x07, 0x6f, 0x70, 0x74, 0x69,
58 0x6f, 0x6e, 0x73, 0x00, 0x00, 0x00, 0x04, 0x68,
59 0x65, 0x61, 0x64, 0x00, 0x00, 0x00, 0x04, 0x70,
60 0x6f, 0x73, 0x74, 0x00, 0x00, 0x00, 0x03, 0x70,
61 0x75, 0x74, 0x00, 0x00, 0x00, 0x06, 0x64, 0x65,
62 0x6c, 0x65, 0x74, 0x65, 0x00, 0x00, 0x00, 0x05,
63 0x74, 0x72, 0x61, 0x63, 0x65, 0x00, 0x00, 0x00,
64 0x06, 0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x00,
65 0x00, 0x00, 0x0e, 0x61, 0x63, 0x63, 0x65, 0x70,
66 0x74, 0x2d, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65,
67 0x74, 0x00, 0x00, 0x00, 0x0f, 0x61, 0x63, 0x63,
68 0x65, 0x70, 0x74, 0x2d, 0x65, 0x6e, 0x63, 0x6f,
69 0x64, 0x69, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x0f,
70 0x61, 0x63, 0x63, 0x65, 0x70, 0x74, 0x2d, 0x6c,
71 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65, 0x00,
72 0x00, 0x00, 0x0d, 0x61, 0x63, 0x63, 0x65, 0x70,
73 0x74, 0x2d, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x73,
74 0x00, 0x00, 0x00, 0x03, 0x61, 0x67, 0x65, 0x00,
75 0x00, 0x00, 0x05, 0x61, 0x6c, 0x6c, 0x6f, 0x77,
76 0x00, 0x00, 0x00, 0x0d, 0x61, 0x75, 0x74, 0x68,
77 0x6f, 0x72, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f,
78 0x6e, 0x00, 0x00, 0x00, 0x0d, 0x63, 0x61, 0x63,
79 0x68, 0x65, 0x2d, 0x63, 0x6f, 0x6e, 0x74, 0x72,
80 0x6f, 0x6c, 0x00, 0x00, 0x00, 0x0a, 0x63, 0x6f,
81 0x6e, 0x6e, 0x65, 0x63, 0x74, 0x69, 0x6f, 0x6e,
82 0x00, 0x00, 0x00, 0x0c, 0x63, 0x6f, 0x6e, 0x74,
83 0x65, 0x6e, 0x74, 0x2d, 0x62, 0x61, 0x73, 0x65,
84 0x00, 0x00, 0x00, 0x10, 0x63, 0x6f, 0x6e, 0x74,
85 0x65, 0x6e, 0x74, 0x2d, 0x65, 0x6e, 0x63, 0x6f,
86 0x64, 0x69, 0x6e, 0x67, 0x00, 0x00, 0x00, 0x10,
87 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d,
88 0x6c, 0x61, 0x6e, 0x67, 0x75, 0x61, 0x67, 0x65,
89 0x00, 0x00, 0x00, 0x0e, 0x63, 0x6f, 0x6e, 0x74,
90 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x65, 0x6e, 0x67,
91 0x74, 0x68, 0x00, 0x00, 0x00, 0x10, 0x63, 0x6f,
92 0x6e, 0x74, 0x65, 0x6e, 0x74, 0x2d, 0x6c, 0x6f,
93 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00,
94 0x00, 0x0b, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
95 0x74, 0x2d, 0x6d, 0x64, 0x35, 0x00, 0x00, 0x00,
96 0x0d, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e, 0x74,
97 0x2d, 0x72, 0x61, 0x6e, 0x67, 0x65, 0x00, 0x00,
98 0x00, 0x0c, 0x63, 0x6f, 0x6e, 0x74, 0x65, 0x6e,
99 0x74, 0x2d, 0x74, 0x79, 0x70, 0x65, 0x00, 0x00,
100 0x00, 0x04, 0x64, 0x61, 0x74, 0x65, 0x00, 0x00,
101 0x00, 0x04, 0x65, 0x74, 0x61, 0x67, 0x00, 0x00,
102 0x00, 0x06, 0x65, 0x78, 0x70, 0x65, 0x63, 0x74,
103 0x00, 0x00, 0x00, 0x07, 0x65, 0x78, 0x70, 0x69,
104 0x72, 0x65, 0x73, 0x00, 0x00, 0x00, 0x04, 0x66,
105 0x72, 0x6f, 0x6d, 0x00, 0x00, 0x00, 0x04, 0x68,
106 0x6f, 0x73, 0x74, 0x00, 0x00, 0x00, 0x08, 0x69,
107 0x66, 0x2d, 0x6d, 0x61, 0x74, 0x63, 0x68, 0x00,
108 0x00, 0x00, 0x11, 0x69, 0x66, 0x2d, 0x6d, 0x6f,
109 0x64, 0x69, 0x66, 0x69, 0x65, 0x64, 0x2d, 0x73,
110 0x69, 0x6e, 0x63, 0x65, 0x00, 0x00, 0x00, 0x0d,
111 0x69, 0x66, 0x2d, 0x6e, 0x6f, 0x6e, 0x65, 0x2d,
112 0x6d, 0x61, 0x74, 0x63, 0x68, 0x00, 0x00, 0x00,
113 0x08, 0x69, 0x66, 0x2d, 0x72, 0x61, 0x6e, 0x67,
114 0x65, 0x00, 0x00, 0x00, 0x13, 0x69, 0x66, 0x2d,
115 0x75, 0x6e, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x69,
116 0x65, 0x64, 0x2d, 0x73, 0x69, 0x6e, 0x63, 0x65,
117 0x00, 0x00, 0x00, 0x0d, 0x6c, 0x61, 0x73, 0x74,
118 0x2d, 0x6d, 0x6f, 0x64, 0x69, 0x66, 0x69, 0x65,
119 0x64, 0x00, 0x00, 0x00, 0x08, 0x6c, 0x6f, 0x63,
120 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00,
121 0x0c, 0x6d, 0x61, 0x78, 0x2d, 0x66, 0x6f, 0x72,
122 0x77, 0x61, 0x72, 0x64, 0x73, 0x00, 0x00, 0x00,
123 0x06, 0x70, 0x72, 0x61, 0x67, 0x6d, 0x61, 0x00,
124 0x00, 0x00, 0x12, 0x70, 0x72, 0x6f, 0x78, 0x79,
125 0x2d, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e, 0x74,
126 0x69, 0x63, 0x61, 0x74, 0x65, 0x00, 0x00, 0x00,
127 0x13, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2d, 0x61,
128 0x75, 0x74, 0x68, 0x6f, 0x72, 0x69, 0x7a, 0x61,
129 0x74, 0x69, 0x6f, 0x6e, 0x00, 0x00, 0x00, 0x05,
130 0x72, 0x61, 0x6e, 0x67, 0x65, 0x00, 0x00, 0x00,
131 0x07, 0x72, 0x65, 0x66, 0x65, 0x72, 0x65, 0x72,
132 0x00, 0x00, 0x00, 0x0b, 0x72, 0x65, 0x74, 0x72,
133 0x79, 0x2d, 0x61, 0x66, 0x74, 0x65, 0x72, 0x00,
134 0x00, 0x00, 0x06, 0x73, 0x65, 0x72, 0x76, 0x65,
135 0x72, 0x00, 0x00, 0x00, 0x02, 0x74, 0x65, 0x00,
136 0x00, 0x00, 0x07, 0x74, 0x72, 0x61, 0x69, 0x6c,
137 0x65, 0x72, 0x00, 0x00, 0x00, 0x11, 0x74, 0x72,
138 0x61, 0x6e, 0x73, 0x66, 0x65, 0x72, 0x2d, 0x65,
139 0x6e, 0x63, 0x6f, 0x64, 0x69, 0x6e, 0x67, 0x00,
140 0x00, 0x00, 0x07, 0x75, 0x70, 0x67, 0x72, 0x61,
141 0x64, 0x65, 0x00, 0x00, 0x00, 0x0a, 0x75, 0x73,
142 0x65, 0x72, 0x2d, 0x61, 0x67, 0x65, 0x6e, 0x74,
143 0x00, 0x00, 0x00, 0x04, 0x76, 0x61, 0x72, 0x79,
144 0x00, 0x00, 0x00, 0x03, 0x76, 0x69, 0x61, 0x00,
145 0x00, 0x00, 0x07, 0x77, 0x61, 0x72, 0x6e, 0x69,
146 0x6e, 0x67, 0x00, 0x00, 0x00, 0x10, 0x77, 0x77,
147 0x77, 0x2d, 0x61, 0x75, 0x74, 0x68, 0x65, 0x6e,
148 0x74, 0x69, 0x63, 0x61, 0x74, 0x65, 0x00, 0x00,
149 0x00, 0x06, 0x6d, 0x65, 0x74, 0x68, 0x6f, 0x64,
150 0x00, 0x00, 0x00, 0x03, 0x67, 0x65, 0x74, 0x00,
151 0x00, 0x00, 0x06, 0x73, 0x74, 0x61, 0x74, 0x75,
152 0x73, 0x00, 0x00, 0x00, 0x06, 0x32, 0x30, 0x30,
153 0x20, 0x4f, 0x4b, 0x00, 0x00, 0x00, 0x07, 0x76,
154 0x65, 0x72, 0x73, 0x69, 0x6f, 0x6e, 0x00, 0x00,
155 0x00, 0x08, 0x48, 0x54, 0x54, 0x50, 0x2f, 0x31,
156 0x2e, 0x31, 0x00, 0x00, 0x00, 0x03, 0x75, 0x72,
157 0x6c, 0x00, 0x00, 0x00, 0x06, 0x70, 0x75, 0x62,
158 0x6c, 0x69, 0x63, 0x00, 0x00, 0x00, 0x0a, 0x73,
159 0x65, 0x74, 0x2d, 0x63, 0x6f, 0x6f, 0x6b, 0x69,
160 0x65, 0x00, 0x00, 0x00, 0x0a, 0x6b, 0x65, 0x65,
161 0x70, 0x2d, 0x61, 0x6c, 0x69, 0x76, 0x65, 0x00,
162 0x00, 0x00, 0x06, 0x6f, 0x72, 0x69, 0x67, 0x69,
163 0x6e, 0x31, 0x30, 0x30, 0x31, 0x30, 0x31, 0x32,
164 0x30, 0x31, 0x32, 0x30, 0x32, 0x32, 0x30, 0x35,
165 0x32, 0x30, 0x36, 0x33, 0x30, 0x30, 0x33, 0x30,
166 0x32, 0x33, 0x30, 0x33, 0x33, 0x30, 0x34, 0x33,
167 0x30, 0x35, 0x33, 0x30, 0x36, 0x33, 0x30, 0x37,
168 0x34, 0x30, 0x32, 0x34, 0x30, 0x35, 0x34, 0x30,
169 0x36, 0x34, 0x30, 0x37, 0x34, 0x30, 0x38, 0x34,
170 0x30, 0x39, 0x34, 0x31, 0x30, 0x34, 0x31, 0x31,
171 0x34, 0x31, 0x32, 0x34, 0x31, 0x33, 0x34, 0x31,
172 0x34, 0x34, 0x31, 0x35, 0x34, 0x31, 0x36, 0x34,
173 0x31, 0x37, 0x35, 0x30, 0x32, 0x35, 0x30, 0x34,
174 0x35, 0x30, 0x35, 0x32, 0x30, 0x33, 0x20, 0x4e,
175 0x6f, 0x6e, 0x2d, 0x41, 0x75, 0x74, 0x68, 0x6f,
176 0x72, 0x69, 0x74, 0x61, 0x74, 0x69, 0x76, 0x65,
177 0x20, 0x49, 0x6e, 0x66, 0x6f, 0x72, 0x6d, 0x61,
178 0x74, 0x69, 0x6f, 0x6e, 0x32, 0x30, 0x34, 0x20,
179 0x4e, 0x6f, 0x20, 0x43, 0x6f, 0x6e, 0x74, 0x65,
180 0x6e, 0x74, 0x33, 0x30, 0x31, 0x20, 0x4d, 0x6f,
181 0x76, 0x65, 0x64, 0x20, 0x50, 0x65, 0x72, 0x6d,
182 0x61, 0x6e, 0x65, 0x6e, 0x74, 0x6c, 0x79, 0x34,
183 0x30, 0x30, 0x20, 0x42, 0x61, 0x64, 0x20, 0x52,
184 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x34, 0x30,
185 0x31, 0x20, 0x55, 0x6e, 0x61, 0x75, 0x74, 0x68,
186 0x6f, 0x72, 0x69, 0x7a, 0x65, 0x64, 0x34, 0x30,
187 0x33, 0x20, 0x46, 0x6f, 0x72, 0x62, 0x69, 0x64,
188 0x64, 0x65, 0x6e, 0x34, 0x30, 0x34, 0x20, 0x4e,
189 0x6f, 0x74, 0x20, 0x46, 0x6f, 0x75, 0x6e, 0x64,
190 0x35, 0x30, 0x30, 0x20, 0x49, 0x6e, 0x74, 0x65,
191 0x72, 0x6e, 0x61, 0x6c, 0x20, 0x53, 0x65, 0x72,
192 0x76, 0x65, 0x72, 0x20, 0x45, 0x72, 0x72, 0x6f,
193 0x72, 0x35, 0x30, 0x31, 0x20, 0x4e, 0x6f, 0x74,
194 0x20, 0x49, 0x6d, 0x70, 0x6c, 0x65, 0x6d, 0x65,
195 0x6e, 0x74, 0x65, 0x64, 0x35, 0x30, 0x33, 0x20,
196 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x20,
197 0x55, 0x6e, 0x61, 0x76, 0x61, 0x69, 0x6c, 0x61,
198 0x62, 0x6c, 0x65, 0x4a, 0x61, 0x6e, 0x20, 0x46,
199 0x65, 0x62, 0x20, 0x4d, 0x61, 0x72, 0x20, 0x41,
200 0x70, 0x72, 0x20, 0x4d, 0x61, 0x79, 0x20, 0x4a,
201 0x75, 0x6e, 0x20, 0x4a, 0x75, 0x6c, 0x20, 0x41,
202 0x75, 0x67, 0x20, 0x53, 0x65, 0x70, 0x74, 0x20,
203 0x4f, 0x63, 0x74, 0x20, 0x4e, 0x6f, 0x76, 0x20,
204 0x44, 0x65, 0x63, 0x20, 0x30, 0x30, 0x3a, 0x30,
205 0x30, 0x3a, 0x30, 0x30, 0x20, 0x4d, 0x6f, 0x6e,
206 0x2c, 0x20, 0x54, 0x75, 0x65, 0x2c, 0x20, 0x57,
207 0x65, 0x64, 0x2c, 0x20, 0x54, 0x68, 0x75, 0x2c,
208 0x20, 0x46, 0x72, 0x69, 0x2c, 0x20, 0x53, 0x61,
209 0x74, 0x2c, 0x20, 0x53, 0x75, 0x6e, 0x2c, 0x20,
210 0x47, 0x4d, 0x54, 0x63, 0x68, 0x75, 0x6e, 0x6b,
211 0x65, 0x64, 0x2c, 0x74, 0x65, 0x78, 0x74, 0x2f,
212 0x68, 0x74, 0x6d, 0x6c, 0x2c, 0x69, 0x6d, 0x61,
213 0x67, 0x65, 0x2f, 0x70, 0x6e, 0x67, 0x2c, 0x69,
214 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x6a, 0x70, 0x67,
215 0x2c, 0x69, 0x6d, 0x61, 0x67, 0x65, 0x2f, 0x67,
216 0x69, 0x66, 0x2c, 0x61, 0x70, 0x70, 0x6c, 0x69,
217 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78,
218 0x6d, 0x6c, 0x2c, 0x61, 0x70, 0x70, 0x6c, 0x69,
219 0x63, 0x61, 0x74, 0x69, 0x6f, 0x6e, 0x2f, 0x78,
220 0x68, 0x74, 0x6d, 0x6c, 0x2b, 0x78, 0x6d, 0x6c,
221 0x2c, 0x74, 0x65, 0x78, 0x74, 0x2f, 0x70, 0x6c,
222 0x61, 0x69, 0x6e, 0x2c, 0x74, 0x65, 0x78, 0x74,
223 0x2f, 0x6a, 0x61, 0x76, 0x61, 0x73, 0x63, 0x72,
224 0x69, 0x70, 0x74, 0x2c, 0x70, 0x75, 0x62, 0x6c,
225 0x69, 0x63, 0x70, 0x72, 0x69, 0x76, 0x61, 0x74,
226 0x65, 0x6d, 0x61, 0x78, 0x2d, 0x61, 0x67, 0x65,
227 0x3d, 0x67, 0x7a, 0x69, 0x70, 0x2c, 0x64, 0x65,
228 0x66, 0x6c, 0x61, 0x74, 0x65, 0x2c, 0x73, 0x64,
229 0x63, 0x68, 0x63, 0x68, 0x61, 0x72, 0x73, 0x65,
230 0x74, 0x3d, 0x75, 0x74, 0x66, 0x2d, 0x38, 0x63,
231 0x68, 0x61, 0x72, 0x73, 0x65, 0x74, 0x3d, 0x69,
232 0x73, 0x6f, 0x2d, 0x38, 0x38, 0x35, 0x39, 0x2d,
233 0x31, 0x2c, 0x75, 0x74, 0x66, 0x2d, 0x2c, 0x2a,
234 0x2c, 0x65, 0x6e, 0x71, 0x3d, 0x30, 0x2e
235 };
236
237 private SpdyCodecUtil() {
238 }
239
240
241
242
243 static int getUnsignedShort(ChannelBuffer buf, int offset) {
244 return (buf.getByte(offset) & 0xFF) << 8 |
245 buf.getByte(offset + 1) & 0xFF;
246 }
247
248
249
250
251 static int getUnsignedMedium(ChannelBuffer buf, int offset) {
252 return (buf.getByte(offset) & 0xFF) << 16 |
253 (buf.getByte(offset + 1) & 0xFF) << 8 |
254 buf.getByte(offset + 2) & 0xFF;
255 }
256
257
258
259
260 static int getUnsignedInt(ChannelBuffer buf, int offset) {
261 return (buf.getByte(offset) & 0x7F) << 24 |
262 (buf.getByte(offset + 1) & 0xFF) << 16 |
263 (buf.getByte(offset + 2) & 0xFF) << 8 |
264 buf.getByte(offset + 3) & 0xFF;
265 }
266
267
268
269
270 static int getSignedInt(ChannelBuffer buf, int offset) {
271 return (buf.getByte(offset) & 0xFF) << 24 |
272 (buf.getByte(offset + 1) & 0xFF) << 16 |
273 (buf.getByte(offset + 2) & 0xFF) << 8 |
274 buf.getByte(offset + 3) & 0xFF;
275 }
276
277
278
279
280 static boolean isServerId(int id) {
281
282 return id % 2 == 0;
283 }
284
285
286
287
288 static void validateHeaderName(String name) {
289 if (name == null) {
290 throw new NullPointerException("name");
291 }
292 if (name.length() == 0) {
293 throw new IllegalArgumentException(
294 "name cannot be length zero");
295 }
296
297
298 if (name.length() > SPDY_MAX_NV_LENGTH) {
299 throw new IllegalArgumentException(
300 "name exceeds allowable length: " + name);
301 }
302 for (int i = 0; i < name.length(); i ++) {
303 char c = name.charAt(i);
304 if (c == 0) {
305 throw new IllegalArgumentException(
306 "name contains null character: " + name);
307 }
308 if (c > 127) {
309 throw new IllegalArgumentException(
310 "name contains non-ascii character: " + name);
311 }
312 }
313 }
314
315
316
317
318 static void validateHeaderValue(String value) {
319 if (value == null) {
320 throw new NullPointerException("value");
321 }
322 for (int i = 0; i < value.length(); i ++) {
323 char c = value.charAt(i);
324 if (c == 0) {
325 throw new IllegalArgumentException(
326 "value contains null character: " + value);
327 }
328 }
329 }
330 }