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 part of dart.io; | 5 part of dart.io; |
6 | 6 |
7 /** | 7 /** |
8 * A high-level class for communicating securely over a TCP socket, using | 8 * A high-level class for communicating securely over a TCP socket, using |
9 * TLS and SSL. The [SecureSocket] exposes both a [Stream] and an | 9 * TLS and SSL. The [SecureSocket] exposes both a [Stream] and an |
10 * [IOSink] interface, making it ideal for using together with | 10 * [IOSink] interface, making it ideal for using together with |
(...skipping 371 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 } | 382 } |
383 } | 383 } |
384 | 384 |
385 bool get writeEventsEnabled => _writeEventsEnabled; | 385 bool get writeEventsEnabled => _writeEventsEnabled; |
386 | 386 |
387 void set writeEventsEnabled(bool value) { | 387 void set writeEventsEnabled(bool value) { |
388 if (value && | 388 if (value && |
389 _controller.hasSubscribers && | 389 _controller.hasSubscribers && |
390 _secureFilter != null && | 390 _secureFilter != null && |
391 _secureFilter.buffers[WRITE_PLAINTEXT].free > 0) { | 391 _secureFilter.buffers[WRITE_PLAINTEXT].free > 0) { |
392 new Timer(0, (_) => _controller.add(RawSocketEvent.WRITE)); | 392 Timer.run(() => _controller.add(RawSocketEvent.WRITE)); |
393 } else { | 393 } else { |
394 _writeEventsEnabled = value; | 394 _writeEventsEnabled = value; |
395 } | 395 } |
396 } | 396 } |
397 | 397 |
398 bool get readEventsEnabled => _readEventsEnabled; | 398 bool get readEventsEnabled => _readEventsEnabled; |
399 | 399 |
400 void set readEventsEnabled(bool value) { | 400 void set readEventsEnabled(bool value) { |
401 _readEventsEnabled = value; | 401 _readEventsEnabled = value; |
402 if (_socketClosedRead) { | 402 if (_socketClosedRead) { |
403 if (value) { | 403 if (value) { |
404 // We have no underlying socket to set off read events. | 404 // We have no underlying socket to set off read events. |
405 new Timer(0, (_) => _readHandler()); | 405 Timer.run(_readHandler); |
406 } | 406 } |
407 } | 407 } |
408 } | 408 } |
409 | 409 |
410 List<int> read([int len]) { | 410 List<int> read([int len]) { |
411 if (_closedRead) { | 411 if (_closedRead) { |
412 throw new SocketIOException("Reading from a closed socket"); | 412 throw new SocketIOException("Reading from a closed socket"); |
413 } | 413 } |
414 if (_status != CONNECTED) { | 414 if (_status != CONNECTED) { |
415 return null; | 415 return null; |
416 } | 416 } |
417 var buffer = _secureFilter.buffers[READ_PLAINTEXT]; | 417 var buffer = _secureFilter.buffers[READ_PLAINTEXT]; |
418 _readEncryptedData(); | 418 _readEncryptedData(); |
419 int toRead = buffer.length; | 419 int toRead = buffer.length; |
420 if (len != null) { | 420 if (len != null) { |
421 if (len is! int || len < 0) { | 421 if (len is! int || len < 0) { |
422 throw new ArgumentError( | 422 throw new ArgumentError( |
423 "Invalid len parameter in SecureSocket.read (len: $len)"); | 423 "Invalid len parameter in SecureSocket.read (len: $len)"); |
424 } | 424 } |
425 if (len < toRead) { | 425 if (len < toRead) { |
426 toRead = len; | 426 toRead = len; |
427 } | 427 } |
428 } | 428 } |
429 List<int> result = (toRead == 0) ? null : | 429 List<int> result = (toRead == 0) ? null : |
430 buffer.data.getRange(buffer.start, toRead); | 430 buffer.data.getRange(buffer.start, toRead); |
431 buffer.advanceStart(toRead); | 431 buffer.advanceStart(toRead); |
432 | 432 |
433 // Set up a read event if the filter still has data. | 433 // Set up a read event if the filter still has data. |
434 if (!_filterReadEmpty) { | 434 if (!_filterReadEmpty) { |
435 new Timer(0, (_) => _readHandler()); | 435 Timer.run(_readHandler); |
436 } | 436 } |
437 | 437 |
438 if (_socketClosedRead) { // An onClose event is pending. | 438 if (_socketClosedRead) { // An onClose event is pending. |
439 // _closedRead is false, since we are in a read call. | 439 // _closedRead is false, since we are in a read call. |
440 if (!_filterReadEmpty) { | 440 if (!_filterReadEmpty) { |
441 // _filterReadEmpty may be out of date since read empties | 441 // _filterReadEmpty may be out of date since read empties |
442 // the plaintext buffer after calling _readEncryptedData. | 442 // the plaintext buffer after calling _readEncryptedData. |
443 // TODO(whesse): Fix this as part of fixing read. | 443 // TODO(whesse): Fix this as part of fixing read. |
444 _readEncryptedData(); | 444 _readEncryptedData(); |
445 } | 445 } |
446 if (_filterReadEmpty) { | 446 if (_filterReadEmpty) { |
447 // This can't be an else clause: the value of _filterReadEmpty changes. | 447 // This can't be an else clause: the value of _filterReadEmpty changes. |
448 // This must be asynchronous, because we are in a read call. | 448 // This must be asynchronous, because we are in a read call. |
449 new Timer(0, (_) => _closeHandler()); | 449 Timer.run(_closeHandler); |
450 } | 450 } |
451 } | 451 } |
452 | 452 |
453 return result; | 453 return result; |
454 } | 454 } |
455 | 455 |
456 // Write the data to the socket, and flush it as much as possible | 456 // Write the data to the socket, and flush it as much as possible |
457 // until it would block. If the write would block, _writeEncryptedData sets | 457 // until it would block. If the write would block, _writeEncryptedData sets |
458 // up handlers to flush the pipeline when possible. | 458 // up handlers to flush the pipeline when possible. |
459 int write(List<int> data, [int offset, int bytes]) { | 459 int write(List<int> data, [int offset, int bytes]) { |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
525 try { | 525 try { |
526 _readEncryptedData(); | 526 _readEncryptedData(); |
527 } catch (e) { _reportError(e, "RawSecureSocket error"); } | 527 } catch (e) { _reportError(e, "RawSecureSocket error"); } |
528 if (!_filterReadEmpty) { | 528 if (!_filterReadEmpty) { |
529 if (_readEventsEnabled) { | 529 if (_readEventsEnabled) { |
530 if (_secureFilter.buffers[READ_PLAINTEXT].length > 0) { | 530 if (_secureFilter.buffers[READ_PLAINTEXT].length > 0) { |
531 _controller.add(RawSocketEvent.READ); | 531 _controller.add(RawSocketEvent.READ); |
532 } | 532 } |
533 if (_socketClosedRead) { | 533 if (_socketClosedRead) { |
534 // Keep firing read events until we are paused or buffer is empty. | 534 // Keep firing read events until we are paused or buffer is empty. |
535 new Timer(0, (_) => _readHandler()); | 535 Timer.run(_readHandler); |
536 } | 536 } |
537 } | 537 } |
538 } else if (_socketClosedRead) { | 538 } else if (_socketClosedRead) { |
539 _closeHandler(); | 539 _closeHandler(); |
540 } | 540 } |
541 } | 541 } |
542 } | 542 } |
543 | 543 |
544 void _doneHandler() { | 544 void _doneHandler() { |
545 if (_filterReadEmpty) { | 545 if (_filterReadEmpty) { |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
595 _writeEncryptedData(); | 595 _writeEncryptedData(); |
596 } | 596 } |
597 | 597 |
598 void _secureHandshakeCompleteHandler() { | 598 void _secureHandshakeCompleteHandler() { |
599 _status = CONNECTED; | 599 _status = CONNECTED; |
600 if (_connectPending) { | 600 if (_connectPending) { |
601 _connectPending = false; | 601 _connectPending = false; |
602 // If we complete the future synchronously, user code will run here, | 602 // If we complete the future synchronously, user code will run here, |
603 // and modify the state of the RawSecureSocket. For example, it | 603 // and modify the state of the RawSecureSocket. For example, it |
604 // could close the socket, and set _filter to null. | 604 // could close the socket, and set _filter to null. |
605 new Timer(0, (_) => _handshakeComplete.complete(this)); | 605 Timer.run(() => _handshakeComplete.complete(this)); |
606 } | 606 } |
607 } | 607 } |
608 | 608 |
609 void _onPauseStateChange() { | 609 void _onPauseStateChange() { |
610 if (!_socketClosedRead || !_socketClosedWrite) { | 610 if (!_socketClosedRead || !_socketClosedWrite) { |
611 if (_controller.isPaused) { | 611 if (_controller.isPaused) { |
612 _socketSubscription.pause(); | 612 _socketSubscription.pause(); |
613 } else { | 613 } else { |
614 _socketSubscription.resume(); | 614 _socketSubscription.resume(); |
615 } | 615 } |
(...skipping 126 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
742 void destroy(); | 742 void destroy(); |
743 void handshake(); | 743 void handshake(); |
744 void init(); | 744 void init(); |
745 X509Certificate get peerCertificate; | 745 X509Certificate get peerCertificate; |
746 int processBuffer(int bufferIndex); | 746 int processBuffer(int bufferIndex); |
747 void registerBadCertificateCallback(Function callback); | 747 void registerBadCertificateCallback(Function callback); |
748 void registerHandshakeCompleteCallback(Function handshakeCompleteHandler); | 748 void registerHandshakeCompleteCallback(Function handshakeCompleteHandler); |
749 | 749 |
750 List<_ExternalBuffer> get buffers; | 750 List<_ExternalBuffer> get buffers; |
751 } | 751 } |
OLD | NEW |