From e4b505b3669b05a5a19e4e6e3ffc5b75764cf29b Mon Sep 17 00:00:00 2001 From: tguerin Date: Wed, 11 Sep 2024 20:02:14 +0000 Subject: [PATCH] Propagate http status code when http upgrade request failed R=mosum@google.com Change-Id: I547d19da251bdea0c259d3896e6e333f9afd0bca Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/384522 Auto-Submit: Thomas Guerin Reviewed-by: Brian Quinlan Commit-Queue: Alexander Aprelev Reviewed-by: Alexander Aprelev --- sdk/lib/_http/websocket.dart | 10 ++++++++-- sdk/lib/_http/websocket_impl.dart | 7 ++++--- tests/standalone/io/web_socket_test.dart | 1 + 3 files changed, 13 insertions(+), 5 deletions(-) diff --git a/sdk/lib/_http/websocket.dart b/sdk/lib/_http/websocket.dart index 4ef5b08e13a7..28b8bcd36a06 100644 --- a/sdk/lib/_http/websocket.dart +++ b/sdk/lib/_http/websocket.dart @@ -407,8 +407,14 @@ abstract class WebSocket class WebSocketException implements IOException { final String message; + final int? httpStatusCode; - const WebSocketException([this.message = ""]); + const WebSocketException([this.message = "", this.httpStatusCode]); - String toString() => "WebSocketException: $message"; + String toString() { + if (httpStatusCode != null) { + return "WebSocketException: $message, HTTP status code: $httpStatusCode"; + } + return "WebSocketException: $message"; + } } diff --git a/sdk/lib/_http/websocket_impl.dart b/sdk/lib/_http/websocket_impl.dart index 28305561aa88..4e7bf59cf6e3 100644 --- a/sdk/lib/_http/websocket_impl.dart +++ b/sdk/lib/_http/websocket_impl.dart @@ -1046,13 +1046,13 @@ class _WebSocketImpl extends Stream with _ServiceObject implements WebSocket { return request.close(); }).then((response) { - Future error(String message) { + Future error(String message, [int? httpStatusCode]) { // Flush data. response.detachSocket().then((socket) { socket.destroy(); }); return Future.error( - WebSocketException(message), callerStackTrace); + WebSocketException(message, httpStatusCode), callerStackTrace); } var connectionHeader = response.headers[HttpHeaders.connectionHeader]; @@ -1061,7 +1061,8 @@ class _WebSocketImpl extends Stream with _ServiceObject implements WebSocket { !connectionHeader.any((value) => value.toLowerCase() == "upgrade") || response.headers.value(HttpHeaders.upgradeHeader)!.toLowerCase() != "websocket") { - return error("Connection to '$uri' was not upgraded to websocket"); + return error("Connection to '$uri' was not upgraded to websocket", + response.statusCode); } String? accept = response.headers.value("Sec-WebSocket-Accept"); if (accept == null) { diff --git a/tests/standalone/io/web_socket_test.dart b/tests/standalone/io/web_socket_test.dart index 99a428784449..0aa3c5911c4f 100644 --- a/tests/standalone/io/web_socket_test.dart +++ b/tests/standalone/io/web_socket_test.dart @@ -300,6 +300,7 @@ class SecurityConfiguration { }); Future.value(createClient(server.port)).catchError((error) { + Expect.equals(HttpStatus.notFound, error.httpStatusCode); server.close(); }); });