Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Redo #150 http codec config on 0.7 #263

Merged
merged 2 commits into from
Jan 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/main/java/reactor/ipc/netty/http/server/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,12 @@ protected ContextHandler<Channel> doHandler(

@Override
public void accept(ChannelPipeline p, ContextHandler<Channel> c) {
p.addLast(NettyPipeline.HttpCodec, new HttpServerCodec());
p.addLast(NettyPipeline.HttpCodec, new HttpServerCodec(
options.httpCodecMaxInitialLineLength(),
options.httpCodecMaxHeaderSize(),
options.httpCodecMaxChunkSize(),
options.httpCodecValidateHeaders(),
options.httpCodecInitialBufferSize()));

if(options.minCompressionResponseSize() >= 0) {
p.addLast(NettyPipeline.CompressionHandler, new CompressionHandler(options.minCompressionResponseSize()));
Expand Down
153 changes: 152 additions & 1 deletion src/main/java/reactor/ipc/netty/http/server/HttpServerOptions.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,20 @@ public static HttpServerOptions.Builder builder() {
}

private final int minCompressionResponseSize;
private final int maxInitialLineLength;
private final int maxHeaderSize;
private final int maxChunkSize;
private final int initialBufferSize;
private final boolean validateHeaders;

private HttpServerOptions(HttpServerOptions.Builder builder) {
super(builder);
this.minCompressionResponseSize = builder.minCompressionResponseSize;
this.maxInitialLineLength = builder.maxInitialLineLength;
this.maxHeaderSize = builder.maxHeaderSize;
this.maxChunkSize = builder.maxChunkSize;
this.validateHeaders = builder.validateHeaders;
this.initialBufferSize = builder.initialBufferSize;
}

/**
Expand All @@ -54,6 +64,55 @@ public int minCompressionResponseSize() {
return minCompressionResponseSize;
}

/**
* Returns the maximum length configured for the initial HTTP line.
*
* @return the initial HTTP line maximum length
* @see io.netty.handler.codec.http.HttpServerCodec
*/
public int httpCodecMaxInitialLineLength() {
return maxInitialLineLength;
}

/**
* Returns the configured HTTP header maximum size.
*
* @return the configured HTTP header maximum size
* @see io.netty.handler.codec.http.HttpServerCodec
*/
public int httpCodecMaxHeaderSize() {
return maxHeaderSize;
}

/**
* Returns the configured HTTP chunk maximum size.
*
* @return the configured HTTP chunk maximum size
* @see io.netty.handler.codec.http.HttpServerCodec
*/
public int httpCodecMaxChunkSize() {
return maxChunkSize;
}

/**
* Returns the HTTP validate headers flag.
*
* @return true if the HTTP codec validates headers, false otherwise
* @see io.netty.handler.codec.http.HttpServerCodec
*/
public boolean httpCodecValidateHeaders() {
return validateHeaders;
}
/**
* Returns the configured HTTP codec initial buffer size.
*
* @return the configured HTTP codec initial buffer size
* @see io.netty.handler.codec.http.HttpServerCodec
*/
public int httpCodecInitialBufferSize() {
return initialBufferSize;
}

@Override
public HttpServerOptions duplicate() {
return builder().from(this).build();
Expand All @@ -76,7 +135,9 @@ public String asSimpleString() {
@Override
public String asDetailedString() {
return super.asDetailedString() +
", minCompressionResponseSize=" + minCompressionResponseSize;
", minCompressionResponseSize=" + minCompressionResponseSize +
", httpCodecSizes={initialLine=" + this.maxInitialLineLength +
",header=" + this.maxHeaderSize + ",chunk="+ this.maxChunkSize + "}";
}

@Override
Expand All @@ -85,7 +146,19 @@ public String toString() {
}

public static final class Builder extends ServerOptions.Builder<Builder> {

public static final int DEFAULT_MAX_INITIAL_LINE_LENGTH = 4096;
public static final int DEFAULT_MAX_HEADER_SIZE = 8192;
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 int minCompressionResponseSize = -1;
private int maxInitialLineLength = DEFAULT_MAX_INITIAL_LINE_LENGTH;
private int maxHeaderSize = DEFAULT_MAX_HEADER_SIZE;
private int maxChunkSize = DEFAULT_MAX_CHUNK_SIZE;
private boolean validateHeaders = DEFAULT_VALIDATE_HEADERS;
private int initialBufferSize = DEFAULT_INITIAL_BUFFER_SIZE;

private Builder(){
super(new ServerBootstrap());
Expand Down Expand Up @@ -120,6 +193,79 @@ public final Builder compression(int minResponseSize) {
return get();
}

/**
* Configure the maximum length that can be decoded for the HTTP request's initial
* line. Defaults to {@code #DEFAULT_MAX_INITIAL_LINE_LENGTH}.
*
* @param value the value for the maximum initial line length (strictly positive)
* @return this option builder for further configuration
*/
public final Builder maxInitialLineLength(int value) {
if (value <= 0) {
throw new IllegalArgumentException(
"maxInitialLineLength must be strictly positive");
}
this.maxInitialLineLength = value;
return get();
}

/**
* Configure the maximum header size that can be decoded for the HTTP request.
* Defaults to {@link #DEFAULT_MAX_HEADER_SIZE}.
*
* @param value the value for the maximum header size (strictly positive)
* @return this option builder for further configuration
*/
public final Builder maxHeaderSize(int value) {
if (value <= 0) {
throw new IllegalArgumentException("maxHeaderSize must be strictly positive");
}
this.maxHeaderSize = value;
return get();
}

/**
* Configure the maximum chunk size that can be decoded for the HTTP request.
* Defaults to {@link #DEFAULT_MAX_CHUNK_SIZE}.
*
* @param value the value for the maximum chunk size (strictly positive)
* @return this option builder for further configuration
*/
public final Builder maxChunkSize(int value) {
if (value <= 0) {
throw new IllegalArgumentException("maxChunkSize must be strictly positive");
}
this.maxChunkSize = value;
return get();
}

/**
* Configure whether or not to validate headers when decoding requests. Defaults to
* #DEFAULT_VALIDATE_HEADERS.
*
* @param validate true to validate headers, false otherwise
* @return this option builder for further configuration
*/
public final Builder validateHeaders(boolean validate) {
this.validateHeaders = validate;
return get();
}

/**
* Configure the initial buffer size for HTTP request decoding. Defaults to
* {@link #DEFAULT_INITIAL_BUFFER_SIZE}.
*
* @param value the initial buffer size to use (strictly positive)
* @return
*/
public final Builder initialBufferSize(int value) {
if (value <= 0) {
throw new IllegalArgumentException("initialBufferSize must be strictly positive");
}
this.initialBufferSize = value;
return get();
}

/**
* Fill the builder with attribute values from the provided options.
*
Expand All @@ -129,6 +275,11 @@ public final Builder compression(int minResponseSize) {
public final Builder from(HttpServerOptions options) {
super.from(options);
this.minCompressionResponseSize = options.minCompressionResponseSize;
this.maxInitialLineLength = options.maxInitialLineLength;
this.maxHeaderSize = options.maxHeaderSize;
this.maxChunkSize = options.maxChunkSize;
this.validateHeaders = options.validateHeaders;
this.initialBufferSize = options.initialBufferSize;
return get();
}

Expand Down
Loading