Skip to content

Commit

Permalink
[java] Update subPath for ProxyNodeWebsockets
Browse files Browse the repository at this point in the history
Signed-off-by: Viet Nguyen Duc <[email protected]>
  • Loading branch information
VietND96 committed Jan 8, 2024
1 parent b0f42c3 commit e951af5
Show file tree
Hide file tree
Showing 8 changed files with 69 additions and 10 deletions.
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
12 changes: 12 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,18 @@ 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;
}
matchAgainst = matchAgainst.replaceFirst(prefix, "");
return compiled.apply(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
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ 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 aNullMatchDoesNotCauseANullPointerExceptionToBeThrown() {
assertThat(new UrlTemplate("/").match(null)).isNull();
Expand Down

0 comments on commit e951af5

Please sign in to comment.