Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Do not fail when compressing empty HttpContent #13655

Merged
merged 2 commits into from
Oct 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufHolder;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.DecoderResult;
Expand Down Expand Up @@ -208,6 +209,9 @@ protected void encode(ChannelHandlerContext ctx, HttpObject msg, List<Object> ou
ensureContent(msg);
if (encodeContent((HttpContent) msg, out)) {
state = State.AWAIT_HEADERS;
} else if (out.isEmpty()) {
// MessageToMessageCodec needs at least one output message
out.add(new DefaultHttpContent(Unpooled.EMPTY_BUFFER));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should better move this to encodeContent WDYT ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@yawkat wdyt ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sorry didn't have time to try this yesterday.

I think it makes more sense to have this in encode. It's only necessary if no further items are added after the call to encodeContent, and in encode, it's more clear that this is the case. In both branches where encodeContent is called (AWAIT_CONTENT and AWAIT_HEADERS + FullHttpResponse) it turns out that encodeContent is only called once and is the last one to add to out as well, so moving the patch would work, but imo this is much harder to see so the check makes more sense in encode.

}
break;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,20 @@ public void testMultipleAcceptEncodingHeaders() {
assertTrue(ch.finishAndReleaseAll());
}

@Test
public void testEmpty() {
EmbeddedChannel ch = new EmbeddedChannel(new HttpContentCompressor());
assertTrue(ch.writeInbound(newRequest()));

DefaultHttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
response.headers().add(HttpHeaderNames.CONTENT_LENGTH, 0);
assertTrue(ch.writeOutbound(response));
assertTrue(ch.writeOutbound(new DefaultHttpContent(Unpooled.EMPTY_BUFFER)));
assertTrue(ch.writeOutbound(DefaultLastHttpContent.EMPTY_LAST_CONTENT));

ch.checkException();
yawkat marked this conversation as resolved.
Show resolved Hide resolved
}

private static FullHttpRequest newRequest() {
FullHttpRequest req = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "/");
req.headers().set(HttpHeaderNames.ACCEPT_ENCODING, "gzip");
Expand Down