-
Notifications
You must be signed in to change notification settings - Fork 38.4k
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
Upgrade match check in WebSocketHandlerMapping does not work if handler mapped to "/*"
#34503
Comments
The Also, the |
I believe that logically, WebSocketHandlerMapping should only return handlers that meet the conditions, and webSocketUpgradeMatch, as a property of WebSocketHandlerMapping, should apply to all returned handlers. In my program, I added a WebSocketHandler with the path set to "/*" and webSocketUpgradeMatch set to true. After starting the application, Spring Boot added the defaultHandler for WebSocketHandlerMapping. However, when handling a regular HTTP request (without the Upgrade header), the defaultHandler was returned. I think WebSocketHandlerMapping should return null in this case, allowing the process to proceed to the next WebSocketHandlerMapping or return a 404 status. |
defaultHandler is meant to use as a fallback option, to send an alternative response when the request doesn't match to the expected URL paths. If you mean to start a WebSocket connection regardless of the URL path, then just map the handler to "/*" rather than relying on a default handler, especially if that's the same handler. In any case, there is no easy way to do what you're requesting since the overriding |
I am indeed registering the handler on the "/" path, and then Spring treats it as the defaultHandler. In other words, Spring considers "/" as the defaultHandler and does not perform any other condition matching.
Object mappedHandler = this.handlerMap.get(urlPath);
if (mappedHandler != null) {
if (mappedHandler != resolvedHandler) {
throw new IllegalStateException(
"Cannot map " + getHandlerDescription(handler) + " to URL path [" + urlPath +
"]: There is already " + getHandlerDescription(mappedHandler) + " mapped.");
}
}
else {
if (urlPath.equals("/")) {
if (logger.isTraceEnabled()) {
logger.trace("Root mapping to " + getHandlerDescription(handler));
}
setRootHandler(resolvedHandler);
}
else if (urlPath.equals("/*")) {
if (logger.isTraceEnabled()) {
logger.trace("Default mapping to " + getHandlerDescription(handler));
}
setDefaultHandler(resolvedHandler);
}
else {
this.handlerMap.put(urlPath, resolvedHandler);
if (getPatternParser() != null) {
this.pathPatternHandlerMap.put(getPatternParser().parse(urlPath), resolvedHandler);
}
if (logger.isTraceEnabled()) {
logger.trace("Mapped [" + urlPath + "] onto " + getHandlerDescription(handler));
}
}
} |
That's a good point. The treatment of |
"/*"
I am changing the target to 7.0 since the change has some potential to affect existing applications. In the mean time, as a workaround, you could use a different pattern that achieves the same outcome, e.g. |
I register a websocket with path '/*' and also called webSocketHandlerMapping.setWebSocketUpgradeMatch(true) in my program. When I send a common http request without
Upgrade
header, WebSocketHandlerMapping aslo returned defaultHandler. I think webSocketUpgradeMatch should take effect for all handler include defaultHandler and getHandler() should return null when webSocketUpgradeMatch=true and noUpgrade
header present.The text was updated successfully, but these errors were encountered: