Skip to content

Commit

Permalink
WebSocketServerProtocolHandler: avoid double copy on handling websock…
Browse files Browse the repository at this point in the history
…et handshake result
  • Loading branch information
mostroverkhov committed Jul 2, 2024
1 parent 3b8ff36 commit 715c5a1
Showing 1 changed file with 8 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 715c5a1

Please sign in to comment.