This is an automated email from the ASF dual-hosted git repository.
markt pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/tomcat.git
The following commit(s) were added to refs/heads/master by this push:
new a20f6f3 Don't send the Keep-Alive response header is the connection
is closed
a20f6f3 is described below
commit a20f6f3f209441013996abe7f8049f1c8793841a
Author: Mark Thomas <[email protected]>
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 98b1f23..22f8787 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 79e7328..219c08a 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 0ef395e..346f264 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 4929b46..8dc1aba 100644
--- a/webapps/docs/changelog.xml
+++ b/webapps/docs/changelog.xml
@@ -125,6 +125,10 @@
associated with used streams and to retain information for more streams
in the priority tree. (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: [email protected]
For additional commands, e-mail: [email protected]