headerTable = new Http2HeaderTableEncoder();
}
@Override
public void encodeHeaders(Http2Headers headers, ByteBuf buffer) throws Http2Exception {
final OutputStream stream = new ByteBufOutputStream(buffer);
try {
if (headers.size() > headerTable.maxHeaderListSize()) {
throw protocolError("Number of headers (%d) exceeds maxHeaderListSize (%d)",
headers.size(), headerTable.maxHeaderListSize());
}
// If there was a change in the table size, serialize the output from the encoder
// resulting from that change.
if (tableSizeChangeOutput.size() > 0) {
buffer.writeBytes(tableSizeChangeOutput.toByteArray());
tableSizeChangeOutput.reset();
}
// Write pseudo headers first as required by the HTTP/2 spec.
for (Http2Headers.PseudoHeaderName pseudoHeader : Http2Headers.PseudoHeaderName.values()) {
AsciiString name = pseudoHeader.value();
AsciiString value = headers.get(name);
if (value != null) {
encodeHeader(name, value, stream);
}
}
headers.forEachEntry(new EntryVisitor() {
@Override
public boolean visit(Entry<AsciiString, AsciiString> entry) throws Exception {
final AsciiString name = entry.getKey();
final AsciiString value = entry.getValue();
if (!Http2Headers.PseudoHeaderName.isPseudoHeader(name)) {
encodeHeader(name, value, stream);
}
return true;
}
});
} catch (Exception e) {
throw Http2Exception.format(Http2Error.COMPRESSION_ERROR, "Failed encoding headers block: %s",
e.getMessage());
} finally {
try {
stream.close();
} catch (IOException e) {
throw new Http2Exception(Http2Error.INTERNAL_ERROR, e.getMessage(), e);
}
}
}