Skip to content

Commit af40d3f

Browse files
committed
fixes #10 update protocol logic
1 parent dd13a40 commit af40d3f

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed

websocket-router/src/main/java/com/networknt/websocket/router/WebSocketRouterHandler.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -95,13 +95,23 @@ public boolean isEnabled() {
9595

9696
@Override
9797
public void onConnect(WebSocketHttpExchange exchange, WebSocketChannel channel) {
98-
String protocol = null;
99-
if (exchange.getRequestURI().startsWith(WsAttributes.WEBSOCKET_SECURE_PROTOCOL)) {
98+
// Detect protocol from URI if it's absolute, otherwise use a safe default or check context.
99+
// The previous startsWith check was always false for relative paths like /chat.
100+
// Detect protocol from outgoing request URI if absolute, otherwise use a safe default or check X-Forwarded-Proto
101+
String protocol = WsAttributes.WEBSOCKET_PROTOCOL;
102+
String xForwardedProto = exchange.getRequestHeader("X-Forwarded-Proto");
103+
if ("https".equalsIgnoreCase(xForwardedProto)) {
100104
protocol = WsAttributes.WEBSOCKET_SECURE_PROTOCOL;
101105
} else {
102-
protocol = WsAttributes.WEBSOCKET_PROTOCOL;
106+
// Check if the current exchange URI is absolute and starts with wss
107+
String requestURI = exchange.getRequestURI();
108+
if (requestURI != null && requestURI.startsWith(WsAttributes.WEBSOCKET_SECURE_PROTOCOL)) {
109+
protocol = WsAttributes.WEBSOCKET_SECURE_PROTOCOL;
110+
}
103111
}
104112

113+
if (LOG.isTraceEnabled()) LOG.trace("Detected protocol: {} from request URI: {}", protocol, exchange.getRequestURI());
114+
105115
String serviceId = exchange.getRequestHeader("service_id");
106116
if(serviceId == null) {
107117
String requestURI = exchange.getRequestURI();

0 commit comments

Comments
 (0)