From 4488967c5697d4d42b1bdf1746e70efe9261fecc Mon Sep 17 00:00:00 2001 From: Marco Pfatschbacher Date: Wed, 27 Feb 2019 10:26:41 +0100 Subject: [PATCH] Fix http input keep-alive handling We were only responding to the first request on an http keep-alive connection with a 202. Any following requests would not be answered, leading to a timeout in the client. Configuring a ChannelFuture for CLOSE_ON_FAILURE did expose the reason for the bug: `io.netty.handler.codec.EncoderException: java.lang.IllegalStateException: unexpected message type: DefaultHttpResponse, state: 1` We ought to use `DefaultFullHttpResponse` instead of `DefaultHttpResponse`. Also keep the `CLOSE_ON_FAILURE` to make this more robust for future bugs. Fixes #5720 --- .../graylog2/inputs/transports/netty/HttpHandler.java | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/graylog2-server/src/main/java/org/graylog2/inputs/transports/netty/HttpHandler.java b/graylog2-server/src/main/java/org/graylog2/inputs/transports/netty/HttpHandler.java index 898b521dc360..f639e00f92c6 100644 --- a/graylog2-server/src/main/java/org/graylog2/inputs/transports/netty/HttpHandler.java +++ b/graylog2-server/src/main/java/org/graylog2/inputs/transports/netty/HttpHandler.java @@ -22,7 +22,7 @@ import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; -import io.netty.handler.codec.http.DefaultHttpResponse; +import io.netty.handler.codec.http.DefaultFullHttpResponse; import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.HttpHeaderNames; import io.netty.handler.codec.http.HttpHeaderValues; @@ -74,7 +74,7 @@ private void writeResponse(Channel channel, HttpVersion httpRequestVersion, HttpResponseStatus status, String origin) { - final HttpResponse response = new DefaultHttpResponse(httpRequestVersion, status); + final HttpResponse response = new DefaultFullHttpResponse(httpRequestVersion, status); response.headers().set(HttpHeaderNames.CONTENT_LENGTH, 0); response.headers().set(HttpHeaderNames.CONNECTION, keepAlive ? HttpHeaderValues.KEEP_ALIVE : HttpHeaderValues.CLOSE); @@ -87,8 +87,6 @@ private void writeResponse(Channel channel, final ChannelFuture channelFuture = channel.writeAndFlush(response); - if (!keepAlive) { - channelFuture.addListener(ChannelFutureListener.CLOSE); - } + channelFuture.addListener(keepAlive ? ChannelFutureListener.CLOSE_ON_FAILURE : ChannelFutureListener.CLOSE); } -} \ No newline at end of file +}