Skip to content

Commit

Permalink
fix #150 Add a configuration API for HttpCodec
Browse files Browse the repository at this point in the history
This commits allows to configure Netty's HttpServerCodec 5 options for
request decoding, through an HttpServer API that consumes a small simple
builder which holds the parameters. Parameters are then injected as
Channel attributes.

This would notably allow the codec to decode requests on very long URIs.
  • Loading branch information
simonbasle committed Jan 9, 2018
1 parent 3bf7341 commit 721bd4b
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
/*
* Copyright (c) 2011-2018 Pivotal Software Inc, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.ipc.netty.http.server;

import java.util.function.Function;

import io.netty.channel.Channel;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.util.Attribute;
import io.netty.util.AttributeKey;
import reactor.ipc.netty.tcp.TcpServer;

/**
* A configuration builder to fine tune the {@link io.netty.handler.codec.http.HttpServerCodec}
* (or more precisely the {@link io.netty.handler.codec.http.HttpServerCodec.HttpServerRequestDecoder}).
* <p>
* Defaults are accessible as constants {@link #DEFAULT_MAX_INITIAL_LINE_LENGTH}, {@link #DEFAULT_MAX_HEADER_SIZE}
* and {@link #DEFAULT_MAX_CHUNK_SIZE}.
*
* @author Simon Baslé
*/
public final class HttpRequestDecoderConfiguration {

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;

static final AttributeKey<Integer> MAX_INITIAL_LINE_LENGTH = AttributeKey.newInstance("httpCodecMaxInitialLineLength");
static final AttributeKey<Integer> MAX_HEADER_SIZE = AttributeKey.newInstance("httpCodecMaxHeaderSize");
static final AttributeKey<Integer> MAX_CHUNK_SIZE = AttributeKey.newInstance("httpCodecMaxChunkSize");
static final AttributeKey<Boolean> VALIDATE_HEADERS = AttributeKey.newInstance("httpCodecValidateHeaders");
static final AttributeKey<Integer> INITIAL_BUFFER_SIZE = AttributeKey.newInstance("httpCodecInitialBufferSize");

int maxInitialLineLength = DEFAULT_MAX_INITIAL_LINE_LENGTH;
int maxHeaderSize = DEFAULT_MAX_HEADER_SIZE;
int maxChunkSize = DEFAULT_MAX_CHUNK_SIZE;
boolean validateHeaders = DEFAULT_VALIDATE_HEADERS;
int initialBufferSize = DEFAULT_INITIAL_BUFFER_SIZE;

/**
* Configure the maximum length that can be decoded for the HTTP request's initial
* line. Defaults to {@link #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 HttpRequestDecoderConfiguration maxInitialLineLength(int value) {
if (value <= 0) {
throw new IllegalArgumentException(
"maxInitialLineLength must be strictly positive");
}
this.maxInitialLineLength = value;
return this;
}

/**
* 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 HttpRequestDecoderConfiguration maxHeaderSize(int value) {
if (value <= 0) {
throw new IllegalArgumentException("maxHeaderSize must be strictly positive");
}
this.maxHeaderSize = value;
return this;
}

/**
* 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 HttpRequestDecoderConfiguration maxChunkSize(int value) {
if (value <= 0) {
throw new IllegalArgumentException("maxChunkSize must be strictly positive");
}
this.maxChunkSize = value;
return this;
}

/**
* 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 HttpRequestDecoderConfiguration validateHeaders(boolean validate) {
this.validateHeaders = validate;
return this;
}

/**
* 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 HttpRequestDecoderConfiguration initialBufferSize(int value) {
if (value <= 0) {
throw new IllegalArgumentException("initialBufferSize must be strictly positive");
}
this.initialBufferSize = value;
return this;
}

/**
* Build a {@link Function} that applies the http request decoder configuration to a
* {@link TcpServer} by enriching its attributes.
*/
Function<TcpServer, TcpServer> build() {
return tcp -> tcp.attr(MAX_INITIAL_LINE_LENGTH, maxInitialLineLength)
.attr(MAX_HEADER_SIZE, maxHeaderSize)
.attr(MAX_CHUNK_SIZE, maxChunkSize)
.attr(VALIDATE_HEADERS, validateHeaders)
.attr(INITIAL_BUFFER_SIZE, initialBufferSize);
}

/**
* Create a {@link HttpServerCodec} using the request decoder configuration attributes
* that would be configured through a {@link HttpRequestDecoderConfiguration}.
*
* @param channel the {@link Channel} on which the attributes have been registered.
* @return a new {@link HttpServerCodec}
*/
public static HttpServerCodec serverCodecFromAttributes(Channel channel) {
Attribute<Integer> lineAttr = channel.attr(MAX_INITIAL_LINE_LENGTH);
int line;
if (lineAttr == null || lineAttr.get() == null) line = DEFAULT_MAX_INITIAL_LINE_LENGTH;
else line = lineAttr.get();

Attribute<Integer> headerAttr = channel.attr(MAX_HEADER_SIZE);
int header;
if (headerAttr == null || headerAttr.get() == null) header = DEFAULT_MAX_HEADER_SIZE;
else header = headerAttr.get();

Attribute<Integer> chunkAttr = channel.attr(MAX_CHUNK_SIZE);
int chunk;
if (chunkAttr == null || chunkAttr.get() == null) chunk = DEFAULT_MAX_CHUNK_SIZE;
else chunk = chunkAttr.get();

Attribute<Boolean> validateAttr = channel.attr(VALIDATE_HEADERS);
boolean validate;
if (validateAttr == null || validateAttr.get() == null) validate = DEFAULT_VALIDATE_HEADERS;
else validate = validateAttr.get();

Attribute<Integer> bufferAttr = channel.attr(INITIAL_BUFFER_SIZE);
int buffer;
if (bufferAttr == null || bufferAttr.get() == null) buffer = DEFAULT_INITIAL_BUFFER_SIZE;
else buffer = bufferAttr.get();

return new HttpServerCodec(line, header, chunk, validate, buffer);
}

}
12 changes: 12 additions & 0 deletions src/main/java/reactor/ipc/netty/http/server/HttpServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,18 @@ public final HttpServer noCompression() {
return tcpConfiguration(COMPRESS_ATTR_DISABLE);
}

/**
* Configure the {@link io.netty.handler.codec.http.HttpServerCodec}'s request decoding options.
*
* @param requestDecoderOptions a function to mutate the provided Http request decoder options
* @return a new {@link HttpServer}
*/
public final HttpServer httpRequestDecoder(Function<HttpRequestDecoderConfiguration, HttpRequestDecoderConfiguration> requestDecoderOptions) {
return tcpConfiguration(
requestDecoderOptions.apply(new HttpRequestDecoderConfiguration())
.build());
}

/**
* Apply {@link ServerBootstrap} configuration given mapper taking currently
* configured one and returning a new one to be ultimately used for socket binding.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package reactor.ipc.netty.http.server;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.EventLoopGroup;
import io.netty.handler.codec.http.HttpServerCodec;
Expand Down Expand Up @@ -70,7 +71,8 @@ public ServerBootstrap configure(ServerBootstrap b) {
BootstrapHandlers.updateConfiguration(b, NettyPipeline.HttpInitializer, (ctx, channel) -> {
ChannelPipeline p = channel.pipeline();

p.addLast(NettyPipeline.HttpCodec, new HttpServerCodec());
HttpServerCodec codec = HttpRequestDecoderConfiguration.serverCodecFromAttributes(channel);
p.addLast(NettyPipeline.HttpCodec, codec);

Attribute<Integer> minCompressionSize = channel.attr(HttpServerOperations.PRODUCE_GZIP);
if(minCompressionSize != null &&
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
* Copyright (c) 2011-2018 Pivotal Software Inc, All Rights Reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package reactor.ipc.netty.http.server;

import org.junit.Before;
import org.junit.Test;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;

public class HttpRequestDecoderConfigurationTest {

private HttpRequestDecoderConfiguration conf;

@Before
public void init() {
conf = new HttpRequestDecoderConfiguration();
}

@Test
public void maxInitialLineLength() {
conf.maxInitialLineLength(123);

assertThat(conf.maxInitialLineLength).as("initial line length").isEqualTo(123);

assertThat(conf.maxHeaderSize).as("default header size").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_MAX_HEADER_SIZE);
assertThat(conf.maxChunkSize).as("default chunk size").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_MAX_CHUNK_SIZE);
assertThat(conf.validateHeaders).as("default validate headers").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_VALIDATE_HEADERS);
assertThat(conf.initialBufferSize).as("default initial buffer sizez").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_INITIAL_BUFFER_SIZE);
}

@Test
public void maxInitialLineLengthBadValues() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> conf.maxInitialLineLength(0))
.as("rejects 0")
.withMessage("maxInitialLineLength must be strictly positive");

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> conf.maxInitialLineLength(-1))
.as("rejects negative")
.withMessage("maxInitialLineLength must be strictly positive");
}

@Test
public void maxHeaderSize() {
conf.maxHeaderSize(123);

assertThat(conf.maxHeaderSize).as("header size").isEqualTo(123);

assertThat(conf.maxInitialLineLength).as("default initial line length").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_MAX_INITIAL_LINE_LENGTH);
assertThat(conf.maxChunkSize).as("default chunk size").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_MAX_CHUNK_SIZE);
assertThat(conf.validateHeaders).as("default validate headers").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_VALIDATE_HEADERS);
assertThat(conf.initialBufferSize).as("default initial buffer sizez").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_INITIAL_BUFFER_SIZE);
}

@Test
public void maxHeaderSizeBadValues() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> conf.maxHeaderSize(0))
.as("rejects 0")
.withMessage("maxHeaderSize must be strictly positive");

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> conf.maxHeaderSize(-1))
.as("rejects negative")
.withMessage("maxHeaderSize must be strictly positive");
}

@Test
public void maxChunkSize() {
conf.maxChunkSize(123);

assertThat(conf.maxChunkSize).as("chunk size").isEqualTo(123);

assertThat(conf.maxInitialLineLength).as("default initial line length").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_MAX_INITIAL_LINE_LENGTH);
assertThat(conf.maxHeaderSize).as("default header size").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_MAX_HEADER_SIZE);
assertThat(conf.validateHeaders).as("default validate headers").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_VALIDATE_HEADERS);
assertThat(conf.initialBufferSize).as("default initial buffer sizez").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_INITIAL_BUFFER_SIZE);
}

@Test
public void maxChunkSizeBadValues() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> conf.maxChunkSize(0))
.as("rejects 0")
.withMessage("maxChunkSize must be strictly positive");

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> conf.maxChunkSize(-1))
.as("rejects negative")
.withMessage("maxChunkSize must be strictly positive");
}

@Test
public void validateHeaders() {
conf.validateHeaders(false);

assertThat(conf.validateHeaders).as("validate headers").isFalse();

assertThat(conf.maxInitialLineLength).as("default initial line length").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_MAX_INITIAL_LINE_LENGTH);
assertThat(conf.maxHeaderSize).as("default header size").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_MAX_HEADER_SIZE);
assertThat(conf.maxChunkSize).as("default chunk size").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_MAX_CHUNK_SIZE);
assertThat(conf.initialBufferSize).as("default initial buffer sizez").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_INITIAL_BUFFER_SIZE);
}

@Test
public void initialBufferSize() {
conf.initialBufferSize(123);

assertThat(conf.initialBufferSize).as("initial buffer size").isEqualTo(123);

assertThat(conf.maxInitialLineLength).as("default initial line length").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_MAX_INITIAL_LINE_LENGTH);
assertThat(conf.maxHeaderSize).as("default header size").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_MAX_HEADER_SIZE);
assertThat(conf.maxChunkSize).as("default chunk size").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_MAX_CHUNK_SIZE);
assertThat(conf.validateHeaders).as("default validate headers").isEqualTo(HttpRequestDecoderConfiguration.DEFAULT_VALIDATE_HEADERS);
}

@Test
public void initialBufferSizeBadValues() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> conf.initialBufferSize(0))
.as("rejects 0")
.withMessage("initialBufferSize must be strictly positive");

assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> conf.initialBufferSize(-1))
.as("rejects negative")
.withMessage("initialBufferSize must be strictly positive");
}
}
Loading

0 comments on commit 721bd4b

Please sign in to comment.