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 javax.net.ssl.SSLEngine;
19
20 import org.jboss.netty.channel.ChannelEvent;
21 import org.jboss.netty.channel.ChannelHandler;
22 import org.jboss.netty.channel.ChannelHandlerContext;
23 import org.jboss.netty.channel.ChannelPipeline;
24 import org.jboss.netty.channel.ChannelUpstreamHandler;
25 import org.jboss.netty.handler.codec.http.HttpChunkAggregator;
26 import org.jboss.netty.handler.codec.http.HttpRequest;
27 import org.jboss.netty.handler.codec.http.HttpRequestDecoder;
28 import org.jboss.netty.handler.codec.http.HttpResponseEncoder;
29 import org.jboss.netty.handler.ssl.SslHandler;
30
31
32
33
34
35
36
37 public abstract class SpdyOrHttpChooser implements ChannelUpstreamHandler {
38
39 public enum SelectedProtocol {
40 SpdyVersion2,
41 SpdyVersion3,
42 HttpVersion1_1,
43 HttpVersion1_0,
44 None
45 }
46
47 private final int maxSpdyContentLength;
48 private final int maxHttpContentLength;
49
50 protected SpdyOrHttpChooser(int maxSpdyContentLength, int maxHttpContentLength) {
51 this.maxSpdyContentLength = maxSpdyContentLength;
52 this.maxHttpContentLength = maxHttpContentLength;
53 }
54
55
56
57
58
59
60 protected abstract SelectedProtocol getProtocol(SSLEngine engine);
61
62 public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws Exception {
63
64 SslHandler handler = ctx.getPipeline().get(SslHandler.class);
65 if (handler == null) {
66
67 throw new IllegalStateException("SslHandler is needed for SPDY");
68 }
69
70 ChannelPipeline pipeline = ctx.getPipeline();
71 SelectedProtocol protocol = getProtocol(handler.getEngine());
72 switch (protocol) {
73 case None:
74
75 return;
76 case SpdyVersion2:
77 addSpdyHandlers(ctx, 2);
78 break;
79 case SpdyVersion3:
80 addSpdyHandlers(ctx, 3);
81 break;
82 case HttpVersion1_0:
83 case HttpVersion1_1:
84 addHttpHandlers(ctx);
85 break;
86 default:
87 throw new IllegalStateException("Unknown SelectedProtocol");
88 }
89
90
91 pipeline.remove(this);
92 ctx.sendUpstream(e);
93 }
94
95
96
97
98 protected void addSpdyHandlers(ChannelHandlerContext ctx, int version) {
99 ChannelPipeline pipeline = ctx.getPipeline();
100 pipeline.addLast("spdyDecoder", new SpdyFrameDecoder(version));
101 pipeline.addLast("spdyEncoder", new SpdyFrameEncoder(version));
102 pipeline.addLast("spdySessionHandler", new SpdySessionHandler(version, true));
103 pipeline.addLast("spdyHttpEncoder", new SpdyHttpEncoder(version));
104 pipeline.addLast("spdyHttpDecoder", new SpdyHttpDecoder(version, maxSpdyContentLength));
105 pipeline.addLast("spdyStreamIdHandler", new SpdyHttpResponseStreamIdHandler());
106 pipeline.addLast("httpRquestHandler", createHttpRequestHandlerForSpdy());
107 }
108
109
110
111
112 protected void addHttpHandlers(ChannelHandlerContext ctx) {
113 ChannelPipeline pipeline = ctx.getPipeline();
114 pipeline.addLast("httpRquestDecoder", new HttpRequestDecoder());
115 pipeline.addLast("httpResponseEncoder", new HttpResponseEncoder());
116 pipeline.addLast("httpChunkAggregator", new HttpChunkAggregator(maxHttpContentLength));
117 pipeline.addLast("httpRquestHandler", createHttpRequestHandlerForHttp());
118 }
119
120
121
122
123
124
125 protected abstract ChannelUpstreamHandler createHttpRequestHandlerForHttp();
126
127
128
129
130
131
132
133
134
135 protected ChannelUpstreamHandler createHttpRequestHandlerForSpdy() {
136 return createHttpRequestHandlerForHttp();
137 }
138 }