55import 'dart:convert' ;
66
77import 'package:shelf/shelf.dart' ;
8+ import 'package:stream_channel/stream_channel.dart' ;
89import 'package:web_socket_channel/web_socket_channel.dart' ;
910
1011/// A class that exposes a handler for upgrading WebSocket requests.
@@ -26,8 +27,8 @@ class WebSocketHandler {
2627
2728 var connection = request.headers['Connection' ];
2829 if (connection == null ) return _notFound ();
29- var tokens =
30- connection. toLowerCase (). split ( ',' ) .map ((token) => token.trim ());
30+ var tokens = connection. toLowerCase (). split ( ',' )
31+ .map ((token) => token.trim ());
3132 if (! tokens.contains ('upgrade' )) return _notFound ();
3233
3334 var upgrade = request.headers['Upgrade' ];
@@ -58,18 +59,18 @@ class WebSocketHandler {
5859 // unexpected origins, we ensure that malicious JavaScript is unable to fake
5960 // a WebSocket handshake.
6061 var origin = request.headers['Origin' ];
61- if (origin != null &&
62- _allowedOrigins != null &&
62+ if (origin != null && _allowedOrigins != null &&
6363 ! _allowedOrigins.contains (origin.toLowerCase ())) {
6464 return _forbidden ('invalid origin "$origin ".' );
6565 }
6666
6767 var protocol = _chooseProtocol (request);
6868 request.hijack ((untypedChannel) {
69- var channel = untypedChannel.cast <List <int >>();
69+ var channel = ( untypedChannel as StreamChannel ) .cast <List <int >>();
7070
7171 var sink = UTF8 .encoder.startChunkedConversion (channel.sink);
72- sink.add ("HTTP/1.1 101 Switching Protocols\r\n "
72+ sink.add (
73+ "HTTP/1.1 101 Switching Protocols\r\n "
7374 "Upgrade: websocket\r\n "
7475 "Connection: Upgrade\r\n "
7576 "Sec-WebSocket-Accept: ${WebSocketChannel .signKey (key )}\r\n " );
@@ -99,20 +100,20 @@ class WebSocketHandler {
99100 }
100101
101102 /// Returns a 404 Not Found response.
102- Response _notFound () => _htmlResponse (
103- 404 , "404 Not Found" , "Only WebSocket connections are supported." );
103+ Response _notFound () => _htmlResponse (404 , "404 Not Found" ,
104+ "Only WebSocket connections are supported." );
104105
105106 /// Returns a 400 Bad Request response.
106107 ///
107108 /// [message] will be HTML-escaped before being included in the response body.
108- Response _badRequest (String message) => _htmlResponse (
109- 400 , "400 Bad Request" , "Invalid WebSocket upgrade request: $message " );
109+ Response _badRequest (String message) => _htmlResponse (400 , "400 Bad Request" ,
110+ "Invalid WebSocket upgrade request: $message " );
110111
111112 /// Returns a 403 Forbidden response.
112113 ///
113114 /// [message] will be HTML-escaped before being included in the response body.
114- Response _forbidden (String message) => _htmlResponse (
115- 403 , "403 Forbidden" , "WebSocket upgrade refused: $message " );
115+ Response _forbidden (String message) => _htmlResponse (403 , "403 Forbidden" ,
116+ "WebSocket upgrade refused: $message " );
116117
117118 /// Creates an HTTP response with the given [statusCode] and an HTML body with
118119 /// [title] and [message] .
0 commit comments