OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 // VMOptions= | 5 // VMOptions= |
6 // VMOptions=--short_socket_read | 6 // VMOptions=--short_socket_read |
7 // VMOptions=--short_socket_write | 7 // VMOptions=--short_socket_write |
8 // VMOptions=--short_socket_read --short_socket_write | 8 // VMOptions=--short_socket_read --short_socket_write |
9 | 9 |
10 library dart.io; | 10 library dart.io; |
11 | 11 |
12 import "package:expect/expect.dart"; | 12 import "package:expect/expect.dart"; |
13 import "dart:async"; | 13 import "dart:async"; |
14 import "dart:convert" show BASE64; | |
15 import "dart:io"; | 14 import "dart:io"; |
16 import "dart:math"; | 15 import "dart:math"; |
17 | 16 |
18 import "hash_utils.dart"; | 17 part "../../../sdk/lib/io/crypto.dart"; |
19 | 18 |
20 const String webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; | 19 const String webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; |
21 | 20 |
22 void testPing(int totalConnections) { | 21 void testPing(int totalConnections) { |
23 HttpServer.bind('localhost', 0).then((server) { | 22 HttpServer.bind('localhost', 0).then((server) { |
24 int closed = 0; | 23 int closed = 0; |
25 server.listen((request) { | 24 server.listen((request) { |
26 var response = request.response; | 25 var response = request.response; |
27 response.statusCode = HttpStatus.SWITCHING_PROTOCOLS; | 26 response.statusCode = HttpStatus.SWITCHING_PROTOCOLS; |
28 response.headers.set(HttpHeaders.CONNECTION, "upgrade"); | 27 response.headers.set(HttpHeaders.CONNECTION, "upgrade"); |
29 response.headers.set(HttpHeaders.UPGRADE, "websocket"); | 28 response.headers.set(HttpHeaders.UPGRADE, "websocket"); |
30 String key = request.headers.value("Sec-WebSocket-Key"); | 29 String key = request.headers.value("Sec-WebSocket-Key"); |
31 var sha1 = new SHA1(); | 30 _SHA1 sha1 = new _SHA1(); |
32 sha1.add("$key$webSocketGUID".codeUnits); | 31 sha1.add("$key$webSocketGUID".codeUnits); |
33 String accept = BASE64.encode(sha1.close()); | 32 String accept = _CryptoUtils.bytesToBase64(sha1.close()); |
34 response.headers.add("Sec-WebSocket-Accept", accept); | 33 response.headers.add("Sec-WebSocket-Accept", accept); |
35 response.headers.contentLength = 0; | 34 response.headers.contentLength = 0; |
36 response.detachSocket().then((socket) { | 35 response.detachSocket().then((socket) { |
37 socket.drain().then((_) { | 36 socket.drain().then((_) { |
38 socket.close(); | 37 socket.close(); |
39 closed++; | 38 closed++; |
40 if (closed == totalConnections) { | 39 if (closed == totalConnections) { |
41 server.close(); | 40 server.close(); |
42 } | 41 } |
43 }); | 42 }); |
44 }); | 43 }); |
45 }); | 44 }); |
46 | 45 |
47 for (int i = 0; i < totalConnections; i++) { | 46 for (int i = 0; i < totalConnections; i++) { |
48 WebSocket.connect('ws://localhost:${server.port}').then((webSocket) { | 47 WebSocket.connect('ws://localhost:${server.port}').then((webSocket) { |
49 webSocket.pingInterval = const Duration(milliseconds: 100); | 48 webSocket.pingInterval = const Duration(milliseconds: 100); |
50 webSocket.drain(); | 49 webSocket.drain(); |
51 }); | 50 }); |
52 } | 51 } |
53 }); | 52 }); |
54 } | 53 } |
55 | 54 |
56 void main() { | 55 void main() { |
57 testPing(10); | 56 testPing(10); |
58 } | 57 } |
OLD | NEW |