Author: markt Date: Wed May 28 12:15:51 2014 New Revision: 1597987 URL: http://svn.apache.org/r1597987 Log: Fix https://issues.apache.org/bugzilla/show_bug.cgi?id=56555 When Tomcat closes the connection based on the HTTP status code of the response, ensure that only one connection header is sent to the client.
Modified: tomcat/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java tomcat/trunk/test/org/apache/coyote/http11/TestAbstractHttp11Processor.java tomcat/trunk/webapps/docs/changelog.xml Modified: tomcat/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java URL: http://svn.apache.org/viewvc/tomcat/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java?rev=1597987&r1=1597986&r2=1597987&view=diff ============================================================================== --- tomcat/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java (original) +++ tomcat/trunk/java/org/apache/coyote/http11/AbstractHttp11Processor.java Wed May 28 12:15:51 2014 @@ -1485,11 +1485,8 @@ public abstract class AbstractHttp11Proc // Connection: close header. keepAlive = keepAlive && !statusDropsConnection(statusCode); if (!keepAlive) { - // Avoid adding the close header twice - if (!connectionClosePresent) { - headers.addValue(Constants.CONNECTION).setString( - Constants.CLOSE); - } + headers.setValue(Constants.CONNECTION).setString( + Constants.CLOSE); } else if (!http11 && !error) { headers.addValue(Constants.CONNECTION).setString(Constants.KEEPALIVE); } Modified: tomcat/trunk/test/org/apache/coyote/http11/TestAbstractHttp11Processor.java URL: http://svn.apache.org/viewvc/tomcat/trunk/test/org/apache/coyote/http11/TestAbstractHttp11Processor.java?rev=1597987&r1=1597986&r2=1597987&view=diff ============================================================================== --- tomcat/trunk/test/org/apache/coyote/http11/TestAbstractHttp11Processor.java (original) +++ tomcat/trunk/test/org/apache/coyote/http11/TestAbstractHttp11Processor.java Wed May 28 12:15:51 2014 @@ -25,6 +25,7 @@ import java.net.Socket; import java.nio.CharBuffer; import java.util.HashMap; import java.util.List; +import java.util.Locale; import java.util.Map; import java.util.concurrent.CountDownLatch; @@ -53,6 +54,45 @@ import org.apache.tomcat.util.buf.ByteCh public class TestAbstractHttp11Processor extends TomcatBaseTest { @Test + public void testStatusForcesClose() throws Exception { + Tomcat tomcat = getTomcatInstance(); + + // Must have a real docBase - just use temp + Context ctxt = tomcat.addContext("", System.getProperty("java.io.tmpdir")); + + // Add protected servlet + Tomcat.addServlet(ctxt, "StatusForcesCloseServlet", new StatusForcesCloseServlet()); + ctxt.addServletMapping("/*", "StatusForcesCloseServlet"); + + tomcat.start(); + + ByteChunk bc = new ByteChunk(); + Map<String,List<String>> responseHeaders = new HashMap<>(); + getUrl("http://localhost:" + getPort() + "/anything", bc, responseHeaders); + + // Assumes header name uses standard case + List<String> values = responseHeaders.get("Connection"); + Assert.assertEquals(1, values.size()); + Assert.assertEquals("close", values.get(0).toLowerCase(Locale.ENGLISH)); + } + + private static class StatusForcesCloseServlet extends HttpServlet { + + private static final long serialVersionUID = 1L; + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) + throws ServletException, IOException { + + // Set the Connection header + resp.setHeader("Connection", "keep-alive"); + + // Set a status code that should force the connection to close + resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); + } + } + + @Test public void testWithTEVoid() throws Exception { Tomcat tomcat = getTomcatInstance(); Modified: tomcat/trunk/webapps/docs/changelog.xml URL: http://svn.apache.org/viewvc/tomcat/trunk/webapps/docs/changelog.xml?rev=1597987&r1=1597986&r2=1597987&view=diff ============================================================================== --- tomcat/trunk/webapps/docs/changelog.xml (original) +++ tomcat/trunk/webapps/docs/changelog.xml Wed May 28 12:15:51 2014 @@ -130,6 +130,11 @@ Ensure that a request without a body is correctly handled during Comet processing. This fixes the Comet chat example. (markt) </fix> + <fix> + <bug>56555</bug>: When Tomcat closes the connection based on the HTTP + status code of the response, ensure that only one connection header is + sent to the client. (markt) + </fix> </changelog> </subsection> <subsection name="Jasper"> --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org