3030import jakarta .websocket .server .ServerEndpointConfig ;
3131import java .io .IOException ;
3232import java .util .HashMap ;
33+ import java .util .List ;
3334import 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}
0 commit comments