package io.netty.handler.codec.http2;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.util.internal.UnstableApi;
import static io.netty.handler.codec.http2.Http2Error.COMPRESSION_ERROR;
import static io.netty.handler.codec.http2.Http2Exception.connectionError;
import static io.netty.util.internal.ObjectUtil.checkNotNull;
@UnstableApi
public class DefaultHttp2HeadersEncoder implements Http2HeadersEncoder, Http2HeadersEncoder.Configuration {
private final HpackEncoder hpackEncoder;
private final SensitivityDetector sensitivityDetector;
private final ByteBuf tableSizeChangeOutput = Unpooled.buffer();
public DefaultHttp2HeadersEncoder() {
this(NEVER_SENSITIVE);
}
public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector) {
this(sensitivityDetector, new HpackEncoder());
}
public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, boolean ignoreMaxHeaderListSize) {
this(sensitivityDetector, new HpackEncoder(ignoreMaxHeaderListSize));
}
public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, boolean ignoreMaxHeaderListSize,
int dynamicTableArraySizeHint) {
this(sensitivityDetector, new HpackEncoder(ignoreMaxHeaderListSize, dynamicTableArraySizeHint));
}
DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, HpackEncoder hpackEncoder) {
this.sensitivityDetector = checkNotNull(sensitivityDetector, "sensitiveDetector");
this.hpackEncoder = checkNotNull(hpackEncoder, "hpackEncoder");
}
@Override
public void encodeHeaders(int streamId, Http2Headers headers, ByteBuf buffer) throws Http2Exception {
try {
if (tableSizeChangeOutput.isReadable()) {
buffer.writeBytes(tableSizeChangeOutput);
tableSizeChangeOutput.clear();
}
hpackEncoder.encodeHeaders(streamId, buffer, headers, sensitivityDetector);
} catch (Http2Exception e) {
throw e;
} catch (Throwable t) {
throw connectionError(COMPRESSION_ERROR, t, "Failed encoding headers block: %s", t.getMessage());
}
}
@Override
public void maxHeaderTableSize(long max) throws Http2Exception {
hpackEncoder.setMaxHeaderTableSize(tableSizeChangeOutput, max);
}
@Override
public long maxHeaderTableSize() {
return hpackEncoder.getMaxHeaderTableSize();
}
@Override
public void maxHeaderListSize(long max) throws Http2Exception {
hpackEncoder.setMaxHeaderListSize(max);
}
@Override
public long maxHeaderListSize() {
return hpackEncoder.getMaxHeaderListSize();
}
@Override
public Configuration configuration() {
return this;
}
}