private int getNameIndex(string name) { int index = StaticTable.GetIndex(name); if (index == -1) { index = getIndex(name); if (index >= 0) { index += StaticTable.Length; } } return(index); }
private HttpHeader GetHeaderField(int index) { if (index <= StaticTable.Length) { var headerField = StaticTable.Get(index); return(headerField); } else if (index - StaticTable.Length <= dynamicTable.Length()) { var headerField = dynamicTable.GetEntry(index - StaticTable.Length); return(headerField); } else { throw new IOException("illegal index value (" + index + ")"); } }
/// <summary> /// Encode the header field into the header block. /// </summary> /// <param name="output">Output.</param> /// <param name="name">Name.</param> /// <param name="value">Value.</param> /// <param name="sensitive">If set to <c>true</c> sensitive.</param> /// <param name="indexType">Index type.</param> /// <param name="useStaticName">Use static name.</param> public void EncodeHeader(BinaryWriter output, string name, string value, bool sensitive = false, HpackUtil.IndexType indexType = HpackUtil.IndexType.Incremental, bool useStaticName = true) { // 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 (MaxHeaderTableSize == 0) { int staticTableIndex = StaticTable.GetIndex(name, value); if (staticTableIndex == -1) { int nameIndex = StaticTable.GetIndex(name); encodeLiteral(output, name, value, HpackUtil.IndexType.None, nameIndex); } else { encodeInteger(output, 0x80, 7, staticTableIndex); } return; } int headerSize = HttpHeader.SizeOf(name, value); // 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; } var headerField = getEntry(name, value); if (headerField != null) { int index = getIndex(headerField.Index) + StaticTable.Length; // Section 6.1. Indexed Header Field Representation encodeInteger(output, 0x80, 7, index); } else { int staticTableIndex = StaticTable.GetIndex(name, value); if (staticTableIndex != -1) { // Section 6.1. Indexed Header Field Representation encodeInteger(output, 0x80, 7, staticTableIndex); } else { int nameIndex = useStaticName ? getNameIndex(name) : -1; ensureCapacity(headerSize); encodeLiteral(output, name, value, indexType, nameIndex); add(name, value); } } }