Skip to content

Commit f477c16

Browse files
committed
Allow WebSocket over HTTP CONNECT
Closes gh-34044
1 parent 1cea1fe commit f477c16

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

Diff for: spring-webflux/src/main/java/org/springframework/web/reactive/socket/server/support/HandshakeWebSocketService.java

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -22,6 +22,7 @@
2222
import java.util.Collections;
2323
import java.util.List;
2424
import java.util.Map;
25+
import java.util.Set;
2526
import java.util.function.Predicate;
2627
import java.util.stream.Collectors;
2728

@@ -66,6 +67,9 @@
6667
*/
6768
public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
6869

70+
// For WebSocket upgrades in HTTP/2 (see RFC 8441)
71+
private static final HttpMethod CONNECT_METHOD = HttpMethod.valueOf("CONNECT");
72+
6973
private static final String SEC_WEBSOCKET_KEY = "Sec-WebSocket-Key";
7074

7175
private static final String SEC_WEBSOCKET_PROTOCOL = "Sec-WebSocket-Protocol";
@@ -201,9 +205,9 @@ public Mono<Void> handleRequest(ServerWebExchange exchange, WebSocketHandler han
201205
HttpMethod method = request.getMethod();
202206
HttpHeaders headers = request.getHeaders();
203207

204-
if (HttpMethod.GET != method) {
208+
if (HttpMethod.GET != method && CONNECT_METHOD != method) {
205209
return Mono.error(new MethodNotAllowedException(
206-
request.getMethod(), Collections.singleton(HttpMethod.GET)));
210+
request.getMethod(), Set.of(HttpMethod.GET, CONNECT_METHOD)));
207211
}
208212

209213
if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {

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

+9-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,7 @@
2525
import java.util.List;
2626
import java.util.Locale;
2727
import java.util.Map;
28+
import java.util.Set;
2829

2930
import org.apache.commons.logging.Log;
3031
import org.apache.commons.logging.LogFactory;
@@ -77,6 +78,9 @@
7778
*/
7879
public abstract class AbstractHandshakeHandler implements HandshakeHandler, Lifecycle {
7980

81+
// For WebSocket upgrades in HTTP/2 (see RFC 8441)
82+
private static final HttpMethod CONNECT_METHOD = HttpMethod.valueOf("CONNECT");
83+
8084
private static final boolean tomcatWsPresent;
8185

8286
private static final boolean jettyWsPresent;
@@ -210,11 +214,12 @@ public final boolean doHandshake(ServerHttpRequest request, ServerHttpResponse r
210214
logger.trace("Processing request " + request.getURI() + " with headers=" + headers);
211215
}
212216
try {
213-
if (HttpMethod.GET != request.getMethod()) {
217+
HttpMethod httpMethod = request.getMethod();
218+
if (HttpMethod.GET != httpMethod && CONNECT_METHOD != httpMethod) {
214219
response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED);
215-
response.getHeaders().setAllow(Collections.singleton(HttpMethod.GET));
220+
response.getHeaders().setAllow(Set.of(HttpMethod.GET, CONNECT_METHOD));
216221
if (logger.isErrorEnabled()) {
217-
logger.error("Handshake failed due to unexpected HTTP method: " + request.getMethod());
222+
logger.error("Handshake failed due to unexpected HTTP method: " + httpMethod);
218223
}
219224
return false;
220225
}

0 commit comments

Comments
 (0)