@Override public void setReadReadyHandler(ReadReadyHandler handler) { delegate.getSourceChannel().getConduit().setReadReadyHandler(readReadyHandler = new SslReadReadyHandler(handler)); }
@Override protected StreamConnection upgradeChannel() { clearChannel(); if (extraBytes != null) { channel.getSourceChannel().setConduit(new ReadDataStreamSourceConduit(channel.getSourceChannel().getConduit(), this)); } return channel; }
public AbstractServerConnection(StreamConnection channel, final ByteBufferPool bufferPool, final HttpHandler rootHandler, final OptionMap undertowOptions, final int bufferSize) { this.channel = channel; this.bufferPool = bufferPool; this.rootHandler = rootHandler; this.undertowOptions = undertowOptions; this.bufferSize = bufferSize; closeSetter = new CloseSetter(); if (channel != null) { this.originalSinkConduit = channel.getSinkChannel().getConduit(); this.originalSourceConduit = channel.getSourceChannel().getConduit(); channel.setCloseListener(closeSetter); } else { this.originalSinkConduit = null; this.originalSourceConduit = null; } }
/** {@inheritDoc} */ @Override public synchronized void startHandshake() throws IOException { if (! tls) { tls = true; ((JsseSslStreamSourceConduit) getSourceChannel().getConduit()).enableTls(); ((JsseSslStreamSinkConduit) getSinkChannel().getConduit()).enableTls(); } sslConduitEngine.beginHandshake(); }
AddressWrappedConnection(StreamConnection delegate, SocketAddress source, SocketAddress dest) { super(delegate.getIoThread()); this.delegate = delegate; this.source = source; this.dest = dest; setSinkConduit(delegate.getSinkChannel().getConduit()); setSourceConduit(delegate.getSourceChannel().getConduit()); }
SslConduit(UndertowSslConnection connection, StreamConnection delegate, SSLEngine engine, ByteBufferPool bufferPool, Runnable handshakeCallback) { this.connection = connection; this.delegate = delegate; this.handshakeCallback = handshakeCallback; this.sink = delegate.getSinkChannel().getConduit(); this.source = delegate.getSourceChannel().getConduit(); this.engine = engine; this.bufferPool = bufferPool; delegate.getSourceChannel().getConduit().setReadReadyHandler(readReadyHandler = new SslReadReadyHandler(null)); delegate.getSinkChannel().getConduit().setWriteReadyHandler(writeReadyHandler = new SslWriteReadyHandler(null)); if(engine.getUseClientMode()) { state = FLAG_IN_HANDSHAKE | FLAG_READ_REQUIRES_WRITE; } else { state = FLAG_IN_HANDSHAKE | FLAG_WRITE_REQUIRES_READ; } }
/** {@inheritDoc} */ @Override protected void closeAction() throws IOException { if (tls) { try { getSinkChannel().getConduit().truncateWrites(); } catch (IOException e) { try { getSourceChannel().getConduit().terminateReads(); } catch (IOException ignored) { } safeClose(connection); throw e; } try { getSourceChannel().getConduit().terminateReads(); } catch (IOException e) { safeClose(connection); throw e; } } connection.close(); }
JsseSslConnection(final StreamConnection streamConnection, final SSLEngine engine, final Pool<ByteBuffer> socketBufferPool, final Pool<ByteBuffer> applicationBufferPool) { super(streamConnection.getIoThread()); this.streamConnection = streamConnection; conduit = new JsseStreamConduit(this, engine, streamConnection.getSourceChannel().getConduit(), streamConnection.getSinkChannel().getConduit(), socketBufferPool, applicationBufferPool); setSourceConduit(conduit); setSinkConduit(conduit); }
private void callOpenListener(StreamConnection streamConnection, final PooledByteBuffer buffer) { if (openListener instanceof DelegateOpenListener) { ((DelegateOpenListener) openListener).handleEvent(streamConnection, buffer); } else { if (buffer != null) { PushBackStreamSourceConduit conduit = new PushBackStreamSourceConduit(streamConnection.getSourceChannel().getConduit()); conduit.pushBack(new PooledAdaptor(buffer)); streamConnection.getSourceChannel().setConduit(conduit); } openListener.handleEvent(streamConnection); } }
/** * Resets the channel to its original state, effectively disabling all current conduit * wrappers. The current state is encapsulated inside a {@link ConduitState} object that * can be used the restore the channel. * * @return An opaque representation of the previous channel state */ public ConduitState resetChannel() { ConduitState ret = new ConduitState(channel.getSinkChannel().getConduit(), channel.getSourceChannel().getConduit()); channel.getSinkChannel().setConduit(originalSinkConduit); channel.getSourceChannel().setConduit(originalSourceConduit); return ret; }
private void prepareResponseChannel(ClientResponse response, ClientExchange exchange) { String encoding = response.getResponseHeaders().getLast(Headers.TRANSFER_ENCODING); boolean chunked = encoding != null && Headers.CHUNKED.equals(new HttpString(encoding)); String length = response.getResponseHeaders().getFirst(Headers.CONTENT_LENGTH); if (exchange.getRequest().getMethod().equals(Methods.HEAD)) { connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), 0, responseFinishedListener)); } else if (chunked) { connection.getSourceChannel().setConduit(new ChunkedStreamSourceConduit(connection.getSourceChannel().getConduit(), pushBackStreamSourceConduit, bufferPool, responseFinishedListener, exchange, connection)); } else if (length != null) { try { long contentLength = Long.parseLong(length); connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), contentLength, responseFinishedListener)); } catch (NumberFormatException e) { handleError(e); throw e; } } else if (response.getProtocol().equals(Protocols.HTTP_1_1) && !Connectors.isEntityBodyAllowed(response.getResponseCode())) { connection.getSourceChannel().setConduit(new FixedLengthStreamSourceConduit(connection.getSourceChannel().getConduit(), 0, responseFinishedListener)); } else { connection.getSourceChannel().setConduit(new FinishableStreamSourceConduit(connection.getSourceChannel().getConduit(), responseFinishedListener)); state |= CLOSE_REQ; } }
JsseSslStreamConnection(StreamConnection connection, SSLEngine sslEngine, final Pool<ByteBuffer> socketBufferPool, final Pool<ByteBuffer> applicationBufferPool, final boolean startTls) { super(connection.getIoThread()); this.connection = connection; final StreamSinkConduit sinkConduit = connection.getSinkChannel().getConduit(); final StreamSourceConduit sourceConduit = connection.getSourceChannel().getConduit(); sslConduitEngine = new JsseSslConduitEngine(this, sinkConduit, sourceConduit, sslEngine, socketBufferPool, applicationBufferPool); tls = ! startTls; setSinkConduit(new JsseSslStreamSinkConduit(sinkConduit, sslConduitEngine, tls)); setSourceConduit(new JsseSslStreamSourceConduit(sourceConduit, sslConduitEngine, tls)); }
public IdleTimeoutConduit(StreamConnection connection) { this.sink = connection.getSinkChannel().getConduit(); this.source = connection.getSourceChannel().getConduit(); setWriteReadyHandler(new WriteReadyHandler.ChannelListenerHandler<>(connection.getSinkChannel())); setReadReadyHandler(new ReadReadyHandler.ChannelListenerHandler<>(connection.getSourceChannel())); }
public HttpServerConnection(StreamConnection channel, final ByteBufferPool bufferPool, final HttpHandler rootHandler, final OptionMap undertowOptions, final int bufferSize, final ConnectorStatisticsImpl connectorStatistics) { super(channel, bufferPool, rootHandler, undertowOptions, bufferSize); if (channel instanceof SslChannel) { sslSessionInfo = new ConnectionSSLSessionInfo(((SslChannel) channel), this); } this.responseConduit = new HttpResponseConduit(channel.getSinkChannel().getConduit(), bufferPool, this); fixedLengthStreamSinkConduit = new ServerFixedLengthStreamSinkConduit(responseConduit, false, false); readDataStreamSourceConduit = new ReadDataStreamSourceConduit(channel.getSourceChannel().getConduit(), this); //todo: do this without an allocation addCloseListener(new CloseListener() { @Override public void closed(ServerConnection connection) { if(connectorStatistics != null) { connectorStatistics.decrementConnectionCount(); } responseConduit.freeBuffers(); } }); }
private void proxyAccept(SocketAddress source, SocketAddress dest, PooledByteBuffer additionalData) { StreamConnection streamConnection = this.streamConnection; if (source != null) { streamConnection = new AddressWrappedConnection(streamConnection, source, dest); } if (ssl != null) { //we need to apply the additional data before the SSL wrapping if (additionalData != null) { PushBackStreamSourceConduit conduit = new PushBackStreamSourceConduit(streamConnection.getSourceChannel().getConduit()); conduit.pushBack(new PooledAdaptor(additionalData)); streamConnection.getSourceChannel().setConduit(conduit); } SslConnection sslConnection = ssl.wrapExistingConnection(streamConnection, sslOptionMap == null ? OptionMap.EMPTY : sslOptionMap, false); streamConnection = sslConnection; callOpenListener(streamConnection, null); } else { callOpenListener(streamConnection, additionalData); } }
@Override public void handleEvent(StreamConnection channel) { final ClientStatisticsImpl clientStatistics; //first we set up statistics, if required if (options.get(UndertowOptions.ENABLE_STATISTICS, false)) { clientStatistics = new ClientStatisticsImpl(); channel.getSinkChannel().setConduit(new BytesSentStreamSinkConduit(channel.getSinkChannel().getConduit(), new ByteActivityCallback() { @Override public void activity(long bytes) { clientStatistics.written += bytes; } })); channel.getSourceChannel().setConduit(new BytesReceivedStreamSourceConduit(channel.getSourceChannel().getConduit(), new ByteActivityCallback() { @Override public void activity(long bytes) { clientStatistics.read += bytes; } })); } else { clientStatistics = null; } Http2Channel http2Channel = new Http2Channel(channel, null, bufferPool, null, true, true, options); Http2ClientConnection http2ClientConnection = new Http2ClientConnection(http2Channel, true, defaultHost, clientStatistics, false); listener.completed(http2ClientConnection); } }
/** * Get the inbound request. If there is no request body, calling this method * may cause the next request to immediately be processed. The {@link StreamSourceChannel#close()} or {@link StreamSourceChannel#shutdownReads()} * method must be called at some point after the request is processed to prevent resource leakage and to allow * the next request to proceed. Any unread content will be discarded. * * @return the channel for the inbound request, or {@code null} if another party already acquired the channel */ public StreamSourceChannel getRequestChannel() { if (requestChannel != null) { if(anyAreSet(state, FLAG_REQUEST_RESET)) { state &= ~FLAG_REQUEST_RESET; return requestChannel; } return null; } if (anyAreSet(state, FLAG_REQUEST_TERMINATED)) { return requestChannel = new ReadDispatchChannel(new ConduitStreamSourceChannel(Configurable.EMPTY, new EmptyStreamSourceConduit(getIoThread()))); } final ConduitWrapper<StreamSourceConduit>[] wrappers = this.requestWrappers; final ConduitStreamSourceChannel sourceChannel = connection.getSourceChannel(); if (wrappers != null) { this.requestWrappers = null; final WrapperConduitFactory<StreamSourceConduit> factory = new WrapperConduitFactory<>(wrappers, requestWrapperCount, sourceChannel.getConduit(), this); sourceChannel.setConduit(factory.create()); } return requestChannel = new ReadDispatchChannel(sourceChannel); }
private void handleConnected(StreamConnection connection, ClientCallback<ClientConnection> listener, URI uri, XnioSsl ssl, ByteBufferPool bufferPool, OptionMap options) { final ClientStatisticsImpl clientStatistics; //first we set up statistics, if required if (options.get(UndertowOptions.ENABLE_STATISTICS, false)) { clientStatistics = new ClientStatisticsImpl(); connection.getSinkChannel().setConduit(new BytesSentStreamSinkConduit(connection.getSinkChannel().getConduit(), new ByteActivityCallback() { @Override public void activity(long bytes) { clientStatistics.written += bytes; } })); connection.getSourceChannel().setConduit(new BytesReceivedStreamSourceConduit(connection.getSourceChannel().getConduit(), new ByteActivityCallback() { @Override public void activity(long bytes) { clientStatistics.read += bytes; } })); } else { clientStatistics = null; } listener.completed(new AjpClientConnection(new AjpClientChannel(connection, bufferPool, options), options, bufferPool, clientStatistics)); }
public void handleEvent(final StreamConnection channel, PooledByteBuffer buffer) { if (UndertowLogger.REQUEST_LOGGER.isTraceEnabled()) { UndertowLogger.REQUEST_LOGGER.tracef("Opened HTTP/2 connection with %s", channel.getPeerAddress()); } //cool, we have a Http2 connection. Http2Channel http2Channel = new Http2Channel(channel, protocol, bufferPool, buffer, false, false, undertowOptions); Integer idleTimeout = undertowOptions.get(UndertowOptions.IDLE_TIMEOUT); if (idleTimeout != null && idleTimeout > 0) { http2Channel.setIdleTimeout(idleTimeout); } if(statisticsEnabled) { channel.getSinkChannel().setConduit(new BytesSentStreamSinkConduit(channel.getSinkChannel().getConduit(), connectorStatistics.sentAccumulator())); channel.getSourceChannel().setConduit(new BytesReceivedStreamSourceConduit(channel.getSourceChannel().getConduit(), connectorStatistics.receivedAccumulator())); connectorStatistics.incrementConnectionCount(); http2Channel.addCloseTask(closeTask); } connections.add(http2Channel); http2Channel.addCloseTask(new ChannelListener<Http2Channel>() { @Override public void handleEvent(Http2Channel channel) { connections.remove(channel); } }); http2Channel.getReceiveSetter().set(new Http2ReceiveListener(rootHandler, getUndertowOptions(), bufferSize, connectorStatistics)); http2Channel.resumeReceives(); }
private static Http2ClientConnection createHttp2Channel(StreamConnection connection, ByteBufferPool bufferPool, OptionMap options, String defaultHost) { final ClientStatisticsImpl clientStatistics; //first we set up statistics, if required if (options.get(UndertowOptions.ENABLE_STATISTICS, false)) { clientStatistics = new ClientStatisticsImpl(); connection.getSinkChannel().setConduit(new BytesSentStreamSinkConduit(connection.getSinkChannel().getConduit(), new ByteActivityCallback() { @Override public void activity(long bytes) { clientStatistics.written += bytes; } })); connection.getSourceChannel().setConduit(new BytesReceivedStreamSourceConduit(connection.getSourceChannel().getConduit(), new ByteActivityCallback() { @Override public void activity(long bytes) { clientStatistics.read += bytes; } })); } else { clientStatistics = null; } Http2Channel http2Channel = new Http2Channel(connection, null, bufferPool, null, true, false, options); return new Http2ClientConnection(http2Channel, false, defaultHost, clientStatistics, true); }