Allow manual configuration of initial HTTP/2 SETTINGS frame.

Motivation:

There is currently no good way to configure the initial SETTINGS frame. The individual settings can be configured on the various components, but doing this bypasses the proper setting update logic in the encoder.

Modifications:

Updated Http2ConnectionHandler to optionally take initial settings in the constructor. If not provided, it will default to current behavior.

Result:

Easy manual configuration of initial settings.
This commit is contained in:
nmittler 2015-05-21 12:12:29 -07:00
parent 8fe6574df0
commit 210baee6bb

View File

@ -52,6 +52,7 @@ public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http
private static final InternalLogger logger = InternalLoggerFactory.getInstance(Http2ConnectionHandler.class); private static final InternalLogger logger = InternalLoggerFactory.getInstance(Http2ConnectionHandler.class);
private final Http2ConnectionDecoder decoder; private final Http2ConnectionDecoder decoder;
private final Http2ConnectionEncoder encoder; private final Http2ConnectionEncoder encoder;
private final Http2Settings initialSettings;
private ChannelFutureListener closeListener; private ChannelFutureListener closeListener;
private BaseDecoder byteDecoder; private BaseDecoder byteDecoder;
@ -65,6 +66,7 @@ public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http
public Http2ConnectionHandler(Http2Connection connection, Http2FrameReader frameReader, public Http2ConnectionHandler(Http2Connection connection, Http2FrameReader frameReader,
Http2FrameWriter frameWriter, Http2FrameListener listener) { Http2FrameWriter frameWriter, Http2FrameListener listener) {
initialSettings = null;
encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter); encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter);
decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, frameReader, listener); decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, frameReader, listener);
} }
@ -75,6 +77,32 @@ public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http
*/ */
public Http2ConnectionHandler(Http2ConnectionDecoder decoder, public Http2ConnectionHandler(Http2ConnectionDecoder decoder,
Http2ConnectionEncoder encoder) { Http2ConnectionEncoder encoder) {
this.initialSettings = null;
this.decoder = checkNotNull(decoder, "decoder");
this.encoder = checkNotNull(encoder, "encoder");
if (encoder.connection() != decoder.connection()) {
throw new IllegalArgumentException("Encoder and Decoder do not share the same connection object");
}
}
public Http2ConnectionHandler(Http2Connection connection, Http2FrameListener listener,
Http2Settings initialSettings) {
this(connection, new DefaultHttp2FrameReader(), new DefaultHttp2FrameWriter(), listener,
initialSettings);
}
public Http2ConnectionHandler(Http2Connection connection, Http2FrameReader frameReader,
Http2FrameWriter frameWriter, Http2FrameListener listener,
Http2Settings initialSettings) {
this.initialSettings = initialSettings;
encoder = new DefaultHttp2ConnectionEncoder(connection, frameWriter);
decoder = new DefaultHttp2ConnectionDecoder(connection, encoder, frameReader, listener);
}
public Http2ConnectionHandler(Http2ConnectionDecoder decoder,
Http2ConnectionEncoder encoder,
Http2Settings initialSettings) {
this.initialSettings = initialSettings;
this.decoder = checkNotNull(decoder, "decoder"); this.decoder = checkNotNull(decoder, "decoder");
this.encoder = checkNotNull(encoder, "encoder"); this.encoder = checkNotNull(encoder, "encoder");
if (encoder.connection() != decoder.connection()) { if (encoder.connection() != decoder.connection()) {
@ -275,7 +303,7 @@ public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http
} }
// Both client and server must send their initial settings. // Both client and server must send their initial settings.
encoder.writeSettings(ctx, decoder.localSettings(), ctx.newPromise()).addListener( encoder.writeSettings(ctx, initialSettings(), ctx.newPromise()).addListener(
ChannelFutureListener.CLOSE_ON_FAILURE); ChannelFutureListener.CLOSE_ON_FAILURE);
} }
} }
@ -556,6 +584,13 @@ public class Http2ConnectionHandler extends ByteToMessageDecoder implements Http
} }
} }
/**
* Gets the initial settings to be sent to the remote endpoint.
*/
private Http2Settings initialSettings() {
return initialSettings != null ? initialSettings : decoder.localSettings();
}
/** /**
* Close the remote endpoint with with a {@code GO_AWAY} frame. Does <strong>not</strong> flush * Close the remote endpoint with with a {@code GO_AWAY} frame. Does <strong>not</strong> flush
* immediately, this is the responsibility of the caller. * immediately, this is the responsibility of the caller.