Skip to content

Commit

Permalink
[grid][java]: relay service can set protocol version in fetching stat…
Browse files Browse the repository at this point in the history
…us (#13849)

* [grid][java]: relay service can set protocol version in fetching status

Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>

* [grid][java] fix code as suggestions

Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>

---------

Signed-off-by: Viet Nguyen Duc <nguyenducviet4496@gmail.com>
  • Loading branch information
VietND96 authored Apr 22, 2024
1 parent e6e7d8e commit 7dd6163
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 1 deletion.
8 changes: 8 additions & 0 deletions java/src/org/openqa/selenium/grid/node/relay/RelayFlags.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,14 @@ public class RelayFlags implements HasRoles {
@ConfigValue(section = RELAY_SECTION, name = "status-endpoint", example = "\"/status\"")
private String serviceStatusEndpoint;

@Parameter(
names = {"--service-protocol-version"},
description =
"Enforce a specific protocol version in HttpClient when communicating with the endpoint"
+ " service status")
@ConfigValue(section = RELAY_SECTION, name = "protocol-version", example = "\"HTTP/1.1\"")
private String serviceProtocolVersion;

@Override
public Set<Role> getRoles() {
return Collections.singleton(NODE_ROLE);
Expand Down
23 changes: 23 additions & 0 deletions java/src/org/openqa/selenium/grid/node/relay/RelayOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.google.common.collect.Multimap;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.http.HttpClient.Version;
import java.time.Duration;
import java.util.Collection;
import java.util.List;
Expand Down Expand Up @@ -103,6 +104,27 @@ public URI getServiceStatusUri() {
}
}

public String getServiceProtocolVersion() {
String rawProtocolVersion = config.get(RELAY_SECTION, "protocol-version").orElse("");
String protocolVersion = rawProtocolVersion;
if (protocolVersion.isEmpty()) {
return protocolVersion;
} else {
protocolVersion = normalizeProtocolVersion(protocolVersion);
}
try {
return Version.valueOf(protocolVersion).toString();
} catch (IllegalArgumentException e) {
LOG.info("Unsupported protocol version: " + protocolVersion);
throw new ConfigException("Unsupported protocol version provided: " + rawProtocolVersion, e);
}
}

private String normalizeProtocolVersion(String protocolVersion) {
// Support input in the form of "http/1.1" or "HTTP/1.1"
return protocolVersion.toUpperCase().replaceAll("/", "_").replaceAll("\\.", "_");
}

// Method being used in SessionSlot
@SuppressWarnings("unused")
private boolean isServiceUp(HttpClient client) {
Expand Down Expand Up @@ -160,6 +182,7 @@ public Map<Capabilities, Collection<SessionFactory>> getSessionFactories(
sessionTimeout,
getServiceUri(),
getServiceStatusUri(),
getServiceProtocolVersion(),
immutable));
}
LOG.info(String.format("Mapping %s, %d times", immutable, maxSessions));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ public class RelaySessionFactory implements SessionFactory {
private final Duration sessionTimeout;
private final URL serviceUrl;
private final URL serviceStatusUrl;
private final String serviceProtocolVersion;
private final Capabilities stereotype;

public RelaySessionFactory(
Expand All @@ -84,12 +85,15 @@ public RelaySessionFactory(
Duration sessionTimeout,
URI serviceUri,
URI serviceStatusUri,
String serviceProtocolVersion,
Capabilities stereotype) {
this.tracer = Require.nonNull("Tracer", tracer);
this.clientFactory = Require.nonNull("HTTP client", clientFactory);
this.sessionTimeout = Require.nonNull("Session timeout", sessionTimeout);
this.serviceUrl = createUrlFromUri(Require.nonNull("Service URL", serviceUri));
this.serviceStatusUrl = createUrlFromUri(serviceStatusUri);
this.serviceProtocolVersion =
Require.nonNull("Service protocol version", serviceProtocolVersion);
this.stereotype = ImmutableCapabilities.copyOf(Require.nonNull("Stereotype", stereotype));
}

Expand Down Expand Up @@ -209,7 +213,12 @@ public boolean isServiceUp() {
// If no status endpoint was configured, we assume the server is up.
return true;
}
try (HttpClient client = clientFactory.createClient(serviceStatusUrl)) {

ClientConfig clientConfig = ClientConfig.defaultConfig().baseUrl(serviceStatusUrl);
if (!serviceProtocolVersion.isEmpty()) {
clientConfig = clientConfig.version(serviceProtocolVersion);
}
try (HttpClient client = clientFactory.createClient(clientConfig)) {
HttpResponse response =
client.execute(new HttpRequest(HttpMethod.GET, serviceStatusUrl.toString()));
LOG.log(Debug.getDebugLogLevel(), () -> Contents.string(response));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,55 @@ void statusUrlIsParsedSuccessfully() {
.isEqualTo("http://127.0.0.1:8888/statusEndpoint");
}

@Test
void protocolVersionIsParsedSuccessfully() {
String[] rawConfig =
new String[] {
"[relay]",
"host = '127.0.0.1'",
"port = '8888'",
"status-endpoint = '/statusEndpoint'",
"protocol-version = 'HTTP/1.1'",
"configs = [\"5\", '{\"browserName\": \"firefox\"}']",
};
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
RelayOptions relayOptions = new RelayOptions(config);
assertThat(relayOptions.getServiceProtocolVersion()).isEqualTo("HTTP_1_1");
rawConfig =
new String[] {
"[relay]",
"host = '127.0.0.1'",
"port = '8888'",
"status-endpoint = '/statusEndpoint'",
"protocol-version = 'HTTP_1_1'",
"configs = [\"5\", '{\"browserName\": \"firefox\"}']",
};
config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
relayOptions = new RelayOptions(config);
assertThat(relayOptions.getServiceProtocolVersion()).isEqualTo("HTTP_1_1");
}

@Test
void protocolVersionThrowsConfigException() {
String[] rawConfig =
new String[] {
"[relay]",
"host = '127.0.0.1'",
"port = '8888'",
"status-endpoint = '/statusEndpoint'",
"protocol-version = 'HTTP/0.9'",
"configs = [\"5\", '{\"browserName\": \"firefox\"}']",
};
Config config = new TomlConfig(new StringReader(String.join("\n", rawConfig)));
RelayOptions relayOptions = new RelayOptions(config);
assertThatExceptionOfType(ConfigException.class)
.isThrownBy(
() -> {
relayOptions.getServiceProtocolVersion();
})
.withMessageContaining("Unsupported protocol version provided: HTTP/0.9");
}

@Test
void missingConfigsThrowsConfigException() {
String[] rawConfig =
Expand Down

0 comments on commit 7dd6163

Please sign in to comment.