Skip to content

Commit

Permalink
Issue #9895 - ensure callback is failed after error in PerMessageDefl…
Browse files Browse the repository at this point in the history
…ateExtension

Signed-off-by: Lachlan Roberts <lachlan@webtide.com>
  • Loading branch information
lachlan-roberts committed Jun 13, 2023
1 parent a471c08 commit 81447c3
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -464,16 +464,18 @@ private boolean inflate(Frame frame, Callback callback, boolean first) throws Da
chunk.setPayload(payload);
chunk.setFin(frame.isFin() && complete);

boolean succeedCallback = complete;
boolean completeCallback = complete;
AtomicReference<ByteBuffer> payloadRef = _payloadRef;
Callback payloadCallback = Callback.from(() ->
{
getBufferPool().release(payloadRef.getAndSet(null));
if (succeedCallback)
if (completeCallback)
callback.succeeded();
}, t ->
{
getBufferPool().release(payloadRef.getAndSet(null));
if (completeCallback)
callback.failed(t);
failFlusher(t);
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.junit.jupiter.api.Test;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertTrue;

Expand Down Expand Up @@ -92,6 +93,27 @@ void testDeflate() throws Exception
assertThat(message, is(sentMessage));
}

@Test
void testDeflateLargerThanMaxMessage() throws Exception
{
ClientUpgradeRequest upgradeRequest = new ClientUpgradeRequest();
upgradeRequest.addExtensions("permessage-deflate");

EventSocket clientSocket = new EventSocket();
ByteBuffer message = largePayloads();
Session session = _client.connect(clientSocket, URI.create("ws://localhost:" + _connector.getLocalPort() + "/ws"), upgradeRequest).get();

// Set the maxBinaryMessageSize on the server to be lower than the size of the message.
assertTrue(_serverSocket.openLatch.await(5, TimeUnit.SECONDS));
_serverSocket.session.setMaxBinaryMessageSize(message.remaining() - 1024);

session.getRemote().sendBytes(message);
assertTrue(clientSocket.closeLatch.await(5, TimeUnit.SECONDS));
assertTrue(_serverSocket.closeLatch.await(5, TimeUnit.SECONDS));
assertThat(_serverSocket.closeCode, is(StatusCode.MESSAGE_TOO_LARGE));
assertThat(_serverSocket.closeReason, containsString("Binary message too large"));
}

private static ByteBuffer largePayloads()
{
var bytes = new byte[4 * 1024 * 1024];
Expand Down

0 comments on commit 81447c3

Please sign in to comment.