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