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

[WFLY-19800] properly builds race environment for https forwarding en… #978

Merged
merged 1 commit into from
Nov 7, 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
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand Down Expand Up @@ -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<String, String>) session.getUserProperties().get(ServerEndpointConfigurator.ENV_USER_PROP), new WebSocketRaceBroadcaster(session), raceResults).run();
} catch (Exception e) {
e.printStackTrace();
} finally {
Expand All @@ -92,36 +94,34 @@ public void onOpen(Session session) {
}
}

/**
* Builds the race's environment, from the specified session.
* @param session
* @return
*/
private Map<String, String> buildRaceEnvironment(Session session) {
final Map<String, String> 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<String, String> environment = new HashMap<>();
sec.getUserProperties().put(ENV_USER_PROP, environment);
final List<String> 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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> 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))
Expand Down
Loading