Use Secure websockets if SslHandler is present in the ChannelPipeline. See #587

This commit is contained in:
Norman Maurer 2012-09-09 19:48:53 +02:00
parent bd557a6330
commit 4353aa6794
2 changed files with 15 additions and 3 deletions

View File

@ -34,6 +34,11 @@
<artifactId>netty-codec</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>${project.groupId}</groupId>
<artifactId>netty-handler</artifactId>
<version>${project.version}</version>
</dependency>
</dependencies>
</project>

View File

@ -23,10 +23,12 @@ import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundMessageHandlerAdapter;
import io.netty.channel.ChannelPipeline;
import io.netty.handler.codec.http.DefaultHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.ssl.SslHandler;
import io.netty.logging.InternalLogger;
import io.netty.logging.InternalLoggerFactory;
@ -55,7 +57,7 @@ class WebSocketServerHandshakeHandler extends ChannelInboundMessageHandlerAdapte
}
final WebSocketServerHandshakerFactory wsFactory = new WebSocketServerHandshakerFactory(
getWebSocketLocation(req, websocketPath), subprotocols, allowExtensions);
getWebSocketLocation(ctx.pipeline(), req, websocketPath), subprotocols, allowExtensions);
final WebSocketServerHandshaker handshaker = wsFactory.newHandshaker(req);
if (handshaker == null) {
wsFactory.sendUnsupportedWebSocketVersionResponse(ctx.channel());
@ -88,8 +90,13 @@ class WebSocketServerHandshakeHandler extends ChannelInboundMessageHandlerAdapte
}
}
private static String getWebSocketLocation(HttpRequest req, String path) {
return "ws://" + req.getHeader(HttpHeaders.Names.HOST) + path;
private static String getWebSocketLocation(ChannelPipeline cp, HttpRequest req, String path) {
String protocol = "ws";
if (cp.get(SslHandler.class) != null) {
// SSL in use so use Secure WebSockets
protocol = "wss";
}
return protocol + "://" + req.getHeader(HttpHeaders.Names.HOST) + path;
}
}