/**
* Not really part of the public API, but is used by the HTTP client to initiate a HTTP2 connection for HTTPS requests.
*/
public static void handlePotentialHttp2Connection(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final Pool<ByteBuffer> bufferPool, final OptionMap options, final ChannelListener<SslConnection> http2FailedListener) {
final SslConnection sslConnection = (SslConnection) connection;
final SSLEngine sslEngine = JsseXnioSsl.getSslEngine(sslConnection);
String existing = (String) sslEngine.getSession().getValue(PROTOCOL_KEY);
if(existing != null) {
if (existing.equals(HTTP2)) {
listener.completed(createHttp2Channel(connection, bufferPool, options));
} else {
sslConnection.getSourceChannel().suspendReads();
http2FailedListener.handleEvent(sslConnection);
}
} else {
final SpdySelectionProvider spdySelectionProvider = new SpdySelectionProvider(sslEngine);
try {
ALPN_PUT_METHOD.invoke(null, sslEngine, spdySelectionProvider);
} catch (Exception e) {
http2FailedListener.handleEvent(sslConnection);
return;
}
try {
sslConnection.startHandshake();
sslConnection.getSourceChannel().getReadSetter().set(new ChannelListener<StreamSourceChannel>() {
@Override
public void handleEvent(StreamSourceChannel channel) {
if (spdySelectionProvider.selected != null) {
if (spdySelectionProvider.selected.equals(HTTP_1_1)) {
sslConnection.getSourceChannel().suspendReads();
http2FailedListener.handleEvent(sslConnection);
return;
} else if (spdySelectionProvider.selected.equals(HTTP2)) {
listener.completed(createHttp2Channel(connection, bufferPool, options));
}
} else {
ByteBuffer buf = ByteBuffer.allocate(100);
try {
int read = channel.read(buf);
if (read > 0) {
PushBackStreamSourceConduit pb = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit());
pb.pushBack(new ImmediatePooled<>(buf));
connection.getSourceChannel().setConduit(pb);
}
if ((spdySelectionProvider.selected == null && read > 0) || HTTP_1_1.equals(spdySelectionProvider.selected)) {
sslConnection.getSourceChannel().suspendReads();
http2FailedListener.handleEvent(sslConnection);
return;
} else if (spdySelectionProvider.selected != null) {
//we have spdy
if (spdySelectionProvider.selected.equals(HTTP2)) {
listener.completed(createHttp2Channel(connection, bufferPool, options));
}
}
} catch (IOException e) {
listener.failed(e);
}
}
}
});
sslConnection.getSourceChannel().resumeReads();
} catch (IOException e) {
listener.failed(e);
} catch (Throwable e) {
listener.failed(new IOException(e));
}