Skip to content

Commit

Permalink
Merge pull request #1094 from baranowb/UNDERTOW-1471_2.0.x
Browse files Browse the repository at this point in the history
[UNDERTOW-1471] unify secure uri processing for wss and https
  • Loading branch information
fl4via committed Jul 20, 2021
2 parents c8a6f00 + cef9268 commit 1ab1424
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
Expand Up @@ -212,10 +212,10 @@ public IoFuture<WebSocketChannel> connect() {
}
private IoFuture<WebSocketChannel> connectImpl(final URI uri, final FutureResult<WebSocketChannel> ioFuture, final int redirectCount) {
WebSocketLogger.REQUEST_LOGGER.debugf("Opening websocket connection to %s", uri);
final String scheme = uri.getScheme().equals("wss") ? "https" : "http";
final String scheme = isSecure(uri) ? "https" : "http";
final URI newUri;
try {
newUri = new URI(scheme, uri.getUserInfo(), uri.getHost(), uri.getPort() == -1 ? (uri.getScheme().equals("wss") ? 443 : 80) : uri.getPort(), uri.getPath().isEmpty() ? "/" : uri.getPath(), uri.getQuery(), uri.getFragment());
newUri = new URI(scheme, uri.getUserInfo(), uri.getHost(), uri.getPort() == -1 ? (isSecure(uri) ? 443 : 80) : uri.getPort(), uri.getPath().isEmpty() ? "/" : uri.getPath(), uri.getQuery(), uri.getFragment());
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
Expand All @@ -240,7 +240,7 @@ private IoFuture<WebSocketChannel> connectImpl(final URI uri, final FutureResult
UndertowClient.getInstance().connect(new ClientCallback<ClientConnection>() {
@Override
public void completed(final ClientConnection connection) {
int port = uri.getPort() > 0 ? uri.getPort() : uri.getScheme().equals("https") || uri.getScheme().equals("wss") ? 443 : 80;
int port = uri.getPort() > 0 ? uri.getPort() : isSecure(uri) ? 443 : 80;
ClientRequest cr = new ClientRequest()
.setMethod(Methods.CONNECT)
.setPath(uri.getHost() + ":" + port)
Expand All @@ -257,7 +257,7 @@ public void completed(ClientExchange response) {
try {
StreamConnection targetConnection = connection.performUpgrade();
WebSocketLogger.REQUEST_LOGGER.debugf("Established websocket connection to %s", uri);
if (uri.getScheme().equals("wss") || uri.getScheme().equals("https")) {
if (isSecure(uri)) {
handleConnectionWithExistingConnection(((UndertowXnioSsl) ssl).wrapExistingConnection(targetConnection, optionMap, uri));
} else {
handleConnectionWithExistingConnection(targetConnection);
Expand Down Expand Up @@ -355,6 +355,10 @@ public Cancellable cancel() {
return ioFuture.getIoFuture();
}

private boolean isSecure(final URI uri) {
return uri.getScheme().equals("wss") || uri.getScheme().equals("https");
}

private class WebsocketConnectionListener implements ChannelListener<StreamConnection> {
private final OptionMap options;
private final WebSocketClientHandshake handshake;
Expand Down
Expand Up @@ -150,9 +150,18 @@ protected void onError(WebSocketChannel channel, Throwable error) {

@Test
public void testTextMessageWss() throws Exception {
testTextMessageSecure("wss://");
}

@Test
public void testTextMessageHttps() throws Exception {
testTextMessageSecure("https://");
}

public void testTextMessageSecure(final String urlProtocol) throws Exception {

UndertowXnioSsl ssl = new UndertowXnioSsl(Xnio.getInstance(), OptionMap.EMPTY, DefaultServer.getClientSSLContext());
final WebSocketClient.ConnectionBuilder connectionBuilder = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI("wss://" + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostSSLPort("default")))
final WebSocketClient.ConnectionBuilder connectionBuilder = WebSocketClient.connectionBuilder(worker, DefaultServer.getBufferPool(), new URI(urlProtocol + DefaultServer.getHostAddress("default") + ":" + DefaultServer.getHostSSLPort("default")))
.setSsl(ssl);
IoFuture<WebSocketChannel> future = connectionBuilder.connect();
future.await(4, TimeUnit.SECONDS);
Expand Down

0 comments on commit 1ab1424

Please sign in to comment.