Skip to content
This repository has been archived by the owner on May 22, 2019. It is now read-only.

Commit

Permalink
Make remaining changes for moving to Netty 4.1
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthias247 committed May 17, 2017
1 parent e98c2f3 commit 12d8f22
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 23 deletions.
5 changes: 5 additions & 0 deletions jawampa-netty/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,10 @@
<artifactId>netty-codec-http</artifactId>
<version>4.1.11.Final</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-handler</artifactId>
<version>4.1.11.Final</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@
import io.netty.handler.codec.http.websocketx.WebSocketFrameAggregator;
import io.netty.handler.codec.http.websocketx.WebSocketVersion;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;

/**
Expand Down Expand Up @@ -109,7 +110,10 @@ public IWampConnector createConnector(final URI uri,
if (needSsl && (nettyConfig == null || nettyConfig.sslContext() == null)) {
// Create a default SslContext when we got none provided through the constructor
try {
sslCtx0 = SslContext.newClientContext(InsecureTrustManagerFactory.INSTANCE);
sslCtx0 =
SslContextBuilder.forClient()
.trustManager(InsecureTrustManagerFactory.INSTANCE)
.build();
}
catch (SSLException e) {
throw e;
Expand Down Expand Up @@ -175,7 +179,6 @@ public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws E
// The transport closed before the websocket handshake was completed
connectListener.connectFailed(cause);
}
super.exceptionCaught(ctx, cause);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,14 @@
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.ssl.SslContext;
import io.netty.handler.ssl.SslContextBuilder;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.util.CharsetUtil;
import static io.netty.handler.codec.http.HttpHeaders.Names.*;
import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static io.netty.handler.codec.http.HttpMethod.*;
import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpVersion.*;
Expand Down Expand Up @@ -119,7 +120,10 @@ public void start() {
if (uri.getScheme().equalsIgnoreCase("wss") && sslCtx == null) {
// Use a self signed certificate when we got none provided through the constructor
SelfSignedCertificate ssc = new SelfSignedCertificate();
sslCtx = SslContext.newServerContext(ssc.certificate(), ssc.privateKey());
sslCtx =
SslContextBuilder
.forServer(ssc.certificate(), ssc.privateKey())
.build();
}

// Use well-known ports if not explicitly specified
Expand Down Expand Up @@ -199,17 +203,17 @@ public void channelRead0(ChannelHandlerContext ctx, FullHttpRequest msg) {

private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
// Handle a bad request.
if (!req.getDecoderResult().isSuccess()) {
if (!req.decoderResult().isSuccess()) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST));
return;
}
// Allow only GET methods.
if (req.getMethod() != GET) {
if (req.method() != GET) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN));
return;
}
// Send the demo page and favicon.ico
if ("/".equals(req.getUri())) {
if ("/".equals(req.uri())) {
ByteBuf content = Unpooled.copiedBuffer(
"<html><head><title>Wamp Router</title></head><body>" +
"<h1>This server provides a wamp router on path " +
Expand All @@ -218,11 +222,11 @@ private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
, CharsetUtil.UTF_8);
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content);
res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
HttpHeaders.setContentLength(res, content.readableBytes());
HttpUtil.setContentLength(res, content.readableBytes());
sendHttpResponse(ctx, req, res);
return;
}
if ("/favicon.ico".equals(req.getUri())) {
if ("/favicon.ico".equals(req.uri())) {
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND);
sendHttpResponse(ctx, req, res);
return;
Expand All @@ -235,15 +239,15 @@ private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
private static void sendHttpResponse(
ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200).
if (res.getStatus().code() != 200) {
ByteBuf buf = Unpooled.copiedBuffer(res.getStatus().toString(), CharsetUtil.UTF_8);
if (res.status().code() != 200) {
ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8);
res.content().writeBytes(buf);
buf.release();
HttpHeaders.setContentLength(res, res.content().readableBytes());
HttpUtil.setContentLength(res, res.content().readableBytes());
}
// Send the response and close the connection if necessary.
ChannelFuture f = ctx.channel().writeAndFlush(res);
if (!HttpHeaders.isKeepAlive(req) || res.getStatus().code() != 200) {
if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) {
f.addListener(ChannelFutureListener.CLOSE);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpHeaderValues;
import io.netty.handler.codec.http.HttpHeaders;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
Expand All @@ -37,8 +39,8 @@
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
import io.netty.handler.ssl.SslHandler;
import io.netty.util.AsciiString;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.StringUtil;
import ws.wamp.jawampa.WampSerialization;
import ws.wamp.jawampa.WampMessages.WampMessage;
import ws.wamp.jawampa.connection.IWampConnection;
Expand All @@ -48,7 +50,7 @@

import java.util.List;

import static io.netty.handler.codec.http.HttpHeaders.Names.*;
import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static io.netty.handler.codec.http.HttpVersion.*;

/**
Expand Down Expand Up @@ -101,18 +103,23 @@ public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception
}

private boolean isUpgradeRequest(FullHttpRequest request) {
if (!request.getDecoderResult().isSuccess()) {
if (!request.decoderResult().isSuccess()) {
return false;
}

String connectionHeaderValue = request.headers().get(HttpHeaders.Names.CONNECTION);
CharSequence connectionHeaderValue =
request.headers().get(HttpHeaderNames.CONNECTION);
if (connectionHeaderValue == null) {
return false;
}
String[] connectionHeaderFields = connectionHeaderValue.toLowerCase().split(",");
AsciiString connectionHeaderString =
new AsciiString(connectionHeaderValue);
AsciiString[] connectionHeaderFields =
connectionHeaderString.toLowerCase().split(',');
boolean hasUpgradeField = false;
for (String s : connectionHeaderFields) {
if (s.trim().equals(HttpHeaders.Values.UPGRADE.toLowerCase())) {
AsciiString upgradeValue = HttpHeaderValues.UPGRADE.toLowerCase();
for (AsciiString s : connectionHeaderFields) {
if (upgradeValue.equals(s.trim())) {
hasUpgradeField = true;
break;
}
Expand All @@ -121,11 +128,12 @@ private boolean isUpgradeRequest(FullHttpRequest request) {
return false;
}

if (!request.headers().contains(HttpHeaders.Names.UPGRADE, HttpHeaders.Values.WEBSOCKET, true)){
if (!request.headers().contains(
HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET, true)) {
return false;
}

return request.getUri().equals(websocketPath);
return request.uri().equals(websocketPath);
}

// All methods inside the connection will be called from the WampRouters thread
Expand Down

0 comments on commit 12d8f22

Please sign in to comment.