You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@tomcat.apache.org by ma...@apache.org on 2022/02/17 23:56:37 UTC

[tomcat] branch 8.5.x updated (ccefd00 -> 03b87ca)

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

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


    from ccefd00  Align with 9.0.x onwards - renames
     new c91844b  Align with 9.0.x - formatting - no functional change
     new 03b87ca  Align with 9.0.x onwards - refactor PollerEvent.run

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.


Summary of changes:
 java/org/apache/tomcat/util/net/NioEndpoint.java   | 94 ++++++++++++----------
 .../apache/tomcat/util/net/SecureNioChannel.java   | 65 +++++++--------
 2 files changed, 83 insertions(+), 76 deletions(-)

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


[tomcat] 01/02: Align with 9.0.x - formatting - no functional change

Posted by ma...@apache.org.
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

commit c91844b548708293e35416e38b2fe8df8df7955e
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Feb 17 23:28:56 2022 +0000

    Align with 9.0.x - formatting - no functional change
---
 .../apache/tomcat/util/net/SecureNioChannel.java   | 65 +++++++++++-----------
 1 file changed, 31 insertions(+), 34 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/SecureNioChannel.java b/java/org/apache/tomcat/util/net/SecureNioChannel.java
index 7ef3528..e2ae886 100644
--- a/java/org/apache/tomcat/util/net/SecureNioChannel.java
+++ b/java/org/apache/tomcat/util/net/SecureNioChannel.java
@@ -46,7 +46,7 @@ import org.apache.tomcat.util.res.StringManager;
 /**
  * Implementation of a secure socket channel
  */
-public class SecureNioChannel extends NioChannel  {
+public class SecureNioChannel extends NioChannel {
 
     private static final Log log = LogFactory.getLog(SecureNioChannel.class);
     private static final StringManager sm = StringManager.getManager(SecureNioChannel.class);
@@ -55,6 +55,8 @@ public class SecureNioChannel extends NioChannel  {
     // various scenarios
     private static final int DEFAULT_NET_BUFFER_SIZE = 16921;
 
+    private final NioEndpoint endpoint;
+
     protected ByteBuffer netInBuffer;
     protected ByteBuffer netOutBuffer;
 
@@ -71,7 +73,6 @@ public class SecureNioChannel extends NioChannel  {
     private final Map<String,List<String>> additionalTlsAttributes = new HashMap<>();
 
     protected NioSelectorPool pool;
-    private final NioEndpoint endpoint;
 
     public SecureNioChannel(SocketChannel channel, SocketBufferHandler bufHandler,
             NioSelectorPool pool, NioEndpoint endpoint) {
@@ -184,20 +185,18 @@ public class SecureNioChannel extends NioChannel  {
             }
         }
 
-        if (!flush(netOutBuffer))
-         {
+        if (!flush(netOutBuffer)) {
             return SelectionKey.OP_WRITE; //we still have data to write
         }
 
         SSLEngineResult handshake = null;
 
         while (!handshakeComplete) {
-            switch ( handshakeStatus ) {
-                case NOT_HANDSHAKING: {
+            switch (handshakeStatus) {
+                case NOT_HANDSHAKING:
                     //should never happen
                     throw new IOException(sm.getString("channel.nio.ssl.notHandshaking"));
-                }
-                case FINISHED: {
+                case FINISHED:
                     if (endpoint.hasNegotiableProtocols()) {
                         if (sslEngine instanceof SSLUtil.ProtocolInfo) {
                             socketWrapper.setNegotiatedProtocol(
@@ -210,9 +209,8 @@ public class SecureNioChannel extends NioChannel  {
                     //we are complete if we have delivered the last package
                     handshakeComplete = !netOutBuffer.hasRemaining();
                     //return 0 if we are complete, otherwise we still have data to write
-                    return handshakeComplete?0:SelectionKey.OP_WRITE;
-                }
-                case NEED_WRAP: {
+                    return handshakeComplete ? 0 : SelectionKey.OP_WRITE;
+                case NEED_WRAP:
                     //perform the wrap function
                     try {
                         handshake = handshakeWrap(write);
@@ -233,18 +231,17 @@ public class SecureNioChannel extends NioChannel  {
                         //wrap should always work with our buffers
                         throw new IOException(sm.getString("channel.nio.ssl.unexpectedStatusDuringWrap", handshake.getStatus()));
                     }
-                    if ( handshakeStatus != HandshakeStatus.NEED_UNWRAP || (!flush(netOutBuffer)) ) {
+                    if (handshakeStatus != HandshakeStatus.NEED_UNWRAP || (!flush(netOutBuffer))) {
                         //should actually return OP_READ if we have NEED_UNWRAP
                         return SelectionKey.OP_WRITE;
                     }
                     //fall down to NEED_UNWRAP on the same call, will result in a
                     //BUFFER_UNDERFLOW if it needs data
-                }
                 //$FALL-THROUGH$
-                case NEED_UNWRAP: {
+                case NEED_UNWRAP:
                     //perform the unwrap function
                     handshake = handshakeUnwrap(read);
-                    if ( handshake.getStatus() == Status.OK ) {
+                    if (handshake.getStatus() == Status.OK) {
                         if (handshakeStatus == HandshakeStatus.NEED_TASK) {
                             handshakeStatus = tasks();
                         }
@@ -253,14 +250,13 @@ public class SecureNioChannel extends NioChannel  {
                         return SelectionKey.OP_READ;
                     } else {
                         throw new IOException(sm.getString("channel.nio.ssl.unexpectedStatusDuringWrap", handshake.getStatus()));
-                    }//switch
+                    }
                     break;
-                }
-                case NEED_TASK: {
+                case NEED_TASK:
                     handshakeStatus = tasks();
                     break;
-                }
-                default: throw new IllegalStateException(sm.getString("channel.nio.ssl.invalidStatus", handshakeStatus));
+                default:
+                    throw new IllegalStateException(sm.getString("channel.nio.ssl.invalidStatus", handshakeStatus));
             }
         }
         // Handshake is complete if this point is reached
@@ -379,10 +375,10 @@ public class SecureNioChannel extends NioChannel  {
     @SuppressWarnings("null") // key cannot be null
     public void rehandshake(long timeout) throws IOException {
         //validate the network buffers are empty
-        if (netInBuffer.position() > 0 && netInBuffer.position()<netInBuffer.limit()) {
+        if (netInBuffer.position() > 0 && netInBuffer.position() < netInBuffer.limit()) {
             throw new IOException(sm.getString("channel.nio.ssl.netInputNotEmpty"));
         }
-        if (netOutBuffer.position() > 0 && netOutBuffer.position()<netOutBuffer.limit()) {
+        if (netOutBuffer.position() > 0 && netOutBuffer.position() < netOutBuffer.limit()) {
             throw new IOException(sm.getString("channel.nio.ssl.netOutputNotEmpty"));
         }
         if (!getBufHandler().isReadBufferEmpty()) {
@@ -403,11 +399,14 @@ public class SecureNioChannel extends NioChannel  {
             while (handshaking) {
                 int hsStatus = this.handshake(isReadable, isWritable);
                 switch (hsStatus) {
-                    case -1 : throw new EOFException(sm.getString("channel.nio.ssl.eofDuringHandshake"));
-                    case  0 : handshaking = false; break;
-                    default : {
+                    case -1 :
+                        throw new EOFException(sm.getString("channel.nio.ssl.eofDuringHandshake"));
+                    case  0 :
+                        handshaking = false;
+                        break;
+                    default :
                         long now = System.currentTimeMillis();
-                        if (selector==null) {
+                        if (selector == null) {
                             selector = Selector.open();
                             key = getIOChannel().register(selector, hsStatus);
                         } else {
@@ -419,7 +418,6 @@ public class SecureNioChannel extends NioChannel  {
                         }
                         isReadable = key.isReadable();
                         isWritable = key.isWritable();
-                    }
                 }
             }
         } catch (IOException x) {
@@ -515,15 +513,15 @@ public class SecureNioChannel extends NioChannel  {
             netInBuffer.compact();
             //read in the status
             handshakeStatus = result.getHandshakeStatus();
-            if ( result.getStatus() == SSLEngineResult.Status.OK &&
-                 result.getHandshakeStatus() == HandshakeStatus.NEED_TASK ) {
+            if (result.getStatus() == SSLEngineResult.Status.OK &&
+                 result.getHandshakeStatus() == HandshakeStatus.NEED_TASK) {
                 //execute tasks if we need to
                 handshakeStatus = tasks();
             }
             //perform another unwrap?
             cont = result.getStatus() == SSLEngineResult.Status.OK &&
                    handshakeStatus == HandshakeStatus.NEED_UNWRAP;
-        }while ( cont );
+        } while (cont);
         return result;
     }
 
@@ -670,9 +668,8 @@ public class SecureNioChannel extends NioChannel  {
                         // This is the normal case for this code
                         getBufHandler().expand(sslEngine.getSession().getApplicationBufferSize());
                         dst = getBufHandler().getReadBuffer();
-                    } else if (dst == getAppReadBufHandler().getByteBuffer()) {
-                        getAppReadBufHandler()
-                                .expand(sslEngine.getSession().getApplicationBufferSize());
+                    } else if (getAppReadBufHandler() != null && dst == getAppReadBufHandler().getByteBuffer()) {
+                        getAppReadBufHandler().expand(sslEngine.getSession().getApplicationBufferSize());
                         dst = getAppReadBufHandler().getByteBuffer();
                     } else {
                         // Can't expand the buffer as there is no way to signal
@@ -902,7 +899,7 @@ public class SecureNioChannel extends NioChannel  {
     public boolean flushOutbound() throws IOException {
         int remaining = netOutBuffer.remaining();
         flush(netOutBuffer);
-        int remaining2= netOutBuffer.remaining();
+        int remaining2 = netOutBuffer.remaining();
         return remaining2 < remaining;
     }
 

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


[tomcat] 02/02: Align with 9.0.x onwards - refactor PollerEvent.run

Posted by ma...@apache.org.
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

commit 03b87cadd567e4af0d3f9e090e91ccc204c63ddd
Author: Mark Thomas <ma...@apache.org>
AuthorDate: Thu Feb 17 23:56:24 2022 +0000

    Align with 9.0.x onwards - refactor PollerEvent.run
---
 java/org/apache/tomcat/util/net/NioEndpoint.java | 94 +++++++++++++-----------
 1 file changed, 52 insertions(+), 42 deletions(-)

diff --git a/java/org/apache/tomcat/util/net/NioEndpoint.java b/java/org/apache/tomcat/util/net/NioEndpoint.java
index 4c6f244..5aae3f0 100644
--- a/java/org/apache/tomcat/util/net/NioEndpoint.java
+++ b/java/org/apache/tomcat/util/net/NioEndpoint.java
@@ -638,11 +638,11 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
     /**
      * PollerEvent, cacheable object for poller events to avoid GC
      */
-    public static class PollerEvent implements Runnable {
+    public static class PollerEvent {
 
         private NioChannel socket;
-        private int interestOps;
         private NioSocketWrapper socketWrapper;
+        private int interestOps;
 
         public PollerEvent(NioChannel ch, NioSocketWrapper w, int intOps) {
             reset(ch, w, intOps);
@@ -654,47 +654,20 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
             socketWrapper = w;
         }
 
-        public void reset() {
-            reset(null, null, 0);
+        public NioSocketWrapper getSocketWrapper() {
+            return socketWrapper;
         }
 
-        @Override
-        public void run() {
-            if (interestOps == OP_REGISTER) {
-                try {
-                    socket.getIOChannel().register(
-                            socket.getPoller().getSelector(), SelectionKey.OP_READ, socketWrapper);
-                } catch (Exception x) {
-                    log.error(sm.getString("endpoint.nio.registerFail"), x);
-                }
-            } else {
-                final SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
-                try {
-                    if (key == null) {
-                        // The key was cancelled (e.g. due to socket closure)
-                        // and removed from the selector while it was being
-                        // processed. Count down the connections at this point
-                        // since it won't have been counted down when the socket
-                        // closed.
-                        socket.socketWrapper.getEndpoint().countDownConnection();
-                        ((NioSocketWrapper) socket.socketWrapper).closed = true;
-                    } else {
-                        final NioSocketWrapper socketWrapper = (NioSocketWrapper) key.attachment();
-                        if (socketWrapper != null) {
-                            //we are registering the key to start with, reset the fairness counter.
-                            int ops = key.interestOps() | interestOps;
-                            socketWrapper.interestOps(ops);
-                            key.interestOps(ops);
-                        } else {
-                            socket.getPoller().cancelledKey(key);
-                        }
-                    }
-                } catch (CancelledKeyException ckx) {
-                    try {
-                        socket.getPoller().cancelledKey(key);
-                    } catch (Exception ignore) {}
-                }
-            }
+        public NioChannel getSocket() {
+            return socket;
+        }
+
+        public int getInterestOps() {
+            return interestOps;
+        }
+
+        public void reset() {
+            reset(null, null, 0);
         }
 
         @Override
@@ -786,8 +759,45 @@ public class NioEndpoint extends AbstractJsseEndpoint<NioChannel,SocketChannel>
             PollerEvent pe = null;
             for (int i = 0, size = events.size(); i < size && (pe = events.poll()) != null; i++ ) {
                 result = true;
+                NioSocketWrapper socketWrapper = pe.getSocketWrapper();
+                NioChannel socket = pe.getSocket();
+                int interestOps = pe.getInterestOps();
                 try {
-                    pe.run();
+                    if (interestOps == OP_REGISTER) {
+                        try {
+                            socket.getIOChannel().register(
+                                    socket.getPoller().getSelector(), SelectionKey.OP_READ, socketWrapper);
+                        } catch (Exception x) {
+                            log.error(sm.getString("endpoint.nio.registerFail"), x);
+                        }
+                    } else {
+                        final SelectionKey key = socket.getIOChannel().keyFor(socket.getPoller().getSelector());
+                        try {
+                            if (key == null) {
+                                // The key was cancelled (e.g. due to socket closure)
+                                // and removed from the selector while it was being
+                                // processed. Count down the connections at this point
+                                // since it won't have been counted down when the socket
+                                // closed.
+                                socket.socketWrapper.getEndpoint().countDownConnection();
+                                ((NioSocketWrapper) socket.socketWrapper).closed = true;
+                            } else {
+                                final NioSocketWrapper attachment = (NioSocketWrapper) key.attachment();
+                                if (attachment != null) {
+                                    //we are registering the key to start with, reset the fairness counter.
+                                    int ops = key.interestOps() | interestOps;
+                                    attachment.interestOps(ops);
+                                    key.interestOps(ops);
+                                } else {
+                                    socket.getPoller().cancelledKey(key);
+                                }
+                            }
+                        } catch (CancelledKeyException ckx) {
+                            try {
+                                socket.getPoller().cancelledKey(key);
+                            } catch (Exception ignore) {}
+                        }
+                    }
                     if (running && eventCache != null) {
                         pe.reset();
                         eventCache.push(pe);

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