KnownHeader

We found 10 examples in language CSharp for this search. You will see 30 fragments of code.

        internal static KnownHeader TryGetKnownHeader(string name)
        {
            KnownHeader candidate = GetCandidate(new StringAccessor(name));
            if (candidate != null && StringComparer.OrdinalIgnoreCase.Equals(name, candidate.Name))
            {
                return candidate;
            }

            return null;
        }

        internal unsafe static KnownHeader TryGetKnownHeader(ReadOnlySpan<byte> name)
        {
            fixed (byte* p = &MemoryMarshal.GetReference(name))
            {
                KnownHeader candidate = GetCandidate(new BytePtrAccessor(p, name.Length));
                if (candidate != null && ByteArrayHelpers.EqualsOrdinalAsciiIgnoreCase(candidate.Name, name))
                {
                    return candidate;
                }
            }

            return null;
        }


        internal static KnownHeader TryGetKnownHeader(string name)
        {
            KnownHeader candidate = GetCandidate(new StringAccessor(name));
            if (candidate != null && StringComparer.OrdinalIgnoreCase.Equals(name, candidate.Name))
            {
                return candidate;
            }

            return null;
        }

        internal unsafe static KnownHeader TryGetKnownHeader(ReadOnlySpan<byte> name)
        {
            fixed (byte* p = &MemoryMarshal.GetReference(name))
            {
                KnownHeader candidate = GetCandidate(new BytePtrAccessor(p, name.Length));
                if (candidate != null && ByteArrayHelpers.EqualsOrdinalAsciiIgnoreCase(candidate.Name, name))
                {
                    return candidate;
                }
            }

            return null;
        }

namespace Titanium.Web.Proxy.Http
{
    /// <summary>
    /// Well known http headers.
    /// </summary>
    public static class KnownHeaders
    {
        // Both
        public static KnownHeader Connection = "Connection";
        public static KnownHeader ConnectionClose = "close";
        public static KnownHeader ConnectionKeepAlive = "keep-alive";

        public static KnownHeader ContentLength = "Content-Length";

        public static KnownHeader ContentType = "Content-Type";
        public static KnownHeader ContentTypeCharset = "charset";
        public static KnownHeader ContentTypeBoundary = "boundary";

        public static KnownHeader Upgrade = "Upgrade";
        public static KnownHeader UpgradeWebsocket = "websocket";

        // Request headers
        public static KnownHeader AcceptEncoding = "Accept-Encoding";

        public static KnownHeader Authorization = "Authorization";

        public static KnownHeader Expect = "Expect";
        public static KnownHeader Expect100Continue = "100-continue";

        public static KnownHeader Host = "Host";

        public static KnownHeader ProxyAuthorization = "Proxy-Authorization";
        public static KnownHeader ProxyAuthorizationBasic = "basic";

        public static KnownHeader ProxyConnection = "Proxy-Connection";
        public static KnownHeader ProxyConnectionClose = "close";

        // Response headers
        public static KnownHeader ContentEncoding = "Content-Encoding";
        public static KnownHeader ContentEncodingDeflate = "deflate";
        public static KnownHeader ContentEncodingGzip = "gzip";
        public static KnownHeader ContentEncodingBrotli = "br";

        public static KnownHeader Location = "Location";

        public static KnownHeader ProxyAuthenticate = "Proxy-Authenticate";

        public static KnownHeader TransferEncoding = "Transfer-Encoding";
        public static KnownHeader TransferEncodingChunked = "chunked";
    }
}


        internal static KnownHeader TryGetKnownHeader(string name)
        {
            KnownHeader candidate = GetCandidate(new StringAccessor(name));
            if (candidate != null && StringComparer.OrdinalIgnoreCase.Equals(name, candidate.Name))
            {
                return candidate;
            }

            return null;
        }

        internal unsafe static KnownHeader TryGetKnownHeader(ReadOnlySpan<byte> name)
        {
            fixed (byte* p = &MemoryMarshal.GetReference(name))
            {
                KnownHeader candidate = GetCandidate(new BytePtrAccessor(p, name.Length));
                if (candidate != null && ByteArrayHelpers.EqualsOrdinalAsciiIgnoreCase(candidate.Name, name))
                {
                    return candidate;
                }
            }

            return null;
        }


        internal static KnownHeader TryGetKnownHeader(string name)
        {
            KnownHeader candidate = GetCandidate(new StringAccessor(name));
            if (candidate != null && StringComparer.OrdinalIgnoreCase.Equals(name, candidate.Name))
            {
                return candidate;
            }

            return null;
        }

        internal unsafe static KnownHeader TryGetKnownHeader(ReadOnlySpan<byte> name)
        {
            fixed (byte* p = &MemoryMarshal.GetReference(name))
            {
                KnownHeader candidate = GetCandidate(new BytePtrAccessor(p, name.Length));
                if (candidate != null && ByteArrayHelpers.EqualsOrdinalAsciiIgnoreCase(candidate.Name, name))
                {
                    return candidate;
                }
            }

            return null;
        }


        internal static KnownHeader TryGetKnownHeader(string name)
        {
            KnownHeader candidate = GetCandidate(new StringAccessor(name));
            if (candidate != null && StringComparer.OrdinalIgnoreCase.Equals(name, candidate.Name))
            {
                return candidate;
            }

            return null;
        }

        internal unsafe static KnownHeader TryGetKnownHeader(ReadOnlySpan<byte> name)
        {
            fixed (byte* p = &MemoryMarshal.GetReference(name))
            {
                KnownHeader candidate = GetCandidate(new BytePtrAccessor(p, name.Length));
                if (candidate != null && ByteArrayHelpers.EqualsOrdinalAsciiIgnoreCase(candidate.Name, name))
                {
                    return candidate;
                }
            }

            return null;
        }


        [Fact]
        public void Add_CallWithNullValue_Throw()
        {
            MockHeaders headers = new MockHeaders(knownHeader, new MockHeaderParser(typeof(Uri)));
            HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers);

            Assert.Throws<ArgumentNullException>(() => { collection.Add(null); });
        }

        [Fact]
        public void Contains_CallWithNullValue_Throw()
        {
            MockHeaders headers = new MockHeaders(knownHeader, new MockHeaderParser(typeof(Uri)));
            HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers);

            Assert.Throws<ArgumentNullException>(() => { collection.Contains(null); });
        }

        [Fact]
        public void CopyTo_EmptyToEmpty_Success()
        {
            MockHeaders headers = new MockHeaders(knownHeader, new MockHeaderParser(typeof(Uri)));
            HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers);

            Uri[] array = new Uri[0];
            collection.CopyTo(array, 0);
        }

        [Fact]
        public void Remove_CallWithNullValue_Throw()
        {
            MockHeaders headers = new MockHeaders();
            HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers);

            Assert.Throws<ArgumentNullException>(() => { collection.Remove(null); });
        }

        [Fact]
        public void GetEnumerator_NoValues_EmptyEnumerator()
        {
            MockHeaders headers = new MockHeaders(knownHeader, new MockHeaderParser(typeof(Uri)));
            HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers);

            IEnumerator<Uri> enumerator = collection.GetEnumerator();

            Assert.False(enumerator.MoveNext(), "No items expected in enumerator.");
        }


        public IHttpHeader AddHeader(string name, string value)
        {
            var knownHeader = Headers.SingleOrDefault(h => string.Equals(h.Name, name, StringComparison.OrdinalIgnoreCase));
            _headers.Remove(knownHeader);

            var newHeader = new UntypedResponseHeader(name, value);
            _headers.Add(newHeader);

            return newHeader;
        }

        /// <summary>
        /// Will update header if a header with the same name already exists.
        /// </summary>
        public void AddHeader(IHttpHeader header)
        {
            var knownHeader = Headers.SingleOrDefault(h => string.Equals(h.Name, header.Name, StringComparison.OrdinalIgnoreCase));
            _headers.Remove(knownHeader);
            _headers.Add(header);
        }

        public void RemoveHeader(string name)
        {
            var knownHeader = Headers.SingleOrDefault(h => string.Equals(h.Name, name, StringComparison.OrdinalIgnoreCase));
            _headers.Remove(knownHeader);
        }


        [Fact]
        public void TryAddWithoutValidation_AddSingleValue_ValueParsed()
        {
            MockHeaders headers = new MockHeaders();
            headers.TryAddWithoutValidation(knownHeader, rawPrefix);

            Assert.Equal(1, headers.Count());
            Assert.Equal(1, headers.First().Value.Count());
            Assert.Equal(parsedPrefix, headers.First().Value.First());

            Assert.Equal(1, headers.Parser.TryParseValueCallCount);
        }

        [Fact]
        public void TryAddWithoutValidation_MultipleAddNullValueCollection_Throws()
        {
            MockHeaders headers = new MockHeaders();
            string[] values = null;
            
            Assert.Throws<ArgumentNullException>(() => { headers.TryAddWithoutValidation(knownHeader, values); });
        }

        [Fact]
        public void Add_SingleAddEmptyValueMultipleTimes_EmptyHeaderAdded()
        {
            MockHeaders headers = new MockHeaders();
            headers.Add(knownHeader, (string)null);
            headers.Add(knownHeader, string.Empty);
            headers.Add(knownHeader, string.Empty);

            // Add() should trigger parsing.
            Assert.Equal(3, headers.Parser.TryParseValueCallCount);

            Assert.Equal(0, headers.Count());
        }

        [Fact]
        public void Add_SingleFirstTryAddWithoutValidationThenAddToSingleValueHeader_AddThrows()
        {
            MockHeaderParser parser = new MockHeaderParser(false); // doesn't support multiple values.
            MockHeaders headers = new MockHeaders(parser);

            headers.TryAddWithoutValidation(knownHeader, rawPrefix + "1");
            Assert.Throws<FormatException>(() => {headers.Add(knownHeader, rawPrefix + "2"); });
        }

        [Fact]
        public void Add_MultipleAddNullValueCollection_Throw()
        {
            MockHeaders headers = new MockHeaders();
            string[] values = null;
            
            Assert.Throws<ArgumentNullException>(() => { headers.Add(knownHeader, values); });
        }

        [Fact]
        public void RemoveParsedValue_RemoveFromNonExistingHeader_ReturnsFalse()
        {
            MockHeaders headers = new MockHeaders();
            headers.Add(knownHeader, rawPrefix);

            // Header 'non-existing-header' can't be found, so false is returned.
            Assert.False(headers.RemoveParsedValue("non-existing-header", "doesntexist"));
        }


        [Fact]
        public void Add_CallWithNullValue_Throw()
        {
            MockHeaders headers = new MockHeaders(knownHeader, new MockHeaderParser(typeof(Uri)));
            HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers);

            Assert.Throws<ArgumentNullException>(() => { collection.Add(null); });
        }

        [Fact]
        public void Contains_CallWithNullValue_Throw()
        {
            MockHeaders headers = new MockHeaders(knownHeader, new MockHeaderParser(typeof(Uri)));
            HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers);

            Assert.Throws<ArgumentNullException>(() => { collection.Contains(null); });
        }

        [Fact]
        public void CopyTo_EmptyToEmpty_Success()
        {
            MockHeaders headers = new MockHeaders(knownHeader, new MockHeaderParser(typeof(Uri)));
            HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers);

            Uri[] array = new Uri[0];
            collection.CopyTo(array, 0);
        }

        [Fact]
        public void Remove_CallWithNullValue_Throw()
        {
            MockHeaders headers = new MockHeaders();
            HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers);

            Assert.Throws<ArgumentNullException>(() => { collection.Remove(null); });
        }

        [Fact]
        public void GetEnumerator_NoValues_EmptyEnumerator()
        {
            MockHeaders headers = new MockHeaders(knownHeader, new MockHeaderParser(typeof(Uri)));
            HttpHeaderValueCollection<Uri> collection = new HttpHeaderValueCollection<Uri>(knownHeader, headers);

            IEnumerator<Uri> enumerator = collection.GetEnumerator();

            Assert.False(enumerator.MoveNext(), "No items expected in enumerator.");
        }

Titanium.Web.Proxy.Http.KnownHeader : Object

Fields :

public String String

Methods :

public String ToString()
public static KnownHeader op_Implicit(String str = )
public Type GetType()
public Boolean Equals(Object obj = )
public Int32 GetHashCode()