This is an automated email from the ASF dual-hosted git repository.

markt pushed a commit to branch 8.5.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git


The following commit(s) were added to refs/heads/8.5.x by this push:
     new fe040cf  Don't send the Keep-Alive response header is the connection 
is closed
fe040cf is described below

commit fe040cf853aeb859653a24dbac13297abd1f6ba2
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 13057c4..5d27a8d 100644
--- a/java/org/apache/coyote/http11/Http11Processor.java
+++ b/java/org/apache/coyote/http11/Http11Processor.java
@@ -1161,9 +1161,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 174793e..48a843b 100644
--- a/test/org/apache/coyote/http11/TestHttp11Processor.java
+++ b/test/org/apache/coyote/http11/TestHttp11Processor.java
@@ -1529,36 +1529,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));
@@ -1568,7 +1598,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();
@@ -1602,7 +1632,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 1c0d118..5cbbe9a 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -90,6 +90,10 @@
         in-flight asynchronous requests. The tracking enables Tomcat to 
shutdown
         gracefully when asynchronous processing is in use. (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

Reply via email to