Skip to content

Commit c4f466f

Browse files
authored
Merge pull request #2136 from emmartins/JBEAP-27650
[WFLY-19800] properly builds race environment for https forwarding en…
2 parents 6a5d921 + 9dad0f7 commit c4f466f

File tree

3 files changed

+31
-25
lines changed

3 files changed

+31
-25
lines changed

thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/EnvironmentProperties.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,9 @@ public interface EnvironmentProperties {
3737
* the app's root path, e.g. /thread-racing
3838
*/
3939
String ROOT_PATH = "ROOT_PATH";
40+
41+
/**
42+
* the app's protocol
43+
*/
44+
String PROTOCOL = "PROTOCOL";
4045
}

thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/WebSocketRace.java

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import jakarta.websocket.server.ServerEndpointConfig;
3131
import java.io.IOException;
3232
import java.util.HashMap;
33+
import java.util.List;
3334
import java.util.Map;
3435

3536
/**
@@ -79,9 +80,10 @@ public class WebSocketRace {
7980
* @param session
8081
*/
8182
@OnOpen
83+
@SuppressWarnings("unchecked")
8284
public void onOpen(Session session) {
8385
try {
84-
new Race(racer1, racer2, racer3, racer4, buildRaceEnvironment(session), new WebSocketRaceBroadcaster(session), raceResults).run();
86+
new Race(racer1, racer2, racer3, racer4, (Map<String, String>) session.getUserProperties().get(ServerEndpointConfigurator.ENV_USER_PROP), new WebSocketRaceBroadcaster(session), raceResults).run();
8587
} catch (Exception e) {
8688
e.printStackTrace();
8789
} finally {
@@ -92,36 +94,34 @@ public void onOpen(Session session) {
9294
}
9395
}
9496

95-
/**
96-
* Builds the race's environment, from the specified session.
97-
* @param session
98-
* @return
99-
*/
100-
private Map<String, String> buildRaceEnvironment(Session session) {
101-
final Map<String, String> environment = new HashMap<>();
102-
final String host = (String) session.getUserProperties().get(ServerEndpointConfigurator.HOST_USER_PROP);
103-
if (host != null) {
104-
final String[] hostSplit = host.split(":");
105-
environment.put(EnvironmentProperties.SERVER_NAME, hostSplit[0]);
106-
environment.put(EnvironmentProperties.SERVER_PORT, (hostSplit.length > 1 ? hostSplit[1] : "80"));
107-
}
108-
// extract the root path from the session's request uri, which starting with websockets 2.1 is an absolute uri
109-
final String absoluteRequestURI = session.getRequestURI().toString();
110-
final String relativeRequestUri = absoluteRequestURI.substring(absoluteRequestURI.indexOf(host)+host.length());
111-
final String rootPath = relativeRequestUri.equals(PATH) ? "" : relativeRequestUri.substring(0, (relativeRequestUri.length() - PATH.length()));
112-
environment.put(EnvironmentProperties.ROOT_PATH, rootPath);
113-
return environment;
114-
}
115-
11697
/**
11798
* This configurator will capture the environment properties, when handshaking a client.
11899
*/
119100
public static class ServerEndpointConfigurator extends ServerEndpointConfig.Configurator {
120-
static final String HOST_USER_PROP = "Host";
101+
static final String ENV_USER_PROP = "env";
121102

122103
@Override
123104
public void modifyHandshake(ServerEndpointConfig sec, HandshakeRequest request, HandshakeResponse response) {
124-
sec.getUserProperties().put(HOST_USER_PROP, request.getHeaders().get(HOST_USER_PROP).get(0));
105+
// let's build the race environment
106+
final Map<String, String> environment = new HashMap<>();
107+
sec.getUserProperties().put(ENV_USER_PROP, environment);
108+
final List<String> xForwardedProto = request.getHeaders().get("x-forwarded-proto");
109+
if (xForwardedProto == null || xForwardedProto.isEmpty()) {
110+
// not using forward, assume http and use host header to figure out host and port
111+
environment.put(EnvironmentProperties.PROTOCOL, "http");
112+
final String hostHeader = request.getHeaders().get("host").get(0);
113+
final String[] hostSplit = hostHeader.split(":");
114+
environment.put(EnvironmentProperties.SERVER_NAME, hostSplit[0]);
115+
environment.put(EnvironmentProperties.SERVER_PORT, (hostSplit.length > 1 ? hostSplit[1] : "80"));
116+
} else {
117+
// using forward
118+
environment.put(EnvironmentProperties.PROTOCOL, xForwardedProto.get(0));
119+
environment.put(EnvironmentProperties.SERVER_NAME, request.getHeaders().get("x-forwarded-host").get(0));
120+
environment.put(EnvironmentProperties.SERVER_PORT, request.getHeaders().get("x-forwarded-port").get(0));
121+
}
122+
final String relativeRequestUri = request.getRequestURI().toString();
123+
final String rootPath = relativeRequestUri.equals(PATH) ? "" : relativeRequestUri.substring(0, (relativeRequestUri.length() - PATH.length()));
124+
environment.put(EnvironmentProperties.ROOT_PATH, rootPath);
125125
}
126126
}
127127
}

thread-racing/src/main/java/org/jboss/as/quickstarts/threadracing/stage/jaxrs/JAXRSRaceStage.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public class JAXRSRaceStage implements RaceStage {
3838
public void run(Race.Registration registration) throws Exception {
3939
// build the REST service uri from race's environment
4040
final Map<String, String> environment = registration.getEnvironment();
41-
final String pitStopURI = new StringBuilder("http://")
41+
final String pitStopURI = new StringBuilder(environment.get(EnvironmentProperties.PROTOCOL))
42+
.append("://")
4243
.append(environment.get(EnvironmentProperties.SERVER_NAME))
4344
.append(':')
4445
.append(environment.get(EnvironmentProperties.SERVER_PORT))

0 commit comments

Comments
 (0)