From 0dca08ab123821061c6681167cfde4021ce5e73f Mon Sep 17 00:00:00 2001 From: Jeff Beck Date: Wed, 24 Dec 2014 14:48:41 -0600 Subject: [PATCH] HttpObjectAggregator only set Content-Length is not already set. Motivation: HEAD requests will have a Content-Length set that doesn't match the actual length. So we only want to set Content-Length header if it isn't already set. Modifications: If check around setting the Content-Length. Result: A HEAD request will no correctly return the specified Content-Length instead of the body length. --- .../handler/codec/http/HttpObjectAggregator.java | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectAggregator.java b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectAggregator.java index 696e8c1de4..50055bef33 100644 --- a/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectAggregator.java +++ b/codec-http/src/main/java/io/netty/handler/codec/http/HttpObjectAggregator.java @@ -212,11 +212,17 @@ public class HttpObjectAggregator extends MessageToMessageDecoder { currentMessage.setTrailingHeaders(new DefaultHttpHeaders()); } - // Set the 'Content-Length' header. - currentMessage.headers().set( - HttpHeaders.Names.CONTENT_LENGTH, - String.valueOf(content.readableBytes())); - + // Set the 'Content-Length' header. If one isn't already set. + // This is important as HEAD responses will use a 'Content-Length' header which + // does not match the actual body, but the number of bytes that would be + // transmitted if a GET would have been used. + // + // See rfc2616 14.13 Content-Length + if (!isContentLengthSet(currentMessage)) { + currentMessage.headers().set( + Names.CONTENT_LENGTH, + String.valueOf(content.readableBytes())); + } // All done out.add(currentMessage); }