From 64f529945282e41eb475952fde382f234da8eec7 Mon Sep 17 00:00:00 2001 From: vibul Date: Wed, 28 Mar 2012 15:50:17 +1100 Subject: [PATCH] Unless the Connection: keep-alive header is present in the HTTP response, apache benchmark (ab) hangs on keep alive connections. This is as per HTTP 1.1 spec: http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection --- .../netty/example/http/file/HttpStaticFileServerHandler.java | 3 +++ .../io/netty/example/http/snoop/HttpSnoopServerHandler.java | 3 +++ 2 files changed, 6 insertions(+) diff --git a/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java b/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java index 29df997778..8b73a41b0d 100644 --- a/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java +++ b/example/src/main/java/io/netty/example/http/file/HttpStaticFileServerHandler.java @@ -160,6 +160,9 @@ public class HttpStaticFileServerHandler extends SimpleChannelUpstreamHandler { setContentLength(response, fileLength); setContentTypeHeader(response, file); setDateAndCacheHeaders(response, file); + if (isKeepAlive(request)) { + response.setHeader(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); + } Channel ch = e.getChannel(); diff --git a/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java b/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java index 512fdd7cda..5c47e837c7 100644 --- a/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java +++ b/example/src/main/java/io/netty/example/http/snoop/HttpSnoopServerHandler.java @@ -39,6 +39,7 @@ import io.netty.handler.codec.http.CookieEncoder; import io.netty.handler.codec.http.DefaultHttpResponse; import io.netty.handler.codec.http.HttpChunk; import io.netty.handler.codec.http.HttpChunkTrailer; +import io.netty.handler.codec.http.HttpHeaders; import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.QueryStringDecoder; @@ -131,6 +132,8 @@ public class HttpSnoopServerHandler extends SimpleChannelUpstreamHandler { if (keepAlive) { // Add 'Content-Length' header only for a keep-alive connection. response.setHeader(CONTENT_LENGTH, response.getContent().readableBytes()); + // Add keep alive header as per http://www.w3.org/Protocols/HTTP/1.1/draft-ietf-http-v11-spec-01.html#Connection + response.setHeader(CONNECTION, HttpHeaders.Values.KEEP_ALIVE); } // Encode the cookie.