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.channel.ChannelHandlerContext;
19 import io.netty.handler.codec.MessageToMessageCodec;
20 import io.netty.handler.codec.http.HttpMessage;
21 import io.netty.util.ReferenceCountUtil;
22
23 import java.util.LinkedList;
24 import java.util.List;
25 import java.util.Queue;
26
27
28
29
30
31
32 public class SpdyHttpResponseStreamIdHandler extends
33 MessageToMessageCodec<Object, HttpMessage> {
34 private static final Integer NO_ID = -1;
35 private final Queue<Integer> ids = new LinkedList<Integer>();
36
37 @Override
38 public boolean acceptInboundMessage(Object msg) throws Exception {
39 return msg instanceof HttpMessage || msg instanceof SpdyRstStreamFrame;
40 }
41
42 @Override
43 protected void encode(ChannelHandlerContext ctx, HttpMessage msg, List<Object> out) throws Exception {
44 Integer id = ids.poll();
45 if (id != null && id.intValue() != NO_ID && !msg.headers().contains(SpdyHttpHeaders.Names.STREAM_ID)) {
46 SpdyHttpHeaders.setStreamId(msg, id);
47 }
48
49 out.add(ReferenceCountUtil.retain(msg));
50 }
51
52 @Override
53 protected void decode(ChannelHandlerContext ctx, Object msg, List<Object> out) throws Exception {
54 if (msg instanceof HttpMessage) {
55 boolean contains = ((HttpMessage) msg).headers().contains(SpdyHttpHeaders.Names.STREAM_ID);
56 if (!contains) {
57 ids.add(NO_ID);
58 } else {
59 ids.add(SpdyHttpHeaders.getStreamId((HttpMessage) msg));
60 }
61 } else if (msg instanceof SpdyRstStreamFrame) {
62 ids.remove(((SpdyRstStreamFrame) msg).streamId());
63 }
64
65 out.add(ReferenceCountUtil.retain(msg));
66 }
67 }