Ejemplo n.º 1
0
        private bool EncodeDynamicHeader(Span <byte> buffer, int staticTableIndex, string name, string value,
                                         int headerLength, Encoding?valueEncoding, out int bytesWritten)
        {
            EncoderHeaderEntry?headerField = GetEntry(name, value);

            if (headerField != null)
            {
                // Already exists in dynamic table. Write index.
                int index = CalculateDynamicTableIndex(headerField.Index);
                return(HPackEncoder.EncodeIndexedHeaderField(index, buffer, out bytesWritten));
            }
            else
            {
                // Doesn't exist in dynamic table. Add new entry to dynamic table.

                int  index   = ResolveDynamicTableIndex(staticTableIndex, name);
                bool success = index == -1
                    ? HPackEncoder.EncodeLiteralHeaderFieldIndexingNewName(name, value, valueEncoding, buffer, out bytesWritten)
                    : HPackEncoder.EncodeLiteralHeaderFieldIndexing(index, value, valueEncoding, buffer, out bytesWritten);

                if (success)
                {
                    uint headerSize = (uint)headerLength;
                    EnsureCapacity(headerSize);
                    AddHeaderEntry(name, value, headerSize);
                }

                return(success);
            }
        }
Ejemplo n.º 2
0
        public bool EncodeHeader(Span <byte> buffer, int staticTableIndex, HeaderEncodingHint encodingHint, string name, string value, out int bytesWritten)
        {
            Debug.Assert(!_pendingTableSizeUpdate, "Dynamic table size update should be encoded before headers.");

            // Never index sensitive value.
            if (encodingHint == HeaderEncodingHint.NeverIndex)
            {
                int index = ResolveDynamicTableIndex(staticTableIndex, name);

                return(index == -1
                    ? HPackEncoder.EncodeLiteralHeaderFieldNeverIndexingNewName(name, value, buffer, out bytesWritten)
                    : HPackEncoder.EncodeLiteralHeaderFieldNeverIndexing(index, value, buffer, out bytesWritten));
            }

            // No dynamic table. Only use the static table.
            if (!_allowDynamicCompression || _maxHeaderTableSize == 0 || encodingHint == HeaderEncodingHint.IgnoreIndex)
            {
                return(staticTableIndex == -1
                    ? HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexingNewName(name, value, buffer, out bytesWritten)
                    : HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexing(staticTableIndex, value, buffer, out bytesWritten));
            }

            // Header is greater than the maximum table size.
            // Don't attempt to add dynamic header as all existing dynamic headers will be removed.
            if (HeaderField.GetLength(name.Length, value.Length) > _maxHeaderTableSize)
            {
                int index = ResolveDynamicTableIndex(staticTableIndex, name);

                return(index == -1
                    ? HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexingNewName(name, value, buffer, out bytesWritten)
                    : HPackEncoder.EncodeLiteralHeaderFieldWithoutIndexing(index, value, buffer, out bytesWritten));
            }

            return(EncodeDynamicHeader(buffer, staticTableIndex, name, value, out bytesWritten));
        }
Ejemplo n.º 3
0
        public bool EnsureDynamicTableSizeUpdate(Span <byte> buffer, out int length)
        {
            // Check if there is a table size update that should be encoded
            if (_pendingTableSizeUpdate)
            {
                bool success = HPackEncoder.EncodeDynamicTableSizeUpdate((int)_maxHeaderTableSize, buffer, out length);
                _pendingTableSizeUpdate = false;
                return(success);
            }

            length = 0;
            return(true);
        }