| | | 1 | | // Licensed to the .NET Foundation under one or more agreements. |
| | | 2 | | // The .NET Foundation licenses this file to you under the MIT license. |
| | | 3 | | |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | |
| | | 7 | | namespace System.Net.Security |
| | | 8 | | { |
| | | 9 | | // A public contract for a base abstract authenticated stream. |
| | | 10 | | public abstract class AuthenticatedStream : Stream |
| | | 11 | | { |
| | | 12 | | private readonly Stream _innerStream; |
| | | 13 | | private readonly bool _leaveStreamOpen; |
| | | 14 | | |
| | 31 | 15 | | protected AuthenticatedStream(Stream innerStream, bool leaveInnerStreamOpen) |
| | 31 | 16 | | { |
| | 31 | 17 | | if (innerStream == null || innerStream == Stream.Null) |
| | 0 | 18 | | { |
| | 0 | 19 | | throw new ArgumentNullException(nameof(innerStream)); |
| | | 20 | | } |
| | | 21 | | |
| | 31 | 22 | | if (!innerStream.CanRead || !innerStream.CanWrite) |
| | 0 | 23 | | { |
| | 0 | 24 | | throw new ArgumentException(SR.net_io_must_be_rw_stream, nameof(innerStream)); |
| | | 25 | | } |
| | | 26 | | |
| | 31 | 27 | | _innerStream = innerStream; |
| | 31 | 28 | | _leaveStreamOpen = leaveInnerStreamOpen; |
| | 31 | 29 | | } |
| | | 30 | | |
| | | 31 | | public bool LeaveInnerStreamOpen |
| | | 32 | | { |
| | | 33 | | get |
| | 0 | 34 | | { |
| | 0 | 35 | | return _leaveStreamOpen; |
| | 0 | 36 | | } |
| | | 37 | | } |
| | | 38 | | |
| | | 39 | | protected Stream InnerStream |
| | | 40 | | { |
| | | 41 | | get |
| | 76 | 42 | | { |
| | 76 | 43 | | return _innerStream; |
| | 76 | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | protected override void Dispose(bool disposing) |
| | 31 | 48 | | { |
| | | 49 | | try |
| | 31 | 50 | | { |
| | 31 | 51 | | if (disposing) |
| | 31 | 52 | | { |
| | 31 | 53 | | if (_leaveStreamOpen) |
| | 0 | 54 | | { |
| | 0 | 55 | | _innerStream.Flush(); |
| | 0 | 56 | | } |
| | | 57 | | else |
| | 31 | 58 | | { |
| | 31 | 59 | | _innerStream.Dispose(); |
| | 31 | 60 | | } |
| | 31 | 61 | | } |
| | 31 | 62 | | } |
| | | 63 | | finally |
| | 31 | 64 | | { |
| | 31 | 65 | | base.Dispose(disposing); |
| | 31 | 66 | | } |
| | 31 | 67 | | } |
| | | 68 | | |
| | | 69 | | public override ValueTask DisposeAsync() |
| | 0 | 70 | | { |
| | | 71 | | try |
| | 0 | 72 | | { |
| | 0 | 73 | | ValueTask vt = _leaveStreamOpen ? |
| | 0 | 74 | | new ValueTask(_innerStream.FlushAsync()) : |
| | 0 | 75 | | _innerStream.DisposeAsync(); |
| | 0 | 76 | | GC.SuppressFinalize(this); |
| | 0 | 77 | | return vt; |
| | | 78 | | } |
| | 0 | 79 | | catch (Exception exc) |
| | 0 | 80 | | { |
| | 0 | 81 | | return ValueTask.FromException(exc); |
| | | 82 | | } |
| | 0 | 83 | | } |
| | | 84 | | |
| | | 85 | | public abstract bool IsAuthenticated { get; } |
| | | 86 | | public abstract bool IsMutuallyAuthenticated { get; } |
| | | 87 | | public abstract bool IsEncrypted { get; } |
| | | 88 | | public abstract bool IsSigned { get; } |
| | | 89 | | public abstract bool IsServer { get; } |
| | | 90 | | } |
| | | 91 | | } |
| | | 92 | | |