Skip to content

Do not fail when compressing empty HttpContent#13655

Merged
normanmaurer merged 2 commits intonetty:4.1from
yawkat:empty-compress
Oct 10, 2023
Merged

Do not fail when compressing empty HttpContent#13655
normanmaurer merged 2 commits intonetty:4.1from
yawkat:empty-compress

Conversation

@yawkat
Copy link
Copy Markdown
Contributor

@yawkat yawkat commented Oct 9, 2023

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.

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.
state = State.AWAIT_HEADERS;
} else if (out.isEmpty()) {
// MessageToMessageCodec needs at least one output message
out.add(new DefaultHttpContent(Unpooled.EMPTY_BUFFER));
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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.

…ntCompressorTest.java

Co-authored-by: Norman Maurer <norman_maurer@apple.com>
@normanmaurer normanmaurer added this to the 4.1.100.Final milestone Oct 9, 2023
@normanmaurer normanmaurer merged commit 4911448 into netty:4.1 Oct 10, 2023
normanmaurer added a commit that referenced this pull request Oct 11, 2023
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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants