From 553c4d2df877ea33777b69b98870a65d0a3ce73c Mon Sep 17 00:00:00 2001 From: Trustin Lee Date: Thu, 4 Jun 2009 03:18:44 +0000 Subject: [PATCH] * There's no need to send content-length header if HttpRequestHandler is going to close the connection right after a response. * HttpClient sets 'Connection' to 'close' so that the HTTP server closes the connection --- src/main/java/org/jboss/netty/example/http/HttpClient.java | 1 + .../org/jboss/netty/example/http/HttpRequestHandler.java | 7 ++++++- 2 files changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/jboss/netty/example/http/HttpClient.java b/src/main/java/org/jboss/netty/example/http/HttpClient.java index e4991d74c7..c44453c49d 100644 --- a/src/main/java/org/jboss/netty/example/http/HttpClient.java +++ b/src/main/java/org/jboss/netty/example/http/HttpClient.java @@ -90,6 +90,7 @@ public class HttpClient { HttpRequest request = new DefaultHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.GET, uri.toASCIIString()); request.addHeader(HttpHeaders.Names.HOST, host); + request.addHeader(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE); CookieEncoder httpCookieEncoder = new CookieEncoder(false); httpCookieEncoder.addCookie("my-cookie", "foo"); httpCookieEncoder.addCookie("another-cookie", "bar"); diff --git a/src/main/java/org/jboss/netty/example/http/HttpRequestHandler.java b/src/main/java/org/jboss/netty/example/http/HttpRequestHandler.java index bb5cd04c80..07354307d2 100644 --- a/src/main/java/org/jboss/netty/example/http/HttpRequestHandler.java +++ b/src/main/java/org/jboss/netty/example/http/HttpRequestHandler.java @@ -131,7 +131,12 @@ public class HttpRequestHandler extends SimpleChannelUpstreamHandler { HttpResponse response = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.setContent(buf); response.setHeader(HttpHeaders.Names.CONTENT_TYPE, "text/plain; charset=UTF-8"); - response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes())); + + if (!close) { + // There's no need to add 'Content-Length' header + // if this is the last response. + response.setHeader(HttpHeaders.Names.CONTENT_LENGTH, String.valueOf(buf.readableBytes())); + } String cookieString = request.getHeader(HttpHeaders.Names.COOKIE); if (cookieString != null) {