use of io.undertow.websockets.WebSocketExtension in project undertow by undertow-io.
the class ServerWebSocketContainer method toExtensionList.
private static List<WebSocketExtension> toExtensionList(final List<Extension> extensions) {
List<WebSocketExtension> ret = new ArrayList<>();
for (Extension e : extensions) {
final List<WebSocketExtension.Parameter> parameters = new ArrayList<>();
for (Extension.Parameter p : e.getParameters()) {
parameters.add(new WebSocketExtension.Parameter(p.getName(), p.getValue()));
}
ret.add(new WebSocketExtension(e.getName(), parameters));
}
return ret;
}
use of io.undertow.websockets.WebSocketExtension in project undertow by undertow-io.
the class WebSocket13ClientHandshake method createChannel.
@Override
public WebSocketChannel createChannel(final StreamConnection channel, final String wsUri, final ByteBufferPool bufferPool, OptionMap options) {
if (negotiation != null && negotiation.getSelectedExtensions() != null && !negotiation.getSelectedExtensions().isEmpty()) {
List<WebSocketExtension> selected = negotiation.getSelectedExtensions();
List<ExtensionFunction> negotiated = new ArrayList<>();
if (selected != null && !selected.isEmpty()) {
for (WebSocketExtension ext : selected) {
for (ExtensionHandshake extHandshake : extensions) {
if (ext.getName().equals(extHandshake.getName())) {
negotiated.add(extHandshake.create());
}
}
}
}
return new WebSocket13Channel(channel, bufferPool, wsUri, negotiation.getSelectedSubProtocol(), true, !negotiated.isEmpty(), CompositeExtensionFunction.compose(negotiated), new HashSet<WebSocketChannel>(), options);
} else {
return new WebSocket13Channel(channel, bufferPool, wsUri, negotiation != null ? negotiation.getSelectedSubProtocol() : "", true, false, NoopExtensionFunction.INSTANCE, new HashSet<WebSocketChannel>(), options);
}
}
use of io.undertow.websockets.WebSocketExtension in project undertow by undertow-io.
the class PerMessageDeflateHandshake method accept.
@Override
public WebSocketExtension accept(final WebSocketExtension extension) {
if (extension == null || !extension.getName().equals(getName()))
return null;
WebSocketExtension negotiated = new WebSocketExtension(extension.getName());
if (extension.getParameters() == null || extension.getParameters().size() == 0)
return negotiated;
for (WebSocketExtension.Parameter parameter : extension.getParameters()) {
if (parameter.getName().equals(SERVER_MAX_WINDOW_BITS)) {
/*
Not supported
*/
} else if (parameter.getName().equals(CLIENT_MAX_WINDOW_BITS)) {
/*
Not supported
*/
} else if (parameter.getName().equals(SERVER_NO_CONTEXT_TAKEOVER)) {
negotiated.getParameters().add(parameter);
if (client) {
decompressContextTakeover = false;
} else {
compressContextTakeover = false;
}
} else if (parameter.getName().equals(CLIENT_NO_CONTEXT_TAKEOVER)) {
negotiated.getParameters().add(parameter);
if (client) {
compressContextTakeover = false;
} else {
decompressContextTakeover = false;
}
} else {
WebSocketLogger.EXTENSION_LOGGER.incorrectExtensionParameter(parameter);
return null;
}
}
WebSocketLogger.EXTENSION_LOGGER.debugf("Negotiated extension %s for handshake %s", negotiated, extension);
return negotiated;
}
use of io.undertow.websockets.WebSocketExtension in project undertow by undertow-io.
the class WebSocketExtensionBasicTestCase method testLongTextMessage.
@Test
public void testLongTextMessage() throws Exception {
XnioWorker client;
Xnio xnio = Xnio.getInstance(WebSocketExtensionBasicTestCase.class.getClassLoader());
client = xnio.createWorker(OptionMap.builder().set(Options.WORKER_IO_THREADS, 2).set(Options.CONNECTION_HIGH_WATER, 1000000).set(Options.CONNECTION_LOW_WATER, 1000000).set(Options.WORKER_TASK_CORE_THREADS, 30).set(Options.WORKER_TASK_MAX_THREADS, 30).set(Options.TCP_NODELAY, true).set(Options.CORK, true).getMap());
WebSocketProtocolHandshakeHandler handler = webSocketDebugHandler().addExtension(new PerMessageDeflateHandshake());
DebugExtensionsHeaderHandler debug = new DebugExtensionsHeaderHandler(handler);
DefaultServer.setRootHandler(path().addPrefixPath("/", debug));
final String SEC_WEBSOCKET_EXTENSIONS = "permessage-deflate; client_no_context_takeover; client_max_window_bits";
List<WebSocketExtension> extensionsList = WebSocketExtension.parse(SEC_WEBSOCKET_EXTENSIONS);
final WebSocketClientNegotiation negotiation = new WebSocketClientNegotiation(null, extensionsList);
Set<ExtensionHandshake> extensionHandshakes = new HashSet<>();
extensionHandshakes.add(new PerMessageDeflateHandshake(true));
final WebSocketChannel clientChannel = WebSocketClient.connect(client, null, DefaultServer.getBufferPool(), OptionMap.EMPTY, new URI(DefaultServer.getDefaultServerURL()), WebSocketVersion.V13, negotiation, extensionHandshakes).get();
final CountDownLatch latch = new CountDownLatch(1);
final AtomicReference<String> result = new AtomicReference<>();
clientChannel.getReceiveSetter().set(new AbstractReceiveListener() {
@Override
protected void onFullTextMessage(WebSocketChannel channel, BufferedTextMessage message) throws IOException {
String data = message.getData();
//WebSocketLogger.ROOT_LOGGER.info("onFullTextMessage() - Client - Received: " + data.getBytes().length + " bytes.");
result.set(data);
latch.countDown();
}
@Override
protected void onFullCloseMessage(WebSocketChannel channel, BufferedBinaryMessage message) throws IOException {
message.getData().close();
WebSocketLogger.ROOT_LOGGER.info("onFullCloseMessage");
}
@Override
protected void onError(WebSocketChannel channel, Throwable error) {
WebSocketLogger.ROOT_LOGGER.info("onError");
super.onError(channel, error);
error.printStackTrace();
latch.countDown();
}
});
clientChannel.resumeReceives();
int LONG_MSG = 125 * 1024;
StringBuilder longMsg = new StringBuilder(LONG_MSG);
for (int i = 0; i < LONG_MSG; i++) {
longMsg.append(Integer.toString(i).charAt(0));
}
WebSockets.sendTextBlocking(longMsg.toString(), clientChannel);
latch.await(300, TimeUnit.SECONDS);
Assert.assertEquals(longMsg.toString(), result.get());
clientChannel.sendClose();
client.shutdown();
}
Aggregations