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.handler.codec.http.HttpMethod;
19 import io.netty.handler.codec.http.HttpResponseStatus;
20 import io.netty.handler.codec.http.HttpVersion;
21
22 import java.util.Collections;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.Map;
26 import java.util.Set;
27
28
29
30
31
32 public abstract class SpdyHeaders implements Iterable<Map.Entry<String, String>> {
33
34 public static final SpdyHeaders EMPTY_HEADERS = new SpdyHeaders() {
35
36 @Override
37 public List<String> getAll(String name) {
38 return Collections.emptyList();
39 }
40
41 @Override
42 public List<Map.Entry<String, String>> entries() {
43 return Collections.emptyList();
44 }
45
46 @Override
47 public boolean contains(String name) {
48 return false;
49 }
50
51 @Override
52 public boolean isEmpty() {
53 return true;
54 }
55
56 @Override
57 public Set<String> names() {
58 return Collections.emptySet();
59 }
60
61 @Override
62 public SpdyHeaders add(String name, Object value) {
63 throw new UnsupportedOperationException("read only");
64 }
65
66 @Override
67 public SpdyHeaders add(String name, Iterable<?> values) {
68 throw new UnsupportedOperationException("read only");
69 }
70
71 @Override
72 public SpdyHeaders set(String name, Object value) {
73 throw new UnsupportedOperationException("read only");
74 }
75
76 @Override
77 public SpdyHeaders set(String name, Iterable<?> values) {
78 throw new UnsupportedOperationException("read only");
79 }
80
81 @Override
82 public SpdyHeaders remove(String name) {
83 throw new UnsupportedOperationException("read only");
84 }
85
86 @Override
87 public SpdyHeaders clear() {
88 throw new UnsupportedOperationException("read only");
89 }
90
91 @Override
92 public Iterator<Map.Entry<String, String>> iterator() {
93 return entries().iterator();
94 }
95
96 @Override
97 public String get(String name) {
98 return null;
99 }
100 };
101
102
103
104
105 public static final class HttpNames {
106
107
108
109 public static final String HOST = ":host";
110
111
112
113 public static final String METHOD = ":method";
114
115
116
117 public static final String PATH = ":path";
118
119
120
121 public static final String SCHEME = ":scheme";
122
123
124
125 public static final String STATUS = ":status";
126
127
128
129 public static final String VERSION = ":version";
130
131 private HttpNames() { }
132 }
133
134
135
136
137
138
139
140
141 public static String getHeader(SpdyHeadersFrame frame, String name) {
142 return frame.headers().get(name);
143 }
144
145
146
147
148
149
150
151
152
153 public static String getHeader(SpdyHeadersFrame frame, String name, String defaultValue) {
154 String value = frame.headers().get(name);
155 if (value == null) {
156 return defaultValue;
157 }
158 return value;
159 }
160
161
162
163
164
165 public static void setHeader(SpdyHeadersFrame frame, String name, Object value) {
166 frame.headers().set(name, value);
167 }
168
169
170
171
172
173 public static void setHeader(SpdyHeadersFrame frame, String name, Iterable<?> values) {
174 frame.headers().set(name, values);
175 }
176
177
178
179
180 public static void addHeader(SpdyHeadersFrame frame, String name, Object value) {
181 frame.headers().add(name, value);
182 }
183
184
185
186
187 public static void removeHost(SpdyHeadersFrame frame) {
188 frame.headers().remove(HttpNames.HOST);
189 }
190
191
192
193
194 public static String getHost(SpdyHeadersFrame frame) {
195 return frame.headers().get(HttpNames.HOST);
196 }
197
198
199
200
201 public static void setHost(SpdyHeadersFrame frame, String host) {
202 frame.headers().set(HttpNames.HOST, host);
203 }
204
205
206
207
208 public static void removeMethod(int spdyVersion, SpdyHeadersFrame frame) {
209 frame.headers().remove(HttpNames.METHOD);
210 }
211
212
213
214
215 public static HttpMethod getMethod(int spdyVersion, SpdyHeadersFrame frame) {
216 try {
217 return HttpMethod.valueOf(frame.headers().get(HttpNames.METHOD));
218 } catch (Exception e) {
219 return null;
220 }
221 }
222
223
224
225
226 public static void setMethod(int spdyVersion, SpdyHeadersFrame frame, HttpMethod method) {
227 frame.headers().set(HttpNames.METHOD, method.name());
228 }
229
230
231
232
233 public static void removeScheme(int spdyVersion, SpdyHeadersFrame frame) {
234 frame.headers().remove(HttpNames.SCHEME);
235 }
236
237
238
239
240 public static String getScheme(int spdyVersion, SpdyHeadersFrame frame) {
241 return frame.headers().get(HttpNames.SCHEME);
242 }
243
244
245
246
247 public static void setScheme(int spdyVersion, SpdyHeadersFrame frame, String scheme) {
248 frame.headers().set(HttpNames.SCHEME, scheme);
249 }
250
251
252
253
254 public static void removeStatus(int spdyVersion, SpdyHeadersFrame frame) {
255 frame.headers().remove(HttpNames.STATUS);
256 }
257
258
259
260
261 public static HttpResponseStatus getStatus(int spdyVersion, SpdyHeadersFrame frame) {
262 try {
263 String status = frame.headers().get(HttpNames.STATUS);
264 int space = status.indexOf(' ');
265 if (space == -1) {
266 return HttpResponseStatus.valueOf(Integer.parseInt(status));
267 } else {
268 int code = Integer.parseInt(status.substring(0, space));
269 String reasonPhrase = status.substring(space + 1);
270 HttpResponseStatus responseStatus = HttpResponseStatus.valueOf(code);
271 if (responseStatus.reasonPhrase().equals(reasonPhrase)) {
272 return responseStatus;
273 } else {
274 return new HttpResponseStatus(code, reasonPhrase);
275 }
276 }
277 } catch (Exception e) {
278 return null;
279 }
280 }
281
282
283
284
285 public static void setStatus(int spdyVersion, SpdyHeadersFrame frame, HttpResponseStatus status) {
286 frame.headers().set(HttpNames.STATUS, status.toString());
287 }
288
289
290
291
292 public static void removeUrl(int spdyVersion, SpdyHeadersFrame frame) {
293 frame.headers().remove(HttpNames.PATH);
294 }
295
296
297
298
299 public static String getUrl(int spdyVersion, SpdyHeadersFrame frame) {
300 return frame.headers().get(HttpNames.PATH);
301 }
302
303
304
305
306 public static void setUrl(int spdyVersion, SpdyHeadersFrame frame, String path) {
307 frame.headers().set(HttpNames.PATH, path);
308 }
309
310
311
312
313 public static void removeVersion(int spdyVersion, SpdyHeadersFrame frame) {
314 frame.headers().remove(HttpNames.VERSION);
315 }
316
317
318
319
320 public static HttpVersion getVersion(int spdyVersion, SpdyHeadersFrame frame) {
321 try {
322 return HttpVersion.valueOf(frame.headers().get(HttpNames.VERSION));
323 } catch (Exception e) {
324 return null;
325 }
326 }
327
328
329
330
331 public static void setVersion(int spdyVersion, SpdyHeadersFrame frame, HttpVersion httpVersion) {
332 frame.headers().set(HttpNames.VERSION, httpVersion.text());
333 }
334
335 @Override
336 public Iterator<Map.Entry<String, String>> iterator() {
337 return entries().iterator();
338 }
339
340
341
342
343
344
345
346
347 public abstract String get(String name);
348
349
350
351
352
353
354
355 public abstract List<String> getAll(String name);
356
357
358
359
360
361
362
363 public abstract List<Map.Entry<String, String>> entries();
364
365
366
367
368
369 public abstract boolean contains(String name);
370
371
372
373
374 public abstract Set<String> names();
375
376
377
378
379 public abstract SpdyHeaders add(String name, Object value);
380
381
382
383
384
385 public abstract SpdyHeaders add(String name, Iterable<?> values);
386
387
388
389
390
391 public abstract SpdyHeaders set(String name, Object value);
392
393
394
395
396
397 public abstract SpdyHeaders set(String name, Iterable<?> values);
398
399
400
401
402 public abstract SpdyHeaders remove(String name);
403
404
405
406
407 public abstract SpdyHeaders clear();
408
409
410
411
412 public abstract boolean isEmpty();
413 }