Fix handshake handling issue
This commit is contained in:
parent
46bcffcf30
commit
9ca4672300
|
|
@ -198,6 +198,10 @@ public final class CloseStatus {
|
||||||
return (this.code == otherStatus.code && ObjectUtils.nullSafeEquals(this.reason, otherStatus.reason));
|
return (this.code == otherStatus.code && ObjectUtils.nullSafeEquals(this.reason, otherStatus.reason));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean equalsCode(CloseStatus other) {
|
||||||
|
return this.code == other.code;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return "CloseStatus [code=" + this.code + ", reason=" + this.reason + "]";
|
return "CloseStatus [code=" + this.code + ", reason=" + this.reason + "]";
|
||||||
|
|
|
||||||
|
|
@ -27,9 +27,12 @@ import javax.websocket.ClientEndpointConfig;
|
||||||
import javax.websocket.ClientEndpointConfig.Configurator;
|
import javax.websocket.ClientEndpointConfig.Configurator;
|
||||||
import javax.websocket.ContainerProvider;
|
import javax.websocket.ContainerProvider;
|
||||||
import javax.websocket.Endpoint;
|
import javax.websocket.Endpoint;
|
||||||
|
import javax.websocket.HandshakeResponse;
|
||||||
import javax.websocket.Session;
|
import javax.websocket.Session;
|
||||||
import javax.websocket.WebSocketContainer;
|
import javax.websocket.WebSocketContainer;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.springframework.http.HttpHeaders;
|
import org.springframework.http.HttpHeaders;
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
import org.springframework.websocket.WebSocketHandler;
|
import org.springframework.websocket.WebSocketHandler;
|
||||||
|
|
@ -47,6 +50,8 @@ import org.springframework.websocket.client.WebSocketConnectFailureException;
|
||||||
*/
|
*/
|
||||||
public class StandardWebSocketClient implements WebSocketClient {
|
public class StandardWebSocketClient implements WebSocketClient {
|
||||||
|
|
||||||
|
private static final Log logger = LogFactory.getLog(StandardWebSocketClient.class);
|
||||||
|
|
||||||
private static final Set<String> EXCLUDED_HEADERS = new HashSet<String>(
|
private static final Set<String> EXCLUDED_HEADERS = new HashSet<String>(
|
||||||
Arrays.asList("Sec-WebSocket-Accept", "Sec-WebSocket-Extensions", "Sec-WebSocket-Key",
|
Arrays.asList("Sec-WebSocket-Accept", "Sec-WebSocket-Extensions", "Sec-WebSocket-Key",
|
||||||
"Sec-WebSocket-Protocol", "Sec-WebSocket-Version"));
|
"Sec-WebSocket-Protocol", "Sec-WebSocket-Version"));
|
||||||
|
|
@ -83,9 +88,22 @@ public class StandardWebSocketClient implements WebSocketClient {
|
||||||
public void beforeRequest(Map<String, List<String>> headers) {
|
public void beforeRequest(Map<String, List<String>> headers) {
|
||||||
for (String headerName : httpHeaders.keySet()) {
|
for (String headerName : httpHeaders.keySet()) {
|
||||||
if (!EXCLUDED_HEADERS.contains(headerName)) {
|
if (!EXCLUDED_HEADERS.contains(headerName)) {
|
||||||
headers.put(headerName, httpHeaders.get(headerName));
|
List<String> value = httpHeaders.get(headerName);
|
||||||
|
if (logger.isTraceEnabled()) {
|
||||||
|
logger.trace("Adding header [" + headerName + "=" + value + "]");
|
||||||
|
}
|
||||||
|
headers.put(headerName, value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if (logger.isTraceEnabled()) {
|
||||||
|
logger.trace("Handshake request headers: " + headers);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void afterResponse(HandshakeResponse handshakeResponse) {
|
||||||
|
if (logger.isTraceEnabled()) {
|
||||||
|
logger.trace("Handshake response headers: " + handshakeResponse.getHeaders());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -134,6 +134,8 @@ public class JettyWebSocketClient implements WebSocketClient, SmartLifecycle {
|
||||||
public WebSocketSession doHandshake(WebSocketHandler webSocketHandler, HttpHeaders headers, URI uri)
|
public WebSocketSession doHandshake(WebSocketHandler webSocketHandler, HttpHeaders headers, URI uri)
|
||||||
throws WebSocketConnectFailureException {
|
throws WebSocketConnectFailureException {
|
||||||
|
|
||||||
|
// TODO: populate headers
|
||||||
|
|
||||||
JettyWebSocketListenerAdapter listener = new JettyWebSocketListenerAdapter(webSocketHandler);
|
JettyWebSocketListenerAdapter listener = new JettyWebSocketListenerAdapter(webSocketHandler);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,7 @@ import java.io.IOException;
|
||||||
import java.nio.charset.Charset;
|
import java.nio.charset.Charset;
|
||||||
import java.security.MessageDigest;
|
import java.security.MessageDigest;
|
||||||
import java.security.NoSuchAlgorithmException;
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
@ -34,6 +35,7 @@ import org.springframework.http.HttpStatus;
|
||||||
import org.springframework.http.server.ServerHttpRequest;
|
import org.springframework.http.server.ServerHttpRequest;
|
||||||
import org.springframework.http.server.ServerHttpResponse;
|
import org.springframework.http.server.ServerHttpResponse;
|
||||||
import org.springframework.util.ClassUtils;
|
import org.springframework.util.ClassUtils;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.springframework.websocket.WebSocketHandler;
|
import org.springframework.websocket.WebSocketHandler;
|
||||||
|
|
||||||
|
|
@ -53,7 +55,7 @@ public class DefaultHandshakeHandler implements HandshakeHandler {
|
||||||
|
|
||||||
protected Log logger = LogFactory.getLog(getClass());
|
protected Log logger = LogFactory.getLog(getClass());
|
||||||
|
|
||||||
private List<String> supportedProtocols;
|
private List<String> supportedProtocols = new ArrayList<String>();
|
||||||
|
|
||||||
private RequestUpgradeStrategy requestUpgradeStrategy;
|
private RequestUpgradeStrategy requestUpgradeStrategy;
|
||||||
|
|
||||||
|
|
@ -101,7 +103,8 @@ public class DefaultHandshakeHandler implements HandshakeHandler {
|
||||||
handleInvalidUpgradeHeader(request, response);
|
handleInvalidUpgradeHeader(request, response);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
if (!request.getHeaders().getConnection().contains("Upgrade")) {
|
if (!request.getHeaders().getConnection().contains("Upgrade") &&
|
||||||
|
!request.getHeaders().getConnection().contains("upgrade")) {
|
||||||
handleInvalidConnectHeader(request, response);
|
handleInvalidConnectHeader(request, response);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
@ -188,7 +191,7 @@ public class DefaultHandshakeHandler implements HandshakeHandler {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected String selectProtocol(List<String> requestedProtocols) {
|
protected String selectProtocol(List<String> requestedProtocols) {
|
||||||
if (requestedProtocols != null) {
|
if (CollectionUtils.isEmpty(requestedProtocols)) {
|
||||||
for (String protocol : requestedProtocols) {
|
for (String protocol : requestedProtocols) {
|
||||||
if (this.supportedProtocols.contains(protocol)) {
|
if (this.supportedProtocols.contains(protocol)) {
|
||||||
return protocol;
|
return protocol;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue