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 20:59:48 UTC
[tomcat] branch master 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 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 d24aade Add offset to the buffer remaining method
d24aade is described below
commit d24aade0fac3fba192c52cc1fb6ab1ffa364ebb6
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 2175667..2ed8ae4 100644
--- a/java/org/apache/tomcat/util/net/Nio2Endpoint.java
+++ b/java/org/apache/tomcat/util/net/Nio2Endpoint.java
@@ -683,7 +683,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,
@@ -1027,16 +1027,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 773b4d6..bbcf4ca 100644
--- a/java/org/apache/tomcat/util/net/NioEndpoint.java
+++ b/java/org/apache/tomcat/util/net/NioEndpoint.java
@@ -1457,7 +1457,7 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
updateLastWrite();
}
}
- if (nBytes != 0 || !arrayHasData(buffers)) {
+ if (nBytes != 0 || !buffersArrayHasRemaining(buffers, offset, length)) {
completionDone = false;
}
}
@@ -1465,7 +1465,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 40ccd5d..0658ea6 100644
--- a/java/org/apache/tomcat/util/net/SocketWrapperBase.java
+++ b/java/org/apache/tomcat/util/net/SocketWrapperBase.java
@@ -1445,9 +1445,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 9c9421c..eb826f8 100644
--- a/test/org/apache/coyote/http11/upgrade/TestUpgradeInternalHandler.java
+++ b/test/org/apache/coyote/http11/upgrade/TestUpgradeInternalHandler.java
@@ -40,7 +40,6 @@ import jakarta.servlet.http.HttpUpgradeHandler;
import jakarta.servlet.http.WebConnection;
import org.junit.Assert;
-import org.junit.Assume;
import org.junit.Test;
import static org.apache.catalina.startup.SimpleHttpClient.CRLF;
@@ -60,10 +59,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();
@@ -228,6 +223,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