| | | 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; |
| | | 5 | | using System.Buffers; |
| | | 6 | | using System.IO; |
| | | 7 | | using System.Threading.Tasks; |
| | | 8 | | using CoreWCF.Configuration; |
| | | 9 | | using CoreWCF.Runtime; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Channels.Framing |
| | | 12 | | { |
| | | 13 | | internal class ServerFramingSingletonMiddleware |
| | | 14 | | { |
| | | 15 | | private readonly HandshakeDelegate _next; |
| | | 16 | | |
| | 83 | 17 | | public ServerFramingSingletonMiddleware(HandshakeDelegate next) |
| | | 18 | | { |
| | 83 | 19 | | _next = next; |
| | 83 | 20 | | } |
| | | 21 | | |
| | | 22 | | public async Task OnConnectedAsync(FramingConnection connection) |
| | | 23 | | { |
| | 50 | 24 | | TimeSpan receiveTimeout = connection.ServiceDispatcher.Binding.ReceiveTimeout; |
| | 50 | 25 | | var timeoutHelper = new TimeoutHelper(receiveTimeout); |
| | 50 | 26 | | bool success = false; |
| | | 27 | | try |
| | | 28 | | { |
| | 50 | 29 | | var decoder = connection.FramingDecoder as ServerSingletonDecoder; |
| | | 30 | | Fx.Assert(decoder != null, "FramingDecoder must be non-null and an instance of ServerSessionDecoder"); |
| | | 31 | | |
| | | 32 | | // first validate our content type |
| | | 33 | | //ValidateContentType(connection, decoder); |
| | 50 | 34 | | UpgradeState upgradeState = UpgradeState.None; |
| | | 35 | | // next read any potential upgrades and finish consuming the preamble |
| | 50 | 36 | | ReadOnlySequence<byte> buffer = ReadOnlySequence<byte>.Empty; |
| | | 37 | | while (true) |
| | | 38 | | { |
| | 55 | 39 | | if (buffer.Length == 0 && CanReadAndDecode(upgradeState)) |
| | | 40 | | { |
| | 51 | 41 | | System.IO.Pipelines.ReadResult readResult = await connection.Input.ReadAsync(); |
| | 51 | 42 | | buffer = readResult.Buffer; |
| | 51 | 43 | | if (readResult.IsCompleted) |
| | | 44 | | { |
| | 0 | 45 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFExceptio |
| | | 46 | | } |
| | | 47 | | } |
| | | 48 | | |
| | | 49 | | while (true) |
| | | 50 | | { |
| | 158 | 51 | | if (CanReadAndDecode(upgradeState)) |
| | | 52 | | { |
| | | 53 | | Fx.Assert(buffer.Length > 0, "There must be something in the buffer to decode"); |
| | 154 | 54 | | int bytesDecoded = decoder.Decode(buffer); |
| | 154 | 55 | | if (bytesDecoded > 0) |
| | | 56 | | { |
| | 53 | 57 | | buffer = buffer.Slice(bytesDecoded); |
| | 53 | 58 | | if (buffer.Length == 0) |
| | | 59 | | { |
| | 51 | 60 | | connection.Input.AdvanceTo(buffer.Start); |
| | | 61 | | } |
| | | 62 | | } |
| | | 63 | | } |
| | | 64 | | |
| | 158 | 65 | | switch (decoder.CurrentState) |
| | | 66 | | { |
| | | 67 | | case ServerSingletonDecoder.State.UpgradeRequest: |
| | | 68 | | switch (upgradeState) |
| | | 69 | | { |
| | | 70 | | case UpgradeState.None: |
| | | 71 | | //change the state so that we don't read/decode until it is safe |
| | 1 | 72 | | ChangeUpgradeState(ref upgradeState, UpgradeState.VerifyingUpgradeRequest); |
| | 1 | 73 | | break; |
| | | 74 | | case UpgradeState.VerifyingUpgradeRequest: |
| | 1 | 75 | | if (connection.StreamUpgradeAcceptor == null) |
| | | 76 | | { |
| | 0 | 77 | | connection.Input.AdvanceTo(buffer.Start); // Make sure that input pipe is ab |
| | 0 | 78 | | await connection.SendFaultAsync(FramingEncodingString.UpgradeInvalidFault, T |
| | 0 | 79 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 80 | | new ProtocolException(SR.Format(SR.UpgradeRequestToNonupgradableService, |
| | | 81 | | } |
| | | 82 | | |
| | 1 | 83 | | if (!connection.StreamUpgradeAcceptor.CanUpgrade(decoder.Upgrade)) |
| | | 84 | | { |
| | 0 | 85 | | connection.Input.AdvanceTo(buffer.Start); // Make sure that input pipe is ab |
| | 0 | 86 | | await connection.SendFaultAsync(FramingEncodingString.UpgradeInvalidFault, T |
| | 0 | 87 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolExcept |
| | | 88 | | } |
| | | 89 | | |
| | 1 | 90 | | ChangeUpgradeState(ref upgradeState, UpgradeState.WritingUpgradeAck); |
| | | 91 | | // accept upgrade |
| | 1 | 92 | | await connection.Output.WriteAsync(ServerSingletonEncoder.UpgradeResponseBytes, |
| | 1 | 93 | | await connection.Output.FlushAsync(timeoutHelper.GetCancellationToken()); |
| | 1 | 94 | | ChangeUpgradeState(ref upgradeState, UpgradeState.UpgradeAckSent); |
| | 1 | 95 | | break; |
| | | 96 | | case UpgradeState.UpgradeAckSent: |
| | | 97 | | // This state was used to capture any extra read bytes into PreReadConnection bu |
| | | 98 | | // This extra state transition has been left here to maintain the same state tra |
| | 1 | 99 | | ChangeUpgradeState(ref upgradeState, UpgradeState.BeginUpgrade); |
| | 1 | 100 | | break; |
| | | 101 | | case UpgradeState.BeginUpgrade: |
| | | 102 | | // Set input pipe so that the next read will return all the unconsumed bytes. |
| | | 103 | | // If all bytes have already been consumed so the buffer has 0 length, AdvanceTo |
| | | 104 | | // as it's already been called. |
| | 1 | 105 | | if (buffer.Length > 0) |
| | | 106 | | { |
| | 0 | 107 | | connection.Input.AdvanceTo(buffer.Start); |
| | | 108 | | } |
| | | 109 | | |
| | 1 | 110 | | buffer = ReadOnlySequence<byte>.Empty; |
| | | 111 | | try |
| | | 112 | | { |
| | 1 | 113 | | await UpgradeConnectionAsync(connection, decoder.Upgrade); |
| | 1 | 114 | | ChangeUpgradeState(ref upgradeState, UpgradeState.EndUpgrade); |
| | 1 | 115 | | } |
| | | 116 | | catch (Exception exception) |
| | | 117 | | { |
| | 0 | 118 | | if (Fx.IsFatal(exception)) |
| | | 119 | | { |
| | 0 | 120 | | throw; |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | throw; |
| | | 124 | | } |
| | | 125 | | break; |
| | | 126 | | case UpgradeState.EndUpgrade: |
| | | 127 | | //Must be a different state here than UpgradeComplete so that we don't try to re |
| | 1 | 128 | | ChangeUpgradeState(ref upgradeState, UpgradeState.UpgradeComplete); |
| | 1 | 129 | | break; |
| | | 130 | | case UpgradeState.UpgradeComplete: |
| | | 131 | | //Client is doing more than one upgrade, reset the state |
| | 0 | 132 | | ChangeUpgradeState(ref upgradeState, UpgradeState.VerifyingUpgradeRequest); |
| | 0 | 133 | | break; |
| | | 134 | | } |
| | | 135 | | break; |
| | | 136 | | case ServerSingletonDecoder.State.Start: |
| | 50 | 137 | | SetupSecurityIfNecessary(connection); |
| | 50 | 138 | | if (upgradeState == UpgradeState.UpgradeComplete //We have done at least one upgrade, bu |
| | 50 | 139 | | || upgradeState == UpgradeState.None)//no upgrade, just send the preample end bytes |
| | | 140 | | { |
| | 50 | 141 | | ChangeUpgradeState(ref upgradeState, UpgradeState.WritingPreambleEnd); |
| | | 142 | | // we've finished the preamble. Ack and return. |
| | 50 | 143 | | await connection.Output.WriteAsync(ServerSessionEncoder.AckResponseBytes); |
| | 50 | 144 | | await connection.Output.FlushAsync(); |
| | | 145 | | //terminal state |
| | 50 | 146 | | ChangeUpgradeState(ref upgradeState, UpgradeState.PreambleEndSent); |
| | | 147 | | } |
| | | 148 | | // If all bytes have already been consumed so the buffer has 0 length, AdvanceTo would t |
| | | 149 | | // as it's already been called. |
| | 50 | 150 | | if (buffer.Length > 0) |
| | | 151 | | { |
| | 0 | 152 | | connection.Input.AdvanceTo(buffer.Start); |
| | | 153 | | } |
| | | 154 | | |
| | 50 | 155 | | success = true; |
| | 50 | 156 | | await _next(connection); |
| | 50 | 157 | | return; |
| | | 158 | | } |
| | | 159 | | |
| | 108 | 160 | | if (buffer.Length == 0) |
| | | 161 | | { |
| | 5 | 162 | | break; |
| | | 163 | | } |
| | | 164 | | } |
| | | 165 | | } |
| | | 166 | | } |
| | | 167 | | finally |
| | | 168 | | { |
| | 50 | 169 | | if (!success) |
| | | 170 | | { |
| | 0 | 171 | | connection.Abort(); |
| | | 172 | | } |
| | | 173 | | } |
| | 50 | 174 | | } |
| | | 175 | | |
| | | 176 | | private bool CanReadAndDecode(UpgradeState upgradeState) |
| | | 177 | | { |
| | | 178 | | //ok to read/decode before we start the upgrade |
| | | 179 | | //and between UpgradeComplete/WritingPreambleAck |
| | 213 | 180 | | return upgradeState == UpgradeState.None |
| | 213 | 181 | | || upgradeState == UpgradeState.UpgradeComplete; |
| | | 182 | | } |
| | | 183 | | |
| | | 184 | | private void ChangeUpgradeState(ref UpgradeState currentState, UpgradeState newState) |
| | | 185 | | { |
| | | 186 | | switch (newState) |
| | | 187 | | { |
| | | 188 | | case UpgradeState.None: |
| | 0 | 189 | | throw Fx.AssertAndThrow("Invalid State Transition: currentState=" + currentState + ", newState=" + n |
| | | 190 | | case UpgradeState.VerifyingUpgradeRequest: |
| | 1 | 191 | | if (currentState != UpgradeState.None //starting first upgrade |
| | 1 | 192 | | && currentState != UpgradeState.UpgradeComplete)//completing one upgrade and starting another |
| | | 193 | | { |
| | 0 | 194 | | throw Fx.AssertAndThrow("Invalid State Transition: currentState=" + currentState + ", newState=" |
| | | 195 | | } |
| | | 196 | | break; |
| | | 197 | | case UpgradeState.WritingUpgradeAck: |
| | 1 | 198 | | if (currentState != UpgradeState.VerifyingUpgradeRequest) |
| | | 199 | | { |
| | 0 | 200 | | throw Fx.AssertAndThrow("Invalid State Transition: currentState=" + currentState + ", newState=" |
| | | 201 | | } |
| | | 202 | | break; |
| | | 203 | | case UpgradeState.UpgradeAckSent: |
| | 1 | 204 | | if (currentState != UpgradeState.WritingUpgradeAck) |
| | | 205 | | { |
| | 0 | 206 | | throw Fx.AssertAndThrow("Invalid State Transition: currentState=" + currentState + ", newState=" |
| | | 207 | | } |
| | | 208 | | break; |
| | | 209 | | case UpgradeState.BeginUpgrade: |
| | 1 | 210 | | if (currentState != UpgradeState.UpgradeAckSent) |
| | | 211 | | { |
| | 0 | 212 | | throw Fx.AssertAndThrow("Invalid State Transition: currentState=" + currentState + ", newState=" |
| | | 213 | | } |
| | | 214 | | break; |
| | | 215 | | case UpgradeState.EndUpgrade: |
| | 1 | 216 | | if (currentState != UpgradeState.BeginUpgrade) |
| | | 217 | | { |
| | 0 | 218 | | throw Fx.AssertAndThrow("Invalid State Transition: currentState=" + currentState + ", newState=" |
| | | 219 | | } |
| | | 220 | | break; |
| | | 221 | | case UpgradeState.UpgradeComplete: |
| | 1 | 222 | | if (currentState != UpgradeState.EndUpgrade) |
| | | 223 | | { |
| | 0 | 224 | | throw Fx.AssertAndThrow("Invalid State Transition: currentState=" + currentState + ", newState=" |
| | | 225 | | } |
| | | 226 | | break; |
| | | 227 | | case UpgradeState.WritingPreambleEnd: |
| | 50 | 228 | | if (currentState != UpgradeState.None //no upgrade being used |
| | 50 | 229 | | && currentState != UpgradeState.UpgradeComplete)//upgrades are now complete, end the preamble ha |
| | | 230 | | { |
| | 0 | 231 | | throw Fx.AssertAndThrow("Invalid State Transition: currentState=" + currentState + ", newState=" |
| | | 232 | | } |
| | | 233 | | break; |
| | | 234 | | case UpgradeState.PreambleEndSent: |
| | 50 | 235 | | if (currentState != UpgradeState.WritingPreambleEnd) |
| | | 236 | | { |
| | 0 | 237 | | throw Fx.AssertAndThrow("Invalid State Transition: currentState=" + currentState + ", newState=" |
| | | 238 | | } |
| | | 239 | | break; |
| | | 240 | | default: |
| | 0 | 241 | | throw Fx.AssertAndThrow("Unexpected Upgrade State: " + newState); |
| | | 242 | | } |
| | | 243 | | |
| | 106 | 244 | | currentState = newState; |
| | 106 | 245 | | } |
| | | 246 | | |
| | | 247 | | public static async Task UpgradeConnectionAsync(FramingConnection connection, string contentType) |
| | | 248 | | { |
| | 1 | 249 | | var duplexPipeStream = new DuplexPipeStream(connection.Input, connection.Output); |
| | 1 | 250 | | connection.RawStream = duplexPipeStream; |
| | 1 | 251 | | StreamUpgradeAcceptor upgradeAcceptor = connection.StreamUpgradeAcceptor; |
| | 1 | 252 | | Stream stream = await upgradeAcceptor.AcceptUpgradeAsync(connection.RawStream); |
| | 1 | 253 | | duplexPipeStream.SetContentType(contentType); |
| | 1 | 254 | | CreatePipelineFromStream(connection, stream); |
| | 1 | 255 | | } |
| | | 256 | | |
| | | 257 | | private static void CreatePipelineFromStream(FramingConnection connection, Stream stream) |
| | | 258 | | { |
| | 1 | 259 | | var wrappedPipeline = new StreamDuplexPipe(connection.Transport, stream); |
| | 1 | 260 | | connection.Transport = wrappedPipeline; |
| | 1 | 261 | | } |
| | | 262 | | |
| | | 263 | | private static void SetupSecurityIfNecessary(FramingConnection connection) |
| | | 264 | | { |
| | 50 | 265 | | if (connection.StreamUpgradeAcceptor is StreamSecurityUpgradeAcceptor securityUpgradeAcceptor) |
| | | 266 | | { |
| | 1 | 267 | | Security.SecurityMessageProperty remoteSecurity = securityUpgradeAcceptor.GetRemoteSecurity(); |
| | | 268 | | |
| | 1 | 269 | | if (remoteSecurity == null) |
| | | 270 | | { |
| | 0 | 271 | | Exception securityFailedException = new ProtocolException( |
| | 0 | 272 | | SR.Format(SR.RemoteSecurityNotNegotiatedOnStreamUpgrade, connection.Via)); |
| | | 273 | | //WriteAuditFailure(securityUpgradeAcceptor, securityFailedException); |
| | 0 | 274 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(securityFailedException); |
| | | 275 | | } |
| | | 276 | | else |
| | | 277 | | { |
| | 1 | 278 | | connection.SecurityMessageProperty = remoteSecurity; |
| | | 279 | | // Audit Authentication Success |
| | | 280 | | //WriteAuditEvent(securityUpgradeAcceptor, AuditLevel.Success, null); |
| | | 281 | | } |
| | | 282 | | } |
| | 50 | 283 | | } |
| | | 284 | | |
| | | 285 | | private enum UpgradeState |
| | | 286 | | { |
| | | 287 | | None, |
| | | 288 | | VerifyingUpgradeRequest, |
| | | 289 | | WritingUpgradeAck, |
| | | 290 | | UpgradeAckSent, |
| | | 291 | | BeginUpgrade, |
| | | 292 | | EndUpgrade, |
| | | 293 | | UpgradeComplete, |
| | | 294 | | WritingPreambleEnd, |
| | | 295 | | PreambleEndSent, |
| | | 296 | | } |
| | | 297 | | } |
| | | 298 | | } |