| 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 import "dart:async"; | 10 import "dart:async"; |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 50 Future<HttpClientResponse> createWebsocket(String url, String headerValue) { | 50 Future<HttpClientResponse> createWebsocket(String url, String headerValue) { |
| 51 HttpClient _httpClient = new HttpClient(); | 51 HttpClient _httpClient = new HttpClient(); |
| 52 Uri uri = Uri.parse(url); | 52 Uri uri = Uri.parse(url); |
| 53 | 53 |
| 54 Random random = new Random(); | 54 Random random = new Random(); |
| 55 // Generate 16 random bytes. | 55 // Generate 16 random bytes. |
| 56 Uint8List nonceData = new Uint8List(16); | 56 Uint8List nonceData = new Uint8List(16); |
| 57 for (int i = 0; i < 16; i++) { | 57 for (int i = 0; i < 16; i++) { |
| 58 nonceData[i] = random.nextInt(256); | 58 nonceData[i] = random.nextInt(256); |
| 59 } | 59 } |
| 60 String nonce = CryptoUtils.bytesToBase64(nonceData); | 60 String nonce = BASE64.encode(nonceData); |
| 61 | 61 |
| 62 uri = new Uri( | 62 uri = new Uri( |
| 63 scheme: uri.scheme == "wss" ? "https" : "http", | 63 scheme: uri.scheme == "wss" ? "https" : "http", |
| 64 userInfo: uri.userInfo, | 64 userInfo: uri.userInfo, |
| 65 host: uri.host, | 65 host: uri.host, |
| 66 port: uri.port, | 66 port: uri.port, |
| 67 path: uri.path, | 67 path: uri.path, |
| 68 query: uri.query, | 68 query: uri.query, |
| 69 fragment: uri.fragment); | 69 fragment: uri.fragment); |
| 70 return _httpClient.openUrl("GET", uri).then((request) { | 70 return _httpClient.openUrl("GET", uri).then((request) { |
| 71 if (uri.userInfo != null && !uri.userInfo.isEmpty) { | 71 if (uri.userInfo != null && !uri.userInfo.isEmpty) { |
| 72 // If the URL contains user information use that for basic | 72 // If the URL contains user information use that for basic |
| 73 // authorization. | 73 // authorization. |
| 74 String auth = CryptoUtils.bytesToBase64(UTF8.encode(uri.userInfo)); | 74 String auth = BASE64.encode(UTF8.encode(uri.userInfo)); |
| 75 request.headers.set(HttpHeaders.AUTHORIZATION, "Basic $auth"); | 75 request.headers.set(HttpHeaders.AUTHORIZATION, "Basic $auth"); |
| 76 } | 76 } |
| 77 // Setup the initial handshake. | 77 // Setup the initial handshake. |
| 78 request.headers | 78 request.headers |
| 79 ..set(HttpHeaders.CONNECTION, "Upgrade") | 79 ..set(HttpHeaders.CONNECTION, "Upgrade") |
| 80 ..set(HttpHeaders.UPGRADE, "websocket") | 80 ..set(HttpHeaders.UPGRADE, "websocket") |
| 81 ..set("Sec-WebSocket-Key", nonce) | 81 ..set("Sec-WebSocket-Key", nonce) |
| 82 ..set("Cache-Control", "no-cache") | 82 ..set("Cache-Control", "no-cache") |
| 83 ..set("Sec-WebSocket-Version", "13") | 83 ..set("Sec-WebSocket-Version", "13") |
| 84 ..set("Sec-WebSocket-Extensions", headerValue); | 84 ..set("Sec-WebSocket-Extensions", headerValue); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 168 } | 168 } |
| 169 | 169 |
| 170 void testCompressionHeaders() { | 170 void testCompressionHeaders() { |
| 171 asyncStart(); | 171 asyncStart(); |
| 172 createServer().then((server) { | 172 createServer().then((server) { |
| 173 server.listen((request) { | 173 server.listen((request) { |
| 174 Expect.equals('Upgrade', request.headers.value(HttpHeaders.CONNECTION)); | 174 Expect.equals('Upgrade', request.headers.value(HttpHeaders.CONNECTION)); |
| 175 Expect.equals('websocket', request.headers.value(HttpHeaders.UPGRADE)); | 175 Expect.equals('websocket', request.headers.value(HttpHeaders.UPGRADE)); |
| 176 | 176 |
| 177 var key = request.headers.value('Sec-WebSocket-Key'); | 177 var key = request.headers.value('Sec-WebSocket-Key'); |
| 178 var sha1 = new SHA1()..add("$key$WEB_SOCKET_GUID".codeUnits); | 178 var digest = sha1.convert("$key$WEB_SOCKET_GUID".codeUnits); |
| 179 var accept = CryptoUtils.bytesToBase64(sha1.close()); | 179 var accept = BASE64.encode(digest.bytes); |
| 180 request.response | 180 request.response |
| 181 ..statusCode = HttpStatus.SWITCHING_PROTOCOLS | 181 ..statusCode = HttpStatus.SWITCHING_PROTOCOLS |
| 182 ..headers.add(HttpHeaders.CONNECTION, "Upgrade") | 182 ..headers.add(HttpHeaders.CONNECTION, "Upgrade") |
| 183 ..headers.add(HttpHeaders.UPGRADE, "websocket") | 183 ..headers.add(HttpHeaders.UPGRADE, "websocket") |
| 184 ..headers.add("Sec-WebSocket-Accept", accept) | 184 ..headers.add("Sec-WebSocket-Accept", accept) |
| 185 ..headers.add("Sec-WebSocket-Extensions", | 185 ..headers.add("Sec-WebSocket-Extensions", |
| 186 "permessage-deflate;" | 186 "permessage-deflate;" |
| 187 // Test quoted values and space padded = | 187 // Test quoted values and space padded = |
| 188 'server_max_window_bits="10"; client_max_window_bits = 12' | 188 'server_max_window_bits="10"; client_max_window_bits = 12' |
| 189 'client_no_context_takeover; server_no_context_takeover'); | 189 'client_no_context_takeover; server_no_context_takeover'); |
| (...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 376 serverMaxWindowBits: 8); | 376 serverMaxWindowBits: 8); |
| 377 testClientRequestHeaders(compression); | 377 testClientRequestHeaders(compression); |
| 378 } | 378 } |
| 379 } | 379 } |
| 380 | 380 |
| 381 main() { | 381 main() { |
| 382 new SecurityConfiguration(secure: false).runTests(); | 382 new SecurityConfiguration(secure: false).runTests(); |
| 383 // TODO(whesse): Make WebSocket.connect() take an optional context: parameter. | 383 // TODO(whesse): Make WebSocket.connect() take an optional context: parameter. |
| 384 // new SecurityConfiguration(secure: true).runTests(); | 384 // new SecurityConfiguration(secure: true).runTests(); |
| 385 } | 385 } |
| OLD | NEW |