Skip to content

Commit

Permalink
Always decode in an event loop (#3369)
Browse files Browse the repository at this point in the history
Fixes #3366
  • Loading branch information
violetagg authored Jul 25, 2024
1 parent bbf0131 commit 0a80819
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -602,13 +602,10 @@ public HttpServerResponse compression(boolean compress) {
if (!compress) {
removeHandler(NettyPipeline.CompressionHandler);
}
else if (channel().pipeline()
.get(NettyPipeline.CompressionHandler) == null) {
else if (channel().pipeline().get(NettyPipeline.CompressionHandler) == null) {
SimpleCompressionHandler handler = new SimpleCompressionHandler();
handler.request = nettyRequest;
try {
//Do not invoke handler.channelRead as it will trigger ctx.fireChannelRead
handler.decode(channel().pipeline().context(NettyPipeline.ReactiveBridge), nettyRequest);

addHandlerFirst(NettyPipeline.CompressionHandler, handler);
}
catch (Throwable e) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2023 VMware, Inc. or its affiliates, All Rights Reserved.
* Copyright (c) 2018-2024 VMware, Inc. or its affiliates, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand All @@ -25,6 +25,8 @@
import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.HttpContentCompressor;
import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse;
import io.netty.handler.codec.http.LastHttpContent;
import io.netty.util.ReferenceCountUtil;

import java.util.ArrayList;
Expand All @@ -37,10 +39,19 @@
*/
final class SimpleCompressionHandler extends HttpContentCompressor {

boolean decoded;
HttpRequest request;

SimpleCompressionHandler() {
super((CompressionOptions[]) null);
}

@Override
protected void decode(ChannelHandlerContext ctx, HttpRequest msg, List<Object> out) throws Exception {
decoded = true;
super.decode(ctx, msg, out);
}

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
throws Exception {
Expand All @@ -49,6 +60,13 @@ public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise)
super.write(ctx, new DefaultHttpContent((ByteBuf) msg), promise);
}
else {
if (!decoded && msg instanceof HttpResponse) {
decode(ctx, request);
}
if (decoded && request != null && msg instanceof LastHttpContent) {
decoded = false;
request = null;
}
super.write(ctx, msg, promise);
}
}
Expand All @@ -63,7 +81,7 @@ void decode(ChannelHandlerContext ctx, HttpRequest msg) {
// decode(...) will observe a freed content
request = new DefaultHttpRequest(msg.protocolVersion(), msg.method(), msg.uri(), msg.headers());
}
super.decode(ctx, request, out);
decode(ctx, request, out);
}
catch (DecoderException e) {
throw e;
Expand Down

0 comments on commit 0a80819

Please sign in to comment.