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 GitHub
parent b559711f3e
commit bcb62be62b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 33 additions and 22 deletions

View File

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

View File

@ -102,6 +102,13 @@ import java.util.List;
* implement all abstract methods properly. * implement all abstract methods properly.
*/ */
public abstract class HttpObjectDecoder extends ByteToMessageDecoder { 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 int DEFAULT_MAX_CHUNK_SIZE = 8192;
public static final boolean DEFAULT_VALIDATE_HEADERS = true;
public static final int DEFAULT_INITIAL_BUFFER_SIZE = 128;
private static final String EMPTY_VALUE = ""; private static final String EMPTY_VALUE = "";
private final int maxChunkSize; private final int maxChunkSize;
@ -147,7 +154,8 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
* {@code maxChunkSize (8192)}. * {@code maxChunkSize (8192)}.
*/ */
protected HttpObjectDecoder() { protected HttpObjectDecoder() {
this(4096, 8192, 8192, true); this(DEFAULT_MAX_INITIAL_LINE_LENGTH, DEFAULT_MAX_HEADER_SIZE, DEFAULT_MAX_CHUNK_SIZE,
DEFAULT_CHUNKED_SUPPORTED);
} }
/** /**
@ -155,7 +163,7 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
*/ */
protected HttpObjectDecoder( protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean chunkedSupported) { int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, boolean chunkedSupported) {
this(maxInitialLineLength, maxHeaderSize, maxChunkSize, chunkedSupported, true); this(maxInitialLineLength, maxHeaderSize, maxChunkSize, chunkedSupported, DEFAULT_VALIDATE_HEADERS);
} }
/** /**
@ -164,7 +172,8 @@ public abstract class HttpObjectDecoder extends ByteToMessageDecoder {
protected HttpObjectDecoder( protected HttpObjectDecoder(
int maxInitialLineLength, int maxHeaderSize, int maxChunkSize, int maxInitialLineLength, int maxHeaderSize, int maxChunkSize,
boolean chunkedSupported, boolean validateHeaders) { boolean chunkedSupported, boolean validateHeaders) {
this(maxInitialLineLength, maxHeaderSize, maxChunkSize, chunkedSupported, validateHeaders, 128); this(maxInitialLineLength, maxHeaderSize, maxChunkSize, chunkedSupported, validateHeaders,
DEFAULT_INITIAL_BUFFER_SIZE);
} }
protected HttpObjectDecoder( protected HttpObjectDecoder(

View File

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

View File

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

View File

@ -23,6 +23,10 @@ import java.util.ArrayDeque;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
import static io.netty.handler.codec.http.HttpObjectDecoder.DEFAULT_MAX_CHUNK_SIZE;
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} * A combination of {@link HttpRequestDecoder} and {@link HttpResponseEncoder}
* which enables easier server side HTTP implementation. * which enables easier server side HTTP implementation.
@ -41,7 +45,7 @@ public final class HttpServerCodec extends CombinedChannelDuplexHandler<HttpRequ
* {@code maxChunkSize (8192)}). * {@code maxChunkSize (8192)}).
*/ */
public HttpServerCodec() { public HttpServerCodec() {
this(4096, 8192, 8192); this(DEFAULT_MAX_INITIAL_LINE_LENGTH, DEFAULT_MAX_HEADER_SIZE, DEFAULT_MAX_CHUNK_SIZE);
} }
/** /**

View File

@ -71,16 +71,6 @@ public class RtspDecoder extends HttpObjectDecoder {
*/ */
private static final Pattern versionPattern = Pattern.compile("RTSP/\\d\\.\\d"); 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. * Constant for default max content length.
*/ */

View File

@ -20,6 +20,8 @@ import io.netty.handler.codec.TooLongFrameException;
import io.netty.handler.codec.http.HttpMessage; import io.netty.handler.codec.http.HttpMessage;
import io.netty.handler.codec.http.HttpObjectDecoder; import io.netty.handler.codec.http.HttpObjectDecoder;
import static io.netty.handler.codec.rtsp.RtspDecoder.DEFAULT_MAX_CONTENT_LENGTH;
/** /**
* Decodes {@link ByteBuf}s into RTSP messages represented in * Decodes {@link ByteBuf}s into RTSP messages represented in
* {@link HttpMessage}s. * {@link HttpMessage}s.
@ -59,7 +61,7 @@ public abstract class RtspObjectDecoder extends HttpObjectDecoder {
* {@code maxContentLength (8192)}. * {@code maxContentLength (8192)}.
*/ */
protected RtspObjectDecoder() { protected RtspObjectDecoder() {
this(4096, 8192, 8192); this(DEFAULT_MAX_INITIAL_LINE_LENGTH, DEFAULT_MAX_HEADER_SIZE, DEFAULT_MAX_CONTENT_LENGTH);
} }
/** /**