This is an automated email from the ASF dual-hosted git repository. markt pushed a commit to branch 9.0.x in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/9.0.x by this push: new c41d07e Don't send the Keep-Alive response header is the connection is closed c41d07e is described below commit c41d07eced661c8a3eb94008dd30d4274a30b177 Author: Mark Thomas <ma...@apache.org> AuthorDate: Mon Sep 28 09:07:00 2020 +0100 Don't send the Keep-Alive response header is the connection is closed --- java/org/apache/coyote/http11/Http11Processor.java | 10 +++-- .../org/apache/catalina/startup/TesterServlet.java | 17 ++++++++ .../apache/coyote/http11/TestHttp11Processor.java | 51 ++++++++++++++++++---- webapps/docs/changelog.xml | 4 ++ 4 files changed, 70 insertions(+), 12 deletions(-) diff --git a/java/org/apache/coyote/http11/Http11Processor.java b/java/org/apache/coyote/http11/Http11Processor.java index 1c72c33..fa9b290 100644 --- a/java/org/apache/coyote/http11/Http11Processor.java +++ b/java/org/apache/coyote/http11/Http11Processor.java @@ -924,9 +924,13 @@ public class Http11Processor extends AbstractProcessor { // FIXME: Add transfer encoding header - if ((entityBody) && (!contentDelimitation)) { - // Mark as close the connection after the request, and add the - // connection: close header + if ((entityBody) && (!contentDelimitation) || connectionClosePresent) { + // Disable keep-alive if: + // - there is a response body but way for the client to determine + // the content length information; or + // - there is a "connection: close" header present + // This will cause the "connection: close" header to be added if it + // is not already present. keepAlive = false; } diff --git a/test/org/apache/catalina/startup/TesterServlet.java b/test/org/apache/catalina/startup/TesterServlet.java index 324d3f4..59851c9 100644 --- a/test/org/apache/catalina/startup/TesterServlet.java +++ b/test/org/apache/catalina/startup/TesterServlet.java @@ -28,6 +28,19 @@ public class TesterServlet extends HttpServlet { private static final long serialVersionUID = 1L; + private final boolean explicitClose; + + + public TesterServlet() { + this(false); + } + + + public TesterServlet(boolean explicitClose) { + this.explicitClose = explicitClose; + } + + @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { @@ -35,5 +48,9 @@ public class TesterServlet extends HttpServlet { resp.setContentType("text/plain"); PrintWriter out = resp.getWriter(); out.print("OK"); + + if (explicitClose) { + resp.setHeader("Connection", "close"); + } } } diff --git a/test/org/apache/coyote/http11/TestHttp11Processor.java b/test/org/apache/coyote/http11/TestHttp11Processor.java index b2107b0..ce4892e 100644 --- a/test/org/apache/coyote/http11/TestHttp11Processor.java +++ b/test/org/apache/coyote/http11/TestHttp11Processor.java @@ -1513,36 +1513,66 @@ public class TestHttp11Processor extends TomcatBaseTest { @Test public void testKeepAliveHeader01() throws Exception { - doTestKeepAliveHeader(false, 3000, 10); + doTestKeepAliveHeader(false, 3000, 10, false); } @Test public void testKeepAliveHeader02() throws Exception { - doTestKeepAliveHeader(true, 5000, 1); + doTestKeepAliveHeader(true, 5000, 1, false); } @Test public void testKeepAliveHeader03() throws Exception { - doTestKeepAliveHeader(true, 5000, 10); + doTestKeepAliveHeader(true, 5000, 10, false); } @Test public void testKeepAliveHeader04() throws Exception { - doTestKeepAliveHeader(true, -1, 10); + doTestKeepAliveHeader(true, -1, 10, false); } @Test public void testKeepAliveHeader05() throws Exception { - doTestKeepAliveHeader(true, -1, 1); + doTestKeepAliveHeader(true, -1, 1, false); } @Test public void testKeepAliveHeader06() throws Exception { - doTestKeepAliveHeader(true, -1, -1); + doTestKeepAliveHeader(true, -1, -1, false); + } + + @Test + public void testKeepAliveHeader07() throws Exception { + doTestKeepAliveHeader(false, 3000, 10, true); + } + + @Test + public void testKeepAliveHeader08() throws Exception { + doTestKeepAliveHeader(true, 5000, 1, true); + } + + @Test + public void testKeepAliveHeader09() throws Exception { + doTestKeepAliveHeader(true, 5000, 10, true); + } + + @Test + public void testKeepAliveHeader10() throws Exception { + doTestKeepAliveHeader(true, -1, 10, true); + } + + @Test + public void testKeepAliveHeader11() throws Exception { + doTestKeepAliveHeader(true, -1, 1, true); + } + + @Test + public void testKeepAliveHeader12() throws Exception { + doTestKeepAliveHeader(true, -1, -1, true); } private void doTestKeepAliveHeader(boolean sendKeepAlive, int keepAliveTimeout, - int maxKeepAliveRequests) throws Exception { + int maxKeepAliveRequests, boolean explicitClose) throws Exception { Tomcat tomcat = getTomcatInstance(); tomcat.getConnector().setProperty("keepAliveTimeout", Integer.toString(keepAliveTimeout)); @@ -1552,7 +1582,7 @@ public class TestHttp11Processor extends TomcatBaseTest { Context ctx = tomcat.addContext("", null); // Add servlet - Tomcat.addServlet(ctx, "TesterServlet", new TesterServlet()); + Tomcat.addServlet(ctx, "TesterServlet", new TesterServlet(explicitClose)); ctx.addServletMappingDecoded("/foo", "TesterServlet"); tomcat.start(); @@ -1586,7 +1616,10 @@ public class TestHttp11Processor extends TomcatBaseTest { } } - if (!sendKeepAlive || keepAliveTimeout < 0 + if (explicitClose) { + Assert.assertEquals("close", connectionHeaderValue); + Assert.assertNull(keepAliveHeaderValue); + } else if (!sendKeepAlive || keepAliveTimeout < 0 && (maxKeepAliveRequests < 0 || maxKeepAliveRequests > 1)) { Assert.assertNull(connectionHeaderValue); Assert.assertNull(keepAliveHeaderValue); diff --git a/webapps/docs/changelog.xml b/webapps/docs/changelog.xml index 9fea913..93babdf 100644 --- a/webapps/docs/changelog.xml +++ b/webapps/docs/changelog.xml @@ -119,6 +119,10 @@ Connector is configured with <code>useAsyncIO="true"</code>. (markt) </fix> + <fix> + Don't send the Keep-Alive response header if the connection has been + explicitly closed. (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