Skip to content

Commit

Permalink
Fixes #5931 - SslConnection should implement getBytesIn()/getBytesOut().
Browse files Browse the repository at this point in the history
Signed-off-by: Simone Bordet <[email protected]>
  • Loading branch information
sbordet committed May 30, 2021
1 parent 8f0bb81 commit f55fb60
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,12 @@

import org.eclipse.jetty.client.api.ContentResponse;
import org.eclipse.jetty.http.HttpHeader;
import org.eclipse.jetty.http.HttpHeaderValue;
import org.eclipse.jetty.http.HttpScheme;
import org.eclipse.jetty.http.HttpStatus;
import org.eclipse.jetty.io.ByteBufferPool;
import org.eclipse.jetty.io.ClientConnectionFactory;
import org.eclipse.jetty.io.ConnectionStatistics;
import org.eclipse.jetty.io.EndPoint;
import org.eclipse.jetty.io.ssl.SslClientConnectionFactory;
import org.eclipse.jetty.io.ssl.SslConnection;
Expand Down Expand Up @@ -1003,4 +1005,28 @@ public void testForcedNonDomainSNI() throws Exception
assertEquals(HttpStatus.OK_200, response3.getStatus());
}
}

@Test
public void testBytesInBytesOut() throws Exception
{
SslContextFactory serverTLSFactory = createServerSslContextFactory();
startServer(serverTLSFactory, new EmptyServerHandler());
ConnectionStatistics serverStats = new ConnectionStatistics();
connector.addManaged(serverStats);

SslContextFactory clientTLSFactory = createClientSslContextFactory();
startClient(clientTLSFactory);
ConnectionStatistics clientStats = new ConnectionStatistics();
client.addManaged(clientStats);

ContentResponse response = client.newRequest("https://localhost:" + connector.getLocalPort())
.header(HttpHeader.CONNECTION, HttpHeaderValue.CLOSE.asString())
.send();
assertEquals(HttpStatus.OK_200, response.getStatus());

assertThat(clientStats.getSentBytes(), Matchers.greaterThan(0L));
assertEquals(clientStats.getSentBytes(), serverStats.getReceivedBytes());
assertThat(clientStats.getReceivedBytes(), Matchers.greaterThan(0L));
assertEquals(clientStats.getReceivedBytes(), serverStats.getSentBytes());
}
}
47 changes: 42 additions & 5 deletions jetty-io/src/main/java/org/eclipse/jetty/io/ssl/SslConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.function.ToIntFunction;
import javax.net.ssl.SSLEngine;
Expand Down Expand Up @@ -104,7 +105,10 @@ private enum FlushState
WAIT_FOR_FILL // Waiting for a fill to happen
}

private final AtomicReference<HandshakeState> _handshake = new AtomicReference<>(HandshakeState.INITIAL);
private final List<SslHandshakeListener> handshakeListeners = new ArrayList<>();
private final AtomicLong _bytesIn = new AtomicLong();
private final AtomicLong _bytesOut = new AtomicLong();
private final ByteBufferPool _bufferPool;
private final SSLEngine _sslEngine;
private final DecryptedEndPoint _decryptedEndPoint;
Expand All @@ -119,7 +123,6 @@ private enum FlushState
private boolean _requireCloseMessage;
private FlushState _flushState = FlushState.IDLE;
private FillState _fillState = FillState.IDLE;
private AtomicReference<HandshakeState> _handshake = new AtomicReference<>(HandshakeState.INITIAL);
private boolean _underflown;

private abstract class RunnableTask implements Runnable, Invocable
Expand Down Expand Up @@ -198,6 +201,18 @@ public SslConnection(ByteBufferPool byteBufferPool, Executor executor, EndPoint
this._decryptedDirectBuffers = useDirectBuffersForDecryption;
}

@Override
public long getBytesIn()
{
return _bytesIn.get();
}

@Override
public long getBytesOut()
{
return _bytesOut.get();
}

public void addHandshakeListener(SslHandshakeListener listener)
{
handshakeListeners.add(listener);
Expand Down Expand Up @@ -675,6 +690,8 @@ public int fill(ByteBuffer buffer) throws IOException

// Let's try reading some encrypted data... even if we have some already.
int netFilled = networkFill(_encryptedInput);
if (netFilled > 0)
_bytesIn.addAndGet(netFilled);
if (LOG.isDebugEnabled())
LOG.debug("net filled={}", netFilled);

Expand Down Expand Up @@ -997,8 +1014,19 @@ public boolean flush(ByteBuffer... appOuts) throws IOException
}

// finish of any previous flushes
if (BufferUtil.hasContent(_encryptedOutput) && !networkFlush(_encryptedOutput))
return false;
if (_encryptedOutput != null)
{
int remaining = _encryptedOutput.remaining();
if (remaining > 0)
{
boolean flushed = networkFlush(_encryptedOutput);
int written = remaining - _encryptedOutput.remaining();
if (written > 0)
_bytesOut.addAndGet(written);
if (!flushed)
return false;
}
}

boolean isEmpty = BufferUtil.isEmpty(appOuts);

Expand Down Expand Up @@ -1076,8 +1104,17 @@ public boolean flush(ByteBuffer... appOuts) throws IOException

// if we have net bytes, let's try to flush them
boolean flushed = true;
if (BufferUtil.hasContent(_encryptedOutput))
flushed = networkFlush(_encryptedOutput);
if (_encryptedOutput != null)
{
int remaining = _encryptedOutput.remaining();
if (remaining > 0)
{
flushed = networkFlush(_encryptedOutput);
int written = remaining - _encryptedOutput.remaining();
if (written > 0)
_bytesOut.addAndGet(written);
}
}

if (LOG.isDebugEnabled())
LOG.debug("net flushed={}, ac={}", flushed, isEmpty);
Expand Down

0 comments on commit f55fb60

Please sign in to comment.