Пример #1
0
        private static void WriteKeyName(WritableBuffer buffer, Header header, ref HeaderTable headerTable, byte preamble, int n)
        {
            var nameIndex = headerTable.GetKey(header.Name);

            WriteUInt32(buffer, nameIndex, preamble, n);
            if (nameIndex == 0)
            {
                WriteNameString(buffer, header.Name, header.Options);
            }
        }
Пример #2
0
        public static void WriteHeader(WritableBuffer buffer, Header header, ref HeaderTable headerTable, MemoryPool memoryPool)
        {
            switch (header.Options & HeaderOptions.IndexMask)
            {
            case HeaderOptions.IndexAddNewValue:
                WriteKeyName(buffer, header, ref headerTable, 0x40, 6);
                WriteValueString(buffer, header.Value, header.Options);
                headerTable = headerTable.Add(header, memoryPool);
                break;

            case HeaderOptions.IndexExistingValue:
                var index = headerTable.GetKey(header.Name, header.Value);
                if (index == 0)
                {
                    throw new InvalidOperationException("Attempted to use a header that did not exist: " + header.ToString() + Environment.NewLine + headerTable.ToString());
                }
                WriteUInt32(buffer, index, 0x80, 7);
                break;

            case HeaderOptions.IndexNeverIndexed:
                WriteKeyName(buffer, header, ref headerTable, 0x10, 4);
                WriteValueString(buffer, header.Value, header.Options);
                break;

            case HeaderOptions.IndexNotIndexed:
                WriteKeyName(buffer, header, ref headerTable, 0x00, 4);
                WriteValueString(buffer, header.Value, header.Options);
                break;

            case HeaderOptions.IndexResize:
                uint len;
                if (!uint.TryParse(header.Value, out len))
                {
                    throw new InvalidOperationException("Invalid index length change: " + header.Value);
                }
                WriteUInt32(buffer, len, 0x20, 5);
                break;

            case HeaderOptions.IndexAutomatic:
                index = headerTable.GetKey(header.Name, header.Value);
                if (index == 0)
                {
                    index = headerTable.GetKey(header.Name);
                    switch (index)
                    {
                    case 1:         // :authority
                    case 15:        // accept-charset
                    case 16:        // accept-encoding
                    case 17:        // accept-language
                    case 38:        // host
                    case 58:        // user-agent
                        // AddNewValue
                        WriteUInt32(buffer, index, 0x40, 6);
                        headerTable = headerTable.Add(header, memoryPool);
                        break;

                    default:
                        // NotIndexed
                        WriteUInt32(buffer, index, 0x00, 4);
                        if (index == 0)
                        {
                            WriteNameString(buffer, header.Name, header.Options);
                        }
                        break;
                    }
                    WriteValueString(buffer, header.Value, header.Options);
                }
                else
                {
                    // ExistingValue
                    WriteUInt32(buffer, index, 0x80, 7);
                }
                break;

            default:
                throw new InvalidOperationException("Invalid header indexing: " + header.Options);
            }
        }