-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
WebSockets Next: add convenient way to handle the subprotocol header #39465
Comments
For the record, it seems that Vert.x only makes it possible to define supported subprotocols globally with That said, we might add the |
- add WebSocketConnection#subprotocol() that can be used to obtain the subprotocol selected by the handshake - the values defined with quarkus.websockets-next.supported-subprotocols contribute to the set of subprotocols passed to the HTTP server configuration - also add constants for handshake headers defined by the RFC - resolves quarkusio#39465
- add WebSocketConnection#subprotocol() that can be used to obtain the subprotocol selected by the handshake - the values defined with quarkus.websockets-next.supported-subprotocols contribute to the set of subprotocols passed to the HTTP server configuration - also add constants for handshake headers defined by the RFC - resolves quarkusio#39465
- add WebSocketConnection#subprotocol() that can be used to obtain the subprotocol selected by the handshake - the values defined with quarkus.websockets-next.supported-subprotocols contribute to the set of subprotocols passed to the HTTP server configuration - also add constants for handshake headers defined by the RFC - resolves quarkusio#39465
@mkouba I stumbled across this while reviewing the latest WebSockets Next work (nice job, by the way, it's looking good!). I wanted to pass along an interesting thing I discovered while playing around with the low-level Vert.x websockets support. As it turns out, Netty has a magic value you can configure so that it will accept any subprotocol: I was able to configure this using the normal Quarkus mechanism: import io.quarkus.vertx.http.HttpServerOptionsCustomizer;
import io.vertx.core.http.HttpServerOptions;
import jakarta.enterprise.context.ApplicationScoped;
import java.util.List;
import static io.netty.handler.codec.http.websocketx.WebSocketServerHandshaker.SUB_PROTOCOL_WILDCARD;
@ApplicationScoped
public class VertxHttpConfiguration implements HttpServerOptionsCustomizer {
@Override
public void customizeHttpServer(HttpServerOptions options) {
allowAnySubProtocol(options);
}
@Override
public void customizeHttpsServer(HttpServerOptions options) {
allowAnySubProtocol(options);
}
@Override
public void customizeDomainSocketServer(HttpServerOptions options) {
allowAnySubProtocol(options);
}
// configure the wildcard protocol so that Vert.x/Netty will use anything we rewrite in the request
private void allowAnySubProtocol(HttpServerOptions options) {
options.setWebSocketSubProtocols(List.of(SUB_PROTOCOL_WILDCARD));
}
} Then, in a reactive route I can negotiate the sub-protocol myself and whatever I write into the header will be returned by Netty: @Route(path = "/my/socket", methods = GET)
public void openSocket(HttpServerRequest request) {
var requestedProtocols = request.headers().get(SEC_WEBSOCKET_PROTOCOL);
var negotiatedProtocol = negotiateProtocol(requestedProtocols);
// rewrite the protocol header to control what Vert.x/Netty uses in the response
request.headers().set(SEC_WEBSOCKET_PROTOCOL, negotiatedProtocol);
request.toWebSocket()
.onItem().invoke(this::connectSocket)
.onFailure().invoke(failure -> log.error("failed to upgrade to websocket", failure))
.subscribe().with(IGNORE);
} A bit of a hack, but could be used to enable programmatic sub-protocol negotiation in WebSockets Next. |
@emattheis Thanks for your feedback! First of all, I don't think that you need to use the As for the negotiation - recently (3.12.0.CR1) we added the |
@mkouba I did this without the new extension, so the customizer seemed like the my only option. I’ll be glad to have the config property instead 😛 I’ll see if I can make the same trick work with the upgrade check, although perhaps it should be renamed as an interceptor if the intent is to allow altering the upgrade behavior instead of just inspecting/rejecting? |
We wanted to avoid confusion with CDI interceptors. The name is not set in stone yet but I can't think of a better name 🤔. |
Description
https://datatracker.ietf.org/doc/html/rfc6455#section-11.3.4
Currently, the only thing a user can do is obtain the header value with
connection.handshakeRequest().header("Sec-WebSocket-Protocol")
.Implementation ideas
No response
The text was updated successfully, but these errors were encountered: