use of io.undertow.websockets.core.protocol.Handshake in project spring-framework by spring-projects.
the class UndertowRequestUpgradeStrategy method upgrade.
@Override
public Mono<Void> upgrade(ServerWebExchange exchange, WebSocketHandler handler, Optional<String> subProtocol) {
ServerHttpRequest request = exchange.getRequest();
Assert.isInstanceOf(UndertowServerHttpRequest.class, request, "UndertowServerHttpRequest required");
HttpServerExchange httpExchange = ((UndertowServerHttpRequest) request).getUndertowExchange();
Set<String> protocols = subProtocol.map(Collections::singleton).orElse(Collections.emptySet());
Hybi13Handshake handshake = new Hybi13Handshake(protocols, false);
List<Handshake> handshakes = Collections.singletonList(handshake);
URI url = request.getURI();
HttpHeaders headers = request.getHeaders();
Mono<Principal> principal = exchange.getPrincipal();
HandshakeInfo info = new HandshakeInfo(url, headers, principal, subProtocol);
DataBufferFactory bufferFactory = exchange.getResponse().bufferFactory();
try {
DefaultCallback callback = new DefaultCallback(info, handler, bufferFactory);
new WebSocketProtocolHandshakeHandler(handshakes, callback).handleRequest(httpExchange);
} catch (Exception ex) {
return Mono.error(ex);
}
return Mono.empty();
}
use of io.undertow.websockets.core.protocol.Handshake in project undertow by undertow-io.
the class ServerWebSocketContainer method handshakes.
static WebSocketHandshakeHolder handshakes(ConfiguredServerEndpoint config, List<ExtensionHandshake> extensions) {
List<Handshake> handshakes = new ArrayList<>();
Handshake jsrHybi13Handshake = new JsrHybi13Handshake(config);
Handshake jsrHybi08Handshake = new JsrHybi08Handshake(config);
Handshake jsrHybi07Handshake = new JsrHybi07Handshake(config);
for (ExtensionHandshake extension : extensions) {
jsrHybi13Handshake.addExtension(extension);
jsrHybi08Handshake.addExtension(extension);
jsrHybi07Handshake.addExtension(extension);
}
handshakes.add(jsrHybi13Handshake);
handshakes.add(jsrHybi08Handshake);
handshakes.add(jsrHybi07Handshake);
return new WebSocketHandshakeHolder(handshakes, config);
}
use of io.undertow.websockets.core.protocol.Handshake in project undertow by undertow-io.
the class WebSocketServlet method doGet.
@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
final ServletWebSocketHttpExchange facade = new ServletWebSocketHttpExchange(req, resp, peerConnections);
Handshake handshaker = null;
for (Handshake method : handshakes) {
if (method.matches(facade)) {
handshaker = method;
break;
}
}
if (handshaker == null) {
UndertowLogger.REQUEST_LOGGER.debug("Could not find hand shaker for web socket request");
resp.sendError(StatusCodes.BAD_REQUEST);
return;
}
final Handshake selected = handshaker;
facade.upgradeChannel(new HttpUpgradeListener() {
@Override
public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
WebSocketChannel channel = selected.createChannel(facade, streamConnection, facade.getBufferPool());
peerConnections.add(channel);
callback.onConnect(facade, channel);
}
});
handshaker.handshake(facade);
}
use of io.undertow.websockets.core.protocol.Handshake in project undertow by undertow-io.
the class WebSocketProtocolHandshakeHandler method handleRequest.
@Override
public void handleRequest(final HttpServerExchange exchange) throws Exception {
if (!exchange.getRequestMethod().equals(Methods.GET)) {
// Only GET is supported to start the handshake
next.handleRequest(exchange);
return;
}
final AsyncWebSocketHttpServerExchange facade = new AsyncWebSocketHttpServerExchange(exchange, peerConnections);
Handshake handshaker = null;
for (Handshake method : handshakes) {
if (method.matches(facade)) {
handshaker = method;
break;
}
}
if (handshaker == null) {
next.handleRequest(exchange);
} else {
WebSocketLogger.REQUEST_LOGGER.debugf("Attempting websocket handshake with %s on %s", handshaker, exchange);
final Handshake selected = handshaker;
if (upgradeListener == null) {
exchange.upgradeChannel(new HttpUpgradeListener() {
@Override
public void handleUpgrade(StreamConnection streamConnection, HttpServerExchange exchange) {
WebSocketChannel channel = selected.createChannel(facade, streamConnection, facade.getBufferPool());
peerConnections.add(channel);
callback.onConnect(facade, channel);
}
});
} else {
exchange.upgradeChannel(upgradeListener);
}
handshaker.handshake(facade);
}
}
use of io.undertow.websockets.core.protocol.Handshake in project undertow by undertow-io.
the class WebSocketServlet method handshakes.
protected List<Handshake> handshakes() {
List<Handshake> handshakes = new ArrayList<>();
handshakes.add(new Hybi13Handshake());
handshakes.add(new Hybi08Handshake());
handshakes.add(new Hybi07Handshake());
return handshakes;
}
Aggregations