OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library shelf_web_socket.web_socket_handler; | 5 library shelf_web_socket.web_socket_handler; |
6 | 6 |
7 import 'dart:convert'; | 7 import 'dart:convert'; |
8 | 8 |
9 import 'package:http_parser/http_parser.dart'; | 9 import 'package:http_parser/http_parser.dart'; |
10 import 'package:shelf/shelf.dart'; | 10 import 'package:shelf/shelf.dart'; |
(...skipping 10 matching lines...) Expand all Loading... |
21 final Set<String> _allowedOrigins; | 21 final Set<String> _allowedOrigins; |
22 | 22 |
23 WebSocketHandler(this._onConnection, this._protocols, this._allowedOrigins); | 23 WebSocketHandler(this._onConnection, this._protocols, this._allowedOrigins); |
24 | 24 |
25 /// The [Handler]. | 25 /// The [Handler]. |
26 Response handle(Request request) { | 26 Response handle(Request request) { |
27 if (request.method != 'GET') return _notFound(); | 27 if (request.method != 'GET') return _notFound(); |
28 | 28 |
29 var connection = request.headers['Connection']; | 29 var connection = request.headers['Connection']; |
30 if (connection == null) return _notFound(); | 30 if (connection == null) return _notFound(); |
31 if (connection.toLowerCase() != 'upgrade') return _notFound(); | 31 var tokens = connection.toLowerCase().split(',') |
| 32 .map((token) => token.trim()); |
| 33 if (!tokens.contains('upgrade')) return _notFound(); |
32 | 34 |
33 var upgrade = request.headers['Upgrade']; | 35 var upgrade = request.headers['Upgrade']; |
34 if (upgrade == null) return _notFound(); | 36 if (upgrade == null) return _notFound(); |
35 if (upgrade.toLowerCase() != 'websocket') return _notFound(); | 37 if (upgrade.toLowerCase() != 'websocket') return _notFound(); |
36 | 38 |
37 var version = request.headers['Sec-WebSocket-Version']; | 39 var version = request.headers['Sec-WebSocket-Version']; |
38 if (version == null) { | 40 if (version == null) { |
39 return _badRequest('missing Sec-WebSocket-Version header.'); | 41 return _badRequest('missing Sec-WebSocket-Version header.'); |
40 } else if (version != '13') { | 42 } else if (version != '13') { |
41 return _notFound(); | 43 return _notFound(); |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 <html> | 126 <html> |
125 <head><title>$title</title></head> | 127 <head><title>$title</title></head> |
126 <body> | 128 <body> |
127 <h1>$title</h1> | 129 <h1>$title</h1> |
128 <p>$message</p> | 130 <p>$message</p> |
129 </body> | 131 </body> |
130 </html> | 132 </html> |
131 """, headers: {'content-type': 'text/html'}); | 133 """, headers: {'content-type': 'text/html'}); |
132 } | 134 } |
133 } | 135 } |
OLD | NEW |