| | | 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 | | using CoreWCF.Security; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Channels |
| | | 9 | | { |
| | | 10 | | internal abstract class StreamSecurityUpgradeAcceptorBase : StreamSecurityUpgradeAcceptor |
| | | 11 | | { |
| | | 12 | | private SecurityMessageProperty _remoteSecurity; |
| | | 13 | | private bool _securityUpgraded; |
| | | 14 | | private readonly string _upgradeString; |
| | | 15 | | |
| | 7 | 16 | | protected StreamSecurityUpgradeAcceptorBase(string upgradeString) |
| | | 17 | | { |
| | 7 | 18 | | _upgradeString = upgradeString; |
| | 7 | 19 | | } |
| | | 20 | | |
| | | 21 | | public override async Task<Stream> AcceptUpgradeAsync(Stream stream) |
| | | 22 | | { |
| | 7 | 23 | | if (stream == null) |
| | | 24 | | { |
| | 0 | 25 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(stream)); |
| | | 26 | | } |
| | | 27 | | |
| | 7 | 28 | | (stream, _remoteSecurity) = await OnAcceptUpgradeAsync(stream); |
| | 7 | 29 | | _securityUpgraded = true; |
| | 7 | 30 | | return stream; |
| | 7 | 31 | | } |
| | | 32 | | |
| | | 33 | | public override bool CanUpgrade(string contentType) |
| | | 34 | | { |
| | 7 | 35 | | if (_securityUpgraded) |
| | | 36 | | { |
| | 0 | 37 | | return false; |
| | | 38 | | } |
| | | 39 | | |
| | 7 | 40 | | return (contentType == _upgradeString); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | public override SecurityMessageProperty GetRemoteSecurity() |
| | | 44 | | { |
| | | 45 | | // this could be null if upgrade not completed. |
| | 5 | 46 | | return _remoteSecurity; |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | protected abstract Task<(Stream, SecurityMessageProperty)> OnAcceptUpgradeAsync(Stream stream); |
| | | 50 | | } |
| | | 51 | | } |