| 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 import 'dart:convert'; | 5 import 'dart:convert'; |
| 6 | 6 |
| 7 import 'package:http_parser/http_parser.dart'; | |
| 8 import 'package:shelf/shelf.dart'; | 7 import 'package:shelf/shelf.dart'; |
| 8 import 'package:web_socket_channel/web_socket_channel.dart'; |
| 9 | 9 |
| 10 /// A class that exposes a handler for upgrading WebSocket requests. | 10 /// A class that exposes a handler for upgrading WebSocket requests. |
| 11 class WebSocketHandler { | 11 class WebSocketHandler { |
| 12 /// The function to call when a request is upgraded. | 12 /// The function to call when a request is upgraded. |
| 13 final Function _onConnection; | 13 final Function _onConnection; |
| 14 | 14 |
| 15 /// The set of protocols the user supports, or `null`. | 15 /// The set of protocols the user supports, or `null`. |
| 16 final Set<String> _protocols; | 16 final Set<String> _protocols; |
| 17 | 17 |
| 18 /// The set of allowed browser origin connections, or `null`.. | 18 /// The set of allowed browser origin connections, or `null`.. |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 return _forbidden('invalid origin "$origin".'); | 63 return _forbidden('invalid origin "$origin".'); |
| 64 } | 64 } |
| 65 | 65 |
| 66 var protocol = _chooseProtocol(request); | 66 var protocol = _chooseProtocol(request); |
| 67 request.hijack((channel) { | 67 request.hijack((channel) { |
| 68 var sink = UTF8.encoder.startChunkedConversion(channel.sink); | 68 var sink = UTF8.encoder.startChunkedConversion(channel.sink); |
| 69 sink.add( | 69 sink.add( |
| 70 "HTTP/1.1 101 Switching Protocols\r\n" | 70 "HTTP/1.1 101 Switching Protocols\r\n" |
| 71 "Upgrade: websocket\r\n" | 71 "Upgrade: websocket\r\n" |
| 72 "Connection: Upgrade\r\n" | 72 "Connection: Upgrade\r\n" |
| 73 "Sec-WebSocket-Accept: ${CompatibleWebSocket.signKey(key)}\r\n"); | 73 "Sec-WebSocket-Accept: ${WebSocketChannel.signKey(key)}\r\n"); |
| 74 if (protocol != null) sink.add("Sec-WebSocket-Protocol: $protocol\r\n"); | 74 if (protocol != null) sink.add("Sec-WebSocket-Protocol: $protocol\r\n"); |
| 75 sink.add("\r\n"); | 75 sink.add("\r\n"); |
| 76 | 76 |
| 77 _onConnection(new WebSocketChannel(channel), protocol); | 77 _onConnection(new WebSocketChannel(channel), protocol); |
| 78 }); | 78 }); |
| 79 | 79 |
| 80 // [request.hijack] is guaranteed to throw a [HijackException], so we'll | 80 // [request.hijack] is guaranteed to throw a [HijackException], so we'll |
| 81 // never get here. | 81 // never get here. |
| 82 assert(false); | 82 assert(false); |
| 83 return null; | 83 return null; |
| (...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 124 <html> | 124 <html> |
| 125 <head><title>$title</title></head> | 125 <head><title>$title</title></head> |
| 126 <body> | 126 <body> |
| 127 <h1>$title</h1> | 127 <h1>$title</h1> |
| 128 <p>$message</p> | 128 <p>$message</p> |
| 129 </body> | 129 </body> |
| 130 </html> | 130 </html> |
| 131 """, headers: {'content-type': 'text/html'}); | 131 """, headers: {'content-type': 'text/html'}); |
| 132 } | 132 } |
| 133 } | 133 } |
| OLD | NEW |