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