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.channel.ChannelHandlerContext;
19 import org.jboss.netty.channel.MessageEvent;
20 import org.jboss.netty.channel.SimpleChannelHandler;
21 import org.jboss.netty.handler.codec.http.HttpMessage;
22 import org.jboss.netty.handler.codec.http.HttpResponse;
23
24 import java.util.Queue;
25 import java.util.concurrent.ConcurrentLinkedQueue;
26
27
28
29
30
31
32 public class SpdyHttpResponseStreamIdHandler extends SimpleChannelHandler {
33 private static final Integer NO_ID = -1;
34 private final Queue<Integer> ids = new ConcurrentLinkedQueue<Integer>();
35
36 @Override
37 public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
38 if (e.getMessage() instanceof HttpMessage) {
39 boolean contains = ((HttpMessage) e.getMessage()).headers().contains(SpdyHttpHeaders.Names.STREAM_ID);
40 if (!contains) {
41 ids.add(NO_ID);
42 } else {
43 ids.add(SpdyHttpHeaders.getStreamId((HttpMessage) e.getMessage()));
44 }
45 } else if (e.getMessage() instanceof SpdyRstStreamFrame) {
46
47 ids.remove(((SpdyRstStreamFrame) e.getMessage()).getStreamId());
48 }
49 super.messageReceived(ctx, e);
50 }
51
52 @Override
53 public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws Exception {
54 if (e.getMessage() instanceof HttpResponse) {
55 HttpResponse response = (HttpResponse) e.getMessage();
56 Integer id = ids.poll();
57 if (id != null && id.intValue() != NO_ID && !response.headers().contains(SpdyHttpHeaders.Names.STREAM_ID)) {
58 SpdyHttpHeaders.setStreamId(response, id);
59 }
60 }
61 super.writeRequested(ctx, e);
62 }
63
64 }