Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: sdk/lib/io/websocket_impl.dart

Issue 15298004: Use dart:crypto's Base64 methods in dart:io. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « sdk/lib/io/iolib_sources.gypi ('k') | tests/standalone/crypto/base64_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 part of dart.io; 5 part of dart.io;
6 6
7 const String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"; 7 const String _webSocketGUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
8 8
9 class _WebSocketMessageType { 9 class _WebSocketMessageType {
10 static const int NONE = 0; 10 static const int NONE = 0;
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after
401 new WebSocketException("Invalid WebSocket upgrade request")); 401 new WebSocketException("Invalid WebSocket upgrade request"));
402 } 402 }
403 403
404 // Send the upgrade response. 404 // Send the upgrade response.
405 response.statusCode = HttpStatus.SWITCHING_PROTOCOLS; 405 response.statusCode = HttpStatus.SWITCHING_PROTOCOLS;
406 response.headers.add(HttpHeaders.CONNECTION, "Upgrade"); 406 response.headers.add(HttpHeaders.CONNECTION, "Upgrade");
407 response.headers.add(HttpHeaders.UPGRADE, "websocket"); 407 response.headers.add(HttpHeaders.UPGRADE, "websocket");
408 String key = request.headers.value("Sec-WebSocket-Key"); 408 String key = request.headers.value("Sec-WebSocket-Key");
409 SHA1 sha1 = new SHA1(); 409 SHA1 sha1 = new SHA1();
410 sha1.add("$key$_webSocketGUID".codeUnits); 410 sha1.add("$key$_webSocketGUID".codeUnits);
411 String accept = _Base64._encode(sha1.close()); 411 String accept = CryptoUtils.bytesToBase64(sha1.close());
412 response.headers.add("Sec-WebSocket-Accept", accept); 412 response.headers.add("Sec-WebSocket-Accept", accept);
413 response.headers.contentLength = 0; 413 response.headers.contentLength = 0;
414 return response.detachSocket() 414 return response.detachSocket()
415 .then((socket) => new _WebSocketImpl._fromSocket(socket, true)); 415 .then((socket) => new _WebSocketImpl._fromSocket(socket, true));
416 } 416 }
417 417
418 static bool _isUpgradeRequest(HttpRequest request) { 418 static bool _isUpgradeRequest(HttpRequest request) {
419 if (request.method != "GET") { 419 if (request.method != "GET") {
420 return false; 420 return false;
421 } 421 }
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after
652 if (uri.userInfo != "") { 652 if (uri.userInfo != "") {
653 throw new WebSocketException("Unsupported user info '${uri.userInfo}'"); 653 throw new WebSocketException("Unsupported user info '${uri.userInfo}'");
654 } 654 }
655 655
656 Random random = new Random(); 656 Random random = new Random();
657 // Generate 16 random bytes. 657 // Generate 16 random bytes.
658 List<int> nonceData = new List<int>(16); 658 List<int> nonceData = new List<int>(16);
659 for (int i = 0; i < 16; i++) { 659 for (int i = 0; i < 16; i++) {
660 nonceData[i] = random.nextInt(256); 660 nonceData[i] = random.nextInt(256);
661 } 661 }
662 String nonce = _Base64._encode(nonceData); 662 String nonce = CryptoUtils.bytesToBase64(nonceData);
663 663
664 uri = new Uri.fromComponents(scheme: uri.scheme == "wss" ? "https" : "http", 664 uri = new Uri.fromComponents(scheme: uri.scheme == "wss" ? "https" : "http",
665 userInfo: uri.userInfo, 665 userInfo: uri.userInfo,
666 domain: uri.domain, 666 domain: uri.domain,
667 port: uri.port, 667 port: uri.port,
668 path: uri.path, 668 path: uri.path,
669 query: uri.query, 669 query: uri.query,
670 fragment: uri.fragment); 670 fragment: uri.fragment);
671 return _httpClient.openUrl("GET", uri) 671 return _httpClient.openUrl("GET", uri)
672 .then((request) { 672 .then((request) {
(...skipping 20 matching lines...) Expand all
693 "websocket") { 693 "websocket") {
694 error("Connection to '$uri' was not upgraded to websocket"); 694 error("Connection to '$uri' was not upgraded to websocket");
695 } 695 }
696 String accept = response.headers.value("Sec-WebSocket-Accept"); 696 String accept = response.headers.value("Sec-WebSocket-Accept");
697 if (accept == null) { 697 if (accept == null) {
698 error("Response did not contain a 'Sec-WebSocket-Accept' header"); 698 error("Response did not contain a 'Sec-WebSocket-Accept' header");
699 } 699 }
700 SHA1 sha1 = new SHA1(); 700 SHA1 sha1 = new SHA1();
701 sha1.add("$nonce$_webSocketGUID".codeUnits); 701 sha1.add("$nonce$_webSocketGUID".codeUnits);
702 List<int> expectedAccept = sha1.close(); 702 List<int> expectedAccept = sha1.close();
703 List<int> receivedAccept = _Base64._decode(accept); 703 List<int> receivedAccept = CryptoUtils.base64StringToBytes(accept);
704 if (expectedAccept.length != receivedAccept.length) { 704 if (expectedAccept.length != receivedAccept.length) {
705 error("Reasponse header 'Sec-WebSocket-Accept' is the wrong length"); 705 error("Reasponse header 'Sec-WebSocket-Accept' is the wrong length");
706 } 706 }
707 for (int i = 0; i < expectedAccept.length; i++) { 707 for (int i = 0; i < expectedAccept.length; i++) {
708 if (expectedAccept[i] != receivedAccept[i]) { 708 if (expectedAccept[i] != receivedAccept[i]) {
709 error("Bad response 'Sec-WebSocket-Accept' header"); 709 error("Bad response 'Sec-WebSocket-Accept' header");
710 } 710 }
711 } 711 }
712 return response.detachSocket() 712 return response.detachSocket()
713 .then((socket) => new _WebSocketImpl._fromSocket(socket)); 713 .then((socket) => new _WebSocketImpl._fromSocket(socket));
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after
796 (code < WebSocketStatus.NORMAL_CLOSURE || 796 (code < WebSocketStatus.NORMAL_CLOSURE ||
797 code == WebSocketStatus.RESERVED_1004 || 797 code == WebSocketStatus.RESERVED_1004 ||
798 code == WebSocketStatus.NO_STATUS_RECEIVED || 798 code == WebSocketStatus.NO_STATUS_RECEIVED ||
799 code == WebSocketStatus.ABNORMAL_CLOSURE || 799 code == WebSocketStatus.ABNORMAL_CLOSURE ||
800 (code > WebSocketStatus.INTERNAL_SERVER_ERROR && 800 (code > WebSocketStatus.INTERNAL_SERVER_ERROR &&
801 code < WebSocketStatus.RESERVED_1015) || 801 code < WebSocketStatus.RESERVED_1015) ||
802 (code >= WebSocketStatus.RESERVED_1015 && 802 (code >= WebSocketStatus.RESERVED_1015 &&
803 code < 3000)); 803 code < 3000));
804 } 804 }
805 } 805 }
OLDNEW
« no previous file with comments | « sdk/lib/io/iolib_sources.gypi ('k') | tests/standalone/crypto/base64_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698