use of org.xnio.StreamConnection in project undertow by undertow-io.
the class HttpClientConnection method doHttp2Upgrade.
protected void doHttp2Upgrade() {
try {
StreamConnection connectedStreamChannel = this.performUpgrade();
Http2Channel http2Channel = new Http2Channel(connectedStreamChannel, null, bufferPool, null, true, true, options);
Http2ClientConnection http2ClientConnection = new Http2ClientConnection(http2Channel, currentRequest.getResponseCallback(), currentRequest.getRequest(), currentRequest.getRequest().getRequestHeaders().getFirst(Headers.HOST), clientStatistics, false);
http2ClientConnection.getCloseSetter().set(new ChannelListener<ClientConnection>() {
@Override
public void handleEvent(ClientConnection channel) {
ChannelListeners.invokeChannelListener(HttpClientConnection.this, HttpClientConnection.this.closeSetter.get());
}
});
http2Delegate = http2ClientConnection;
//make sure the read listener is immediately invoked, as it may not happen if data is pushed back
connectedStreamChannel.getSourceChannel().wakeupReads();
currentRequest = null;
} catch (IOException e) {
UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
safeClose(this);
}
}
use of org.xnio.StreamConnection in project undertow by undertow-io.
the class Http2ClearClientProvider method connect.
@Override
public void connect(final ClientCallback<ClientConnection> listener, final InetSocketAddress bindAddress, final URI uri, final XnioIoThread ioThread, final XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap options) {
final URI upgradeUri;
try {
upgradeUri = new URI("http", uri.getUserInfo(), uri.getHost(), uri.getPort(), uri.getPath(), uri.getQuery(), uri.getFragment());
} catch (URISyntaxException e) {
listener.failed(new IOException(e));
return;
}
if (bindAddress != null) {
ioThread.openStreamConnection(bindAddress, new InetSocketAddress(uri.getHost(), uri.getPort()), new ChannelListener<StreamConnection>() {
@Override
public void handleEvent(StreamConnection channel) {
Map<String, String> headers = createHeaders(options, bufferPool, uri);
HttpUpgrade.performUpgrade(channel, upgradeUri, headers, new Http2ClearOpenListener(bufferPool, options, listener, uri.getHost()), null).addNotifier(new FailedNotifier(listener), null);
}
}, new ChannelListener<BoundChannel>() {
@Override
public void handleEvent(BoundChannel channel) {
}
}, options).addNotifier(new FailedNotifier(listener), null);
} else {
ioThread.openStreamConnection(new InetSocketAddress(uri.getHost(), uri.getPort()), new ChannelListener<StreamConnection>() {
@Override
public void handleEvent(StreamConnection channel) {
Map<String, String> headers = createHeaders(options, bufferPool, uri);
HttpUpgrade.performUpgrade(channel, upgradeUri, headers, new Http2ClearOpenListener(bufferPool, options, listener, uri.getHost()), null).addNotifier(new FailedNotifier(listener), null);
}
}, new ChannelListener<BoundChannel>() {
@Override
public void handleEvent(BoundChannel channel) {
}
}, options).addNotifier(new FailedNotifier(listener), null);
}
}
use of org.xnio.StreamConnection in project undertow by undertow-io.
the class PipeliningBufferingStreamSinkConduit method performFlush.
void performFlush(final HttpServerExchange exchange, final HttpServerConnection connection) {
try {
final HttpServerConnection.ConduitState oldState = connection.resetChannel();
if (!flushPipelinedData()) {
final StreamConnection channel = connection.getChannel();
channel.getSinkChannel().setWriteListener(new ChannelListener<Channel>() {
@Override
public void handleEvent(Channel c) {
try {
if (flushPipelinedData()) {
channel.getSinkChannel().setWriteListener(null);
channel.getSinkChannel().suspendWrites();
connection.restoreChannel(oldState);
connection.getReadListener().exchangeComplete(exchange);
}
} catch (IOException e) {
UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
IoUtils.safeClose(channel);
}
}
});
connection.getChannel().getSinkChannel().resumeWrites();
return;
} else {
connection.restoreChannel(oldState);
connection.getReadListener().exchangeComplete(exchange);
}
} catch (IOException e) {
UndertowLogger.REQUEST_IO_LOGGER.ioException(e);
IoUtils.safeClose(connection.getChannel());
}
}
use of org.xnio.StreamConnection in project undertow by undertow-io.
the class Http2UpgradeHandler method handleRequest.
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
final String upgrade = exchange.getRequestHeaders().getFirst(Headers.UPGRADE);
if (upgrade != null && upgradeStrings.contains(upgrade)) {
String settings = exchange.getRequestHeaders().getFirst("HTTP2-Settings");
if (settings != null) {
//required by spec
final ByteBuffer settingsFrame = FlexBase64.decodeURL(settings);
exchange.getResponseHeaders().put(Headers.UPGRADE, upgrade);
exchange.upgradeChannel(new HttpUpgradeListener() {
@Override
public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
OptionMap undertowOptions = exchange.getConnection().getUndertowOptions();
Http2Channel channel = new Http2Channel(streamConnection, upgrade, exchange.getConnection().getByteBufferPool(), null, false, true, true, settingsFrame, undertowOptions);
Http2ReceiveListener receiveListener = new Http2ReceiveListener(new HttpHandler() {
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
//as the request was only to create the initial request
if (exchange.getRequestHeaders().contains("X-HTTP2-connect-only")) {
exchange.endExchange();
return;
}
exchange.setProtocol(Protocols.HTTP_2_0);
next.handleRequest(exchange);
}
}, undertowOptions, exchange.getConnection().getBufferSize(), null);
channel.getReceiveSetter().set(receiveListener);
receiveListener.handleInitialRequest(exchange, channel);
channel.resumeReceives();
}
});
return;
}
}
next.handleRequest(exchange);
}
use of org.xnio.StreamConnection in project undertow by undertow-io.
the class WebSocketProtocolHandshakeHandler method handleRequest.
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
if (!exchange.getRequestMethod().equals(Methods.GET)) {
// Only GET is supported to start the handshake
next.handleRequest(exchange);
return;
}
final AsyncWebSocketHttpServerExchange facade = new AsyncWebSocketHttpServerExchange(exchange, peerConnections);
Handshake handshaker = null;
for (Handshake method : handshakes) {
if (method.matches(facade)) {
handshaker = method;
break;
}
}
if (handshaker == null) {
next.handleRequest(exchange);
} else {
WebSocketLogger.REQUEST_LOGGER.debugf("Attempting websocket handshake with %s on %s", handshaker, exchange);
final Handshake selected = handshaker;
if (upgradeListener == null) {
exchange.upgradeChannel(new HttpUpgradeListener() {
@Override
public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
WebSocketChannel channel = selected.createChannel(facade, streamConnection, facade.getBufferPool());
peerConnections.add(channel);
callback.onConnect(facade, channel);
}
});
} else {
exchange.upgradeChannel(upgradeListener);
}
handshaker.handshake(facade);
}
}
Aggregations