You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2020/10/29 14:45:35 UTC

[httpcomponents-core] 02/02: Revised HTTP protocol negotiation for non-blocking I/O sessions

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

olegk pushed a commit to branch tls_upgrade_redesign
in repository https://gitbox.apache.org/repos/asf/httpcomponents-core.git

commit 445c6c6d835fc55b1203f65be1730f1955289a21
Author: Oleg Kalnichevski <ol...@apache.org>
AuthorDate: Thu Oct 29 15:43:45 2020 +0100

    Revised HTTP protocol negotiation for non-blocking I/O sessions
---
 .../impl/nio/ClientHttpProtocolNegotiator.java     | 52 +++++++++++++++-------
 .../impl/nio/ProtocolNegotiationException.java     | 49 ++++++++++++++++++++
 .../impl/nio/ServerHttpProtocolNegotiator.java     | 16 ++++++-
 3 files changed, 98 insertions(+), 19 deletions(-)

diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java
index 30e6117..1bd8907 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientHttpProtocolNegotiator.java
@@ -32,6 +32,7 @@ import java.net.SocketAddress;
 import java.nio.ByteBuffer;
 import java.nio.channels.ByteChannel;
 import java.nio.channels.SelectionKey;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 
 import javax.net.ssl.SSLSession;
@@ -76,6 +77,7 @@ public class ClientHttpProtocolNegotiator implements HttpConnectionEventHandler
     private final ClientH2StreamMultiplexerFactory http2StreamHandlerFactory;
     private final HttpVersionPolicy versionPolicy;
     private final AtomicReference<HttpConnectionEventHandler> protocolHandlerRef;
+    private final AtomicBoolean initialized;
 
     private volatile ByteBuffer preface;
     private volatile BufferedData inBuf;
@@ -90,6 +92,7 @@ public class ClientHttpProtocolNegotiator implements HttpConnectionEventHandler
         this.http2StreamHandlerFactory = Args.notNull(http2StreamHandlerFactory, "HTTP/2 stream handler factory");
         this.versionPolicy = versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE;
         this.protocolHandlerRef = new AtomicReference<>(null);
+        this.initialized = new AtomicBoolean();
     }
 
     private void startHttp1(final IOSession session) {
@@ -126,21 +129,7 @@ public class ClientHttpProtocolNegotiator implements HttpConnectionEventHandler
         }
     }
 
-    private void writeOutPreface(final IOSession session) throws IOException {
-        if (preface.hasRemaining()) {
-            final ByteChannel channel = session;
-            channel.write(preface);
-        }
-        if (!preface.hasRemaining()) {
-            session.clearEvent(SelectionKey.OP_WRITE);
-            startHttp2(session);
-        } else {
-            session.setEvent(SelectionKey.OP_WRITE);
-        }
-    }
-
-    @Override
-    public void connected(final IOSession session) throws IOException {
+    private void initialize(final IOSession session) throws IOException {
         switch (versionPolicy) {
             case NEGOTIATE:
                 final TlsDetails tlsDetails = ioSession.getTlsDetails();
@@ -158,6 +147,28 @@ public class ClientHttpProtocolNegotiator implements HttpConnectionEventHandler
         if (preface == null) {
             startHttp1(session);
         } else {
+            session.setEvent(SelectionKey.OP_WRITE);
+        }
+    }
+
+    private void writeOutPreface(final IOSession session) throws IOException {
+        if (preface.hasRemaining()) {
+            final ByteChannel channel = session;
+            channel.write(preface);
+        }
+        if (!preface.hasRemaining()) {
+            session.clearEvent(SelectionKey.OP_WRITE);
+            startHttp2(session);
+            preface = null;
+        }
+    }
+
+    @Override
+    public void connected(final IOSession session) throws IOException {
+        if (initialized.compareAndSet(false, true)) {
+            initialize(session);
+        }
+        if (preface != null) {
             writeOutPreface(session);
         }
     }
@@ -170,15 +181,22 @@ public class ClientHttpProtocolNegotiator implements HttpConnectionEventHandler
             }
             inBuf.put(src);
         }
-        outputReady(session);
+        if (preface != null) {
+            writeOutPreface(session);
+        } else {
+            throw new ProtocolNegotiationException("Unexpected input");
+        }
     }
 
     @Override
     public void outputReady(final IOSession session) throws IOException {
+        if (initialized.compareAndSet(false, true)) {
+            initialize(session);
+        }
         if (preface != null) {
             writeOutPreface(session);
         } else {
-            session.close(CloseMode.GRACEFUL);
+            throw new ProtocolNegotiationException("Unexpected output");
         }
     }
 
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ProtocolNegotiationException.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ProtocolNegotiationException.java
new file mode 100644
index 0000000..b984db2
--- /dev/null
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ProtocolNegotiationException.java
@@ -0,0 +1,49 @@
+/*
+ * ====================================================================
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation.  For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.hc.core5.http2.impl.nio;
+
+import java.io.IOException;
+
+/**
+ * Signals a protocol error in HTTP protocol negotiation.
+ *
+ * @since 5.1
+ */
+public class ProtocolNegotiationException extends IOException {
+
+    /**
+     * Creates a MessageConstraintException with the specified detail message.
+     *
+     * @param message The exception detail message
+     */
+    public ProtocolNegotiationException(final String message) {
+        super(message);
+    }
+
+
+}
diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiator.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiator.java
index 7bb65d2..a0eb407 100644
--- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiator.java
+++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ServerHttpProtocolNegotiator.java
@@ -30,6 +30,7 @@ package org.apache.hc.core5.http2.impl.nio;
 import java.io.IOException;
 import java.net.SocketAddress;
 import java.nio.ByteBuffer;
+import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
 
 import javax.net.ssl.SSLSession;
@@ -74,6 +75,7 @@ public class ServerHttpProtocolNegotiator implements HttpConnectionEventHandler
     private final HttpVersionPolicy versionPolicy;
     private final BufferedData inBuf;
     private final AtomicReference<HttpConnectionEventHandler> protocolHandlerRef;
+    private final AtomicBoolean initialized;
 
     private volatile boolean expectValidH2Preface;
 
@@ -88,10 +90,10 @@ public class ServerHttpProtocolNegotiator implements HttpConnectionEventHandler
         this.versionPolicy = versionPolicy != null ? versionPolicy : HttpVersionPolicy.NEGOTIATE;
         this.inBuf = BufferedData.allocate(1024);
         this.protocolHandlerRef = new AtomicReference<>(null);
+        this.initialized = new AtomicBoolean();
     }
 
-    @Override
-    public void connected(final IOSession session) {
+    private void initialize(final IOSession session) {
         try {
             final TlsDetails tlsDetails = ioSession.getTlsDetails();
             switch (versionPolicy) {
@@ -123,6 +125,13 @@ public class ServerHttpProtocolNegotiator implements HttpConnectionEventHandler
     }
 
     @Override
+    public void connected(final IOSession session) {
+        if (initialized.compareAndSet(false, true)) {
+            initialize(session);
+        }
+    }
+
+    @Override
     public void inputReady(final IOSession session, final ByteBuffer src) {
         try {
             if (src != null) {
@@ -178,6 +187,9 @@ public class ServerHttpProtocolNegotiator implements HttpConnectionEventHandler
 
     @Override
     public void outputReady(final IOSession session) {
+        if (initialized.compareAndSet(false, true)) {
+            initialize(session);
+        }
     }
 
     @Override