From 45afde3f9a51fd9dc5ade04bf0e9649bd6eec267 Mon Sep 17 00:00:00 2001 From: Gaojie Liu Date: Wed, 22 Jun 2022 11:26:54 -0700 Subject: [PATCH] HTTPCLIENT-2222: fixed ProtocolNegotiationException Aligned with the implementation of the latest release to get rid of ProtocolNegotiationException: https://github.com/apache/httpcomponents-core/blob/rel/v5.2-beta2/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/ClientH2PrefaceHandler.java#L142 --- .../http2/impl/nio/H2OnlyClientProtocolNegotiator.java | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2OnlyClientProtocolNegotiator.java b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2OnlyClientProtocolNegotiator.java index 9808bfbb80..367e240011 100644 --- a/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2OnlyClientProtocolNegotiator.java +++ b/httpcore5-h2/src/main/java/org/apache/hc/core5/http2/impl/nio/H2OnlyClientProtocolNegotiator.java @@ -36,6 +36,7 @@ import org.apache.hc.core5.concurrent.FutureCallback; import org.apache.hc.core5.http.HttpVersion; import org.apache.hc.core5.http.ProtocolVersion; +import org.apache.hc.core5.http.impl.nio.BufferedData; import org.apache.hc.core5.http2.ssl.ApplicationProtocol; import org.apache.hc.core5.reactor.IOSession; import org.apache.hc.core5.reactor.ProtocolIOSession; @@ -58,6 +59,7 @@ public class H2OnlyClientProtocolNegotiator extends ProtocolNegotiatorBase { private final AtomicBoolean initialized; private volatile ByteBuffer preface; + private volatile BufferedData inBuf; public H2OnlyClientProtocolNegotiator( final ProtocolIOSession ioSession, @@ -104,7 +106,8 @@ private void writeOutPreface(final IOSession session) throws IOException { } if (!preface.hasRemaining()) { session.clearEvent(SelectionKey.OP_WRITE); - startProtocol(HttpVersion.HTTP_2, new ClientH2IOEventHandler(http2StreamHandlerFactory.create(ioSession)), null); + final ByteBuffer data = inBuf != null ? inBuf.data() : null; + startProtocol(HttpVersion.HTTP_2, new ClientH2IOEventHandler(http2StreamHandlerFactory.create(ioSession)), data); preface = null; } } @@ -131,7 +134,10 @@ public void outputReady(final IOSession session) throws IOException { @Override public void inputReady(final IOSession session, final ByteBuffer src) throws IOException { if (src != null) { - throw new ProtocolNegotiationException("Unexpected input"); + if (inBuf == null) { + inBuf = BufferedData.allocate(src.remaining()); + } + inBuf.put(src); } if (preface != null) { writeOutPreface(session);