Skip to content

Commit

Permalink
More SSL improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Mar 8, 2016
1 parent 05f582b commit 45c90ec
Showing 1 changed file with 39 additions and 48 deletions.
87 changes: 39 additions & 48 deletions core/src/main/java/io/undertow/protocols/ssl/SslConduit.java
Expand Up @@ -153,8 +153,6 @@ public class SslConduit implements StreamSourceConduit, StreamSinkConduit {
private SslWriteReadyHandler writeReadyHandler; private SslWriteReadyHandler writeReadyHandler;
private SslReadReadyHandler readReadyHandler; private SslReadReadyHandler readReadyHandler;


private boolean invokingReadListenerHandshake = false;

private final Runnable runReadListenerCommand = new Runnable() { private final Runnable runReadListenerCommand = new Runnable() {
@Override @Override
public void run() { public void run() {
Expand Down Expand Up @@ -648,14 +646,19 @@ private long doUnwrap(ByteBuffer[] userBuffers, int off, int len) throws IOExcep
} }


PooledByteBuffer unwrappedData = this.unwrappedData; PooledByteBuffer unwrappedData = this.unwrappedData;
boolean existingUnwrappedData = false;
//copy any exiting data //copy any exiting data
if(unwrappedData != null && userBuffers != null) { if(unwrappedData != null) {
long copied = Buffers.copy(userBuffers, off, len, unwrappedData.getBuffer()); if(userBuffers != null) {
if(!unwrappedData.getBuffer().hasRemaining()) { long copied = Buffers.copy(userBuffers, off, len, unwrappedData.getBuffer());
unwrappedData.close(); if (!unwrappedData.getBuffer().hasRemaining()) {
this.unwrappedData = null; unwrappedData.close();
this.unwrappedData = null;
}
return copied;
} else {
existingUnwrappedData = true;
} }
return copied;
} }
try { try {
//we need to store how much data is in the unwrap buffer. If no progress can be made then we unset //we need to store how much data is in the unwrap buffer. If no progress can be made then we unset
Expand Down Expand Up @@ -757,10 +760,6 @@ private long doUnwrap(ByteBuffer[] userBuffers, int off, int len) throws IOExcep
throw e; throw e;
} finally { } finally {
try { try {
boolean requiresListenerInvocation = false; //if there is data in the buffer and reads are resumed we should re-run the listener
if (unwrappedData != null && unwrappedData.getBuffer().hasRemaining()) {
requiresListenerInvocation = true;
}
if (dataToUnwrap != null) { if (dataToUnwrap != null) {
//if there is no data in the buffer we just free it //if there is no data in the buffer we just free it
if (!dataToUnwrap.getBuffer().hasRemaining()) { if (!dataToUnwrap.getBuffer().hasRemaining()) {
Expand All @@ -770,16 +769,8 @@ private long doUnwrap(ByteBuffer[] userBuffers, int off, int len) throws IOExcep
} else if (allAreClear(state, FLAG_DATA_TO_UNWRAP)) { } else if (allAreClear(state, FLAG_DATA_TO_UNWRAP)) {
//if there is not enough data in the buffer we compact it to make room for more //if there is not enough data in the buffer we compact it to make room for more
dataToUnwrap.getBuffer().compact(); dataToUnwrap.getBuffer().compact();
} else {
//there is more data, make sure we trigger a read listener invocation
requiresListenerInvocation = true;
} }
} }
//if we are in the read listener handshake we don't need to invoke
//as it is about to be invoked anyway
if (requiresListenerInvocation && anyAreSet(state, FLAG_READS_RESUMED) && !invokingReadListenerHandshake) {
runReadListener(false);
}
} catch (Exception e) { } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
Expand Down Expand Up @@ -940,31 +931,34 @@ private void closed() {
return; return;
} }
state |= FLAG_CLOSED | FLAG_DELEGATE_SINK_SHUTDOWN | FLAG_DELEGATE_SOURCE_SHUTDOWN | FLAG_WRITE_SHUTDOWN | FLAG_READ_SHUTDOWN; state |= FLAG_CLOSED | FLAG_DELEGATE_SINK_SHUTDOWN | FLAG_DELEGATE_SOURCE_SHUTDOWN | FLAG_WRITE_SHUTDOWN | FLAG_READ_SHUTDOWN;
notifyReadClosed(); try {
notifyWriteClosed(); notifyReadClosed();
if(dataToUnwrap != null) { notifyWriteClosed();
dataToUnwrap.close(); if (dataToUnwrap != null) {
dataToUnwrap = null; dataToUnwrap.close();
} dataToUnwrap = null;
if(unwrappedData != null) { }
unwrappedData.close(); if (unwrappedData != null) {
unwrappedData = null; unwrappedData.close();
} unwrappedData = null;
if(wrappedData != null) { }
wrappedData.close(); if (wrappedData != null) {
wrappedData = null; wrappedData.close();
} wrappedData = null;
if(allAreClear(state, FLAG_ENGINE_OUTBOUND_SHUTDOWN)) {
engine.closeOutbound();
}
if(allAreClear(state, FLAG_ENGINE_INBOUND_SHUTDOWN)) {
try {
engine.closeInbound();
} catch (SSLException e) {
UndertowLogger.REQUEST_LOGGER.ioException(e);
} }
if (allAreClear(state, FLAG_ENGINE_OUTBOUND_SHUTDOWN)) {
engine.closeOutbound();
}
if (allAreClear(state, FLAG_ENGINE_INBOUND_SHUTDOWN)) {
try {
engine.closeInbound();
} catch (SSLException e) {
UndertowLogger.REQUEST_LOGGER.ioException(e);
}
}
} finally {
IoUtils.safeClose(delegate);
} }
IoUtils.safeClose(delegate);
} }


/** /**
Expand Down Expand Up @@ -1051,15 +1045,12 @@ private SslReadReadyHandler(ReadReadyHandler delegateHandler) {


@Override @Override
public void readReady() { public void readReady() {
if(anyAreSet(state, FLAG_WRITE_REQUIRES_READ) && anyAreSet(state, FLAG_WRITES_RESUMED | FLAG_READS_RESUMED) && !anyAreSet(state, FLAG_ENGINE_INBOUND_SHUTDOWN)) { if(anyAreSet(state, FLAG_WRITE_REQUIRES_READ | FLAG_READ_REQUIRES_WRITE) && anyAreSet(state, FLAG_WRITES_RESUMED | FLAG_READS_RESUMED) && !anyAreSet(state, FLAG_ENGINE_INBOUND_SHUTDOWN)) {
try { try {
invokingReadListenerHandshake = true;
doHandshake(); doHandshake();
} catch (IOException e) { } catch (IOException e) {
UndertowLogger.REQUEST_LOGGER.ioException(e); UndertowLogger.REQUEST_LOGGER.ioException(e);
IoUtils.safeClose(delegate); IoUtils.safeClose(delegate);
} finally {
invokingReadListenerHandshake = false;
} }
} }
boolean noProgress = false; boolean noProgress = false;
Expand Down Expand Up @@ -1088,7 +1079,7 @@ public void readReady() {
delegateHandler.readReady(); delegateHandler.readReady();
} }
} }
if(!anyAreSet(state, FLAG_READS_RESUMED | FLAG_WRITE_REQUIRES_READ)) { if(!anyAreSet(state, FLAG_READS_RESUMED) && !allAreSet(state, FLAG_WRITE_REQUIRES_READ | FLAG_WRITES_RESUMED)) {
delegate.getSourceChannel().suspendReads(); delegate.getSourceChannel().suspendReads();
} else if(anyAreSet(state, FLAG_READS_RESUMED) && (unwrappedData != null || anyAreSet(state, FLAG_DATA_TO_UNWRAP))) { } else if(anyAreSet(state, FLAG_READS_RESUMED) && (unwrappedData != null || anyAreSet(state, FLAG_DATA_TO_UNWRAP))) {
if(anyAreSet(state, FLAG_READ_CLOSED)) { if(anyAreSet(state, FLAG_READ_CLOSED)) {
Expand Down

0 comments on commit 45c90ec

Please sign in to comment.