Skip to content

Commit 6235734

Browse files
simonbaslesmaldini
authored andcommitted
improve API, separate individual parameter setters + add 2 last
1 parent 22c20ba commit 6235734

File tree

3 files changed

+192
-115
lines changed

3 files changed

+192
-115
lines changed

src/main/java/reactor/ipc/netty/http/server/HttpServer.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,9 @@ public void accept(ChannelPipeline p, ContextHandler<Channel> c) {
257257
p.addLast(NettyPipeline.HttpCodec, new HttpServerCodec(
258258
options.httpCodecMaxInitialLineLength(),
259259
options.httpCodecMaxHeaderSize(),
260-
options.httpCodecMaxChunkSize()));
260+
options.httpCodecMaxChunkSize(),
261+
options.httpCodecValidateHeaders(),
262+
options.httpCodecInitialBufferSize()));
261263

262264
if(options.minCompressionResponseSize() >= 0) {
263265
p.addLast(NettyPipeline.CompressionHandler, new CompressionHandler(options.minCompressionResponseSize()));

src/main/java/reactor/ipc/netty/http/server/HttpServerOptions.java

Lines changed: 101 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,17 @@ public static HttpServerOptions.Builder builder() {
4141
private final int maxInitialLineLength;
4242
private final int maxHeaderSize;
4343
private final int maxChunkSize;
44+
private final int initialBufferSize;
45+
private final boolean validateHeaders;
4446

4547
private HttpServerOptions(HttpServerOptions.Builder builder) {
4648
super(builder);
4749
this.minCompressionResponseSize = builder.minCompressionResponseSize;
4850
this.maxInitialLineLength = builder.maxInitialLineLength;
4951
this.maxHeaderSize = builder.maxHeaderSize;
5052
this.maxChunkSize = builder.maxChunkSize;
53+
this.validateHeaders = builder.validateHeaders;
54+
this.initialBufferSize = builder.initialBufferSize;
5155
}
5256

5357
/**
@@ -90,6 +94,25 @@ public int httpCodecMaxChunkSize() {
9094
return maxChunkSize;
9195
}
9296

97+
/**
98+
* Returns the HTTP validate headers flag.
99+
*
100+
* @return true if the HTTP codec validates headers, false otherwise
101+
* @see io.netty.handler.codec.http.HttpServerCodec
102+
*/
103+
public boolean httpCodecValidateHeaders() {
104+
return validateHeaders;
105+
}
106+
/**
107+
* Returns the configured HTTP codec initial buffer size.
108+
*
109+
* @return the configured HTTP codec initial buffer size
110+
* @see io.netty.handler.codec.http.HttpServerCodec
111+
*/
112+
public int httpCodecInitialBufferSize() {
113+
return initialBufferSize;
114+
}
115+
93116
@Override
94117
public HttpServerOptions duplicate() {
95118
return builder().from(this).build();
@@ -123,10 +146,19 @@ public String toString() {
123146
}
124147

125148
public static final class Builder extends ServerOptions.Builder<Builder> {
149+
150+
public static final int DEFAULT_MAX_INITIAL_LINE_LENGTH = 4096;
151+
public static final int DEFAULT_MAX_HEADER_SIZE = 8192;
152+
public static final int DEFAULT_MAX_CHUNK_SIZE = 8192;
153+
public static final boolean DEFAULT_VALIDATE_HEADERS = true;
154+
public static final int DEFAULT_INITIAL_BUFFER_SIZE = 128;
155+
126156
private int minCompressionResponseSize = -1;
127-
private int maxInitialLineLength = 4096;
128-
private int maxHeaderSize = 8192;
129-
private int maxChunkSize = 8192;
157+
private int maxInitialLineLength = DEFAULT_MAX_INITIAL_LINE_LENGTH;
158+
private int maxHeaderSize = DEFAULT_MAX_HEADER_SIZE;
159+
private int maxChunkSize = DEFAULT_MAX_CHUNK_SIZE;
160+
private boolean validateHeaders = DEFAULT_VALIDATE_HEADERS;
161+
private int initialBufferSize = DEFAULT_INITIAL_BUFFER_SIZE;
130162

131163
private Builder(){
132164
super(new ServerBootstrap());
@@ -162,28 +194,75 @@ public final Builder compression(int minResponseSize) {
162194
}
163195

164196
/**
165-
* Configure the {@link io.netty.handler.codec.http.HttpServerCodec HTTP codec}
166-
* maximum initial HTTP line length, header size and chunk size.
167-
* <p>
168-
* Negative or zero values are not valid, but will be interpreted as "don't change
169-
* the current configuration for that field": on first call the Netty defaults of
170-
* {@code (4096,8192,8192)} will be used.
197+
* Configure the maximum length that can be decoded for the HTTP request's initial
198+
* line. Defaults to {@code #DEFAULT_MAX_INITIAL_LINE_LENGTH}.
171199
*
172-
* @param maxInitialLineLength the HTTP initial line maximum length. Use 0 to ignore/keep default.
173-
* @param maxHeaderSize the HTTP header maximum size. Use 0 to ignore/keep default.
174-
* @param maxChunkSize the HTTP chunk maximum size. Use 0 to ignore/keep default.
175-
* @return {@code this}
200+
* @param value the value for the maximum initial line length (strictly positive)
201+
* @return this option builder for further configuration
176202
*/
177-
public final Builder httpCodecOptions(int maxInitialLineLength, int maxHeaderSize, int maxChunkSize) {
178-
if (maxInitialLineLength > 0) {
179-
this.maxInitialLineLength = maxInitialLineLength;
203+
public final Builder maxInitialLineLength(int value) {
204+
if (value <= 0) {
205+
throw new IllegalArgumentException(
206+
"maxInitialLineLength must be strictly positive");
180207
}
181-
if (maxHeaderSize > 0) {
182-
this.maxHeaderSize = maxHeaderSize;
208+
this.maxInitialLineLength = value;
209+
return get();
210+
}
211+
212+
/**
213+
* Configure the maximum header size that can be decoded for the HTTP request.
214+
* Defaults to {@link #DEFAULT_MAX_HEADER_SIZE}.
215+
*
216+
* @param value the value for the maximum header size (strictly positive)
217+
* @return this option builder for further configuration
218+
*/
219+
public final Builder maxHeaderSize(int value) {
220+
if (value <= 0) {
221+
throw new IllegalArgumentException("maxHeaderSize must be strictly positive");
183222
}
184-
if (maxChunkSize > 0) {
185-
this.maxChunkSize = maxChunkSize;
223+
this.maxHeaderSize = value;
224+
return get();
225+
}
226+
227+
/**
228+
* Configure the maximum chunk size that can be decoded for the HTTP request.
229+
* Defaults to {@link #DEFAULT_MAX_CHUNK_SIZE}.
230+
*
231+
* @param value the value for the maximum chunk size (strictly positive)
232+
* @return this option builder for further configuration
233+
*/
234+
public final Builder maxChunkSize(int value) {
235+
if (value <= 0) {
236+
throw new IllegalArgumentException("maxChunkSize must be strictly positive");
237+
}
238+
this.maxChunkSize = value;
239+
return get();
240+
}
241+
242+
/**
243+
* Configure whether or not to validate headers when decoding requests. Defaults to
244+
* #DEFAULT_VALIDATE_HEADERS.
245+
*
246+
* @param validate true to validate headers, false otherwise
247+
* @return this option builder for further configuration
248+
*/
249+
public final Builder validateHeaders(boolean validate) {
250+
this.validateHeaders = validate;
251+
return get();
252+
}
253+
254+
/**
255+
* Configure the initial buffer size for HTTP request decoding. Defaults to
256+
* {@link #DEFAULT_INITIAL_BUFFER_SIZE}.
257+
*
258+
* @param value the initial buffer size to use (strictly positive)
259+
* @return
260+
*/
261+
public final Builder initialBufferSize(int value) {
262+
if (value <= 0) {
263+
throw new IllegalArgumentException("initialBufferSize must be strictly positive");
186264
}
265+
this.initialBufferSize = value;
187266
return get();
188267
}
189268

@@ -199,6 +278,8 @@ public final Builder from(HttpServerOptions options) {
199278
this.maxInitialLineLength = options.maxInitialLineLength;
200279
this.maxHeaderSize = options.maxHeaderSize;
201280
this.maxChunkSize = options.maxChunkSize;
281+
this.validateHeaders = options.validateHeaders;
282+
this.initialBufferSize = options.initialBufferSize;
202283
return get();
203284
}
204285

0 commit comments

Comments
 (0)