use of org.springframework.web.socket.WebSocketExtension in project spring-framework by spring-projects.
the class AbstractHandshakeHandler method doHandshake.
@Override
public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws HandshakeFailureException {
WebSocketHttpHeaders headers = new WebSocketHttpHeaders(request.getHeaders());
if (logger.isTraceEnabled()) {
logger.trace("Processing request " + request.getURI() + " with headers=" + headers);
}
try {
if (HttpMethod.GET != request.getMethod()) {
response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED);
response.getHeaders().setAllow(Collections.singleton(HttpMethod.GET));
if (logger.isErrorEnabled()) {
logger.error("Handshake failed due to unexpected HTTP method: " + request.getMethod());
}
return false;
}
if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
handleInvalidUpgradeHeader(request, response);
return false;
}
if (!headers.getConnection().contains("Upgrade") && !headers.getConnection().contains("upgrade")) {
handleInvalidConnectHeader(request, response);
return false;
}
if (!isWebSocketVersionSupported(headers)) {
handleWebSocketVersionNotSupported(request, response);
return false;
}
if (!isValidOrigin(request)) {
response.setStatusCode(HttpStatus.FORBIDDEN);
return false;
}
String wsKey = headers.getSecWebSocketKey();
if (wsKey == null) {
if (logger.isErrorEnabled()) {
logger.error("Missing \"Sec-WebSocket-Key\" header");
}
response.setStatusCode(HttpStatus.BAD_REQUEST);
return false;
}
} catch (IOException ex) {
throw new HandshakeFailureException("Response update failed during upgrade to WebSocket: " + request.getURI(), ex);
}
String subProtocol = selectProtocol(headers.getSecWebSocketProtocol(), wsHandler);
List<WebSocketExtension> requested = headers.getSecWebSocketExtensions();
List<WebSocketExtension> supported = this.requestUpgradeStrategy.getSupportedExtensions(request);
List<WebSocketExtension> extensions = filterRequestedExtensions(request, requested, supported);
Principal user = determineUser(request, wsHandler, attributes);
if (logger.isTraceEnabled()) {
logger.trace("Upgrading to WebSocket, subProtocol=" + subProtocol + ", extensions=" + extensions);
}
this.requestUpgradeStrategy.upgrade(request, response, subProtocol, extensions, user, wsHandler, attributes);
return true;
}
use of org.springframework.web.socket.WebSocketExtension in project spring-framework by spring-projects.
the class JettyRequestUpgradeStrategy method buildWebSocketExtensions.
private List<WebSocketExtension> buildWebSocketExtensions() {
Set<String> names = this.factory.getExtensionFactory().getExtensionNames();
List<WebSocketExtension> result = new ArrayList<>(names.size());
for (String name : names) {
result.add(new WebSocketExtension(name));
}
return result;
}
use of org.springframework.web.socket.WebSocketExtension in project spring-framework by spring-projects.
the class AbstractStandardUpgradeStrategy method upgrade.
@Override
public void upgrade(ServerHttpRequest request, ServerHttpResponse response, String selectedProtocol, List<WebSocketExtension> selectedExtensions, Principal user, WebSocketHandler wsHandler, Map<String, Object> attrs) throws HandshakeFailureException {
HttpHeaders headers = request.getHeaders();
InetSocketAddress localAddr = null;
try {
localAddr = request.getLocalAddress();
} catch (Exception ex) {
// Ignore
}
InetSocketAddress remoteAddr = null;
try {
remoteAddr = request.getRemoteAddress();
} catch (Exception ex) {
// Ignore
}
StandardWebSocketSession session = new StandardWebSocketSession(headers, attrs, localAddr, remoteAddr, user);
StandardWebSocketHandlerAdapter endpoint = new StandardWebSocketHandlerAdapter(wsHandler, session);
List<Extension> extensions = new ArrayList<>();
for (WebSocketExtension extension : selectedExtensions) {
extensions.add(new WebSocketToStandardExtensionAdapter(extension));
}
upgradeInternal(request, response, selectedProtocol, extensions, endpoint);
}
use of org.springframework.web.socket.WebSocketExtension in project spring-framework by spring-projects.
the class JettyWebSocketSession method initializeNativeSession.
@Override
public void initializeNativeSession(Session session) {
super.initializeNativeSession(session);
this.id = ObjectUtils.getIdentityHexString(getNativeSession());
this.uri = session.getUpgradeRequest().getRequestURI();
HttpHeaders headers = new HttpHeaders();
headers.putAll(session.getUpgradeRequest().getHeaders());
this.headers = HttpHeaders.readOnlyHttpHeaders(headers);
this.acceptedProtocol = session.getUpgradeResponse().getAcceptedSubProtocol();
List<ExtensionConfig> jettyExtensions = session.getUpgradeResponse().getExtensions();
if (!CollectionUtils.isEmpty(jettyExtensions)) {
List<WebSocketExtension> extensions = new ArrayList<>(jettyExtensions.size());
for (ExtensionConfig jettyExtension : jettyExtensions) {
extensions.add(new WebSocketExtension(jettyExtension.getName(), jettyExtension.getParameters()));
}
this.extensions = Collections.unmodifiableList(extensions);
} else {
this.extensions = Collections.emptyList();
}
if (this.user == null) {
this.user = session.getUpgradeRequest().getUserPrincipal();
}
}
use of org.springframework.web.socket.WebSocketExtension in project spring-framework by spring-projects.
the class AbstractWebSocketClient method doHandshake.
@Override
public final ListenableFuture<WebSocketSession> doHandshake(WebSocketHandler webSocketHandler, WebSocketHttpHeaders headers, URI uri) {
Assert.notNull(webSocketHandler, "WebSocketHandler must not be null");
assertUri(uri);
if (logger.isDebugEnabled()) {
logger.debug("Connecting to " + uri);
}
HttpHeaders headersToUse = new HttpHeaders();
if (headers != null) {
for (String header : headers.keySet()) {
if (!specialHeaders.contains(header.toLowerCase())) {
headersToUse.put(header, headers.get(header));
}
}
}
List<String> subProtocols = (headers != null && headers.getSecWebSocketProtocol() != null ? headers.getSecWebSocketProtocol() : Collections.emptyList());
List<WebSocketExtension> extensions = (headers != null && headers.getSecWebSocketExtensions() != null ? headers.getSecWebSocketExtensions() : Collections.emptyList());
return doHandshakeInternal(webSocketHandler, headersToUse, uri, subProtocols, extensions, Collections.emptyMap());
}
Aggregations