| | | 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.Text; |
| | | 6 | | using CoreWCF.Runtime; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Channels.Framing |
| | | 9 | | { |
| | | 10 | | internal static class IntEncoder |
| | | 11 | | { |
| | | 12 | | public const int MaxEncodedSize = 5; |
| | | 13 | | |
| | | 14 | | public static int Encode(int value, byte[] bytes, int offset) |
| | | 15 | | { |
| | 448 | 16 | | int count = 1; |
| | 881 | 17 | | while ((value & 0xFFFFFF80) != 0) |
| | | 18 | | { |
| | 433 | 19 | | bytes[offset++] = (byte)((value & 0x7F) | 0x80); |
| | 433 | 20 | | count++; |
| | 433 | 21 | | value >>= 7; |
| | | 22 | | } |
| | 448 | 23 | | bytes[offset] = (byte)value; |
| | 448 | 24 | | return count; |
| | | 25 | | } |
| | | 26 | | |
| | | 27 | | public static int GetEncodedSize(int value) |
| | | 28 | | { |
| | 86 | 29 | | if (value < 0) |
| | | 30 | | { |
| | 0 | 31 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException(nameof(value), |
| | 0 | 32 | | SRCommon.ValueMustBeNonNegative)); |
| | | 33 | | } |
| | | 34 | | |
| | 86 | 35 | | int count = 1; |
| | 169 | 36 | | while ((value & 0xFFFFFF80) != 0) |
| | | 37 | | { |
| | 83 | 38 | | count++; |
| | 83 | 39 | | value >>= 7; |
| | | 40 | | } |
| | 86 | 41 | | return count; |
| | | 42 | | } |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | internal abstract class EncodedFramingRecord |
| | | 46 | | { |
| | | 47 | | protected EncodedFramingRecord(byte[] encodedBytes) |
| | | 48 | | { |
| | | 49 | | EncodedBytes = encodedBytes; |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | internal EncodedFramingRecord(FramingRecordType recordType, string value) |
| | | 53 | | { |
| | | 54 | | int valueByteCount = Encoding.UTF8.GetByteCount(value); |
| | | 55 | | int sizeByteCount = IntEncoder.GetEncodedSize(valueByteCount); |
| | | 56 | | EncodedBytes = Fx.AllocateByteArray(checked(1 + sizeByteCount + valueByteCount)); |
| | | 57 | | EncodedBytes[0] = (byte)recordType; |
| | | 58 | | int offset = 1; |
| | | 59 | | offset += IntEncoder.Encode(valueByteCount, EncodedBytes, offset); |
| | | 60 | | Encoding.UTF8.GetBytes(value, 0, value.Length, EncodedBytes, offset); |
| | | 61 | | SetEncodedBytes(EncodedBytes); |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | |
| | | 65 | | public byte[] EncodedBytes { get; private set; } |
| | | 66 | | |
| | | 67 | | protected void SetEncodedBytes(byte[] encodedBytes) |
| | | 68 | | { |
| | | 69 | | EncodedBytes = encodedBytes; |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | public override int GetHashCode() |
| | | 73 | | { |
| | | 74 | | return (EncodedBytes[0] << 16) | |
| | | 75 | | (EncodedBytes[EncodedBytes.Length / 2] << 8) | |
| | | 76 | | EncodedBytes[EncodedBytes.Length - 1]; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | public override bool Equals(object o) |
| | | 80 | | { |
| | | 81 | | if (o is EncodedFramingRecord) |
| | | 82 | | { |
| | | 83 | | return Equals((EncodedFramingRecord)o); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | return false; |
| | | 87 | | } |
| | | 88 | | |
| | | 89 | | public bool Equals(EncodedFramingRecord other) |
| | | 90 | | { |
| | | 91 | | if (other == null) |
| | | 92 | | { |
| | | 93 | | return false; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | if (other == this) |
| | | 97 | | { |
| | | 98 | | return true; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | byte[] otherBytes = other.EncodedBytes; |
| | | 102 | | if (EncodedBytes.Length != otherBytes.Length) |
| | | 103 | | { |
| | | 104 | | return false; |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | for (int i = 0; i < EncodedBytes.Length; i++) |
| | | 108 | | { |
| | | 109 | | if (EncodedBytes[i] != otherBytes[i]) |
| | | 110 | | { |
| | | 111 | | return false; |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | return true; |
| | | 116 | | } |
| | | 117 | | } |
| | | 118 | | |
| | | 119 | | internal class EncodedContentType : EncodedFramingRecord |
| | | 120 | | { |
| | | 121 | | private EncodedContentType(FramingEncodingType encodingType) : |
| | | 122 | | base(new byte[] { (byte)FramingRecordType.KnownEncoding, (byte)encodingType }) |
| | | 123 | | { |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private EncodedContentType(string contentType) |
| | | 127 | | : base(FramingRecordType.ExtensibleEncoding, contentType) |
| | | 128 | | { |
| | | 129 | | } |
| | | 130 | | |
| | | 131 | | public static EncodedContentType Create(string contentType) |
| | | 132 | | { |
| | | 133 | | if (contentType == FramingEncodingString.ExtendedBinarySessionDeflate) |
| | | 134 | | { |
| | | 135 | | return new EncodedContentType(FramingEncodingType.ExtendedBinarySessionDeflate); |
| | | 136 | | } |
| | | 137 | | else if (contentType == FramingEncodingString.ExtendedBinaryDeflate) |
| | | 138 | | { |
| | | 139 | | return new EncodedContentType(FramingEncodingType.ExtendedBinaryDeflate); |
| | | 140 | | } |
| | | 141 | | else if (contentType == FramingEncodingString.ExtendedBinarySessionGZip) |
| | | 142 | | { |
| | | 143 | | return new EncodedContentType(FramingEncodingType.ExtendedBinarySessionGZip); |
| | | 144 | | } |
| | | 145 | | else if (contentType == FramingEncodingString.ExtendedBinaryGZip) |
| | | 146 | | { |
| | | 147 | | return new EncodedContentType(FramingEncodingType.ExtendedBinaryGZip); |
| | | 148 | | } |
| | | 149 | | else if(contentType == FramingEncodingString.BinarySession) |
| | | 150 | | { |
| | | 151 | | return new EncodedContentType(FramingEncodingType.BinarySession); |
| | | 152 | | } |
| | | 153 | | else if (contentType == FramingEncodingString.Binary) |
| | | 154 | | { |
| | | 155 | | return new EncodedContentType(FramingEncodingType.Binary); |
| | | 156 | | } |
| | | 157 | | else if (contentType == FramingEncodingString.Soap12Utf8) |
| | | 158 | | { |
| | | 159 | | return new EncodedContentType(FramingEncodingType.Soap12Utf8); |
| | | 160 | | } |
| | | 161 | | else if (contentType == FramingEncodingString.Soap11Utf8) |
| | | 162 | | { |
| | | 163 | | return new EncodedContentType(FramingEncodingType.Soap11Utf8); |
| | | 164 | | } |
| | | 165 | | else if (contentType == FramingEncodingString.Soap12Utf16) |
| | | 166 | | { |
| | | 167 | | return new EncodedContentType(FramingEncodingType.Soap12Utf16); |
| | | 168 | | } |
| | | 169 | | else if (contentType == FramingEncodingString.Soap11Utf16) |
| | | 170 | | { |
| | | 171 | | return new EncodedContentType(FramingEncodingType.Soap11Utf16); |
| | | 172 | | } |
| | | 173 | | else if (contentType == FramingEncodingString.Soap12Utf16FFFE) |
| | | 174 | | { |
| | | 175 | | return new EncodedContentType(FramingEncodingType.Soap12Utf16FFFE); |
| | | 176 | | } |
| | | 177 | | else if (contentType == FramingEncodingString.Soap11Utf16FFFE) |
| | | 178 | | { |
| | | 179 | | return new EncodedContentType(FramingEncodingType.Soap11Utf16FFFE); |
| | | 180 | | } |
| | | 181 | | else if (contentType == FramingEncodingString.MTOM) |
| | | 182 | | { |
| | | 183 | | return new EncodedContentType(FramingEncodingType.MTOM); |
| | | 184 | | } |
| | | 185 | | else |
| | | 186 | | { |
| | | 187 | | return new EncodedContentType(contentType); |
| | | 188 | | } |
| | | 189 | | } |
| | | 190 | | } |
| | | 191 | | |
| | | 192 | | internal class EncodedVia : EncodedFramingRecord |
| | | 193 | | { |
| | | 194 | | public EncodedVia(string via) |
| | | 195 | | : base(FramingRecordType.Via, via) |
| | | 196 | | { |
| | | 197 | | } |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | internal class EncodedUpgrade : EncodedFramingRecord |
| | | 201 | | { |
| | | 202 | | public EncodedUpgrade(string contentType) |
| | | 203 | | : base(FramingRecordType.UpgradeRequest, contentType) |
| | | 204 | | { |
| | | 205 | | } |
| | | 206 | | } |
| | | 207 | | |
| | | 208 | | internal class EncodedFault : EncodedFramingRecord |
| | | 209 | | { |
| | | 210 | | public EncodedFault(string fault) |
| | | 211 | | : base(FramingRecordType.Fault, fault) |
| | | 212 | | { |
| | | 213 | | } |
| | | 214 | | } |
| | | 215 | | |
| | | 216 | | // used by SimplexEncoder/DuplexEncoder |
| | | 217 | | internal abstract class SessionEncoder |
| | | 218 | | { |
| | | 219 | | public const int MaxMessageFrameSize = 1 + IntEncoder.MaxEncodedSize; |
| | | 220 | | |
| | | 221 | | protected SessionEncoder() { } |
| | | 222 | | |
| | | 223 | | public static byte[] PreambleEndBytes = new byte[] { |
| | | 224 | | (byte)FramingRecordType.PreambleEnd |
| | | 225 | | }; |
| | | 226 | | |
| | | 227 | | public static byte[] EndBytes = new byte[] { |
| | | 228 | | (byte)FramingRecordType.End |
| | | 229 | | }; |
| | | 230 | | |
| | | 231 | | public static int CalcStartSize(EncodedVia via, EncodedContentType contentType) |
| | | 232 | | { |
| | | 233 | | return via.EncodedBytes.Length + contentType.EncodedBytes.Length; |
| | | 234 | | } |
| | | 235 | | |
| | | 236 | | public static void EncodeStart(byte[] buffer, int offset, EncodedVia via, EncodedContentType contentType) |
| | | 237 | | { |
| | | 238 | | Buffer.BlockCopy(via.EncodedBytes, 0, buffer, offset, via.EncodedBytes.Length); |
| | | 239 | | Buffer.BlockCopy(contentType.EncodedBytes, 0, buffer, offset + via.EncodedBytes.Length, contentType.EncodedB |
| | | 240 | | } |
| | | 241 | | |
| | | 242 | | public static ArraySegment<byte> EncodeMessageFrame(ArraySegment<byte> messageFrame) |
| | | 243 | | { |
| | | 244 | | int spaceNeeded = 1 + IntEncoder.GetEncodedSize(messageFrame.Count); |
| | | 245 | | int offset = messageFrame.Offset - spaceNeeded; |
| | | 246 | | if (offset < 0) |
| | | 247 | | { |
| | | 248 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("messageFrame. |
| | | 249 | | messageFrame.Offset, SR.Format(SR.SpaceNeededExceedsMessageFrameOffset, spaceNeeded))); |
| | | 250 | | } |
| | | 251 | | |
| | | 252 | | byte[] buffer = messageFrame.Array; |
| | | 253 | | buffer[offset++] = (byte)FramingRecordType.SizedEnvelope; |
| | | 254 | | IntEncoder.Encode(messageFrame.Count, buffer, offset); |
| | | 255 | | return new ArraySegment<byte>(buffer, messageFrame.Offset - spaceNeeded, messageFrame.Count + spaceNeeded); |
| | | 256 | | } |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | // used by ServerDuplexEncoder/ServerSimplexEncoder |
| | | 260 | | internal abstract class ServerSessionEncoder : SessionEncoder |
| | | 261 | | { |
| | | 262 | | protected ServerSessionEncoder() { } |
| | | 263 | | |
| | | 264 | | public static byte[] AckResponseBytes = new byte[] { |
| | | 265 | | (byte)FramingRecordType.PreambleAck |
| | | 266 | | }; |
| | | 267 | | public static byte[] UpgradeResponseBytes = new byte[] { |
| | | 268 | | (byte)FramingRecordType.UpgradeResponse |
| | | 269 | | }; |
| | | 270 | | } |
| | | 271 | | |
| | | 272 | | // Pattern: |
| | | 273 | | // ModeBytes, |
| | | 274 | | // EncodeStart, |
| | | 275 | | // EncodeUpgrade*, |
| | | 276 | | // EncodeMessageFrame*, |
| | | 277 | | // EndBytes |
| | | 278 | | internal class ClientDuplexEncoder : SessionEncoder |
| | | 279 | | { |
| | | 280 | | private ClientDuplexEncoder() { } |
| | | 281 | | |
| | | 282 | | public static byte[] ModeBytes = new byte[] { |
| | | 283 | | (byte)FramingRecordType.Version, |
| | | 284 | | (byte)FramingVersion.Major, |
| | | 285 | | (byte)FramingVersion.Minor, |
| | | 286 | | (byte)FramingRecordType.Mode, |
| | | 287 | | (byte)FramingMode.Duplex }; |
| | | 288 | | } |
| | | 289 | | |
| | | 290 | | // Pattern: |
| | | 291 | | // ModeBytes, |
| | | 292 | | // EncodeStart, |
| | | 293 | | // EncodeUpgrade*, |
| | | 294 | | // EncodeMessageFrame*, |
| | | 295 | | // EndBytes |
| | | 296 | | internal class ClientSimplexEncoder : SessionEncoder |
| | | 297 | | { |
| | | 298 | | private ClientSimplexEncoder() { } |
| | | 299 | | |
| | | 300 | | public static byte[] ModeBytes = new byte[] { |
| | | 301 | | (byte)FramingRecordType.Version, |
| | | 302 | | (byte)FramingVersion.Major, |
| | | 303 | | (byte)FramingVersion.Minor, |
| | | 304 | | (byte)FramingRecordType.Mode, |
| | | 305 | | (byte)FramingMode.Simplex }; |
| | | 306 | | } |
| | | 307 | | |
| | | 308 | | // shared code for client and server |
| | | 309 | | internal abstract class SingletonEncoder |
| | | 310 | | { |
| | | 311 | | protected SingletonEncoder() |
| | | 312 | | { |
| | | 313 | | } |
| | | 314 | | |
| | | 315 | | public static byte[] EnvelopeStartBytes = new byte[] { |
| | | 316 | | (byte)FramingRecordType.UnsizedEnvelope }; |
| | | 317 | | |
| | | 318 | | public static byte[] EnvelopeEndBytes = new byte[] { |
| | | 319 | | (byte)0 }; |
| | | 320 | | |
| | | 321 | | public static byte[] EnvelopeEndFramingEndBytes = new byte[] { |
| | | 322 | | (byte)0, (byte)FramingRecordType.End }; |
| | | 323 | | |
| | | 324 | | public static byte[] EndBytes = new byte[] { |
| | | 325 | | (byte)FramingRecordType.End }; |
| | | 326 | | |
| | | 327 | | public static ArraySegment<byte> EncodeMessageFrame(ArraySegment<byte> messageFrame) |
| | | 328 | | { |
| | | 329 | | int spaceNeeded = IntEncoder.GetEncodedSize(messageFrame.Count); |
| | | 330 | | int offset = messageFrame.Offset - spaceNeeded; |
| | | 331 | | if (offset < 0) |
| | | 332 | | { |
| | | 333 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new ArgumentOutOfRangeException("messageFrame. |
| | | 334 | | messageFrame.Offset, SR.Format(SR.SpaceNeededExceedsMessageFrameOffset, spaceNeeded))); |
| | | 335 | | } |
| | | 336 | | |
| | | 337 | | byte[] buffer = messageFrame.Array; |
| | | 338 | | IntEncoder.Encode(messageFrame.Count, buffer, offset); |
| | | 339 | | return new ArraySegment<byte>(buffer, offset, messageFrame.Count + spaceNeeded); |
| | | 340 | | } |
| | | 341 | | } |
| | | 342 | | |
| | | 343 | | // Pattern: |
| | | 344 | | // ModeBytes, |
| | | 345 | | // EncodeStart, |
| | | 346 | | // EncodeUpgrade*, |
| | | 347 | | // EnvelopeStartBytes, |
| | | 348 | | // streamed-message-bytes* |
| | | 349 | | internal class ClientSingletonEncoder : SingletonEncoder |
| | | 350 | | { |
| | | 351 | | private ClientSingletonEncoder() { } |
| | | 352 | | |
| | | 353 | | |
| | | 354 | | public static byte[] PreambleEndBytes = new byte[] { |
| | | 355 | | (byte)FramingRecordType.PreambleEnd |
| | | 356 | | }; |
| | | 357 | | |
| | | 358 | | public static byte[] ModeBytes = new byte[] { |
| | | 359 | | (byte)FramingRecordType.Version, |
| | | 360 | | (byte)FramingVersion.Major, |
| | | 361 | | (byte)FramingVersion.Minor, |
| | | 362 | | (byte)FramingRecordType.Mode, |
| | | 363 | | (byte)FramingMode.Singleton }; |
| | | 364 | | |
| | | 365 | | public static int CalcStartSize(EncodedVia via, EncodedContentType contentType) |
| | | 366 | | { |
| | | 367 | | return via.EncodedBytes.Length + contentType.EncodedBytes.Length; |
| | | 368 | | } |
| | | 369 | | |
| | | 370 | | public static void EncodeStart(byte[] buffer, int offset, EncodedVia via, EncodedContentType contentType) |
| | | 371 | | { |
| | | 372 | | Buffer.BlockCopy(via.EncodedBytes, 0, buffer, offset, via.EncodedBytes.Length); |
| | | 373 | | Buffer.BlockCopy(contentType.EncodedBytes, 0, buffer, offset + via.EncodedBytes.Length, contentType.EncodedB |
| | | 374 | | } |
| | | 375 | | } |
| | | 376 | | |
| | | 377 | | // Pattern: |
| | | 378 | | // (UpgradeResponseBytes, upgrade-bytes)?, |
| | | 379 | | internal class ServerSingletonEncoder : SingletonEncoder |
| | | 380 | | { |
| | | 381 | | private ServerSingletonEncoder() { } |
| | | 382 | | |
| | | 383 | | public static byte[] AckResponseBytes = new byte[] { |
| | | 384 | | (byte)FramingRecordType.PreambleAck |
| | | 385 | | }; |
| | | 386 | | |
| | | 387 | | public static byte[] UpgradeResponseBytes = new byte[] { |
| | | 388 | | (byte)FramingRecordType.UpgradeResponse |
| | | 389 | | }; |
| | | 390 | | } |
| | | 391 | | } |