Exemple #1
0
        /// <summary>
        /// Encode the header field into the header block.
        /// The given <see cref="ICharSequence"/>s must be immutable!
        /// </summary>
        /// <param name="output"></param>
        /// <param name="name"></param>
        /// <param name="value"></param>
        /// <param name="sensitive"></param>
        /// <param name="headerSize"></param>
        void EncodeHeader(IByteBuffer output, ICharSequence name, ICharSequence value, bool sensitive, long headerSize)
        {
            // If the header value is sensitive then it must never be indexed
            if (sensitive)
            {
                int nameIndex = GetNameIndex(name);
                EncodeLiteral(output, name, value, HpackUtil.IndexType.Never, nameIndex);
                return;
            }

            // If the peer will only use the static table
            if (0ul >= (ulong)_maxHeaderTableSize)
            {
                int staticTableIndex = HpackStaticTable.GetIndexInsensitive(name, value);
                if (staticTableIndex == -1)
                {
                    int nameIndex = HpackStaticTable.GetIndex(name);
                    EncodeLiteral(output, name, value, HpackUtil.IndexType.None, nameIndex);
                }
                else
                {
                    EncodeInteger(output, 0x80, 7, staticTableIndex);
                }

                return;
            }

            // If the headerSize is greater than the max table size then it must be encoded literally
            if (headerSize > _maxHeaderTableSize)
            {
                int nameIndex = GetNameIndex(name);
                EncodeLiteral(output, name, value, HpackUtil.IndexType.None, nameIndex);
                return;
            }

            HeaderEntry headerField = GetEntryInsensitive(name, value);

            if (headerField is object)
            {
                int index = GetIndex(headerField.Index) + HpackStaticTable.Length;
                // Section 6.1. Indexed Header Field Representation
                EncodeInteger(output, 0x80, 7, index);
            }
            else
            {
                int staticTableIndex = HpackStaticTable.GetIndexInsensitive(name, value);
                if (staticTableIndex != -1)
                {
                    // Section 6.1. Indexed Header Field Representation
                    EncodeInteger(output, 0x80, 7, staticTableIndex);
                }
                else
                {
                    EnsureCapacity(headerSize);
                    EncodeLiteral(output, name, value, HpackUtil.IndexType.Incremental, GetNameIndex(name));
                    Add(name, value, headerSize);
                }
            }
        }
Exemple #2
0
        int GetNameIndex(ICharSequence name)
        {
            int index = HpackStaticTable.GetIndex(name);

            if (index == -1)
            {
                index = GetIndex(name);
                if (index >= 0)
                {
                    index += HpackStaticTable.Length;
                }
            }

            return(index);
        }