use of io.undertow.websockets.extensions.ExtensionHandshake in project undertow by undertow-io.
the class JsrHybi13Handshake method selectedExtension.
@Override
protected List<WebSocketExtension> selectedExtension(List<WebSocketExtension> extensionList) {
List<Extension> ext = new ArrayList<>();
for (WebSocketExtension i : extensionList) {
ext.add(ExtensionImpl.create(i));
}
List<Extension> selected = HandshakeUtil.selectExtensions(config, ext);
if (selected == null) {
return Collections.emptyList();
}
Map<String, ExtensionHandshake> extensionMap = new HashMap<>();
for (ExtensionHandshake availible : availableExtensions) {
extensionMap.put(availible.getName(), availible);
}
List<WebSocketExtension> ret = new ArrayList<>();
List<ExtensionHandshake> accepted = new ArrayList<>();
for (Extension i : selected) {
ExtensionHandshake handshake = extensionMap.get(i.getName());
if (handshake == null) {
//should not happen
continue;
}
List<WebSocketExtension.Parameter> parameters = new ArrayList<>();
for (Extension.Parameter p : i.getParameters()) {
parameters.add(new WebSocketExtension.Parameter(p.getName(), p.getValue()));
}
if (!handshake.isIncompatible(accepted)) {
WebSocketExtension accept = handshake.accept(new WebSocketExtension(i.getName(), parameters));
if (accept != null) {
ret.add(accept);
accepted.add(handshake);
}
}
}
return ret;
}
use of io.undertow.websockets.extensions.ExtensionHandshake 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.extensions.ExtensionHandshake in project undertow by undertow-io.
the class JsrWebsocketExtensionTestCase method testExtensionsHeaders.
@Test
public void testExtensionsHeaders() throws Exception {
final String SEC_WEBSOCKET_EXTENSIONS = "permessage-deflate; client_no_context_takeover; client_max_window_bits";
// List format
final String SEC_WEBSOCKET_EXTENSIONS_EXPECTED = "[permessage-deflate; client_no_context_takeover]";
List<WebSocketExtension> extensions = WebSocketExtension.parse(SEC_WEBSOCKET_EXTENSIONS);
final WebSocketClientNegotiation negotiation = new WebSocketClientNegotiation(null, extensions);
Set<ExtensionHandshake> extensionHandshakes = new HashSet<>();
extensionHandshakes.add(new PerMessageDeflateHandshake(true));
final WebSocketChannel clientChannel = WebSocketClient.connect(DefaultServer.getWorker(), 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 . Data: " + data);
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();
StreamSinkFrameChannel sendChannel = clientChannel.send(WebSocketFrameType.TEXT);
new StringWriteChannelListener("Hello, World!").setup(sendChannel);
latch.await(10, TimeUnit.SECONDS);
Assert.assertEquals("Hello, World!", result.get());
clientChannel.sendClose();
Assert.assertEquals(SEC_WEBSOCKET_EXTENSIONS_EXPECTED, debug.getResponseExtensions().toString());
}
Aggregations