Ejemplo n.º 1
0
        public override bool Equals(Object value)
        {
            DecoderExceptionFallback that = value as DecoderExceptionFallback;

            if (that != null)
            {
                return(true);
            }
            return(false);
        }
        /// <summary>Gets the best encoding available for the specified charset request.</summary>
        /// <param name="acceptCharset">
        /// The Accept-Charset header value (eg: "iso-8859-5, unicode-1-1;q=0.8").
        /// </param>
        /// <returns>An Encoding object appropriate to the specifed charset request.</returns>
        internal static Encoding EncodingFromAcceptCharset(string acceptCharset)
        {
            // Determines the appropriate encoding mapping according to
            // RFC 2616.14.2 (http://tools.ietf.org/html/rfc2616#section-14.2).
            Encoding result = null;
            if (!string.IsNullOrEmpty(acceptCharset))
            {
                // PERF: keep a cache of original strings to resolved Encoding.
                List<CharsetPart> parts = new List<CharsetPart>(AcceptCharsetParts(acceptCharset));
                parts.Sort(delegate(CharsetPart x, CharsetPart y)
                {
                    return y.Quality - x.Quality;
                });

                var encoderFallback = new EncoderExceptionFallback();
                var decoderFallback = new DecoderExceptionFallback();
                foreach (CharsetPart part in parts)
                {
                    if (part.Quality > 0)
                    {
                        // When UTF-8 is specified, select the version that doesn't use the BOM.
                        if (String.Compare("utf-8", part.Charset, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            result = FallbackEncoding;
                            break;
                        }
                        else
                        {
                            try
                            {
                                result = Encoding.GetEncoding(part.Charset, encoderFallback, decoderFallback);
                                break;
                            }
                            catch (ArgumentException)
                            {
                                // This exception is thrown when the character
                                // set isn't supported - it is ignored so
                                // other possible charsets are evaluated.
                            }
                        }
                    }
                }
            }

            // No Charset was specifed, or if charsets were specified, no valid charset was found.
            // Returning a different charset is also valid.
            if (result == null)
            {
                result = FallbackEncoding;
            }

            return result;
        }
Ejemplo n.º 3
0
        internal static Encoding EncodingFromAcceptCharset(string acceptCharset)
        {
            Encoding result = null;
            if (!string.IsNullOrEmpty(acceptCharset))
            {
                List<CharsetPart> parts = new List<CharsetPart>(AcceptCharsetParts(acceptCharset));
                parts.Sort(delegate(CharsetPart x, CharsetPart y)
                {
                    return y.Quality - x.Quality;
                });

                var encoderFallback = new EncoderExceptionFallback();
                var decoderFallback = new DecoderExceptionFallback();
                foreach (CharsetPart part in parts)
                {
                    if (part.Quality > 0)
                    {
                        if (String.Compare("utf-8", part.Charset, StringComparison.OrdinalIgnoreCase) == 0)
                        {
                            result = FallbackEncoding;
                            break;
                        }
                        else
                        {
                            try
                            {
                                result = Encoding.GetEncoding(part.Charset, encoderFallback, decoderFallback);
                                break;
                            }
                            catch (ArgumentException)
                            {
                            }
                        }
                    }
                }
            }

            if (result == null)
            {
                result = FallbackEncoding;
            }

            return result;
        }
Ejemplo n.º 4
0
 internal static Encoding EncodingFromAcceptCharset(string acceptCharset)
 {
     Encoding fallbackEncoding = null;
     if (!string.IsNullOrEmpty(acceptCharset))
     {
         List<CharsetPart> list = new List<CharsetPart>(AcceptCharsetParts(acceptCharset));
         list.Sort((Comparison<CharsetPart>) ((x, y) => (y.Quality - x.Quality)));
         EncoderExceptionFallback encoderFallback = new EncoderExceptionFallback();
         DecoderExceptionFallback decoderFallback = new DecoderExceptionFallback();
         foreach (CharsetPart part in list)
         {
             if (part.Quality > 0)
             {
                 if (string.Compare("UTF-8", part.Charset, StringComparison.OrdinalIgnoreCase) == 0)
                 {
                     fallbackEncoding = FallbackEncoding;
                     break;
                 }
                 try
                 {
                     fallbackEncoding = Encoding.GetEncoding(part.Charset, encoderFallback, decoderFallback);
                     break;
                 }
                 catch (ArgumentException)
                 {
                 }
             }
         }
     }
     return (fallbackEncoding ?? FallbackEncoding);
 }