You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by mi...@apache.org on 2019/10/11 11:15:11 UTC

[tomcat] branch BZ-63835/9.0.x created (now a5e3e1d)

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

michaelo pushed a change to branch BZ-63835/9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git.


      at a5e3e1d  Properly determine requests left on a "keepAlive" connection

This branch includes the following new commits:

     new f903e1f  First draft
     new a5e3e1d  Properly determine requests left on a "keepAlive" connection

The 2 revisions listed above as "new" are entirely new to this
repository and will be described in separate emails.  The revisions
listed as "add" were already present in the repository and have only
been added to this reference.



---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[tomcat] 01/02: First draft

Posted by mi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch BZ-63835/9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit f903e1fe0d3902111b626e82d201d7598a15118e
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Fri Oct 11 10:30:08 2019 +0200

    First draft
---
 java/org/apache/coyote/http11/Http11Processor.java | 36 ++++++++++++++++++----
 1 file changed, 30 insertions(+), 6 deletions(-)

diff --git a/java/org/apache/coyote/http11/Http11Processor.java b/java/org/apache/coyote/http11/Http11Processor.java
index a176206..7bc50a4 100644
--- a/java/org/apache/coyote/http11/Http11Processor.java
+++ b/java/org/apache/coyote/http11/Http11Processor.java
@@ -21,6 +21,7 @@ import java.io.InterruptedIOException;
 import java.nio.ByteBuffer;
 import java.util.Enumeration;
 import java.util.Locale;
+import java.util.concurrent.TimeUnit;
 import java.util.regex.Pattern;
 
 import javax.servlet.http.HttpServletResponse;
@@ -913,7 +914,7 @@ public class Http11Processor extends AbstractProcessor {
         } else {
             // If the response code supports an entity body and we're on
             // HTTP 1.1 then we chunk unless we have a Connection: close header
-            connectionClosePresent = isConnectionClose(headers);
+            connectionClosePresent = isConnectionValue(headers, Constants.CLOSE);
             if (http11 && entityBody && !connectionClosePresent) {
                 outputBuffer.addActiveFilter(outputFilters[Constants.CHUNKED_FILTER]);
                 contentDelimitation = true;
@@ -957,10 +958,33 @@ public class Http11Processor extends AbstractProcessor {
                 headers.addValue(Constants.CONNECTION).setString(
                         Constants.CLOSE);
             }
-        } else if (!http11 && !getErrorState().isError()) {
-            headers.addValue(Constants.CONNECTION).setString(Constants.KEEPALIVE);
-        }
+        } else if (!getErrorState().isError()) {
+            if (!http11) {
+                headers.addValue(Constants.CONNECTION).setString(Constants.KEEPALIVE);
+            }
+
+            boolean connectionKeepAlivePresent =
+                isConnectionValue(request.getMimeHeaders(), Constants.KEEPALIVE);
+
+            if (connectionKeepAlivePresent) {
+                int keepAliveTimeout = protocol.getKeepAliveTimeout();
+                int maxKeepAliveRequests = protocol.getMaxKeepAliveRequests();
+
+                if (keepAliveTimeout > 0) {
+                    String value = "timeout=" + TimeUnit.MILLISECONDS.toSeconds(keepAliveTimeout);
+
+                    if (maxKeepAliveRequests > 0) {
+                        value += ", max=" + maxKeepAliveRequests;
+                    }
 
+                    headers.setValue("Keep-Alive").setString(value);
+                }
+
+                if (http11) {
+                    headers.addValue(Constants.CONNECTION).setString(Constants.KEEPALIVE);
+                }
+            }
+        }
         // Add server header
         String server = protocol.getServer();
         if (server == null) {
@@ -992,12 +1016,12 @@ public class Http11Processor extends AbstractProcessor {
         outputBuffer.commit();
     }
 
-    private static boolean isConnectionClose(MimeHeaders headers) {
+    private static boolean isConnectionValue(MimeHeaders headers, String value) {
         MessageBytes connection = headers.getValue(Constants.CONNECTION);
         if (connection == null) {
             return false;
         }
-        return connection.equals(Constants.CLOSE);
+        return connection.equals(value);
     }
 
     private void prepareSendfile(OutputFilter[] outputFilters) {


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org


[tomcat] 02/02: Properly determine requests left on a "keepAlive" connection

Posted by mi...@apache.org.
This is an automated email from the ASF dual-hosted git repository.

michaelo pushed a commit to branch BZ-63835/9.0.x
in repository https://gitbox.apache.org/repos/asf/tomcat.git

commit a5e3e1d7a498a3156350ae8bbed36706b2600e64
Author: Michael Osipov <mi...@apache.org>
AuthorDate: Fri Oct 11 12:51:44 2019 +0200

    Properly determine requests left on a "keepAlive" connection
---
 java/org/apache/coyote/http11/Http11Processor.java     | 8 +++++++-
 java/org/apache/tomcat/util/net/SocketWrapperBase.java | 6 +++++-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/java/org/apache/coyote/http11/Http11Processor.java b/java/org/apache/coyote/http11/Http11Processor.java
index 7bc50a4..314c3d1 100644
--- a/java/org/apache/coyote/http11/Http11Processor.java
+++ b/java/org/apache/coyote/http11/Http11Processor.java
@@ -974,7 +974,13 @@ public class Http11Processor extends AbstractProcessor {
                     String value = "timeout=" + TimeUnit.MILLISECONDS.toSeconds(keepAliveTimeout);
 
                     if (maxKeepAliveRequests > 0) {
-                        value += ", max=" + maxKeepAliveRequests;
+                        /*
+                         * We need to add 1 here because the value is already decremented in
+                         * service() by 1. Otherwise the client would see 99 on the first request.
+                         * In HTTPd this value is modified after this code block has been run.
+                         */
+                        int left = socketWrapper.getKeepAliveLeft() + 1;
+                        value += ", max=" + left;
                     }
 
                     headers.setValue("Keep-Alive").setString(value);
diff --git a/java/org/apache/tomcat/util/net/SocketWrapperBase.java b/java/org/apache/tomcat/util/net/SocketWrapperBase.java
index 2c082d6..154c9c7 100644
--- a/java/org/apache/tomcat/util/net/SocketWrapperBase.java
+++ b/java/org/apache/tomcat/util/net/SocketWrapperBase.java
@@ -212,12 +212,16 @@ public abstract class SocketWrapperBase<E> {
         return this.writeTimeout;
     }
 
+    public int getKeepAliveLeft() {
+        return keepAliveLeft;
+    }
+
     public void setKeepAliveLeft(int keepAliveLeft) {
         this.keepAliveLeft = keepAliveLeft;
     }
 
     public int decrementKeepAlive() {
-        return --keepAliveLeft;
+        return keepAliveLeft--;
     }
 
     public String getRemoteHost() {


---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@tomcat.apache.org
For additional commands, e-mail: dev-help@tomcat.apache.org