Support RFC 8441 upgrades over HTTP/2 CONNECT

See gh-34362

Signed-off-by: Jared Wiltshire <jazdw@users.noreply.github.com>
This commit is contained in:
Jared Wiltshire 2025-02-03 16:45:47 -07:00 committed by rstoyanchev
parent d59991fcc9
commit 49f9b40fba
3 changed files with 32 additions and 26 deletions

View File

@ -205,11 +205,12 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
HttpMethod method = request.getMethod();
HttpHeaders headers = request.getHeaders();
if (HttpMethod.GET != method && CONNECT_METHOD != method) {
if (HttpMethod.GET != method && !CONNECT_METHOD.equals(method)) {
return Mono.error(new MethodNotAllowedException(
request.getMethod(), Set.of(HttpMethod.GET, CONNECT_METHOD)));
}
if (HttpMethod.GET == method) {
if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
return handleBadRequest(exchange, "Invalid 'Upgrade' header: " + headers);
}
@ -223,6 +224,7 @@ public class HandshakeWebSocketService implements WebSocketService, Lifecycle {
if (key == null) {
return handleBadRequest(exchange, "Missing \"Sec-WebSocket-Key\" header");
}
}
String protocol = selectProtocol(headers, handler);

View File

@ -151,7 +151,7 @@ public class WebSocketHttpHeaders extends HttpHeaders {
}
/**
* Returns the value of the {@code Sec-WebSocket-Key} header.
* Returns the value of the {@code Sec-WebSocket-Protocol} header.
* @return the value of the header
*/
public List<String> getSecWebSocketProtocol() {

View File

@ -215,7 +215,7 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
}
try {
HttpMethod httpMethod = request.getMethod();
if (HttpMethod.GET != httpMethod && CONNECT_METHOD != httpMethod) {
if (HttpMethod.GET != httpMethod && !CONNECT_METHOD.equals(httpMethod)) {
response.setStatusCode(HttpStatus.METHOD_NOT_ALLOWED);
response.getHeaders().setAllow(Set.of(HttpMethod.GET, CONNECT_METHOD));
if (logger.isErrorEnabled()) {
@ -223,6 +223,7 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
}
return false;
}
if (HttpMethod.GET == httpMethod) {
if (!"WebSocket".equalsIgnoreCase(headers.getUpgrade())) {
handleInvalidUpgradeHeader(request, response);
return false;
@ -231,6 +232,7 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
handleInvalidConnectHeader(request, response);
return false;
}
}
if (!isWebSocketVersionSupported(headers)) {
handleWebSocketVersionNotSupported(request, response);
return false;
@ -239,6 +241,7 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
response.setStatusCode(HttpStatus.FORBIDDEN);
return false;
}
if (HttpMethod.GET == httpMethod) {
String wsKey = headers.getSecWebSocketKey();
if (wsKey == null) {
if (logger.isErrorEnabled()) {
@ -248,6 +251,7 @@ public abstract class AbstractHandshakeHandler implements HandshakeHandler, Life
return false;
}
}
}
catch (IOException ex) {
throw new HandshakeFailureException(
"Response update failed during upgrade to WebSocket: " + request.getURI(), ex);