use of org.eclipse.jetty.http2.hpack.HpackContext.StaticEntry in project jetty.project by eclipse.
the class HpackEncoder method encode.
public void encode(ByteBuffer buffer, HttpField field) {
if (field.getValue() == null)
field = new HttpField(field.getHeader(), field.getName(), "");
int field_size = field.getName().length() + field.getValue().length();
_headerListSize += field_size + 32;
final int p = _debug ? buffer.position() : -1;
String encoding = null;
// Is there an entry for the field?
Entry entry = _context.get(field);
if (entry != null) {
// Known field entry, so encode it as indexed
if (entry.isStatic()) {
buffer.put(((StaticEntry) entry).getEncodedField());
if (_debug)
encoding = "IdxFieldS1";
} else {
int index = _context.index(entry);
buffer.put((byte) 0x80);
NBitInteger.encode(buffer, 7, index);
if (_debug)
encoding = "IdxField" + (entry.isStatic() ? "S" : "") + (1 + NBitInteger.octectsNeeded(7, index));
}
} else {
// Unknown field entry, so we will have to send literally.
final boolean indexed;
// But do we know it's name?
HttpHeader header = field.getHeader();
// Select encoding strategy
if (header == null) {
// Select encoding strategy for unknown header names
Entry name = _context.get(field.getName());
if (field instanceof PreEncodedHttpField) {
int i = buffer.position();
((PreEncodedHttpField) field).putTo(buffer, HttpVersion.HTTP_2);
byte b = buffer.get(i);
indexed = b < 0 || b >= 0x40;
if (_debug)
encoding = indexed ? "PreEncodedIdx" : "PreEncoded";
} else // has the custom header name been seen before?
if (name == null) {
// unknown name and value, so let's index this just in case it is
// the first time we have seen a custom name or a custom field.
// unless the name is changing, this is worthwhile
indexed = true;
encodeName(buffer, (byte) 0x40, 6, field.getName(), null);
encodeValue(buffer, true, field.getValue());
if (_debug)
encoding = "LitHuffNHuffVIdx";
} else {
// known custom name, but unknown value.
// This is probably a custom field with changing value, so don't index.
indexed = false;
encodeName(buffer, (byte) 0x00, 4, field.getName(), null);
encodeValue(buffer, true, field.getValue());
if (_debug)
encoding = "LitHuffNHuffV!Idx";
}
} else {
// Select encoding strategy for known header names
Entry name = _context.get(header);
if (field instanceof PreEncodedHttpField) {
// Preencoded field
int i = buffer.position();
((PreEncodedHttpField) field).putTo(buffer, HttpVersion.HTTP_2);
byte b = buffer.get(i);
indexed = b < 0 || b >= 0x40;
if (_debug)
encoding = indexed ? "PreEncodedIdx" : "PreEncoded";
} else if (__DO_NOT_INDEX.contains(header)) {
// Non indexed field
indexed = false;
boolean never_index = __NEVER_INDEX.contains(header);
boolean huffman = !__DO_NOT_HUFFMAN.contains(header);
encodeName(buffer, never_index ? (byte) 0x10 : (byte) 0x00, 4, header.asString(), name);
encodeValue(buffer, huffman, field.getValue());
if (_debug)
encoding = "Lit" + ((name == null) ? "HuffN" : ("IdxN" + (name.isStatic() ? "S" : "") + (1 + NBitInteger.octectsNeeded(4, _context.index(name))))) + (huffman ? "HuffV" : "LitV") + (indexed ? "Idx" : (never_index ? "!!Idx" : "!Idx"));
} else if (field_size >= _context.getMaxDynamicTableSize() || header == HttpHeader.CONTENT_LENGTH && field.getValue().length() > 2) {
// Non indexed if field too large or a content length for 3 digits or more
indexed = false;
encodeName(buffer, (byte) 0x00, 4, header.asString(), name);
encodeValue(buffer, true, field.getValue());
if (_debug)
encoding = "LitIdxNS" + (1 + NBitInteger.octectsNeeded(4, _context.index(name))) + "HuffV!Idx";
} else {
// indexed
indexed = true;
boolean huffman = !__DO_NOT_HUFFMAN.contains(header);
encodeName(buffer, (byte) 0x40, 6, header.asString(), name);
encodeValue(buffer, huffman, field.getValue());
if (_debug)
encoding = ((name == null) ? "LitHuffN" : ("LitIdxN" + (name.isStatic() ? "S" : "") + (1 + NBitInteger.octectsNeeded(6, _context.index(name))))) + (huffman ? "HuffVIdx" : "LitVIdx");
}
}
// table and reference set.
if (indexed)
if (_context.add(field) == null)
throw new IllegalStateException();
}
if (_debug) {
int e = buffer.position();
if (LOG.isDebugEnabled())
LOG.debug("encode {}:'{}' to '{}'", encoding, field, TypeUtil.toHexString(buffer.array(), buffer.arrayOffset() + p, e - p));
}
}
Aggregations