1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28 package org.apache.hc.core5.http2.config;
29
30 import org.apache.hc.core5.annotation.Contract;
31 import org.apache.hc.core5.annotation.ThreadingBehavior;
32 import org.apache.hc.core5.http2.frame.FrameConsts;
33 import org.apache.hc.core5.util.Args;
34
35
36
37
38
39
40 @Contract(threading = ThreadingBehavior.IMMUTABLE)
41 public class H2Config {
42
43 public static final H2Config DEFAULT = custom().build();
44 public static final H2Config INIT = initial().build();
45
46 private final int headerTableSize;
47 private final boolean pushEnabled;
48 private final int maxConcurrentStreams;
49 private final int initialWindowSize;
50 private final int maxFrameSize;
51 private final int maxHeaderListSize;
52 private final boolean compressionEnabled;
53
54 H2Config(final int headerTableSize, final boolean pushEnabled, final int maxConcurrentStreams,
55 final int initialWindowSize, final int maxFrameSize, final int maxHeaderListSize,
56 final boolean compressionEnabled) {
57 super();
58 this.headerTableSize = headerTableSize;
59 this.pushEnabled = pushEnabled;
60 this.maxConcurrentStreams = maxConcurrentStreams;
61 this.initialWindowSize = initialWindowSize;
62 this.maxFrameSize = maxFrameSize;
63 this.maxHeaderListSize = maxHeaderListSize;
64 this.compressionEnabled = compressionEnabled;
65 }
66
67 public int getHeaderTableSize() {
68 return headerTableSize;
69 }
70
71 public boolean isPushEnabled() {
72 return pushEnabled;
73 }
74
75 public int getMaxConcurrentStreams() {
76 return maxConcurrentStreams;
77 }
78
79 public int getInitialWindowSize() {
80 return initialWindowSize;
81 }
82
83 public int getMaxFrameSize() {
84 return maxFrameSize;
85 }
86
87 public int getMaxHeaderListSize() {
88 return maxHeaderListSize;
89 }
90
91 public boolean isCompressionEnabled() {
92 return compressionEnabled;
93 }
94
95 @Override
96 public String toString() {
97 final StringBuilder builder = new StringBuilder();
98 builder.append("[headerTableSize=").append(this.headerTableSize)
99 .append(", pushEnabled=").append(this.pushEnabled)
100 .append(", maxConcurrentStreams=").append(this.maxConcurrentStreams)
101 .append(", initialWindowSize=").append(this.initialWindowSize)
102 .append(", maxFrameSize=").append(this.maxFrameSize)
103 .append(", maxHeaderListSize=").append(this.maxHeaderListSize)
104 .append(", compressionEnabled=").append(this.compressionEnabled)
105 .append("]");
106 return builder.toString();
107 }
108
109 public static H2Config.Builder custom() {
110 return new Builder();
111 }
112
113 private static final int INIT_HEADER_TABLE_SIZE = 4096;
114 private static final boolean INIT_ENABLE_PUSH = true;
115 private static final int INIT_MAX_FRAME_SIZE = FrameConsts.MIN_FRAME_SIZE;
116 private static final int INIT_WINDOW_SIZE = 65535;
117 private static final int INIT_CONCURRENT_STREAM = 250;
118
119 public static H2Config.Builder initial() {
120 return new Builder()
121 .setHeaderTableSize(INIT_HEADER_TABLE_SIZE)
122 .setPushEnabled(INIT_ENABLE_PUSH)
123 .setMaxConcurrentStreams(Integer.MAX_VALUE)
124 .setMaxFrameSize(INIT_MAX_FRAME_SIZE)
125 .setInitialWindowSize(INIT_WINDOW_SIZE)
126 .setMaxHeaderListSize(Integer.MAX_VALUE);
127 }
128
129 public static H2Config.Builder copy(final H2Config config) {
130 Args.notNull(config, "Connection config");
131 return new Builder()
132 .setHeaderTableSize(config.getHeaderTableSize())
133 .setPushEnabled(config.isPushEnabled())
134 .setMaxConcurrentStreams(config.getMaxConcurrentStreams())
135 .setInitialWindowSize(config.getInitialWindowSize())
136 .setMaxFrameSize(config.getMaxFrameSize())
137 .setMaxHeaderListSize(config.getMaxHeaderListSize())
138 .setCompressionEnabled(config.isCompressionEnabled());
139 }
140
141 public static class Builder {
142
143 private int headerTableSize;
144 private boolean pushEnabled;
145 private int maxConcurrentStreams;
146 private int initialWindowSize;
147 private int maxFrameSize;
148 private int maxHeaderListSize;
149 private boolean compressionEnabled;
150
151 Builder() {
152 this.headerTableSize = INIT_HEADER_TABLE_SIZE * 2;
153 this.pushEnabled = INIT_ENABLE_PUSH;
154 this.maxConcurrentStreams = INIT_CONCURRENT_STREAM;
155 this.initialWindowSize = INIT_WINDOW_SIZE;
156 this.maxFrameSize = FrameConsts.MIN_FRAME_SIZE * 4;
157 this.maxHeaderListSize = FrameConsts.MAX_FRAME_SIZE;
158 this.compressionEnabled = true;
159 }
160
161 public Builder setHeaderTableSize(final int headerTableSize) {
162 Args.notNegative(headerTableSize, "Header table size");
163 this.headerTableSize = headerTableSize;
164 return this;
165 }
166
167 public Builder setPushEnabled(final boolean pushEnabled) {
168 this.pushEnabled = pushEnabled;
169 return this;
170 }
171
172 public Builder setMaxConcurrentStreams(final int maxConcurrentStreams) {
173 Args.positive(maxConcurrentStreams, "Max concurrent streams");
174 this.maxConcurrentStreams = maxConcurrentStreams;
175 return this;
176 }
177
178 public Builder setInitialWindowSize(final int initialWindowSize) {
179 Args.positive(initialWindowSize, "Initial window size");
180 this.initialWindowSize = initialWindowSize;
181 return this;
182 }
183
184 public Builder setMaxFrameSize(final int maxFrameSize) {
185 this.maxFrameSize = Args.checkRange(maxFrameSize, FrameConsts.MIN_FRAME_SIZE, FrameConsts.MAX_FRAME_SIZE,
186 "Invalid max frame size");
187 return this;
188 }
189
190 public Builder setMaxHeaderListSize(final int maxHeaderListSize) {
191 Args.positive(maxHeaderListSize, "Max header list size");
192 this.maxHeaderListSize = maxHeaderListSize;
193 return this;
194 }
195
196 public Builder setCompressionEnabled(final boolean compressionEnabled) {
197 this.compressionEnabled = compressionEnabled;
198 return this;
199 }
200
201 public H2Config build() {
202 return new H2Config(
203 headerTableSize,
204 pushEnabled,
205 maxConcurrentStreams,
206 initialWindowSize,
207 maxFrameSize,
208 maxHeaderListSize,
209 compressionEnabled);
210 }
211
212 }
213
214 }