use of org.xnio.http.ExtendedHandshakeChecker in project undertow by undertow-io.
the class WebSocket13ClientHandshake method handshakeChecker.
@Override
public ExtendedHandshakeChecker handshakeChecker(final URI uri, final Map<String, List<String>> requestHeaders) {
final String sentKey = requestHeaders.containsKey(Headers.SEC_WEB_SOCKET_KEY_STRING) ? requestHeaders.get(Headers.SEC_WEB_SOCKET_KEY_STRING).get(0) : null;
return new ExtendedHandshakeChecker() {
@Override
public void checkHandshakeExtended(Map<String, List<String>> headers) throws IOException {
try {
if (negotiation != null) {
negotiation.afterRequest(headers);
}
String upgrade = getFirst(Headers.UPGRADE_STRING, headers);
if (upgrade == null || !upgrade.trim().equalsIgnoreCase("websocket")) {
throw WebSocketMessages.MESSAGES.noWebSocketUpgradeHeader();
}
String connHeader = getFirst(Headers.CONNECTION_STRING, headers);
if (connHeader == null || !connHeader.trim().equalsIgnoreCase("upgrade")) {
throw WebSocketMessages.MESSAGES.noWebSocketConnectionHeader();
}
String acceptKey = getFirst(Headers.SEC_WEB_SOCKET_ACCEPT_STRING, headers);
final String dKey = solve(sentKey);
if (!dKey.equals(acceptKey)) {
throw WebSocketMessages.MESSAGES.webSocketAcceptKeyMismatch(dKey, acceptKey);
}
if (negotiation != null) {
String subProto = getFirst(Headers.SEC_WEB_SOCKET_PROTOCOL_STRING, headers);
if (subProto != null && !subProto.isEmpty() && !negotiation.getSupportedSubProtocols().contains(subProto)) {
throw WebSocketMessages.MESSAGES.unsupportedProtocol(subProto, negotiation.getSupportedSubProtocols());
}
List<WebSocketExtension> extensions = Collections.emptyList();
String extHeader = getFirst(Headers.SEC_WEB_SOCKET_EXTENSIONS_STRING, headers);
if (extHeader != null) {
extensions = WebSocketExtension.parse(extHeader);
}
negotiation.handshakeComplete(subProto, extensions);
}
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new IOException(e);
}
}
};
}
Aggregations