Skip to content

Commit

Permalink
HTTPCLIENT-2222: fixed ProtocolNegotiationException
Browse files Browse the repository at this point in the history
  • Loading branch information
gaojieliu authored and ok2c committed Jun 23, 2022
1 parent c6dfb03 commit 45afde3
Showing 1 changed file with 8 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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,
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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);
Expand Down

0 comments on commit 45afde3

Please sign in to comment.