From 66c4c07ec08b568923b60752a89146f279140f64 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Wed, 17 Jul 2013 12:01:50 +0200 Subject: [PATCH] [#1595] Fix IllegalStateException thrown by HttpObjectEncoder when an empty HttpContent was written --- .../netty/handler/codec/http/HttpObjectEncoder.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java index 23c0625067..1d0898e2ae 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectEncoder.java @@ -16,6 +16,7 @@ package io.netty.handler.codec.http; import io.netty.buffer.ByteBuf; +import io.netty.buffer.Unpooled; import io.netty.channel.ChannelHandlerContext; import io.netty.handler.codec.MessageToMessageEncoder; import io.netty.util.CharsetUtil; @@ -81,6 +82,10 @@ public abstract class HttpObjectEncoder extends MessageTo if (state == ST_CONTENT_NON_CHUNK) { if (contentLength > 0) { out.add(content.retain()); + } else { + // Need to produce some output otherwise an + // IllegalstateException will be thrown + out.add(Unpooled.EMPTY_BUFFER); } if (chunk instanceof LastHttpContent) { @@ -110,6 +115,12 @@ public abstract class HttpObjectEncoder extends MessageTo } state = ST_INIT; + } else { + if (contentLength == 0) { + // Need to produce some output otherwise an + // IllegalstateException will be thrown + out.add(Unpooled.EMPTY_BUFFER); + } } } else { throw new Error();