Skip to content

Commit

Permalink
Move generic code to HttpOrSpdyChooser to simplify implementations
Browse files Browse the repository at this point in the history
Motivation:

HttpOrSpdyChooser can be simplified so the user not need to implement getProtocol(...) method.

Modification:

Add implementation for the method. The user can override it if necessary.

Result:

Easier usage of HttpOrSpdyChooser.
  • Loading branch information
Norman Maurer committed Jul 7, 2014
1 parent 93a265e commit 696e355
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpResponseEncoder;
import io.netty.handler.ssl.SslHandler;
import io.netty.util.internal.StringUtil;

import javax.net.ssl.SSLEngine;
import java.util.List;
Expand Down Expand Up @@ -82,7 +83,15 @@ protected SpdyOrHttpChooser(int maxSpdyContentLength, int maxHttpContentLength)
* {@link SelectedProtocol#UNKNOWN}.
*
*/
protected abstract SelectedProtocol getProtocol(SSLEngine engine);
protected SelectedProtocol getProtocol(SSLEngine engine) {
String[] protocol = StringUtil.split(engine.getSession().getProtocol(), ':');
if (protocol.length < 2) {
// Use HTTP/1.1 as default
return SelectedProtocol.HTTP_1_1;
}
SelectedProtocol selectedProtocol = SelectedProtocol.protocol(protocol[1]);
return selectedProtocol;
}

@Override
protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
import io.netty.channel.ChannelHandler;
import io.netty.handler.codec.spdy.SpdyOrHttpChooser;

import javax.net.ssl.SSLEngine;

/**
* Negotiates with the browser if SPDY or HTTP is going to be used. Once decided, the Netty pipeline is setup with
* the correct handlers for the selected protocol.
Expand All @@ -36,14 +34,6 @@ public SpdyOrHttpHandler(int maxSpdyContentLength, int maxHttpContentLength) {
super(maxSpdyContentLength, maxHttpContentLength);
}

@Override
protected SelectedProtocol getProtocol(SSLEngine engine) {
String[] protocol = engine.getSession().getProtocol().split(":");
SelectedProtocol selectedProtocol = SelectedProtocol.protocol(protocol[1]);
System.err.println("Selected Protocol is " + selectedProtocol);
return selectedProtocol;
}

@Override
protected ChannelHandler createHttpRequestHandlerForHttp() {
return new SpdyServerHandler();
Expand Down

0 comments on commit 696e355

Please sign in to comment.