Fix websocket examples (#9757)

Motivation:

After fix #9377 some websocket examples work incorrect

Modification:

Replace `Unpooled.EMPTY_BUFFER` to `ctx.alloc().buffer(0)` for responses with possible content

Result:

Examples work
This commit is contained in:
Andrey Mizurov 2019-11-06 11:44:29 +03:00 committed by Norman Maurer
parent adc4a6170d
commit 042b51ae52
3 changed files with 45 additions and 65 deletions

View File

@ -16,7 +16,8 @@
package io.netty.example.http.websocketx.benchmarkserver; package io.netty.example.http.websocketx.benchmarkserver;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
@ -24,6 +25,7 @@ import io.netty.handler.codec.http.DefaultFullHttpResponse;
import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil; import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame; import io.netty.handler.codec.http.websocketx.BinaryWebSocketFrame;
import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame; import io.netty.handler.codec.http.websocketx.CloseWebSocketFrame;
@ -33,18 +35,9 @@ import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker; import io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker;
import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory; import io.netty.handler.codec.http.websocketx.WebSocketServerHandshakerFactory;
import io.netty.util.CharsetUtil;
import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; import static io.netty.handler.codec.http.HttpMethod.*;
import static io.netty.handler.codec.http.HttpHeaderValues.CLOSE; import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
import static io.netty.handler.codec.http.HttpMethod.GET;
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_0;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
/** /**
* Handles handshakes and messages * Handles handshakes and messages
@ -72,20 +65,22 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) { private void handleHttpRequest(ChannelHandlerContext ctx, FullHttpRequest req) {
// Handle a bad request. // Handle a bad request.
if (!req.decoderResult().isSuccess()) { if (!req.decoderResult().isSuccess()) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST, Unpooled.EMPTY_BUFFER)); sendHttpResponse(ctx, req, new DefaultFullHttpResponse(req.protocolVersion(), BAD_REQUEST,
ctx.alloc().buffer(0)));
return; return;
} }
// Allow only GET methods. // Allow only GET methods.
if (!GET.equals(req.method())) { if (!GET.equals(req.method())) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN, Unpooled.EMPTY_BUFFER)); sendHttpResponse(ctx, req, new DefaultFullHttpResponse(req.protocolVersion(), FORBIDDEN,
ctx.alloc().buffer(0)));
return; return;
} }
// Send the demo page and favicon.ico // Send the demo page and favicon.ico
if ("/".equals(req.uri())) { if ("/".equals(req.uri())) {
ByteBuf content = WebSocketServerBenchmarkPage.getContent(getWebSocketLocation(req)); ByteBuf content = WebSocketServerBenchmarkPage.getContent(getWebSocketLocation(req));
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); FullHttpResponse res = new DefaultFullHttpResponse(req.protocolVersion(), OK, content);
res.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8"); res.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
HttpUtil.setContentLength(res, content.readableBytes()); HttpUtil.setContentLength(res, content.readableBytes());
@ -93,8 +88,10 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
sendHttpResponse(ctx, req, res); sendHttpResponse(ctx, req, res);
return; return;
} }
if ("/favicon.ico".equals(req.uri())) { if ("/favicon.ico".equals(req.uri())) {
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND, Unpooled.EMPTY_BUFFER); FullHttpResponse res = new DefaultFullHttpResponse(req.protocolVersion(), NOT_FOUND,
ctx.alloc().buffer(0));
sendHttpResponse(ctx, req, res); sendHttpResponse(ctx, req, res);
return; return;
} }
@ -132,26 +129,19 @@ public class WebSocketServerHandler extends SimpleChannelInboundHandler<Object>
} }
} }
private static void sendHttpResponse( private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200). // Generate an error page if response getStatus code is not OK (200).
if (res.status().code() != 200) { HttpResponseStatus responseStatus = res.status();
ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); if (responseStatus.code() != 200) {
res.content().writeBytes(buf); ByteBufUtil.writeUtf8(res.content(), responseStatus.toString());
buf.release();
HttpUtil.setContentLength(res, res.content().readableBytes()); HttpUtil.setContentLength(res, res.content().readableBytes());
} }
// Send the response and close the connection if necessary. // Send the response and close the connection if necessary.
if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { boolean keepAlive = HttpUtil.isKeepAlive(req) && responseStatus.code() == 200;
// Tell the client we're going to close the connection. HttpUtil.setKeepAlive(res, keepAlive);
res.headers().set(CONNECTION, CLOSE); ChannelFuture future = ctx.write(res); // Flushed in channelReadComplete()
ctx.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE); if (!keepAlive) {
} else { future.addListener(ChannelFutureListener.CLOSE);
if (req.protocolVersion().equals(HTTP_1_0)) {
res.headers().set(CONNECTION, KEEP_ALIVE);
}
ctx.writeAndFlush(res);
} }
} }

View File

@ -15,13 +15,13 @@
*/ */
package io.netty.example.http.websocketx.server; package io.netty.example.http.websocketx.server;
import java.util.Locale;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler; import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame; import io.netty.handler.codec.http.websocketx.TextWebSocketFrame;
import io.netty.handler.codec.http.websocketx.WebSocketFrame; import io.netty.handler.codec.http.websocketx.WebSocketFrame;
import java.util.Locale;
/** /**
* Echoes uppercase content of text frames. * Echoes uppercase content of text frames.
*/ */

View File

@ -16,7 +16,8 @@
package io.netty.example.http.websocketx.server; package io.netty.example.http.websocketx.server;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.ByteBufUtil;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelPipeline; import io.netty.channel.ChannelPipeline;
@ -26,21 +27,13 @@ import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaderNames;
import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponseStatus;
import io.netty.handler.codec.http.HttpUtil; import io.netty.handler.codec.http.HttpUtil;
import io.netty.handler.ssl.SslHandler; import io.netty.handler.ssl.SslHandler;
import io.netty.util.CharsetUtil;
import static io.netty.handler.codec.http.HttpHeaderNames.CONTENT_TYPE; import static io.netty.handler.codec.http.HttpHeaderNames.*;
import static io.netty.handler.codec.http.HttpHeaderNames.CONNECTION; import static io.netty.handler.codec.http.HttpMethod.*;
import static io.netty.handler.codec.http.HttpHeaderValues.CLOSE; import static io.netty.handler.codec.http.HttpResponseStatus.*;
import static io.netty.handler.codec.http.HttpHeaderValues.KEEP_ALIVE;
import static io.netty.handler.codec.http.HttpMethod.GET;
import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST;
import static io.netty.handler.codec.http.HttpResponseStatus.FORBIDDEN;
import static io.netty.handler.codec.http.HttpResponseStatus.NOT_FOUND;
import static io.netty.handler.codec.http.HttpResponseStatus.OK;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_0;
import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1;
/** /**
* Outputs index page content. * Outputs index page content.
@ -57,13 +50,15 @@ public class WebSocketIndexPageHandler extends SimpleChannelInboundHandler<FullH
protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception { protected void channelRead0(ChannelHandlerContext ctx, FullHttpRequest req) throws Exception {
// Handle a bad request. // Handle a bad request.
if (!req.decoderResult().isSuccess()) { if (!req.decoderResult().isSuccess()) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, BAD_REQUEST, Unpooled.EMPTY_BUFFER)); sendHttpResponse(ctx, req, new DefaultFullHttpResponse(req.protocolVersion(), BAD_REQUEST,
ctx.alloc().buffer(0)));
return; return;
} }
// Allow only GET methods. // Allow only GET methods.
if (!GET.equals(req.method())) { if (!GET.equals(req.method())) {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, FORBIDDEN, Unpooled.EMPTY_BUFFER)); sendHttpResponse(ctx, req, new DefaultFullHttpResponse(req.protocolVersion(), FORBIDDEN,
ctx.alloc().buffer(0)));
return; return;
} }
@ -71,14 +66,15 @@ public class WebSocketIndexPageHandler extends SimpleChannelInboundHandler<FullH
if ("/".equals(req.uri()) || "/index.html".equals(req.uri())) { if ("/".equals(req.uri()) || "/index.html".equals(req.uri())) {
String webSocketLocation = getWebSocketLocation(ctx.pipeline(), req, websocketPath); String webSocketLocation = getWebSocketLocation(ctx.pipeline(), req, websocketPath);
ByteBuf content = WebSocketServerIndexPage.getContent(webSocketLocation); ByteBuf content = WebSocketServerIndexPage.getContent(webSocketLocation);
FullHttpResponse res = new DefaultFullHttpResponse(HTTP_1_1, OK, content); FullHttpResponse res = new DefaultFullHttpResponse(req.protocolVersion(), OK, content);
res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8"); res.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
HttpUtil.setContentLength(res, content.readableBytes()); HttpUtil.setContentLength(res, content.readableBytes());
sendHttpResponse(ctx, req, res); sendHttpResponse(ctx, req, res);
} else { } else {
sendHttpResponse(ctx, req, new DefaultFullHttpResponse(HTTP_1_1, NOT_FOUND, Unpooled.EMPTY_BUFFER)); sendHttpResponse(ctx, req, new DefaultFullHttpResponse(req.protocolVersion(), NOT_FOUND,
ctx.alloc().buffer(0)));
} }
} }
@ -90,23 +86,17 @@ public class WebSocketIndexPageHandler extends SimpleChannelInboundHandler<FullH
private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) { private static void sendHttpResponse(ChannelHandlerContext ctx, FullHttpRequest req, FullHttpResponse res) {
// Generate an error page if response getStatus code is not OK (200). // Generate an error page if response getStatus code is not OK (200).
if (res.status().code() != 200) { HttpResponseStatus responseStatus = res.status();
ByteBuf buf = Unpooled.copiedBuffer(res.status().toString(), CharsetUtil.UTF_8); if (responseStatus.code() != 200) {
res.content().writeBytes(buf); ByteBufUtil.writeUtf8(res.content(), responseStatus.toString());
buf.release();
HttpUtil.setContentLength(res, res.content().readableBytes()); HttpUtil.setContentLength(res, res.content().readableBytes());
} }
// Send the response and close the connection if necessary. // Send the response and close the connection if necessary.
if (!HttpUtil.isKeepAlive(req) || res.status().code() != 200) { boolean keepAlive = HttpUtil.isKeepAlive(req) && responseStatus.code() == 200;
// Tell the client we're going to close the connection. HttpUtil.setKeepAlive(res, keepAlive);
res.headers().set(CONNECTION, CLOSE); ChannelFuture future = ctx.writeAndFlush(res);
ctx.writeAndFlush(res).addListener(ChannelFutureListener.CLOSE); if (!keepAlive) {
} else { future.addListener(ChannelFutureListener.CLOSE);
if (req.protocolVersion().equals(HTTP_1_0)) {
res.headers().set(CONNECTION, KEEP_ALIVE);
}
ctx.writeAndFlush(res);
} }
} }