| | | 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 | | using Microsoft.Extensions.Logging; |
| | | 11 | | |
| | | 12 | | namespace CoreWCF.Channels.Framing |
| | | 13 | | { |
| | | 14 | | internal class ServerFramingDuplexSessionMiddleware |
| | | 15 | | { |
| | | 16 | | private readonly HandshakeDelegate _next; |
| | | 17 | | |
| | 83 | 18 | | public ServerFramingDuplexSessionMiddleware(HandshakeDelegate next) |
| | | 19 | | { |
| | 83 | 20 | | _next = next; |
| | 83 | 21 | | } |
| | | 22 | | |
| | | 23 | | public async Task OnConnectedAsync(FramingConnection connection) |
| | | 24 | | { |
| | 64 | 25 | | bool success = false; |
| | | 26 | | try |
| | | 27 | | { |
| | 64 | 28 | | var decoder = connection.FramingDecoder as ServerSessionDecoder; |
| | | 29 | | Fx.Assert(decoder != null, "FramingDecoder must be non-null and an instance of ServerSessionDecoder"); |
| | | 30 | | // first validate our content type |
| | 64 | 31 | | ValidateContentType(connection, decoder); |
| | | 32 | | |
| | | 33 | | // next read any potential upgrades and finish consuming the preamble |
| | | 34 | | ReadOnlySequence<byte> buffer; |
| | 7 | 35 | | while (true) |
| | | 36 | | { |
| | 71 | 37 | | System.IO.Pipelines.ReadResult readResult = await connection.Input.ReadAsync(); |
| | 71 | 38 | | buffer = readResult.Buffer; |
| | 71 | 39 | | if (readResult.IsCompleted) |
| | | 40 | | { |
| | 0 | 41 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(decoder.CreatePrematureEOFException()) |
| | | 42 | | } |
| | | 43 | | |
| | 227 | 44 | | while (buffer.Length > 0) |
| | | 45 | | { |
| | 220 | 46 | | int bytesDecoded = decoder.Decode(buffer); |
| | 220 | 47 | | if (bytesDecoded > 0) |
| | | 48 | | { |
| | 85 | 49 | | buffer = buffer.Slice(bytesDecoded); |
| | | 50 | | } |
| | | 51 | | |
| | 220 | 52 | | switch (decoder.CurrentState) |
| | | 53 | | { |
| | | 54 | | case ServerSessionDecoder.State.UpgradeRequest: |
| | 7 | 55 | | ProcessUpgradeRequest(connection, decoder); |
| | | 56 | | |
| | | 57 | | // accept upgrade |
| | 7 | 58 | | await connection.Output.WriteAsync(ServerSessionEncoder.UpgradeResponseBytes); |
| | 7 | 59 | | await connection.Output.FlushAsync(); |
| | | 60 | | //await context.Transport.Output.WriteAsync |
| | | 61 | | //Connection.Write(ServerSessionEncoder.UpgradeResponseBytes, 0, ServerSessionEncoder.Up |
| | | 62 | | |
| | | 63 | | try |
| | | 64 | | { |
| | 7 | 65 | | connection.Input.AdvanceTo(buffer.Start); |
| | 7 | 66 | | buffer = ReadOnlySequence<byte>.Empty; |
| | 7 | 67 | | await UpgradeConnectionAsync(connection, decoder.Upgrade); |
| | | 68 | | // TODO: ChannelBinding |
| | | 69 | | //if (this.channelBindingProvider != null && this.channelBindingProvider.IsChannelBi |
| | | 70 | | //{ |
| | | 71 | | // this.SetChannelBinding(this.channelBindingProvider.GetChannelBinding(this.upgr |
| | | 72 | | //} |
| | | 73 | | |
| | | 74 | | //this.connectionBuffer = Connection.AsyncReadBuffer; |
| | 7 | 75 | | } |
| | | 76 | | catch (Exception exception) |
| | | 77 | | { |
| | 0 | 78 | | if (Fx.IsFatal(exception)) |
| | | 79 | | { |
| | 0 | 80 | | throw; |
| | | 81 | | } |
| | | 82 | | |
| | | 83 | | // Audit Authentication Failure |
| | | 84 | | //WriteAuditFailure(upgradeAcceptor as StreamSecurityUpgradeAcceptor, exception); |
| | | 85 | | throw; |
| | | 86 | | } |
| | | 87 | | break; |
| | | 88 | | |
| | | 89 | | case ServerSessionDecoder.State.Start: |
| | 64 | 90 | | SetupSecurityIfNecessary(connection); |
| | | 91 | | // we've finished the preamble. Ack and continue to the next middleware. |
| | 64 | 92 | | await connection.Output.WriteAsync(ServerSessionEncoder.AckResponseBytes); |
| | 64 | 93 | | await connection.Output.FlushAsync(); |
| | 64 | 94 | | connection.Input.AdvanceTo(buffer.Start); |
| | 64 | 95 | | await _next(connection); |
| | 63 | 96 | | success = true; |
| | 63 | 97 | | return; |
| | | 98 | | |
| | | 99 | | } |
| | | 100 | | } |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | finally |
| | | 104 | | { |
| | 64 | 105 | | if (!success) |
| | | 106 | | { |
| | 1 | 107 | | connection.Abort(); |
| | | 108 | | } |
| | | 109 | | } |
| | 63 | 110 | | } |
| | | 111 | | |
| | | 112 | | private static void ValidateContentType(FramingConnection connection, FramingDecoder decoder) |
| | | 113 | | { |
| | 64 | 114 | | MessageEncoderFactory messageEncoderFactory = connection.MessageEncoderFactory; |
| | 64 | 115 | | MessageEncoder messageEncoder = messageEncoderFactory.CreateSessionEncoder(); |
| | 64 | 116 | | connection.MessageEncoder = messageEncoder; |
| | | 117 | | |
| | 64 | 118 | | if (!messageEncoder.IsContentTypeSupported(decoder.ContentType)) |
| | | 119 | | { |
| | | 120 | | // TODO: Send fault response |
| | | 121 | | //SendFault(FramingEncodingString.ContentTypeInvalidFault, ref timeoutHelper); |
| | 0 | 122 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ProtocolException(SR.Format( |
| | 0 | 123 | | SR.ContentTypeMismatch, decoder.ContentType, messageEncoder.ContentType))); |
| | | 124 | | } |
| | | 125 | | |
| | 64 | 126 | | if (messageEncoder is ICompressedMessageEncoder compressedMessageEncoder && compressedMessageEncoder.Compres |
| | | 127 | | { |
| | 4 | 128 | | compressedMessageEncoder.SetSessionContentType(decoder.ContentType); |
| | | 129 | | } |
| | 64 | 130 | | } |
| | | 131 | | |
| | | 132 | | private static void ProcessUpgradeRequest(FramingConnection connection, ServerSessionDecoder decoder) |
| | | 133 | | { |
| | 7 | 134 | | StreamUpgradeAcceptor upgradeAcceptor = connection.StreamUpgradeAcceptor; |
| | 7 | 135 | | if (upgradeAcceptor == null) |
| | | 136 | | { |
| | | 137 | | // TODO: SendFault |
| | | 138 | | //SendFault(FramingEncodingString.UpgradeInvalidFault, ref timeoutHelper); |
| | 0 | 139 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 140 | | new ProtocolException(SR.Format(SR.UpgradeRequestToNonupgradableService, decoder.Upgrade))); |
| | | 141 | | } |
| | | 142 | | |
| | 7 | 143 | | if (!upgradeAcceptor.CanUpgrade(decoder.Upgrade)) |
| | | 144 | | { |
| | | 145 | | // TODO: SendFault |
| | | 146 | | //SendFault(FramingEncodingString.UpgradeInvalidFault, ref timeoutHelper); |
| | 0 | 147 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 148 | | new ProtocolException(SR.Format(SR.UpgradeProtocolNotSupported, decoder.Upgrade))); |
| | | 149 | | } |
| | 7 | 150 | | } |
| | | 151 | | |
| | | 152 | | public static async Task UpgradeConnectionAsync(FramingConnection connection, string contentType) |
| | | 153 | | { |
| | 7 | 154 | | var duplexPipeStream = new DuplexPipeStream(connection.Input, connection.Output); |
| | 7 | 155 | | connection.RawStream = duplexPipeStream; |
| | 7 | 156 | | StreamUpgradeAcceptor upgradeAcceptor = connection.StreamUpgradeAcceptor; |
| | 7 | 157 | | connection.Logger.StartStreamUpgradeAccept(upgradeAcceptor); |
| | 7 | 158 | | Stream stream = await upgradeAcceptor.AcceptUpgradeAsync(connection.RawStream); |
| | 7 | 159 | | duplexPipeStream.SetContentType(contentType); |
| | 7 | 160 | | connection.Logger.CompleteStreamUpgradeAccept(upgradeAcceptor); |
| | 7 | 161 | | CreatePipelineFromStream(connection, stream); |
| | 7 | 162 | | } |
| | | 163 | | |
| | | 164 | | private static void SetupSecurityIfNecessary(FramingConnection connection) |
| | | 165 | | { |
| | 64 | 166 | | if (connection.StreamUpgradeAcceptor is StreamSecurityUpgradeAcceptor securityUpgradeAcceptor) |
| | | 167 | | { |
| | 7 | 168 | | Security.SecurityMessageProperty remoteSecurity = securityUpgradeAcceptor.GetRemoteSecurity(); |
| | | 169 | | |
| | 7 | 170 | | if (remoteSecurity == null) |
| | | 171 | | { |
| | 0 | 172 | | Exception securityFailedException = new ProtocolException( |
| | 0 | 173 | | SR.Format(SR.RemoteSecurityNotNegotiatedOnStreamUpgrade, connection.Via)); |
| | | 174 | | //WriteAuditFailure(securityUpgradeAcceptor, securityFailedException); |
| | 0 | 175 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(securityFailedException); |
| | | 176 | | } |
| | | 177 | | else |
| | | 178 | | { |
| | 7 | 179 | | connection.SecurityMessageProperty = remoteSecurity; |
| | | 180 | | // Audit Authentication Success |
| | | 181 | | //WriteAuditEvent(securityUpgradeAcceptor, AuditLevel.Success, null); |
| | | 182 | | } |
| | | 183 | | } |
| | 64 | 184 | | } |
| | | 185 | | |
| | | 186 | | private static void CreatePipelineFromStream(FramingConnection connection, Stream stream) |
| | | 187 | | { |
| | 7 | 188 | | var wrappedPipeline = new StreamDuplexPipe(connection.Transport, stream); |
| | 7 | 189 | | connection.Transport = wrappedPipeline; |
| | 7 | 190 | | } |
| | | 191 | | } |
| | | 192 | | } |