use of jakarta.websocket.Extension.Parameter in project tomcat by apache.
the class PerMessageDeflate method negotiate.
static PerMessageDeflate negotiate(List<List<Parameter>> preferences, boolean isServer) {
// Accept the first preference that the endpoint is able to support
for (List<Parameter> preference : preferences) {
boolean ok = true;
boolean serverContextTakeover = true;
int serverMaxWindowBits = -1;
boolean clientContextTakeover = true;
int clientMaxWindowBits = -1;
for (Parameter param : preference) {
if (SERVER_NO_CONTEXT_TAKEOVER.equals(param.getName())) {
if (serverContextTakeover) {
serverContextTakeover = false;
} else {
// Duplicate definition
throw new IllegalArgumentException(sm.getString("perMessageDeflate.duplicateParameter", SERVER_NO_CONTEXT_TAKEOVER));
}
} else if (CLIENT_NO_CONTEXT_TAKEOVER.equals(param.getName())) {
if (clientContextTakeover) {
clientContextTakeover = false;
} else {
// Duplicate definition
throw new IllegalArgumentException(sm.getString("perMessageDeflate.duplicateParameter", CLIENT_NO_CONTEXT_TAKEOVER));
}
} else if (SERVER_MAX_WINDOW_BITS.equals(param.getName())) {
if (serverMaxWindowBits == -1) {
serverMaxWindowBits = Integer.parseInt(param.getValue());
if (serverMaxWindowBits < 8 || serverMaxWindowBits > 15) {
throw new IllegalArgumentException(sm.getString("perMessageDeflate.invalidWindowSize", SERVER_MAX_WINDOW_BITS, Integer.valueOf(serverMaxWindowBits)));
}
// to 15
if (isServer && serverMaxWindowBits != 15) {
ok = false;
break;
// Note server window size is not an issue for the
// client since the client will assume 15 and if the
// server uses a smaller window everything will
// still work
}
} else {
// Duplicate definition
throw new IllegalArgumentException(sm.getString("perMessageDeflate.duplicateParameter", SERVER_MAX_WINDOW_BITS));
}
} else if (CLIENT_MAX_WINDOW_BITS.equals(param.getName())) {
if (clientMaxWindowBits == -1) {
if (param.getValue() == null) {
// Hint to server that the client supports this
// option. Java SE API (as of Java 11) does not
// expose the API to control the Window size. It is
// effectively hard-coded to 15
clientMaxWindowBits = 15;
} else {
clientMaxWindowBits = Integer.parseInt(param.getValue());
if (clientMaxWindowBits < 8 || clientMaxWindowBits > 15) {
throw new IllegalArgumentException(sm.getString("perMessageDeflate.invalidWindowSize", CLIENT_MAX_WINDOW_BITS, Integer.valueOf(clientMaxWindowBits)));
}
}
// to 15
if (!isServer && clientMaxWindowBits != 15) {
ok = false;
break;
// Note client window size is not an issue for the
// server since the server will assume 15 and if the
// client uses a smaller window everything will
// still work
}
} else {
// Duplicate definition
throw new IllegalArgumentException(sm.getString("perMessageDeflate.duplicateParameter", CLIENT_MAX_WINDOW_BITS));
}
} else {
// Unknown parameter
throw new IllegalArgumentException(sm.getString("perMessageDeflate.unknownParameter", param.getName()));
}
}
if (ok) {
return new PerMessageDeflate(serverContextTakeover, serverMaxWindowBits, clientContextTakeover, clientMaxWindowBits, isServer);
}
}
// Failed to negotiate agreeable terms
return null;
}
use of jakarta.websocket.Extension.Parameter in project tomcat by apache.
the class TestPerMessageDeflate method testSendEmptyMessagePartWithContextTakeover.
/*
* https://bz.apache.org/bugzilla/show_bug.cgi?id=61491
*/
@Test
public void testSendEmptyMessagePartWithContextTakeover() throws IOException {
// Set up the extension using defaults
List<Parameter> parameters = Collections.emptyList();
List<List<Parameter>> preferences = new ArrayList<>();
preferences.add(parameters);
PerMessageDeflate perMessageDeflate = PerMessageDeflate.negotiate(preferences, true);
perMessageDeflate.setNext(new TesterTransformation());
ByteBuffer bb1 = ByteBuffer.wrap("A".getBytes(StandardCharsets.UTF_8));
MessagePart mp1 = new MessagePart(true, 0, Constants.OPCODE_TEXT, bb1, null, null, -1);
List<MessagePart> uncompressedParts1 = new ArrayList<>();
uncompressedParts1.add(mp1);
perMessageDeflate.sendMessagePart(uncompressedParts1);
ByteBuffer bb2 = ByteBuffer.wrap("".getBytes(StandardCharsets.UTF_8));
MessagePart mp2 = new MessagePart(true, 0, Constants.OPCODE_TEXT, bb2, null, null, -1);
List<MessagePart> uncompressedParts2 = new ArrayList<>();
uncompressedParts2.add(mp2);
perMessageDeflate.sendMessagePart(uncompressedParts2);
}
use of jakarta.websocket.Extension.Parameter in project tomcat by apache.
the class TestUtil method doTestParseExtensionHeaderMultiple.
private void doTestParseExtensionHeaderMultiple(String header) {
// Simple test
List<Extension> result = new ArrayList<>();
Util.parseExtensionHeader(result, header);
Assert.assertEquals(3, result.size());
Extension ext = result.get(0);
Assert.assertEquals("ext", ext.getName());
List<Parameter> params = ext.getParameters();
Assert.assertEquals(2, params.size());
Parameter paramA = params.get(0);
Assert.assertEquals("a", paramA.getName());
Assert.assertEquals("1", paramA.getValue());
Parameter paramB = params.get(1);
Assert.assertEquals("b", paramB.getName());
Assert.assertEquals("2", paramB.getValue());
Extension ext2 = result.get(1);
Assert.assertEquals("ext2", ext2.getName());
List<Parameter> params2 = ext2.getParameters();
Assert.assertEquals(2, params2.size());
Parameter paramC = params2.get(0);
Assert.assertEquals("c", paramC.getName());
Assert.assertNull(paramC.getValue());
Parameter paramD = params2.get(1);
Assert.assertEquals("d", paramD.getName());
Assert.assertEquals("xyz", paramD.getValue());
Extension ext3 = result.get(2);
Assert.assertEquals("ext3", ext3.getName());
List<Parameter> params3 = ext3.getParameters();
Assert.assertEquals(0, params3.size());
}
use of jakarta.websocket.Extension.Parameter in project tomcat by apache.
the class TestUtil method doTestParseExtensionHeaderSimple.
private void doTestParseExtensionHeaderSimple(String header) {
// Simple test
List<Extension> result = new ArrayList<>();
Util.parseExtensionHeader(result, header);
Assert.assertEquals(1, result.size());
Extension ext = result.get(0);
Assert.assertEquals("ext", ext.getName());
List<Parameter> params = ext.getParameters();
Assert.assertEquals(2, params.size());
Parameter paramA = params.get(0);
Assert.assertEquals("a", paramA.getName());
Assert.assertEquals("1", paramA.getValue());
Parameter paramB = params.get(1);
Assert.assertEquals("b", paramB.getName());
Assert.assertEquals("2", paramB.getValue());
}
use of jakarta.websocket.Extension.Parameter in project tomcat by apache.
the class TestPerMessageDeflate method testMessagePartThatFillsBufffer.
/*
* https://bz.apache.org/bugzilla/show_bug.cgi?id=65317
*/
@Test
public void testMessagePartThatFillsBufffer() throws IOException {
// Set up the extension using defaults
List<Parameter> parameters = Collections.emptyList();
List<List<Parameter>> preferences = new ArrayList<>();
preferences.add(parameters);
// Set up the compression and sending of the message.
PerMessageDeflate perMessageDeflateTx = PerMessageDeflate.negotiate(preferences, true);
perMessageDeflateTx.setNext(new TesterTransformation());
byte[] data = new byte[8192];
ByteBuffer bb = ByteBuffer.wrap(data);
MessagePart mp = new MessagePart(true, 0, Constants.OPCODE_BINARY, bb, null, null, -1);
List<MessagePart> uncompressedParts = new ArrayList<>();
uncompressedParts.add(mp);
List<MessagePart> compressedParts = perMessageDeflateTx.sendMessagePart(uncompressedParts);
MessagePart compressedPart = compressedParts.get(0);
// Set up the decompression and process the received messafe
PerMessageDeflate perMessageDeflateRx = PerMessageDeflate.negotiate(preferences, true);
perMessageDeflateRx.setNext(new TesterTransformation(compressedPart.getPayload()));
ByteBuffer received = ByteBuffer.allocate(8192);
TransformationResult tr = perMessageDeflateRx.getMoreData(compressedPart.getOpCode(), compressedPart.isFin(), compressedPart.getRsv(), received);
Assert.assertEquals(8192, received.position());
Assert.assertEquals(TransformationResult.END_OF_FRAME, tr);
}