diff --git a/netty-websocket-http1/src/main/java/com/jauntsdn/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java b/netty-websocket-http1/src/main/java/com/jauntsdn/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java index 49d0c76..40f7adc 100644 --- a/netty-websocket-http1/src/main/java/com/jauntsdn/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java +++ b/netty-websocket-http1/src/main/java/com/jauntsdn/netty/handler/codec/http/websocketx/WebSocketServerProtocolHandler.java @@ -20,6 +20,8 @@ import static io.netty.handler.codec.http.HttpResponseStatus.BAD_REQUEST; import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; +import io.netty.buffer.ByteBuf; +import io.netty.buffer.ByteBufUtil; import io.netty.buffer.Unpooled; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -182,11 +184,13 @@ private void handleHandshakeResult( if (cause != null) { handshake.tryFailure(cause); if (cause instanceof WebSocketHandshakeException) { + String errorMessage = cause.getMessage(); + ByteBuf errorContent = + errorMessage == null || errorMessage.isEmpty() + ? Unpooled.EMPTY_BUFFER + : ByteBufUtil.writeUtf8(ctx.alloc(), errorMessage); FullHttpResponse response = - new DefaultFullHttpResponse( - HTTP_1_1, - HttpResponseStatus.BAD_REQUEST, - Unpooled.wrappedBuffer(cause.getMessage().getBytes())); + new DefaultFullHttpResponse(HTTP_1_1, HttpResponseStatus.BAD_REQUEST, errorContent); ctx.channel().writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } else { ctx.fireExceptionCaught(cause);