diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java index bc0680bea7..038e180259 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientCodec.java @@ -21,6 +21,7 @@ import io.netty.channel.ChannelHandlerContext; import io.netty.channel.ChannelPipeline; import io.netty.channel.CombinedChannelDuplexHandler; import io.netty.handler.codec.PrematureChannelClosureException; +import io.netty.util.ReferenceCountUtil; import java.util.ArrayDeque; import java.util.List; @@ -87,6 +88,14 @@ public final class HttpClientCodec extends CombinedChannelDuplexHandler out) throws Exception { + + if (upgraded) { + out.add(ReferenceCountUtil.retain(msg)); + return; + } + if (msg instanceof HttpRequest && !done) { queue.offer(((HttpRequest) msg).method()); } diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientUpgradeHandler.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientUpgradeHandler.java index 1d4d74bb12..9fdf4afb85 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientUpgradeHandler.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpClientUpgradeHandler.java @@ -62,6 +62,13 @@ public class HttpClientUpgradeHandler extends HttpObjectAggregator implements Ch * The source codec that is used in the pipeline initially. */ public interface SourceCodec { + + /** + * Removes or disables the encoder of this codec so that the {@link UpgradeCodec} can send an initial greeting + * (if any). + */ + void prepareUpgradeFrom(ChannelHandlerContext ctx); + /** * Removes this codec (i.e. all associated handlers) from the pipeline. */ @@ -222,8 +229,9 @@ public class HttpClientUpgradeHandler extends HttpObjectAggregator implements Ch } // Upgrade to the new protocol. - sourceCodec.upgradeFrom(ctx); + sourceCodec.prepareUpgradeFrom(ctx); upgradeCodec.upgradeTo(ctx, response); + sourceCodec.upgradeFrom(ctx); // Notify that the upgrade to the new protocol completed successfully. ctx.fireUserEventTriggered(UpgradeEvent.UPGRADE_SUCCESSFUL); diff --git a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java index 134b1a05f9..28c1804bfd 100644 --- a/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java +++ b/codec-http2/src/main/java/io/netty/handler/codec/http2/Http2ConnectionHandler.java @@ -47,7 +47,6 @@ import static io.netty.handler.codec.http2.Http2Stream.State.IDLE; import static io.netty.util.CharsetUtil.UTF_8; import static io.netty.util.internal.ObjectUtil.checkNotNull; import static java.lang.Math.min; -import static java.lang.String.format; import static java.util.concurrent.TimeUnit.MILLISECONDS; /** @@ -724,20 +723,17 @@ public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http if (future.isSuccess()) { if (errorCode != NO_ERROR.code()) { if (logger.isDebugEnabled()) { - logger.debug( - format("Sent GOAWAY: lastStreamId '%d', errorCode '%d', " + - "debugData '%s'. Forcing shutdown of the connection.", - lastStreamId, errorCode, debugData.toString(UTF_8)), - future.cause()); + logger.debug("{} Sent GOAWAY: lastStreamId '{}', errorCode '{}', " + + "debugData '{}'. Forcing shutdown of the connection.", + ctx.channel(), lastStreamId, errorCode, debugData.toString(UTF_8), future.cause()); } ctx.close(); } } else { if (logger.isErrorEnabled()) { - logger.error( - format("Sending GOAWAY failed: lastStreamId '%d', errorCode '%d', " + - "debugData '%s'. Forcing shutdown of the connection.", - lastStreamId, errorCode, debugData.toString(UTF_8)), future.cause()); + logger.error("{} Sending GOAWAY failed: lastStreamId '{}', errorCode '{}', " + + "debugData '{}'. Forcing shutdown of the connection.", + ctx.channel(), lastStreamId, errorCode, debugData.toString(UTF_8), future.cause()); } ctx.close(); }