From 8365ad7c128595c2f1247c9245af0d39d9b9d9af Mon Sep 17 00:00:00 2001 From: Eduardo Martins Date: Wed, 6 Nov 2024 16:44:12 +0100 Subject: [PATCH] [WFLY-19800] properly builds race environment for https forwarding env (such as OpenShift) --- .../threadracing/EnvironmentProperties.java | 5 ++ .../threadracing/WebSocketRace.java | 48 +++++++++---------- .../stage/jaxrs/JAXRSRaceStage.java | 3 +- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/EnvironmentProperties.java b/thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/EnvironmentProperties.java index 69c97c2768..afb03606bf 100644 --- a/thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/EnvironmentProperties.java +++ b/thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/EnvironmentProperties.java @@ -37,4 +37,9 @@ public interface EnvironmentProperties { * the app's root path, e.g. /thread-racing */ String ROOT_PATH = "ROOT_PATH"; + + /** + * the app's protocol + */ + String PROTOCOL = "PROTOCOL"; } diff --git a/thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/WebSocketRace.java b/thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/WebSocketRace.java index d0e561a49e..a9865d9909 100644 --- a/thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/WebSocketRace.java +++ b/thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/WebSocketRace.java @@ -30,6 +30,7 @@ import jakarta.websocket.server.ServerEndpointConfig; import java.io.IOException; import java.util.HashMap; +import java.util.List; import java.util.Map; /** @@ -79,9 +80,10 @@ public class WebSocketRace { * @param session */ @OnOpen + @SuppressWarnings("unchecked") public void onOpen(Session session) { try { - new Race(racer1, racer2, racer3, racer4, buildRaceEnvironment(session), new WebSocketRaceBroadcaster(session), raceResults).run(); + new Race(racer1, racer2, racer3, racer4, (Map) session.getUserProperties().get(ServerEndpointConfigurator.ENV_USER_PROP), new WebSocketRaceBroadcaster(session), raceResults).run(); } catch (Exception e) { e.printStackTrace(); } finally { @@ -92,36 +94,34 @@ public void onOpen(Session session) { } } - /** - * Builds the race's environment, from the specified session. - * @param session - * @return - */ - private Map buildRaceEnvironment(Session session) { - final Map environment = new HashMap<>(); - final String host = (String) session.getUserProperties().get(ServerEndpointConfigurator.HOST_USER_PROP); - if (host != null) { - final String[] hostSplit = host.split(":"); - environment.put(EnvironmentProperties.SERVER_NAME, hostSplit[0]); - environment.put(EnvironmentProperties.SERVER_PORT, (hostSplit.length > 1 ? hostSplit[1] : "80")); - } - // extract the root path from the session's request uri, which starting with websockets 2.1 is an absolute uri - final String absoluteRequestURI = session.getRequestURI().toString(); - final String relativeRequestUri = absoluteRequestURI.substring(absoluteRequestURI.indexOf(host)+host.length()); - final String rootPath = relativeRequestUri.equals(PATH) ? "" : relativeRequestUri.substring(0, (relativeRequestUri.length() - PATH.length())); - environment.put(EnvironmentProperties.ROOT_PATH, rootPath); - return environment; - } - /** * This configurator will capture the environment properties, when handshaking a client. */ public static class ServerEndpointConfigurator extends ServerEndpointConfig.Configurator { - static final String HOST_USER_PROP = "Host"; + static final String ENV_USER_PROP = "env"; @Override public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) { - sec.getUserProperties().put(HOST_USER_PROP, request.getHeaders().get(HOST_USER_PROP).get(0)); + // let's build the race environment + final Map environment = new HashMap<>(); + sec.getUserProperties().put(ENV_USER_PROP, environment); + final List xForwardedProto = request.getHeaders().get("x-forwarded-proto"); + if (xForwardedProto == null || xForwardedProto.isEmpty()) { + // not using forward, assume http and use host header to figure out host and port + environment.put(EnvironmentProperties.PROTOCOL, "http"); + final String hostHeader = request.getHeaders().get("host").get(0); + final String[] hostSplit = hostHeader.split(":"); + environment.put(EnvironmentProperties.SERVER_NAME, hostSplit[0]); + environment.put(EnvironmentProperties.SERVER_PORT, (hostSplit.length > 1 ? hostSplit[1] : "80")); + } else { + // using forward + environment.put(EnvironmentProperties.PROTOCOL, xForwardedProto.get(0)); + environment.put(EnvironmentProperties.SERVER_NAME, request.getHeaders().get("x-forwarded-host").get(0)); + environment.put(EnvironmentProperties.SERVER_PORT, request.getHeaders().get("x-forwarded-port").get(0)); + } + final String relativeRequestUri = request.getRequestURI().toString(); + final String rootPath = relativeRequestUri.equals(PATH) ? "" : relativeRequestUri.substring(0, (relativeRequestUri.length() - PATH.length())); + environment.put(EnvironmentProperties.ROOT_PATH, rootPath); } } } diff --git a/thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/stage/jaxrs/JAXRSRaceStage.java b/thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/stage/jaxrs/JAXRSRaceStage.java index 5e2787daae..e373995363 100644 --- a/thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/stage/jaxrs/JAXRSRaceStage.java +++ b/thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/stage/jaxrs/JAXRSRaceStage.java @@ -38,7 +38,8 @@ public class JAXRSRaceStage implements RaceStage { public void run(Race.Registration registration) throws Exception { // build the REST service uri from race's environment final Map environment = registration.getEnvironment(); - final String pitStopURI = new StringBuilder("http://") + final String pitStopURI = new StringBuilder(environment.get(EnvironmentProperties.PROTOCOL)) + .append("://") .append(environment.get(EnvironmentProperties.SERVER_NAME)) .append(':') .append(environment.get(EnvironmentProperties.SERVER_PORT))