/**
* Not really part of the public API, but is used by the HTTP client to initiate a SPDY connection for HTTPS requests.
*/
public static void handlePotentialSpdyConnection(final StreamConnection connection, final ClientCallback<ClientConnection> listener, final Pool<ByteBuffer> bufferPool, final OptionMap options, final ChannelListener<SslConnection> spdyFailedListener) {
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(SPDY_3) || existing.equals(SPDY_3_1)) {
listener.completed(createSpdyChannel(connection, bufferPool));
} else {
sslConnection.getSourceChannel().suspendReads();
spdyFailedListener.handleEvent(sslConnection);
}
} else {
final SpdySelectionProvider spdySelectionProvider = new SpdySelectionProvider(sslEngine);
try {
NPN_PUT_METHOD.invoke(null, sslEngine, spdySelectionProvider);
} catch (Exception e) {
spdyFailedListener.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();
spdyFailedListener.handleEvent(sslConnection);
return;
} else if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
listener.completed(createSpdyChannel(connection, bufferPool));
}
} 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();
spdyFailedListener.handleEvent(sslConnection);
return;
} else if (spdySelectionProvider.selected != null) {
//we have spdy
if (spdySelectionProvider.selected.equals(SPDY_3) || spdySelectionProvider.selected.equals(SPDY_3_1)) {
listener.completed(createSpdyChannel(connection, bufferPool));
}
}
} catch (IOException e) {
listener.failed(e);
}
}
}
});
sslConnection.getSourceChannel().resumeReads();
} catch (IOException e) {
listener.failed(e);
}
}