From 90d15c46272f4b83a35c2640ce1169a5fb8ff024 Mon Sep 17 00:00:00 2001 From: Jeff Smick Date: Sat, 8 Sep 2012 20:43:06 -0700 Subject: [PATCH] Upgrade connection after completed response Channel handlers above the HttpEncoder may delay the repsonse being written to the socket. We need to wait for the response to complete before upgrading the pipeline. --- .../WebSocketServerHandshaker00.java | 8 +++++++- .../WebSocketServerHandshaker08.java | 18 +++++++++++------- .../WebSocketServerHandshaker13.java | 18 +++++++++++------- 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java index d76fa6a995..0c85265989 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker00.java @@ -22,6 +22,7 @@ import io.netty.buffer.ByteBuf; import io.netty.buffer.Unpooled; import io.netty.channel.Channel; import io.netty.channel.ChannelFuture; +import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelPipeline; import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.HttpChunkAggregator; @@ -180,7 +181,12 @@ public class WebSocketServerHandshaker00 extends WebSocketServerHandshaker { ChannelFuture future = channel.write(res); - p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket00FrameEncoder()); + future.addListener(new ChannelFutureListener() { + public void operationComplete(ChannelFuture future) { + ChannelPipeline p = future.channel().pipeline(); + p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket00FrameEncoder()); + } + }); return future; } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java index f4be1b93a0..c56fa98028 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker08.java @@ -147,14 +147,18 @@ public class WebSocketServerHandshaker08 extends WebSocketServerHandshaker { ChannelFuture future = channel.write(res); // Upgrade the connection and send the handshake response. - ChannelPipeline p = channel.pipeline(); - if (p.get(HttpChunkAggregator.class) != null) { - p.remove(HttpChunkAggregator.class); - } + future.addListener(new ChannelFutureListener() { + public void operationComplete(ChannelFuture future) { + ChannelPipeline p = future.channel().pipeline(); + if (p.get(HttpChunkAggregator.class) != null) { + p.remove(HttpChunkAggregator.class); + } - p.replace(HttpRequestDecoder.class, "wsdecoder", - new WebSocket08FrameDecoder(true, allowExtensions, getMaxFramePayloadLength())); - p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket08FrameEncoder(false)); + p.replace(HttpRequestDecoder.class, "wsdecoder", + new WebSocket08FrameDecoder(true, allowExtensions, getMaxFramePayloadLength())); + p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket08FrameEncoder(false)); + } + }); return future; } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java index 95737bd23e..353bba0e09 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/websocketx/WebSocketServerHandshaker13.java @@ -147,14 +147,18 @@ public class WebSocketServerHandshaker13 extends WebSocketServerHandshaker { ChannelFuture future = channel.write(res); // Upgrade the connection and send the handshake response. - ChannelPipeline p = channel.pipeline(); - if (p.get(HttpChunkAggregator.class) != null) { - p.remove(HttpChunkAggregator.class); - } + future.addListener(new ChannelFutureListener() { + public void operationComplete(ChannelFuture future) { + ChannelPipeline p = future.channel().pipeline(); + if (p.get(HttpChunkAggregator.class) != null) { + p.remove(HttpChunkAggregator.class); + } - p.replace(HttpRequestDecoder.class, "wsdecoder", - new WebSocket13FrameDecoder(true, allowExtensions, getMaxFramePayloadLength())); - p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket13FrameEncoder(false)); + p.replace(HttpRequestDecoder.class, "wsdecoder", + new WebSocket13FrameDecoder(true, allowExtensions, getMaxFramePayloadLength())); + p.replace(HttpResponseEncoder.class, "wsencoder", new WebSocket13FrameEncoder(false)); + } + }); return future; }