You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by re...@apache.org on 2020/02/12 21:05:50 UTC

[tomcat] branch 9.0.x updated: Add offset to the buffer remaining method

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

remm 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 23448f5  Add offset to the buffer remaining method
23448f5 is described below

commit 23448f55f4b6364d7b142d33b9bc5a56ea8da7be
Author: remm <re...@apache.org>
AuthorDate: Wed Feb 12 21:59:30 2020 +0100

    Add offset to the buffer remaining method
    
    Add a zero length write to the existing test. Also enable the test for
    APR.
---
 java/org/apache/tomcat/util/net/Nio2Endpoint.java        | 10 +++++-----
 java/org/apache/tomcat/util/net/NioEndpoint.java         |  4 ++--
 java/org/apache/tomcat/util/net/SocketWrapperBase.java   |  6 +++---
 .../http11/upgrade/TestUpgradeInternalHandler.java       | 16 +++++++++++-----
 4 files changed, 21 insertions(+), 15 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/Nio2Endpoint.java b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
index 67d0b81..e22d52e 100644
--- a/java/org/apache/tomcat/util/net/Nio2Endpoint.java
+++ b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
@@ -682,7 +682,7 @@ public class Nio2Endpoint extends AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                     synchronized (writeCompletionHandler) {
                         if (nBytes.longValue() < 0) {
                             failed(new EOFException(sm.getString("iob.failedwrite")), attachment);
-                        } else if (!nonBlockingWriteBuffer.isEmpty() || arrayHasData(attachment)) {
+                        } else if (!nonBlockingWriteBuffer.isEmpty() || buffersArrayHasRemaining(attachment, 0, attachment.length)) {
                             // Continue writing data using a gathering write
                             ByteBuffer[] array = nonBlockingWriteBuffer.toArray(attachment);
                             getSocket().write(array, 0, array.length,
@@ -1026,16 +1026,16 @@ public class Nio2Endpoint extends AbstractJsseEndpoint<Nio2Channel,AsynchronousS
                         synchronized (writeCompletionHandler) {
                             socketBufferHandler.configureWriteBufferForRead();
                             ByteBuffer[] array = nonBlockingWriteBuffer.toArray(socketBufferHandler.getWriteBuffer());
-                            if (arrayHasData(array)) {
+                            if (buffersArrayHasRemaining(array, 0, array.length)) {
                                 getSocket().write(array, 0, array.length, timeout, unit,
                                         array, new CompletionHandler<Long, ByteBuffer[]>() {
                                             @Override
                                             public void completed(Long nBytes, ByteBuffer[] buffers) {
                                                 if (nBytes.longValue() < 0) {
                                                     failed(new EOFException(), null);
-                                                } else if (arrayHasData(buffers)) {
-                                                    getSocket().write(array, 0, array.length, toTimeout(getWriteTimeout()),
-                                                            TimeUnit.MILLISECONDS, array, this);
+                                                } else if (buffersArrayHasRemaining(buffers, 0, buffers.length)) {
+                                                    getSocket().write(buffers, 0, buffers.length, toTimeout(getWriteTimeout()),
+                                                            TimeUnit.MILLISECONDS, buffers, this);
                                                 } else {
                                                     // Continue until everything is written
                                                     process();
diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java
index af17c3b..66f941c 100644
--- a/java/org/apache/tomcat/util/net/NioEndpoint.java
+++ b/java/org/apache/tomcat/util/net/NioEndpoint.java
@@ -1543,7 +1543,7 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
                                     updateLastWrite();
                                 }
                             }
-                            if (nBytes != 0 || !arrayHasData(buffers)) {
+                            if (nBytes != 0 || !buffersArrayHasRemaining(buffers, offset, length)) {
                                 completionDone = false;
                             }
                         }
@@ -1551,7 +1551,7 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
                         setError(e);
                     }
                 }
-                if (nBytes > 0 || (nBytes == 0 && !arrayHasData(buffers))) {
+                if (nBytes > 0 || (nBytes == 0 && !buffersArrayHasRemaining(buffers, offset, length))) {
                     // The bytes processed are only updated in the completion handler
                     completion.completed(Long.valueOf(nBytes), this);
                 } else if (nBytes < 0 || getError() != null) {
diff --git a/java/org/apache/tomcat/util/net/SocketWrapperBase.java b/java/org/apache/tomcat/util/net/SocketWrapperBase.java
index 383d96c..91e245e 100644
--- a/java/org/apache/tomcat/util/net/SocketWrapperBase.java
+++ b/java/org/apache/tomcat/util/net/SocketWrapperBase.java
@@ -1475,9 +1475,9 @@ public abstract class SocketWrapperBase<E> {
         return max;
     }
 
-    protected static boolean arrayHasData(ByteBuffer[] byteBuffers) {
-        for (ByteBuffer byteBuffer : byteBuffers) {
-            if (byteBuffer.hasRemaining()) {
+    protected static boolean buffersArrayHasRemaining(ByteBuffer[] buffers, int offset, int length) {
+        for (int pos = offset; pos < offset + length; pos++) {
+            if (buffers[pos].hasRemaining()) {
                 return true;
             }
         }
diff --git a/test/org/apache/coyote/http11/upgrade/TestUpgradeInternalHandler.java b/test/org/apache/coyote/http11/upgrade/TestUpgradeInternalHandler.java
index 7619c45..41a6511 100644
--- a/test/org/apache/coyote/http11/upgrade/TestUpgradeInternalHandler.java
+++ b/test/org/apache/coyote/http11/upgrade/TestUpgradeInternalHandler.java
@@ -39,7 +39,6 @@ import javax.servlet.http.HttpUpgradeHandler;
 import javax.servlet.http.WebConnection;
 
 import org.junit.Assert;
-import org.junit.Assume;
 import org.junit.Test;
 
 import static org.apache.catalina.startup.SimpleHttpClient.CRLF;
@@ -59,10 +58,6 @@ public class TestUpgradeInternalHandler extends TomcatBaseTest {
 
     @Test
     public void testUpgradeInternal() throws Exception {
-        Assume.assumeTrue(
-                "Only supported on NIO X",
-                getTomcatInstance().getConnector().getProtocolHandlerClassName().contains("Nio"));
-
         UpgradeConnection uc = doUpgrade(EchoAsync.class);
         PrintWriter pw = new PrintWriter(uc.getWriter());
         BufferedReader reader = uc.getReader();
@@ -227,6 +222,17 @@ public class TestUpgradeInternalHandler extends TomcatBaseTest {
                 }
             }, buffer);
             System.out.println("CompletionState: " + state);
+            // Test zero length write used by websockets
+            state = wrapper.write(BlockingMode.BLOCK, 10, TimeUnit.SECONDS, null, SocketWrapperBase.COMPLETE_WRITE_WITH_COMPLETION, new CompletionHandler<Long, Void>() {
+                @Override
+                public void completed(Long result, Void attachment) {
+                    System.out.println("Write: " + result.longValue());
+                }
+                @Override
+                public void failed(Throwable exc, Void attachment) {
+                    exc.printStackTrace();
+                }
+            }, buffer);
         }
 
         @Override


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