@Override public void afterRequest(Map<String, List<String>> headers) { headers.forEach(this.responseHeaders::put); if (this.delegate != null) { this.delegate.afterRequest(headers); } } }
/** * Creates a new connection builder that can be used to create a web socket connection. * @param worker The XnioWorker to use for the connection * @param bufferPool The buffer pool * @param uri The connection URI * @return The connection builder */ public static ConnectionBuilder connectionBuilder(XnioWorker worker, ByteBufferPool bufferPool, URI uri) { return new ConnectionBuilder(worker, bufferPool, uri); }
public DefaultNegotiation(List<String> protocols, HttpHeaders requestHeaders, ConnectionBuilder connectionBuilder) { super(protocols, Collections.emptyList()); this.requestHeaders = requestHeaders; this.delegate = connectionBuilder.getClientNegotiation(); }
@Deprecated public static IoFuture<WebSocketChannel> connect(XnioWorker worker, XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap optionMap, InetSocketAddress bindAddress, final URI uri, WebSocketVersion version, WebSocketClientNegotiation clientNegotiation, Set<ExtensionHandshake> clientExtensions) { return connectionBuilder(worker, bufferPool, uri) .setSsl(ssl) .setOptionMap(optionMap) .setBindAddress(bindAddress) .setVersion(version) .setClientNegotiation(clientNegotiation) .setClientExtensions(clientExtensions) .connect(); }
throw new RuntimeException(e); final WebSocketClientHandshake handshake = WebSocketClientHandshake.create(version, newUri, clientNegotiation, clientExtensions); final Map<String, String> originalHeaders = handshake.createHeaders(); originalHeaders.put(Headers.HOST_STRING, uri.getHost() + ":" + newUri.getPort()); final Map<String, List<String>> headers = new HashMap<>(); clientNegotiation.beforeRequest(headers); final IoFuture<?> result; if (ssl != null) { result = HttpUpgrade.performUpgrade(worker, ssl, toBind, newUri, headers, new WebsocketConnectionListener(optionMap, handshake, newUri, ioFuture), null, optionMap, handshake.handshakeChecker(newUri, headers)); } else { result = HttpUpgrade.performUpgrade(worker, toBind, newUri, headers, new WebsocketConnectionListener(optionMap, handshake, newUri, ioFuture), null, optionMap, handshake.handshakeChecker(newUri, headers));
private Mono<Void> executeInternal(URI url, HttpHeaders headers, WebSocketHandler handler) { MonoProcessor<Void> completion = MonoProcessor.create(); return Mono.fromCallable( () -> { if (logger.isDebugEnabled()) { logger.debug("Connecting to " + url); } List<String> protocols = handler.getSubProtocols(); ConnectionBuilder builder = createConnectionBuilder(url); DefaultNegotiation negotiation = new DefaultNegotiation(protocols, headers, builder); builder.setClientNegotiation(negotiation); return builder.connect().addNotifier( new IoFuture.HandlingNotifier<WebSocketChannel, Object>() { @Override public void handleDone(WebSocketChannel channel, Object attachment) { handleChannel(url, handler, completion, negotiation, channel); } @Override public void handleFailed(IOException ex, Object attachment) { completion.onError(new IllegalStateException("Failed to connect to " + url, ex)); } }, null); }) .then(completion); }
try { if (negotiation != null) { negotiation.afterRequest(headers); final String dKey = solve(sentKey); if (!dKey.equals(acceptKey)) { throw WebSocketMessages.MESSAGES.webSocketAcceptKeyMismatch(dKey, acceptKey); if (subProto != null && !subProto.isEmpty() && !negotiation.getSupportedSubProtocols().contains(subProto)) { throw WebSocketMessages.MESSAGES.unsupportedProtocol(subProto, negotiation.getSupportedSubProtocols()); extensions = WebSocketExtension.parse(extHeader); negotiation.handshakeComplete(subProto, extensions);
headers.put(Headers.UPGRADE_STRING, "websocket"); headers.put(Headers.CONNECTION_STRING, "upgrade"); String key = createSecKey(); headers.put(Headers.SEC_WEB_SOCKET_KEY_STRING, key); headers.put(Headers.SEC_WEB_SOCKET_VERSION_STRING, getVersion().toHttpHeaderValue()); if (negotiation != null) { List<String> subProtocols = negotiation.getSupportedSubProtocols(); if (subProtocols != null && !subProtocols.isEmpty()) { StringBuilder sb = new StringBuilder(); List<WebSocketExtension> extensions = negotiation.getSupportedExtensions(); if (extensions != null && !extensions.isEmpty()) { StringBuilder sb = new StringBuilder();
/** * Create a {@link ConnectionBuilder} for the given URI. * <p>The default implementation creates a builder with the configured * {@link #getXnioWorker() XnioWorker} and {@link #getByteBufferPool() ByteBufferPool} and * then passes it to the {@link #getConnectionBuilderConsumer() consumer} * provided at construction time. */ protected ConnectionBuilder createConnectionBuilder(URI url) { ConnectionBuilder builder = io.undertow.websockets.client.WebSocketClient .connectionBuilder(getXnioWorker(), getByteBufferPool(), url); this.builderConsumer.accept(builder); return builder; }
@Deprecated public static IoFuture<WebSocketChannel> connect(XnioWorker worker, XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap optionMap, final URI uri, WebSocketVersion version, WebSocketClientNegotiation clientNegotiation) { return connect(worker, ssl, bufferPool, optionMap, uri, version, clientNegotiation, null); }
@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); } }
@Override public void beforeRequest(Map<String, List<String>> headers) { this.requestHeaders.forEach(headers::put); if (this.delegate != null) { this.delegate.beforeRequest(headers); } }
public static WebSocketClientHandshake create(final WebSocketVersion version, final URI uri) { return create(version, uri, null, null); }
public static WebSocketClientHandshake create(final WebSocketVersion version, final URI uri, WebSocketClientNegotiation clientNegotiation, Set<ExtensionHandshake> extensions) { switch (version) { case V13: return new WebSocket13ClientHandshake(uri, clientNegotiation, extensions); } throw new IllegalArgumentException(); }
public IoFuture<WebSocketChannel> connect() { return connectImpl(uri, new FutureResult<WebSocketChannel>(), 0); } private IoFuture<WebSocketChannel> connectImpl(final URI uri, final FutureResult<WebSocketChannel> ioFuture, final int redirectCount) {
@Override public void handleEvent(StreamConnection channel) { WebSocketChannel result = handshake.createChannel(channel, newUri.toString(), bufferPool, options); ioFuture.setResult(result); } }
@Deprecated public static IoFuture<WebSocketChannel> connect(XnioWorker worker, final ByteBufferPool bufferPool, final OptionMap optionMap, final URI uri, WebSocketVersion version) { return connect(worker, bufferPool, optionMap, uri, version, null); }
@Deprecated public static IoFuture<WebSocketChannel> connect(XnioWorker worker, XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap optionMap, final URI uri, WebSocketVersion version, WebSocketClientNegotiation clientNegotiation, Set<ExtensionHandshake> clientExtensions) { return connect(worker, ssl, bufferPool, optionMap, null, uri, version, clientNegotiation, clientExtensions); }
@Deprecated public static IoFuture<WebSocketChannel> connect(XnioWorker worker, XnioSsl ssl, final ByteBufferPool bufferPool, final OptionMap optionMap, final URI uri, WebSocketVersion version) { return connect(worker, ssl, bufferPool, optionMap, uri, version, null); }
@Deprecated public static IoFuture<WebSocketChannel> connect(XnioWorker worker, final ByteBufferPool bufferPool, final OptionMap optionMap, final URI uri, WebSocketVersion version, WebSocketClientNegotiation clientNegotiation) { return connect(worker, null, bufferPool, optionMap, uri, version, clientNegotiation); }