1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package io.netty5.handler.codec.http2;
17
18 import io.netty5.buffer.api.Buffer;
19 import io.netty5.buffer.api.BufferAllocator;
20 import io.netty5.util.internal.UnstableApi;
21
22 import static io.netty5.handler.codec.http2.Http2Error.COMPRESSION_ERROR;
23 import static io.netty5.handler.codec.http2.Http2Exception.connectionError;
24 import static java.util.Objects.requireNonNull;
25
26 @UnstableApi
27 public class DefaultHttp2HeadersEncoder implements Http2HeadersEncoder, Http2HeadersEncoder.Configuration {
28 private final HpackEncoder hpackEncoder;
29 private final SensitivityDetector sensitivityDetector;
30 private final Buffer tableSizeChangeOutput = BufferAllocator.onHeapUnpooled().allocate(256);
31
32 public DefaultHttp2HeadersEncoder() {
33 this(NEVER_SENSITIVE);
34 }
35
36 public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector) {
37 this(sensitivityDetector, new HpackEncoder());
38 }
39
40 public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, boolean ignoreMaxHeaderListSize) {
41 this(sensitivityDetector, new HpackEncoder(ignoreMaxHeaderListSize));
42 }
43
44 public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, boolean ignoreMaxHeaderListSize,
45 int dynamicTableArraySizeHint) {
46 this(sensitivityDetector, ignoreMaxHeaderListSize, dynamicTableArraySizeHint, HpackEncoder.HUFF_CODE_THRESHOLD);
47 }
48
49 public DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, boolean ignoreMaxHeaderListSize,
50 int dynamicTableArraySizeHint, int huffCodeThreshold) {
51 this(sensitivityDetector,
52 new HpackEncoder(ignoreMaxHeaderListSize, dynamicTableArraySizeHint, huffCodeThreshold));
53 }
54
55
56
57
58
59 DefaultHttp2HeadersEncoder(SensitivityDetector sensitivityDetector, HpackEncoder hpackEncoder) {
60 this.sensitivityDetector = requireNonNull(sensitivityDetector, "sensitiveDetector");
61 this.hpackEncoder = requireNonNull(hpackEncoder, "hpackEncoder");
62 }
63
64 @Override
65 public void encodeHeaders(int streamId, Http2Headers headers, Buffer buffer) throws Http2Exception {
66 try {
67
68
69 if (tableSizeChangeOutput.readableBytes() > 0) {
70 buffer.writeBytes(tableSizeChangeOutput);
71 tableSizeChangeOutput.resetOffsets();
72 }
73
74 hpackEncoder.encodeHeaders(streamId, buffer, headers, sensitivityDetector);
75 } catch (Http2Exception e) {
76 throw e;
77 } catch (Throwable t) {
78 throw connectionError(COMPRESSION_ERROR, t, "Failed encoding headers block: %s", t.getMessage());
79 }
80 }
81
82 @Override
83 public void maxHeaderTableSize(long max) throws Http2Exception {
84 hpackEncoder.setMaxHeaderTableSize(tableSizeChangeOutput, max);
85 }
86
87 @Override
88 public long maxHeaderTableSize() {
89 return hpackEncoder.getMaxHeaderTableSize();
90 }
91
92 @Override
93 public void maxHeaderListSize(long max) throws Http2Exception {
94 hpackEncoder.setMaxHeaderListSize(max);
95 }
96
97 @Override
98 public long maxHeaderListSize() {
99 return hpackEncoder.getMaxHeaderListSize();
100 }
101
102 @Override
103 public Configuration configuration() {
104 return this;
105 }
106 }