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
Conversation
Motivation: HttpContentCompressor fails with an exception when trying to write an empty HttpContent: ``` io.netty.handler.codec.EncoderException: MessageToMessageCodec$1 must produce at least one message. at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:99) at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116) at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:879) at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:863) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:968) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:856) at io.netty.channel.ChannelDuplexHandler.write(ChannelDuplexHandler.java:115) ``` Modification: If the compression has no output, write an empty dummy HttpContent. This is not an ideal solution, but this should not happen often anyway. Result: HttpContentCompressor does not fail anymore.
@@ -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)); |
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yawkat wdyt ?
There was a problem hiding this comment.
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.
codec-http/src/test/java/io/netty/handler/codec/http/HttpContentCompressorTest.java
Show resolved
Hide resolved
…ntCompressorTest.java Co-authored-by: Norman Maurer <norman_maurer@apple.com>
Motivation: HttpContentCompressor fails with an exception when trying to write an empty HttpContent: ``` io.netty.handler.codec.EncoderException: MessageToMessageCodec$1 must produce at least one message. at io.netty.handler.codec.MessageToMessageEncoder.write(MessageToMessageEncoder.java:99) at io.netty.handler.codec.MessageToMessageCodec.write(MessageToMessageCodec.java:116) at io.netty.channel.AbstractChannelHandlerContext.invokeWrite0(AbstractChannelHandlerContext.java:879) at io.netty.channel.AbstractChannelHandlerContext.invokeWrite(AbstractChannelHandlerContext.java:863) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:968) at io.netty.channel.AbstractChannelHandlerContext.write(AbstractChannelHandlerContext.java:856) at io.netty.channel.ChannelDuplexHandler.write(ChannelDuplexHandler.java:115) ``` Modification: If the compression has no output, write an empty dummy HttpContent. This is not an ideal solution, but this should not happen often anyway. Result: HttpContentCompressor does not fail anymore. --------- Co-authored-by: Norman Maurer <norman_maurer@apple.com>
Motivation:
HttpContentCompressor fails with an exception when trying to write an empty HttpContent:
Modification:
If the compression has no output, write an empty dummy HttpContent. This is not an ideal solution, but this should not happen often anyway.
Result:
HttpContentCompressor does not fail anymore.