Skip to content

Commit 49f9b40

Browse files
jazdwrstoyanchev
authored andcommitted
Support RFC 8441 upgrades over HTTP/2 CONNECT
See gh-34362 Signed-off-by: Jared Wiltshire <[email protected]>
1 parent d59991f commit 49f9b40

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

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

+13-11
Original file line numberDiff line numberDiff line change
@@ -205,23 +205,25 @@ public Mono<Void> handleRequest(ServerWebExchange exchange, WebSocketHandler han
205205
HttpMethod method = request.getMethod();
206206
HttpHeaders headers = request.getHeaders();
207207

208-
if (HttpMethod.GET != method && CONNECT_METHOD != method) {
208+
if (HttpMethod.GET != method && !CONNECT_METHOD.equals(method)) {
209209
return Mono.error(new MethodNotAllowedException(
210210
request.getMethod(), Set.of(HttpMethod.GET, CONNECT_METHOD)));
211211
}
212212

213-
if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
214-
return handleBadRequest(exchange, "Invalid 'Upgrade' header: " + headers);
215-
}
213+
if (HttpMethod.GET == method) {
214+
if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
215+
return handleBadRequest(exchange, "Invalid 'Upgrade' header: " + headers);
216+
}
216217

217-
List<String> connectionValue = headers.getConnection();
218-
if (!connectionValue.contains("Upgrade") && !connectionValue.contains("upgrade")) {
219-
return handleBadRequest(exchange, "Invalid 'Connection' header: " + headers);
220-
}
218+
List<String> connectionValue = headers.getConnection();
219+
if (!connectionValue.contains("Upgrade") && !connectionValue.contains("upgrade")) {
220+
return handleBadRequest(exchange, "Invalid 'Connection' header: " + headers);
221+
}
221222

222-
String key = headers.getFirst(SEC_WEBSOCKET_KEY);
223-
if (key == null) {
224-
return handleBadRequest(exchange, "Missing \"Sec-WebSocket-Key\" header");
223+
String key = headers.getFirst(SEC_WEBSOCKET_KEY);
224+
if (key == null) {
225+
return handleBadRequest(exchange, "Missing \"Sec-WebSocket-Key\" header");
226+
}
225227
}
226228

227229
String protocol = selectProtocol(headers, handler);

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)