| | | 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.Net.Security; |
| | | 6 | | using System.Security.Authentication; |
| | | 7 | | using System.Text; |
| | | 8 | | using System.Xml; |
| | | 9 | | using CoreWCF.Security; |
| | | 10 | | |
| | | 11 | | namespace CoreWCF.Channels |
| | | 12 | | { |
| | | 13 | | internal static class EncoderDefaults |
| | | 14 | | { |
| | | 15 | | internal const int MaxReadPoolSize = 64; |
| | | 16 | | internal const int MaxWritePoolSize = 16; |
| | | 17 | | |
| | | 18 | | internal const int BufferedReadDefaultMaxDepth = 128; |
| | | 19 | | internal const int BufferedReadDefaultMaxStringContentLength = int.MaxValue; |
| | | 20 | | internal const int BufferedReadDefaultMaxArrayLength = int.MaxValue; |
| | | 21 | | internal const int BufferedReadDefaultMaxBytesPerRead = int.MaxValue; |
| | | 22 | | internal const int BufferedReadDefaultMaxNameTableCharCount = int.MaxValue; |
| | | 23 | | |
| | | 24 | | internal const CompressionFormat DefaultCompressionFormat = CompressionFormat.None; |
| | | 25 | | |
| | 10 | 26 | | internal static readonly XmlDictionaryReaderQuotas ReaderQuotas = new XmlDictionaryReaderQuotas(); |
| | | 27 | | |
| | | 28 | | internal static bool IsDefaultReaderQuotas(XmlDictionaryReaderQuotas quotas) |
| | | 29 | | { |
| | 0 | 30 | | return quotas.ModifiedQuotas == 0x00; |
| | | 31 | | } |
| | | 32 | | } |
| | | 33 | | |
| | | 34 | | internal static class TextEncoderDefaults |
| | | 35 | | { |
| | | 36 | | internal static readonly Encoding Encoding = Encoding.GetEncoding(EncodingString, new EncoderExceptionFallback() |
| | | 37 | | new DecoderExceptionFallback()); |
| | | 38 | | |
| | | 39 | | internal const string EncodingString = "utf-8"; |
| | | 40 | | |
| | | 41 | | internal static readonly Encoding[] SupportedEncodings = |
| | | 42 | | { |
| | | 43 | | Encoding.UTF8, Encoding.Unicode, |
| | | 44 | | Encoding.BigEndianUnicode |
| | | 45 | | }; |
| | | 46 | | |
| | | 47 | | internal static readonly CharSetEncoding[] CharSetEncodings = |
| | | 48 | | { |
| | | 49 | | new CharSetEncoding("utf-8", Encoding.UTF8), |
| | | 50 | | new CharSetEncoding("utf-16LE", Encoding.Unicode), |
| | | 51 | | new CharSetEncoding("utf-16BE", Encoding.BigEndianUnicode), |
| | | 52 | | new CharSetEncoding("utf-16", null), // Ignore. Ambiguous charSet, so autodetect. |
| | | 53 | | new CharSetEncoding(null, null), // CharSet omitted, so autodetect. |
| | | 54 | | }; |
| | | 55 | | |
| | | 56 | | |
| | | 57 | | internal static void ValidateEncoding(Encoding encoding) |
| | | 58 | | { |
| | | 59 | | string charSet = encoding.WebName; |
| | | 60 | | Encoding[] supportedEncodings = SupportedEncodings; |
| | | 61 | | for (int i = 0; i < supportedEncodings.Length; i++) |
| | | 62 | | { |
| | | 63 | | if (charSet == supportedEncodings[i].WebName) |
| | | 64 | | { |
| | | 65 | | return; |
| | | 66 | | } |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | | 70 | | new ArgumentException(SR.Format(SR.MessageTextEncodingNotSupported, charSet), nameof(encoding))); |
| | | 71 | | } |
| | | 72 | | |
| | | 73 | | internal static string EncodingToCharSet(Encoding encoding) |
| | | 74 | | { |
| | | 75 | | string webName = encoding.WebName; |
| | | 76 | | CharSetEncoding[] charSetEncodings = CharSetEncodings; |
| | | 77 | | for (int i = 0; i < charSetEncodings.Length; i++) |
| | | 78 | | { |
| | | 79 | | Encoding enc = charSetEncodings[i].Encoding; |
| | | 80 | | if (enc == null) |
| | | 81 | | { |
| | | 82 | | continue; |
| | | 83 | | } |
| | | 84 | | |
| | | 85 | | if (enc.WebName == webName) |
| | | 86 | | { |
| | | 87 | | return charSetEncodings[i].CharSet; |
| | | 88 | | } |
| | | 89 | | } |
| | | 90 | | return null; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | internal static bool TryGetEncoding(string charSet, out Encoding encoding) |
| | | 94 | | { |
| | | 95 | | CharSetEncoding[] charSetEncodings = CharSetEncodings; |
| | | 96 | | |
| | | 97 | | // Quick check for exact equality |
| | | 98 | | for (int i = 0; i < charSetEncodings.Length; i++) |
| | | 99 | | { |
| | | 100 | | if (charSetEncodings[i].CharSet == charSet) |
| | | 101 | | { |
| | | 102 | | encoding = charSetEncodings[i].Encoding; |
| | | 103 | | return true; |
| | | 104 | | } |
| | | 105 | | } |
| | | 106 | | |
| | | 107 | | // Check for case insensitive match |
| | | 108 | | for (int i = 0; i < charSetEncodings.Length; i++) |
| | | 109 | | { |
| | | 110 | | string compare = charSetEncodings[i].CharSet; |
| | | 111 | | if (compare == null) |
| | | 112 | | { |
| | | 113 | | continue; |
| | | 114 | | } |
| | | 115 | | |
| | | 116 | | if (compare.Equals(charSet, StringComparison.OrdinalIgnoreCase)) |
| | | 117 | | { |
| | | 118 | | encoding = charSetEncodings[i].Encoding; |
| | | 119 | | return true; |
| | | 120 | | } |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | encoding = null; |
| | | 124 | | return false; |
| | | 125 | | } |
| | | 126 | | |
| | | 127 | | internal class CharSetEncoding |
| | | 128 | | { |
| | | 129 | | internal string CharSet; |
| | | 130 | | internal Encoding Encoding; |
| | | 131 | | |
| | | 132 | | internal CharSetEncoding(string charSet, Encoding enc) |
| | | 133 | | { |
| | | 134 | | CharSet = charSet; |
| | | 135 | | Encoding = enc; |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | } |
| | | 139 | | |
| | | 140 | | internal static class MtomEncoderDefaults |
| | | 141 | | { |
| | | 142 | | internal const int MaxBufferSize = 65536; |
| | | 143 | | } |
| | | 144 | | |
| | | 145 | | internal static class BinaryEncoderDefaults |
| | | 146 | | { |
| | | 147 | | internal static EnvelopeVersion EnvelopeVersion { get { return EnvelopeVersion.Soap12; } } |
| | | 148 | | internal static BinaryVersion BinaryVersion { get { return BinaryVersion.Version1; } } |
| | | 149 | | internal const int MaxSessionSize = 2048; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | internal static class TransportDefaults |
| | | 153 | | { |
| | | 154 | | internal const bool ExtractGroupsForWindowsAccounts = SspiSecurityTokenProvider.DefaultExtractWindowsGroupClaims |
| | | 155 | | internal const bool ManualAddressing = false; |
| | | 156 | | internal const long MaxReceivedMessageSize = 65536; |
| | | 157 | | internal const int MaxBufferSize = (int)MaxReceivedMessageSize; |
| | | 158 | | internal const long MaxBufferPoolSize = 512 * 1024; |
| | | 159 | | internal const int MaxFaultSize = MaxBufferSize; |
| | | 160 | | internal const bool RequireClientCertificate = false; |
| | | 161 | | internal const int TcpUriDefaultPort = 808; |
| | | 162 | | |
| | | 163 | | internal const SslProtocols SslProtocols = System.Security.Authentication.SslProtocols.None; // Let the OS decid |
| | | 164 | | |
| | | 165 | | internal static MessageEncoderFactory GetDefaultMessageEncoderFactory() |
| | | 166 | | { |
| | | 167 | | return new BinaryMessageEncodingBindingElement().CreateMessageEncoderFactory(); |
| | | 168 | | } |
| | | 169 | | } |
| | | 170 | | |
| | | 171 | | internal static class ConnectionOrientedTransportDefaults |
| | | 172 | | { |
| | | 173 | | internal const int ConnectionBufferSize = 8192; |
| | | 174 | | } |
| | | 175 | | } |