Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node WebSocket not working with sub-path option #13407

Merged
merged 4 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion java/src/org/openqa/selenium/grid/commands/Standalone.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ protected Handlers createHandlers(Config config) {
httpHandler = combine(httpHandler, Route.get("/readyz").to(() -> readinessCheck));
Node node = createNode(config, bus, distributor, combinedHandler);

return new Handlers(httpHandler, new ProxyNodeWebsockets(clientFactory, node));
return new Handlers(httpHandler, new ProxyNodeWebsockets(clientFactory, node, subPath));
}

@Override
Expand Down
12 changes: 7 additions & 5 deletions java/src/org/openqa/selenium/grid/node/ProxyNodeWebsockets.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,18 +56,20 @@ public class ProxyNodeWebsockets
ImmutableSet.of("goog:chromeOptions", "moz:debuggerAddress", "ms:edgeOptions");
private final HttpClient.Factory clientFactory;
private final Node node;
private final String gridSubPath;

public ProxyNodeWebsockets(HttpClient.Factory clientFactory, Node node) {
public ProxyNodeWebsockets(HttpClient.Factory clientFactory, Node node, String gridSubPath) {
this.clientFactory = Objects.requireNonNull(clientFactory);
this.node = Objects.requireNonNull(node);
this.gridSubPath = gridSubPath;
}

@Override
public Optional<Consumer<Message>> apply(String uri, Consumer<Message> downstream) {
UrlTemplate.Match fwdMatch = FWD_TEMPLATE.match(uri);
UrlTemplate.Match cdpMatch = CDP_TEMPLATE.match(uri);
UrlTemplate.Match bidiMatch = BIDI_TEMPLATE.match(uri);
UrlTemplate.Match vncMatch = VNC_TEMPLATE.match(uri);
UrlTemplate.Match fwdMatch = FWD_TEMPLATE.match(uri, gridSubPath);
UrlTemplate.Match cdpMatch = CDP_TEMPLATE.match(uri, gridSubPath);
UrlTemplate.Match bidiMatch = BIDI_TEMPLATE.match(uri, gridSubPath);
UrlTemplate.Match vncMatch = VNC_TEMPLATE.match(uri, gridSubPath);

if (bidiMatch == null && cdpMatch == null && vncMatch == null && fwdMatch == null) {
return Optional.empty();
Expand Down
15 changes: 15 additions & 0 deletions java/src/org/openqa/selenium/grid/node/config/NodeOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,21 @@ public boolean isManagedDownloadsEnabled() {
return config.getBool(NODE_SECTION, "enable-managed-downloads").orElse(Boolean.FALSE);
}

public String getGridSubPath() {
return normalizeSubPath(getPublicGridUri().map(URI::getPath).orElse(""));
}

public static String normalizeSubPath(String prefix) {
prefix = prefix.trim();
if (!prefix.startsWith("/")) {
prefix = "/" + prefix; // Prefix with a '/' if absent.
}
if (prefix.endsWith("/")) {
prefix = prefix.substring(0, prefix.length() - 1); // Remove the trailing '/' if present.
}
return prefix;
}

public Node getNode() {
return config.getClass(NODE_SECTION, "implementation", Node.class, DEFAULT_NODE_IMPLEMENTATION);
}
Expand Down
3 changes: 2 additions & 1 deletion java/src/org/openqa/selenium/grid/node/httpd/NodeServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,8 @@ protected Handlers createHandlers(Config config) {

Route httpHandler = Route.combine(node, get("/readyz").to(() -> readinessCheck));

return new Handlers(httpHandler, new ProxyNodeWebsockets(clientFactory, node));
return new Handlers(
httpHandler, new ProxyNodeWebsockets(clientFactory, node, nodeOptions.getGridSubPath()));
}

@Override
Expand Down
4 changes: 1 addition & 3 deletions java/src/org/openqa/selenium/grid/node/local/LocalNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -838,9 +838,7 @@ private Session createExternalSession(
private URI rewrite(String path) {
try {
String scheme = "https".equals(gridUri.getScheme()) ? "wss" : "ws";
if (gridUri.getPath() != null && !gridUri.getPath().equals("/")) {
path = gridUri.getPath() + path;
}
path = NodeOptions.normalizeSubPath(gridUri.getPath()) + path;
return new URI(
scheme, gridUri.getUserInfo(), gridUri.getHost(), gridUri.getPort(), path, null, null);
} catch (URISyntaxException e) {
Expand Down
14 changes: 14 additions & 0 deletions java/src/org/openqa/selenium/remote/http/UrlTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ public UrlTemplate.Match match(String matchAgainst) {
return compiled.apply(matchAgainst);
}

/**
* @return A {@link Match} with all parameters filled if successful, null otherwise. Remove
* subPath from matchAgainst before matching.
*/
public UrlTemplate.Match match(String matchAgainst, String prefix) {
if (matchAgainst == null || prefix == null) {
return null;
}
if (!prefix.isEmpty() && !prefix.equals("/")) {
matchAgainst = matchAgainst.replaceFirst(prefix, "");
}
return match(matchAgainst);
}

@SuppressWarnings("InnerClassMayBeStatic")
public class Match {
private final String url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -673,6 +673,28 @@ void settingTheHubWithDefaultValueSetsTheGridUrlToTheNonLoopbackAddress() {
.isEqualTo(Optional.of(URI.create(nonLoopbackAddressUrl)));
}

@Test
void settingSubPathForNodeServerExtractFromGridUrl() {
String[] rawConfig =
new String[] {
"[node]", "grid-url = \"http://localhost:4444/mySubPath\"",
};
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
NodeOptions nodeOptions = new NodeOptions(config);
assertThat(nodeOptions.getGridSubPath()).isEqualTo("/mySubPath");
}

@Test
void settingSubPathForNodeServerExtractFromHub() {
String[] rawConfig =
new String[] {
"[node]", "hub = \"http://0.0.0.0:4444/mySubPath\"",
};
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
NodeOptions nodeOptions = new NodeOptions(config);
assertThat(nodeOptions.getGridSubPath()).isEqualTo("/mySubPath");
}

@Test
void notSettingSlotMatcherAvailable() {
String[] rawConfig =
Expand Down
18 changes: 18 additions & 0 deletions java/test/org/openqa/selenium/remote/http/UrlTemplateTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,24 @@ void itIsFineForTheFirstCharacterToBeAPattern() {
assertThat(match.getParameters()).isEqualTo(ImmutableMap.of("cake", "cheese"));
}

@Test
void shouldMatchAgainstUrlTemplateExcludePrefix() {
UrlTemplate.Match match =
new UrlTemplate("/session/{id}/se/vnc").match("/prefix/session/1234/se/vnc", "/prefix");

assertThat(match.getUrl()).isEqualTo("/session/1234/se/vnc");
assertThat(match.getParameters()).isEqualTo(ImmutableMap.of("id", "1234"));
}

@Test
void shouldMatchAgainstUrlTemplateWithEmptyPrefix() {
UrlTemplate.Match match =
new UrlTemplate("/session/{id}/se/vnc").match("/session/1234/se/vnc", "");

assertThat(match.getUrl()).isEqualTo("/session/1234/se/vnc");
assertThat(match.getParameters()).isEqualTo(ImmutableMap.of("id", "1234"));
}

@Test
void aNullMatchDoesNotCauseANullPointerExceptionToBeThrown() {
assertThat(new UrlTemplate("/").match(null)).isNull();
Expand Down
Loading