public Http2ServerConnection(Http2Channel channel, Http2StreamSourceChannel requestChannel, OptionMap undertowOptions, int bufferSize, HttpHandler rootHandler) { this.channel = channel; this.requestChannel = requestChannel; this.undertowOptions = undertowOptions; this.bufferSize = bufferSize; this.rootHandler = rootHandler; responseChannel = requestChannel.getResponseChannel(); originalSinkConduit = new StreamSinkChannelWrappingConduit(responseChannel); originalSourceConduit = new StreamSourceChannelWrappingConduit(requestChannel); this.conduitStreamSinkChannel = new ConduitStreamSinkChannel(responseChannel, originalSinkConduit); this.conduitStreamSourceChannel = new ConduitStreamSourceChannel(channel, originalSourceConduit); }
log.debugf("exchange complete in connection to %s", getPeerAddress()); connection.getSinkChannel().setConduit(originalSinkConduit); connection.getSourceChannel().setConduit(pushBackStreamSourceConduit); connection.getSinkChannel().suspendWrites(); connection.getSinkChannel().setWriteListener(null); pendingResponse = null; this.state |= CLOSED; safeClose(connection); } else if (anyAreSet(state, UPGRADE_REQUESTED)) { connection.getSourceChannel().suspendReads(); currentRequest = null; pendingResponse = null; if (next == null) { connection.getSourceChannel().setReadListener(clientReadListener); connection.getSourceChannel().resumeReads(); } else { initiateRequest(next);
public void requestDone() { if(delegate instanceof ConduitStreamSourceChannel) { ((ConduitStreamSourceChannel)delegate).setReadListener(null); ((ConduitStreamSourceChannel)delegate).setCloseListener(null); } else { delegate.getReadSetter().set(null); delegate.getCloseSetter().set(null); } }
private void handleFailedRead(ConduitStreamSourceChannel channel, int res) { if (res == 0) { channel.setReadListener(this); channel.resumeReads(); } else if (res == -1) { IoUtils.safeClose(connection); } }
private void doResume() { if (readData != null && !readData.isFreed()) { channel.getSourceChannel().wakeupReads(); } else { channel.getSourceChannel().resumeReads(); } }
/** * 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; }
@Override public void run() { handle = null; if (expireTime == -1) { return; } long current = System.currentTimeMillis(); if (current < expireTime) { //timeout has been bumped, re-schedule handle = WorkerUtils.executeAfter(connection.getIoThread(),timeoutCommand, (expireTime - current) + FUZZ_FACTOR, TimeUnit.MILLISECONDS); return; } UndertowLogger.REQUEST_LOGGER.tracef("Timing out channel %s due to inactivity", connection.getSourceChannel()); IoUtils.safeClose(connection); if (connection.getSourceChannel().isReadResumed()) { ChannelListeners.invokeChannelListener(connection.getSourceChannel(), connection.getSourceChannel().getReadListener()); } if (connection.getSinkChannel().isWriteResumed()) { ChannelListeners.invokeChannelListener(connection.getSinkChannel(), connection.getSinkChannel().getWriteListener()); } } };
@Override public void handleEvent(final StreamConnection channel) { if (UndertowLogger.REQUEST_LOGGER.isTraceEnabled()) { UndertowLogger.REQUEST_LOGGER.tracef("Opened connection with %s", channel.getPeerAddress()); Integer readTimeout = channel.getOption(Options.READ_TIMEOUT); Integer idle = undertowOptions.get(UndertowOptions.IDLE_TIMEOUT); if(idle != null) { IdleTimeoutConduit conduit = new IdleTimeoutConduit(channel); channel.getSourceChannel().setConduit(conduit); channel.getSinkChannel().setConduit(conduit); channel.getSourceChannel().setConduit(new ReadTimeoutStreamSourceConduit(channel.getSourceChannel().getConduit(), channel, this)); Integer writeTimeout = channel.getOption(Options.WRITE_TIMEOUT); if (writeTimeout != null && writeTimeout > 0) { channel.getSinkChannel().setConduit(new WriteTimeoutStreamSinkConduit(channel.getSinkChannel().getConduit(), channel, this)); IoUtils.safeClose(channel); UndertowLogger.REQUEST_IO_LOGGER.ioException(e); channel.getSinkChannel().setConduit(new BytesSentStreamSinkConduit(channel.getSinkChannel().getConduit(), connectorStatistics.sentAccumulator())); channel.getSourceChannel().setConduit(new BytesReceivedStreamSourceConduit(channel.getSourceChannel().getConduit(), connectorStatistics.receivedAccumulator())); connectorStatistics.incrementConnectionCount(); channel.getSourceChannel().setReadListener(readListener); readListener.handleEvent(channel.getSourceChannel());
channel.getSourceChannel().setReadListener(HttpReadListener.this); channel.getSourceChannel().resumeReads(); requestStateUpdater.set(this, 0); } else { while (true) { if (connection.getOriginalSourceConduit().isReadShutdown() || connection.getOriginalSinkConduit().isWriteShutdown()) { channel.getSourceChannel().suspendReads(); channel.getSinkChannel().suspendWrites(); IoUtils.safeClose(connection); return; } else { try { newRequest(); channel.getSourceChannel().setReadListener(HttpReadListener.this); channel.getSourceChannel().resumeReads(); } finally { requestStateUpdater.set(this, 0); while (true) { if (connection.getOriginalSinkConduit().isWriteShutdown()) { channel.getSourceChannel().suspendReads(); channel.getSinkChannel().suspendWrites(); IoUtils.safeClose(connection); return; } else if (requestStateUpdater.compareAndSet(this, 1, 2)) { try { newRequest(); channel.getSourceChannel().suspendReads();
public void handleEvent(final StreamSourceChannel channel) { if(connection.getOriginalSinkConduit().isWriteShutdown() || connection.getOriginalSourceConduit().isReadShutdown()) { safeClose(connection); channel.suspendReads(); return; res = channel.read(buffer); } catch (IOException e) { UndertowLogger.REQUEST_IO_LOGGER.ioException(e); safeClose(connection); return; safeClose(connection); UndertowLogger.REQUEST_LOGGER.requestHeaderWasTooLarge(connection.getPeerAddress(), maxRequestSize); safeClose(connection); return; final AjpServerResponseConduit responseConduit = new AjpServerResponseConduit(connection.getChannel().getSinkChannel().getConduit(), connection.getByteBufferPool(), httpServerExchange, new ConduitListener<AjpServerResponseConduit>() { @Override public void handleEvent(AjpServerResponseConduit channel) { connection.getChannel().getSinkChannel().setConduit(responseConduit); connection.getChannel().getSourceChannel().setConduit(createSourceConduit(connection.getChannel().getSourceChannel().getConduit(), responseConduit, httpServerExchange));
public void exchangeComplete(final HttpServerExchange exchange) { if (!exchange.isUpgrade() && exchange.isPersistent()) { startRequest(); ConduitStreamSourceChannel channel = ((AjpServerConnection) exchange.getConnection()).getChannel().getSourceChannel(); channel.getReadSetter().set(this); channel.wakeupReads(); } else if(!exchange.isPersistent()) { safeClose(exchange.getConnection()); } }
@Override public void handleEvent(ConduitStreamSourceChannel channel) { try { int res = channel.read(b); if (res != 0) { IoUtils.safeClose(connection); IoUtils.safeClose(additional); key.remove(); } } catch (Exception e) { if (e instanceof IOException) { UndertowLogger.REQUEST_IO_LOGGER.ioException((IOException) e); } else { UndertowLogger.REQUEST_IO_LOGGER.ioException(new IOException(e)); } IoUtils.safeClose(connection); IoUtils.safeClose(additional); key.remove(); } } });
private void handleCPing() { state = new AjpRequestParseState(); final StreamConnection underlyingChannel = connection.getChannel(); underlyingChannel.getSourceChannel().suspendReads(); final ByteBuffer buffer = ByteBuffer.wrap(CPONG); int res; try { do { res = underlyingChannel.getSinkChannel().write(buffer); if (res == 0) { underlyingChannel.getSinkChannel().setWriteListener(new ChannelListener<ConduitStreamSinkChannel>() { @Override public void handleEvent(ConduitStreamSinkChannel channel) { underlyingChannel.getSinkChannel().resumeWrites(); return; AjpReadListener.this.handleEvent(underlyingChannel.getSourceChannel()); } catch (IOException e) { UndertowLogger.REQUEST_IO_LOGGER.ioException(e); safeClose(connection);
private static void doDrain(final StreamConnection connection, final Closeable... additional) { if (!connection.getSourceChannel().isOpen()) { IoUtils.safeClose(connection); IoUtils.safeClose(additional); return; int res = connection.getSourceChannel().read(b); b.clear(); if (res == 0) { final XnioExecutor.Key key = WorkerUtils.executeAfter(connection.getIoThread(), new Runnable() { @Override public void run() { connection.getSourceChannel().setReadListener(new ChannelListener<ConduitStreamSourceChannel>() { @Override public void handleEvent(ConduitStreamSourceChannel channel) { connection.getSourceChannel().resumeReads(); } else { IoUtils.safeClose(connection); IoUtils.safeClose(additional); UndertowLogger.REQUEST_IO_LOGGER.ioException((IOException) e); } else { UndertowLogger.REQUEST_IO_LOGGER.ioException(new IOException(e)); IoUtils.safeClose(connection);
this.maxQueuedBuffers = settings.get(UndertowOptions.MAX_QUEUED_READ_BUFFERS, 10); this.settings = settings; if (readData != null) { connectedStreamChannel.getSourceChannel().setConduit(idle); connectedStreamChannel.getSinkChannel().setConduit(idle); this.idleTimeoutConduit = idle; this.channel = connectedStreamChannel; channel.getSourceChannel().getReadSetter().set(null); channel.getSourceChannel().suspendReads(); channel.getSourceChannel().getReadSetter().set(new FrameReadListener()); connectedStreamChannel.getSinkChannel().getWriteSetter().set(new FrameWriteListener()); FrameCloseListener closeListener = new FrameCloseListener(); connectedStreamChannel.getSinkChannel().getCloseSetter().set(closeListener); connectedStreamChannel.getSourceChannel().getCloseSetter().set(closeListener);
try { while (true) { int res = channel.getSourceChannel().read(buffer.getBuffer()); if (res == -1) { IoUtils.safeClose(channel); return; UndertowLogger.REQUEST_IO_LOGGER.noALPNFallback(channel.getPeerAddress()); IoUtils.safeClose(channel); return; } else if (res > 0) { if (fallbackProtocol == null) { UndertowLogger.REQUEST_IO_LOGGER.noALPNFallback(channel.getPeerAddress()); IoUtils.safeClose(channel); return; return; } else if (res == 0) { channel.getSourceChannel().resumeReads(); return; UndertowLogger.REQUEST_IO_LOGGER.ioException(e); IoUtils.safeClose(channel); } catch (Throwable t) {
doHandshake(); } catch (IOException e) { UndertowLogger.REQUEST_LOGGER.ioException(e); IoUtils.safeClose(delegate); } catch (Throwable t) { UndertowLogger.REQUEST_IO_LOGGER.handleUnexpectedFailure(t); IoUtils.safeClose(delegate); } finally { invokingReadListenerHandshake = false; delegate.getSourceChannel().suspendReads(); if (anyAreSet(state, FLAG_READS_RESUMED)) { if (delegateHandler == null) { final ChannelListener<? super ConduitStreamSourceChannel> readListener = connection.getSourceChannel().getReadListener(); if (readListener == null) { suspendReads();
public void handleEventWithNoRunningRequest(final ConduitStreamSourceChannel channel) { PooledByteBuffer existing = connection.getExtraBytes(); if ((existing == null && connection.getOriginalSourceConduit().isReadShutdown()) || connection.getOriginalSinkConduit().isWriteShutdown()) { IoUtils.safeClose(connection); channel.suspendReads(); return; buffer.clear(); try { res = channel.read(buffer); } catch (IOException e) { UndertowLogger.REQUEST_IO_LOGGER.debug("Error reading request", e); IoUtils.safeClose(connection); return; read = total; if (read > maxRequestSize) { UndertowLogger.REQUEST_LOGGER.requestHeaderWasTooLarge(connection.getPeerAddress(), maxRequestSize); sendBadRequestAndClose(connection.getChannel(), null); return; HttpString protocol = httpServerExchange.getProtocol(); if(protocol != Protocols.HTTP_1_1 && protocol != Protocols.HTTP_1_0 && protocol != Protocols.HTTP_0_9) { UndertowLogger.REQUEST_IO_LOGGER.debugf("Closing connection from %s due to unknown protocol %s", connection.getChannel().getPeerAddress(), protocol); sendBadRequestAndClose(connection.getChannel(), new IOException()); return; channel.suspendReads();
/** {@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(); }
HttpClientConnection(final StreamConnection connection, final OptionMap options, final ByteBufferPool bufferPool) { 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) { connection.getSourceChannel().setConduit(new BytesReceivedStreamSourceConduit(connection.getSourceChannel().getConduit(), new ByteActivityCallback() { @Override public void activity(long bytes) { this.pushBackStreamSourceConduit = new PushBackStreamSourceConduit(connection.getSourceChannel().getConduit()); this.connection.getSourceChannel().setConduit(pushBackStreamSourceConduit); this.bufferPool = bufferPool; this.originalSinkConduit = connection.getSinkChannel().getConduit(); connection.getCloseSetter().set(new ChannelListener<StreamConnection>() { connection.getSourceChannel().setReadListener(clientReadListener); connection.getSourceChannel().resumeReads();