You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2016/04/18 17:43:19 UTC

svn commit: r1739775 - in /tomcat/trunk: java/org/apache/catalina/connector/OutputBuffer.java test/org/apache/coyote/http11/TestHttp11Processor.java webapps/docs/changelog.xml

Author: markt
Date: Mon Apr 18 15:43:19 2016
New Revision: 1739775

URL: http://svn.apache.org/viewvc?rev=1739775&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/trunk/java/org/apache/catalina/connector/OutputBuffer.java
    tomcat/trunk/test/org/apache/coyote/http11/TestHttp11Processor.java
    tomcat/trunk/webapps/docs/changelog.xml

Modified: tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java?rev=1739775&r1=1739774&r2=1739775&view=diff
==============================================================================
--- tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java (original)
+++ tomcat/trunk/java/org/apache/catalina/connector/OutputBuffer.java Mon Apr 18 15:43:19 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/trunk/test/org/apache/coyote/http11/TestHttp11Processor.java
URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/coyote/http11/TestHttp11Processor.java?rev=1739775&r1=1739774&r2=1739775&view=diff
==============================================================================
--- tomcat/trunk/test/org/apache/coyote/http11/TestHttp11Processor.java (original)
+++ tomcat/trunk/test/org/apache/coyote/http11/TestHttp11Processor.java Mon Apr 18 15:43:19 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/trunk/webapps/docs/changelog.xml
URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1739775&r1=1739774&r2=1739775&view=diff
==============================================================================
--- tomcat/trunk/webapps/docs/changelog.xml (original)
+++ tomcat/trunk/webapps/docs/changelog.xml Mon Apr 18 15:43:19 2016
@@ -140,6 +140,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-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org