static void AssertContains(string a, string b, bool caseSensitiveEquals, bool caseInsenstaiveEquals)
        {
            var asciiA = new AsciiString(a);
            var asciiB = new AsciiString(b);

            Assert.Equal(caseSensitiveEquals, AsciiString.Contains(asciiA, asciiB));
            Assert.Equal(caseInsenstaiveEquals, AsciiString.ContainsIgnoreCase(asciiA, asciiB));
        }
        protected internal ZlibWrapper?DetermineWrapper(ICharSequence acceptEncoding)
        {
            float starQ    = -1.0f;
            float gzipQ    = -1.0f;
            float deflateQ = -1.0f;

            ICharSequence[] parts = CharUtil.Split(acceptEncoding, ',');
            foreach (ICharSequence encoding in parts)
            {
                float q         = 1.0f;
                int   equalsPos = encoding.IndexOf('=');
                if (equalsPos != -1)
                {
                    try
                    {
                        var ddd = encoding.ToString(equalsPos + 1);
                        q = float.Parse(encoding.ToString(equalsPos + 1));
                    }
                    catch (FormatException)
                    {
                        // Ignore encoding
                        q = 0.0f;
                    }
                }

                if (encoding.Contains('*'))
                {
                    starQ = q;
                }
                else if (AsciiString.Contains(encoding, GZipString) && q > gzipQ)
                {
                    gzipQ = q;
                }
                else if (AsciiString.Contains(encoding, DeflateString) && q > deflateQ)
                {
                    deflateQ = q;
                }
            }
            if (gzipQ > 0.0f || deflateQ > 0.0f)
            {
                return(gzipQ >= deflateQ ? ZlibWrapper.Gzip : ZlibWrapper.Zlib);
            }
            if (starQ > 0.0f)
            {
                // ReSharper disable CompareOfFloatsByEqualityOperator
                if (gzipQ == -1.0f)
                {
                    return(ZlibWrapper.Gzip);
                }
                if (deflateQ == -1.0f)
                {
                    return(ZlibWrapper.Zlib);
                }
                // ReSharper restore CompareOfFloatsByEqualityOperator
            }
            return(null);
        }
Exemple #3
0
        /// <summary>
        /// Returns true if the server switched to a different protocol than HTTP/1.0 or HTTP/1.1, e.g. HTTP/2 or Websocket.
        /// Returns false if the upgrade happened in a different layer, e.g. upgrade from HTTP/1.1 to HTTP/1.1 over TLS.
        /// </summary>
        /// <param name="msg"></param>
        /// <returns></returns>
        protected bool IsSwitchingToNonHttp1Protocol(IHttpResponse msg)
        {
            if (msg.Status.Code != StatusCodes.Status101SwitchingProtocols)
            {
                return(false);
            }

            return(!msg.Headers.TryGet(HttpHeaderNames.Upgrade, out ICharSequence newProtocol) ||
                   !AsciiString.Contains(newProtocol, HttpVersion.Http10String) &&
                   !AsciiString.Contains(newProtocol, HttpVersion.Http11String));
        }