Author: markt Date: Mon Mar 2 11:32:36 2015 New Revision: 1663264 URL: http://svn.apache.org/r1663264 Log: Fix https://bz.apache.org/bugzilla/show_bug.cgi?id=57601 Ensure HEAD requests return the correct content length (i.e. the same as for a GET) when the requested resource includes a resource served by the Default servlet.
Added: tomcat/trunk/test/webapp/bug5nnnn/bug57601.jsp (with props) tomcat/trunk/test/webapp/bug5nnnn/bug57601.txt (with props) Modified: tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java tomcat/trunk/test/org/apache/catalina/servlets/TestDefaultServlet.java Modified: tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java?rev=1663264&r1=1663263&r2=1663264&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java (original) +++ tomcat/trunk/java/org/apache/catalina/servlets/DefaultServlet.java Mon Mar 2 11:32:36 2015 @@ -39,6 +39,7 @@ import java.util.Iterator; import java.util.Locale; import java.util.StringTokenizer; +import javax.servlet.DispatcherType; import javax.servlet.RequestDispatcher; import javax.servlet.ServletContext; import javax.servlet.ServletException; @@ -411,13 +412,13 @@ public class DefaultServlet extends Http * @exception ServletException if a servlet-specified error occurs */ @Override - protected void doHead(HttpServletRequest request, - HttpServletResponse response) - throws IOException, ServletException { - - // Serve the requested resource, without the data content - serveResource(request, response, false, fileEncoding); - + protected void doHead(HttpServletRequest request, HttpServletResponse response) + throws IOException, ServletException { + // Serve the requested resource, without the data content unless we are + // being included since in that case the content needs to be provided so + // the correct content length is reported for the including resource + boolean serveContent = DispatcherType.INCLUDE.equals(request.getDispatcherType()); + serveResource(request, response, serveContent, fileEncoding); } Modified: tomcat/trunk/test/org/apache/catalina/servlets/TestDefaultServlet.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/catalina/servlets/TestDefaultServlet.java?rev=1663264&r1=1663263&r2=1663264&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/catalina/servlets/TestDefaultServlet.java (original) +++ tomcat/trunk/test/org/apache/catalina/servlets/TestDefaultServlet.java Mon Mar 2 11:32:36 2015 @@ -23,6 +23,7 @@ import java.io.OutputStreamWriter; import java.io.Writer; import java.text.SimpleDateFormat; import java.util.Date; +import java.util.HashMap; import java.util.List; import java.util.Locale; import java.util.Map; @@ -35,6 +36,7 @@ import static org.junit.Assert.assertFal import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import org.junit.Assert; import org.junit.Test; import static org.apache.catalina.startup.SimpleHttpClient.CRLF; @@ -340,6 +342,33 @@ public class TestDefaultServlet extends assertTrue(client.isResponse404()); } + /** + * Verifies that the same Content-Length is returned for both GET and HEAD + * operations when a static resource served by the DefaultServlet is + * included. + */ + @Test + public void testBug57601() throws Exception { + Tomcat tomcat = getTomcatInstanceTestWebapp(false, true); + + Map<String,List<String>> resHeaders= new HashMap<>(); + String path = "http://localhost:" + getPort() + "/test/bug5nnnn/bug57601.jsp"; + ByteChunk out = new ByteChunk(); + + int rc = getUrl(path, out, resHeaders); + Assert.assertEquals(HttpServletResponse.SC_OK, rc); + String length = resHeaders.get("Content-Length").get(0); + Assert.assertEquals(Long.parseLong(length), out.getLength()); + out.recycle(); + + rc = headUrl(path, out, resHeaders); + Assert.assertEquals(HttpServletResponse.SC_OK, rc); + Assert.assertEquals(0, out.getLength()); + Assert.assertEquals(length, resHeaders.get("Content-Length").get(0)); + + tomcat.stop(); + } + public static int getUrl(String path, ByteChunk out, Map<String, List<String>> resHead) throws IOException { out.recycle(); Added: tomcat/trunk/test/webapp/bug5nnnn/bug57601.jsp URL: http://svn.apache.org/viewvc/tomcat/trunk/test/webapp/bug5nnnn/bug57601.jsp?rev=1663264&view=auto ============================================================================== --- tomcat/trunk/test/webapp/bug5nnnn/bug57601.jsp (added) +++ tomcat/trunk/test/webapp/bug5nnnn/bug57601.jsp Mon Mar 2 11:32:36 2015 @@ -0,0 +1,18 @@ +<%-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You under the Apache License, Version 2.0 + (the "License"); you may not use this file except in compliance with + the License. You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. + --%> +Outer +<jsp:include page="bug57601.txt"/> \ No newline at end of file Propchange: tomcat/trunk/test/webapp/bug5nnnn/bug57601.jsp ------------------------------------------------------------------------------ svn:eol-style = native Added: tomcat/trunk/test/webapp/bug5nnnn/bug57601.txt URL: http://svn.apache.org/viewvc/tomcat/trunk/test/webapp/bug5nnnn/bug57601.txt?rev=1663264&view=auto ============================================================================== --- tomcat/trunk/test/webapp/bug5nnnn/bug57601.txt (added) +++ tomcat/trunk/test/webapp/bug5nnnn/bug57601.txt Mon Mar 2 11:32:36 2015 @@ -0,0 +1,16 @@ +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. +# The ASF licenses this file to You under the Apache License, Version 2.0 +# (the "License"); you may not use this file except in compliance with +# the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +Inner \ No newline at end of file Propchange: tomcat/trunk/test/webapp/bug5nnnn/bug57601.txt ------------------------------------------------------------------------------ svn:eol-style = native --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org