Consolidate HttpObjectDecoder default values into constants (#10344)

Motivation

HttpObjectDecoder and its associated classes make frequent use of
default values for maxInitialLineLength, maxHeaderSize, maxChunkSize,
etc. Today, these defaults are defined in-line in constructors and
duplicated across many classes. This repetition is more prone to error
and inconsistencies.

Furthermore, due to the current lack of builder support, if a user wants
to change just one of these values (e.g., maxHeaderSize), they are also
required to know and repeat the other default values (e.g.,
maxInitialLineLength and maxChunkSize).

The primary motivation for this change is as we are considering adding
another constructor parameter (for multiple content length behavior),
appending this parameter may require some users to have prior knowledge
of the default initialBufferSize, and it would be cleaner to allow them
to reference the default constant.

Modifications

* Consolidate the HttpObjectDecoder default values into public constants
* Reference these constants where possible

Result

No functional change. Additional telescoping constructors will be easier
and safer to write. Users may have an easier experience changing single
parameters.
This commit is contained in:
Bennett Lynch 2020-06-11 22:39:10 -07:00 committed by Norman Maurer
parent 121daab927
commit f945cfbd66
6 changed files with 23 additions and 21 deletions

View File

@ -28,6 +28,9 @@ import java.util.List;
import java.util.Queue;
import java.util.concurrent.atomic.AtomicLong;
import static io.netty.handler.codec.http.HttpObjectDecoder.DEFAULT_MAX_HEADER_SIZE;
import static io.netty.handler.codec.http.HttpObjectDecoder.DEFAULT_MAX_INITIAL_LINE_LENGTH;
/**
* A combination of {@link HttpRequestEncoder} and {@link HttpResponseDecoder}
* which enables easier client side HTTP implementation. {@link HttpClientCodec}
@ -61,7 +64,7 @@ public final class HttpClientCodec extends CombinedChannelDuplexHandler<HttpResp
* {@code maxChunkSize (8192)}).
*/
public HttpClientCodec() {
this(4096, 8192, false);
this(DEFAULT_MAX_INITIAL_LINE_LENGTH, DEFAULT_MAX_HEADER_SIZE, false);
}
/**

View File

@ -102,6 +102,12 @@ import java.util.List;
* implement all abstract methods properly.
*/
public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
public static final int DEFAULT_MAX_INITIAL_LINE_LENGTH = 4096;
public static final int DEFAULT_MAX_HEADER_SIZE = 8192;
public static final boolean DEFAULT_CHUNKED_SUPPORTED = true;
public static final boolean DEFAULT_VALIDATE_HEADERS = true;
public static final int DEFAULT_INITIAL_BUFFER_SIZE = 128;
private static final String EMPTY_VALUE = "";
private final boolean chunkedSupported;
@ -146,7 +152,7 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
* {@code maxChunkSize (8192)}.
*/
protected HttpObjectDecoder() {
this(4096, 8192, true);
this(DEFAULT_MAX_INITIAL_LINE_LENGTH, DEFAULT_MAX_HEADER_SIZE, DEFAULT_CHUNKED_SUPPORTED);
}
/**
@ -154,7 +160,7 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
*/
protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize, boolean chunkedSupported) {
this(maxInitialLineLength, maxHeaderSize, chunkedSupported, true);
this(maxInitialLineLength, maxHeaderSize, chunkedSupported, DEFAULT_VALIDATE_HEADERS);
}
/**
@ -163,7 +169,7 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize,
boolean chunkedSupported, boolean validateHeaders) {
this(maxInitialLineLength, maxHeaderSize, chunkedSupported, validateHeaders, 128);
this(maxInitialLineLength, maxHeaderSize, chunkedSupported, validateHeaders, DEFAULT_INITIAL_BUFFER_SIZE);
}
protected HttpObjectDecoder(

View File

@ -67,18 +67,18 @@ public class HttpRequestDecoder extends HttpObjectDecoder {
*/
public HttpRequestDecoder(
int maxInitialLineLength, int maxHeaderSize) {
super(maxInitialLineLength, maxHeaderSize, true);
super(maxInitialLineLength, maxHeaderSize, DEFAULT_CHUNKED_SUPPORTED);
}
public HttpRequestDecoder(
int maxInitialLineLength, int maxHeaderSize, boolean validateHeaders) {
super(maxInitialLineLength, maxHeaderSize, true, validateHeaders);
super(maxInitialLineLength, maxHeaderSize, DEFAULT_CHUNKED_SUPPORTED, validateHeaders);
}
public HttpRequestDecoder(
int maxInitialLineLength, int maxHeaderSize, boolean validateHeaders,
int initialBufferSize) {
super(maxInitialLineLength, maxHeaderSize, true, validateHeaders, initialBufferSize);
super(maxInitialLineLength, maxHeaderSize, DEFAULT_CHUNKED_SUPPORTED, validateHeaders, initialBufferSize);
}
@Override

View File

@ -98,18 +98,18 @@ public class HttpResponseDecoder extends HttpObjectDecoder {
*/
public HttpResponseDecoder(
int maxInitialLineLength, int maxHeaderSize) {
super(maxInitialLineLength, maxHeaderSize, true);
super(maxInitialLineLength, maxHeaderSize, DEFAULT_CHUNKED_SUPPORTED);
}
public HttpResponseDecoder(
int maxInitialLineLength, int maxHeaderSize, boolean validateHeaders) {
super(maxInitialLineLength, maxHeaderSize, true, validateHeaders);
super(maxInitialLineLength, maxHeaderSize, DEFAULT_CHUNKED_SUPPORTED, validateHeaders);
}
public HttpResponseDecoder(
int maxInitialLineLength, int maxHeaderSize, boolean validateHeaders,
int initialBufferSize) {
super(maxInitialLineLength, maxHeaderSize, true, validateHeaders, initialBufferSize);
super(maxInitialLineLength, maxHeaderSize, DEFAULT_CHUNKED_SUPPORTED, validateHeaders, initialBufferSize);
}
@Override

View File

@ -22,6 +22,9 @@ import io.netty.channel.CombinedChannelDuplexHandler;
import java.util.ArrayDeque;
import java.util.Queue;
import static io.netty.handler.codec.http.HttpObjectDecoder.DEFAULT_MAX_HEADER_SIZE;
import static io.netty.handler.codec.http.HttpObjectDecoder.DEFAULT_MAX_INITIAL_LINE_LENGTH;
/**
* A combination of {@link HttpRequestDecoder} and {@link HttpResponseEncoder}
* which enables easier server side HTTP implementation.
@ -40,7 +43,7 @@ public final class HttpServerCodec extends CombinedChannelDuplexHandler<HttpRequ
* {@code maxChunkSize (8192)}).
*/
public HttpServerCodec() {
this(4096, 8192);
this(DEFAULT_MAX_INITIAL_LINE_LENGTH, DEFAULT_MAX_HEADER_SIZE);
}
/**

View File

@ -71,16 +71,6 @@ public class RtspDecoder extends HttpObjectDecoder {
*/
private static final Pattern versionPattern = Pattern.compile("RTSP/\\d\\.\\d");
/**
* Constant for default max initial line length.
*/
public static final int DEFAULT_MAX_INITIAL_LINE_LENGTH = 4096;
/**
* Constant for default max header size.
*/
public static final int DEFAULT_MAX_HEADER_SIZE = 8192;
/**
* Constant for default max content length.
*/