Author: markt
Date: Mon Apr 18 15:44:18 2016
New Revision: 1739776

URL: http://svn.apache.org/viewvc?rev=1739776&view=rev
Log:
Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=59310
Do not add a "Content-Length: 0" header for custom responses to HEAD requests 
that do not set a Content-Length value.

Modified:
    tomcat/tc8.5.x/trunk/   (props changed)
    tomcat/tc8.5.x/trunk/java/org/apache/catalina/connector/OutputBuffer.java
    tomcat/tc8.5.x/trunk/test/org/apache/coyote/http11/TestHttp11Processor.java
    tomcat/tc8.5.x/trunk/webapps/docs/changelog.xml

Propchange: tomcat/tc8.5.x/trunk/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Mon Apr 18 15:44:18 2016
@@ -1 +1 @@
-/tomcat/trunk:1734785,1734799,1734845,1734928,1735041,1735044,1735480,1735577,1735597,1735599-1735600,1735615,1736145,1736162,1736209,1736280,1736297,1736299,1736489,1736646,1736703,1736836,1736849,1737104-1737105,1737112,1737117,1737119-1737120,1737155,1737157,1737192,1737280,1737339,1737632,1737664,1737715,1737748,1737785,1737834,1737860,1737959,1738005,1738007,1738014-1738015,1738018,1738022,1738039,1738043,1738059-1738060,1738147,1738149,1738174-1738175,1738261,1738589,1738623-1738625,1738643,1738816,1738850,1738855,1738946-1738948,1738953-1738954,1738979,1738982,1739079-1739081,1739087,1739113,1739153,1739172,1739176,1739191,1739474,1739726,1739762
+/tomcat/trunk:1734785,1734799,1734845,1734928,1735041,1735044,1735480,1735577,1735597,1735599-1735600,1735615,1736145,1736162,1736209,1736280,1736297,1736299,1736489,1736646,1736703,1736836,1736849,1737104-1737105,1737112,1737117,1737119-1737120,1737155,1737157,1737192,1737280,1737339,1737632,1737664,1737715,1737748,1737785,1737834,1737860,1737959,1738005,1738007,1738014-1738015,1738018,1738022,1738039,1738043,1738059-1738060,1738147,1738149,1738174-1738175,1738261,1738589,1738623-1738625,1738643,1738816,1738850,1738855,1738946-1738948,1738953-1738954,1738979,1738982,1739079-1739081,1739087,1739113,1739153,1739172,1739176,1739191,1739474,1739726,1739762,1739775

Modified: 
tomcat/tc8.5.x/trunk/java/org/apache/catalina/connector/OutputBuffer.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc8.5.x/trunk/java/org/apache/catalina/connector/OutputBuffer.java?rev=1739776&r1=1739775&r2=1739776&view=diff
==============================================================================
--- tomcat/tc8.5.x/trunk/java/org/apache/catalina/connector/OutputBuffer.java 
(original)
+++ tomcat/tc8.5.x/trunk/java/org/apache/catalina/connector/OutputBuffer.java 
Mon Apr 18 15:44:18 2016
@@ -262,10 +262,13 @@ public class OutputBuffer extends Writer
             cb.flushBuffer();
         }
 
-        if ((!coyoteResponse.isCommitted())
-            && (coyoteResponse.getContentLengthLong() == -1)) {
+        if ((!coyoteResponse.isCommitted()) && 
(coyoteResponse.getContentLengthLong() == -1) &&
+                !coyoteResponse.getRequest().method().equals("HEAD")) {
             // If this didn't cause a commit of the response, the final content
-            // length can be calculated
+            // length can be calculated. Only do this if this is not a HEAD
+            // request since in that case no body should have been written and
+            // setting a value of zero here will result in an explicit content
+            // length of zero being set on the response.
             if (!coyoteResponse.isCommitted()) {
                 coyoteResponse.setContentLength(bb.getLength());
             }

Modified: 
tomcat/tc8.5.x/trunk/test/org/apache/coyote/http11/TestHttp11Processor.java
URL: 
http://svn.apache.org/viewvc/tomcat/tc8.5.x/trunk/test/org/apache/coyote/http11/TestHttp11Processor.java?rev=1739776&r1=1739775&r2=1739776&view=diff
==============================================================================
--- tomcat/tc8.5.x/trunk/test/org/apache/coyote/http11/TestHttp11Processor.java 
(original)
+++ tomcat/tc8.5.x/trunk/test/org/apache/coyote/http11/TestHttp11Processor.java 
Mon Apr 18 15:44:18 2016
@@ -801,4 +801,48 @@ public class TestHttp11Processor extends
             return true;
         }
     }
+
+
+    @Test
+    public void testBug59310() throws Exception {
+        Tomcat tomcat = getTomcatInstance();
+
+        // No file system docBase required
+        Context ctx = tomcat.addContext("", null);
+
+        Tomcat.addServlet(ctx, "Bug59310", new Bug59310Servlet());
+        ctx.addServletMapping("/test", "Bug59310");
+
+        tomcat.start();
+
+        ByteChunk responseBody = new ByteChunk();
+        Map<String,List<String>> responseHeaders = new HashMap<>();
+
+        int rc = headUrl("http://localhost:"; + getPort() + "/test", 
responseBody,
+                responseHeaders);
+
+        assertEquals(HttpServletResponse.SC_OK, rc);
+        assertEquals(0, responseBody.getLength());
+        assertFalse(responseHeaders.containsKey("Content-Length"));
+    }
+
+
+    private class Bug59310Servlet extends HttpServlet {
+
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        protected void doGet(HttpServletRequest req, HttpServletResponse resp)
+                throws ServletException, IOException {
+            // TODO Auto-generated method stub
+            super.doGet(req, resp);
+        }
+
+        @Override
+        protected void doHead(HttpServletRequest req, HttpServletResponse resp)
+                throws ServletException, IOException {
+            //resp.setContentLengthLong(-1);
+            //resp.flushBuffer();
+        }
+    }
 }

Modified: tomcat/tc8.5.x/trunk/webapps/docs/changelog.xml
URL: 
http://svn.apache.org/viewvc/tomcat/tc8.5.x/trunk/webapps/docs/changelog.xml?rev=1739776&r1=1739775&r2=1739776&view=diff
==============================================================================
--- tomcat/tc8.5.x/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/tc8.5.x/trunk/webapps/docs/changelog.xml Mon Apr 18 15:44:18 2016
@@ -135,6 +135,11 @@
         from the properties passed in the call to <code>getAuthContext</code>.
         Based on a patch by Thomas Maslen. (markt)
       </fix>
+      <fix>
+        <bug>59310</bug>: Do not add a <code>Content-Length: 0</code> header 
for
+        custom responses to <code>HEAD</code> requests that do not set a
+        <code>Content-Length</code> value. (markt)
+      </fix>
     </changelog>
   </subsection>
   <subsection name="Coyote">



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org
For additional commands, e-mail: dev-h...@tomcat.apache.org

Reply via email to