| | | 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 System.Xml; |
| | | 7 | | |
| | | 8 | | namespace CoreWCF.Channels |
| | | 9 | | { |
| | | 10 | | internal static class EncoderDefaults |
| | | 11 | | { |
| | | 12 | | internal const int MaxReadPoolSize = 64; |
| | | 13 | | internal const int MaxWritePoolSize = 16; |
| | | 14 | | |
| | | 15 | | internal const int BufferedReadDefaultMaxDepth = 128; |
| | | 16 | | internal const int BufferedReadDefaultMaxStringContentLength = int.MaxValue; |
| | | 17 | | internal const int BufferedReadDefaultMaxArrayLength = int.MaxValue; |
| | | 18 | | internal const int BufferedReadDefaultMaxBytesPerRead = int.MaxValue; |
| | | 19 | | internal const int BufferedReadDefaultMaxNameTableCharCount = int.MaxValue; |
| | | 20 | | |
| | | 21 | | internal static readonly XmlDictionaryReaderQuotas ReaderQuotas = new XmlDictionaryReaderQuotas(); |
| | | 22 | | |
| | | 23 | | internal static bool IsDefaultReaderQuotas(XmlDictionaryReaderQuotas quotas) |
| | | 24 | | { |
| | | 25 | | return quotas.ModifiedQuotas == 0x00; |
| | | 26 | | } |
| | | 27 | | } |
| | | 28 | | |
| | | 29 | | internal static class TransportDefaults |
| | | 30 | | { |
| | | 31 | | internal const long MaxReceivedMessageSize = 65536; |
| | | 32 | | internal const int MaxBufferSize = (int)MaxReceivedMessageSize; |
| | | 33 | | internal const long MaxBufferPoolSize = 512 * 1024; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | internal static class HttpTransportDefaults |
| | | 37 | | { |
| | | 38 | | internal const TransferMode TransferMode = CoreWCF.TransferMode.Buffered; |
| | | 39 | | internal const string Realm = ""; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | internal static class TextEncoderDefaults |
| | | 43 | | { |
| | 2 | 44 | | public static readonly Encoding Encoding = Encoding.GetEncoding(EncodingString, new EncoderExceptionFallback(), |
| | 2 | 45 | | new DecoderExceptionFallback()); |
| | | 46 | | |
| | | 47 | | internal const string EncodingString = "utf-8"; |
| | | 48 | | |
| | 2 | 49 | | internal static readonly Encoding[] SupportedEncodings = |
| | 2 | 50 | | { |
| | 2 | 51 | | Encoding.UTF8, Encoding.Unicode, |
| | 2 | 52 | | Encoding.BigEndianUnicode |
| | 2 | 53 | | }; |
| | | 54 | | |
| | 2 | 55 | | internal static readonly CharSetEncoding[] CharSetEncodings = |
| | 2 | 56 | | { |
| | 2 | 57 | | new CharSetEncoding("utf-8", Encoding.UTF8), |
| | 2 | 58 | | new CharSetEncoding("utf-16LE", Encoding.Unicode), |
| | 2 | 59 | | new CharSetEncoding("utf-16BE", Encoding.BigEndianUnicode), |
| | 2 | 60 | | new CharSetEncoding("utf-16", null), // Ignore. Ambiguous charSet, so autodetect. |
| | 2 | 61 | | new CharSetEncoding(null, null), // CharSet omitted, so autodetect. |
| | 2 | 62 | | }; |
| | | 63 | | |
| | | 64 | | |
| | | 65 | | public static void ValidateEncoding(Encoding encoding) |
| | | 66 | | { |
| | 113 | 67 | | string charSet = encoding.WebName; |
| | 113 | 68 | | Encoding[] supportedEncodings = SupportedEncodings; |
| | 226 | 69 | | for (int i = 0; i < supportedEncodings.Length; i++) |
| | | 70 | | { |
| | 113 | 71 | | if (charSet == supportedEncodings[i].WebName) |
| | | 72 | | { |
| | 113 | 73 | | return; |
| | | 74 | | } |
| | | 75 | | } |
| | | 76 | | |
| | 0 | 77 | | throw DiagnosticUtility.ExceptionUtility.ThrowHelperError( |
| | 0 | 78 | | new ArgumentException(SR.Format(SR.MessageTextEncodingNotSupported, charSet), nameof(encoding))); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | public static string EncodingToCharSet(Encoding encoding) |
| | | 82 | | { |
| | 146 | 83 | | string webName = encoding.WebName; |
| | 146 | 84 | | CharSetEncoding[] charSetEncodings = CharSetEncodings; |
| | 298 | 85 | | for (int i = 0; i < charSetEncodings.Length; i++) |
| | | 86 | | { |
| | 149 | 87 | | Encoding enc = charSetEncodings[i]._encoding; |
| | 149 | 88 | | if (enc == null) |
| | | 89 | | { |
| | | 90 | | continue; |
| | | 91 | | } |
| | | 92 | | |
| | 149 | 93 | | if (enc.WebName == webName) |
| | | 94 | | { |
| | 146 | 95 | | return charSetEncodings[i]._charSet; |
| | | 96 | | } |
| | | 97 | | } |
| | 0 | 98 | | return null; |
| | | 99 | | } |
| | | 100 | | |
| | | 101 | | public static bool TryGetEncoding(string charSet, out Encoding encoding) |
| | | 102 | | { |
| | 0 | 103 | | CharSetEncoding[] charSetEncodings = CharSetEncodings; |
| | | 104 | | |
| | | 105 | | // Quick check for exact equality |
| | 0 | 106 | | for (int i = 0; i < charSetEncodings.Length; i++) |
| | | 107 | | { |
| | 0 | 108 | | if (charSetEncodings[i]._charSet == charSet) |
| | | 109 | | { |
| | 0 | 110 | | encoding = charSetEncodings[i]._encoding; |
| | 0 | 111 | | return true; |
| | | 112 | | } |
| | | 113 | | } |
| | | 114 | | |
| | | 115 | | // Check for case insensitive match |
| | 0 | 116 | | for (int i = 0; i < charSetEncodings.Length; i++) |
| | | 117 | | { |
| | 0 | 118 | | string compare = charSetEncodings[i]._charSet; |
| | 0 | 119 | | if (compare == null) |
| | | 120 | | { |
| | | 121 | | continue; |
| | | 122 | | } |
| | | 123 | | |
| | 0 | 124 | | if (compare.Equals(charSet, StringComparison.OrdinalIgnoreCase)) |
| | | 125 | | { |
| | 0 | 126 | | encoding = charSetEncodings[i]._encoding; |
| | 0 | 127 | | return true; |
| | | 128 | | } |
| | | 129 | | } |
| | | 130 | | |
| | 0 | 131 | | encoding = null; |
| | 0 | 132 | | return false; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | internal class CharSetEncoding |
| | | 136 | | { |
| | | 137 | | internal string _charSet; |
| | | 138 | | internal Encoding _encoding; |
| | | 139 | | |
| | 10 | 140 | | internal CharSetEncoding(string charSet, Encoding enc) |
| | | 141 | | { |
| | 10 | 142 | | _charSet = charSet; |
| | 10 | 143 | | _encoding = enc; |
| | 10 | 144 | | } |
| | | 145 | | } |
| | | 146 | | } |
| | | 147 | | } |