Skip to content

Commit

Permalink
Propagate http status code when http upgrade request failed
Browse files Browse the repository at this point in the history
[email protected]

Change-Id: I547d19da251bdea0c259d3896e6e333f9afd0bca
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/384522
Auto-Submit: Thomas Guerin <[email protected]>
Reviewed-by: Brian Quinlan <[email protected]>
Commit-Queue: Alexander Aprelev <[email protected]>
Reviewed-by: Alexander Aprelev <[email protected]>
  • Loading branch information
tguerin authored and Commit Queue committed Sep 11, 2024
1 parent 009f3d0 commit e4b505b
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
10 changes: 8 additions & 2 deletions sdk/lib/_http/websocket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
}
7 changes: 4 additions & 3 deletions sdk/lib/_http/websocket_impl.dart
Original file line number Diff line number Diff line change
Expand Up @@ -1046,13 +1046,13 @@ class _WebSocketImpl extends Stream with _ServiceObject implements WebSocket {

return request.close();
}).then((response) {
Future<WebSocket> error(String message) {
Future<WebSocket> error(String message, [int? httpStatusCode]) {
// Flush data.
response.detachSocket().then((socket) {
socket.destroy();
});
return Future<WebSocket>.error(
WebSocketException(message), callerStackTrace);
WebSocketException(message, httpStatusCode), callerStackTrace);
}

var connectionHeader = response.headers[HttpHeaders.connectionHeader];
Expand All @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions tests/standalone/io/web_socket_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ class SecurityConfiguration {
});

Future<WebSocket?>.value(createClient(server.port)).catchError((error) {
Expect.equals(HttpStatus.notFound, error.httpStatusCode);
server.close();
});
});
Expand Down

0 comments on commit e4b505b

Please sign in to comment.