Skip to content

Commit e2bf022

Browse files
committed
Fix HTTP/2 CONNECT WebSocket upgrades (RFC 8441)
1 parent f477c16 commit e2bf022

File tree

2 files changed

+19
-15
lines changed

2 files changed

+19
-15
lines changed

spring-websocket/src/main/java/org/springframework/web/socket/WebSocketHttpHeaders.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ public void setSecWebSocketProtocol(List<String> secWebSocketProtocols) {
151151
}
152152

153153
/**
154-
* Returns the value of the {@code Sec-WebSocket-Key} header.
154+
* Returns the value of the {@code Sec-WebSocket-Protocol} header.
155155
* @return the value of the header
156156
*/
157157
public List<String> getSecWebSocketProtocol() {

spring-websocket/src/main/java/org/springframework/web/socket/server/support/AbstractHandshakeHandler.java

+18-14
Original file line numberDiff line numberDiff line change
@@ -215,21 +215,23 @@ public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse r
215215
}
216216
try {
217217
HttpMethod httpMethod = request.getMethod();
218-
if (HttpMethod.GET != httpMethod && CONNECT_METHOD != httpMethod) {
218+
if (HttpMethod.GET != httpMethod && !CONNECT_METHOD.equals(httpMethod)) {
219219
response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED);
220220
response.getHeaders().setAllow(Set.of(HttpMethod.GET, CONNECT_METHOD));
221221
if (logger.isErrorEnabled()) {
222222
logger.error("Handshake failed due to unexpected HTTP method: " + httpMethod);
223223
}
224224
return false;
225225
}
226-
if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
227-
handleInvalidUpgradeHeader(request, response);
228-
return false;
229-
}
230-
if (!headers.getConnection().contains("Upgrade") && !headers.getConnection().contains("upgrade")) {
231-
handleInvalidConnectHeader(request, response);
232-
return false;
226+
if (HttpMethod.GET == httpMethod) {
227+
if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
228+
handleInvalidUpgradeHeader(request, response);
229+
return false;
230+
}
231+
if (!headers.getConnection().contains("Upgrade") && !headers.getConnection().contains("upgrade")) {
232+
handleInvalidConnectHeader(request, response);
233+
return false;
234+
}
233235
}
234236
if (!isWebSocketVersionSupported(headers)) {
235237
handleWebSocketVersionNotSupported(request, response);
@@ -239,13 +241,15 @@ public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse r
239241
response.setStatusCode(HttpStatus.FORBIDDEN);
240242
return false;
241243
}
242-
String wsKey = headers.getSecWebSocketKey();
243-
if (wsKey == null) {
244-
if (logger.isErrorEnabled()) {
245-
logger.error("Missing \"Sec-WebSocket-Key\" header");
244+
if (HttpMethod.GET == httpMethod) {
245+
String wsKey = headers.getSecWebSocketKey();
246+
if (wsKey == null) {
247+
if (logger.isErrorEnabled()) {
248+
logger.error("Missing \"Sec-WebSocket-Key\" header");
249+
}
250+
response.setStatusCode(HttpStatus.BAD_REQUEST);
251+
return false;
246252
}
247-
response.setStatusCode(HttpStatus.BAD_REQUEST);
248-
return false;
249253
}
250254
}
251255
catch (IOException ex) {

0 commit comments

Comments
 (0)